blob: d9eb782fff97429ac8e4418329e72828be3b22ce [file] [log] [blame] [view]
Christopher Ferris713a8e32016-03-18 14:29:51 -07001Malloc Debug
2============
3
4Malloc debug is a method of debugging native memory problems. It can help
5detect memory corruption, memory leaks, and use after free issues.
6
7Currently, malloc debug requires root to enable. When it is enabled, it works
8by adding a shim layer that replaces the normal allocation calls. The replaced
9calls are:
10
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070011* `malloc`
12* `free`
13* `calloc`
14* `realloc`
15* `posix_memalign`
16* `memalign`
17* `malloc_usable_size`
Christopher Ferris713a8e32016-03-18 14:29:51 -070018
19On 32 bit systems, these two deprecated functions are also replaced:
20
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070021* `pvalloc`
22* `valloc`
Christopher Ferris713a8e32016-03-18 14:29:51 -070023
24Any errors detected by the library are reported in the log.
25
26Controlling Malloc Debug Behavior
27---------------------------------
28Malloc debug is controlled by individual options. Each option can be enabled
29individually, or in a group of other options. Every single option can be
30combined with every other option.
31
32Option Descriptions
33-------------------
34### front\_guard[=SIZE\_BYTES]
35Enables a small buffer placed before the allocated data. This is an attempt
36to find memory corruption occuring to a region before the original allocation.
37On first allocation, this front guard is written with a specific pattern (0xaa).
38When the allocation is freed, the guard is checked to verify it has not been
39modified. If any part of the front guard is modified, an error will be reported
40in the log indicating what bytes changed.
41
42If the backtrace option is also enabled, then any error message will include
43the backtrace of the allocation site.
44
45If SIZE\_BYTES is present, it indicates the number of bytes in the guard.
46The default is 32 bytes, the max bytes is 16384. SIZE\_BYTES will be
47padded so that it is a multiple of 8 bytes on 32 bit systems and 16 bytes
48on 64 bit systems to make sure that the allocation returned is aligned
49properly.
50
51This option adds a special header to all allocations that contains the guard
52and information about the original allocation.
53
54Example error:
55
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070056 04-10 12:00:45.621 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 SIZE 100 HAS A CORRUPTED FRONT GUARD
57 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[-32] = 0x00 (expected 0xaa)
58 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[-15] = 0x02 (expected 0xaa)
Christopher Ferris713a8e32016-03-18 14:29:51 -070059
60### rear\_guard[=SIZE\_BYTES]
61Enables a small buffer placed after the allocated data. This is an attempt
62to find memory corruption occuring to a region after the original allocation.
63On first allocation, this rear guard is written with a specific pattern (0xbb).
64When the allocation is freed, the guard is checked to verify it has not been
65modified. If any part of the rear guard is modified, an error will be reported
66in the log indicating what bytes changed.
67
68If SIZE\_BYTES is present, it indicates the number of bytes in the guard.
69The default is 32 bytes, the max bytes is 16384.
70
71This option adds a special header to all allocations that contains
72information about the original allocation.
73
74Example error:
75
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -070076 04-10 12:00:45.621 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 SIZE 100 HAS A CORRUPTED REAR GUARD
77 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[130] = 0xbf (expected 0xbb)
78 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[131] = 0x00 (expected 0xbb)
Christopher Ferris713a8e32016-03-18 14:29:51 -070079
80### guard[=SIZE\_BYTES]
81Enables both a front guard and a rear guard on all allocations.
82
83If SIZE\_BYTES is present, it indicates the number of bytes in both guards.
84The default is 32 bytes, the max bytes is 16384.
85
86### backtrace[=MAX\_FRAMES]
87Enable capturing the backtrace of each allocation site.
88This option will slow down allocations by an order of magnitude. If the
89system runs too slowly with this option enabled, decreasing the maximum number
90of frames captured will speed the allocations up.
91
92Note that any backtrace frames that occur within the malloc backtrace library
93itself are not recorded.
94
95If MAX\_FRAMES is present, it indicates the maximum number of frames to
96capture in a backtrace. The default is 16 frames, the maximumum value
97this can be set to is 256.
98
99This option adds a special header to all allocations that contains the
100backtrace and information about the original allocation.
101
102### backtrace\_enable\_on\_signal[=MAX\_FRAMES]
103Enable capturing the backtrace of each allocation site. If the
104backtrace capture is toggled when the process receives the signal
105SIGRTMAX - 19 (which is 45 on most Android devices). When this
106option is used alone, backtrace capture starts out disabled until the signal
107is received. If both this option and the backtrace option are set, then
108backtrace capture is enabled until the signal is received.
109
110If MAX\_FRAMES is present, it indicates the maximum number of frames to
111capture in a backtrace. The default is 16 frames, the maximumum value
112this can be set to is 256.
113
114This option adds a special header to all allocations that contains the
115backtrace and information about the original allocation.
116
117### fill\_on\_alloc[=MAX\_FILLED\_BYTES]
118Any allocation routine, other than calloc, will result in the allocation being
119filled with the value 0xeb. When doing a realloc to a larger size, the bytes
120above the original usable size will be set to 0xeb.
121
122If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number
123of bytes in the allocation. The default is to fill the entire allocation.
124
125### fill\_on\_free[=MAX\_FILLED\_BYTES]
126When an allocation is freed, fill it with 0xef.
127
128If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number
129of bytes in the allocation. The default is to fill the entire allocation.
130
131### fill[=MAX\_FILLED\_BYTES]
132This enables both the fill\_on\_alloc option and the fill\_on\_free option.
133
134If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number
135of bytes in the allocation. The default is to fill the entire allocation.
136
137### expand\_alloc[=EXPAND\_BYTES]
138Add an extra amount to allocate for every allocation.
139
140If XX is present, it is the number of bytes to expand the allocation by.
141The default is 16 bytes, the max bytes is 16384.
142
143### free\_track[=ALLOCATION\_COUNT]
144When a pointer is freed, do not free the memory right away, but add it to
145a list of freed allocations. In addition to being added to the list, the
146entire allocation is filled with the value 0xef, and the backtrace at
147the time of the free is recorded. The backtrace recording is completely
148separate from the backtrace option, and happens automatically if this
149option is enabled. By default, a maximum of 16 frames will be recorded,
150but this value can be changed using the free\_track\_backtrace\_num\_frames
151option. It can also be completely disabled by setting the option to zero.
152See the full description of this option below.
153
154When the list is full, an allocation is removed from the list and is
155checked to make sure that none of the contents have been modified since
156being placed on the list. When the program terminates, all of the allocations
157left on the list are verified.
158
159If ALLOCATION\_COUNT is present, it indicates the total number of allocations
160in the list. The default is to record 100 freed allocations, the max
161allocations to record is 16384.
162
163This option adds a special header to all allocations that contains
164information about the original allocation.
165
166Example error:
167
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700168 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE
169 04-15 12:00:31.305 7412 7412 E malloc_debug: allocation[20] = 0xaf (expected 0xef)
170 04-15 12:00:31.305 7412 7412 E malloc_debug: allocation[99] = 0x12 (expected 0xef)
171 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of free:
172 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
173 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
174 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
175 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
Christopher Ferris713a8e32016-03-18 14:29:51 -0700176
177In addition, there is another type of error message that can occur if
178an allocation has a special header applied, and the header is corrupted
179before the verification occurs. This is the error message that will be found
180in the log:
181
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700182 04-15 12:00:31.604 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 HAS CORRUPTED HEADER TAG 0x1cc7dc00 AFTER FREE
Christopher Ferris713a8e32016-03-18 14:29:51 -0700183
184### free\_track\_backtrace\_num\_frames[=MAX\_FRAMES]
185This option only has meaning if free\_track is set. It indicates how many
186backtrace frames to capture when an allocation is freed.
187
188If MAX\_FRAMES is present, it indicates the number of frames to capture.
189If the value is set to zero, then no backtrace will be captured when the
190allocation is freed. The default is to record 16 frames, the max number of
191frames to to record is 256.
192
193### leak\_track
194Track all live allocations. When the program terminates, all of the live
195allocations will be dumped to the log. If the backtrace option was enabled,
196then the log will include the backtrace of the leaked allocations. This
197option is not useful when enabled globally because a lot of programs do not
198free everything before the program terminates.
199
200This option adds a special header to all allocations that contains
201information about the original allocation.
202
203Example leak error found in the log:
204
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700205 04-15 12:35:33.304 7412 7412 E malloc_debug: +++ APP leaked block of size 100 at 0x2be3b0b0 (leak 1 of 2)
206 04-15 12:35:33.304 7412 7412 E malloc_debug: Backtrace at time of allocation:
207 04-15 12:35:33.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
208 04-15 12:35:33.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
209 04-15 12:35:33.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
210 04-15 12:35:33.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
211 04-15 12:35:33.305 7412 7412 E malloc_debug: +++ APP leaked block of size 24 at 0x7be32380 (leak 2 of 2)
212 04-15 12:35:33.305 7412 7412 E malloc_debug: Backtrace at time of allocation:
213 04-15 12:35:33.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
214 04-15 12:35:33.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
215 04-15 12:35:33.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
216 04-15 12:35:33.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
Christopher Ferris713a8e32016-03-18 14:29:51 -0700217
218Additional Errors
219-----------------
220There are a few other error messages that might appear in the log.
221
222### Use After Free
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700223 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE (free)
224 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace of original free:
225 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
226 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
227 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
228 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
229 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure:
230 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
231 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
232 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
233 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
Christopher Ferris713a8e32016-03-18 14:29:51 -0700234
235This indicates that code is attempting to free an already freed pointer. The
236name in parenthesis indicates that the application called the function
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700237*free* with the bad pointer.
Christopher Ferris713a8e32016-03-18 14:29:51 -0700238
239For example, this message:
240
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700241 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE (realloc)
Christopher Ferris713a8e32016-03-18 14:29:51 -0700242
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700243Would indicate that the application called the *realloc* function
Christopher Ferris713a8e32016-03-18 14:29:51 -0700244with an already freed pointer.
245
246### Invalid Tag
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700247 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 HAS INVALID TAG 1ee7d000 (malloc_usable_size)
248 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure:
249 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so
250 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160)
251 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so
252 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so
Christopher Ferris713a8e32016-03-18 14:29:51 -0700253
254This indicates that a function (malloc\_usable\_size) was called with
255a pointer that is either not allocated memory, or that the memory of
256the pointer has been corrupted.
257
258As with the other error message, the function in parenthesis is the
259function that was called with the bad pointer.
260
261Examples
262========
263Enable backtrace tracking of all allocation for all processes:
264
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700265 adb shell stop
266 adb shell setprop libc.debug.malloc.options backtrace
267 adb shell start
Christopher Ferris713a8e32016-03-18 14:29:51 -0700268
269Enable backtrace tracking for a specific process (ls):
270
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700271 adb shell setprop libc.debug.malloc.options backtrace
272 adb shell setprop libc.debug.malloc.program ls
273 adb shell ls
Christopher Ferris713a8e32016-03-18 14:29:51 -0700274
275Enable backtrace tracking for the zygote and zygote based processes:
276
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700277 adb shell stop
278 adb shell setprop libc.debug.malloc.program app_process
279 adb shell setprop libc.debug.malloc.options backtrace
280 adb shell start
Christopher Ferris713a8e32016-03-18 14:29:51 -0700281
282Enable multiple options (backtrace and guards):
283
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700284 adb shell stop
285 adb shell setprop libc.debug.malloc.options "\"backtrace guards\""
286 adb shell start
Christopher Ferris713a8e32016-03-18 14:29:51 -0700287
288Enable malloc debug when multiple processes have the same name. This method
289can be used to enable malloc debug for only a very specific process if
290multiple processes have the same name.
291
292Note: The double quotes in the adb shell command are necessary. Otherwise,
293the setprop command will fail since the backtrace guards options will look
294like two arguments instead of one.
295
Christopher Ferrisc7bfe2e2016-04-26 16:07:29 -0700296 adb shell
297 # setprop libc.debug.malloc.env_enabled
298 # setprop libc.debug.malloc.options backtrace
299 # export LIBC_DEBUG_MALLOC_ENABLE 1
300 # ls