5

I have an App that uses a library with native libs and now in Android 11 is crashing due to fdsan.

I have contacted the library providers, but they are not going to deliver a new version of the library with this issue solved, so I need to be able to somehow disable fdsan.

I think the solution would be to call when I am on Android 11 or higher:

if(android_get_device_api_level()>=30)
     android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);

But I am unable to compile because of error:

use of undeclared identifier 'android_fdsan_set_error_level'

Because this was only added in Android 10 (API 29)

So my question is how can I call this function in my project? I am creating a specific cpp file for this and a specific library for this, so I would need to some how for this library say that it is only for Android 10 or higher. Is there some definition in the cpp file or in the Android.mk file that I can use to be able to compile this specific library that I will invoke from Java in Android 11 or higher only when my App starts?

1 Answers1

2

According to the documentation here you should do it like this:

if (android_fdsan_set_error_level) {
    android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
}

You will have to set your target SDK to 10 or above for this to work.

Alternatively, if you are compiling this in to a separate native binary, you can use conditional compilation.

Include this file.

Then do something like this:

#if __ANDROID_API__ >= __ANDROID_API_P__
    android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE);
#endif

Just make sure this binary is never loaded on older devices, else the linker will give an error.
Depending on how your whole project is built, you may need to ship a separate APK for this in a bundle.

Finally, if all else fails, you may try using dlopen and dlsym to load the function dynamically at runtime, but you will need to find out which module exactly it is included in.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • The #if __ANDROID_API__ >= __ANDROID_API_P__ will not work. This is a confitional compilation, since I am targeting older Android versions, it will simply not compile – Filipe Madureira Sep 21 '21 at 13:31
  • I did try dlopen and dlsym, but never found in what module it is included in – Filipe Madureira Sep 21 '21 at 13:32
  • @FilipeMadureira here is what you can try: compile a sample so with this function call targeting Android 10 or above, then run `objdump -T` command on this so. You will need Linux or Mac to use this command, but it will show you exactly where each function is imported from. – Lev M. Sep 21 '21 at 16:44
  • @FilipeMadureira from what I can see, I am pretty sure google built it in to their glibc, called "Bionic" on Android, but I am not entirely sure about the so name you will need fro `dlopen`. – Lev M. Sep 21 '21 at 16:45
  • 1
    Thanks on the bionic/glibc tip. It is in the libc.so, so I loaded the android_fdsan_set_error_level() from there and got it working – Filipe Madureira Sep 23 '21 at 08:50
  • 1
    Hi, I still have this issue, while having targetSdkVersion 32, or what you meant by target android 10? Do you have working sample? – Anton Shkurenko Jun 02 '22 at 18:00
  • @LevM. __ANDROID_API__ is associated with minSdkVersion and not targetSdk version. Please edit the answer. https://developer.android.com/ndk/guides/sdk-versions#minsdkversion – Amit Kaushik Mar 08 '23 at 07:59