3 ms·
Is there a way to detect it dynamically, e.g. by running C code under a debug mode or in an interpreter that errors out when undefined behavior is encountered?
by mjn 14y ago
Is there a way to detect it dynamically, e.g. by running C code under a debug mode or in an interpreter that errors out when undefined behavior is encountered? I've occasionally wanted to have something like that to use in tests, so I could ensure that at least my common code paths aren't relying on undefined behavior. I know about gcc's -ftrapv and a few other options, but nothing comprehensive.
- jacobparker 14y agoIn theory, for sure. Valgrind can test for certain kinds of undefined behaviour - it runs the code in a special virtual machine. You could also have the compiler insert checks. Obviously this isn't desirable for a lot of C projects by default, but (other than in places like kernel development etc.) it could be a nice debugging aid. I don't know of any good tools for doing this comprehensively.
- gsg 14y agoI've heard of several: http://embed.cs.utah.edu/ioc/ http://embed.cs.utah.edu/ioc/ http://code.google.com/p/c-semantics/ http://code.google.com/p/c-semantics/ Haven't used either in anger though.
- mjn 14y agoThanks! I'd run across the first one, but it's also only for the case of integer overflow. The 2nd is new to me, and looks quite comprehensive.
- pascal_cuoq 14y agoBesides the already mentioned: - IOC : low overhead, only for integer overflows - KCC : high overhead, for all kinds of undefined behavior, limited standard library support (and source-level only) - Valgrind : medium overhead, for various memory errors, binary, may fail to detect undefined behaviors that have been made undetectable by compilation. You may also find: - various memory-safe C compilers. There are plenty here, I had better let you do the googling. medium overhead, generally better than Valgrind at being sound (since they work at source level), unless they trade efficiency for soundness: http://research.microsoft.com/pubs/101450/baggy-usenix2009.pdf http://research.microsoft.com/pubs/101450/baggy-usenix2009.p... . May require all source code to be available. - Frama-C's value analysis, a static analyzer that can be used as a C interpreter. This is what I work on. Limitations comparable to KCC, quite a bit faster (but still high overhead), some slightly different design choices. I do not have a good single write-up for this use, but some details are available at these URLs: http://blog.frama-c.com/public/csmith.pdf http://blog.frama-c.com/public/csmith.pdf http://blog.frama-c.com/index.php?post/2011/08/29/CompCert-gets-a-safe-interpreter-mode http://blog.frama-c.com/index.php?post/2011/08/29/CompCert-g...