[Sanitizer] Simplify StackTrace::FastUnwindStack interface and fix a bug with one-frame stack traces
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@192428 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/msan/msan.cc b/lib/msan/msan.cc
index 6b52c72..aebbd05 100644
--- a/lib/msan/msan.cc
+++ b/lib/msan/msan.cc
@@ -190,10 +190,7 @@
uptr stack_top, stack_bottom;
GetCurrentStackBounds(&stack_top, &stack_bottom);
- stack->size = 0;
- stack->trace[0] = pc;
- stack->max_size = max_s;
- stack->FastUnwindStack(pc, bp, stack_top, stack_bottom);
+ stack->FastUnwindStack(pc, bp, stack_top, stack_bottom, max_s);
}
void PrintWarning(uptr pc, uptr bp) {
diff --git a/lib/sanitizer_common/sanitizer_flags.cc b/lib/sanitizer_common/sanitizer_flags.cc
index 7342261..036f97b 100644
--- a/lib/sanitizer_common/sanitizer_flags.cc
+++ b/lib/sanitizer_common/sanitizer_flags.cc
@@ -32,6 +32,10 @@
ParseFlag(str, &f->detect_leaks, "detect_leaks");
ParseFlag(str, &f->leak_check_at_exit, "leak_check_at_exit");
ParseFlag(str, &f->allocator_may_return_null, "allocator_may_return_null");
+
+ // Do a sanity check for certain flags.
+ if (f->malloc_context_size < 1)
+ f->malloc_context_size = 1;
}
static bool GetFlagValue(const char *env, const char *name,
diff --git a/lib/sanitizer_common/sanitizer_posix_libcdep.cc b/lib/sanitizer_common/sanitizer_posix_libcdep.cc
index 429f5de..24ba3ca 100644
--- a/lib/sanitizer_common/sanitizer_posix_libcdep.cc
+++ b/lib/sanitizer_common/sanitizer_posix_libcdep.cc
@@ -92,22 +92,16 @@
#ifndef SANITIZER_GO
void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp,
uptr stack_top, uptr stack_bottom, bool fast) {
-#if !SANITIZER_CAN_FAST_UNWIND
- fast = false;
-#endif
-#if SANITIZER_MAC
- // Always unwind fast on Mac.
- (void)fast;
-#else
+ // Check if fast unwind is available. Fast unwind is the only option on Mac.
+ if (!SANITIZER_CAN_FAST_UNWIND)
+ fast = false;
+ else if (SANITIZER_MAC)
+ fast = true;
+
if (!fast)
- return stack->SlowUnwindStack(pc, max_s);
-#endif // SANITIZER_MAC
- stack->size = 0;
- stack->trace[0] = pc;
- if (max_s > 1) {
- stack->max_size = max_s;
- stack->FastUnwindStack(pc, bp, stack_top, stack_bottom);
- }
+ stack->SlowUnwindStack(pc, max_s);
+ else
+ stack->FastUnwindStack(pc, bp, stack_top, stack_bottom, max_s);
}
#endif // SANITIZER_GO
diff --git a/lib/sanitizer_common/sanitizer_stacktrace.cc b/lib/sanitizer_common/sanitizer_stacktrace.cc
index e9ecfc9..a87b647 100644
--- a/lib/sanitizer_common/sanitizer_stacktrace.cc
+++ b/lib/sanitizer_common/sanitizer_stacktrace.cc
@@ -106,8 +106,14 @@
}
void StackTrace::FastUnwindStack(uptr pc, uptr bp,
- uptr stack_top, uptr stack_bottom) {
- CHECK(size == 0 && trace[0] == pc);
+ uptr stack_top, uptr stack_bottom,
+ uptr max_depth) {
+ max_size = max_depth;
+ if (max_size == 0) {
+ size = 0;
+ return;
+ }
+ trace[0] = pc;
size = 1;
uhwptr *frame = (uhwptr *)bp;
uhwptr *prev_frame = frame - 1;
diff --git a/lib/sanitizer_common/sanitizer_stacktrace.h b/lib/sanitizer_common/sanitizer_stacktrace.h
index b2e0d6b..b9ada16 100644
--- a/lib/sanitizer_common/sanitizer_stacktrace.h
+++ b/lib/sanitizer_common/sanitizer_stacktrace.h
@@ -51,7 +51,8 @@
}
}
- void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom);
+ void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom,
+ uptr max_depth);
void SlowUnwindStack(uptr pc, uptr max_depth);
void PopStackFrames(uptr count);
diff --git a/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc b/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
index 3d352cb..82c4426 100644
--- a/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
+++ b/lib/sanitizer_common/tests/sanitizer_stacktrace_test.cc
@@ -47,16 +47,11 @@
// Bottom is one slot before the start because FastUnwindStack uses >.
fake_bottom = (uptr)&fake_stack[-1];
start_pc = PC(0);
-
- // This is common setup done by __asan::GetStackTrace().
- trace.size = 0;
- trace.max_size = ARRAY_SIZE(fake_stack);
- trace.trace[0] = start_pc;
}
TEST_F(FastUnwindTest, Basic) {
trace.FastUnwindStack(start_pc, (uptr)&fake_stack[0],
- fake_top, fake_bottom);
+ fake_top, fake_bottom, kStackTraceMax);
// Should get all on-stack retaddrs and start_pc.
EXPECT_EQ(6U, trace.size);
EXPECT_EQ(start_pc, trace.trace[0]);
@@ -70,7 +65,7 @@
// Make one fp point to itself.
fake_stack[4] = (uptr)&fake_stack[4];
trace.FastUnwindStack(start_pc, (uptr)&fake_stack[0],
- fake_top, fake_bottom);
+ fake_top, fake_bottom, kStackTraceMax);
// Should get all on-stack retaddrs up to the 4th slot and start_pc.
EXPECT_EQ(4U, trace.size);
EXPECT_EQ(start_pc, trace.trace[0]);
@@ -83,7 +78,7 @@
// Make one fp misaligned.
fake_stack[4] += 3;
trace.FastUnwindStack(start_pc, (uptr)&fake_stack[0],
- fake_top, fake_bottom);
+ fake_top, fake_bottom, kStackTraceMax);
// Should get all on-stack retaddrs up to the 4th slot and start_pc.
EXPECT_EQ(4U, trace.size);
EXPECT_EQ(start_pc, trace.trace[0]);
@@ -92,5 +87,12 @@
}
}
+TEST_F(FastUnwindTest, OneFrameStackTrace) {
+ trace.FastUnwindStack(start_pc, (uptr)&fake_stack[0],
+ fake_top, fake_bottom, 1);
+ EXPECT_EQ(1U, trace.size);
+ EXPECT_EQ(1U, trace.max_size);
+ EXPECT_EQ(start_pc, trace.trace[0]);
+}
} // namespace __sanitizer