Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 1 | Malloc Debug |
| 2 | ============ |
| 3 | |
| 4 | Malloc debug is a method of debugging native memory problems. It can help |
| 5 | detect memory corruption, memory leaks, and use after free issues. |
| 6 | |
Christopher Ferris | eab4803 | 2016-05-25 13:04:29 -0700 | [diff] [blame] | 7 | This documentation describes how to enable this feature on Android N or later |
Elliott Hughes | 7dc7d2b | 2018-04-06 14:44:49 -0700 | [diff] [blame^] | 8 | versions of the Android OS. (See the "Examples" section.) |
Christopher Ferris | eab4803 | 2016-05-25 13:04:29 -0700 | [diff] [blame] | 9 | |
| 10 | The documentation for malloc debug on older versions of Android is |
| 11 | [here](README_marshmallow_and_earlier.md). |
| 12 | |
Christopher Ferris | eab4803 | 2016-05-25 13:04:29 -0700 | [diff] [blame] | 13 | When malloc debug is enabled, it works by adding a shim layer that replaces |
| 14 | the normal allocation calls. The replaced calls are: |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 15 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 16 | * `malloc` |
| 17 | * `free` |
| 18 | * `calloc` |
| 19 | * `realloc` |
| 20 | * `posix_memalign` |
| 21 | * `memalign` |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 22 | * `aligned_alloc` |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 23 | * `malloc_usable_size` |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 24 | |
| 25 | On 32 bit systems, these two deprecated functions are also replaced: |
| 26 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 27 | * `pvalloc` |
| 28 | * `valloc` |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 29 | |
| 30 | Any errors detected by the library are reported in the log. |
| 31 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 32 | NOTE: There is a small behavioral change beginning in P for realloc. |
| 33 | Before, a realloc from one size to a smaller size would not update the |
| 34 | backtrace related to the allocation. Starting in P, every single realloc |
| 35 | call changes the backtrace for the pointer no matter whether the pointer |
| 36 | returned has changed or not. |
| 37 | |
| 38 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 39 | Controlling Malloc Debug Behavior |
| 40 | --------------------------------- |
| 41 | Malloc debug is controlled by individual options. Each option can be enabled |
| 42 | individually, or in a group of other options. Every single option can be |
| 43 | combined with every other option. |
| 44 | |
| 45 | Option Descriptions |
| 46 | ------------------- |
| 47 | ### front\_guard[=SIZE\_BYTES] |
| 48 | Enables a small buffer placed before the allocated data. This is an attempt |
| 49 | to find memory corruption occuring to a region before the original allocation. |
| 50 | On first allocation, this front guard is written with a specific pattern (0xaa). |
| 51 | When the allocation is freed, the guard is checked to verify it has not been |
| 52 | modified. If any part of the front guard is modified, an error will be reported |
| 53 | in the log indicating what bytes changed. |
| 54 | |
| 55 | If the backtrace option is also enabled, then any error message will include |
| 56 | the backtrace of the allocation site. |
| 57 | |
| 58 | If SIZE\_BYTES is present, it indicates the number of bytes in the guard. |
| 59 | The default is 32 bytes, the max bytes is 16384. SIZE\_BYTES will be |
| 60 | padded so that it is a multiple of 8 bytes on 32 bit systems and 16 bytes |
| 61 | on 64 bit systems to make sure that the allocation returned is aligned |
| 62 | properly. |
| 63 | |
| 64 | This option adds a special header to all allocations that contains the guard |
| 65 | and information about the original allocation. |
| 66 | |
| 67 | Example error: |
| 68 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 69 | 04-10 12:00:45.621 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 SIZE 100 HAS A CORRUPTED FRONT GUARD |
| 70 | 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[-32] = 0x00 (expected 0xaa) |
| 71 | 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[-15] = 0x02 (expected 0xaa) |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 72 | |
| 73 | ### rear\_guard[=SIZE\_BYTES] |
| 74 | Enables a small buffer placed after the allocated data. This is an attempt |
| 75 | to find memory corruption occuring to a region after the original allocation. |
| 76 | On first allocation, this rear guard is written with a specific pattern (0xbb). |
| 77 | When the allocation is freed, the guard is checked to verify it has not been |
| 78 | modified. If any part of the rear guard is modified, an error will be reported |
| 79 | in the log indicating what bytes changed. |
| 80 | |
| 81 | If SIZE\_BYTES is present, it indicates the number of bytes in the guard. |
| 82 | The default is 32 bytes, the max bytes is 16384. |
| 83 | |
| 84 | This option adds a special header to all allocations that contains |
| 85 | information about the original allocation. |
| 86 | |
| 87 | Example error: |
| 88 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 89 | 04-10 12:00:45.621 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 SIZE 100 HAS A CORRUPTED REAR GUARD |
| 90 | 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[130] = 0xbf (expected 0xbb) |
| 91 | 04-10 12:00:45.622 7412 7412 E malloc_debug: allocation[131] = 0x00 (expected 0xbb) |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 92 | |
| 93 | ### guard[=SIZE\_BYTES] |
| 94 | Enables both a front guard and a rear guard on all allocations. |
| 95 | |
| 96 | If SIZE\_BYTES is present, it indicates the number of bytes in both guards. |
| 97 | The default is 32 bytes, the max bytes is 16384. |
| 98 | |
| 99 | ### backtrace[=MAX\_FRAMES] |
| 100 | Enable capturing the backtrace of each allocation site. |
| 101 | This option will slow down allocations by an order of magnitude. If the |
| 102 | system runs too slowly with this option enabled, decreasing the maximum number |
| 103 | of frames captured will speed the allocations up. |
| 104 | |
| 105 | Note that any backtrace frames that occur within the malloc backtrace library |
| 106 | itself are not recorded. |
| 107 | |
| 108 | If MAX\_FRAMES is present, it indicates the maximum number of frames to |
| 109 | capture in a backtrace. The default is 16 frames, the maximumum value |
| 110 | this can be set to is 256. |
| 111 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 112 | Before P, this option adds a special header to all allocations that contains |
| 113 | the backtrace and information about the original allocation. After that, this |
| 114 | option will not add a special header. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 115 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 116 | As of P, this option will also enable dumping backtrace heap data to a |
| 117 | file when the process receives the signal SIGRTMAX - 17 ( which is 47 on most |
| 118 | Android devices). The format of this dumped data is the same format as |
| 119 | that dumped when running am dumpheap -n. The default is to dump this data |
| 120 | to the file /data/local/tmp/backtrace\_heap.**PID**.txt. This is useful when |
| 121 | used with native only executables that run for a while since these processes |
| 122 | are not spawned from a zygote process. |
| 123 | |
| 124 | Note that when the signal is received, the heap is not dumped until the next |
| 125 | malloc/free occurs. |
| 126 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 127 | ### backtrace\_enable\_on\_signal[=MAX\_FRAMES] |
| 128 | Enable capturing the backtrace of each allocation site. If the |
| 129 | backtrace capture is toggled when the process receives the signal |
| 130 | SIGRTMAX - 19 (which is 45 on most Android devices). When this |
| 131 | option is used alone, backtrace capture starts out disabled until the signal |
| 132 | is received. If both this option and the backtrace option are set, then |
| 133 | backtrace capture is enabled until the signal is received. |
| 134 | |
| 135 | If MAX\_FRAMES is present, it indicates the maximum number of frames to |
| 136 | capture in a backtrace. The default is 16 frames, the maximumum value |
| 137 | this can be set to is 256. |
| 138 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 139 | Before P, this option adds a special header to all allocations that contains |
| 140 | the backtrace and information about the original allocation. After that, this |
| 141 | option will not add a special header. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 142 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 143 | ### backtrace\_dump\_on\_exit |
| 144 | As of P, when the backtrace option has been enabled, this causes the backtrace |
| 145 | dump heap data to be dumped to a file when the program exits. If the backtrace |
| 146 | option has not been enabled, this does nothing. The default is to dump this |
| 147 | to the file named /data/local/tmp/backtrace\_heap.**PID**.exit.txt. |
| 148 | |
| 149 | The file location can be changed by setting the backtrace\_dump\_prefix |
| 150 | option. |
| 151 | |
| 152 | ### backtrace\_dump\_prefix |
| 153 | As of P, when the backtrace options has been enabled, this sets the prefix |
| 154 | used for dumping files when the signal SIGRTMAX - 17 is received or when |
| 155 | the program exits and backtrace\_dump\_on\_exit is set. |
| 156 | |
| 157 | The default is /data/local/tmp/backtrace\_heap. |
| 158 | |
| 159 | When this value is changed from the default, then the filename chosen |
| 160 | on the signal will be backtrace\_dump\_prefix.**PID**.txt. The filename chosen |
| 161 | when the program exits will be backtrace\_dump\_prefix.**PID**.exit.txt. |
| 162 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 163 | ### fill\_on\_alloc[=MAX\_FILLED\_BYTES] |
| 164 | Any allocation routine, other than calloc, will result in the allocation being |
| 165 | filled with the value 0xeb. When doing a realloc to a larger size, the bytes |
| 166 | above the original usable size will be set to 0xeb. |
| 167 | |
| 168 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 169 | of bytes in the allocation. The default is to fill the entire allocation. |
| 170 | |
| 171 | ### fill\_on\_free[=MAX\_FILLED\_BYTES] |
| 172 | When an allocation is freed, fill it with 0xef. |
| 173 | |
| 174 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 175 | of bytes in the allocation. The default is to fill the entire allocation. |
| 176 | |
| 177 | ### fill[=MAX\_FILLED\_BYTES] |
| 178 | This enables both the fill\_on\_alloc option and the fill\_on\_free option. |
| 179 | |
| 180 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 181 | of bytes in the allocation. The default is to fill the entire allocation. |
| 182 | |
| 183 | ### expand\_alloc[=EXPAND\_BYTES] |
| 184 | Add an extra amount to allocate for every allocation. |
| 185 | |
| 186 | If XX is present, it is the number of bytes to expand the allocation by. |
| 187 | The default is 16 bytes, the max bytes is 16384. |
| 188 | |
| 189 | ### free\_track[=ALLOCATION\_COUNT] |
| 190 | When a pointer is freed, do not free the memory right away, but add it to |
| 191 | a list of freed allocations. In addition to being added to the list, the |
| 192 | entire allocation is filled with the value 0xef, and the backtrace at |
| 193 | the time of the free is recorded. The backtrace recording is completely |
| 194 | separate from the backtrace option, and happens automatically if this |
| 195 | option is enabled. By default, a maximum of 16 frames will be recorded, |
| 196 | but this value can be changed using the free\_track\_backtrace\_num\_frames |
| 197 | option. It can also be completely disabled by setting the option to zero. |
| 198 | See the full description of this option below. |
| 199 | |
| 200 | When the list is full, an allocation is removed from the list and is |
| 201 | checked to make sure that none of the contents have been modified since |
| 202 | being placed on the list. When the program terminates, all of the allocations |
| 203 | left on the list are verified. |
| 204 | |
| 205 | If ALLOCATION\_COUNT is present, it indicates the total number of allocations |
| 206 | in the list. The default is to record 100 freed allocations, the max |
| 207 | allocations to record is 16384. |
| 208 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 209 | Before P, this option adds a special header to all allocations that contains |
| 210 | the backtrace and information about the original allocation. After that, this |
| 211 | option will not add a special header. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 212 | |
| 213 | Example error: |
| 214 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 215 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE |
| 216 | 04-15 12:00:31.305 7412 7412 E malloc_debug: allocation[20] = 0xaf (expected 0xef) |
| 217 | 04-15 12:00:31.305 7412 7412 E malloc_debug: allocation[99] = 0x12 (expected 0xef) |
| 218 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of free: |
| 219 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 220 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 221 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 222 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 223 | |
| 224 | In addition, there is another type of error message that can occur if |
| 225 | an allocation has a special header applied, and the header is corrupted |
| 226 | before the verification occurs. This is the error message that will be found |
| 227 | in the log: |
| 228 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 229 | 04-15 12:00:31.604 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 HAS CORRUPTED HEADER TAG 0x1cc7dc00 AFTER FREE |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 230 | |
| 231 | ### free\_track\_backtrace\_num\_frames[=MAX\_FRAMES] |
| 232 | This option only has meaning if free\_track is set. It indicates how many |
| 233 | backtrace frames to capture when an allocation is freed. |
| 234 | |
| 235 | If MAX\_FRAMES is present, it indicates the number of frames to capture. |
| 236 | If the value is set to zero, then no backtrace will be captured when the |
| 237 | allocation is freed. The default is to record 16 frames, the max number of |
| 238 | frames to to record is 256. |
| 239 | |
| 240 | ### leak\_track |
| 241 | Track all live allocations. When the program terminates, all of the live |
| 242 | allocations will be dumped to the log. If the backtrace option was enabled, |
| 243 | then the log will include the backtrace of the leaked allocations. This |
| 244 | option is not useful when enabled globally because a lot of programs do not |
| 245 | free everything before the program terminates. |
| 246 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 247 | Before P, this option adds a special header to all allocations that contains |
| 248 | the backtrace and information about the original allocation. After that, this |
| 249 | option will not add a special header. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 250 | |
| 251 | Example leak error found in the log: |
| 252 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 253 | 04-15 12:35:33.304 7412 7412 E malloc_debug: +++ APP leaked block of size 100 at 0x2be3b0b0 (leak 1 of 2) |
| 254 | 04-15 12:35:33.304 7412 7412 E malloc_debug: Backtrace at time of allocation: |
| 255 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 256 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 257 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 258 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
| 259 | 04-15 12:35:33.305 7412 7412 E malloc_debug: +++ APP leaked block of size 24 at 0x7be32380 (leak 2 of 2) |
| 260 | 04-15 12:35:33.305 7412 7412 E malloc_debug: Backtrace at time of allocation: |
| 261 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 262 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 263 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 264 | 04-15 12:35:33.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 265 | |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 266 | ### record\_allocs[=TOTAL\_ENTRIES] |
| 267 | Keep track of every allocation/free made on every thread and dump them |
| 268 | to a file when the signal SIGRTMAX - 18 (which is 46 on most Android devices) |
| 269 | is received. |
| 270 | |
| 271 | If TOTAL\_ENTRIES is set, then it indicates the total number of |
| 272 | allocation/free records that can be retained. If the number of records |
| 273 | reaches the TOTAL\_ENTRIES value, then any further allocations/frees are |
| 274 | not recorded. The default value is 8,000,000 and the maximum value this |
| 275 | can be set to is 50,000,000. |
| 276 | |
| 277 | Once the signal is received, and the current records are written to the |
| 278 | file, all current records are deleted. Any allocations/frees occuring while |
| 279 | the data is being dumped to the file are ignored. |
| 280 | |
| 281 | **NOTE**: This option is not available until the O release of Android. |
| 282 | |
| 283 | The allocation data is written in a human readable format. Every line begins |
| 284 | with the THREAD\_ID returned by gettid(), which is the thread that is making |
| 285 | the allocation/free. If a new thread is created, no special line is added |
| 286 | to the file. However, when a thread completes, a special entry is added to |
| 287 | the file indicating this. |
| 288 | |
| 289 | The thread complete line is: |
| 290 | |
| 291 | **THREAD\_ID**: thread\_done 0x0 |
| 292 | |
| 293 | Example: |
| 294 | |
| 295 | 187: thread_done 0x0 |
| 296 | |
| 297 | Below is how each type of allocation/free call ends up in the file dump. |
| 298 | |
| 299 | pointer = malloc(size) |
| 300 | |
| 301 | **THREAD\_ID**: malloc pointer size |
| 302 | |
| 303 | Example: |
| 304 | |
| 305 | 186: malloc 0xb6038060 20 |
| 306 | |
| 307 | free(pointer) |
| 308 | |
| 309 | **THREAD\_ID**: free pointer |
| 310 | |
| 311 | Example: |
| 312 | |
| 313 | 186: free 0xb6038060 |
| 314 | |
| 315 | pointer = calloc(nmemb, size) |
| 316 | |
| 317 | **THREAD\_ID**: calloc pointer nmemb size |
| 318 | |
| 319 | Example: |
| 320 | |
| 321 | 186: calloc 0xb609f080 32 4 |
| 322 | |
| 323 | new\_pointer = realloc(old\_pointer, size) |
| 324 | |
| 325 | **THREAD\_ID**: realloc new\_pointer old\_pointer size |
| 326 | |
| 327 | Example: |
| 328 | |
| 329 | 186: realloc 0xb609f080 0xb603e9a0 12 |
| 330 | |
| 331 | pointer = memalign(alignment, size) |
| 332 | |
| 333 | **THREAD\_ID**: memalign pointer alignment size |
| 334 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 335 | pointer = aligned\_alloc(alignment, size) |
| 336 | |
| 337 | **THREAD\_ID**: memalign pointer alignment size |
| 338 | |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 339 | posix\_memalign(&pointer, alignment, size) |
| 340 | |
| 341 | **THREAD\_ID**: memalign pointer alignment size |
| 342 | |
| 343 | Example: |
| 344 | |
| 345 | 186: memalign 0x85423660 16 104 |
| 346 | |
| 347 | pointer = valloc(size) |
| 348 | |
| 349 | **THREAD\_ID**: memalign pointer 4096 size |
| 350 | |
| 351 | Example: |
| 352 | |
| 353 | 186: memalign 0x85423660 4096 112 |
| 354 | |
| 355 | pointer = pvalloc(size) |
| 356 | |
| 357 | **THREAD\_ID**: memalign pointer 4096 <b>SIZE\_ROUNDED\_UP\_TO\_4096</b> |
| 358 | |
| 359 | Example: |
| 360 | |
| 361 | 186: memalign 0x85423660 4096 8192 |
| 362 | |
| 363 | ### record\_allocs\_file[=FILE\_NAME] |
| 364 | This option only has meaning if record\_allocs is set. It indicates the |
| 365 | file where the recorded allocations will be found. |
| 366 | |
| 367 | If FILE\_NAME is set, then it indicates where the record allocation data |
| 368 | will be placed. |
| 369 | |
| 370 | **NOTE**: This option is not available until the O release of Android. |
| 371 | |
Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 372 | ### verify\_pointers |
| 373 | Track all live allocations to determine if a pointer is used that does not |
| 374 | exist. This option is a lightweight way to verify that all |
| 375 | free/malloc\_usable\_size/realloc calls are passed valid pointers. |
| 376 | |
| 377 | Example error: |
| 378 | |
| 379 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 UNKNOWN POINTER (free) |
| 380 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure: |
| 381 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 382 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 383 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 384 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
| 385 | |
| 386 | Where the name of the function varies depending on the function that called |
| 387 | with a bad pointer. Only three functions do this checking: free, |
| 388 | malloc\_usable\_size, realloc. |
| 389 | |
| 390 | **NOTE**: This option is not available until the P release of Android. |
| 391 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 392 | Additional Errors |
| 393 | ----------------- |
| 394 | There are a few other error messages that might appear in the log. |
| 395 | |
| 396 | ### Use After Free |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 397 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE (free) |
| 398 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace of original free: |
| 399 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 400 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 401 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 402 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
| 403 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure: |
| 404 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 405 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 406 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 407 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 408 | |
| 409 | This indicates that code is attempting to free an already freed pointer. The |
| 410 | name in parenthesis indicates that the application called the function |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 411 | *free* with the bad pointer. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 412 | |
| 413 | For example, this message: |
| 414 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 415 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 USED AFTER FREE (realloc) |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 416 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 417 | Would indicate that the application called the *realloc* function |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 418 | with an already freed pointer. |
| 419 | |
| 420 | ### Invalid Tag |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 421 | 04-15 12:00:31.304 7412 7412 E malloc_debug: +++ ALLOCATION 0x12345678 HAS INVALID TAG 1ee7d000 (malloc_usable_size) |
| 422 | 04-15 12:00:31.305 7412 7412 E malloc_debug: Backtrace at time of failure: |
| 423 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #00 pc 00029310 /system/lib/libc.so |
| 424 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #01 pc 00021438 /system/lib/libc.so (newlocale+160) |
| 425 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #02 pc 000a9e38 /system/lib/libc++.so |
| 426 | 04-15 12:00:31.305 7412 7412 E malloc_debug: #03 pc 000a28a8 /system/lib/libc++.so |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 427 | |
| 428 | This indicates that a function (malloc\_usable\_size) was called with |
| 429 | a pointer that is either not allocated memory, or that the memory of |
| 430 | the pointer has been corrupted. |
| 431 | |
| 432 | As with the other error message, the function in parenthesis is the |
| 433 | function that was called with the bad pointer. |
| 434 | |
Christopher Ferris | 602b88c | 2017-08-04 13:04:04 -0700 | [diff] [blame] | 435 | Backtrace Heap Dump Format |
| 436 | ========================== |
| 437 | |
| 438 | This section describes the format of the backtrace heap dump. This data is |
| 439 | generated by am dumpheap -n or, as of P, by the signal or on exit. |
| 440 | |
| 441 | The data has this header: |
| 442 | |
| 443 | Android Native Heap Dump v1.0 |
| 444 | |
| 445 | Total memory: XXXX |
| 446 | Allocation records: YYYY |
| 447 | Backtrace size: ZZZZ |
| 448 | |
| 449 | Total memory is the total of all of the currently live allocations. |
| 450 | Allocation records is the total number of allocation records. |
| 451 | Backtrace size is the maximum number of backtrace frames that can be present. |
| 452 | |
| 453 | Following this header are two different sections, the first section is the |
| 454 | allocation records, the second section is the map data. |
| 455 | |
| 456 | The allocation record data has this format: |
| 457 | |
| 458 | z ZYGOTE_CHILD_ALLOC sz ALLOCATION_SIZE num NUM_ALLOCATIONS bt FRAMES |
| 459 | |
| 460 | ZYGOTE\_CHILD\_ALLOC is either 0 or 1. 0 means this was allocated by the |
| 461 | zygote process or in a process not spawned from the zygote. 1 means this |
| 462 | was allocated by an application after it forked off from the zygote process. |
| 463 | |
| 464 | ALLOCATION\_SIZE is the size of the allocation. |
| 465 | NUM\_ALLOCATIONS is the number of allocations that have this size and have the |
| 466 | same backtrace. |
| 467 | FRAMES is a list of instruction pointers that represent the backtrace of the |
| 468 | allocation. |
| 469 | |
| 470 | Example: |
| 471 | |
| 472 | z 0 sz 400 num 1 bt 0000a230 0000b500 |
| 473 | z 1 sz 500 num 3 bt 0000b000 0000c000 |
| 474 | |
| 475 | The first allocation record was created by the zygote of size 400 only one |
| 476 | with this backtrace/size and a backtrace of 0xa230, 0xb500. |
| 477 | The second allocation record was create by an application spawned from the |
| 478 | zygote of size 500, where there are three of these allocation with the same |
| 479 | backtrace/size and a backtrace of 0xb000, 0xc000. |
| 480 | |
| 481 | The final section is the map data for the process: |
| 482 | |
| 483 | MAPS |
| 484 | 7fe9181000-7fe91a2000 rw-p 00000000 00:00 0 /system/lib/libc.so |
| 485 | . |
| 486 | . |
| 487 | . |
| 488 | END |
| 489 | |
| 490 | The map data is simply the output of /proc/PID/maps. This data can be used to |
| 491 | decode the frames in the backtraces. |
| 492 | |
| 493 | There is a tool to visualize this data, development/scripts/native\_heapdump\_viewer.py. |
| 494 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 495 | Examples |
| 496 | ======== |
Elliott Hughes | 644275a | 2017-08-15 23:17:35 -0700 | [diff] [blame] | 497 | |
| 498 | ### For platform developers |
| 499 | |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 500 | Enable backtrace tracking of all allocation for all processes: |
| 501 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 502 | adb shell stop |
| 503 | adb shell setprop libc.debug.malloc.options backtrace |
| 504 | adb shell start |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 505 | |
| 506 | Enable backtrace tracking for a specific process (ls): |
| 507 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 508 | adb shell setprop libc.debug.malloc.options backtrace |
| 509 | adb shell setprop libc.debug.malloc.program ls |
| 510 | adb shell ls |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 511 | |
| 512 | Enable backtrace tracking for the zygote and zygote based processes: |
| 513 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 514 | adb shell stop |
| 515 | adb shell setprop libc.debug.malloc.program app_process |
| 516 | adb shell setprop libc.debug.malloc.options backtrace |
| 517 | adb shell start |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 518 | |
Mikhail Naganov | 5a1a953 | 2018-01-03 08:50:16 -0800 | [diff] [blame] | 519 | Enable multiple options (backtrace and guard): |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 520 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 521 | adb shell stop |
Mikhail Naganov | 5a1a953 | 2018-01-03 08:50:16 -0800 | [diff] [blame] | 522 | adb shell setprop libc.debug.malloc.options "\"backtrace guard\"" |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 523 | adb shell start |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 524 | |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 525 | Note: The two levels of quoting in the adb shell command is necessary. |
| 526 | The outer layer of quoting is for the shell on the host, to ensure that the |
Mikhail Naganov | 5a1a953 | 2018-01-03 08:50:16 -0800 | [diff] [blame] | 527 | inner layer of quoting is sent to the device, to make 'backtrace guard' |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 528 | a single argument. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 529 | |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 530 | Enable malloc debug using an environment variable (pre-O Android release): |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 531 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 532 | adb shell |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 533 | # setprop libc.debug.malloc.env_enabled 1 |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 534 | # setprop libc.debug.malloc.options backtrace |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 535 | # export LIBC_DEBUG_MALLOC_ENABLE=1 |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame] | 536 | # ls |
Christopher Ferris | ac66d16 | 2016-09-28 14:51:12 -0700 | [diff] [blame] | 537 | |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 538 | Enable malloc debug using an environment variable (Android O or later): |
| 539 | |
| 540 | adb shell |
| 541 | # export LIBC_DEBUG_MALLOC_OPTIONS=backtrace |
| 542 | # ls |
| 543 | |
| 544 | Any process spawned from this shell will run with malloc debug enabled |
| 545 | using the backtrace option. |
Christopher Ferris | ac66d16 | 2016-09-28 14:51:12 -0700 | [diff] [blame] | 546 | |
| 547 | adb shell stop |
| 548 | adb shell setprop libc.debug.malloc.options backtrace |
| 549 | adb shell start |
| 550 | adb shell am dumpheap -n <PID_TO_DUMP> /data/local/tmp/heap.txt |
| 551 | |
| 552 | It is possible to use the backtrace\_enable\_on\_signal option as well, |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 553 | but, obviously, it must be enabled through the signal before the file will |
| 554 | contain any data. |
| 555 | |
Elliott Hughes | 644275a | 2017-08-15 23:17:35 -0700 | [diff] [blame] | 556 | ### For app developers |
Christopher Ferris | 4c65669a | 2017-05-24 19:04:33 -0700 | [diff] [blame] | 557 | |
| 558 | Enable malloc debug for a specific program/application (Android O or later): |
| 559 | |
| 560 | adb shell setprop wrap.<APP> '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"' |
| 561 | |
| 562 | For example, to enable malloc debug for the google search box (Android O or later): |
| 563 | |
| 564 | adb shell setprop wrap.com.google.android.googlequicksearchbox '"LIBC_DEBUG_MALLOC_OPTIONS=backtrace logwrapper"' |
| 565 | adb shell am force-stop com.google.android.googlequicksearchbox |
| 566 | |
| 567 | NOTE: On pre-O versions of the Android OS, property names had a length limit |
| 568 | of 32. This meant that to create a wrap property with the name of the app, it |
| 569 | was necessary to truncate the name to fit. On O, property names can be |
| 570 | an order of magnitude larger, so there should be no need to truncate the name |
| 571 | at all. |
Elliott Hughes | 644275a | 2017-08-15 23:17:35 -0700 | [diff] [blame] | 572 | |
| 573 | To detect leaks while an app is running: |
| 574 | |
| 575 | adb shell dumpsys meminfo --unreachable <PID_OF_APP> |
| 576 | |
| 577 | Without also enabling malloc debug, this command will only tell |
| 578 | you whether it can detect leaked memory, not where those leaks are |
| 579 | occurring. If you enable malloc debug with the backtrace option for your |
| 580 | app before running the dumpsys command, you'll get backtraces showing |
| 581 | where the memory was allocated. |
| 582 | |
| 583 | For backtraces from your app to be useful, you'll want to keep the |
| 584 | symbols in your app's shared libraries rather than stripping them. That |
| 585 | way you'll see the location of the leak directly without having to use |
| 586 | something like the <code>ndk-stack</code> tool. |
| 587 | |
| 588 | ### Analyzing heap dumps |
| 589 | |
| 590 | To analyze the data produced by the dumpheap command, run this script: |
| 591 | |
| 592 | development/scripts/native_heapdump_viewer.py |
| 593 | |
| 594 | In order for the script to properly symbolize the stacks in the file, |
| 595 | make sure the script is executed from the tree that built the image. |
| 596 | |
| 597 | To collect, transfer, and analyze a dump: |
| 598 | |
| 599 | adb shell am dumpheap -n <PID_TO_DUMP> /data/local/tmp/heap.txt |
| 600 | adb shell pull /data/local/tmp/heap.txt . |
| 601 | python development/scripts/native_heapdump_viewer.py --symbols /some/path/to/symbols/ heap.txt > heap_info.txt |
| 602 | |
| 603 | At the moment, the script will look for symbols in the given directory, |
| 604 | using the path the .so file would have on the device. So if your .so file |
| 605 | is at `/data/app/.../lib/arm/libx.so` on the device, it will need to be at |
| 606 | `/some/path/to/symbols/data/app/.../lib/arm/libx.so` locally given the |
| 607 | command line above. That is: you need to mirror the directory structure |
| 608 | for the app in the symbols directory. |