diff options
author | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2016-04-20 22:45:23 +0000 |
---|---|---|
committer | Evgeniy Stepanov <eugeni.stepanov@gmail.com> | 2016-04-20 22:45:23 +0000 |
commit | b69978df2c04e4e06e3ed7ed7c0c1f471738b365 (patch) | |
tree | 0dfdd8bfa4b2eb57bfbfeb284e84dc226ac414e5 | |
parent | 656c2db9b771f78d4ba5afe565e51d8e8b1a04dd (diff) |
[asan] Add __strdup interceptor.
This happens on Linux when building as C (not C++) with optimization.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@266931 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/asan/asan_interceptors.cc | 20 | ||||
-rw-r--r-- | lib/asan/asan_interceptors.h | 6 | ||||
-rw-r--r-- | test/asan/TestCases/strdup_oob_test.cc | 6 |
3 files changed, 31 insertions, 1 deletions
diff --git a/lib/asan/asan_interceptors.cc b/lib/asan/asan_interceptors.cc index 0f2ea5c8e..859af4a62 100644 --- a/lib/asan/asan_interceptors.cc +++ b/lib/asan/asan_interceptors.cc @@ -563,6 +563,23 @@ INTERCEPTOR(char*, strdup, const char *s) { return reinterpret_cast<char*>(new_mem); } +#if ASAN_INTERCEPT___STRDUP +INTERCEPTOR(char*, __strdup, const char *s) { + void *ctx; + ASAN_INTERCEPTOR_ENTER(ctx, strdup); + if (UNLIKELY(!asan_inited)) return internal_strdup(s); + ENSURE_ASAN_INITED(); + uptr length = REAL(strlen)(s); + if (flags()->replace_str) { + ASAN_READ_RANGE(ctx, s, length + 1); + } + 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 // ASAN_INTERCEPT___STRDUP + INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, wcslen); @@ -719,6 +736,9 @@ void InitializeAsanInterceptors() { ASAN_INTERCEPT_FUNC(strncat); ASAN_INTERCEPT_FUNC(strncpy); ASAN_INTERCEPT_FUNC(strdup); +#if ASAN_INTERCEPT___STRDUP + ASAN_INTERCEPT_FUNC(__strdup); +#endif #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX ASAN_INTERCEPT_FUNC(index); #endif diff --git a/lib/asan/asan_interceptors.h b/lib/asan/asan_interceptors.h index d4edc1351..d747c31a5 100644 --- a/lib/asan/asan_interceptors.h +++ b/lib/asan/asan_interceptors.h @@ -72,6 +72,12 @@ # define ASAN_INTERCEPT___CXA_ATEXIT 0 #endif +#if SANITIZER_LINUX && !SANITIZER_ANDROID +# define ASAN_INTERCEPT___STRDUP 1 +#else +# define ASAN_INTERCEPT___STRDUP 0 +#endif + DECLARE_REAL(int, memcmp, const void *a1, const void *a2, uptr size) DECLARE_REAL(void*, memcpy, void *to, const void *from, uptr size) DECLARE_REAL(void*, memset, void *block, int c, uptr size) diff --git a/test/asan/TestCases/strdup_oob_test.cc b/test/asan/TestCases/strdup_oob_test.cc index a039568b2..dc9eec803 100644 --- a/test/asan/TestCases/strdup_oob_test.cc +++ b/test/asan/TestCases/strdup_oob_test.cc @@ -3,6 +3,9 @@ // RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s // RUN: %clangxx_asan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s +// When built as C on Linux, strdup is transformed to __strdup. +// RUN: %clangxx_asan -O3 -xc %s -o %t && not %run %t 2>&1 | FileCheck %s + #include <string.h> char kString[] = "foo"; @@ -14,7 +17,8 @@ int main(int argc, char **argv) { // CHECK: #0 {{.*}}main {{.*}}strdup_oob_test.cc:[[@LINE-2]] // CHECK-LABEL: allocated by thread T{{.*}} here: // CHECK: #{{[01]}} {{.*}}strdup + // CHECK: #{{.*}}main {{.*}}strdup_oob_test.cc:[[@LINE-6]] // CHECK-LABEL: SUMMARY - // CHECK: strdup_oob_test.cc:[[@LINE-6]] + // CHECK: strdup_oob_test.cc:[[@LINE-7]] return x; } |