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 /** 31 * Base64 encoded dex bytes for: 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 assertEquals(Object a, Object b)58 public static void assertEquals(Object a, Object b) { 59 if (a != b) { 60 throw new Error("Expected " + b + ", got " + a); 61 } 62 } 63 main(String[] args)64 public static void main(String[] args) throws Exception, Throwable { 65 System.loadLibrary(args[0]); 66 Field f = Transform.class.getDeclaredField("foo"); 67 Transform.foo = "THIS IS A FOO VALUE"; 68 assertEquals(f.get(null), Transform.foo); 69 MethodHandle j = 70 NativeFieldScopeCheck( 71 f, 72 () -> { 73 Redefinition.doCommonStructuralClassRedefinition(Transform.class, DEX_BYTES); 74 }); 75 assertEquals(j.invokeExact(), Transform.foo); 76 } 77 78 // Hold the field as a ArtField, run the 'test' function, turn the ArtField into a MethodHandle 79 // directly and return that. NativeFieldScopeCheck(Field in, Runnable test)80 public static native MethodHandle NativeFieldScopeCheck(Field in, Runnable test); 81 } 82