Skip to content

Commit

Permalink
Throw when ScriptC is used on unsupported ABIs
Browse files Browse the repository at this point in the history
ScriptC won't be supported on any new architecture (for example riscv64), so
throw an exception when it is used on an unsupported architecture.

Note that using `Build.SUPPORTED_ABIS` includes ABIs supported due to
dynamic binary translation, which do not support ScriptC. So we can't
use `Build.SUPPORTED_ABIS` to determine if ScriptC is supported or
not.

Instead we attempt loading `libRS.so`.
`libRS.so` is not present on riscv64 images, so this allows us to know
if we are on a riscv64 system.
A problem occurs on x86 images with binary translation for riscv64
(aka berberis): when running a riscv64 binary, the binary translation
system will fallback to the x86 version of the libraries via what is
called a "native bridge". This means that `libRS.so` would successfully
load on those system, which we do not want because even though it would load it wouldn't run properly.
This is why a separate CL in the binary translation codebase was landed that explicitly refuses to load `libRS.so`:
https://android-review.git.corp.google.com/c/platform/frameworks/libs/binary_translation/+/2971952

Bug: 206676167
Test: atest CtsRsBlasTestCases:android.cts.rsblas.IntrinsicBLAS#test_L3_SGEMM_API -- --abi x86_64

Change-Id: I2ed2e2ae531046d1d00e9a2d2f864375b0bb3570
  • Loading branch information
jyaif committed Feb 22, 2024
1 parent f647472 commit 4f585f7
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions rs/java/android/renderscript/ScriptC.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,19 @@ protected ScriptC(RenderScript rs, String resName, byte[] bitcode32, byte[] bitc
setID(id);
}

private static void throwExceptionIfSDKTooHigh() {
private static void throwExceptionIfScriptCUnsupported() {
// Checks that this device actually does have an ABI that supports ScriptC.
//
// For an explanation as to why `System.loadLibrary` is used, see discussion at
// https://android-review.googlesource.com/c/platform/frameworks/base/+/2957974/comment/2f908b80_a05292ee
try {
System.loadLibrary("RS");
} catch (UnsatisfiedLinkError e) {
String s = "This device does not have an ABI that supports ScriptC.";
throw new UnsupportedOperationException(s);
}

// Throw an exception if the target API is 35 or above
String message =
"ScriptC scripts are not supported when targeting an API Level >= 35. Please refer "
+ "to https://developer.android.com/guide/topics/renderscript/migration-guide "
Expand All @@ -113,7 +125,7 @@ private static void throwExceptionIfSDKTooHigh() {
}

private static synchronized long internalCreate(RenderScript rs, Resources resources, int resourceID) {
throwExceptionIfSDKTooHigh();
throwExceptionIfScriptCUnsupported();
byte[] pgm;
int pgmLength;
InputStream is = resources.openRawResource(resourceID);
Expand Down Expand Up @@ -150,7 +162,7 @@ private static synchronized long internalCreate(RenderScript rs, Resources resou

private static synchronized long internalStringCreate(RenderScript rs, String resName, byte[] bitcode) {
// Log.v(TAG, "Create script for resource = " + resName);
throwExceptionIfSDKTooHigh();
throwExceptionIfScriptCUnsupported();
return rs.nScriptCCreate(resName, RenderScript.getCachePath(), bitcode, bitcode.length);
}
}

0 comments on commit 4f585f7

Please sign in to comment.