Race condition in android dlopen()?

帅比萌擦擦* 提交于 2019-12-01 17:39:46

问题


My Android app has a simple "loader" NativeActivity with a very simple android_main() which only loads a different shared object and passes control to it:

typedef void (*Tandroid_main)( android_app*);
void android_main( android_app* state )
{
    void* glib = dlopen("libmain.so", RTLD_NOW);
    void* fmain = dlsym(glib, "android_main");
    Tandroid_main libmain = (Tandroid_main)fmain;
    libmain(state)
}

This works well.. about half of the times. Other times it crashes since dlopen() fails and return NULL with errno=2 (No such file).
Due to the strange inconsistency of this occurrence I suspected a timing issue and indeed, adding a sleep(1) before dlopen() stopped it from happening. Something more robust than sleep(1) would be just trying it in a loop:

int count = 0;
void* glib = dlopen(soName, RTLD_NOW);
while(glib == NULL) {
    sched_yield();
    ++count;
    glib = dlopen(soName, RTLD_NOW);
}

The count I'm getting from this loop is usually something in the range of 10-70 on my device. But this is a hackish ugly solution.

What is really going on here? How come I can only load other shared objects only slightly after the NativeActivity starts? Is there a better way to find when when it's safe to load it?

It should be noted that I am also calling System.loadLibrary("main") from my NativeActivity's onCreate()


回答1:


Not sure, but it is recommended to call loadLibrary() from a static initializer:

public class MainActivity extends Activity {
    static {
        System.loadLibrary("main")
    }
    ...
}

Does it help?



来源:https://stackoverflow.com/questions/32425865/race-condition-in-android-dlopen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!