Skip to content

Commit

Permalink
Handle getApplicationContext==null gracefully during init
Browse files Browse the repository at this point in the history
Differential Revision: D49024899

fbshipit-source-id: dddc1b1303585e3674f648cb588d8c8207589a16
  • Loading branch information
simpleton authored and facebook-github-bot committed Sep 7, 2023
1 parent 1a4eb7e commit c297ac5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
5 changes: 3 additions & 2 deletions java/com/facebook/soloader/SoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public static void init(
try {
isEnabled = initEnableConfig(context);
if (isEnabled) {
sAppType = getAppType(context, flags);
sAppType = getAppType(context);
if ((flags & SOLOADER_EXPLICITLY_ENABLE_BACKUP_SOSOURCE) == 0
&& SysUtil.isSupportedDirectLoad(context, sAppType)) {
// SoLoader doesn't need backup soSource if it supports directly loading .so file from APK
Expand Down Expand Up @@ -569,11 +569,12 @@ private static synchronized void initSoLoader(
sSoFileLoader = new SoFileLoaderImpl();
}

private static int getAppType(Context context, int flags) {
private static int getAppType(Context context) {
if (sAppType != AppType.UNSET) {
return sAppType;
}
if (context == null) {
LogUtil.d(TAG, "context is null, fallback to THIRD_PARTY_APP appType");
return AppType.THIRD_PARTY_APP;
}

Expand Down
19 changes: 12 additions & 7 deletions java/com/facebook/soloader/SysUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,22 @@ public static boolean is64Bit() {
return android.os.Process.is64Bit();
}

public static boolean isSupportedDirectLoad(Context context, int appType) throws IOException {
public static boolean isSupportedDirectLoad(@Nullable Context context, int appType)
throws IOException {
if (appType == SoLoader.AppType.SYSTEM_APP) {
// Ideally, system_app permanently stores dso files uncompressed and page-aligned, even with
// FLAG_EXTRACT_NATIVE_LIBS flag. But to support a specific Oculus's sideload method, we
// need this extra checking. ref: D27831042
return isApkUncompressedDso(context);
if (context != null && context.getApplicationContext() != null) {
// Ideally, system_app permanently stores dso files uncompressed and page-aligned, even
// with FLAG_EXTRACT_NATIVE_LIBS flag. But to support a specific Oculus's sideload method,
// we need this extra checking. ref: D27831042
return isApkUncompressedDso(context);
}
return true;
} else {
return isDisabledExtractNativeLibs(context);
}
}

public static boolean isDisabledExtractNativeLibs(Context context) {
public static boolean isDisabledExtractNativeLibs(@Nullable Context context) {
return context != null
&& (context.getApplicationInfo().flags & ApplicationInfo.FLAG_EXTRACT_NATIVE_LIBS) == 0;
}
Expand Down Expand Up @@ -412,7 +416,8 @@ public static boolean is64Bit() {
return is64bit;
}

public static boolean isSupportedDirectLoad(Context context, int appType) throws IOException {
public static boolean isSupportedDirectLoad(@Nullable Context context, int appType)
throws IOException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Android starts to support directly loading from API 23.
// https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#opening-shared-libraries-directly-from-an-apk
Expand Down

0 comments on commit c297ac5

Please sign in to comment.