1 /* 2 * Copyright (C) 2016 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.lang.reflect.*; 18 import java.util.Base64; 19 20 import art.Redefinition; 21 22 class Main { 23 public static String TEST_NAME = "935-non-retransformable"; 24 25 /** 26 * base64 encoded class/dex file for 27 * class Transform { 28 * public void sayHi() { 29 * System.out.println("Hello"); 30 * } 31 * public void sayGoodbye() { 32 * System.out.println("Goodbye"); 33 * } 34 * } 35 */ 36 private static final byte[] CLASS_BYTES = Base64.getDecoder().decode( 37 "yv66vgAAADQAHwoABwAQCQARABIIABMKABQAFQgAFgcAFwcAGAEABjxpbml0PgEAAygpVgEABENv" + 38 "ZGUBAA9MaW5lTnVtYmVyVGFibGUBAAVzYXlIaQEACnNheUdvb2RieWUBAApTb3VyY2VGaWxlAQAO" + 39 "VHJhbnNmb3JtLmphdmEMAAgACQcAGQwAGgAbAQAFSGVsbG8HABwMAB0AHgEAB0dvb2RieWUBAAlU" + 40 "cmFuc2Zvcm0BABBqYXZhL2xhbmcvT2JqZWN0AQAQamF2YS9sYW5nL1N5c3RlbQEAA291dAEAFUxq" + 41 "YXZhL2lvL1ByaW50U3RyZWFtOwEAE2phdmEvaW8vUHJpbnRTdHJlYW0BAAdwcmludGxuAQAVKExq" + 42 "YXZhL2xhbmcvU3RyaW5nOylWACAABgAHAAAAAAADAAAACAAJAAEACgAAAB0AAQABAAAABSq3AAGx" + 43 "AAAAAQALAAAABgABAAAAAQABAAwACQABAAoAAAAlAAIAAQAAAAmyAAISA7YABLEAAAABAAsAAAAK" + 44 "AAIAAAADAAgABAABAA0ACQABAAoAAAAlAAIAAQAAAAmyAAISBbYABLEAAAABAAsAAAAKAAIAAAAG" + 45 "AAgABwABAA4AAAACAA8="); 46 private static final byte[] DEX_BYTES = Base64.getDecoder().decode( 47 "ZGV4CjAzNQDpaN+7jX/ZLl9Jr0HAEV7nqL1YDuakKakgAwAAcAAAAHhWNBIAAAAAAAAAAIACAAAQ" + 48 "AAAAcAAAAAYAAACwAAAAAgAAAMgAAAABAAAA4AAAAAUAAADoAAAAAQAAABABAADwAQAAMAEAAJYB" + 49 "AACeAQAApwEAAK4BAAC7AQAA0gEAAOYBAAD6AQAADgIAAB4CAAAhAgAAJQIAADkCAAA+AgAARwIA" + 50 "AFMCAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABQAAAAAAAAAKAAAABQAAAJABAAAEAAEA" + 51 "DAAAAAAAAAAAAAAAAAAAAA4AAAAAAAAADwAAAAEAAQANAAAAAgAAAAAAAAAAAAAAAAAAAAIAAAAA" + 52 "AAAACAAAAAAAAABrAgAAAAAAAAEAAQABAAAAWgIAAAQAAABwEAQAAAAOAAMAAQACAAAAXwIAAAkA" + 53 "AABiAAAAGwEBAAAAbiADABAADgAAAAMAAQACAAAAZQIAAAkAAABiAAAAGwECAAAAbiADABAADgAA" + 54 "AAEAAAADAAY8aW5pdD4AB0dvb2RieWUABUhlbGxvAAtMVHJhbnNmb3JtOwAVTGphdmEvaW8vUHJp" + 55 "bnRTdHJlYW07ABJMamF2YS9sYW5nL09iamVjdDsAEkxqYXZhL2xhbmcvU3RyaW5nOwASTGphdmEv" + 56 "bGFuZy9TeXN0ZW07AA5UcmFuc2Zvcm0uamF2YQABVgACVkwAEmVtaXR0ZXI6IGphY2stNC4yMgAD" + 57 "b3V0AAdwcmludGxuAApzYXlHb29kYnllAAVzYXlIaQABAAcOAAYABw6HAAMABw6HAAAAAQIAgIAE" + 58 "sAIBAcgCAQHsAgAAAA0AAAAAAAAAAQAAAAAAAAABAAAAEAAAAHAAAAACAAAABgAAALAAAAADAAAA" + 59 "AgAAAMgAAAAEAAAAAQAAAOAAAAAFAAAABQAAAOgAAAAGAAAAAQAAABABAAABIAAAAwAAADABAAAB" + 60 "EAAAAQAAAJABAAACIAAAEAAAAJYBAAADIAAAAwAAAFoCAAAAIAAAAQAAAGsCAAAAEAAAAQAAAIAC" + 61 "AAA="); 62 63 getClassLoaderFor(String location)64 public static ClassLoader getClassLoaderFor(String location) throws Exception { 65 try { 66 Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader"); 67 Constructor<?> ctor = class_loader_class.getConstructor(String.class, ClassLoader.class); 68 /* on Dalvik, this is a DexFile; otherwise, it's null */ 69 return (ClassLoader)ctor.newInstance(location + "/" + TEST_NAME + "-ex.jar", 70 Main.class.getClassLoader()); 71 } catch (ClassNotFoundException e) { 72 // Running on RI. Use URLClassLoader. 73 return new java.net.URLClassLoader( 74 new java.net.URL[] { new java.net.URL("file://" + location + "/classes-ex/") }); 75 } 76 } 77 main(String[] args)78 public static void main(String[] args) { 79 Redefinition.setPopRetransformations(false); 80 Redefinition.addCommonTransformationResult("Transform", CLASS_BYTES, DEX_BYTES); 81 Redefinition.enableCommonRetransformation(true); 82 try { 83 /* this is the "alternate" DEX/Jar file */ 84 ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION")); 85 Class<?> klass = (Class<?>)new_loader.loadClass("TestMain"); 86 if (klass == null) { 87 throw new AssertionError("loadClass failed"); 88 } 89 Method run_test = klass.getMethod("runTest"); 90 run_test.invoke(null); 91 92 // Remove the original transformation. It has been used by now. 93 Redefinition.popTransformationFor("Transform"); 94 // Make sure we don't get called for transformation again. 95 Redefinition.addCommonTransformationResult("Transform", new byte[0], new byte[0]); 96 Redefinition.doCommonClassRetransformation(new_loader.loadClass("Transform")); 97 run_test.invoke(null); 98 } catch (Exception e) { 99 System.out.println(e.toString()); 100 e.printStackTrace(); 101 } 102 } 103 } 104