5 ms·
Does that verifier have a strong possibility of false positives? I'm curious why C compilers have such a strong history of making reasonable checks optional an
by tyingq 7y ago
Does that verifier have a strong possibility of false positives? I'm curious why C compilers have such a strong history of making reasonable checks optional and hidden behind a bunch of switches.
- jzwinck 7y agoUBSan and ASan have essentially zero false positives. They do point out undefined behavior which happens to work on your platform, but at the very least those are still portability bugs.
- Liquid_Fire 7y agoThis is a runtime check and so has a (small) performance overhead.
- hannob 7y agoNo, actually the false positive rate of these flags is practically zero. (I'm not sure if it's 100% zero, but I used those extensively, reported many bugs and every time a developer told me "this is a false positive" they were wrong.) The reason they aren't enabled by default is that that's not what they're designed for. They have a significant performance impact, you can't enable them all at once, they conflict with other security features and they may introduce security issues. These are developer features. They aren't there to run your production code, they are there to test during development and bug finding.
- saagarjha 7y agoHere's one that I encountered that I cannot figure out: $ g++ -x c++ -fsanitize=undefined - #include <iostream> int main() { int *a = nullptr; std::cout << std::addressof(*a) << std::endl; } ^D $ ./a.out <stdin>:5:29: runtime error: reference binding to null pointer of type 'int' 0
- lallysingh 7y agoDoesn't addressof() take a reference?
- Karliss 7y agoYou are not allowed to dereference a null pointer no matter how you use it even if you later convert it back to address.
- cpdt 7y agoAnd to elaborate on how the error message relates to this: std::addressof takes a reference which also can’t be null. So when you dereference the pointer you’re actually turning it into a reference (or binding the reference to the pointer, as the error message says).
- edflsafoiewq 7y agoIn C99 however, you are allowed to do &٭a because when the operand of & is the result of the unary ٭ operator > neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue
- SAI_Peregrinus 7y agoFor anyone who wants it, this is in section 6.5.3.2 Address and indirection operators, paragraph 3.
- hvdijk 7y agoYou are allowed to dereference a null pointer, but you are not allowed to access the result or use it as an initialiser for a reference. You can only immediately discard the result, or immediately take the result's address. Using std::addressof(* a) first binds a reference, then uses that reference to take the address, hence the error. You won't get any UBSAN error with &*a.
- 7y ago
- zamalek 7y ago> every time a developer told me "this is a false positive" they were wrong This sounds like a good UB learning experience. The parent comments sounds like the author is running into UB more than they really want to.
- favorited 7y agoI know TSan has zero false-positives, because it only flags a data race when multiple threads access the same memory without synchronization (and at least one of those accesses is a write). Not sure about the other LLVM sanitizers.
- wolf550e 7y agoWhen most people who know C learned C, this technology didn't exist. Currently John Regehr teaches his students that they must use sanitizers. Note that ruining sanitizers in prod might be insecure. They're for development.
- wolf550e 7y ago*running, obviously. Too late to edit.
- flohofwoe 7y agoIn my experience both clang ASAN and UBSAN (and TSAN, the thread sanitizer) are very solid tools, IFIR I haven't seen a false positive yet (of course some code may be specifically written to rely on undefined behaviour, but of course that's a mine field). On the other hand, the clang static analyzer may have a shocking amount of messages when run first on a large existing code base, and some of those warnings can be considered more "opinions" than warnings. It's still makes sense and is very rewarding to make a code base "static analyzer clean". The runtime sanitizers in comparison are very precise and always pointed to actual "sleeper bugs", it's almost definitely a good idea to use them and take their warnings serious. But anyway, clang ASAN, UBSAN, TSAN and the static analyzer are all really excellent and important tools for everybody writing C or C++ code. PS: the reason why those checks are optional is that they increase compilation time (sometimes dramatically, like 10x slower compilation or more), and they add runtime instrumentation code which both increases the executables size and decreases performance dramatically (also 2..10x times or more, although the clang sanitizers are really quite fast compared to other solutions).
- ryandrake 7y agoA lot of developers I've worked with don't want to use these tools because they are working with already-awful legacy codebases that will emit screenfuls of (legit) positives when run, and it feels overwhelming. I remember working on some teams where all compiler warnings were turned off because if you turned on even the default ones, you'd get hundreds of thousands of them during the build, and it looked bad. Since nobody had the budget to spend any time cleaning up warnings because "customers don't care about compiler warnings" they would never get fixed. Forget even sanitizers: You wouldn't even get the splash screen displayed before ASAN would barf on you. The best projects are the ones that start out from day 1 with all the pedantic warnings turned on, warnings-as-errors, static analysis and sanitizers run as part of the automated build and any peep out of them gets treated just like an error would get treated. When you start the project out that way, there isn't that de-motivating initial hump to get over.
- pletnes 7y agoBeen there done that. One great feature I ran across was that the intel compiler has a #pragma incantation that enables many checks on a per-file basis. This lets you create new code and selectively fix old code without these obvious defects.