From 4b9f050c2acab536356342ab96e6cc76c281ac24 Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Tue, 5 Nov 2013 23:29:12 +0000 Subject: [ASan] Make sure slow stack unwinder doesn't return empty stacks. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@194107 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../lit_tests/TestCases/malloc_context_size.cc | 19 ++++++++++++++++ lib/sanitizer_common/sanitizer_linux_libcdep.cc | 26 +++++++++++----------- 2 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 lib/asan/lit_tests/TestCases/malloc_context_size.cc diff --git a/lib/asan/lit_tests/TestCases/malloc_context_size.cc b/lib/asan/lit_tests/TestCases/malloc_context_size.cc new file mode 100644 index 00000000..7449dbe2 --- /dev/null +++ b/lib/asan/lit_tests/TestCases/malloc_context_size.cc @@ -0,0 +1,19 @@ +// RUN: %clangxx_asan -O0 %s -o %t +// RUN: ASAN_OPTIONS=malloc_context_size=0:fast_unwind_on_malloc=0 not %t 2>&1 | FileCheck %s +// RUN: ASAN_OPTIONS=malloc_context_size=0:fast_unwind_on_malloc=1 not %t 2>&1 | FileCheck %s +// RUN: ASAN_OPTIONS=malloc_context_size=1:fast_unwind_on_malloc=0 not %t 2>&1 | FileCheck %s +// RUN: ASAN_OPTIONS=malloc_context_size=1:fast_unwind_on_malloc=1 not %t 2>&1 | FileCheck %s + +int main() { + char *x = new char[20]; + delete[] x; + return x[0]; + // CHECK: freed by thread T{{.*}} here: + // CHECK-NEXT: #0 0x{{.*}} in operator delete[] + // CHECK-NOT: #1 0x{{.*}} + // CHECK: previously allocated by thread T{{.*}} here: + // CHECK-NEXT: #0 0x{{.*}} in operator new[] + // CHECK-NOT: #1 0x{{.*}} + + // CHECK: SUMMARY: AddressSanitizer: heap-use-after-free +} diff --git a/lib/sanitizer_common/sanitizer_linux_libcdep.cc b/lib/sanitizer_common/sanitizer_linux_libcdep.cc index 0bc38880..d415279d 100644 --- a/lib/sanitizer_common/sanitizer_linux_libcdep.cc +++ b/lib/sanitizer_common/sanitizer_linux_libcdep.cc @@ -160,19 +160,19 @@ static bool MatchPc(uptr cur_pc, uptr trace_pc) { void StackTrace::SlowUnwindStack(uptr pc, uptr max_depth) { size = 0; - UnwindTraceArg arg = {this, max_depth}; - if (max_depth > 1) { - _Unwind_Backtrace(Unwind_Trace, &arg); - // We need to pop a few frames so that pc is on top. - // trace[0] belongs to the current function so we always pop it. - int to_pop = 1; - /**/ if (size > 1 && MatchPc(pc, trace[1])) to_pop = 1; - else if (size > 2 && MatchPc(pc, trace[2])) to_pop = 2; - else if (size > 3 && MatchPc(pc, trace[3])) to_pop = 3; - else if (size > 4 && MatchPc(pc, trace[4])) to_pop = 4; - else if (size > 5 && MatchPc(pc, trace[5])) to_pop = 5; - PopStackFrames(to_pop); - } + if (max_depth == 0) + return; + UnwindTraceArg arg = {this, Min(max_depth + 1, kStackTraceMax)}; + _Unwind_Backtrace(Unwind_Trace, &arg); + // We need to pop a few frames so that pc is on top. + // trace[0] belongs to the current function so we always pop it. + int to_pop = 1; + /**/ if (size > 1 && MatchPc(pc, trace[1])) to_pop = 1; + else if (size > 2 && MatchPc(pc, trace[2])) to_pop = 2; + else if (size > 3 && MatchPc(pc, trace[3])) to_pop = 3; + else if (size > 4 && MatchPc(pc, trace[4])) to_pop = 4; + else if (size > 5 && MatchPc(pc, trace[5])) to_pop = 5; + PopStackFrames(to_pop); trace[0] = pc; } -- cgit v1.2.3