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 | |
| 47 | struct Feature { |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 48 | Feature(std::string name, size_t default_value, size_t min_value, size_t max_value, |
| 49 | uint64_t option, size_t* value, bool* config, bool combo_option) |
| 50 | : name(name), default_value(default_value), min_value(min_value), max_value(max_value), |
| 51 | option(option), value(value), config(config), combo_option(combo_option) {} |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 52 | std::string name; |
| 53 | size_t default_value = 0; |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 54 | size_t min_value = 0; |
| 55 | size_t max_value = 0; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 56 | |
| 57 | uint64_t option = 0; |
| 58 | size_t* value = nullptr; |
| 59 | bool* config = nullptr; |
| 60 | // If set to true, then all of the options following are set on until |
| 61 | // for which the combo_option value is set. |
| 62 | bool combo_option = false; |
| 63 | }; |
| 64 | |
| 65 | class PropertyParser { |
| 66 | public: |
| 67 | PropertyParser(const char* property) : cur_(property) {} |
| 68 | |
| 69 | bool Get(std::string* property, size_t* value, bool* value_set); |
| 70 | |
| 71 | bool Done() { return done_; } |
| 72 | |
| 73 | void LogUsage(); |
| 74 | |
| 75 | static constexpr uint8_t DEFAULT_FILL_ALLOC_VALUE = 0xeb; |
| 76 | static constexpr uint8_t DEFAULT_FILL_FREE_VALUE = 0xef; |
| 77 | |
| 78 | static constexpr uint8_t DEFAULT_FRONT_GUARD_VALUE = 0xaa; |
| 79 | static constexpr uint8_t DEFAULT_REAR_GUARD_VALUE = 0xbb; |
| 80 | |
| 81 | private: |
| 82 | const char* cur_ = nullptr; |
| 83 | |
| 84 | bool done_ = false; |
| 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(PropertyParser); |
| 87 | }; |
| 88 | |
| 89 | bool PropertyParser::Get(std::string* property, size_t* value, bool* value_set) { |
| 90 | // Process each property name we can find. |
| 91 | while (isspace(*cur_)) |
| 92 | ++cur_; |
| 93 | |
| 94 | if (*cur_ == '\0') { |
| 95 | done_ = true; |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | const char* property_start = cur_; |
| 100 | while (!isspace(*cur_) && *cur_ != '=' && *cur_ != '\0') |
| 101 | ++cur_; |
| 102 | |
| 103 | *property = std::string(property_start, cur_ - property_start); |
| 104 | |
| 105 | // Skip any spaces after the name. |
| 106 | while (isspace(*cur_) && *cur_ != '=' && *cur_ != '\0') |
| 107 | ++cur_; |
| 108 | |
| 109 | if (*cur_ == '=') { |
| 110 | ++cur_; |
| 111 | errno = 0; |
| 112 | *value_set = true; |
| 113 | char* end; |
| 114 | long read_value = strtol(cur_, const_cast<char**>(&end), 10); |
| 115 | if (errno != 0) { |
| 116 | error_log("%s: bad value for option '%s': %s", getprogname(), property->c_str(), |
| 117 | strerror(errno)); |
| 118 | return false; |
| 119 | } |
| 120 | if (cur_ == end || (!isspace(*end) && *end != '\0')) { |
| 121 | if (cur_ == end) { |
| 122 | error_log("%s: bad value for option '%s'", getprogname(), property->c_str()); |
| 123 | } else { |
| 124 | error_log("%s: bad value for option '%s', non space found after option: %s", |
| 125 | getprogname(), property->c_str(), end); |
| 126 | } |
| 127 | return false; |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 128 | } else if (read_value < 0) { |
| 129 | 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] | 130 | getprogname(), property->c_str(), read_value); |
| 131 | return false; |
| 132 | } |
| 133 | *value = static_cast<size_t>(read_value); |
| 134 | cur_ = end; |
| 135 | } else { |
| 136 | *value_set = false; |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | void PropertyParser::LogUsage() { |
| 142 | error_log("malloc debug options usage:"); |
| 143 | error_log(""); |
| 144 | error_log(" front_guard[=XX]"); |
| 145 | error_log(" Enables a front guard on all allocations. If XX is set"); |
| 146 | error_log(" it sets the number of bytes in the guard. The default is"); |
| 147 | error_log(" 32 bytes."); |
| 148 | error_log(""); |
| 149 | error_log(" rear_guard[=XX]"); |
| 150 | error_log(" Enables a rear guard on all allocations. If XX is set"); |
| 151 | error_log(" it sets the number of bytes in the guard. The default is"); |
| 152 | error_log(" 32 bytes."); |
| 153 | error_log(""); |
| 154 | error_log(" guard[=XX]"); |
| 155 | error_log(" Enables both a front guard and a rear guard on all allocations."); |
| 156 | error_log(" If XX is set it sets the number of bytes in both guards."); |
| 157 | error_log(" The default is 32 bytes."); |
| 158 | error_log(""); |
| 159 | error_log(" backtrace[=XX]"); |
| 160 | error_log(" Enable capturing the backtrace at the point of allocation."); |
| 161 | error_log(" If XX is set it sets the number of backtrace frames."); |
| 162 | error_log(" The default is 16 frames."); |
| 163 | error_log(""); |
| 164 | error_log(" backtrace_enable_on_signal[=XX]"); |
| 165 | error_log(" Enable capturing the backtrace at the point of allocation."); |
| 166 | error_log(" The backtrace capture is not enabled until the process"); |
| 167 | error_log(" receives a signal. If XX is set it sets the number of backtrace"); |
| 168 | error_log(" frames. The default is 16 frames."); |
| 169 | error_log(""); |
| 170 | error_log(" fill_on_alloc[=XX]"); |
| 171 | error_log(" On first allocation, fill with the value 0x%02x.", DEFAULT_FILL_ALLOC_VALUE); |
| 172 | error_log(" If XX is set it will only fill up to XX bytes of the"); |
| 173 | error_log(" allocation. The default is to fill the entire allocation."); |
| 174 | error_log(""); |
| 175 | error_log(" fill_on_free[=XX]"); |
| 176 | error_log(" On free, fill with the value 0x%02x. If XX is set it will", |
| 177 | DEFAULT_FILL_FREE_VALUE); |
| 178 | error_log(" only fill up to XX bytes of the allocation. The default is to"); |
| 179 | error_log(" fill the entire allocation."); |
| 180 | error_log(""); |
| 181 | error_log(" fill[=XX]"); |
| 182 | error_log(" On both first allocation free, fill with the value 0x%02x on", |
| 183 | DEFAULT_FILL_ALLOC_VALUE); |
| 184 | error_log(" first allocation and the value 0x%02x. If XX is set, only fill", |
| 185 | DEFAULT_FILL_FREE_VALUE); |
| 186 | error_log(" up to XX bytes. The default is to fill the entire allocation."); |
| 187 | error_log(""); |
| 188 | error_log(" expand_alloc[=XX]"); |
| 189 | error_log(" Allocate an extra number of bytes for every allocation call."); |
| 190 | error_log(" If XX is set, that is the number of bytes to expand the"); |
| 191 | error_log(" allocation by. The default is 16 bytes."); |
| 192 | error_log(""); |
| 193 | error_log(" free_track[=XX]"); |
| 194 | error_log(" When a pointer is freed, do not free the memory right away."); |
| 195 | error_log(" Instead, keep XX of these allocations around and then verify"); |
| 196 | error_log(" that they have not been modified when the total number of freed"); |
| 197 | error_log(" allocations exceeds the XX amount. When the program terminates,"); |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 198 | error_log(" the rest of these allocations are verified. When this option is"); |
| 199 | error_log(" enabled, it automatically records the backtrace at the time of the free."); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 200 | error_log(" The default is to record 100 allocations."); |
| 201 | error_log(""); |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 202 | error_log(" free_track_backtrace_num_frames[=XX]"); |
| 203 | error_log(" This option only has meaning if free_track is set. This indicates"); |
| 204 | error_log(" how many backtrace frames to capture when an allocation is freed."); |
| 205 | error_log(" If XX is set, that is the number of frames to capture. If XX"); |
| 206 | error_log(" is set to zero, then no backtrace will be captured."); |
| 207 | error_log(" The default is to record 16 frames."); |
| 208 | error_log(""); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 209 | error_log(" leak_track"); |
| 210 | error_log(" Enable the leak tracking of memory allocations."); |
| 211 | } |
| 212 | |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 213 | static bool SetFeature( |
| 214 | 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] | 215 | if (feature.config) { |
| 216 | *feature.config = true; |
| 217 | } |
| 218 | if (feature.value != nullptr) { |
| 219 | if (value_set) { |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 220 | if (value < feature.min_value) { |
| 221 | error_log("%s: bad value for option '%s', value must be >= %zu: %zu", |
| 222 | getprogname(), name.c_str(), feature.min_value, value); |
| 223 | return false; |
| 224 | } else if (value > feature.max_value) { |
| 225 | error_log("%s: bad value for option '%s', value must be <= %zu: %zu", |
| 226 | getprogname(), name.c_str(), feature.max_value, value); |
| 227 | return false; |
| 228 | } |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 229 | *feature.value = value; |
| 230 | } else { |
| 231 | *feature.value = feature.default_value; |
| 232 | } |
| 233 | } else if (value_set) { |
| 234 | 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] | 235 | getprogname(), name.c_str()); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | // This function is designed to be called once. A second call will not |
| 242 | // reset all variables. |
| 243 | bool Config::SetFromProperties() { |
| 244 | char property_str[PROP_VALUE_MAX]; |
| 245 | memset(property_str, 0, sizeof(property_str)); |
| 246 | if (!__system_property_get("libc.debug.malloc.options", property_str)) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | // Initialize a few default values. |
| 251 | fill_alloc_value = PropertyParser::DEFAULT_FILL_ALLOC_VALUE; |
| 252 | fill_free_value = PropertyParser::DEFAULT_FILL_FREE_VALUE; |
| 253 | front_guard_value = PropertyParser::DEFAULT_FRONT_GUARD_VALUE; |
| 254 | rear_guard_value = PropertyParser::DEFAULT_REAR_GUARD_VALUE; |
| 255 | backtrace_signal = SIGRTMIN + 10; |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 256 | free_track_backtrace_num_frames = 16; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 257 | |
| 258 | // Parse the options are of the format: |
| 259 | // option_name or option_name=XX |
| 260 | |
| 261 | // Supported features: |
| 262 | const Feature features[] = { |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 263 | Feature("guard", 32, 1, 16384, 0, nullptr, nullptr, true), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 264 | // Enable front guard. Value is the size of the guard. |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 265 | Feature("front_guard", 32, 1, 16384, FRONT_GUARD, &this->front_guard_bytes, nullptr, true), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 266 | // Enable end guard. Value is the size of the guard. |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 267 | Feature("rear_guard", 32, 1, 16384, REAR_GUARD, &this->rear_guard_bytes, nullptr, true), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 268 | |
| 269 | // Enable logging the backtrace on allocation. Value is the total |
| 270 | // number of frames to log. |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 271 | Feature("backtrace", 16, 1, 256, BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames, |
Christopher Ferris | f2b67b8 | 2016-01-25 14:36:34 -0800 | [diff] [blame] | 272 | &this->backtrace_enabled, false), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 273 | // Enable gathering backtrace values on a signal. |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 274 | Feature("backtrace_enable_on_signal", 16, 1, 256, BACKTRACE | TRACK_ALLOCS, |
| 275 | &this->backtrace_frames, &this->backtrace_enable_on_signal, false), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 276 | |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 277 | Feature("fill", SIZE_MAX, 1, SIZE_MAX, 0, nullptr, nullptr, true), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 278 | // Fill the allocation with an arbitrary pattern on allocation. |
| 279 | // Value is the number of bytes of the allocation to fill |
| 280 | // (default entire allocation). |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 281 | 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] | 282 | nullptr, true), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 283 | // Fill the allocation with an arbitrary pattern on free. |
| 284 | // Value is the number of bytes of the allocation to fill |
| 285 | // (default entire allocation). |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 286 | 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] | 287 | |
| 288 | // Expand the size of every alloc by this number bytes. Value is |
| 289 | // the total number of bytes to expand every allocation by. |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 290 | Feature ("expand_alloc", 16, 1, 16384, EXPAND_ALLOC, &this->expand_alloc_bytes, |
| 291 | nullptr, false), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 292 | |
| 293 | // Keep track of the freed allocations and verify at a later date |
| 294 | // that they have not been used. Turning this on, also turns on |
| 295 | // fill on free. |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 296 | Feature("free_track", 100, 1, 16384, FREE_TRACK | FILL_ON_FREE, &this->free_track_allocations, |
Christopher Ferris | f2b67b8 | 2016-01-25 14:36:34 -0800 | [diff] [blame] | 297 | nullptr, false), |
Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 298 | // Number of backtrace frames to keep when free_track is enabled. If this |
| 299 | // value is set to zero, no backtrace will be kept. |
| 300 | Feature("free_track_backtrace_num_frames", 16, 0, 256, 0, |
| 301 | &this->free_track_backtrace_num_frames, nullptr, false), |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 302 | |
| 303 | // Enable printing leaked allocations. |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 304 | 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] | 305 | }; |
| 306 | |
| 307 | // Process each property name we can find. |
| 308 | std::string property; |
| 309 | size_t value; |
| 310 | bool value_set; |
| 311 | PropertyParser parser(property_str); |
| 312 | bool found = false; |
| 313 | bool valid = true; |
| 314 | while (valid && parser.Get(&property, &value, &value_set)) { |
| 315 | for (size_t i = 0; i < sizeof(features)/sizeof(Feature); i++) { |
| 316 | if (property == features[i].name) { |
| 317 | if (features[i].option == 0 && features[i].combo_option) { |
| 318 | i++; |
| 319 | for (; i < sizeof(features)/sizeof(Feature) && features[i].combo_option; i++) { |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 320 | if (!SetFeature(property, features[i], value, value_set)) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 321 | valid = false; |
| 322 | break; |
| 323 | } |
| 324 | options |= features[i].option; |
| 325 | } |
| 326 | if (!valid) { |
| 327 | break; |
| 328 | } |
| 329 | } else { |
Christopher Ferris | 166c853 | 2016-01-27 17:23:36 -0800 | [diff] [blame] | 330 | if (!SetFeature(property, features[i], value, value_set)) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 331 | valid = false; |
| 332 | break; |
| 333 | } |
| 334 | options |= features[i].option; |
| 335 | } |
| 336 | found = true; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | if (valid && !found) { |
| 341 | error_log("%s: unknown option %s", getprogname(), property.c_str()); |
| 342 | valid = false; |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | valid = valid && parser.Done(); |
| 348 | |
| 349 | if (valid) { |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 350 | // 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] | 351 | // make sure that the header is aligned properly. |
| 352 | if (options & FRONT_GUARD) { |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 353 | front_guard_bytes = BIONIC_ALIGN(front_guard_bytes, MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | // This situation can occur if the free_track option is specified and |
| 357 | // the fill_on_free option is not. In this case, indicate the whole |
| 358 | // allocation should be filled. |
| 359 | if ((options & FILL_ON_FREE) && fill_on_free_bytes == 0) { |
| 360 | fill_on_free_bytes = SIZE_MAX; |
| 361 | } |
| 362 | } else { |
| 363 | parser.LogUsage(); |
| 364 | } |
| 365 | |
| 366 | return valid; |
| 367 | } |