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 package art;
18 
19 import java.util.Base64;
20 import java.lang.reflect.*;
21 public class Test1993 {
22 
23   static class Transform {
sayHi()24     public void sayHi() {
25       // Use lower 'h' to make sure the string will have a different string id
26       // than the transformation (the transformation code is the same except
27       // the actual printed String, which was making the test inacurately passing
28       // in JIT mode when loading the string from the dex cache, as the string ids
29       // of the two different strings were the same).
30       // We know the string ids will be different because lexicographically:
31       // "Goodbye" < "LTransform;" < "hello".
32       System.out.println("hello");
33     }
34   }
35 
36   /**
37    * base64 encoded class/dex file for
38    * class Transform {
39    *   public void sayHi() {
40    *    System.out.println("Goodbye");
41    *   }
42    * }
43    */
44   private static final byte[] DEX_BYTES = Base64.getDecoder().decode(
45     "ZGV4CjAzNQDxrdbiBcsn0r58mdtcdyDxVUxwbWfShNQwBAAAcAAAAHhWNBIAAAAAAAAAAGwDAAAV" +
46     "AAAAcAAAAAkAAADEAAAAAgAAAOgAAAABAAAAAAEAAAQAAAAIAQAAAQAAACgBAADoAgAASAEAAJIB" +
47     "AACaAQAAowEAAL0BAADNAQAA8QEAABECAAAoAgAAPAIAAFACAABkAgAAcwIAAH4CAACBAgAAhQIA" +
48     "AJICAACYAgAAnQIAAKYCAACtAgAAtAIAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAA" +
49     "DAAAAAwAAAAIAAAAAAAAAA0AAAAIAAAAjAEAAAcABAAQAAAAAAAAAAAAAAAAAAAAEgAAAAQAAQAR" +
50     "AAAABQAAAAAAAAAAAAAAAAAAAAUAAAAAAAAACgAAAFwDAAA7AwAAAAAAAAEAAQABAAAAgAEAAAQA" +
51     "AABwEAMAAAAOAAMAAQACAAAAhAEAAAgAAABiAAAAGgEBAG4gAgAQAA4AAwAOAAUADngAAAAAAQAA" +
52     "AAYABjxpbml0PgAHR29vZGJ5ZQAYTGFydC9UZXN0MTk5MyRUcmFuc2Zvcm07AA5MYXJ0L1Rlc3Qx" +
53     "OTkzOwAiTGRhbHZpay9hbm5vdGF0aW9uL0VuY2xvc2luZ0NsYXNzOwAeTGRhbHZpay9hbm5vdGF0" +
54     "aW9uL0lubmVyQ2xhc3M7ABVMamF2YS9pby9QcmludFN0cmVhbTsAEkxqYXZhL2xhbmcvT2JqZWN0" +
55     "OwASTGphdmEvbGFuZy9TdHJpbmc7ABJMamF2YS9sYW5nL1N5c3RlbTsADVRlc3QxOTkzLmphdmEA" +
56     "CVRyYW5zZm9ybQABVgACVkwAC2FjY2Vzc0ZsYWdzAARuYW1lAANvdXQAB3ByaW50bG4ABXNheUhp" +
57     "AAV2YWx1ZQB2fn5EOHsiY29tcGlsYXRpb24tbW9kZSI6ImRlYnVnIiwibWluLWFwaSI6MSwic2hh" +
58     "LTEiOiJjZDkwMDIzOTMwZDk3M2Y1NzcxMWYxZDRmZGFhZDdhM2U0NzE0NjM3IiwidmVyc2lvbiI6" +
59     "IjEuNy4xNC1kZXYifQACAgETGAECAwIOBAgPFwsAAAEBAICABMgCAQHgAgAAAAAAAAACAAAALAMA" +
60     "ADIDAABQAwAAAAAAAAAAAAAAAAAAEAAAAAAAAAABAAAAAAAAAAEAAAAVAAAAcAAAAAIAAAAJAAAA" +
61     "xAAAAAMAAAACAAAA6AAAAAQAAAABAAAAAAEAAAUAAAAEAAAACAEAAAYAAAABAAAAKAEAAAEgAAAC" +
62     "AAAASAEAAAMgAAACAAAAgAEAAAEQAAABAAAAjAEAAAIgAAAVAAAAkgEAAAQgAAACAAAALAMAAAAg" +
63     "AAABAAAAOwMAAAMQAAACAAAATAMAAAYgAAABAAAAXAMAAAAQAAABAAAAbAMAAA==");
64 
run()65   public static void run() throws Exception {
66     Redefinition.setTestConfiguration(Redefinition.Config.COMMON_REDEFINE);
67     doTest(new Transform());
68   }
69 
doTest(Transform t)70   public static void doTest(Transform t) throws Exception {
71     System.out.println("Can structurally Redefine: " +
72       Redefinition.isStructurallyModifiable(Transform.class));
73     t.sayHi();
74     Redefinition.doCommonStructuralClassRedefinition(Transform.class, DEX_BYTES);
75     t.sayHi();
76     // Check and make sure we didn't structurally redefine by looking for ClassExt.obsoleteClass
77     Field ext_data_field = Class.class.getDeclaredField("extData");
78     ext_data_field.setAccessible(true);
79     Object ext_data = ext_data_field.get(Transform.class);
80     Field obsolete_class_field = ext_data.getClass().getDeclaredField("obsoleteClass");
81     obsolete_class_field.setAccessible(true);
82     if (obsolete_class_field.get(ext_data) != null)  {
83       System.out.println("Expected no ClassExt.obsoleteClass but got " + obsolete_class_field.get(ext_data));
84     }
85   }
86 }
87