From 549e52200b6af3c33cba7ef759ea98b49fa40082 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Mon, 22 Feb 2016 19:23:26 -0800 Subject: [PATCH] Move hard-coded values to constants. Change all of the hard-coded config values to be defined constants. Also make sure to use these constants in the usage output. Bug: 26739265 Change-Id: I597a6d71279a2df9cc7e98c42ac70224261bac99 --- libc/malloc_debug/Config.cpp | 86 ++++++++++++++-------- .../tests/malloc_debug_config_tests.cpp | 17 +++-- 2 files changed, 64 insertions(+), 39 deletions(-) diff --git a/libc/malloc_debug/Config.cpp b/libc/malloc_debug/Config.cpp index ce83aa4bb..4586fda3b 100644 --- a/libc/malloc_debug/Config.cpp +++ b/libc/malloc_debug/Config.cpp @@ -44,6 +44,26 @@ #include "Config.h" #include "debug_log.h" +// Config constants +static constexpr uint8_t DEFAULT_FILL_ALLOC_VALUE = 0xeb; +static constexpr uint8_t DEFAULT_FILL_FREE_VALUE = 0xef; + +static constexpr uint8_t DEFAULT_FRONT_GUARD_VALUE = 0xaa; +static constexpr uint8_t DEFAULT_REAR_GUARD_VALUE = 0xbb; + +// Used as the default for all guard values. +static constexpr size_t DEFAULT_GUARD_BYTES = 32; +static constexpr size_t MAX_GUARD_BYTES = 16384; + +static constexpr size_t DEFAULT_BACKTRACE_FRAMES = 16; +static constexpr size_t MAX_BACKTRACE_FRAMES = 256; + +static constexpr size_t DEFAULT_EXPAND_BYTES = 16; +static constexpr size_t MAX_EXPAND_BYTES = 16384; + +static constexpr size_t DEFAULT_FREE_TRACK_ALLOCATIONS = 100; +static constexpr size_t MAX_FREE_TRACK_ALLOCATIONS = 16384; + struct Feature { Feature(std::string name, size_t default_value, size_t min_value, size_t max_value, uint64_t option, size_t* value, bool* config, bool combo_option) @@ -72,12 +92,6 @@ class PropertyParser { void LogUsage(); - static constexpr uint8_t DEFAULT_FILL_ALLOC_VALUE = 0xeb; - static constexpr uint8_t DEFAULT_FILL_FREE_VALUE = 0xef; - - static constexpr uint8_t DEFAULT_FRONT_GUARD_VALUE = 0xaa; - static constexpr uint8_t DEFAULT_REAR_GUARD_VALUE = 0xbb; - private: const char* cur_ = nullptr; @@ -144,28 +158,31 @@ void PropertyParser::LogUsage() { error_log(" front_guard[=XX]"); error_log(" Enables a front guard on all allocations. If XX is set"); error_log(" it sets the number of bytes in the guard. The default is"); - error_log(" 32 bytes."); + error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); error_log(""); error_log(" rear_guard[=XX]"); error_log(" Enables a rear guard on all allocations. If XX is set"); error_log(" it sets the number of bytes in the guard. The default is"); - error_log(" 32 bytes."); + error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); error_log(""); error_log(" guard[=XX]"); error_log(" Enables both a front guard and a rear guard on all allocations."); error_log(" If XX is set it sets the number of bytes in both guards."); - error_log(" The default is 32 bytes."); + error_log(" The default is %zu bytes, the max bytes is %zu.", + DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); error_log(""); error_log(" backtrace[=XX]"); error_log(" Enable capturing the backtrace at the point of allocation."); error_log(" If XX is set it sets the number of backtrace frames."); - error_log(" The default is 16 frames."); + error_log(" The default is %zu frames, the max number of frames is %zu.", + DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); error_log(""); error_log(" backtrace_enable_on_signal[=XX]"); error_log(" Enable capturing the backtrace at the point of allocation."); error_log(" The backtrace capture is not enabled until the process"); error_log(" receives a signal. If XX is set it sets the number of backtrace"); - error_log(" frames. The default is 16 frames."); + error_log(" frames. The default is %zu frames, the max number of frames is %zu.", + DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); error_log(""); error_log(" fill_on_alloc[=XX]"); error_log(" On first allocation, fill with the value 0x%02x.", DEFAULT_FILL_ALLOC_VALUE); @@ -188,7 +205,8 @@ void PropertyParser::LogUsage() { error_log(" expand_alloc[=XX]"); error_log(" Allocate an extra number of bytes for every allocation call."); error_log(" If XX is set, that is the number of bytes to expand the"); - error_log(" allocation by. The default is 16 bytes."); + error_log(" allocation by. The default is %zu bytes, the max bytes is %zu.", + DEFAULT_EXPAND_BYTES, MAX_EXPAND_BYTES); error_log(""); error_log(" free_track[=XX]"); error_log(" When a pointer is freed, do not free the memory right away."); @@ -197,14 +215,17 @@ void PropertyParser::LogUsage() { error_log(" allocations exceeds the XX amount. When the program terminates,"); error_log(" the rest of these allocations are verified. When this option is"); error_log(" enabled, it automatically records the backtrace at the time of the free."); - error_log(" The default is to record 100 allocations."); + error_log(" The default is to record %zu allocations, the max allocations", + DEFAULT_FREE_TRACK_ALLOCATIONS); + error_log(" to record is %zu.", MAX_FREE_TRACK_ALLOCATIONS); error_log(""); error_log(" free_track_backtrace_num_frames[=XX]"); error_log(" This option only has meaning if free_track is set. This indicates"); error_log(" how many backtrace frames to capture when an allocation is freed."); error_log(" If XX is set, that is the number of frames to capture. If XX"); error_log(" is set to zero, then no backtrace will be captured."); - error_log(" The default is to record 16 frames."); + error_log(" The default is to record %zu frames, the max number of frames is %zu.", + DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); error_log(""); error_log(" leak_track"); error_log(" Enable the leak tracking of memory allocations."); @@ -248,10 +269,10 @@ bool Config::SetFromProperties() { } // Initialize a few default values. - fill_alloc_value = PropertyParser::DEFAULT_FILL_ALLOC_VALUE; - fill_free_value = PropertyParser::DEFAULT_FILL_FREE_VALUE; - front_guard_value = PropertyParser::DEFAULT_FRONT_GUARD_VALUE; - rear_guard_value = PropertyParser::DEFAULT_REAR_GUARD_VALUE; + fill_alloc_value = DEFAULT_FILL_ALLOC_VALUE; + fill_free_value = DEFAULT_FILL_FREE_VALUE; + front_guard_value = DEFAULT_FRONT_GUARD_VALUE; + rear_guard_value = DEFAULT_REAR_GUARD_VALUE; backtrace_signal = SIGRTMIN + 10; free_track_backtrace_num_frames = 16; @@ -260,19 +281,22 @@ bool Config::SetFromProperties() { // Supported features: const Feature features[] = { - Feature("guard", 32, 1, 16384, 0, nullptr, nullptr, true), + Feature("guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, 0, nullptr, nullptr, true), // Enable front guard. Value is the size of the guard. - Feature("front_guard", 32, 1, 16384, FRONT_GUARD, &this->front_guard_bytes, nullptr, true), + Feature("front_guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, FRONT_GUARD, + &this->front_guard_bytes, nullptr, true), // Enable end guard. Value is the size of the guard. - Feature("rear_guard", 32, 1, 16384, REAR_GUARD, &this->rear_guard_bytes, nullptr, true), + Feature("rear_guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, REAR_GUARD, + &this->rear_guard_bytes, nullptr, true), // Enable logging the backtrace on allocation. Value is the total // number of frames to log. - Feature("backtrace", 16, 1, 256, BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames, - &this->backtrace_enabled, false), + Feature("backtrace", DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, + BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames, &this->backtrace_enabled, false), // Enable gathering backtrace values on a signal. - Feature("backtrace_enable_on_signal", 16, 1, 256, BACKTRACE | TRACK_ALLOCS, - &this->backtrace_frames, &this->backtrace_enable_on_signal, false), + Feature("backtrace_enable_on_signal", DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, + BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames, &this->backtrace_enable_on_signal, + false), Feature("fill", SIZE_MAX, 1, SIZE_MAX, 0, nullptr, nullptr, true), // Fill the allocation with an arbitrary pattern on allocation. @@ -287,18 +311,18 @@ bool Config::SetFromProperties() { // Expand the size of every alloc by this number bytes. Value is // the total number of bytes to expand every allocation by. - Feature ("expand_alloc", 16, 1, 16384, EXPAND_ALLOC, &this->expand_alloc_bytes, - nullptr, false), + Feature ("expand_alloc", DEFAULT_EXPAND_BYTES, 1, MAX_EXPAND_BYTES, EXPAND_ALLOC, + &this->expand_alloc_bytes, nullptr, false), // Keep track of the freed allocations and verify at a later date // that they have not been used. Turning this on, also turns on // fill on free. - Feature("free_track", 100, 1, 16384, FREE_TRACK | FILL_ON_FREE, &this->free_track_allocations, - nullptr, false), + Feature("free_track", DEFAULT_FREE_TRACK_ALLOCATIONS, 1, MAX_FREE_TRACK_ALLOCATIONS, + FREE_TRACK | FILL_ON_FREE, &this->free_track_allocations, nullptr, false), // Number of backtrace frames to keep when free_track is enabled. If this // value is set to zero, no backtrace will be kept. - Feature("free_track_backtrace_num_frames", 16, 0, 256, 0, - &this->free_track_backtrace_num_frames, nullptr, false), + Feature("free_track_backtrace_num_frames", DEFAULT_BACKTRACE_FRAMES, + 0, MAX_BACKTRACE_FRAMES, 0, &this->free_track_backtrace_num_frames, nullptr, false), // Enable printing leaked allocations. Feature("leak_track", 0, 0, 0, LEAK_TRACK | TRACK_ALLOCS, nullptr, nullptr, false), diff --git a/libc/malloc_debug/tests/malloc_debug_config_tests.cpp b/libc/malloc_debug/tests/malloc_debug_config_tests.cpp index b8535e180..a6cea98e9 100644 --- a/libc/malloc_debug/tests/malloc_debug_config_tests.cpp +++ b/libc/malloc_debug/tests/malloc_debug_config_tests.cpp @@ -51,28 +51,28 @@ std::string usage_string( "6 malloc_debug front_guard[=XX]\n" "6 malloc_debug Enables a front guard on all allocations. If XX is set\n" "6 malloc_debug it sets the number of bytes in the guard. The default is\n" - "6 malloc_debug 32 bytes.\n" + "6 malloc_debug 32 bytes, the max bytes is 16384.\n" "6 malloc_debug \n" "6 malloc_debug rear_guard[=XX]\n" "6 malloc_debug Enables a rear guard on all allocations. If XX is set\n" "6 malloc_debug it sets the number of bytes in the guard. The default is\n" - "6 malloc_debug 32 bytes.\n" + "6 malloc_debug 32 bytes, the max bytes is 16384.\n" "6 malloc_debug \n" "6 malloc_debug guard[=XX]\n" "6 malloc_debug Enables both a front guard and a rear guard on all allocations.\n" "6 malloc_debug If XX is set it sets the number of bytes in both guards.\n" - "6 malloc_debug The default is 32 bytes.\n" + "6 malloc_debug The default is 32 bytes, the max bytes is 16384.\n" "6 malloc_debug \n" "6 malloc_debug backtrace[=XX]\n" "6 malloc_debug Enable capturing the backtrace at the point of allocation.\n" "6 malloc_debug If XX is set it sets the number of backtrace frames.\n" - "6 malloc_debug The default is 16 frames.\n" + "6 malloc_debug The default is 16 frames, the max number of frames is 256.\n" "6 malloc_debug \n" "6 malloc_debug backtrace_enable_on_signal[=XX]\n" "6 malloc_debug Enable capturing the backtrace at the point of allocation.\n" "6 malloc_debug The backtrace capture is not enabled until the process\n" "6 malloc_debug receives a signal. If XX is set it sets the number of backtrace\n" - "6 malloc_debug frames. The default is 16 frames.\n" + "6 malloc_debug frames. The default is 16 frames, the max number of frames is 256.\n" "6 malloc_debug \n" "6 malloc_debug fill_on_alloc[=XX]\n" "6 malloc_debug On first allocation, fill with the value 0xeb.\n" @@ -92,7 +92,7 @@ std::string usage_string( "6 malloc_debug expand_alloc[=XX]\n" "6 malloc_debug Allocate an extra number of bytes for every allocation call.\n" "6 malloc_debug If XX is set, that is the number of bytes to expand the\n" - "6 malloc_debug allocation by. The default is 16 bytes.\n" + "6 malloc_debug allocation by. The default is 16 bytes, the max bytes is 16384.\n" "6 malloc_debug \n" "6 malloc_debug free_track[=XX]\n" "6 malloc_debug When a pointer is freed, do not free the memory right away.\n" @@ -101,14 +101,15 @@ std::string usage_string( "6 malloc_debug allocations exceeds the XX amount. When the program terminates,\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 The default is to record 100 allocations, the max allocations\n" + "6 malloc_debug to record is 16384.\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 The default is to record 16 frames, the max number of frames is 256.\n" "6 malloc_debug \n" "6 malloc_debug leak_track\n" "6 malloc_debug Enable the leak tracking of memory allocations.\n" -- 2.11.0