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 | |
| 7 | Currently, malloc debug requires root to enable. When it is enabled, it works |
| 8 | by adding a shim layer that replaces the normal allocation calls. The replaced |
| 9 | calls are: |
| 10 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 11 | * `malloc` |
| 12 | * `free` |
| 13 | * `calloc` |
| 14 | * `realloc` |
| 15 | * `posix_memalign` |
| 16 | * `memalign` |
| 17 | * `malloc_usable_size` |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 18 | |
| 19 | On 32 bit systems, these two deprecated functions are also replaced: |
| 20 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 21 | * `pvalloc` |
| 22 | * `valloc` |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 23 | |
| 24 | Any errors detected by the library are reported in the log. |
| 25 | |
| 26 | Controlling Malloc Debug Behavior |
| 27 | --------------------------------- |
| 28 | Malloc debug is controlled by individual options. Each option can be enabled |
| 29 | individually, or in a group of other options. Every single option can be |
| 30 | combined with every other option. |
| 31 | |
| 32 | Option Descriptions |
| 33 | ------------------- |
| 34 | ### front\_guard[=SIZE\_BYTES] |
| 35 | Enables a small buffer placed before the allocated data. This is an attempt |
| 36 | to find memory corruption occuring to a region before the original allocation. |
| 37 | On first allocation, this front guard is written with a specific pattern (0xaa). |
| 38 | When the allocation is freed, the guard is checked to verify it has not been |
| 39 | modified. If any part of the front guard is modified, an error will be reported |
| 40 | in the log indicating what bytes changed. |
| 41 | |
| 42 | If the backtrace option is also enabled, then any error message will include |
| 43 | the backtrace of the allocation site. |
| 44 | |
| 45 | If SIZE\_BYTES is present, it indicates the number of bytes in the guard. |
| 46 | The default is 32 bytes, the max bytes is 16384. SIZE\_BYTES will be |
| 47 | padded so that it is a multiple of 8 bytes on 32 bit systems and 16 bytes |
| 48 | on 64 bit systems to make sure that the allocation returned is aligned |
| 49 | properly. |
| 50 | |
| 51 | This option adds a special header to all allocations that contains the guard |
| 52 | and information about the original allocation. |
| 53 | |
| 54 | Example error: |
| 55 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 56 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 59 | |
| 60 | ### rear\_guard[=SIZE\_BYTES] |
| 61 | Enables a small buffer placed after the allocated data. This is an attempt |
| 62 | to find memory corruption occuring to a region after the original allocation. |
| 63 | On first allocation, this rear guard is written with a specific pattern (0xbb). |
| 64 | When the allocation is freed, the guard is checked to verify it has not been |
| 65 | modified. If any part of the rear guard is modified, an error will be reported |
| 66 | in the log indicating what bytes changed. |
| 67 | |
| 68 | If SIZE\_BYTES is present, it indicates the number of bytes in the guard. |
| 69 | The default is 32 bytes, the max bytes is 16384. |
| 70 | |
| 71 | This option adds a special header to all allocations that contains |
| 72 | information about the original allocation. |
| 73 | |
| 74 | Example error: |
| 75 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 76 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 79 | |
| 80 | ### guard[=SIZE\_BYTES] |
| 81 | Enables both a front guard and a rear guard on all allocations. |
| 82 | |
| 83 | If SIZE\_BYTES is present, it indicates the number of bytes in both guards. |
| 84 | The default is 32 bytes, the max bytes is 16384. |
| 85 | |
| 86 | ### backtrace[=MAX\_FRAMES] |
| 87 | Enable capturing the backtrace of each allocation site. |
| 88 | This option will slow down allocations by an order of magnitude. If the |
| 89 | system runs too slowly with this option enabled, decreasing the maximum number |
| 90 | of frames captured will speed the allocations up. |
| 91 | |
| 92 | Note that any backtrace frames that occur within the malloc backtrace library |
| 93 | itself are not recorded. |
| 94 | |
| 95 | If MAX\_FRAMES is present, it indicates the maximum number of frames to |
| 96 | capture in a backtrace. The default is 16 frames, the maximumum value |
| 97 | this can be set to is 256. |
| 98 | |
| 99 | This option adds a special header to all allocations that contains the |
| 100 | backtrace and information about the original allocation. |
| 101 | |
| 102 | ### backtrace\_enable\_on\_signal[=MAX\_FRAMES] |
| 103 | Enable capturing the backtrace of each allocation site. If the |
| 104 | backtrace capture is toggled when the process receives the signal |
| 105 | SIGRTMAX - 19 (which is 45 on most Android devices). When this |
| 106 | option is used alone, backtrace capture starts out disabled until the signal |
| 107 | is received. If both this option and the backtrace option are set, then |
| 108 | backtrace capture is enabled until the signal is received. |
| 109 | |
| 110 | If MAX\_FRAMES is present, it indicates the maximum number of frames to |
| 111 | capture in a backtrace. The default is 16 frames, the maximumum value |
| 112 | this can be set to is 256. |
| 113 | |
| 114 | This option adds a special header to all allocations that contains the |
| 115 | backtrace and information about the original allocation. |
| 116 | |
| 117 | ### fill\_on\_alloc[=MAX\_FILLED\_BYTES] |
| 118 | Any allocation routine, other than calloc, will result in the allocation being |
| 119 | filled with the value 0xeb. When doing a realloc to a larger size, the bytes |
| 120 | above the original usable size will be set to 0xeb. |
| 121 | |
| 122 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 123 | of bytes in the allocation. The default is to fill the entire allocation. |
| 124 | |
| 125 | ### fill\_on\_free[=MAX\_FILLED\_BYTES] |
| 126 | When an allocation is freed, fill it with 0xef. |
| 127 | |
| 128 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 129 | of bytes in the allocation. The default is to fill the entire allocation. |
| 130 | |
| 131 | ### fill[=MAX\_FILLED\_BYTES] |
| 132 | This enables both the fill\_on\_alloc option and the fill\_on\_free option. |
| 133 | |
| 134 | If MAX\_FILLED\_BYTES is present, it will only fill up to the specified number |
| 135 | of bytes in the allocation. The default is to fill the entire allocation. |
| 136 | |
| 137 | ### expand\_alloc[=EXPAND\_BYTES] |
| 138 | Add an extra amount to allocate for every allocation. |
| 139 | |
| 140 | If XX is present, it is the number of bytes to expand the allocation by. |
| 141 | The default is 16 bytes, the max bytes is 16384. |
| 142 | |
| 143 | ### free\_track[=ALLOCATION\_COUNT] |
| 144 | When a pointer is freed, do not free the memory right away, but add it to |
| 145 | a list of freed allocations. In addition to being added to the list, the |
| 146 | entire allocation is filled with the value 0xef, and the backtrace at |
| 147 | the time of the free is recorded. The backtrace recording is completely |
| 148 | separate from the backtrace option, and happens automatically if this |
| 149 | option is enabled. By default, a maximum of 16 frames will be recorded, |
| 150 | but this value can be changed using the free\_track\_backtrace\_num\_frames |
| 151 | option. It can also be completely disabled by setting the option to zero. |
| 152 | See the full description of this option below. |
| 153 | |
| 154 | When the list is full, an allocation is removed from the list and is |
| 155 | checked to make sure that none of the contents have been modified since |
| 156 | being placed on the list. When the program terminates, all of the allocations |
| 157 | left on the list are verified. |
| 158 | |
| 159 | If ALLOCATION\_COUNT is present, it indicates the total number of allocations |
| 160 | in the list. The default is to record 100 freed allocations, the max |
| 161 | allocations to record is 16384. |
| 162 | |
| 163 | This option adds a special header to all allocations that contains |
| 164 | information about the original allocation. |
| 165 | |
| 166 | Example error: |
| 167 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 168 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 176 | |
| 177 | In addition, there is another type of error message that can occur if |
| 178 | an allocation has a special header applied, and the header is corrupted |
| 179 | before the verification occurs. This is the error message that will be found |
| 180 | in the log: |
| 181 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 182 | 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] | 183 | |
| 184 | ### free\_track\_backtrace\_num\_frames[=MAX\_FRAMES] |
| 185 | This option only has meaning if free\_track is set. It indicates how many |
| 186 | backtrace frames to capture when an allocation is freed. |
| 187 | |
| 188 | If MAX\_FRAMES is present, it indicates the number of frames to capture. |
| 189 | If the value is set to zero, then no backtrace will be captured when the |
| 190 | allocation is freed. The default is to record 16 frames, the max number of |
| 191 | frames to to record is 256. |
| 192 | |
| 193 | ### leak\_track |
| 194 | Track all live allocations. When the program terminates, all of the live |
| 195 | allocations will be dumped to the log. If the backtrace option was enabled, |
| 196 | then the log will include the backtrace of the leaked allocations. This |
| 197 | option is not useful when enabled globally because a lot of programs do not |
| 198 | free everything before the program terminates. |
| 199 | |
| 200 | This option adds a special header to all allocations that contains |
| 201 | information about the original allocation. |
| 202 | |
| 203 | Example leak error found in the log: |
| 204 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 205 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 217 | |
| 218 | Additional Errors |
| 219 | ----------------- |
| 220 | There are a few other error messages that might appear in the log. |
| 221 | |
| 222 | ### Use After Free |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 223 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 234 | |
| 235 | This indicates that code is attempting to free an already freed pointer. The |
| 236 | name in parenthesis indicates that the application called the function |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 237 | *free* with the bad pointer. |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 238 | |
| 239 | For example, this message: |
| 240 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 241 | 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] | 242 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 243 | Would indicate that the application called the *realloc* function |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 244 | with an already freed pointer. |
| 245 | |
| 246 | ### Invalid Tag |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 247 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 253 | |
| 254 | This indicates that a function (malloc\_usable\_size) was called with |
| 255 | a pointer that is either not allocated memory, or that the memory of |
| 256 | the pointer has been corrupted. |
| 257 | |
| 258 | As with the other error message, the function in parenthesis is the |
| 259 | function that was called with the bad pointer. |
| 260 | |
| 261 | Examples |
| 262 | ======== |
| 263 | Enable backtrace tracking of all allocation for all processes: |
| 264 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 265 | adb shell stop |
| 266 | adb shell setprop libc.debug.malloc.options backtrace |
| 267 | adb shell start |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 268 | |
| 269 | Enable backtrace tracking for a specific process (ls): |
| 270 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 271 | adb shell setprop libc.debug.malloc.options backtrace |
| 272 | adb shell setprop libc.debug.malloc.program ls |
| 273 | adb shell ls |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 274 | |
| 275 | Enable backtrace tracking for the zygote and zygote based processes: |
| 276 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 277 | 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 Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 281 | |
| 282 | Enable multiple options (backtrace and guards): |
| 283 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 284 | adb shell stop |
| 285 | adb shell setprop libc.debug.malloc.options "\"backtrace guards\"" |
| 286 | adb shell start |
Christopher Ferris | 713a8e3 | 2016-03-18 14:29:51 -0700 | [diff] [blame] | 287 | |
| 288 | Enable malloc debug when multiple processes have the same name. This method |
| 289 | can be used to enable malloc debug for only a very specific process if |
| 290 | multiple processes have the same name. |
| 291 | |
| 292 | Note: The double quotes in the adb shell command are necessary. Otherwise, |
| 293 | the setprop command will fail since the backtrace guards options will look |
| 294 | like two arguments instead of one. |
| 295 | |
Christopher Ferris | c7bfe2e | 2016-04-26 16:07:29 -0700 | [diff] [blame^] | 296 | 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 |