1 /*
2  * Copyright (C) 2017 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.ArrayList;
20 // Common Redefinition functions. Placed here for use by CTS
21 public class Redefinition {
22   public static class CommonClassDefinition {
23     public final Class<?> target;
24     public final byte[] class_file_bytes;
25     public final byte[] dex_file_bytes;
26 
CommonClassDefinition(Class<?> target, byte[] class_file_bytes, byte[] dex_file_bytes)27     public CommonClassDefinition(Class<?> target, byte[] class_file_bytes, byte[] dex_file_bytes) {
28       this.target = target;
29       this.class_file_bytes = class_file_bytes;
30       this.dex_file_bytes = dex_file_bytes;
31     }
32   }
33 
34   public static class DexOnlyClassDefinition extends CommonClassDefinition {
DexOnlyClassDefinition(Class<?> target, byte[] dex_file_bytes)35     public DexOnlyClassDefinition(Class<?> target, byte[] dex_file_bytes) {
36       super(target, new byte[0], dex_file_bytes);
37     }
38   }
39 
40   // A set of possible test configurations. Test should set this if they need to.
41   // This must be kept in sync with the defines in ti-agent/common_helper.cc
42   public static enum Config {
43     COMMON_REDEFINE(0),
44     COMMON_RETRANSFORM(1),
45     COMMON_TRANSFORM(2),
46     STRUCTURAL_TRANSFORM(3);
47 
48     private final int val;
Config(int val)49     private Config(int val) {
50       this.val = val;
51     }
52   }
53 
setTestConfiguration(Config type)54   public static void setTestConfiguration(Config type) {
55     nativeSetTestConfiguration(type.val);
56   }
57 
nativeSetTestConfiguration(int type)58   private static native void nativeSetTestConfiguration(int type);
59 
60   // Transforms the class
doCommonClassRedefinition(Class<?> target, byte[] classfile, byte[] dexfile)61   public static native void doCommonClassRedefinition(Class<?> target,
62                                                       byte[] classfile,
63                                                       byte[] dexfile);
64 
doMultiClassRedefinition(CommonClassDefinition... defs)65   public static void doMultiClassRedefinition(CommonClassDefinition... defs) {
66     ArrayList<Class<?>> classes = new ArrayList<>();
67     ArrayList<byte[]> class_files = new ArrayList<>();
68     ArrayList<byte[]> dex_files = new ArrayList<>();
69 
70     for (CommonClassDefinition d : defs) {
71       classes.add(d.target);
72       class_files.add(d.class_file_bytes);
73       dex_files.add(d.dex_file_bytes);
74     }
75     doCommonMultiClassRedefinition(classes.toArray(new Class<?>[0]),
76                                    class_files.toArray(new byte[0][]),
77                                    dex_files.toArray(new byte[0][]));
78   }
79 
addMultiTransformationResults(CommonClassDefinition... defs)80   public static void addMultiTransformationResults(CommonClassDefinition... defs) {
81     for (CommonClassDefinition d : defs) {
82       addCommonTransformationResult(d.target.getCanonicalName(),
83                                     d.class_file_bytes,
84                                     d.dex_file_bytes);
85     }
86   }
87 
doCommonMultiClassRedefinition(Class<?>[] targets, byte[][] classfiles, byte[][] dexfiles)88   public static native void doCommonMultiClassRedefinition(Class<?>[] targets,
89                                                            byte[][] classfiles,
90                                                            byte[][] dexfiles);
doCommonClassRetransformation(Class<?>.... target)91   public static native void doCommonClassRetransformation(Class<?>... target);
setPopRetransformations(boolean pop)92   public static native void setPopRetransformations(boolean pop);
popTransformationFor(String name)93   public static native void popTransformationFor(String name);
enableCommonRetransformation(boolean enable)94   public static native void enableCommonRetransformation(boolean enable);
addCommonTransformationResult(String target_name, byte[] class_bytes, byte[] dex_bytes)95   public static native void addCommonTransformationResult(String target_name,
96                                                           byte[] class_bytes,
97                                                           byte[] dex_bytes);
98 
doCommonStructuralClassRedefinition(Class<?> target, byte[] dex_file)99   public static native void doCommonStructuralClassRedefinition(Class<?> target, byte[] dex_file);
doMultiStructuralClassRedefinition(CommonClassDefinition... defs)100   public static void doMultiStructuralClassRedefinition(CommonClassDefinition... defs) {
101     ArrayList<Class<?>> classes = new ArrayList<>();
102     ArrayList<byte[]> dex_files = new ArrayList<>();
103 
104     for (CommonClassDefinition d : defs) {
105       classes.add(d.target);
106       dex_files.add(d.dex_file_bytes);
107     }
108     doCommonMultiStructuralClassRedefinition(classes.toArray(new Class<?>[0]),
109                                              dex_files.toArray(new byte[0][]));
110   }
doCommonMultiStructuralClassRedefinition(Class<?>[] targets, byte[][] dexfiles)111   public static native void doCommonMultiStructuralClassRedefinition(Class<?>[] targets,
112                                                                      byte[][] dexfiles);
isStructurallyModifiable(Class<?> target)113   public static native boolean isStructurallyModifiable(Class<?> target);
114 }
115