Home | History | Annotate | Download | only in src
      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 art.Redefinition;
     18 
     19 import java.lang.reflect.*;
     20 import java.util.Base64;
     21 import java.util.concurrent.CountDownLatch;
     22 import java.util.function.Consumer;
     23 
     24 class Main {
     25   public static String TEST_NAME = "1950-unprepared-transform";
     26 
     27   // Base 64 encoding of the following class:
     28   //
     29   // public class Transform {
     30   //   public String toString() {
     31   //     return "Transformed object!";
     32   //   }
     33   // }
     34   public static final byte[] CLASS_BYTES = Base64.getDecoder().decode(
     35     "yv66vgAAADQAEQoABAANCAAOBwAPBwAQAQAGPGluaXQ+AQADKClWAQAEQ29kZQEAD0xpbmVOdW1i" +
     36     "ZXJUYWJsZQEACHRvU3RyaW5nAQAUKClMamF2YS9sYW5nL1N0cmluZzsBAApTb3VyY2VGaWxlAQAO" +
     37     "VHJhbnNmb3JtLmphdmEMAAUABgEAE1RyYW5zZm9ybWVkIG9iamVjdCEBAAlUcmFuc2Zvcm0BABBq" +
     38     "YXZhL2xhbmcvT2JqZWN0ACEAAwAEAAAAAAACAAEABQAGAAEABwAAAB0AAQABAAAABSq3AAGxAAAA" +
     39     "AQAIAAAABgABAAAAEgABAAkACgABAAcAAAAbAAEAAQAAAAMSArAAAAABAAgAAAAGAAEAAAAUAAEA" +
     40     "CwAAAAIADA==");
     41 
     42   public static final byte[] DEX_BYTES = Base64.getDecoder().decode(
     43     "ZGV4CjAzOACaXU/P8oJOECPrdN1Cu9/ob2cUb2vOKxqYAgAAcAAAAHhWNBIAAAAAAAAAABACAAAK" +
     44     "AAAAcAAAAAQAAACYAAAAAgAAAKgAAAAAAAAAAAAAAAMAAADAAAAAAQAAANgAAACgAQAA+AAAADAB" +
     45     "AAA4AQAAOwEAAEgBAABcAQAAcAEAAIABAACVAQAAmAEAAKIBAAACAAAAAwAAAAQAAAAHAAAAAQAA" +
     46     "AAIAAAAAAAAABwAAAAMAAAAAAAAAAAABAAAAAAAAAAAACAAAAAEAAQAAAAAAAAAAAAEAAAABAAAA" +
     47     "AAAAAAUAAAAAAAAAAAIAAAAAAAACAAEAAAAAACwBAAADAAAAGgAGABEAAAABAAEAAQAAACgBAAAE" +
     48     "AAAAcBACAAAADgASAA4AFAAOAAY8aW5pdD4AAUwAC0xUcmFuc2Zvcm07ABJMamF2YS9sYW5nL09i" +
     49     "amVjdDsAEkxqYXZhL2xhbmcvU3RyaW5nOwAOVHJhbnNmb3JtLmphdmEAE1RyYW5zZm9ybWVkIG9i" +
     50     "amVjdCEAAVYACHRvU3RyaW5nAFx+fkQ4eyJtaW4tYXBpIjoyNywic2hhLTEiOiI3YTdjNDlhY2Nj" +
     51     "NTkzNTIyNzY4MTY3MThhNGM3YWU1MmY5NjgzZjk5IiwidmVyc2lvbiI6InYxLjIuNC1kZXYifQAA" +
     52     "AAEBAIGABJACAQH4AQAACwAAAAAAAAABAAAAAAAAAAEAAAAKAAAAcAAAAAIAAAAEAAAAmAAAAAMA" +
     53     "AAACAAAAqAAAAAUAAAADAAAAwAAAAAYAAAABAAAA2AAAAAEgAAACAAAA+AAAAAMgAAACAAAAKAEA" +
     54     "AAIgAAAKAAAAMAEAAAAgAAABAAAAAAIAAAAQAAABAAAAEAIAAA==");
     55 
     56   public static native void setupClassLoadHook(Thread target);
     57   public static native void clearClassLoadHook(Thread target);
     58   private static Consumer<Class<?>> doRedefine = null;
     59 
     60   public static void doClassLoad(Class<?> c) {
     61     try {
     62       if (c.getName().equals("Transform")) {
     63         Redefinition.addCommonTransformationResult("Transform", CLASS_BYTES, DEX_BYTES);
     64         doRedefine.accept(c);
     65         System.out.println("retransformClasses on an unprepared class succeeded");
     66       }
     67     } catch (Throwable e) {
     68       System.out.println("Trying to redefine: " + c + ". " +
     69           "Caught error " + e.getClass() + ": " + e.getMessage());
     70     }
     71   }
     72 
     73   public static ClassLoader getClassLoaderFor(String location) throws Exception {
     74     try {
     75       Class<?> class_loader_class = Class.forName("dalvik.system.PathClassLoader");
     76       Constructor<?> ctor = class_loader_class.getConstructor(String.class, ClassLoader.class);
     77       /* on Dalvik, this is a DexFile; otherwise, it's null */
     78       return (ClassLoader)ctor.newInstance(location + "/" + TEST_NAME + "-ex.jar",
     79                                            Main.class.getClassLoader());
     80     } catch (ClassNotFoundException e) {
     81       // Running on RI. Use URLClassLoader.
     82       return new java.net.URLClassLoader(
     83           new java.net.URL[] { new java.net.URL("file://" + location + "/classes-ex/") });
     84     }
     85   }
     86 
     87   public static void testCurrentThread() throws Throwable {
     88     System.out.println("Redefine in ClassLoad on current thread.");
     89     doRedefine = (c) -> { Redefinition.doCommonClassRetransformation(c); };
     90     ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION"));
     91     Class<?> klass = (Class<?>)new_loader.loadClass("Transform");
     92     if (klass == null) {
     93       throw new AssertionError("loadClass failed");
     94     }
     95     Object o = klass.newInstance();
     96     System.out.println("Object out is: " + o);
     97   }
     98 
     99   public static void testRemoteThread() throws Throwable {
    100     System.out.println("Redefine during ClassLoad on another thread.");
    101     final Class[] loaded = new Class[] { null, };
    102     final CountDownLatch gotClass = new CountDownLatch(1);
    103     final CountDownLatch wokeUp = new CountDownLatch(1);
    104     Thread redef_thread = new Thread(() -> {
    105       try {
    106         gotClass.await();
    107         wokeUp.countDown();
    108         // This will wait until the otehr thread returns so we need to wake up the other thread
    109         // first.
    110         Redefinition.doCommonClassRetransformation(loaded[0]);
    111       } catch (Exception e) {
    112         throw new Error("Failed to do redef!", e);
    113       }
    114     });
    115     redef_thread.start();
    116     doRedefine = (c) -> {
    117       try {
    118         loaded[0] = c;
    119         gotClass.countDown();
    120         wokeUp.await();
    121         // Let the other thread do some stuff.
    122         Thread.sleep(5000);
    123       } catch (Exception e) {
    124         throw new Error("Failed to do redef!", e);
    125       }
    126     };
    127     ClassLoader new_loader = getClassLoaderFor(System.getenv("DEX_LOCATION"));
    128     Class<?> klass = (Class<?>)new_loader.loadClass("Transform");
    129     if (klass == null) {
    130       throw new AssertionError("loadClass failed");
    131     }
    132     Object o = klass.newInstance();
    133     System.out.println("Object out is: " + o);
    134     redef_thread.join();
    135     System.out.println("Redefinition thread finished.");
    136   }
    137 
    138   public static void main(String[] args) {
    139     // make sure we can do the transform.
    140     Redefinition.setTestConfiguration(Redefinition.Config.COMMON_RETRANSFORM);
    141     Redefinition.setPopRetransformations(false);
    142     Redefinition.enableCommonRetransformation(true);
    143     setupClassLoadHook(Thread.currentThread());
    144     try {
    145       testCurrentThread();
    146       testRemoteThread();
    147     } catch (Throwable e) {
    148       System.out.println(e.toString());
    149       e.printStackTrace(System.out);
    150     }
    151     clearClassLoadHook(Thread.currentThread());
    152   }
    153 }
    154