diff options
author | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2016-08-01 10:56:41 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-08-01 10:56:41 +0000 |
commit | 71ef72a5382eb63ae3cd6cd04e41785aafc0c7df (patch) | |
tree | b8acb5e61787b544601805687b299e2eb6f9c25f | |
parent | 14267266e145a24155f1a368592bac84b14bffb4 (diff) | |
parent | 895e37b768ced42a813e64e868314aca83fb53ad (diff) |
Merge "Disable Runtime#load/loadLibrary(String,ClassLoader) in >N"
-rw-r--r-- | luni/src/test/java/libcore/java/lang/OldRuntimeTest.java | 72 | ||||
-rwxr-xr-x[-rw-r--r--] | ojluni/src/main/java/java/lang/Runtime.java | 12 |
2 files changed, 84 insertions, 0 deletions
diff --git a/luni/src/test/java/libcore/java/lang/OldRuntimeTest.java b/luni/src/test/java/libcore/java/lang/OldRuntimeTest.java index 294cea2edb..8397587c4b 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.security.Permission; 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 @@ public class OldRuntimeTest extends junit.framework.TestCase { //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 index fa0adbe8d1..e91d8228ed 100644..100755 --- a/ojluni/src/main/java/java/lang/Runtime.java +++ b/ojluni/src/main/java/java/lang/Runtime.java @@ -883,8 +883,19 @@ public class Runtime { 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 @@ public class Runtime { * @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); |