• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 art.Redefinition;
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.Method;
20 import java.net.URL;
21 import java.net.URLClassLoader;
22 import java.nio.ByteBuffer;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Base64;
26 
27 public class Main {
28   private static String TEST_NAME = "1964-add-to-dex-classloader-file";
29   private static boolean IS_ART = System.getProperty("java.vm.name").equals("Dalvik");
30 
31   private static String TEST_CLASS_NAME = "foobar.TestClass";
32   private static String NEW_CLASS_NAME = "foobar.NewClass";
33 
34   /**
35    * base64 encoded class/dex file for
36    * package foobar;
37    * public class TestClass {
38    *   public static void sayHi() {
39    *    System.out.println("Hello again from TestClass sayHi function");
40    *    TestClass.sayBye();
41    *   }
42    *   static void sayBye() {
43    *    System.out.println("Goodbye from TestClass!");
44    *   }
45    * }
46    */
47   private static byte[] CLASS_BYTES = Base64.getDecoder().decode(
48       "yv66vgAAADUAIQoACAARCQASABMIABQKABUAFgoABwAXCAAYBwAZBwAaAQAGPGluaXQ+AQADKClW"
49       + "AQAEQ29kZQEAD0xpbmVOdW1iZXJUYWJsZQEABXNheUhpAQAGc2F5QnllAQAKU291cmNlRmlsZQEA"
50       + "DlRlc3RDbGFzcy5qYXZhDAAJAAoHABsMABwAHQEAI0hlbGxvIGZyb20gVGVzdENsYXNzIHNheUhp"
51       + "IGZ1bmN0aW9uBwAeDAAfACAMAA4ACgEAF0dvb2RieWUgZnJvbSBUZXN0Q2xhc3MhAQAQZm9vYmFy"
52       + "L1Rlc3RDbGFzcwEAEGphdmEvbGFuZy9PYmplY3QBABBqYXZhL2xhbmcvU3lzdGVtAQADb3V0AQAV"
53       + "TGphdmEvaW8vUHJpbnRTdHJlYW07AQATamF2YS9pby9QcmludFN0cmVhbQEAB3ByaW50bG4BABUo"
54       + "TGphdmEvbGFuZy9TdHJpbmc7KVYAIQAHAAgAAAAAAAMAAQAJAAoAAQALAAAAHQABAAEAAAAFKrcA"
55       + "AbEAAAABAAwAAAAGAAEAAAACAAkADQAKAAEACwAAACwAAgAAAAAADLIAAhIDtgAEuAAFsQAAAAEA"
56       + "DAAAAA4AAwAAAAQACAAFAAsABgAIAA4ACgABAAsAAAAlAAIAAAAAAAmyAAISBrYABLEAAAABAAwA"
57       + "AAAKAAIAAAAIAAgACQABAA8AAAACABA=");
58 
59   private static byte[] DEX_BYTES = Base64.getDecoder().decode(
60       "ZGV4CjAzNQARmtFTPdWXebnrTNy5b71tEiJKC96qIPXAAwAAcAAAAHhWNBIAAAAAAAAAABQDAAAQ"
61       + "AAAAcAAAAAYAAACwAAAAAgAAAMgAAAABAAAA4AAAAAUAAADoAAAAAQAAABABAACQAgAAMAEAAKYB"
62       + "AACuAQAAxwEAAOwBAAAAAgAAFwIAACsCAAA/AgAAUwIAAGMCAABmAgAAagIAAG8CAAB4AgAAgAIA"
63       + "AIcCAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABQAAAAAAAAAKAAAABQAAAKABAAAEAAEA"
64       + "CwAAAAAAAAAAAAAAAAAAAA0AAAAAAAAADgAAAAEAAQAMAAAAAgAAAAAAAAAAAAAAAQAAAAIAAAAA"
65       + "AAAACAAAAAAAAAD+AgAAAAAAAAEAAQABAAAAjgEAAAQAAABwEAQAAAAOAAIAAAACAAAAkgEAAAgA"
66       + "AABiAAAAGgEBAG4gAwAQAA4AAgAAAAIAAACXAQAACwAAAGIAAAAaAQIAbiADABAAcQABAAAADgAC"
67       + "AA4ACAAOeAAEAA54PAAAAAABAAAAAwAGPGluaXQ+ABdHb29kYnllIGZyb20gVGVzdENsYXNzIQAj"
68       + "SGVsbG8gZnJvbSBUZXN0Q2xhc3Mgc2F5SGkgZnVuY3Rpb24AEkxmb29iYXIvVGVzdENsYXNzOwAV"
69       + "TGphdmEvaW8vUHJpbnRTdHJlYW07ABJMamF2YS9sYW5nL09iamVjdDsAEkxqYXZhL2xhbmcvU3Ry"
70       + "aW5nOwASTGphdmEvbGFuZy9TeXN0ZW07AA5UZXN0Q2xhc3MuamF2YQABVgACVkwAA291dAAHcHJp"
71       + "bnRsbgAGc2F5QnllAAVzYXlIaQB1fn5EOHsiY29tcGlsYXRpb24tbW9kZSI6ImRlYnVnIiwibWlu"
72       + "LWFwaSI6MSwic2hhLTEiOiJkMzI4MmI4ZjU0N2MyMzRjNGU0YzkzMDljMzZjNzk1YTI5ODU2ZWFi"
73       + "IiwidmVyc2lvbiI6IjEuNi4xLWRldiJ9AAAAAwAAgYAEsAIBCMgCAQnoAgAAAAAOAAAAAAAAAAEA"
74       + "AAAAAAAAAQAAABAAAABwAAAAAgAAAAYAAACwAAAAAwAAAAIAAADIAAAABAAAAAEAAADgAAAABQAA"
75       + "AAUAAADoAAAABgAAAAEAAAAQAQAAASAAAAMAAAAwAQAAAyAAAAMAAACOAQAAARAAAAEAAACgAQAA"
76       + "AiAAABAAAACmAQAAACAAAAEAAAD+AgAAAxAAAAEAAAAQAwAAABAAAAEAAAAUAwAA");
77   /**
78    * base64 encoded class/dex file for
79    * package foobar;
80    * public class TestClass {
81    *   public static void sayHi() {
82    *    System.out.println("Hello again from TestClass sayHi function");
83    *    NewClass.sayHi();
84    *   }
85    *   static void sayBye() {
86    *    System.out.println("Goodbye again from TestClass!");
87    *   }
88    * }
89    */
90   private static byte[] REDEF_CLASS_BYTES = Base64.getDecoder().decode(
91       "yv66vgAAADUAIwoACAARCQASABMIABQKABUAFgoAFwAYCAAZBwAaBwAbAQAGPGluaXQ+AQADKClW"
92       + "AQAEQ29kZQEAD0xpbmVOdW1iZXJUYWJsZQEABXNheUhpAQAGc2F5QnllAQAKU291cmNlRmlsZQEA"
93       + "DlRlc3RDbGFzcy5qYXZhDAAJAAoHABwMAB0AHgEAKUhlbGxvIGFnYWluIGZyb20gVGVzdENsYXNz"
94       + "IHNheUhpIGZ1bmN0aW9uBwAfDAAgACEHACIMAA0ACgEAHUdvb2RieWUgYWdhaW4gZnJvbSBUZXN0"
95       + "Q2xhc3MhAQAQZm9vYmFyL1Rlc3RDbGFzcwEAEGphdmEvbGFuZy9PYmplY3QBABBqYXZhL2xhbmcv"
96       + "U3lzdGVtAQADb3V0AQAVTGphdmEvaW8vUHJpbnRTdHJlYW07AQATamF2YS9pby9QcmludFN0cmVh"
97       + "bQEAB3ByaW50bG4BABUoTGphdmEvbGFuZy9TdHJpbmc7KVYBAA9mb29iYXIvTmV3Q2xhc3MAIQAH"
98       + "AAgAAAAAAAMAAQAJAAoAAQALAAAAHQABAAEAAAAFKrcAAbEAAAABAAwAAAAGAAEAAAACAAkADQAK"
99       + "AAEACwAAACwAAgAAAAAADLIAAhIDtgAEuAAFsQAAAAEADAAAAA4AAwAAAAQACAAFAAsABgAIAA4A"
100       + "CgABAAsAAAAlAAIAAAAAAAmyAAISBrYABLEAAAABAAwAAAAKAAIAAAAIAAgACQABAA8AAAACABA=");
101 
102   private static byte[] REDEF_DEX_BYTES = Base64.getDecoder().decode(
103       "ZGV4CjAzNQA2plEeYRH4vl6wJgnAZOVcZ537QN9NXB3wAwAAcAAAAHhWNBIAAAAAAAAAAEQDAAAR"
104       + "AAAAcAAAAAcAAAC0AAAAAgAAANAAAAABAAAA6AAAAAYAAADwAAAAAQAAACABAACwAgAAQAEAALYB"
105       + "AAC+AQAA3QEAAAgCAAAbAgAALwIAAEYCAABaAgAAbgIAAIICAACSAgAAlQIAAJkCAACeAgAApwIA"
106       + "AK8CAAC2AgAAAwAAAAQAAAAFAAAABgAAAAcAAAAIAAAACgAAAAoAAAAGAAAAAAAAAAsAAAAGAAAA"
107       + "sAEAAAUAAgAMAAAAAAAAAA8AAAABAAAAAAAAAAEAAAAOAAAAAQAAAA8AAAACAAEADQAAAAMAAAAA"
108       + "AAAAAQAAAAEAAAADAAAAAAAAAAkAAAAAAAAALQMAAAAAAAABAAEAAQAAAJ4BAAAEAAAAcBAFAAAA"
109       + "DgACAAAAAgAAAKIBAAAIAAAAYgAAABoBAQBuIAQAEAAOAAIAAAACAAAApwEAAAsAAABiAAAAGgEC"
110       + "AG4gBAAQAHEAAAAAAA4AAgAOAAgADngABAAOeDwAAAAAAQAAAAQABjxpbml0PgAdR29vZGJ5ZSBh"
111       + "Z2FpbiBmcm9tIFRlc3RDbGFzcyEAKUhlbGxvIGFnYWluIGZyb20gVGVzdENsYXNzIHNheUhpIGZ1"
112       + "bmN0aW9uABFMZm9vYmFyL05ld0NsYXNzOwASTGZvb2Jhci9UZXN0Q2xhc3M7ABVMamF2YS9pby9Q"
113       + "cmludFN0cmVhbTsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABJMamF2"
114       + "YS9sYW5nL1N5c3RlbTsADlRlc3RDbGFzcy5qYXZhAAFWAAJWTAADb3V0AAdwcmludGxuAAZzYXlC"
115       + "eWUABXNheUhpAHV+fkQ4eyJjb21waWxhdGlvbi1tb2RlIjoiZGVidWciLCJtaW4tYXBpIjoxLCJz"
116       + "aGEtMSI6ImQzMjgyYjhmNTQ3YzIzNGM0ZTRjOTMwOWMzNmM3OTVhMjk4NTZlYWIiLCJ2ZXJzaW9u"
117       + "IjoiMS42LjEtZGV2In0AAAADAAGBgATAAgEI2AIBCfgCAAAAAAAOAAAAAAAAAAEAAAAAAAAAAQAA"
118       + "ABEAAABwAAAAAgAAAAcAAAC0AAAAAwAAAAIAAADQAAAABAAAAAEAAADoAAAABQAAAAYAAADwAAAA"
119       + "BgAAAAEAAAAgAQAAASAAAAMAAABAAQAAAyAAAAMAAACeAQAAARAAAAEAAACwAQAAAiAAABEAAAC2"
120       + "AQAAACAAAAEAAAAtAwAAAxAAAAEAAABAAwAAABAAAAEAAABEAwAA");
121 
SafePrintCause(Throwable t)122   public static void SafePrintCause(Throwable t) {
123     StackTraceElement cause = t.getStackTrace()[0];
124     System.out.println(" --- " + t.getClass().getName() + " At " + cause.getClassName() + "." +
125                        cause.getMethodName() + "(" + cause.getFileName() + ":" +
126                        cause.getLineNumber() + ")");
127   }
128 
run()129   public static void run() throws Exception {
130     System.out.println(" - Run while adding new referenced class.");
131     try {
132       run(true);
133     } catch (Exception e) {
134       // Unfortunately art and RI have different messages here so just return the type.
135       System.out.println(" -- Exception caught when running test with new class added! " +
136                          e.getCause().getClass().getName());
137       SafePrintCause(e.getCause());
138       System.out.println(e);
139       e.printStackTrace();
140     }
141     System.out.println(" - Run without adding new referenced class.");
142     try {
143       run(false);
144     } catch (Exception e) {
145       // Unfortunately art and RI have different messages here so just return the type.
146       System.out.println(" -- Exception caught when running test without new class added! " +
147                          e.getCause().getClass().getName());
148       SafePrintCause(e.getCause());
149     }
150   }
151 
run(boolean add_new)152   public static void run(boolean add_new) throws Exception {
153     ClassLoader cl = getClassLoader();
154     Class<?> target = cl.loadClass(TEST_CLASS_NAME);
155     Method sayHi = target.getDeclaredMethod("sayHi");
156     System.out.println(" -- Running sayHi before redefinition");
157     sayHi.invoke(null);
158     if (add_new) {
159       System.out.println(" -- Adding NewClass to classloader!");
160       addToClassLoader(cl);
161     }
162     System.out.println(" -- Redefine the TestClass");
163     Redefinition.doCommonClassRedefinition(target, REDEF_CLASS_BYTES, REDEF_DEX_BYTES);
164     System.out.println(" -- call TestClass again, now with NewClass refs");
165     sayHi.invoke(null);
166   }
167 
168   public static class ExtensibleClassLoader extends URLClassLoader {
ExtensibleClassLoader()169     public ExtensibleClassLoader() {
170       // Initially we don't have any URLs
171       super(new URL[] {}, ExtensibleClassLoader.class.getClassLoader());
172     }
173 
addSingleUrl(String file)174     public void addSingleUrl(String file) throws Exception {
175       this.addURL(new URL("file://" + file));
176     }
177 
findClass(String name)178     protected Class<?> findClass(String name) throws ClassNotFoundException {
179       // Just define the TestClass without other jars.
180       if (name.equals(TEST_CLASS_NAME)) {
181         return this.defineClass(TEST_CLASS_NAME, CLASS_BYTES, 0, CLASS_BYTES.length);
182       } else {
183         return super.findClass(name);
184       }
185     }
186   }
187 
getClassLoader()188   public static ClassLoader getClassLoader() throws Exception {
189     if (!IS_ART) {
190       return new ExtensibleClassLoader();
191     } else {
192       Class<?> class_loader_class = Class.forName("dalvik.system.InMemoryDexClassLoader");
193       Constructor<?> ctor = class_loader_class.getConstructor(ByteBuffer.class, ClassLoader.class);
194       return (ClassLoader)ctor.newInstance(ByteBuffer.wrap(DEX_BYTES), Main.class.getClassLoader());
195     }
196   }
197 
addToClassLoader(ClassLoader cl)198   public static void addToClassLoader(ClassLoader cl) throws Exception {
199     if (IS_ART) {
200       addToClassLoaderNative(cl, System.getenv("DEX_LOCATION") + "/" + TEST_NAME + "-ex.jar");
201     } else {
202       ((ExtensibleClassLoader)cl).addSingleUrl(System.getenv("DEX_LOCATION") + "/classes-ex/");
203     }
204   }
205 
addToClassLoaderNative(ClassLoader loader, String segment)206   public static native void addToClassLoaderNative(ClassLoader loader, String segment);
main(String[] args)207   public static void main(String[] args) throws Exception {
208     run();
209   }
210 }
211