Skip to content

Commit

Permalink
Get the highest priority ABI by parsing current executable's path ins…
Browse files Browse the repository at this point in the history
…tead of parsing already-extracted libraries.
  • Loading branch information
codyi96 committed Aug 27, 2019
1 parent 33ee0cd commit dc3025a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 58 deletions.
2 changes: 1 addition & 1 deletion java/com/facebook/soloader/ExoSoSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private final class ExoUnpacker extends Unpacker {

Set<String> librariesAbiSet = new LinkedHashSet<>();

for (String abi : SysUtil.getSortedSupportedAbisByNativeLibrary(mContext)) {
for (String abi : SysUtil.getSupportedAbis()) {
File abiDir = new File(exoDir, abi);
if (!abiDir.isDirectory()) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion java/com/facebook/soloader/ExtractFromZipSoSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ final ZipDso[] ensureDsos() {
Set<String> librariesAbiSet = new LinkedHashSet<>();
HashMap<String, ZipDso> providedLibraries = new HashMap<>();
Pattern zipSearchPattern = Pattern.compile(mZipSearchPattern);
String[] supportedAbis = SysUtil.getSortedSupportedAbisByNativeLibrary(mSoSource.mContext);
String[] supportedAbis = SysUtil.getSupportedAbis();
Enumeration<? extends ZipEntry> entries = mZipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
Expand Down
86 changes: 30 additions & 56 deletions java/com/facebook/soloader/SysUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
Expand Down Expand Up @@ -80,61 +81,6 @@ public static String[] getSupportedAbis() {
}
}

/**
* Return an list of ABIs we supported on this device ordered according to preference.
* @param context
* @return Ordered array of supported ABIs, the ABI nativeLibrary used is top priority.
*/
public static String[] getSortedSupportedAbisByNativeLibrary(Context context) {
// Only use 32-bit abi when API < 21
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return new String[]{Build.CPU_ABI, Build.CPU_ABI2};
}
String[] supportedAbis = LollipopSysdeps.getSupportedAbis();
Log.i(TAG, "SupportedAbis is " + Arrays.toString(supportedAbis));
// If context or nativeLibraryDir is null, return abis unsorted.
if (context == null || context.getApplicationInfo().nativeLibraryDir == null) {
return supportedAbis;
}
File appLibsDir = new File(context.getApplicationInfo().nativeLibraryDir);
if (!appLibsDir.exists()
|| !appLibsDir.canRead()
|| appLibsDir.listFiles() == null) {
return supportedAbis;
}
String topPriorityABI = null;
for (File appLib : appLibsDir.listFiles()) {
try {
MinElf.ISA isaOfFile = MinElf.getISAFromFile(appLib);
if (isaOfFile != MinElf.ISA.NOT_SO && isaOfFile != MinElf.ISA.OTHERS) {
topPriorityABI = isaOfFile.toString();
Log.i(TAG, "topPriorityAbi is " + topPriorityABI);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (topPriorityABI != null) {
final String finalTopPriorityABI = topPriorityABI;
Arrays.sort(supportedAbis, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1.equals(finalTopPriorityABI)) {
return -1;
} else if (o2.equals(finalTopPriorityABI)) {
return 1;
} else {
return 0;
}
}
});
Log.i(TAG, "SortedSupportedAbis by NativeLibrary is " + Arrays.toString(supportedAbis));
}
// If we can't judge which abi is used by nativeLibrary, then use result of default method.
return supportedAbis;
}

/**
* Pre-allocate disk space for a file if we can do that
* on this version of the OS.
Expand Down Expand Up @@ -183,7 +129,35 @@ public static void dumbDeleteRecursive(File file) throws IOException {
private static final class LollipopSysdeps {
@DoNotOptimize
public static String[] getSupportedAbis() {
return Build.SUPPORTED_ABIS;
String[] supportedAbis = Build.SUPPORTED_ABIS;
ArrayList<String> priorAbis = new ArrayList<>();
try {
// SoLoader will give first rank to arm64-v8a & x86_64, if current process is app_process64.
// Otherwise(means current process is app_process32), give first rank to armeabi-v7a & x86.
if (Os.readlink("/proc/self/exe").contains("64")) {
priorAbis.add(MinElf.ISA.AARCH64.toString());
priorAbis.add(MinElf.ISA.X86_64.toString());
} else {
priorAbis.add(MinElf.ISA.ARM.toString());
priorAbis.add(MinElf.ISA.X86.toString());
}
} catch(ErrnoException e) {
e.printStackTrace();
}
final ArrayList<String> finalPriorAbis = priorAbis;
Arrays.sort(supportedAbis, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (finalPriorAbis.contains(o1)) {
return -1;
} else if (finalPriorAbis.contains(o2)) {
return 1;
} else {
return 0;
}
}
});
return supportedAbis;
}

@DoNotOptimize
Expand Down

0 comments on commit dc3025a

Please sign in to comment.