facebook / SoLoader

Native code loader for Android
Apache License 2.0
1.34k stars 175 forks source link

SoLoader causes instrumented tests to crash #94

Closed CiaraSouthgateTR closed 1 year ago

CiaraSouthgateTR commented 2 years ago

Bug Report

SoLoader looks for libfbjni in the .test path during instrumented tests. I am not sure how it makes that decision, since package name still reports without .test. Since the tests use a debug variant, I don't see a way to prevent Flipper loading during instrumented tests.

java.lang.UnsatisfiedLinkError: dlopen failed: library "/data/app/com.appscoop.takingroot.staging.test-1Dt01Y4N51I9qkS3sSAU3w==/base.apk!/lib/x86/libfbjni.so" not found
  at java.lang.Runtime.load0(Runtime.java:938)
  at java.lang.System.load(System.java:1631)
  at com.facebook.soloader.SoLoader$1.load(SoLoader.java:529)
  at com.facebook.soloader.DirectApkSoSource.loadLibrary(DirectApkSoSource.java:77)
  at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:1038)
  at com.facebook.soloader.SoLoader.loadLibraryBySoNameImpl(SoLoader.java:914)
  at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:826)
  at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:773)
  at com.facebook.soloader.NativeLoaderToSoLoaderDelegate.loadLibrary(NativeLoaderToSoLoaderDelegate.java:29)
  at com.facebook.soloader.nativeloader.NativeLoader.loadLibrary(NativeLoader.java:51)
  at com.facebook.soloader.nativeloader.NativeLoader.loadLibrary(NativeLoader.java:30)
  at com.facebook.jni.HybridData.<clinit>(HybridData.java:34)
  at com.facebook.flipper.android.FlipperThread.run(FlipperThread.java:25)
libfbjni.so not found on /vendor/lib
libfbjni.so not found on /system/lib
couldn't find DSO to load: libfbjni.so
 SoSource 0: com.facebook.soloader.DirectorySoSource[root = /data/app/com.appscoop.takingroot.staging-_zWOlH2oMX4dVzDGOq6zXw==/lib/x86 flags = 0]
 SoSource 1: com.facebook.soloader.DirectApkSoSource[root = /data/app/com.appscoop.takingroot.staging.test-1Dt01Y4N51I9qkS3sSAU3w==/base.apk!/lib/x86]
 SoSource 2: com.facebook.soloader.DirectorySoSource[root = /vendor/lib flags = 2]
 SoSource 3: com.facebook.soloader.DirectorySoSource[root = /system/lib flags = 2]
 Native lib dir: /data/app/com.appscoop.takingroot.staging-_zWOlH2oMX4dVzDGOq6zXw==/lib/x86
 result: 0
FATAL EXCEPTION: FlipperEventBaseThread
Process: com.appscoop.takingroot.staging, PID: 5862
java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libfbjni.so
    SoSource 0: com.facebook.soloader.DirectorySoSource[root = /data/app/com.appscoop.takingroot.staging-_zWOlH2oMX4dVzDGOq6zXw==/lib/x86 flags = 0]
    SoSource 1: com.facebook.soloader.DirectApkSoSource[root = /data/app/com.appscoop.takingroot.staging.test-1Dt01Y4N51I9qkS3sSAU3w==/base.apk!/lib/x86]
    SoSource 2: com.facebook.soloader.DirectorySoSource[root = /vendor/lib flags = 2]
    SoSource 3: com.facebook.soloader.DirectorySoSource[root = /system/lib flags = 2]
    Native lib dir: /data/app/com.appscoop.takingroot.staging-_zWOlH2oMX4dVzDGOq6zXw==/lib/x86
result: 0
    at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:1098)
    at com.facebook.soloader.SoLoader.loadLibraryBySoNameImpl(SoLoader.java:914)
    at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:826)
    at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:773)
    at com.facebook.soloader.NativeLoaderToSoLoaderDelegate.loadLibrary(NativeLoaderToSoLoaderDelegate.java:29)
    at com.facebook.soloader.nativeloader.NativeLoader.loadLibrary(NativeLoader.java:51)
    at com.facebook.soloader.nativeloader.NativeLoader.loadLibrary(NativeLoader.java:30)
    at com.facebook.jni.HybridData.<clinit>(HybridData.java:34)
    at com.facebook.flipper.android.FlipperThread.run(FlipperThread.java:25)
FATAL EXCEPTION: FlipperConnectionThread
Process: com.appscoop.takingroot.staging, PID: 5862
 java.lang.NoClassDefFoundError: <clinit> failed for class com.facebook.flipper.android.EventBase; see exception in other thread
    at com.facebook.flipper.android.FlipperThread.run(FlipperThread.java:25)

To Reproduce

Run an instrumented test

Environment

Android API 31 debugImplementation 'com.facebook.flipper:flipper:0.137.0' debugImplementation 'com.facebook.soloader:soloader:0.10.3' releaseImplementation 'com.facebook.flipper:flipper-noop:0.137.0'

henryzx commented 2 years ago

same here. any update?

CiaraSouthgateTR commented 2 years ago

@henryzx I found a workaround that lets me run my tests: https://stackoverflow.com/questions/21367646/how-to-determine-if-android-application-is-started-with-junit-testing-instrument?noredirect=1&lq=1

RowlandOti commented 2 years ago

Co-experiencing this issue. @CiaraSouthgateFT are you able to clarify how that solution you posted helps?

twyatt commented 2 years ago

@RowlandOti I believe what @CiaraSouthgateFT was saying is that they conditionally don't use SoLoader if they're within an instrumentation test.

For example, if you create a file under your test or androidTest source set:

package com.example

object TestMarker

Then you can detect if you're running in a test (in your main or debug source set):

private val isTest: Boolean
    get() = try {
        Class.forName("com.example.TestMarker")
        true
    } catch (e: ClassNotFoundException) {
        false
    }

Then, where you normally use SoLoader:

if (!isTest) SoLoader.init(context, false)

...it doesn't "fix" the issue, but does allow instrumentation tests to run (where SoLoader and tools like Flipper, are not needed).

Hope that helps.

ericschaal commented 2 years ago

Hi,

I encountered the same issue while running Detox (instrumented tests) on a React Native 0.68 project with Hermes enabled. AndroidJUnitRunner would instantly crash at startup.

...
crashes with java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libhermes.so

I was able to solve it by downgrading SoLoader to v0.9.0+ using resolutionStrategy.

Any idea why this is happening?

EDIT: I also tried various 0.10.x versions v0.10.1 is working v0.10.2 is crashing v0.10.3 is crashing

Looks like something broke in v0.10.2

netshade commented 2 years ago

I also ran into the same problem as @ericschaal and your research helped me get our Android Detox tests running again. Thanks very much.

mikehardy commented 2 years ago

In turn, I was mentioned in that linked wix/Detox issue and was curious what was going on here. It appears that there has been a mountain of work in this repo since the last release, all directly affecting the code paths in question, so examining the diff between 0.10.1 (working) and 0.10.2 (not working) isn't that fruitful since "current stable" is so different.

Begs the question: is it possible to get a new release from current repo state (even if just attached) and some guidance on how to integrate it via gradle so testing + a reproduction repo is productive?

mehmetarikan commented 2 years ago

Stumble upon the same issue when updating the flipper to latest(0.149.0) version. After digging it I noticed that flipper updates its soLoader dependency on v0.119.0 from 0.10.1 to 0.10.3. I verified flipper v0.118.1 works without any issue but v0.119.0 crashes all instrumented tests to fail. I disabled flipper for the tests but we also use yogalayout and have to load soLoader and due to this transitive dependency tests were still failing. I fix the problem by enforcing soLoader to v0.10.1.

implementation('com.facebook.soloader:soloader') {
        version {
            strictly '0.10.1'
        }
    }
tinder-cesardiez commented 2 years ago

Issue seems to be gone for us after updating the SoLoader dependency to 10.0.4: https://github.com/facebook/SoLoader/releases/tag/v0.10.4

RowlandOti commented 2 years ago

Yes, I can see a commit that explains why this happens as well as the applied fix: https://github.com/facebook/SoLoader/commit/90084463c550b537501524f00153575d010b2f21

mikehardy commented 2 years ago

So it seems soloader 0.10.4 fixes it It appears Flipper 0.154.0 ingests the fix https://github.com/facebook/flipper/commit/23f7e2bdee188036797a3ecafb2020b0ece6a604

I'm posting this info for react-native here for release planning purposes https://github.com/reactwg/react-native-releases/discussions/26#discussioncomment-3166381 (just merged on react-native main, should hopefully be in react-native 0.70.0 when released)

Gerlison commented 2 years ago

Issue seems to be gone for us after updating the SoLoader dependency to 10.0.4: https://github.com/facebook/SoLoader/releases/tag/v0.10.4

Yes, I can see a commit that explains why this happens as well as the applied fix: 9008446

So it seems soloader 0.10.4 fixes it It appears Flipper 0.154.0 ingests the fix facebook/flipper@23f7e2b

I'm posting this info for react-native here for release planning purposes reactwg/react-native-releases#26 (comment) (just merged on react-native main, should hopefully be in react-native 0.70.0 when released)

All these answers helped me. Firstly i tried just to use specific version of soloader (0.10.4+) image , and that alone didn't work for detox test build, because it was causing another error. Which, later, I solved by doing the following guide: https://wix.github.io/Detox/docs/introduction/android/#setting-detox-up-as-a-compiling-dependency

Thank you all

ko-devHong commented 1 year ago

Upgrading from 63.0 to 0.70.0 resolved the issue with ios, but the problem is not solved at all in Android. It seems to be a soloader problem that many people refer to.

i already added implementation 'com.facebook.soloader:soloader:0.10.4+'

this is my log

752-8752/**** E/SoLoader: couldn't find DSO to load: libjsc.so
        SoSource 0: com.facebook.soloader.DirectorySoSource[root = /data/app/~~Es-u6o8MM1pCV02wkZHNxQ==/*****-zVphl1YCSx-HLrtHV-X58Q==/lib/arm64 flags = 0]
        SoSource 1: com.facebook.soloader.DirectApkSoSource[root = (/data/app/~~Es-u6o8MM1pCV02wkZHNxQ==/*****-zVphl1YCSx-HLrtHV-X58Q==/base.apk!/lib/arm64-v8a, )]
        SoSource 2: com.facebook.soloader.DirectorySoSource[root = /system/lib64 flags = 2]
        SoSource 3: com.facebook.soloader.DirectorySoSource[root = /vendor/lib64 flags = 2]
        Native lib dir: /data/app/~~Es-u6o8MM1pCV02wkZHNxQ==/*****-zVphl1YCSx-HLrtHV-X58Q==/lib/arm64
     result: 0
2022-10-23 19:02:05.123 8752-8752/***** E/SoLoader: couldn't find DSO to load: libjscexecutor.so caused by: couldn't find DSO to load: libjsc.so
        SoSource 0: com.facebook.soloader.DirectorySoSource[root = /data/app/~~Es-u6o8MM1pCV02wkZHNxQ==/*****-zVphl1YCSx-HLrtHV-X58Q==/lib/arm64 flags = 0]
        SoSource 1: com.facebook.soloader.DirectApkSoSource[root = (/data/app/~~Es-u6o8MM1pCV02wkZHNxQ==/*******-zVphl1YCSx-HLrtHV-X58Q==/base.apk!/lib/arm64-v8a, )]
        SoSource 2: com.facebook.soloader.DirectorySoSource[root = /system/lib64 flags = 2]
        SoSource 3: com.facebook.soloader.DirectorySoSource[root = /vendor/lib64 flags = 2]
        Native lib dir: /data/app/~~Es-u6o8MM1pCV02wkZHNxQ==/*****-zVphl1YCSx-HLrtHV-X58Q==/lib/arm64
     result: 0 result: 0
2022-10-23 19:02:05.124 8752-8752/******* E/SoLoader: couldn't find DSO to load: libhermes.so caused by: dlopen failed: library "/vendor/lib64/libhermes.so" not found result: 0
2022-10-23 19:02:05.124 1233-1233/? E/audit: type=1400 audit(1666519325.120:34327460): avc:  denied  { read } for  pid=8752 comm="gym.app.staging" name="libhermes.so" dev="dm-9" ino=3278 scontext=u:r:untrusted_app:s0:c243,c258,c512,c768 tcontext=u:object_r:vendor_file:s0 tclass=file permissive=0 SEPF_SM-S906N_12_0001 audit_filtered
2022-10-23 19:02:05.125 8752-8752/****** E/AndroidRuntime: FATAL EXCEPTION: main
    Process: *****, PID: 8752
    java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libhermes.so caused by: dlopen failed: library "/vendor/lib64/libhermes.so" not found result: 0
        at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:1127)
        at com.facebook.soloader.SoLoader.loadLibraryBySoNameImpl(SoLoader.java:943)
        at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:855)
        at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:802)
        at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:772)
        at com.facebook.hermes.reactexecutor.HermesExecutor.loadLibrary(HermesExecutor.java:25)
        at com.facebook.hermes.reactexecutor.HermesExecutor.<clinit>(HermesExecutor.java:19)
        at com.facebook.hermes.reactexecutor.HermesExecutor.loadLibrary(HermesExecutor.java:23)
        at com.facebook.react.ReactInstanceManagerBuilder.getDefaultJSExecutorFactory(ReactInstanceManagerBuilder.java:393)
        at com.facebook.react.ReactInstanceManagerBuilder.build(ReactInstanceManagerBuilder.java:343)
        at com.facebook.react.ReactNativeHost.createReactInstanceManager(ReactNativeHost.java:96)
        at com.facebook.react.ReactNativeHost.getReactInstanceManager(ReactNativeHost.java:42)
        at com.soundgym.app.MainApplication.onCreate(MainApplication.java:76)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1211)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7588)
        at android.app.ActivityThread.access$1700(ActivityThread.java:315)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2286)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8751)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
     Caused by: java.lang.UnsatisfiedLinkError: dlopen failed: library "/vendor/lib64/libhermes.so" not found
        at java.lang.Runtime.load0(Runtime.java:929)
        at java.lang.System.load(System.java:1620)
        at com.facebook.soloader.SoLoader$1.load(SoLoader.java:558)
        at com.facebook.soloader.DirectorySoSource.loadLibraryFrom(DirectorySoSource.java:110)
        at com.facebook.soloader.DirectorySoSource.loadLibrary(DirectorySoSource.java:63)
simpleton commented 1 year ago

Hi @ko-devHong, can you share the crashy apk?

simpleton commented 1 year ago

This issue should be addressed in 0.10.4.