I am trying to build (with clang) my application with the address sanitizer described here (https://github.com/google/sanitizers/wiki/AddressSanitizer, more precisely here: https://github.com/google/sanitizers/wiki/AddressSanitizerOnAndroid), but I am having trouble understanding the whole process, especially using gradle.
It looks like there is at least 3 ways of enabling it:
1°) Following the first link, t says that all you have to do is doing this:
adding
-fsanitize=address
to the cppFlags + optional-fno-omit-frame-pointer
adding
-fsanitize=address
to the linker flags (is it necessary?)
2°) following the second link, it seems that you have to do:
- same as first
- root a device then run asan_device_setup on it through adb
- add a
LD_PRELOAD=libclang_rt.asan-arm-android.so
somewhere? I guess it should be put in the 'arguments' part of the gradle externalNativeBuild? But where can the app find this library? Do I have to link it myself? Or is it already somewhere on the device?
3°) I also found a "new" way of doing it, which shouldn't requires roots access (well it does but it is a bug due to be corrected at some point):
https://virtualrealitypop.com/oreo-ndk-secrets-7d075a9b084
This method actually does what is done in first and second point, plus runs the app by launching a shell script that exports some values for asan to work.
As far as my investigation goes, I am a bit confused on what is the proper method to have a fully sanitized app (with statically linked libraries) working on my rooted emulator.
The farther I have gone was to actually build and launch the app (using 2°), but without specifying LD_PRELOAD flag), but the app crashes with a container-overflow in some eglMakeCurrent function that isn't even part of my code, and I don't get any stack for it:
02-19 16:26:21.553 28771-28789/com.mycompany.myapp I/zygote: Background concurrent copying GC freed 10159(1175KB) AllocSpace objects, 12(304KB) LOS objects, 50% free, 2MB/4MB, paused 144.861ms total 1.252s
[ 02-19 16:26:21.554 28771:28956 I/ ]
=================================================================
[ 02-19 16:26:21.554 28771:28956 I/ ]
[ 02-19 16:26:21.557 28771:28956 I/ ]
[ 02-19 16:26:21.563 28771:28956 I/ ]
==28771==ERROR: AddressSanitizer: container-overflow on address 0xa136e990 at pc 0xa49849e2 bp 0x82e60558 sp 0x82e60128
[ 02-19 16:26:21.563 28771:28956 I/ ]
[ 02-19 16:26:21.565 28771:28956 I/ ]
[ 02-19 16:26:21.566 28771:28956 I/ ]
WRITE of size 2 at 0xa136e990 thread T334 (GLThread 337)
[ 02-19 16:26:21.566 28771:28956 I/ ]
I am not sure it is a real overflow because I am not sure all my app is built with sanitizer (I have built my so+ all my statics with it but is it enough?), and https://github.com/google/sanitizers/wiki/AddressSanitizerContainerOverflow says that if your whole app isn't built with sanitizer you may get false positives.
So my questions are:
A°) has someone actually managed to build a sanitized app using android studio?
B°) If yes, what is the correct way to do it (meaning the one that will be supported)?