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.invoke.*; 19 import java.lang.reflect.Field; 20 import java.util.Base64; 21 22 public class Main { 23 public static final class Transform { 24 static { 25 } 26 27 public static Object foo = null; 28 } 29 30 /* Base64 encoded dex bytes for: 31 * 32 * public static final class Transform { 33 * static {} 34 * public static Object bar = null; 35 * public static Object foo = null; 36 * } 37 */ 38 public static final byte[] DEX_BYTES = 39 Base64.getDecoder() 40 .decode( 41 "ZGV4CjAzNQCjkRjcSr1RJO8FnnCjHV/8h6keJP/+P3WQAwAAcAAAAHhWNBIAAAAAAAAAANgCAAAQ" 42 + "AAAAcAAAAAYAAACwAAAAAQAAAMgAAAACAAAA1AAAAAMAAADkAAAAAQAAAPwAAAB0AgAAHAEAAFwB" 43 + "AABmAQAAbgEAAIABAACIAQAArAEAAMwBAADgAQAA6wEAAPYBAAD5AQAABgIAAAsCAAAQAgAAFgIA" 44 + "AB0CAAACAAAAAwAAAAQAAAAFAAAABgAAAAkAAAAJAAAABQAAAAAAAAAAAAQACwAAAAAABAAMAAAA" 45 + "AAAAAAAAAAAAAAAAAQAAAAQAAAABAAAAAAAAABEAAAAEAAAAAAAAAAcAAADIAgAApAIAAAAAAAAB" 46 + "AAAAAAAAAFABAAAGAAAAEgBpAAAAaQABAA4AAQABAAEAAABVAQAABAAAAHAQAgAAAA4ABwAOPAAF" 47 + "AA4AAAAACDxjbGluaXQ+AAY8aW5pdD4AEExNYWluJFRyYW5zZm9ybTsABkxNYWluOwAiTGRhbHZp" 48 + "ay9hbm5vdGF0aW9uL0VuY2xvc2luZ0NsYXNzOwAeTGRhbHZpay9hbm5vdGF0aW9uL0lubmVyQ2xh" 49 + "c3M7ABJMamF2YS9sYW5nL09iamVjdDsACU1haW4uamF2YQAJVHJhbnNmb3JtAAFWAAthY2Nlc3NG" 50 + "bGFncwADYmFyAANmb28ABG5hbWUABXZhbHVlAHZ+fkQ4eyJjb21waWxhdGlvbi1tb2RlIjoiZGVi" 51 + "dWciLCJtaW4tYXBpIjoxLCJzaGEtMSI6IjI4YmNlZjUwYWM4NTk3Y2YyMmU4OTJiMWJjM2EzYjky" 52 + "Yjc0ZTcwZTkiLCJ2ZXJzaW9uIjoiMS42LjMyLWRldiJ9AAICAQ4YAQIDAgoEGQ0XCAIAAgAACQEJ" 53 + "AIiABJwCAYGABLgCAAAAAAIAAACVAgAAmwIAALwCAAAAAAAAAAAAAAAAAAAPAAAAAAAAAAEAAAAA" 54 + "AAAAAQAAABAAAABwAAAAAgAAAAYAAACwAAAAAwAAAAEAAADIAAAABAAAAAIAAADUAAAABQAAAAMA" 55 + "AADkAAAABgAAAAEAAAD8AAAAASAAAAIAAAAcAQAAAyAAAAIAAABQAQAAAiAAABAAAABcAQAABCAA" 56 + "AAIAAACVAgAAACAAAAEAAACkAgAAAxAAAAIAAAC4AgAABiAAAAEAAADIAgAAABAAAAEAAADYAgAA"); 57 main(String[] args)58 public static void main(String[] args) throws Exception, Throwable { 59 System.loadLibrary(args[0]); 60 Field f = Transform.class.getDeclaredField("foo"); 61 Transform.foo = "THIS IS A FOO VALUE"; 62 System.out.println("Foo value is " + f.get(null)); 63 final int max_depth = 10; 64 Object[] results = new Object[max_depth]; 65 Runnable res = 66 () -> { 67 Redefinition.doCommonStructuralClassRedefinition(Transform.class, DEX_BYTES); 68 }; 69 for (int i = 0; i < max_depth; i++) { 70 final Runnable next = res; 71 final int id = i; 72 res = 73 () -> { 74 try { 75 results[id] = NativeFieldScopeCheck(f, next).invokeExact(); 76 } catch (Throwable t) { 77 throw new Error("Failed!", t); 78 } 79 }; 80 } 81 res.run(); 82 for (int i = 0; i < max_depth; i++) { 83 System.out.println("Result at depth " + i + ": " + results[i]); 84 } 85 } 86 87 // Hold the field as a ArtField, run the 'test' function, turn the ArtField into a MethodHandle 88 // directly and return that. NativeFieldScopeCheck(Field in, Runnable test)89 public static native MethodHandle NativeFieldScopeCheck(Field in, Runnable test); 90 } 91