implement object size checking for clang
| Bugzilla Link | 5028 |
| Version | unspecified |
| OS | All |
| Reporter | LLVM Bugzilla Contributor |
| CC | @asl,@echristo,@belkadan,@tkremenek |
Extended Description
Summary: the compile-time and run-time safety tests enabled by use of the _FORTIFY_SOURCE macro are not included when compiling with LLVM-clang or LLVM-GCC.
Steps to reproduce: take the following source code, and compile with the version of llvm-gcc or clang from /Developer/usr/bin in an Xcode 3.2 installation on Snow Leopard.
#include <stdio.h> #include <string.h>
int main (int argc, const char * argv[]) { char buffer[16]; strcpy(buffer, "hello"); strcpy(buffer, "This sentence is much too long for you."); strcpy(buffer, argv[1]); return 0; }
Expected results: a warning is emitted by the compiler as it would be if compiled with GCC, due to the statically-testable buffer overrun at the second strcpy() invocation. If the compiled program is run, it should abort inside the libc overrun-check function.
Actual results: no warning is emitted. The program crashes when the stack is smashed.
strcpy(buffer, "This sentence is much too long for you.");
This particular example now gets an analyzer warning, based on the last bit of my GSoC work from last summer. (The checks are in CStringChecker.)
strcpy(buffer, argv[1]);
The analyzer can't /prove/ this will cause an overflow, so currently it doesn't warn. That's pretty much in keeping with the analyzer's "don't cry wolf" principle, but might be less than ideal; maybe the real answer is just to mark strcpy() as deprecated. ;-)
CStringChecker currently only supports memcpy() and strcpy() and a few variants, though. It wouldn't be too hard to add at least minimal support for most of the other _FORTIFY_SOURCE functions (though the printf family might be tricky). That might deserve a different bug, though.
No compile time warnings, but the runtime safety checks are there. I think we were leaning on static analysis for the compile time side of these checks since we don't want the optimizer emitting warnings.
Eric, is this done?
We now have codegen in r85324. I'll leave this open for the static analysis part, which will give nice pretty warnings.
I've also logged this in radar at rdar://problem/7242637
Correct, clang does not yet has support for object size checking. Related: rdar://problem/6194158 [irgen] Implement object size checking
assigned to @tkremenek