-
Notifications
You must be signed in to change notification settings - Fork 5.5k
memory: Add memory-debug scribbling when tcmalloc is disabled and not compiled for optimization #5450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
memory: Add memory-debug scribbling when tcmalloc is disabled and not compiled for optimization #5450
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a5d2e9a
Copy mem_debug impl from pagespeed as an alternative to tcmalloc's de…
jmarantz c5ab27c
add mem-debug files.
jmarantz f608251
attempt to use tcmalloc hook to do allocation scribbling; doesn't work.
jmarantz 68668a6
Remove broken attempt to get alloc scribbling working with tcmalloc.
jmarantz cf1ff7e
Remove superfluous changes
jmarantz c20d7cd
Remove stale comment.
jmarantz d2d196b
Hack in a maze of twisty passages to get -D setting through blaze.
jmarantz 324d7c2
format
jmarantz 8f4dd93
Merge branch 'master' into mem_debug
jmarantz a3c0b54
Clean up coding and hook up allocated-bytes count into memory/stats.cc.
jmarantz a37ebbb
Merge branch 'master' into mem_debug
jmarantz bd0d593
formatting
jmarantz 32b8d0d
Added a few signed/unsigned cleanups, assert <4g allocations, etc.
jmarantz 3fe801c
Clean up namespaces, filenames, etc. Share align() with BlockMemoryHa…
jmarantz 6282866
use raw 'noexcept'.
jmarantz 4022204
More comment cleanups.
jmarantz a5303ef
fix build path
jmarantz efdfc93
Remove some stray paths -- I am linking into main.cc but the MainComm…
jmarantz 0da923e
More cleanups & add test.
jmarantz a612677
Use the exported MEMORY_DEBUG_ENABLED for determining if memory debug…
jmarantz d532232
Address review comments.
jmarantz 664c08a
Add BUILD file for new test.
jmarantz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Very simple memory debugging overrides for operator new/delete, to | ||
| // help us quickly find simple memory violations: | ||
| // 1. Double destruct | ||
| // 2. Read before write (via scribbling) | ||
| // 3. Read after delete (via scribbling) | ||
| // | ||
| // Note that valgrind does all of this much better, but is too slow to run all | ||
| // the time. asan does read-after-delete detection but not read-before-init | ||
| // detection. See | ||
| // https://clang.llvm.org/docs/AddressSanitizer.html#initialization-order-checking | ||
| // for more details. | ||
|
|
||
| // Principle of operation: add 8 bytes to every allocation. The first | ||
| // 4 bytes are a marker (kLiveMarker or kDeadMarker1). The next 4 | ||
| // bytes are used to store size of the allocation, which helps us | ||
| // know how many bytes to scribble when we free. | ||
| // | ||
| // This code was adapted from mod_pagespeed, and adapted for Envoy | ||
| // style. Original source: | ||
| // https://github.com/apache/incubator-pagespeed-mod/blob/master/pagespeed/kernel/base/mem_debug.cc | ||
|
|
||
| #define INSTALL_HOOKS | ||
|
|
||
| // We don't run memory debugging for optimizd builds to avoid impacting | ||
| // production performance. | ||
| #ifndef NDEBUG | ||
|
|
||
| // We can't run memory debugging with tcmalloc due to conflicts with | ||
| // overriding operator new/delete. Note tcmalloc allows installation | ||
| // of a malloc hook (MallocHook::AddNewHook(&tcmallocHook)) with | ||
| // tcmallocHook(const void* ptr, size_t size). I tried const_casting ptr | ||
| // and scribbling over it, but this results in SEGV in grpc and the | ||
| // internals of gtest. | ||
| // | ||
| // And in any case, you can't use the tcmalloc hooks to do free-scribbling | ||
| // as it does not pass in the size to the free hook. See | ||
| // gperftools/malloc_hook.h for details. | ||
|
|
||
| #if !defined(TCMALLOC) && !defined(ENVOY_DISABLE_MEMDEBUG) | ||
|
|
||
| #include <cstdlib> | ||
|
|
||
| #include "common/common/assert.h" | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr int32_t kLiveMarker = 0xfeedface; // first 4 bytes after alloc | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| constexpr int32_t kDeadMarker1 = 0xabacabff; // first 4 bytes after free | ||
| constexpr int32_t kDeadMarker2 = 0xdeadbeef; // overwrites the 'size' field on free | ||
| constexpr size_t kOverhead = 2 * sizeof(int32_t); // number of extra bytes to alloc | ||
|
|
||
| void scribble(void* ptr, size_t size, int32_t scribble_word) { | ||
| int32_t num_ints = size / sizeof(int32_t); | ||
| int32_t* p = static_cast<int32_t*>(ptr); | ||
| for (int i = 0; i < num_ints; ++i, ++p) { | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| *p = scribble_word; | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| size_t roundedSize(size_t size) { | ||
| if (size == 0) { | ||
| size = kOverhead; | ||
| } else if ((size % kOverhead) != 0) { | ||
| size = size + kOverhead - (size % kOverhead); | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| } | ||
| return size; | ||
| } | ||
|
|
||
| void* debugMalloc(size_t size) { | ||
| size_t rounded = roundedSize(size); | ||
| int32_t* marker = static_cast<int32_t*>(malloc(rounded + kOverhead)); | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| ASSERT(marker != NULL); | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| marker[0] = kLiveMarker; | ||
| marker[1] = size; | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| int32_t* ret = marker + 2; | ||
| scribble(ret, rounded, kLiveMarker); | ||
| return reinterpret_cast<char*>(marker) + kOverhead; | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| void debugFree(void* ptr) { | ||
| if (ptr != NULL) { | ||
| char* alloced_ptr = static_cast<char*>(ptr) - kOverhead; | ||
| int32_t* marker = reinterpret_cast<int32_t*>(alloced_ptr); | ||
| scribble(ptr, roundedSize(marker[1]), kDeadMarker2); | ||
| ASSERT(kLiveMarker == marker[0]); | ||
| marker[0] = kDeadMarker1; | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| marker[1] = kDeadMarker2; | ||
| free(marker); | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| // C++ operator new/delete overrides, in all 8 combinations: | ||
| // (new vs delete) * (const std::nothrow_t& vs not) * ([] vs not) | ||
| // On MacOS __THROW appears to be missing so hide those in an ifdef. | ||
| #ifndef __THROW | ||
| #define __THROW | ||
| #endif | ||
|
|
||
| void* operator new(size_t size) { return debugMalloc(size); } | ||
| void operator delete(void* ptr)_GLIBCXX_USE_NOEXCEPT { debugFree(ptr); } | ||
|
jmarantz marked this conversation as resolved.
Outdated
|
||
| void operator delete(void* ptr, size_t)_GLIBCXX_USE_NOEXCEPT { debugFree(ptr); } | ||
|
|
||
| void* operator new[](size_t size) { return debugMalloc(size); } | ||
| void operator delete[](void* ptr) _GLIBCXX_USE_NOEXCEPT { debugFree(ptr); } | ||
| void operator delete[](void* ptr, size_t) _GLIBCXX_USE_NOEXCEPT { debugFree(ptr); } | ||
|
|
||
| #endif // !TCMALLOC && !ENVOY_DISABLE_MEMDEBUG | ||
| #endif // !NDEBUG | ||
|
|
||
| // We provide the entry-point to be called to force-load the memory debugger | ||
| // regardless of compilation mode. | ||
| namespace Envoy { | ||
|
|
||
| void MemDebugLoader() {} | ||
|
|
||
| } // namespace Envoy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #pragma once | ||
|
|
||
| namespace Envoy { | ||
|
|
||
| // Called to force-load the memory debugging module, which (when tcmalloc is | ||
| // disabled) overrides operator new/delete. See comments in the .cc file for | ||
| // more details. | ||
| void MemDebugLoader(); | ||
|
|
||
| } // namespace Envoy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.