1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.function.Consumer;
import java.lang.reflect.Method;
import java.util.Base64;
public class Main {
// import java.util.function.Consumer;
//
// class Transform {
// private void Start(Consumer<String> reporter) {
// reporter.accept("Hello - private - Transformed");
// }
//
// private void Finish(Consumer<String> reporter) {
// reporter.accept("Goodbye - private - Transformed");
// }
//
// public void sayHi(Runnable r, Consumer<String> reporter) {
// reporter.accept("pre Start private method call - Transformed");
// Start(reporter);
// reporter.accept("post Start private method call - Transformed");
// r.run();
// reporter.accept("pre Finish private method call - Transformed");
// Finish(reporter);
// reporter.accept("post Finish private method call - Transformed");
// }
// }
private static final byte[] CLASS_BYTES = Base64.getDecoder().decode(
"yv66vgAAADQAMAoADQAcCAAdCwAeAB8IACAIACEKAAwAIggAI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AAN" +
"AAAAAAAEAAAADgAPAAEAEAAAAB0AAQABAAAABSq3AAGxAAAAAQARAAAABgABAAAAEwACABIAEwAC" +
"ABAAAAAlAAIAAgAAAAkrEgK5AAMCALEAAAABABEAAAAKAAIAAAAVAAgAFgAUAAAAAgAVAAIAFgAT" +
"AAIAEAAAACUAAgACAAAACSsSBLkAAwIAsQAAAAEAEQAAAAoAAgAAABkACAAaABQAAAACABUAAQAX" +
"ABgAAgAQAAAAZQACAAMAAAAxLBIFuQADAgAqLLcABiwSB7kAAwIAK7kACAEALBIJuQADAgAqLLcA" +
"CiwSC7kAAwIAsQAAAAEAEQAAACIACAAAAB0ACAAeAA0AHwAVACAAGwAhACMAIgAoACMAMAAkABQA" +
"AAACABkAAQAaAAAAAgAb");
private static final byte[] DEX_BYTES = Base64.getDecoder().decode(
"ZGV4CjAzNQBc8wr9PcHqnOR61m+0kimXTSddVMToJPuYBQAAcAAAAHhWNBIAAAAAAAAAAOAEAAAc" +
"AAAAcAAAAAYAAADgAAAABAAAAPgAAAAAAAAAAAAAAAcAAAAoAQAAAQAAAGABAAAYBAAAgAEAAHoC" +
"AAB9AgAAgAIAAIgCAACOAgAAlgIAALcCAADWAgAA4wIAAAIDAAAWAwAALAMAAEADAABeAwAAfQMA" +
"AIQDAACUAwAAlwMAAJsDAACgAwAAqAMAALwDAADrAwAAGQQAAEcEAAB0BAAAeQQAAIAEAAAHAAAA" +
"CAAAAAkAAAAKAAAADQAAABAAAAAQAAAABQAAAAAAAAARAAAABQAAAGQCAAASAAAABQAAAGwCAAAR" +
"AAAABQAAAHQCAAAAAAAAAgAAAAAAAwAEAAAAAAADAA4AAAAAAAIAGgAAAAIAAAACAAAAAwAAABkA" +
"AAAEAAEAEwAAAAAAAAAAAAAAAgAAAAAAAAAPAAAAPAIAAMoEAAAAAAAAAQAAAKgEAAABAAAAuAQA" +
"AAEAAQABAAAAhwQAAAQAAABwEAQAAAAOAAMAAgACAAAAjAQAAAcAAAAbAAUAAAByIAYAA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AQEbHAYXABcKFwwXARcLFwMAAAMBAICABJADAQKoAwECyAMDAegDDwAAAAAA" +
"AAABAAAAAAAAAAEAAAAcAAAAcAAAAAIAAAAGAAAA4AAAAAMAAAAEAAAA+AAAAAUAAAAHAAAAKAEA" +
"AAYAAAABAAAAYAEAAAMQAAACAAAAgAEAAAEgAAAEAAAAkAEAAAYgAAABAAAAPAIAAAEQAAADAAAA" +
"ZAIAAAIgAAAcAAAAegIAAAMgAAAEAAAAhwQAAAQgAAACAAAAqAQAAAAgAAABAAAAygQAAAAQAAAB" +
"AAAA4AQAAA==");
// A class that we can use to keep track of the output of this test.
private static class TestWatcher implements Consumer<String> {
private StringBuilder sb;
public TestWatcher() {
sb = new StringBuilder();
}
@Override
public void accept(String s) {
sb.append(s);
sb.append('\n');
}
public String getOutput() {
return sb.toString();
}
public void clear() {
sb = new StringBuilder();
}
}
public static void main(String[] args) {
System.loadLibrary(args[1]);
doTest(new Transform(), new TestWatcher());
}
// TODO Workaround to (1) inability to ensure that current_method is not put into a register by
// the JIT and/or (2) inability to deoptimize frames near runtime functions.
// TODO Fix one/both of these issues.
public static void doCall(Runnable r) {
r.run();
}
private static boolean interpreting = true;
private static boolean retry = false;
public static void doTest(Transform t, TestWatcher w) {
// Get the methods that need to be optimized.
Method say_hi_method;
Method do_call_method;
// Figure out if we can even JIT at all.
final boolean has_jit = hasJit();
try {
say_hi_method = Transform.class.getDeclaredMethod(
"sayHi", Runnable.class, Consumer.class);
do_call_method = Main.class.getDeclaredMethod("doCall", Runnable.class);
} catch (Exception e) {
System.out.println("Unable to find methods!");
e.printStackTrace();
return;
}
// Makes sure the stack is the way we want it for the test and does the redefinition. It will
// set the retry boolean to true if we need to go around again due to a bad stack.
Runnable do_redefinition = () -> {
if (has_jit &&
(Main.isInterpretedFunction(say_hi_method, true) ||
Main.isInterpretedFunction(do_call_method, false))) {
// Try again. We are not running the right jitted methods/cannot redefine them now.
retry = true;
} else {
// Actually do the redefinition. The stack looks good.
retry = false;
w.accept("transforming calling function");
doCommonClassRedefinition(Transform.class, CLASS_BYTES, DEX_BYTES);
}
};
// This does nothing.
Runnable noop = () -> {};
// This just prints something out to show we are running the Runnable.
Runnable say_nothing = () -> { w.accept("Not doing anything here"); };
// This checks to see if we have jitted the methods we are testing.
Runnable check_interpreting = () -> {
// TODO remove the second check when we remove the doCall function. We need to check that
// both of these functions aren't being interpreted because if sayHi is the test doesn't do
// anything and if doCall is then there will be a runtime call right above the sayHi
// function preventing sayHi from being deoptimized.
interpreting = has_jit && (Main.isInterpretedFunction(say_hi_method, true) ||
Main.isInterpretedFunction(do_call_method, false));
};
do {
w.clear();
// Wait for the methods to be jitted
long j = 0;
do {
for (int i = 0; i < 10000; i++) {
t.sayHi(noop, w);
j++;
// Clear so that we won't OOM if we go around a few times.
w.clear();
}
t.sayHi(check_interpreting, w);
if (j >= 1000000) {
System.out.println("FAIL: Could not make sayHi be Jitted!");
return;
}
j++;
} while(interpreting);
// Clear output. Now we try for real.
w.clear();
// Try and redefine.
t.sayHi(say_nothing, w);
t.sayHi(do_redefinition, w);
t.sayHi(say_nothing, w);
} while (retry);
// Print output of last run.
System.out.print(w.getOutput());
}
private static native boolean hasJit();
private static native boolean isInterpretedFunction(Method m, boolean require_deoptimizable);
// Transforms the class
private static native void doCommonClassRedefinition(Class<?> target,
byte[] classfile,
byte[] dexfile);
}
|