1 /* 2 * Copyright (C) 2021 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 static art.Redefinition.addCommonTransformationResult; 18 import static art.Redefinition.enableCommonRetransformation; 19 import static art.Redefinition.setPopRetransformations; 20 21 import java.lang.reflect.*; 22 import java.util.Base64; 23 24 class Main { 25 public static String TEST_NAME = "2039-load-transform-larger"; 26 27 /** 28 * base64 encoded class/dex file for 29 * class Transform { 30 * public static int PAD1; 31 * public static int PAD2; 32 * public static int PAD3; 33 * public static int PAD4; 34 * public static int PAD5; 35 * public static int PAD6; 36 * public static int PAD7; 37 * public static int PAD8; 38 * public static int PAD9; 39 * public static int PAD10; 40 * public static String TO_SAY = "Goodbye"; 41 * public void sayHi() { 42 * System.out.println(TO_SAY); 43 * } 44 * } 45 */ 46 private static final byte[] CLASS_BYTES = Base64.getDecoder().decode( 47 "yv66vgAAADcAQAoAEQAnCQAoACkJABAAKgoAKwAsCQAQAC0JABAALgkAEAAvCQAQADAJABAAMQkA" + 48 "EAAyCQAQADMJABAANAkAEAA1CQAQADYIADcHADgHADkBAARQQUQxAQABSQEABFBBRDIBAARQQUQz" + 49 "AQAEUEFENAEABFBBRDUBAARQQUQ2AQAEUEFENwEABFBBRDgBAARQQUQ5AQAFUEFEMTABAAZUT19T" + 50 "QVkBABJMamF2YS9sYW5nL1N0cmluZzsBAAY8aW5pdD4BAAMoKVYBAARDb2RlAQAPTGluZU51bWJl" + 51 "clRhYmxlAQAFc2F5SGkBAAg8Y2xpbml0PgEAClNvdXJjZUZpbGUBAA5UcmFuc2Zvcm0uamF2YQwA" + 52 "HwAgBwA6DAA7ADwMAB0AHgcAPQwAPgA/DAASABMMABQAEwwAFQATDAAWABMMABcAEwwAGAATDAAZ" + 53 "ABMMABoAEwwAGwATDAAcABMBAAdHb29kYnllAQAJVHJhbnNmb3JtAQAQamF2YS9sYW5nL09iamVj" + 54 "dAEAEGphdmEvbGFuZy9TeXN0ZW0BAANvdXQBABVMamF2YS9pby9QcmludFN0cmVhbTsBABNqYXZh" + 55 "L2lvL1ByaW50U3RyZWFtAQAHcHJpbnRsbgEAFShMamF2YS9sYW5nL1N0cmluZzspVgAgABAAEQAA" + 56 "AAsACQASABMAAAAJABQAEwAAAAkAFQATAAAACQAWABMAAAAJABcAEwAAAAkAGAATAAAACQAZABMA" + 57 "AAAJABoAEwAAAAkAGwATAAAACQAcABMAAAAJAB0AHgAAAAMAAAAfACAAAQAhAAAAHQABAAEAAAAF" + 58 "KrcAAbEAAAABACIAAAAGAAEAAAABAAEAIwAgAAEAIQAAACYAAgABAAAACrIAArIAA7YABLEAAAAB" + 59 "ACIAAAAKAAIAAAAOAAkADwAIACQAIAABACEAAABuAAEAAAAAAC4DswAFA7MABgOzAAcDswAIA7MA" + 60 "CQOzAAoDswALA7MADAOzAA0DswAOEg+zAAOxAAAAAQAiAAAALgALAAAAAgAEAAMACAAEAAwABQAQ" + 61 "AAYAFAAHABgACAAcAAkAIAAKACQACwAoAAwAAQAlAAAAAgAm"); 62 private static final byte[] DEX_BYTES = Base64.getDecoder().decode( 63 "ZGV4CjAzNQDwqDsREETVXjkzn+/MRpRRrCJUZTv/xY28BAAAcAAAAHhWNBIAAAAAAAAAABAEAAAb" + 64 "AAAAcAAAAAcAAADcAAAAAgAAAPgAAAAMAAAAEAEAAAUAAABwAQAAAQAAAJgBAAAEAwAAuAEAAFIC" + 65 "AABcAgAAZAIAAG0CAABwAgAAfQIAAJQCAACoAgAAvAIAANACAADWAgAA3QIAAOMCAADpAgAA7wIA" + 66 "APUCAAD7AgAAAQMAAAcDAAANAwAAFQMAACUDAAAoA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FiNWViZThkIiwi" + 80 "dmVyc2lvbiI6IjMuMC4zMC1kZXYifQALAAIBAAkBCQEJAQkBCQEJAQkBCQEJAQkBCQCIgAS4AwGA" + 81 "gAT8AwIBlAQAAAAAAAAOAAAAAAAAAAEAAAAAAAAAAQAAABsAAABwAAAAAgAAAAcAAADcAAAAAwAA" + 82 "AAIAAAD4AAAABAAAAAwAAAAQAQAABQAAAAUAAABwAQAABgAAAAEAAACYAQAAASAAAAMAAAC4AQAA" + 83 "AyAAAAMAAAA0AgAAARAAAAEAAABMAgAAAiAAABsAAABSAgAAACAAAAEAAADgAwAAAxAAAAEAAAAM" + 84 "BAAAABAAAAEAAAAQBAAA"); 85 getClassLoaderFor(String location)86 public static ClassLoader getClassLoaderFor(String location) throws Exception { 87 try { 88 Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader"); 89 Constructor<?> ctor = class_loader_class.getConstructor(String.class, ClassLoader.class); 90 /* on Dalvik, this is a DexFile; otherwise, it's null */ 91 return (ClassLoader)ctor.newInstance(location + "/" + TEST_NAME + "-ex.jar", 92 Main.class.getClassLoader()); 93 } catch (ClassNotFoundException e) { 94 // Running on RI. Use URLClassLoader. 95 return new java.net.URLClassLoader( 96 new java.net.URL[] { new java.net.URL("file://" + location + "/classes-ex/") }); 97 } 98 } 99 main(String[] args)100 public static void main(String[] args) { 101 // Don't pop transformations. Make sure that even if 2 threads race to define the class both 102 // will get the same result. 103 setPopRetransformations(false); 104 addCommonTransformationResult("Transform", CLASS_BYTES, DEX_BYTES); 105 enableCommonRetransformation(true); 106 try { 107 /* this is the "alternate" DEX/Jar file */ 108 ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION")); 109 Class<?> klass = (Class<?>)new_loader.loadClass("TestMain"); 110 if (klass == null) { 111 throw new AssertionError("loadClass failed"); 112 } 113 Method run_test = klass.getMethod("runTest"); 114 run_test.invoke(null); 115 } catch (Exception e) { 116 System.out.println(e.toString()); 117 e.printStackTrace(System.out); 118 } 119 } 120 } 121