blob: 76901e2bfb58e6d7f2696ae135b1419f485d5e17 [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 Ferrisf2b67b82016-01-25 14:36:34 -080048 Feature(std::string name, size_t default_value, uint64_t option, size_t* value,
49 bool* config, bool combo_option)
50 : name(name), default_value(default_value), option(option), value(value),
51 config(config), combo_option(combo_option) {}
Christopher Ferris63860cb2015-11-16 17:30:32 -080052 std::string name;
53 size_t default_value = 0;
54
55 uint64_t option = 0;
56 size_t* value = nullptr;
57 bool* config = nullptr;
58 // If set to true, then all of the options following are set on until
59 // for which the combo_option value is set.
60 bool combo_option = false;
61};
62
63class PropertyParser {
64 public:
65 PropertyParser(const char* property) : cur_(property) {}
66
67 bool Get(std::string* property, size_t* value, bool* value_set);
68
69 bool Done() { return done_; }
70
71 void LogUsage();
72
73 static constexpr uint8_t DEFAULT_FILL_ALLOC_VALUE = 0xeb;
74 static constexpr uint8_t DEFAULT_FILL_FREE_VALUE = 0xef;
75
76 static constexpr uint8_t DEFAULT_FRONT_GUARD_VALUE = 0xaa;
77 static constexpr uint8_t DEFAULT_REAR_GUARD_VALUE = 0xbb;
78
79 private:
80 const char* cur_ = nullptr;
81
82 bool done_ = false;
83
84 DISALLOW_COPY_AND_ASSIGN(PropertyParser);
85};
86
87bool PropertyParser::Get(std::string* property, size_t* value, bool* value_set) {
88 // Process each property name we can find.
89 while (isspace(*cur_))
90 ++cur_;
91
92 if (*cur_ == '\0') {
93 done_ = true;
94 return false;
95 }
96
97 const char* property_start = cur_;
98 while (!isspace(*cur_) && *cur_ != '=' && *cur_ != '\0')
99 ++cur_;
100
101 *property = std::string(property_start, cur_ - property_start);
102
103 // Skip any spaces after the name.
104 while (isspace(*cur_) && *cur_ != '=' && *cur_ != '\0')
105 ++cur_;
106
107 if (*cur_ == '=') {
108 ++cur_;
109 errno = 0;
110 *value_set = true;
111 char* end;
112 long read_value = strtol(cur_, const_cast<char**>(&end), 10);
113 if (errno != 0) {
114 error_log("%s: bad value for option '%s': %s", getprogname(), property->c_str(),
115 strerror(errno));
116 return false;
117 }
118 if (cur_ == end || (!isspace(*end) && *end != '\0')) {
119 if (cur_ == end) {
120 error_log("%s: bad value for option '%s'", getprogname(), property->c_str());
121 } else {
122 error_log("%s: bad value for option '%s', non space found after option: %s",
123 getprogname(), property->c_str(), end);
124 }
125 return false;
126 } else if (read_value <= 0) {
127 error_log("%s: bad value for option '%s', value must be > 0: %ld",
128 getprogname(), property->c_str(), read_value);
129 return false;
130 }
131 *value = static_cast<size_t>(read_value);
132 cur_ = end;
133 } else {
134 *value_set = false;
135 }
136 return true;
137}
138
139void PropertyParser::LogUsage() {
140 error_log("malloc debug options usage:");
141 error_log("");
142 error_log(" front_guard[=XX]");
143 error_log(" Enables a front guard on all allocations. If XX is set");
144 error_log(" it sets the number of bytes in the guard. The default is");
145 error_log(" 32 bytes.");
146 error_log("");
147 error_log(" rear_guard[=XX]");
148 error_log(" Enables a rear guard on all allocations. If XX is set");
149 error_log(" it sets the number of bytes in the guard. The default is");
150 error_log(" 32 bytes.");
151 error_log("");
152 error_log(" guard[=XX]");
153 error_log(" Enables both a front guard and a rear guard on all allocations.");
154 error_log(" If XX is set it sets the number of bytes in both guards.");
155 error_log(" The default is 32 bytes.");
156 error_log("");
157 error_log(" backtrace[=XX]");
158 error_log(" Enable capturing the backtrace at the point of allocation.");
159 error_log(" If XX is set it sets the number of backtrace frames.");
160 error_log(" The default is 16 frames.");
161 error_log("");
162 error_log(" backtrace_enable_on_signal[=XX]");
163 error_log(" Enable capturing the backtrace at the point of allocation.");
164 error_log(" The backtrace capture is not enabled until the process");
165 error_log(" receives a signal. If XX is set it sets the number of backtrace");
166 error_log(" frames. The default is 16 frames.");
167 error_log("");
168 error_log(" fill_on_alloc[=XX]");
169 error_log(" On first allocation, fill with the value 0x%02x.", DEFAULT_FILL_ALLOC_VALUE);
170 error_log(" If XX is set it will only fill up to XX bytes of the");
171 error_log(" allocation. The default is to fill the entire allocation.");
172 error_log("");
173 error_log(" fill_on_free[=XX]");
174 error_log(" On free, fill with the value 0x%02x. If XX is set it will",
175 DEFAULT_FILL_FREE_VALUE);
176 error_log(" only fill up to XX bytes of the allocation. The default is to");
177 error_log(" fill the entire allocation.");
178 error_log("");
179 error_log(" fill[=XX]");
180 error_log(" On both first allocation free, fill with the value 0x%02x on",
181 DEFAULT_FILL_ALLOC_VALUE);
182 error_log(" first allocation and the value 0x%02x. If XX is set, only fill",
183 DEFAULT_FILL_FREE_VALUE);
184 error_log(" up to XX bytes. The default is to fill the entire allocation.");
185 error_log("");
186 error_log(" expand_alloc[=XX]");
187 error_log(" Allocate an extra number of bytes for every allocation call.");
188 error_log(" If XX is set, that is the number of bytes to expand the");
189 error_log(" allocation by. The default is 16 bytes.");
190 error_log("");
191 error_log(" free_track[=XX]");
192 error_log(" When a pointer is freed, do not free the memory right away.");
193 error_log(" Instead, keep XX of these allocations around and then verify");
194 error_log(" that they have not been modified when the total number of freed");
195 error_log(" allocations exceeds the XX amount. When the program terminates,");
196 error_log(" the rest of these allocations are verified.");
197 error_log(" The default is to record 100 allocations.");
198 error_log("");
199 error_log(" leak_track");
200 error_log(" Enable the leak tracking of memory allocations.");
201}
202
203static bool SetFeature(const Feature& feature, size_t value, bool value_set) {
204 if (feature.config) {
205 *feature.config = true;
206 }
207 if (feature.value != nullptr) {
208 if (value_set) {
209 *feature.value = value;
210 } else {
211 *feature.value = feature.default_value;
212 }
213 } else if (value_set) {
214 error_log("%s: value set for option '%s' which does not take a value",
215 getprogname(), feature.name.c_str());
216 return false;
217 }
218 return true;
219}
220
221// This function is designed to be called once. A second call will not
222// reset all variables.
223bool Config::SetFromProperties() {
224 char property_str[PROP_VALUE_MAX];
225 memset(property_str, 0, sizeof(property_str));
226 if (!__system_property_get("libc.debug.malloc.options", property_str)) {
227 return false;
228 }
229
230 // Initialize a few default values.
231 fill_alloc_value = PropertyParser::DEFAULT_FILL_ALLOC_VALUE;
232 fill_free_value = PropertyParser::DEFAULT_FILL_FREE_VALUE;
233 front_guard_value = PropertyParser::DEFAULT_FRONT_GUARD_VALUE;
234 rear_guard_value = PropertyParser::DEFAULT_REAR_GUARD_VALUE;
235 backtrace_signal = SIGRTMIN + 10;
236
237 // Parse the options are of the format:
238 // option_name or option_name=XX
239
240 // Supported features:
241 const Feature features[] = {
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800242 Feature("guard", 32, 0, nullptr, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800243 // Enable front guard. Value is the size of the guard.
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800244 Feature("front_guard", 32, FRONT_GUARD, &this->front_guard_bytes, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800245 // Enable end guard. Value is the size of the guard.
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800246 Feature("rear_guard", 32, REAR_GUARD, &this->rear_guard_bytes, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800247
248 // Enable logging the backtrace on allocation. Value is the total
249 // number of frames to log.
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800250 Feature("backtrace", 16, BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames,
251 &this->backtrace_enabled, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800252 // Enable gathering backtrace values on a signal.
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800253 Feature("backtrace_enable_on_signal", 16, BACKTRACE | TRACK_ALLOCS, &this->backtrace_frames,
254 &this->backtrace_enable_on_signal, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800255
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800256 Feature("fill", SIZE_MAX, 0, nullptr, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800257 // Fill the allocation with an arbitrary pattern on allocation.
258 // Value is the number of bytes of the allocation to fill
259 // (default entire allocation).
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800260 Feature("fill_on_alloc", SIZE_MAX, FILL_ON_ALLOC, &this->fill_on_alloc_bytes,
261 nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800262 // Fill the allocation with an arbitrary pattern on free.
263 // Value is the number of bytes of the allocation to fill
264 // (default entire allocation).
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800265 Feature("fill_on_free", SIZE_MAX, FILL_ON_FREE, &this->fill_on_free_bytes, nullptr, true),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800266
267 // Expand the size of every alloc by this number bytes. Value is
268 // the total number of bytes to expand every allocation by.
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800269 Feature ("expand_alloc", 16, EXPAND_ALLOC, &this->expand_alloc_bytes, nullptr, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800270
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.
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800274 Feature("free_track", 100, FREE_TRACK | FILL_ON_FREE, &this->free_track_allocations,
275 nullptr, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800276
277 // Enable printing leaked allocations.
Christopher Ferrisf2b67b82016-01-25 14:36:34 -0800278 Feature("leak_track", 0, LEAK_TRACK | TRACK_ALLOCS, nullptr, nullptr, false),
Christopher Ferris63860cb2015-11-16 17:30:32 -0800279 };
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}