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 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 = "934-load-transform";
26 
27   /**
28    * base64 encoded class/dex file for
29    * class Transform {
30    *   public void sayHi() {
31    *    System.out.println("Goodbye");
32    *   }
33    * }
34    */
35   private static final byte[] CLASS_BYTES = Base64.getDecoder().decode(
36     "yv66vgAAADQAHAoABgAOCQAPABAIABEKABIAEwcAFAcAFQEABjxpbml0PgEAAygpVgEABENvZGUB" +
37     "AA9MaW5lTnVtYmVyVGFibGUBAAVzYXlIaQEAClNvdXJjZUZpbGUBAA5UcmFuc2Zvcm0uamF2YQwA" +
38     "BwAIBwAWDAAXABgBAAdHb29kYnllBwAZDAAaABsBAAlUcmFuc2Zvcm0BABBqYXZhL2xhbmcvT2Jq" +
39     "ZWN0AQAQamF2YS9sYW5nL1N5c3RlbQEAA291dAEAFUxqYXZhL2lvL1ByaW50U3RyZWFtOwEAE2ph" +
40     "dmEvaW8vUHJpbnRTdHJlYW0BAAdwcmludGxuAQAVKExqYXZhL2xhbmcvU3RyaW5nOylWACAABQAG" +
41     "AAAAAAACAAAABwAIAAEACQAAAB0AAQABAAAABSq3AAGxAAAAAQAKAAAABgABAAAAEQABAAsACAAB" +
42     "AAkAAAAlAAIAAQAAAAmyAAISA7YABLEAAAABAAoAAAAKAAIAAAATAAgAFAABAAwAAAACAA0=");
43   private static final byte[] DEX_BYTES = Base64.getDecoder().decode(
44     "ZGV4CjAzNQCLXSBQ5FiS3f16krSYZFF8xYZtFVp0GRXMAgAAcAAAAHhWNBIAAAAAAAAAACwCAAAO" +
45     "AAAAcAAAAAYAAACoAAAAAgAAAMAAAAABAAAA2AAAAAQAAADgAAAAAQAAAAABAACsAQAAIAEAAGIB" +
46     "AABqAQAAcwEAAIABAACXAQAAqwEAAL8BAADTAQAA4wEAAOYBAADqAQAA/gEAAAMCAAAMAgAAAgAA" +
47     "AAMAAAAEAAAABQAAAAYAAAAIAAAACAAAAAUAAAAAAAAACQAAAAUAAABcAQAABAABAAsAAAAAAAAA" +
48     "AAAAAAAAAAANAAAAAQABAAwAAAACAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAHAAAAAAAAAB4CAAAA" +
49     "AAAAAQABAAEAAAATAgAABAAAAHAQAwAAAA4AAwABAAIAAAAYAgAACQAAAGIAAAAbAQEAAABuIAIA" +
50     "EAAOAAAAAQAAAAMABjxpbml0PgAHR29vZGJ5ZQALTFRyYW5zZm9ybTsAFUxqYXZhL2lvL1ByaW50" +
51     "U3RyZWFtOwASTGphdmEvbGFuZy9PYmplY3Q7ABJMamF2YS9sYW5nL1N0cmluZzsAEkxqYXZhL2xh" +
52     "bmcvU3lzdGVtOwAOVHJhbnNmb3JtLmphdmEAAVYAAlZMABJlbWl0dGVyOiBqYWNrLTMuMzYAA291" +
53     "dAAHcHJpbnRsbgAFc2F5SGkAEQAHDgATAAcOhQAAAAEBAICABKACAQG4Ag0AAAAAAAAAAQAAAAAA" +
54     "AAABAAAADgAAAHAAAAACAAAABgAAAKgAAAADAAAAAgAAAMAAAAAEAAAAAQAAANgAAAAFAAAABAAA" +
55     "AOAAAAAGAAAAAQAAAAABAAABIAAAAgAAACABAAABEAAAAQAAAFwBAAACIAAADgAAAGIBAAADIAAA" +
56     "AgAAABMCAAAAIAAAAQAAAB4CAAAAEAAAAQAAACwCAAA=");
57 
getClassLoaderFor(String location)58   public static ClassLoader getClassLoaderFor(String location) throws Exception {
59     try {
60       Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader");
61       Constructor<?> ctor = class_loader_class.getConstructor(String.class, ClassLoader.class);
62       /* on Dalvik, this is a DexFile; otherwise, it's null */
63       return (ClassLoader)ctor.newInstance(location + "/" + TEST_NAME + "-ex.jar",
64                                            Main.class.getClassLoader());
65     } catch (ClassNotFoundException e) {
66       // Running on RI. Use URLClassLoader.
67       return new java.net.URLClassLoader(
68           new java.net.URL[] { new java.net.URL("file://" + location + "/classes-ex/") });
69     }
70   }
71 
main(String[] args)72   public static void main(String[] args) {
73     // Don't pop transformations. Make sure that even if 2 threads race to define the class both
74     // will get the same result.
75     setPopRetransformations(false);
76     addCommonTransformationResult("Transform", CLASS_BYTES, DEX_BYTES);
77     enableCommonRetransformation(true);
78     try {
79       /* this is the "alternate" DEX/Jar file */
80       ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION"));
81       Class<?> klass = (Class<?>)new_loader.loadClass("TestMain");
82       if (klass == null) {
83         throw new AssertionError("loadClass failed");
84       }
85       Method run_test = klass.getMethod("runTest");
86       run_test.invoke(null);
87     } catch (Exception e) {
88       System.out.println(e.toString());
89       e.printStackTrace(System.out);
90     }
91   }
92 }
93