Merge "Disable Runtime#load/loadLibrary(String,ClassLoader) in >N"
diff --git a/luni/src/test/java/libcore/java/lang/OldRuntimeTest.java b/luni/src/test/java/libcore/java/lang/OldRuntimeTest.java
index 294cea2..8397587 100644
--- a/luni/src/test/java/libcore/java/lang/OldRuntimeTest.java
+++ b/luni/src/test/java/libcore/java/lang/OldRuntimeTest.java
@@ -27,6 +27,9 @@
import java.util.Arrays;
import java.util.Vector;
import tests.support.resource.Support_Resources;
+import dalvik.system.VMRuntime;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
public class OldRuntimeTest extends junit.framework.TestCase {
@@ -519,4 +522,73 @@
//expected
}
}
+
+ // b/25859957
+ public void test_loadDeprecated() throws Exception {
+ final int savedTargetSdkVersion = VMRuntime.getRuntime().getTargetSdkVersion();
+ try {
+ try {
+ // Call Runtime#load(String, ClassLoader) at API level 24 (N). It will fail
+ // with a UnsatisfiedLinkError because requested library doesn't exits.
+ VMRuntime.getRuntime().setTargetSdkVersion(24);
+ Method loadMethod =
+ Runtime.class.getDeclaredMethod("load", String.class, ClassLoader.class);
+ loadMethod.setAccessible(true);
+ loadMethod.invoke(Runtime.getRuntime(), "nonExistentLibrary", null);
+ fail();
+ } catch(InvocationTargetException expected) {
+ assertTrue(expected.getCause() instanceof UnsatisfiedLinkError);
+ }
+
+ try {
+ // Call Runtime#load(String, ClassLoader) at API level 25. It will fail
+ // with a IllegalStateException because it's deprecated.
+ VMRuntime.getRuntime().setTargetSdkVersion(25);
+ Method loadMethod =
+ Runtime.class.getDeclaredMethod("load", String.class, ClassLoader.class);
+ loadMethod.setAccessible(true);
+ loadMethod.invoke(Runtime.getRuntime(), "nonExistentLibrary", null);
+ fail();
+ } catch(InvocationTargetException expected) {
+ assertTrue(expected.getCause() instanceof UnsupportedOperationException);
+ }
+ } finally {
+ VMRuntime.getRuntime().setTargetSdkVersion(savedTargetSdkVersion);
+ }
+ }
+
+ // b/25859957
+ public void test_loadLibraryDeprecated() throws Exception {
+ final int savedTargetSdkVersion = VMRuntime.getRuntime().getTargetSdkVersion();
+ try {
+ try {
+ // Call Runtime#loadLibrary(String, ClassLoader) at API level 24 (N). It will fail
+ // with a UnsatisfiedLinkError because requested library doesn't exits.
+ VMRuntime.getRuntime().setTargetSdkVersion(24);
+ Method loadMethod =
+ Runtime.class.getDeclaredMethod("loadLibrary", String.class, ClassLoader.class);
+ loadMethod.setAccessible(true);
+ loadMethod.invoke(Runtime.getRuntime(), "nonExistentLibrary", null);
+ fail();
+ } catch(InvocationTargetException expected) {
+ assertTrue(expected.getCause() instanceof UnsatisfiedLinkError);
+ }
+
+ try {
+ // Call Runtime#load(String, ClassLoader) at API level 25. It will fail
+ // with a IllegalStateException because it's deprecated.
+
+ VMRuntime.getRuntime().setTargetSdkVersion(25);
+ Method loadMethod =
+ Runtime.class.getDeclaredMethod("loadLibrary", String.class, ClassLoader.class);
+ loadMethod.setAccessible(true);
+ loadMethod.invoke(Runtime.getRuntime(), "nonExistentLibrary", null);
+ fail();
+ } catch(InvocationTargetException expected) {
+ assertTrue(expected.getCause() instanceof UnsupportedOperationException);
+ }
+ } finally {
+ VMRuntime.getRuntime().setTargetSdkVersion(savedTargetSdkVersion);
+ }
+ }
}
diff --git a/ojluni/src/main/java/java/lang/Runtime.java b/ojluni/src/main/java/java/lang/Runtime.java
old mode 100644
new mode 100755
index fa0adbe..e91d822
--- a/ojluni/src/main/java/java/lang/Runtime.java
+++ b/ojluni/src/main/java/java/lang/Runtime.java
@@ -883,8 +883,19 @@
load0(VMStack.getStackClass1(), filename);
}
+ /** Check target sdk, if it's higher than N, we throw an UnsupportedOperationException */
+ private void checkTargetSdkVersionForLoad(String methodName) {
+ final int targetSdkVersion = VMRuntime.getRuntime().getTargetSdkVersion();
+ if (targetSdkVersion > 24) {
+ throw new UnsupportedOperationException(methodName + " is not supported on SDK " +
+ targetSdkVersion);
+ }
+ }
+
// Fixes b/25859957 regression. Depending on private methods is bad, mkay.
void load(String absolutePath, ClassLoader loader) {
+ checkTargetSdkVersionForLoad("java.lang.Runtime#load(String, ClassLoader)");
+
java.lang.System.logE("java.lang.Runtime#load(String, ClassLoader)" +
" is private and will be removed in a future Android release");
if (absolutePath == null) {
@@ -970,6 +981,7 @@
* @hide
*/
public void loadLibrary(String libname, ClassLoader classLoader) {
+ checkTargetSdkVersionForLoad("java.lang.Runtime#loadLibrary(String, ClassLoader)");
java.lang.System.logE("java.lang.Runtime#loadLibrary(String, ClassLoader)" +
" is private and will be removed in a future Android release");
loadLibrary0(classLoader, libname);