Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Unified Diff: runtime/vm/malloc_hooks_test.cc

Issue 2680213002: Updated MallocHooks to collect stack traces when memory is allocated. (Closed)
Patch Set: Rebased and performed refactoring Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..9b08acbb72123e39ad75ec443da2a7e9809e03db 100644
--- a/runtime/vm/malloc_hooks_test.cc
+++ b/runtime/vm/malloc_hooks_test.cc
@@ -7,10 +7,10 @@
#if defined(DART_USE_TCMALLOC) && !defined(PRODUCT)
#include "platform/assert.h"
-#include "vm/class_finalizer.h"
#include "vm/globals.h"
#include "vm/malloc_hooks.h"
-#include "vm/symbols.h"
+#include "vm/profiler.h"
+#include "vm/profiler_service.h"
#include "vm/unit_test.h"
namespace dart {
@@ -56,7 +56,7 @@ UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) {
EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
const intptr_t buffer_size = 10;
- volatile char* buffer = new char[buffer_size];
+ char* buffer = new char[buffer_size];
MallocHookTestBufferInitializer(buffer, buffer_size);
EXPECT_EQ(1L, MallocHooks::allocation_count());
@@ -75,6 +75,88 @@ UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) {
MallocHooks::TearDown();
}
+
+VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) {
+ MallocHooks::ResetStats();
+ ASSERT(MallocHooks::ProfilingEnabled());
+
+ char* var = static_cast<char*>(malloc(16 * sizeof(char)));
+ Sample* sample = MallocHooks::GetSample(var);
+ EXPECT(sample != NULL);
+
+ free(var);
+ sample = MallocHooks::GetSample(var);
+ EXPECT(sample == NULL);
+}
+
+
+#if defined(TARGET_OS_WINDOWS)
+__declspec(noinline) static uintptr_t GetProgramCounter() {
zra 2017/02/16 21:45:27 Use DART_NOINLINE defined in platform/globals.h.
bkonyi 2017/02/16 22:55:06 Yes, this was from vm/profiler.cc. I've moved the
+ return reinterpret_cast<uintptr_t>(_ReturnAddress());
+}
+#else
+static uintptr_t __attribute__((noinline)) GetProgramCounter() {
+ return reinterpret_cast<uintptr_t>(
+ __builtin_extract_return_addr(__builtin_return_address(0)));
+}
+#endif
+
+
+static char* __attribute__((noinline))
zra 2017/02/16 21:45:27 DART_NOINLINE
bkonyi 2017/02/16 22:55:06 Done.
+StackTraceLengthHelper(uintptr_t* end_address) {
+ char* var = static_cast<char*>(malloc(16 * sizeof(char)));
+ *end_address = GetProgramCounter();
+ return var;
+}
+
+
+VM_UNIT_TEST_CASE(StackTraceMallocHookLengthTest) {
+ uintptr_t test_start_address =
+ reinterpret_cast<uintptr_t>(Dart_TestStackTraceMallocHookLengthTest);
+ uintptr_t helper_start_address =
+ reinterpret_cast<uintptr_t>(StackTraceLengthHelper);
+ uintptr_t helper_end_address = 0;
+
+ MallocHooks::ResetStats();
+ ASSERT(MallocHooks::ProfilingEnabled());
+
+ char* var = StackTraceLengthHelper(&helper_end_address);
+ Sample* sample = MallocHooks::GetSample(var);
+ EXPECT(sample != NULL);
+ uintptr_t test_end_address = GetProgramCounter();
+
+ uword address = sample->At(0);
+ EXPECT((helper_start_address <= address) && (helper_end_address >= address));
+ address = sample->At(1);
+ EXPECT((test_start_address <= address) && (test_end_address >= address));
+ free(var);
+}
+
+
+ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) {
+ MallocHooks::ResetStats();
+ ASSERT(MallocHooks::ProfilingEnabled());
+ ClearProfileVisitor cpv(Isolate::Current());
+ Profiler::sample_buffer()->VisitSamples(&cpv);
+
+ char* var = static_cast<char*>(malloc(16 * sizeof(char)));
+ JSONStream js;
+ ProfilerService::PrintNativeAllocationJSON(&js, Profile::kNoTags, -1, -1);
+ const char* json = js.ToCString();
+
+ // Check that all the stack frames from the current down to main are actually
+ // present in the profile. This is just a simple sanity check to make sure
+ // that the ProfileTrie has a representation of the stack trace collected when
+ // var is allocated. More intense testing is already done in profiler_test.cc.
+ EXPECT_SUBSTRING("\"dart::Dart_TestStackTraceMallocHookSimpleJSONTest()\"",
+ json);
+ EXPECT_SUBSTRING("\"dart::TestCase::Run()\"", json);
+ EXPECT_SUBSTRING("\"dart::TestCaseBase::RunTest()\"", json);
+ EXPECT_SUBSTRING("\"main\"", json);
+
+ free(var);
+}
+
}; // namespace dart
#endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT)
« no previous file with comments | « runtime/vm/malloc_hooks.cc ('k') | runtime/vm/os_thread.h » ('j') | runtime/vm/profiler.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698