[ASan] reimplement strdup() interceptor to get nicer stack traces for memory chunks allocated there

llvm-svn: 184546
This commit is contained in:
Alexey Samsonov 2013-06-21 14:41:59 +00:00
parent b58b72e151
commit a9db3f9757
2 changed files with 24 additions and 10 deletions

View File

@ -461,21 +461,16 @@ INTERCEPTOR(char*, strcpy, char *to, const char *from) { // NOLINT
#if ASAN_INTERCEPT_STRDUP
INTERCEPTOR(char*, strdup, const char *s) {
#if SANITIZER_MAC
// FIXME: because internal_strdup() uses InternalAlloc(), which currently
// just calls malloc() on Mac, we can't use internal_strdup() with the
// dynamic runtime. We can remove the call to REAL(strdup) once InternalAlloc
// starts using mmap() instead.
// See also http://code.google.com/p/address-sanitizer/issues/detail?id=123.
if (!asan_inited) return REAL(strdup)(s);
#endif
if (!asan_inited) return internal_strdup(s);
ENSURE_ASAN_INITED();
uptr length = REAL(strlen)(s);
if (flags()->replace_str) {
uptr length = REAL(strlen)(s);
ASAN_READ_RANGE(s, length + 1);
}
return REAL(strdup)(s);
GET_STACK_TRACE_MALLOC;
void *new_mem = asan_malloc(length + 1, &stack);
REAL(memcpy)(new_mem, s, length + 1);
return reinterpret_cast<char*>(new_mem);
}
#endif

View File

@ -0,0 +1,19 @@
// RUN: %clangxx_asan -O0 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
// RUN: %clangxx_asan -O1 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
// RUN: %clangxx_asan -O2 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
// RUN: %clangxx_asan -O3 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
#include <string.h>
char kString[] = "foo";
int main(int argc, char **argv) {
char *copy = strdup(kString);
int x = copy[4 + argc]; // BOOM
// CHECK: AddressSanitizer: heap-buffer-overflow
// CHECK: #0 {{.*}}main {{.*}}strdup_oob_test.cc:[[@LINE-2]]
// CHECK: allocated by thread T{{.*}} here:
// CHECK: #0 {{.*}}strdup
// CHECK: strdup_oob_test.cc:[[@LINE-6]]
return x;
}