Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ASan on Android with LD_PRELOAD #1154

Open
gamebaker opened this issue Oct 21, 2019 · 7 comments
Open

Using ASan on Android with LD_PRELOAD #1154

gamebaker opened this issue Oct 21, 2019 · 7 comments

Comments

@gamebaker
Copy link

gamebaker commented Oct 21, 2019

Hi, I'm using ASan as a shared library on a rooted Android phone, with the LD_PRELOAD method, by these following steps:

  1. Copy "system/bin/app_process64" to "app_process64.real";
  2. Replace the original "app_process64" file with a bash script:
#!/system/bin/sh
export ASAN_OPTIONS=start_deactivated=1,alloc_dealloc_mismatch=0,malloc_context_size=0,allow_user_segv_handler=1,halt_on_error=1
export LD_PRELOAD=/data/libclang_rt.asan-aarch64-android.so
exec /system/bin/app_process64.real $@
  1. Launch target app with command line:
am start -n com.test.librarytest/.MainActivity

The system works, but it always ends up with an error:

root@A33:/ # am start -n com.test.librarytest/.MainActivity
WARNING: linker: /data/libclang_rt.asan-aarch64-android.so: unused DT entry: type 0x6ffffef5 arg 0x10d30
WARNING: linker: /data/libclang_rt.asan-aarch64-android.so: unused DT entry: type 0x6ffffffe arg 0x163c4
WARNING: linker: /data/libclang_rt.asan-aarch64-android.so: unused DT entry: type 0x6fffffff arg 0x3
=================================================================
==27966==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x0055bc37a9b0 in thread T0
    #0 0x7f99b752db  (/data/libclang_rt.asan-aarch64-android.so+0x682db)
    #1 0x7f98689eef  (/system/lib64/libselinux.so+0x5eef)
    #2 0x7f98689403  (/system/lib64/libselinux.so+0x5403)
    #3 0x7f99a3ce5f  (/system/lib64/libandroid_runtime.so+0xd3e5f)
    #4 0x7f999fa17f  (/system/lib64/libandroid_runtime.so+0x9117f)
    #5 0x7f999fa32b  (/system/lib64/libandroid_runtime.so+0x9132b)
    #6 0x5589e96eaf  (/system/bin/app_process64.real+0x1eaf)
    #7 0x7f99877507  (/system/lib64/libc.so+0x13507)
    #8 0x5589e9727b  (/system/bin/app_process64.real+0x227b)

AddressSanitizer can not describe address in more detail (wild memory access suspected).
SUMMARY: AddressSanitizer: bad-free (/data/libclang_rt.asan-aarch64-android.so+0x682db)
==27966==ABORTING
1|root@A33:/ #

I assume Android alloc some memory before ASan loaded and then free them in Asan, which causes this problem, but I don't know how to fix it. I've been stuck here for a while, so any suggestions?

@DanAlbert
Copy link
Member

See https://developer.android.com/ndk/guides/asan for the recommended way of using ASan with Android apps.

@eugenis
Copy link
Contributor

eugenis commented Oct 21, 2019 via email

@gamebaker gamebaker reopened this Oct 22, 2019
@gamebaker
Copy link
Author

gamebaker commented Oct 22, 2019

@DanAlbert The wrap.sh way is only available for API level 27 and above, but the device for testing is Android 5.1.1, so doesn't work here, sigh. Thanks for replying.

@gamebaker
Copy link
Author

gamebaker commented Oct 22, 2019

@eugenis The Android version is 5.1.1, by "corresponding change in compiler-rt" do you mean compile of ASan, or compile of Android?
I've tried adding verbosity=2,debug=1 to ASAN_OPTIONS, but it gives the same log, nothing changed.
I'm kind of a newbie to this, so I gonna try symbolizing the stack trace to make some progress, hope I can figure this out.
Thanks for replying, it helps.

@eugenis
Copy link
Contributor

eugenis commented Oct 24, 2019 via email

@Enna1
Copy link

Enna1 commented Feb 10, 2023

Hi, I encountered a similar issue:
A chunk of memory was malloced by /apex/com.android.runtime/lib64/bionic/libc.so, but freed by /system/lib64/lbclang_rt.asan-aarch64-android.so .
The memory was malloced here: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/__cxa_thread_atexit_impl.cpp#36
any suggestions would be appreciated.
Thanks!

@linux-xhyang
Copy link

Hi, I encountered a similar issue:
I've noticed a pattern: if during the initialization of a static variable, the assigned value is an object created with 'new' from bionic, this issue occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants