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 // 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 67 struct Feature { 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) {} 72 std::string name; 73 size_t default_value = 0; 74 size_t min_value = 0; 75 size_t max_value = 0; 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 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; 142 } else if (read_value < 0) { 143 error_log("%s: bad value for option '%s', value cannot be negative: %ld", 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"); 161 error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); 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"); 166 error_log(" %zu bytes, the max bytes is %zu.", DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); 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."); 171 error_log(" The default is %zu bytes, the max bytes is %zu.", 172 DEFAULT_GUARD_BYTES, MAX_GUARD_BYTES); 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."); 177 error_log(" The default is %zu frames, the max number of frames is %zu.", 178 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); 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"); 184 error_log(" frames. The default is %zu frames, the max number of frames is %zu.", 185 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); 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"); 208 error_log(" allocation by. The default is %zu bytes, the max bytes is %zu.", 209 DEFAULT_EXPAND_BYTES, MAX_EXPAND_BYTES); 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,"); 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."); 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); 221 error_log(""); 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."); 227 error_log(" The default is to record %zu frames, the max number of frames is %zu.", 228 DEFAULT_BACKTRACE_FRAMES, MAX_BACKTRACE_FRAMES); 229 error_log(""); 230 error_log(" leak_track"); 231 error_log(" Enable the leak tracking of memory allocations."); 232 } 233 234 static bool SetFeature( 235 const std::string name, const Feature& feature, size_t value, bool value_set) { 236 if (feature.config) { 237 *feature.config = true; 238 } 239 if (feature.value != nullptr) { 240 if (value_set) { 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 } 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", 256 getprogname(), name.c_str()); 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. 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; 276 backtrace_signal = SIGRTMAX - 19; 277 free_track_backtrace_num_frames = 16; 278 279 // Parse the options are of the format: 280 // option_name or option_name=XX 281 282 // Supported features: 283 const Feature features[] = { 284 Feature("guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, 0, nullptr, nullptr, true), 285 // Enable front guard. Value is the size of the guard. 286 Feature("front_guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, FRONT_GUARD, 287 &this->front_guard_bytes, nullptr, true), 288 // Enable end guard. Value is the size of the guard. 289 Feature("rear_guard", DEFAULT_GUARD_BYTES, 1, MAX_GUARD_BYTES, REAR_GUARD, 290 &this->rear_guard_bytes, nullptr, true), 291 292 // Enable logging the backtrace on allocation. Value is the total 293 // number of frames to log. 294 Feature("backtrace", DEFAULT_BACKTRACE_FRAMES, 1, MAX_BACKTRACE_FRAMES, 295 BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames, &this->backtrace_enabled, false), 296 // Enable gathering backtrace values on a signal. 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), 300 301 Feature("fill", SIZE_MAX, 1, SIZE_MAX, 0, nullptr, nullptr, true), 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). 305 Feature("fill_on_alloc", SIZE_MAX, 1, SIZE_MAX, FILL_ON_ALLOC, &this->fill_on_alloc_bytes, 306 nullptr, true), 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). 310 Feature("fill_on_free", SIZE_MAX, 1, SIZE_MAX, FILL_ON_FREE, &this->fill_on_free_bytes, nullptr, true), 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. 314 Feature ("expand_alloc", DEFAULT_EXPAND_BYTES, 1, MAX_EXPAND_BYTES, EXPAND_ALLOC, 315 &this->expand_alloc_bytes, nullptr, false), 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. 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), 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. 324 Feature("free_track_backtrace_num_frames", DEFAULT_BACKTRACE_FRAMES, 325 0, MAX_BACKTRACE_FRAMES, 0, &this->free_track_backtrace_num_frames, nullptr, false), 326 327 // Enable printing leaked allocations. 328 Feature("leak_track", 0, 0, 0, LEAK_TRACK | TRACK_ALLOCS, nullptr, nullptr, false), 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); 336 bool valid = true; 337 while (valid && parser.Get(&property, &value, &value_set)) { 338 bool found = false; 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++) { 344 if (!SetFeature(property, features[i], value, value_set)) { 345 valid = false; 346 break; 347 } 348 options |= features[i].option; 349 } 350 if (!valid) { 351 break; 352 } 353 } else { 354 if (!SetFeature(property, features[i], value, value_set)) { 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) { 374 // It's necessary to align the front guard to MINIMUM_ALIGNMENT_BYTES to 375 // make sure that the header is aligned properly. 376 if (options & FRONT_GUARD) { 377 front_guard_bytes = BIONIC_ALIGN(front_guard_bytes, MINIMUM_ALIGNMENT_BYTES); 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 } 392