blob: 224bb08ed8b8b7efe13d73e7187ac365759e5ca2 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
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
47struct Feature {
Christopher Ferris166c8532016-01-27 17:23:36 -080048 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 Ferris63860cb2015-11-16 17:30:32 -080052 std::string name;
53 size_t default_value = 0;
Christopher Ferris166c8532016-01-27 17:23:36 -080054 size_t min_value = 0;
55 size_t max_value = 0;
Christopher Ferris63860cb2015-11-16 17:30:32 -080056
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
65class 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
89bool 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 Ferris166c8532016-01-27 17:23:36 -0800128 } else if (read_value < 0) {
129 error_log("%s: bad value for option '%s', value cannot be negative: %ld",
Christopher Ferris63860cb2015-11-16 17:30:32 -0800130 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
141void 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,");
198 error_log(" the rest of these allocations are verified.");
199 error_log(" The default is to record 100 allocations.");
200 error_log("");
201 error_log(" leak_track");
202 error_log(" Enable the leak tracking of memory allocations.");
203}
204
Christopher Ferris166c8532016-01-27 17:23:36 -0800205static bool SetFeature(
206 const std::string name, const Feature& feature, size_t value, bool value_set) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800207 if (feature.config) {
208 *feature.config = true;
209 }
210 if (feature.value != nullptr) {
211 if (value_set) {
Christopher Ferris166c8532016-01-27 17:23:36 -0800212 if (value < feature.min_value) {
213 error_log("%s: bad value for option '%s', value must be >= %zu: %zu",
214 getprogname(), name.c_str(), feature.min_value, value);
215 return false;
216 } else if (value > feature.max_value) {
217 error_log("%s: bad value for option '%s', value must be <= %zu: %zu",
218 getprogname(), name.c_str(), feature.max_value, value);
219 return false;
220 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800221 *feature.value = value;
222 } else {
223 *feature.value = feature.default_value;
224 }
225 } else if (value_set) {
226 error_log("%s: value set for option '%s' which does not take a value",
Christopher Ferris166c8532016-01-27 17:23:36 -0800227 getprogname(), name.c_str());
Christopher Ferris63860cb2015-11-16 17:30:32 -0800228 return false;
229 }
230 return true;
231}
232
233// This function is designed to be called once. A second call will not
234// reset all variables.
235bool Config::SetFromProperties() {
236 char property_str[PROP_VALUE_MAX];
237 memset(property_str, 0, sizeof(property_str));
238 if (!__system_property_get("libc.debug.malloc.options", property_str)) {
239 return false;
240 }
241
242 // Initialize a few default values.
243 fill_alloc_value = PropertyParser::DEFAULT_FILL_ALLOC_VALUE;
244 fill_free_value = PropertyParser::DEFAULT_FILL_FREE_VALUE;
245 front_guard_value = PropertyParser::DEFAULT_FRONT_GUARD_VALUE;
246 rear_guard_value = PropertyParser::DEFAULT_REAR_GUARD_VALUE;
247 backtrace_signal = SIGRTMIN + 10;
248
249 // Parse the options are of the format:
250 // option_name or option_name=XX
251
252 // Supported features:
253 const Feature features[] = {
Christopher Ferris166c8532016-01-27 17:23:36 -0800254 Feature("guard", 32, 1, 16384, 0, nullptr, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800255 // Enable front guard. Value is the size of the guard.
Christopher Ferris166c8532016-01-27 17:23:36 -0800256 Feature("front_guard", 32, 1, 16384, FRONT_GUARD, &this->front_guard_bytes, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800257 // Enable end guard. Value is the size of the guard.
Christopher Ferris166c8532016-01-27 17:23:36 -0800258 Feature("rear_guard", 32, 1, 16384, REAR_GUARD, &this->rear_guard_bytes, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800259
260 // Enable logging the backtrace on allocation. Value is the total
261 // number of frames to log.
Christopher Ferris166c8532016-01-27 17:23:36 -0800262 Feature("backtrace", 16, 1, 256, BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames,
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800263 &this->backtrace_enabled, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800264 // Enable gathering backtrace values on a signal.
Christopher Ferris166c8532016-01-27 17:23:36 -0800265 Feature("backtrace_enable_on_signal", 16, 1, 256, BACKTRACE | TRACK_ALLOCS,
266 &this->backtrace_frames, &this->backtrace_enable_on_signal, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800267
Christopher Ferris166c8532016-01-27 17:23:36 -0800268 Feature("fill", SIZE_MAX, 1, SIZE_MAX, 0, nullptr, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800269 // Fill the allocation with an arbitrary pattern on allocation.
270 // Value is the number of bytes of the allocation to fill
271 // (default entire allocation).
Christopher Ferris166c8532016-01-27 17:23:36 -0800272 Feature("fill_on_alloc", SIZE_MAX, 1, SIZE_MAX, FILL_ON_ALLOC, &this->fill_on_alloc_bytes,
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800273 nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800274 // Fill the allocation with an arbitrary pattern on free.
275 // Value is the number of bytes of the allocation to fill
276 // (default entire allocation).
Christopher Ferris166c8532016-01-27 17:23:36 -0800277 Feature("fill_on_free", SIZE_MAX, 1, SIZE_MAX, FILL_ON_FREE, &this->fill_on_free_bytes, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800278
279 // Expand the size of every alloc by this number bytes. Value is
280 // the total number of bytes to expand every allocation by.
Christopher Ferris166c8532016-01-27 17:23:36 -0800281 Feature ("expand_alloc", 16, 1, 16384, EXPAND_ALLOC, &this->expand_alloc_bytes,
282 nullptr, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800283
284 // Keep track of the freed allocations and verify at a later date
285 // that they have not been used. Turning this on, also turns on
286 // fill on free.
Christopher Ferris166c8532016-01-27 17:23:36 -0800287 Feature("free_track", 100, 1, 16384, FREE_TRACK | FILL_ON_FREE, &this->free_track_allocations,
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800288 nullptr, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800289
290 // Enable printing leaked allocations.
Christopher Ferris166c8532016-01-27 17:23:36 -0800291 Feature("leak_track", 0, 0, 0, LEAK_TRACK | TRACK_ALLOCS, nullptr, nullptr, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800292 };
293
294 // Process each property name we can find.
295 std::string property;
296 size_t value;
297 bool value_set;
298 PropertyParser parser(property_str);
299 bool found = false;
300 bool valid = true;
301 while (valid && parser.Get(&property, &value, &value_set)) {
302 for (size_t i = 0; i < sizeof(features)/sizeof(Feature); i++) {
303 if (property == features[i].name) {
304 if (features[i].option == 0 && features[i].combo_option) {
305 i++;
306 for (; i < sizeof(features)/sizeof(Feature) && features[i].combo_option; i++) {
Christopher Ferris166c8532016-01-27 17:23:36 -0800307 if (!SetFeature(property, features[i], value, value_set)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800308 valid = false;
309 break;
310 }
311 options |= features[i].option;
312 }
313 if (!valid) {
314 break;
315 }
316 } else {
Christopher Ferris166c8532016-01-27 17:23:36 -0800317 if (!SetFeature(property, features[i], value, value_set)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -0800318 valid = false;
319 break;
320 }
321 options |= features[i].option;
322 }
323 found = true;
324 break;
325 }
326 }
327 if (valid && !found) {
328 error_log("%s: unknown option %s", getprogname(), property.c_str());
329 valid = false;
330 break;
331 }
332 }
333
334 valid = valid && parser.Done();
335
336 if (valid) {
337 // It's necessary to align the front guard to sizeof(uintptr_t) to
338 // make sure that the header is aligned properly.
339 if (options & FRONT_GUARD) {
340 front_guard_bytes = BIONIC_ALIGN(front_guard_bytes, sizeof(uintptr_t));
341 }
342
343 // This situation can occur if the free_track option is specified and
344 // the fill_on_free option is not. In this case, indicate the whole
345 // allocation should be filled.
346 if ((options & FILL_ON_FREE) && fill_on_free_bytes == 0) {
347 fill_on_free_bytes = SIZE_MAX;
348 }
349 } else {
350 parser.LogUsage();
351 }
352
353 return valid;
354}