Index: runtime/vm/malloc_hooks_test.cc |
diff --git a/runtime/vm/malloc_hooks_test.cc b/runtime/vm/malloc_hooks_test.cc |
index 5054a5047dd0f09c6f4de145c68b79e9b8676740..55f1752230cf1f7efb5c081bf07818530e4aea57 100644 |
--- a/runtime/vm/malloc_hooks_test.cc |
+++ b/runtime/vm/malloc_hooks_test.cc |
@@ -6,10 +6,14 @@ |
#if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
+#include <execinfo.h> |
Cutch
2017/02/08 00:28:54
This is a cross platform test file. backtrace/exec
bkonyi
2017/02/08 01:37:05
This was more here for me to check that my impleme
zra
2017/02/08 17:42:55
Not sure how portable this is. Should this be gate
bkonyi
2017/02/09 21:22:46
Yes, it probably should. However, DART_USE_TCMALLO
|
+ |
#include "platform/assert.h" |
#include "vm/class_finalizer.h" |
#include "vm/globals.h" |
#include "vm/malloc_hooks.h" |
+#include "vm/native_symbol.h" |
+#include "vm/profiler.h" |
#include "vm/symbols.h" |
#include "vm/unit_test.h" |
@@ -75,6 +79,58 @@ UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { |
MallocHooks::TearDown(); |
} |
+// Helper used to add another frame to the stack before allocating. |
+char* AllocationHelper(void* stack_trace[32]) { |
+ backtrace(stack_trace, 32); |
+ return static_cast<char*>(malloc(16 * sizeof(char))); |
+} |
+ |
+VM_UNIT_TEST_CASE(StackTraceMallocHookTest) { |
+ MallocHooks::ResetStats(); |
+ ASSERT(OSThread::DoesCurrentThreadExist()); |
+ |
+ const intptr_t first_frame_count = 6; |
+ void* stack_trace[32]; |
+ |
+ // Perform the memory allocation and grab the stack trace created by |
+ // backtrace() for comparison purposes. |
+ char* var = AllocationHelper(stack_trace); |
+ |
+ // Update top pc of the stack trace to match the location that malloc was |
+ // called in AllocationHelper. The rest of the pcs should match without any |
+ // changes. |
+ stack_trace[0] = |
+ reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(stack_trace[0]) + 15); |
+ |
+ Sample* sample = MallocHooks::GetSample(var); |
+ ASSERT(sample != NULL); |
+ |
+ intptr_t frame_count = 0; |
+ while (sample != NULL) { |
Cutch
2017/02/08 00:28:53
This test might not be necessary. We have tests wh
bkonyi
2017/02/08 01:37:05
Makes sense. I'll remove it before actually landin
|
+ for (uintptr_t i = 0; i < 8; ++i) { |
+ // Cease comparisons after we pass the frame for main. |
+ if (sample->At(i) == 0) { |
+ break; |
+ } |
+ |
+ // The first 6 frames in the stack trace we generate are all from the |
+ // memory allocation. The frame for AllocationHelper is the 7th element, |
+ // so we skip checking all the frames before that. |
+ if (frame_count >= first_frame_count) { |
+ EXPECT_EQ(reinterpret_cast<uword>( |
+ stack_trace[frame_count - first_frame_count]), |
+ sample->At(i)); |
+ } |
+ ++frame_count; |
+ } |
+ |
+ if (!sample->is_continuation_sample()) { |
+ break; |
+ } |
+ sample = Profiler::sample_buffer()->At(sample->continuation_index()); |
+ } |
+} |
+ |
}; // namespace dart |
#endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |