summaryrefslogtreecommitdiff
path: root/libc/malloc_debug/tests/malloc_debug_config_tests.cpp
diff options
context:
space:
mode:
authorChristopher Ferris <cferris@google.com>2016-01-28 18:35:05 -0800
committerChristopher Ferris <cferris@google.com>2016-01-29 15:19:22 -0800
commit7993b80f894db20af4d1d154221c42fea6171a3d (patch)
treefc99d36000a8f0853224d75296d96509a730ee05 /libc/malloc_debug/tests/malloc_debug_config_tests.cpp
parent50647711ebaf2360aca05cc94a2fdf431c1a153e (diff)
Add better free tracking.
Included in this change: - Change the tag when a pointer is freed so it's easy to detect if an already freed pointer is being used. - Move the free backtrace out of the header. This backtrace is only used under only some circumstances, so no need to allocate space in all headers for it. - Add new option free_track_backtrace_num_frames to specify how many frames to record when the free occurs. This removes the dependency on the backtrace option to get backtraces. Bug: 26739265 Change-Id: I76f5209507dcf46af67ada162a7cb2bf282116f2
Diffstat (limited to 'libc/malloc_debug/tests/malloc_debug_config_tests.cpp')
-rw-r--r--libc/malloc_debug/tests/malloc_debug_config_tests.cpp61
1 files changed, 60 insertions, 1 deletions
diff --git a/libc/malloc_debug/tests/malloc_debug_config_tests.cpp b/libc/malloc_debug/tests/malloc_debug_config_tests.cpp
index 247e31979..551e49860 100644
--- a/libc/malloc_debug/tests/malloc_debug_config_tests.cpp
+++ b/libc/malloc_debug/tests/malloc_debug_config_tests.cpp
@@ -99,9 +99,17 @@ std::string usage_string(
"6 malloc_debug Instead, keep XX of these allocations around and then verify\n"
"6 malloc_debug that they have not been modified when the total number of freed\n"
"6 malloc_debug allocations exceeds the XX amount. When the program terminates,\n"
- "6 malloc_debug the rest of these allocations are verified.\n"
+ "6 malloc_debug the rest of these allocations are verified. When this option is\n"
+ "6 malloc_debug enabled, it automatically records the backtrace at the time of the free.\n"
"6 malloc_debug The default is to record 100 allocations.\n"
"6 malloc_debug \n"
+ "6 malloc_debug free_track_backtrace_num_frames[=XX]\n"
+ "6 malloc_debug This option only has meaning if free_track is set. This indicates\n"
+ "6 malloc_debug how many backtrace frames to capture when an allocation is freed.\n"
+ "6 malloc_debug If XX is set, that is the number of frames to capture. If XX\n"
+ "6 malloc_debug is set to zero, then no backtrace will be captured.\n"
+ "6 malloc_debug The default is to record 16 frames.\n"
+ "6 malloc_debug \n"
"6 malloc_debug leak_track\n"
"6 malloc_debug Enable the leak tracking of memory allocations.\n"
);
@@ -339,11 +347,13 @@ TEST_F(MallocDebugConfigTest, free_track) {
ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
ASSERT_EQ(1234U, config->free_track_allocations);
ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes);
+ ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
ASSERT_TRUE(InitConfig("free_track"));
ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
ASSERT_EQ(100U, config->free_track_allocations);
ASSERT_EQ(SIZE_MAX, config->fill_on_free_bytes);
+ ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
ASSERT_STREQ("", getFakeLogBuf().c_str());
ASSERT_STREQ("", getFakeLogPrint().c_str());
@@ -354,11 +364,50 @@ TEST_F(MallocDebugConfigTest, free_track_and_fill_on_free) {
ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
ASSERT_EQ(1234U, config->free_track_allocations);
ASSERT_EQ(32U, config->fill_on_free_bytes);
+ ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
ASSERT_TRUE(InitConfig("free_track fill_on_free=60"));
ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
ASSERT_EQ(100U, config->free_track_allocations);
ASSERT_EQ(60U, config->fill_on_free_bytes);
+ ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
+
+ ASSERT_STREQ("", getFakeLogBuf().c_str());
+ ASSERT_STREQ("", getFakeLogPrint().c_str());
+}
+
+TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames) {
+ ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames=123"));
+
+ ASSERT_EQ(0U, config->options);
+ ASSERT_EQ(123U, config->free_track_backtrace_num_frames);
+
+ ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames"));
+ ASSERT_EQ(0U, config->options);
+ ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
+
+ ASSERT_STREQ("", getFakeLogBuf().c_str());
+ ASSERT_STREQ("", getFakeLogPrint().c_str());
+}
+
+TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_zero) {
+ ASSERT_TRUE(InitConfig("free_track_backtrace_num_frames=0"));
+
+ ASSERT_EQ(0U, config->options);
+ ASSERT_EQ(0U, config->free_track_backtrace_num_frames);
+
+ ASSERT_STREQ("", getFakeLogBuf().c_str());
+ ASSERT_STREQ("", getFakeLogPrint().c_str());
+}
+
+TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_and_free_track) {
+ ASSERT_TRUE(InitConfig("free_track free_track_backtrace_num_frames=123"));
+ ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
+ ASSERT_EQ(123U, config->free_track_backtrace_num_frames);
+
+ ASSERT_TRUE(InitConfig("free_track free_track_backtrace_num_frames"));
+ ASSERT_EQ(FREE_TRACK | FILL_ON_FREE, config->options);
+ ASSERT_EQ(16U, config->free_track_backtrace_num_frames);
ASSERT_STREQ("", getFakeLogBuf().c_str());
ASSERT_STREQ("", getFakeLogPrint().c_str());
@@ -550,3 +599,13 @@ TEST_F(MallocDebugConfigTest, free_track_max_error) {
"value must be <= 16384: 21000\n");
ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
}
+
+TEST_F(MallocDebugConfigTest, free_track_backtrace_num_frames_max_error) {
+ ASSERT_FALSE(InitConfig("free_track_backtrace_num_frames=400"));
+
+ ASSERT_STREQ("", getFakeLogBuf().c_str());
+ std::string log_msg(
+ "6 malloc_debug malloc_testing: bad value for option 'free_track_backtrace_num_frames', "
+ "value must be <= 256: 400\n");
+ ASSERT_STREQ((log_msg + usage_string).c_str(), getFakeLogPrint().c_str());
+}