1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker package art; 18*795d594fSAndroid Build Coastguard Worker 19*795d594fSAndroid Build Coastguard Worker import java.util.ArrayList; 20*795d594fSAndroid Build Coastguard Worker // Common Redefinition functions. Placed here for use by CTS 21*795d594fSAndroid Build Coastguard Worker public class Redefinition { 22*795d594fSAndroid Build Coastguard Worker public static class CommonClassDefinition { 23*795d594fSAndroid Build Coastguard Worker public final Class<?> target; 24*795d594fSAndroid Build Coastguard Worker public final byte[] class_file_bytes; 25*795d594fSAndroid Build Coastguard Worker public final byte[] dex_file_bytes; 26*795d594fSAndroid Build Coastguard Worker CommonClassDefinition(Class<?> target, byte[] class_file_bytes, byte[] dex_file_bytes)27*795d594fSAndroid Build Coastguard Worker public CommonClassDefinition(Class<?> target, byte[] class_file_bytes, byte[] dex_file_bytes) { 28*795d594fSAndroid Build Coastguard Worker this.target = target; 29*795d594fSAndroid Build Coastguard Worker this.class_file_bytes = class_file_bytes; 30*795d594fSAndroid Build Coastguard Worker this.dex_file_bytes = dex_file_bytes; 31*795d594fSAndroid Build Coastguard Worker } 32*795d594fSAndroid Build Coastguard Worker } 33*795d594fSAndroid Build Coastguard Worker 34*795d594fSAndroid Build Coastguard Worker public static class DexOnlyClassDefinition extends CommonClassDefinition { DexOnlyClassDefinition(Class<?> target, byte[] dex_file_bytes)35*795d594fSAndroid Build Coastguard Worker public DexOnlyClassDefinition(Class<?> target, byte[] dex_file_bytes) { 36*795d594fSAndroid Build Coastguard Worker super(target, new byte[0], dex_file_bytes); 37*795d594fSAndroid Build Coastguard Worker } 38*795d594fSAndroid Build Coastguard Worker } 39*795d594fSAndroid Build Coastguard Worker 40*795d594fSAndroid Build Coastguard Worker // A set of possible test configurations. Test should set this if they need to. 41*795d594fSAndroid Build Coastguard Worker // This must be kept in sync with the defines in ti-agent/common_helper.cc 42*795d594fSAndroid Build Coastguard Worker public static enum Config { 43*795d594fSAndroid Build Coastguard Worker COMMON_REDEFINE(0), 44*795d594fSAndroid Build Coastguard Worker COMMON_RETRANSFORM(1), 45*795d594fSAndroid Build Coastguard Worker COMMON_TRANSFORM(2), 46*795d594fSAndroid Build Coastguard Worker STRUCTURAL_TRANSFORM(3); 47*795d594fSAndroid Build Coastguard Worker 48*795d594fSAndroid Build Coastguard Worker private final int val; Config(int val)49*795d594fSAndroid Build Coastguard Worker private Config(int val) { 50*795d594fSAndroid Build Coastguard Worker this.val = val; 51*795d594fSAndroid Build Coastguard Worker } 52*795d594fSAndroid Build Coastguard Worker } 53*795d594fSAndroid Build Coastguard Worker setTestConfiguration(Config type)54*795d594fSAndroid Build Coastguard Worker public static void setTestConfiguration(Config type) { 55*795d594fSAndroid Build Coastguard Worker nativeSetTestConfiguration(type.val); 56*795d594fSAndroid Build Coastguard Worker } 57*795d594fSAndroid Build Coastguard Worker nativeSetTestConfiguration(int type)58*795d594fSAndroid Build Coastguard Worker private static native void nativeSetTestConfiguration(int type); 59*795d594fSAndroid Build Coastguard Worker 60*795d594fSAndroid Build Coastguard Worker // Transforms the class doCommonClassRedefinition(Class<?> target, byte[] classfile, byte[] dexfile)61*795d594fSAndroid Build Coastguard Worker public static native void doCommonClassRedefinition(Class<?> target, 62*795d594fSAndroid Build Coastguard Worker byte[] classfile, 63*795d594fSAndroid Build Coastguard Worker byte[] dexfile); 64*795d594fSAndroid Build Coastguard Worker doMultiClassRedefinition(CommonClassDefinition... defs)65*795d594fSAndroid Build Coastguard Worker public static void doMultiClassRedefinition(CommonClassDefinition... defs) { 66*795d594fSAndroid Build Coastguard Worker ArrayList<Class<?>> classes = new ArrayList<>(); 67*795d594fSAndroid Build Coastguard Worker ArrayList<byte[]> class_files = new ArrayList<>(); 68*795d594fSAndroid Build Coastguard Worker ArrayList<byte[]> dex_files = new ArrayList<>(); 69*795d594fSAndroid Build Coastguard Worker 70*795d594fSAndroid Build Coastguard Worker for (CommonClassDefinition d : defs) { 71*795d594fSAndroid Build Coastguard Worker classes.add(d.target); 72*795d594fSAndroid Build Coastguard Worker class_files.add(d.class_file_bytes); 73*795d594fSAndroid Build Coastguard Worker dex_files.add(d.dex_file_bytes); 74*795d594fSAndroid Build Coastguard Worker } 75*795d594fSAndroid Build Coastguard Worker doCommonMultiClassRedefinition(classes.toArray(new Class<?>[0]), 76*795d594fSAndroid Build Coastguard Worker class_files.toArray(new byte[0][]), 77*795d594fSAndroid Build Coastguard Worker dex_files.toArray(new byte[0][])); 78*795d594fSAndroid Build Coastguard Worker } 79*795d594fSAndroid Build Coastguard Worker addMultiTransformationResults(CommonClassDefinition... defs)80*795d594fSAndroid Build Coastguard Worker public static void addMultiTransformationResults(CommonClassDefinition... defs) { 81*795d594fSAndroid Build Coastguard Worker for (CommonClassDefinition d : defs) { 82*795d594fSAndroid Build Coastguard Worker addCommonTransformationResult(d.target.getCanonicalName(), 83*795d594fSAndroid Build Coastguard Worker d.class_file_bytes, 84*795d594fSAndroid Build Coastguard Worker d.dex_file_bytes); 85*795d594fSAndroid Build Coastguard Worker } 86*795d594fSAndroid Build Coastguard Worker } 87*795d594fSAndroid Build Coastguard Worker doCommonMultiClassRedefinition(Class<?>[] targets, byte[][] classfiles, byte[][] dexfiles)88*795d594fSAndroid Build Coastguard Worker public static native void doCommonMultiClassRedefinition(Class<?>[] targets, 89*795d594fSAndroid Build Coastguard Worker byte[][] classfiles, 90*795d594fSAndroid Build Coastguard Worker byte[][] dexfiles); doCommonClassRetransformation(Class<?>.... target)91*795d594fSAndroid Build Coastguard Worker public static native void doCommonClassRetransformation(Class<?>... target); setPopRetransformations(boolean pop)92*795d594fSAndroid Build Coastguard Worker public static native void setPopRetransformations(boolean pop); popTransformationFor(String name)93*795d594fSAndroid Build Coastguard Worker public static native void popTransformationFor(String name); enableCommonRetransformation(boolean enable)94*795d594fSAndroid Build Coastguard Worker public static native void enableCommonRetransformation(boolean enable); addCommonTransformationResult(String target_name, byte[] class_bytes, byte[] dex_bytes)95*795d594fSAndroid Build Coastguard Worker public static native void addCommonTransformationResult(String target_name, 96*795d594fSAndroid Build Coastguard Worker byte[] class_bytes, 97*795d594fSAndroid Build Coastguard Worker byte[] dex_bytes); 98*795d594fSAndroid Build Coastguard Worker doCommonStructuralClassRedefinition(Class<?> target, byte[] dex_file)99*795d594fSAndroid Build Coastguard Worker public static native void doCommonStructuralClassRedefinition(Class<?> target, byte[] dex_file); doMultiStructuralClassRedefinition(CommonClassDefinition... defs)100*795d594fSAndroid Build Coastguard Worker public static void doMultiStructuralClassRedefinition(CommonClassDefinition... defs) { 101*795d594fSAndroid Build Coastguard Worker ArrayList<Class<?>> classes = new ArrayList<>(); 102*795d594fSAndroid Build Coastguard Worker ArrayList<byte[]> dex_files = new ArrayList<>(); 103*795d594fSAndroid Build Coastguard Worker 104*795d594fSAndroid Build Coastguard Worker for (CommonClassDefinition d : defs) { 105*795d594fSAndroid Build Coastguard Worker classes.add(d.target); 106*795d594fSAndroid Build Coastguard Worker dex_files.add(d.dex_file_bytes); 107*795d594fSAndroid Build Coastguard Worker } 108*795d594fSAndroid Build Coastguard Worker doCommonMultiStructuralClassRedefinition(classes.toArray(new Class<?>[0]), 109*795d594fSAndroid Build Coastguard Worker dex_files.toArray(new byte[0][])); 110*795d594fSAndroid Build Coastguard Worker } doCommonMultiStructuralClassRedefinition(Class<?>[] targets, byte[][] dexfiles)111*795d594fSAndroid Build Coastguard Worker public static native void doCommonMultiStructuralClassRedefinition(Class<?>[] targets, 112*795d594fSAndroid Build Coastguard Worker byte[][] dexfiles); isStructurallyModifiable(Class<?> target)113*795d594fSAndroid Build Coastguard Worker public static native boolean isStructurallyModifiable(Class<?> target); 114*795d594fSAndroid Build Coastguard Worker } 115