| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <ctype.h> | 
|  | 30 | #include <errno.h> | 
|  | 31 | #include <limits.h> | 
|  | 32 | #include <signal.h> | 
|  | 33 | #include <stdlib.h> | 
|  | 34 | #include <string.h> | 
|  | 35 | #include <sys/cdefs.h> | 
|  | 36 |  | 
|  | 37 | #include <string> | 
|  | 38 | #include <vector> | 
|  | 39 |  | 
|  | 40 | #include <sys/system_properties.h> | 
|  | 41 |  | 
|  | 42 | #include <private/bionic_macros.h> | 
|  | 43 |  | 
|  | 44 | #include "Config.h" | 
|  | 45 | #include "debug_log.h" | 
|  | 46 |  | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 47 | // Config constants | 
|  | 48 | static constexpr uint8_t DEFAULT_FILL_ALLOC_VALUE = 0xeb; | 
|  | 49 | static constexpr uint8_t DEFAULT_FILL_FREE_VALUE = 0xef; | 
|  | 50 |  | 
|  | 51 | static constexpr uint8_t DEFAULT_FRONT_GUARD_VALUE = 0xaa; | 
|  | 52 | static constexpr uint8_t DEFAULT_REAR_GUARD_VALUE = 0xbb; | 
|  | 53 |  | 
|  | 54 | // Used as the default for all guard values. | 
|  | 55 | static constexpr size_t DEFAULT_GUARD_BYTES = 32; | 
|  | 56 | static constexpr size_t MAX_GUARD_BYTES = 16384; | 
|  | 57 |  | 
|  | 58 | static constexpr size_t DEFAULT_BACKTRACE_FRAMES = 16; | 
|  | 59 | static constexpr size_t MAX_BACKTRACE_FRAMES = 256; | 
|  | 60 |  | 
|  | 61 | static constexpr size_t DEFAULT_EXPAND_BYTES = 16; | 
|  | 62 | static constexpr size_t MAX_EXPAND_BYTES = 16384; | 
|  | 63 |  | 
|  | 64 | static constexpr size_t DEFAULT_FREE_TRACK_ALLOCATIONS = 100; | 
|  | 65 | static constexpr size_t MAX_FREE_TRACK_ALLOCATIONS = 16384; | 
|  | 66 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 67 | struct Feature { | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 68 | Feature(std::string name, size_t default_value, size_t min_value, size_t max_value, | 
|  | 69 | uint64_t option, size_t* value, bool* config, bool combo_option) | 
|  | 70 | : name(name), default_value(default_value), min_value(min_value), max_value(max_value), | 
|  | 71 | option(option), value(value), config(config), combo_option(combo_option) {} | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 72 | std::string name; | 
|  | 73 | size_t default_value = 0; | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 74 | size_t min_value = 0; | 
|  | 75 | size_t max_value = 0; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 76 |  | 
|  | 77 | uint64_t option = 0; | 
|  | 78 | size_t* value = nullptr; | 
|  | 79 | bool* config = nullptr; | 
|  | 80 | // If set to true, then all of the options following are set on until | 
|  | 81 | // for which the combo_option value is set. | 
|  | 82 | bool combo_option = false; | 
|  | 83 | }; | 
|  | 84 |  | 
|  | 85 | class PropertyParser { | 
|  | 86 | public: | 
|  | 87 | PropertyParser(const char* property) : cur_(property) {} | 
|  | 88 |  | 
|  | 89 | bool Get(std::string* property, size_t* value, bool* value_set); | 
|  | 90 |  | 
|  | 91 | bool Done() { return done_; } | 
|  | 92 |  | 
|  | 93 | void LogUsage(); | 
|  | 94 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 95 | private: | 
|  | 96 | const char* cur_ = nullptr; | 
|  | 97 |  | 
|  | 98 | bool done_ = false; | 
|  | 99 |  | 
|  | 100 | DISALLOW_COPY_AND_ASSIGN(PropertyParser); | 
|  | 101 | }; | 
|  | 102 |  | 
|  | 103 | bool PropertyParser::Get(std::string* property, size_t* value, bool* value_set) { | 
|  | 104 | // Process each property name we can find. | 
|  | 105 | while (isspace(*cur_)) | 
|  | 106 | ++cur_; | 
|  | 107 |  | 
|  | 108 | if (*cur_ == '\0') { | 
|  | 109 | done_ = true; | 
|  | 110 | return false; | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | const char* property_start = cur_; | 
|  | 114 | while (!isspace(*cur_) && *cur_ != '=' && *cur_ != '\0') | 
|  | 115 | ++cur_; | 
|  | 116 |  | 
|  | 117 | *property = std::string(property_start, cur_ - property_start); | 
|  | 118 |  | 
|  | 119 | // Skip any spaces after the name. | 
|  | 120 | while (isspace(*cur_) && *cur_ != '=' && *cur_ != '\0') | 
|  | 121 | ++cur_; | 
|  | 122 |  | 
|  | 123 | if (*cur_ == '=') { | 
|  | 124 | ++cur_; | 
|  | 125 | errno = 0; | 
|  | 126 | *value_set = true; | 
|  | 127 | char* end; | 
|  | 128 | long read_value = strtol(cur_, const_cast<char**>(&end), 10); | 
|  | 129 | if (errno != 0) { | 
|  | 130 | error_log("%s: bad value for option '%s': %s", getprogname(), property->c_str(), | 
|  | 131 | strerror(errno)); | 
|  | 132 | return false; | 
|  | 133 | } | 
|  | 134 | if (cur_ == end || (!isspace(*end) && *end != '\0')) { | 
|  | 135 | if (cur_ == end) { | 
|  | 136 | error_log("%s: bad value for option '%s'", getprogname(), property->c_str()); | 
|  | 137 | } else { | 
|  | 138 | error_log("%s: bad value for option '%s', non space found after option: %s", | 
|  | 139 | getprogname(), property->c_str(), end); | 
|  | 140 | } | 
|  | 141 | return false; | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 142 | } else if (read_value < 0) { | 
|  | 143 | error_log("%s: bad value for option '%s', value cannot be negative: %ld", | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 144 | getprogname(), property->c_str(), read_value); | 
|  | 145 | return false; | 
|  | 146 | } | 
|  | 147 | *value = static_cast<size_t>(read_value); | 
|  | 148 | cur_ = end; | 
|  | 149 | } else { | 
|  | 150 | *value_set = false; | 
|  | 151 | } | 
|  | 152 | return true; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | void PropertyParser::LogUsage() { | 
|  | 156 | error_log("malloc debug options usage:"); | 
|  | 157 | error_log(""); | 
|  | 158 | error_log("  front_guard[=XX]"); | 
|  | 159 | error_log("    Enables a front guard on all allocations. If XX is set"); | 
|  | 160 | error_log("    it sets the number of bytes in the guard. The default is"); | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 161 | error_log("    %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 162 | error_log(""); | 
|  | 163 | error_log("  rear_guard[=XX]"); | 
|  | 164 | error_log("    Enables a rear guard on all allocations. If XX is set"); | 
|  | 165 | error_log("    it sets the number of bytes in the guard. The default is"); | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 166 | error_log("    %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 167 | error_log(""); | 
|  | 168 | error_log("  guard[=XX]"); | 
|  | 169 | error_log("    Enables both a front guard and a rear guard on all allocations."); | 
|  | 170 | error_log("    If XX is set it sets the number of bytes in both guards."); | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 171 | error_log("    The default is %zu bytes, the max bytes is %zu.", | 
|  | 172 | DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 173 | error_log(""); | 
|  | 174 | error_log("  backtrace[=XX]"); | 
|  | 175 | error_log("    Enable capturing the backtrace at the point of allocation."); | 
|  | 176 | error_log("    If XX is set it sets the number of backtrace frames."); | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 177 | error_log("    The default is %zu frames, the max number of frames is %zu.", | 
|  | 178 | DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 179 | error_log(""); | 
|  | 180 | error_log("  backtrace_enable_on_signal[=XX]"); | 
|  | 181 | error_log("    Enable capturing the backtrace at the point of allocation."); | 
|  | 182 | error_log("    The backtrace capture is not enabled until the process"); | 
|  | 183 | error_log("    receives a signal. If XX is set it sets the number of backtrace"); | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 184 | error_log("    frames. The default is %zu frames, the max number of frames is %zu.", | 
|  | 185 | DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 186 | error_log(""); | 
|  | 187 | error_log("  fill_on_alloc[=XX]"); | 
|  | 188 | error_log("    On first allocation, fill with the value 0x%02x.", DEFAULT_FILL_ALLOC_VALUE); | 
|  | 189 | error_log("    If XX is set it will only fill up to XX bytes of the"); | 
|  | 190 | error_log("    allocation. The default is to fill the entire allocation."); | 
|  | 191 | error_log(""); | 
|  | 192 | error_log("  fill_on_free[=XX]"); | 
|  | 193 | error_log("    On free, fill with the value 0x%02x. If XX is set it will", | 
|  | 194 | DEFAULT_FILL_FREE_VALUE); | 
|  | 195 | error_log("    only fill up to XX bytes of the allocation. The default is to"); | 
|  | 196 | error_log("    fill the entire allocation."); | 
|  | 197 | error_log(""); | 
|  | 198 | error_log("  fill[=XX]"); | 
|  | 199 | error_log("    On both first allocation free, fill with the value 0x%02x on", | 
|  | 200 | DEFAULT_FILL_ALLOC_VALUE); | 
|  | 201 | error_log("    first allocation and the value 0x%02x. If XX is set, only fill", | 
|  | 202 | DEFAULT_FILL_FREE_VALUE); | 
|  | 203 | error_log("    up to XX bytes. The default is to fill the entire allocation."); | 
|  | 204 | error_log(""); | 
|  | 205 | error_log("  expand_alloc[=XX]"); | 
|  | 206 | error_log("    Allocate an extra number of bytes for every allocation call."); | 
|  | 207 | error_log("    If XX is set, that is the number of bytes to expand the"); | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 208 | error_log("    allocation by. The default is %zu bytes, the max bytes is %zu.", | 
|  | 209 | DEFAULT_EXPAND_BYTES, MAX_EXPAND_BYTES); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 210 | error_log(""); | 
|  | 211 | error_log("  free_track[=XX]"); | 
|  | 212 | error_log("    When a pointer is freed, do not free the memory right away."); | 
|  | 213 | error_log("    Instead, keep XX of these allocations around and then verify"); | 
|  | 214 | error_log("    that they have not been modified when the total number of freed"); | 
|  | 215 | error_log("    allocations exceeds the XX amount. When the program terminates,"); | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 216 | error_log("    the rest of these allocations are verified. When this option is"); | 
|  | 217 | error_log("    enabled, it automatically records the backtrace at the time of the free."); | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 218 | error_log("    The default is to record %zu allocations, the max allocations", | 
|  | 219 | DEFAULT_FREE_TRACK_ALLOCATIONS); | 
|  | 220 | error_log("    to record is %zu.", MAX_FREE_TRACK_ALLOCATIONS); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 221 | error_log(""); | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 222 | error_log("  free_track_backtrace_num_frames[=XX]"); | 
|  | 223 | error_log("    This option only has meaning if free_track is set. This indicates"); | 
|  | 224 | error_log("    how many backtrace frames to capture when an allocation is freed."); | 
|  | 225 | error_log("    If XX is set, that is the number of frames to capture. If XX"); | 
|  | 226 | error_log("    is set to zero, then no backtrace will be captured."); | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 227 | error_log("    The default is to record %zu frames, the max number of frames is %zu.", | 
|  | 228 | DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 229 | error_log(""); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 230 | error_log("  leak_track"); | 
|  | 231 | error_log("    Enable the leak tracking of memory allocations."); | 
|  | 232 | } | 
|  | 233 |  | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 234 | static bool SetFeature( | 
|  | 235 | const std::string name, const Feature& feature, size_t value, bool value_set) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 236 | if (feature.config) { | 
|  | 237 | *feature.config = true; | 
|  | 238 | } | 
|  | 239 | if (feature.value != nullptr) { | 
|  | 240 | if (value_set) { | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 241 | if (value < feature.min_value) { | 
|  | 242 | error_log("%s: bad value for option '%s', value must be >= %zu: %zu", | 
|  | 243 | getprogname(), name.c_str(), feature.min_value, value); | 
|  | 244 | return false; | 
|  | 245 | } else if (value > feature.max_value) { | 
|  | 246 | error_log("%s: bad value for option '%s', value must be <= %zu: %zu", | 
|  | 247 | getprogname(), name.c_str(), feature.max_value, value); | 
|  | 248 | return false; | 
|  | 249 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 250 | *feature.value = value; | 
|  | 251 | } else { | 
|  | 252 | *feature.value = feature.default_value; | 
|  | 253 | } | 
|  | 254 | } else if (value_set) { | 
|  | 255 | error_log("%s: value set for option '%s' which does not take a value", | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 256 | getprogname(), name.c_str()); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 257 | return false; | 
|  | 258 | } | 
|  | 259 | return true; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | // This function is designed to be called once. A second call will not | 
|  | 263 | // reset all variables. | 
|  | 264 | bool Config::SetFromProperties() { | 
|  | 265 | char property_str[PROP_VALUE_MAX]; | 
|  | 266 | memset(property_str, 0, sizeof(property_str)); | 
|  | 267 | if (!__system_property_get("libc.debug.malloc.options", property_str)) { | 
|  | 268 | return false; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | // Initialize a few default values. | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 272 | fill_alloc_value = DEFAULT_FILL_ALLOC_VALUE; | 
|  | 273 | fill_free_value = DEFAULT_FILL_FREE_VALUE; | 
|  | 274 | front_guard_value = DEFAULT_FRONT_GUARD_VALUE; | 
|  | 275 | rear_guard_value = DEFAULT_REAR_GUARD_VALUE; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 276 | backtrace_signal = SIGRTMIN + 10; | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 277 | free_track_backtrace_num_frames = 16; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 278 |  | 
|  | 279 | // Parse the options are of the format: | 
|  | 280 | //   option_name or option_name=XX | 
|  | 281 |  | 
|  | 282 | // Supported features: | 
|  | 283 | const Feature features[] = { | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 284 | Feature("guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, 0, nullptr, nullptr, true), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 285 | // Enable front guard. Value is the size of the guard. | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 286 | Feature("front_guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, FRONT_GUARD, | 
|  | 287 | &this->front_guard_bytes, nullptr, true), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 288 | // Enable end guard. Value is the size of the guard. | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 289 | Feature("rear_guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, REAR_GUARD, | 
|  | 290 | &this->rear_guard_bytes, nullptr, true), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 291 |  | 
|  | 292 | // Enable logging the backtrace on allocation. Value is the total | 
|  | 293 | // number of frames to log. | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 294 | Feature("backtrace", DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, | 
|  | 295 | BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames, &this->backtrace_enabled, false), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 296 | // Enable gathering backtrace values on a signal. | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 297 | Feature("backtrace_enable_on_signal", DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, | 
|  | 298 | BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames, &this->backtrace_enable_on_signal, | 
|  | 299 | false), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 300 |  | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 301 | Feature("fill", SIZE_MAX, 1, SIZE_MAX, 0, nullptr, nullptr, true), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 302 | // Fill the allocation with an arbitrary pattern on allocation. | 
|  | 303 | // Value is the number of bytes of the allocation to fill | 
|  | 304 | // (default entire allocation). | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 305 | Feature("fill_on_alloc", SIZE_MAX, 1, SIZE_MAX, FILL_ON_ALLOC, &this->fill_on_alloc_bytes, | 
| Christopher Ferris | f2b67b8 | 2016-01-25 14:36:34 -0800 | [diff] [blame] | 306 | nullptr, true), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 307 | // Fill the allocation with an arbitrary pattern on free. | 
|  | 308 | // Value is the number of bytes of the allocation to fill | 
|  | 309 | // (default entire allocation). | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 310 | Feature("fill_on_free", SIZE_MAX, 1, SIZE_MAX, FILL_ON_FREE, &this->fill_on_free_bytes, nullptr, true), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 311 |  | 
|  | 312 | // Expand the size of every alloc by this number bytes. Value is | 
|  | 313 | // the total number of bytes to expand every allocation by. | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 314 | Feature ("expand_alloc", DEFAULT_EXPAND_BYTES, 1, MAX_EXPAND_BYTES, EXPAND_ALLOC, | 
|  | 315 | &this->expand_alloc_bytes, nullptr, false), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 316 |  | 
|  | 317 | // Keep track of the freed allocations and verify at a later date | 
|  | 318 | // that they have not been used. Turning this on, also turns on | 
|  | 319 | // fill on free. | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 320 | Feature("free_track", DEFAULT_FREE_TRACK_ALLOCATIONS, 1, MAX_FREE_TRACK_ALLOCATIONS, | 
|  | 321 | FREE_TRACK | FILL_ON_FREE, &this->free_track_allocations, nullptr, false), | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 322 | // Number of backtrace frames to keep when free_track is enabled. If this | 
|  | 323 | // value is set to zero, no backtrace will be kept. | 
| Christopher Ferris | 549e522 | 2016-02-22 19:23:26 -0800 | [diff] [blame] | 324 | Feature("free_track_backtrace_num_frames", DEFAULT_BACKTRACE_FRAMES, | 
|  | 325 | 0, MAX_BACKTRACE_FRAMES, 0, &this->free_track_backtrace_num_frames, nullptr, false), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 326 |  | 
|  | 327 | // Enable printing leaked allocations. | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 328 | Feature("leak_track", 0, 0, 0, LEAK_TRACK | TRACK_ALLOCS, nullptr, nullptr, false), | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 329 | }; | 
|  | 330 |  | 
|  | 331 | // Process each property name we can find. | 
|  | 332 | std::string property; | 
|  | 333 | size_t value; | 
|  | 334 | bool value_set; | 
|  | 335 | PropertyParser parser(property_str); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 336 | bool valid = true; | 
|  | 337 | while (valid && parser.Get(&property, &value, &value_set)) { | 
| Christopher Ferris | a4b1425 | 2016-03-14 15:29:46 -0700 | [diff] [blame] | 338 | bool found = false; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 339 | for (size_t i = 0; i < sizeof(features)/sizeof(Feature); i++) { | 
|  | 340 | if (property == features[i].name) { | 
|  | 341 | if (features[i].option == 0 && features[i].combo_option) { | 
|  | 342 | i++; | 
|  | 343 | for (; i < sizeof(features)/sizeof(Feature) && features[i].combo_option; i++) { | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 344 | if (!SetFeature(property, features[i], value, value_set)) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 345 | valid = false; | 
|  | 346 | break; | 
|  | 347 | } | 
|  | 348 | options |= features[i].option; | 
|  | 349 | } | 
|  | 350 | if (!valid) { | 
|  | 351 | break; | 
|  | 352 | } | 
|  | 353 | } else { | 
| Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 354 | if (!SetFeature(property, features[i], value, value_set)) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 355 | valid = false; | 
|  | 356 | break; | 
|  | 357 | } | 
|  | 358 | options |= features[i].option; | 
|  | 359 | } | 
|  | 360 | found = true; | 
|  | 361 | break; | 
|  | 362 | } | 
|  | 363 | } | 
|  | 364 | if (valid && !found) { | 
|  | 365 | error_log("%s: unknown option %s", getprogname(), property.c_str()); | 
|  | 366 | valid = false; | 
|  | 367 | break; | 
|  | 368 | } | 
|  | 369 | } | 
|  | 370 |  | 
|  | 371 | valid = valid && parser.Done(); | 
|  | 372 |  | 
|  | 373 | if (valid) { | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 374 | // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 375 | // make sure that the header is aligned properly. | 
|  | 376 | if (options & FRONT_GUARD) { | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 377 | front_guard_bytes = BIONIC_ALIGN(front_guard_bytes, MINIMUM_ALIGNMENT_BYTES); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 378 | } | 
|  | 379 |  | 
|  | 380 | // This situation can occur if the free_track option is specified and | 
|  | 381 | // the fill_on_free option is not. In this case, indicate the whole | 
|  | 382 | // allocation should be filled. | 
|  | 383 | if ((options & FILL_ON_FREE) && fill_on_free_bytes == 0) { | 
|  | 384 | fill_on_free_bytes = SIZE_MAX; | 
|  | 385 | } | 
|  | 386 | } else { | 
|  | 387 | parser.LogUsage(); | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | return valid; | 
|  | 391 | } |