1*8d67ca89SAndroid Build Coastguard WorkerMalloc Hooks 2*8d67ca89SAndroid Build Coastguard Worker============ 3*8d67ca89SAndroid Build Coastguard Worker 4*8d67ca89SAndroid Build Coastguard WorkerMalloc hooks allows a program to intercept all allocation/free calls that 5*8d67ca89SAndroid Build Coastguard Workerhappen during execution. It is only available in Android P and newer versions 6*8d67ca89SAndroid Build Coastguard Workerof the OS. 7*8d67ca89SAndroid Build Coastguard Worker 8*8d67ca89SAndroid Build Coastguard WorkerThere are two ways to enable these hooks, set a special system 9*8d67ca89SAndroid Build Coastguard Workerproperty, or set a special environment variable and run your app/program. 10*8d67ca89SAndroid Build Coastguard Worker 11*8d67ca89SAndroid Build Coastguard WorkerWhen malloc hooks is enabled, it works by adding a shim layer that replaces 12*8d67ca89SAndroid Build Coastguard Workerthe normal allocation calls. The replaced calls are: 13*8d67ca89SAndroid Build Coastguard Worker 14*8d67ca89SAndroid Build Coastguard Worker* `malloc` 15*8d67ca89SAndroid Build Coastguard Worker* `free` 16*8d67ca89SAndroid Build Coastguard Worker* `calloc` 17*8d67ca89SAndroid Build Coastguard Worker* `realloc` 18*8d67ca89SAndroid Build Coastguard Worker* `posix_memalign` 19*8d67ca89SAndroid Build Coastguard Worker* `memalign` 20*8d67ca89SAndroid Build Coastguard Worker* `aligned_alloc` 21*8d67ca89SAndroid Build Coastguard Worker* `malloc_usable_size` 22*8d67ca89SAndroid Build Coastguard Worker 23*8d67ca89SAndroid Build Coastguard WorkerOn 32 bit systems, these two deprecated functions are also replaced: 24*8d67ca89SAndroid Build Coastguard Worker 25*8d67ca89SAndroid Build Coastguard Worker* `pvalloc` 26*8d67ca89SAndroid Build Coastguard Worker* `valloc` 27*8d67ca89SAndroid Build Coastguard Worker 28*8d67ca89SAndroid Build Coastguard WorkerThese four hooks are defined in malloc.h: 29*8d67ca89SAndroid Build Coastguard Worker 30*8d67ca89SAndroid Build Coastguard Worker void* (*volatile __malloc_hook)(size_t, const void*); 31*8d67ca89SAndroid Build Coastguard Worker void* (*volatile __realloc_hook)(void*, size_t, const void*); 32*8d67ca89SAndroid Build Coastguard Worker void (*volatile __free_hook)(void*, const void*); 33*8d67ca89SAndroid Build Coastguard Worker void* (*volatile __memalign_hook)(size_t, size_t, const void*); 34*8d67ca89SAndroid Build Coastguard Worker 35*8d67ca89SAndroid Build Coastguard WorkerWhen malloc is called and \_\_malloc\_hook has been set, then the hook 36*8d67ca89SAndroid Build Coastguard Workerfunction is called instead. 37*8d67ca89SAndroid Build Coastguard Worker 38*8d67ca89SAndroid Build Coastguard WorkerWhen realloc is called and \_\_realloc\_hook has been set, then the hook 39*8d67ca89SAndroid Build Coastguard Workerfunction is called instead. 40*8d67ca89SAndroid Build Coastguard Worker 41*8d67ca89SAndroid Build Coastguard WorkerWhen free is called and \_\_free\_hook has been set, then the hook 42*8d67ca89SAndroid Build Coastguard Workerfunction is called instead. 43*8d67ca89SAndroid Build Coastguard Worker 44*8d67ca89SAndroid Build Coastguard WorkerWhen memalign is called and \_\_memalign\_hook has been set, then the hook 45*8d67ca89SAndroid Build Coastguard Workerfunction is called instead. 46*8d67ca89SAndroid Build Coastguard Worker 47*8d67ca89SAndroid Build Coastguard WorkerFor posix\_memalign, if \_\_memalign\_hook has been set, then the hook is 48*8d67ca89SAndroid Build Coastguard Workercalled, but only if alignment is a power of 2. 49*8d67ca89SAndroid Build Coastguard Worker 50*8d67ca89SAndroid Build Coastguard WorkerFor aligned\_alloc, if \_\_memalign\_hook has been set, then the hook is 51*8d67ca89SAndroid Build Coastguard Workercalled, but only if alignment is a power of 2. 52*8d67ca89SAndroid Build Coastguard Worker 53*8d67ca89SAndroid Build Coastguard WorkerFor calloc, if \_\_malloc\_hook has been set, then the hook function is 54*8d67ca89SAndroid Build Coastguard Workercalled, then the allocated memory is set to zero. 55*8d67ca89SAndroid Build Coastguard Worker 56*8d67ca89SAndroid Build Coastguard WorkerFor the two deprecated functions pvalloc and valloc, if \_\_memalign\_hook 57*8d67ca89SAndroid Build Coastguard Workerhas been set, then the hook is called with an appropriate alignment value. 58*8d67ca89SAndroid Build Coastguard Worker 59*8d67ca89SAndroid Build Coastguard WorkerThere is no hook for malloc\_usable\_size as of now. 60*8d67ca89SAndroid Build Coastguard Worker 61*8d67ca89SAndroid Build Coastguard WorkerThese hooks can be set at any time, but there is no thread safety, so 62*8d67ca89SAndroid Build Coastguard Workerthe caller must guarantee that it does not depend on allocations/frees 63*8d67ca89SAndroid Build Coastguard Workeroccurring at the same time. 64*8d67ca89SAndroid Build Coastguard Worker 65*8d67ca89SAndroid Build Coastguard WorkerImplementation Details 66*8d67ca89SAndroid Build Coastguard Worker====================== 67*8d67ca89SAndroid Build Coastguard WorkerWhen malloc hooks is enabled, then the hook pointers are set to 68*8d67ca89SAndroid Build Coastguard Workerthe current default allocation functions. It is expected that if an 69*8d67ca89SAndroid Build Coastguard Workerapp does intercept the allocation/free calls, it will eventually call 70*8d67ca89SAndroid Build Coastguard Workerthe original hook function to do allocations. If the app does not do this, 71*8d67ca89SAndroid Build Coastguard Workerit runs the risk of crashing whenever a malloc\_usable\_size call is made. 72*8d67ca89SAndroid Build Coastguard Worker 73*8d67ca89SAndroid Build Coastguard WorkerExample Implementation 74*8d67ca89SAndroid Build Coastguard Worker====================== 75*8d67ca89SAndroid Build Coastguard WorkerBelow is a simple implementation intercepting only malloc/calloc calls. 76*8d67ca89SAndroid Build Coastguard Worker 77*8d67ca89SAndroid Build Coastguard Worker void* new_malloc_hook(size_t bytes, const void* arg) { 78*8d67ca89SAndroid Build Coastguard Worker return orig_malloc_hook(bytes, arg); 79*8d67ca89SAndroid Build Coastguard Worker } 80*8d67ca89SAndroid Build Coastguard Worker 81*8d67ca89SAndroid Build Coastguard Worker auto orig_malloc_hook = __malloc_hook; 82*8d67ca89SAndroid Build Coastguard Worker __malloc_hook = new_malloc_hook; 83*8d67ca89SAndroid Build Coastguard Worker 84*8d67ca89SAndroid Build Coastguard WorkerEnabling Examples 85*8d67ca89SAndroid Build Coastguard Worker================= 86*8d67ca89SAndroid Build Coastguard Worker 87*8d67ca89SAndroid Build Coastguard Worker### For platform developers 88*8d67ca89SAndroid Build Coastguard Worker 89*8d67ca89SAndroid Build Coastguard WorkerEnable the hooks for all processes: 90*8d67ca89SAndroid Build Coastguard Worker 91*8d67ca89SAndroid Build Coastguard Worker adb shell stop 92*8d67ca89SAndroid Build Coastguard Worker adb shell setprop libc.debug.hooks.enable 1 93*8d67ca89SAndroid Build Coastguard Worker adb shell start 94*8d67ca89SAndroid Build Coastguard Worker 95*8d67ca89SAndroid Build Coastguard WorkerEnable malloc hooks using an environment variable: 96*8d67ca89SAndroid Build Coastguard Worker 97*8d67ca89SAndroid Build Coastguard Worker adb shell 98*8d67ca89SAndroid Build Coastguard Worker # export LIBC_HOOKS_ENABLE=1 99*8d67ca89SAndroid Build Coastguard Worker # ls 100*8d67ca89SAndroid Build Coastguard Worker 101*8d67ca89SAndroid Build Coastguard WorkerAny process spawned from this shell will run with malloc hooks enabled. 102*8d67ca89SAndroid Build Coastguard Worker 103*8d67ca89SAndroid Build Coastguard Worker### For app developers 104*8d67ca89SAndroid Build Coastguard Worker 105*8d67ca89SAndroid Build Coastguard WorkerEnable malloc hooks for a specific program/application: 106*8d67ca89SAndroid Build Coastguard Worker 107*8d67ca89SAndroid Build Coastguard Worker adb shell setprop wrap.<APP> '"LIBC_HOOKS_ENABLE=1"' 108*8d67ca89SAndroid Build Coastguard Worker 109*8d67ca89SAndroid Build Coastguard WorkerFor example, to enable malloc hooks for the google search box: 110*8d67ca89SAndroid Build Coastguard Worker 111*8d67ca89SAndroid Build Coastguard Worker adb shell setprop wrap.com.google.android.googlequicksearchbox '"LIBC_HOOKS_ENABLE=1 logwrapper"' 112*8d67ca89SAndroid Build Coastguard Worker adb shell am force-stop com.google.android.googlequicksearchbox 113*8d67ca89SAndroid Build Coastguard Worker 114*8d67ca89SAndroid Build Coastguard WorkerNOTE: On pre-O versions of the Android OS, property names had a length limit 115*8d67ca89SAndroid Build Coastguard Workerof 32. This meant that to create a wrap property with the name of the app, it 116*8d67ca89SAndroid Build Coastguard Workerwas necessary to truncate the name to fit. On O, property names can be 117*8d67ca89SAndroid Build Coastguard Workeran order of magnitude larger, so there should be no need to truncate the name 118*8d67ca89SAndroid Build Coastguard Workerat all. 119