1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import java.io.File; 18 import java.lang.reflect.Method; 19 import java.util.Base64; 20 21 public class Main { main(String[] args)22 public static void main(String[] args) throws ClassNotFoundException { 23 System.loadLibrary(args[0]); 24 25 // Run the initialization routine. This will enable hidden API checks in 26 // the runtime, in case they are not enabled by default. 27 init(); 28 29 // Load the '-ex' APK and attach it to the boot class path. 30 appendToBootClassLoader(DEX_EXTRA, /* isCorePlatform */ false); 31 32 // Find the test class in boot class loader and verify that its members are hidden. 33 Class<?> klass = Class.forName("art.Test2038", true, BOOT_CLASS_LOADER); 34 assertFieldIsHidden(klass, "before set-policy"); 35 assertMethodIsHidden(klass, "before set-policy"); 36 37 int old_policy = disablePolicy(); 38 39 // Verify that the class members are not hidden. 40 assertFieldNotHidden(klass, "after disable-policy"); 41 assertMethodNotHidden(klass, "after disable-policy"); 42 43 setPolicy(old_policy); 44 45 assertFieldIsHidden(klass, "after set-policy 2"); 46 assertMethodIsHidden(klass, "after set-policy 2"); 47 } 48 assertMethodNotHidden(Class<?> klass, String msg)49 private static void assertMethodNotHidden(Class<?> klass, String msg) { 50 try { 51 klass.getDeclaredMethod("foo"); 52 } catch (NoSuchMethodException ex) { 53 // Unexpected. Should not have thrown NoSuchMethodException. 54 throw new RuntimeException("Method should be accessible " + msg); 55 } 56 } 57 assertFieldNotHidden(Class<?> klass, String msg)58 private static void assertFieldNotHidden(Class<?> klass, String msg) { 59 try { 60 klass.getDeclaredField("bar"); 61 } catch (NoSuchFieldException ex) { 62 // Unexpected. Should not have thrown NoSuchFieldException. 63 throw new RuntimeException("Field should be accessible " + msg); 64 } 65 } assertMethodIsHidden(Class<?> klass, String msg)66 private static void assertMethodIsHidden(Class<?> klass, String msg) { 67 try { 68 klass.getDeclaredMethod("foo"); 69 // Unexpected. Should have thrown NoSuchMethodException. 70 throw new RuntimeException("Method should not be accessible " + msg); 71 } catch (NoSuchMethodException ex) { 72 } 73 } 74 assertFieldIsHidden(Class<?> klass, String msg)75 private static void assertFieldIsHidden(Class<?> klass, String msg) { 76 try { 77 klass.getDeclaredField("bar"); 78 // Unexpected. Should have thrown NoSuchFieldException. 79 throw new RuntimeException("Field should not be accessible " + msg); 80 } catch (NoSuchFieldException ex) { 81 } 82 } 83 84 private static final String DEX_EXTRA = 85 new File(System.getenv("DEX_LOCATION"), "2038-hiddenapi-jvmti-ext-ex.jar").getAbsolutePath(); 86 87 private static ClassLoader BOOT_CLASS_LOADER = Object.class.getClassLoader(); 88 89 // Native functions. Note that these are implemented in 674-hiddenapi/hiddenapi.cc. appendToBootClassLoader(String dexPath, boolean isCorePlatform)90 private static native void appendToBootClassLoader(String dexPath, boolean isCorePlatform); init()91 private static native void init(); 92 93 // Native function implemented in hiddenapi_ext.cc setPolicy(int new_policy)94 private static native int setPolicy(int new_policy); disablePolicy()95 private static native int disablePolicy(); 96 } 97