Skip to content

Commit

Permalink
Add SoLoader flag to skip calling merged JNI_OnLoad
Browse files Browse the repository at this point in the history
Summary:
`SoLoader.loadLibrary()` currently crashes if you attempt to load a merged
library that does not define JNI_OnLoad.  In the past, we've told people
to work around this by just not calling `loadLibrary` for those libs.
However, I'm about to land a cxx_library with no entry points, which
will be loaded solely to ensure its static constructors are called.
(Those static constructors register Caffe2 operators that are called later.)

Add a flag for SoLoader to skip attempting to call JNI_OnLoad,
which will be used for loading the operator library.

Reviewed By: joelmccall

Differential Revision: D14248187

fbshipit-source-id: 67f34b7f03754cc0c7fef05da64ff1701cf68ef7
  • Loading branch information
dreiss authored and facebook-github-bot committed Jul 2, 2019
1 parent 39f1a78 commit a672eff
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions java/com/facebook/soloader/SoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ public class SoLoader {
*/
public static final int SOLOADER_DISABLE_BACKUP_SOSOURCE = (1 << 3);

/**
* Skip calling JNI_OnLoad if the library is merged. This is necessary for libraries that don't
* define JNI_OnLoad and are only loaded for their side effects (like static constructors
* registering callbacks). DO NOT use this to allow implicit JNI registration (by naming your
* methods Java_com_facebook_whatever) because that is buggy on Android.
*/
public static final int SOLOADER_SKIP_MERGED_JNI_ONLOAD = (1 << 4);

@GuardedBy("sSoSourcesLock")
private static int sFlags;

Expand Down Expand Up @@ -641,19 +649,21 @@ private static boolean loadLibraryBySoName(
}
}

boolean isAlreadyMerged =
!TextUtils.isEmpty(shortName) && sLoadedAndMergedLibraries.contains(shortName);
if (mergedLibName != null && !isAlreadyMerged) {
if (SYSTRACE_LIBRARY_LOADING) {
Api18TraceUtils.beginTraceSection("MergedSoMapping.invokeJniOnload[" + shortName + "]");
}
try {
Log.d(TAG, "About to merge: " + shortName + " / " + soName);
MergedSoMapping.invokeJniOnload(shortName);
sLoadedAndMergedLibraries.add(shortName);
} finally {
if ((loadFlags & SOLOADER_SKIP_MERGED_JNI_ONLOAD) == 0) {
boolean isAlreadyMerged =
!TextUtils.isEmpty(shortName) && sLoadedAndMergedLibraries.contains(shortName);
if (mergedLibName != null && !isAlreadyMerged) {
if (SYSTRACE_LIBRARY_LOADING) {
Api18TraceUtils.endSection();
Api18TraceUtils.beginTraceSection("MergedSoMapping.invokeJniOnload[" + shortName + "]");
}
try {
Log.d(TAG, "About to merge: " + shortName + " / " + soName);
MergedSoMapping.invokeJniOnload(shortName);
sLoadedAndMergedLibraries.add(shortName);
} finally {
if (SYSTRACE_LIBRARY_LOADING) {
Api18TraceUtils.endSection();
}
}
}
}
Expand Down

0 comments on commit a672eff

Please sign in to comment.