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