| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import art.Locals; |
| 18 |
import art.StackTrace; |
| 19 |
import art.Suspension; |
| 20 |
import java.lang.reflect.Constructor; |
| 21 |
import java.lang.reflect.Executable; |
| 22 |
import java.lang.reflect.Method; |
| 23 |
import java.nio.ByteBuffer; |
| 24 |
import java.time.Instant; |
| 25 |
import java.util.concurrent.Semaphore; |
| 26 |
import java.util.Arrays; |
| 27 |
import java.util.Collection; |
| 28 |
import java.util.List; |
| 29 |
import java.util.Set; |
| 30 |
import java.util.function.Function; |
| 31 |
import java.util.function.Predicate; |
| 32 |
import java.util.function.Supplier; |
| 33 |
import java.util.function.Consumer; |
| 34 |
|
| 35 |
public class Main { |
| 36 |
public static final int SET_VALUE = 1337; |
| 37 |
public static final String TARGET_VAR = "TARGET"; |
| 38 |
|
| 39 |
public static void main(String[] args) throws Exception { |
| 40 |
System.loadLibrary(args[0]); |
| 41 |
Locals.EnableLocalVariableAccess(); |
| 42 |
runGet(); |
| 43 |
runSet(); |
| 44 |
} |
| 45 |
|
| 46 |
public static void reportValue(Object val) { |
| 47 |
System.out.println("\tValue is '" + val + "'"); |
| 48 |
} |
| 49 |
|
| 50 |
public static class IntRunner implements Runnable { |
| 51 |
private volatile boolean continueBusyLoop; |
| 52 |
private volatile boolean inBusyLoop; |
| 53 |
private final boolean expectOsr; |
| 54 |
public IntRunner(boolean expectOsr) { |
| 55 |
this.continueBusyLoop = true; |
| 56 |
this.inBusyLoop = false; |
| 57 |
this.expectOsr = expectOsr; |
| 58 |
} |
| 59 |
public void run() { |
| 60 |
int TARGET = 42; |
| 61 |
boolean normalJit = hasJit() && getJitThreshold() != 0; |
| 62 |
if (normalJit && expectOsr && !Main.isInterpreted()) { |
| 63 |
System.out.println("Unexpectedly in jit code prior to restarting the JIT!"); |
| 64 |
} |
| 65 |
startJit(); |
| 66 |
|
| 67 |
while (continueBusyLoop) { |
| 68 |
inBusyLoop = true; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
Instant osrDeadline = Instant.now().plusSeconds(expectOsr ? 600 : 3); |
| 73 |
do { |
| 74 |
|
| 75 |
inBusyLoop = true; |
| 76 |
} while (normalJit && !Main.isInOsrCode("run") && osrDeadline.compareTo(Instant.now()) > 0); |
| 77 |
|
| 78 |
|
| 79 |
if (normalJit) { |
| 80 |
boolean inOsr = Main.isInOsrCode("run"); |
| 81 |
if (expectOsr && !inOsr) { |
| 82 |
throw new Error( |
| 83 |
"Expected to be in OSR but was not. interpreter: " + Main.isInterpreted()); |
| 84 |
} else if (!expectOsr && inOsr) { |
| 85 |
throw new Error( |
| 86 |
"Expected not to be in OSR but was. interpreter: " + Main.isInterpreted()); |
| 87 |
} |
| 88 |
} |
| 89 |
reportValue(TARGET); |
| 90 |
} |
| 91 |
public void waitForBusyLoopStart() { while (!inBusyLoop) {} } |
| 92 |
public void finish() { |
| 93 |
continueBusyLoop = false; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public static void runGet() throws Exception { |
| 98 |
Method target = IntRunner.class.getDeclaredMethod("run"); |
| 99 |
|
| 100 |
stopJit(); |
| 101 |
|
| 102 |
IntRunner int_runner = new IntRunner(true); |
| 103 |
Thread target_get = new Thread(int_runner, "GetLocalInt - Target"); |
| 104 |
target_get.start(); |
| 105 |
int_runner.waitForBusyLoopStart(); |
| 106 |
try { |
| 107 |
Suspension.suspend(target_get); |
| 108 |
} catch (Exception e) { |
| 109 |
System.out.println("FAIL: got " + e); |
| 110 |
e.printStackTrace(); |
| 111 |
int_runner.finish(); |
| 112 |
target_get.join(); |
| 113 |
return; |
| 114 |
} |
| 115 |
try { |
| 116 |
StackTrace.StackFrameData frame = FindStackFrame(target_get, target); |
| 117 |
int depth = frame.depth; |
| 118 |
if (depth != 0) { throw new Error("Expected depth 0 but got " + depth); } |
| 119 |
int slot = FindSlot(frame); |
| 120 |
int value = Locals.GetLocalVariableInt(target_get, depth, slot); |
| 121 |
System.out.println("From GetLocalInt(), value is " + value); |
| 122 |
} finally { |
| 123 |
Suspension.resume(target_get); |
| 124 |
int_runner.finish(); |
| 125 |
target_get.join(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
public static void runSet() throws Exception { |
| 130 |
Method target = IntRunner.class.getDeclaredMethod("run"); |
| 131 |
|
| 132 |
stopJit(); |
| 133 |
|
| 134 |
IntRunner int_runner = new IntRunner(false); |
| 135 |
Thread target_set = new Thread(int_runner, "SetLocalInt - Target"); |
| 136 |
target_set.start(); |
| 137 |
int_runner.waitForBusyLoopStart(); |
| 138 |
try { |
| 139 |
Suspension.suspend(target_set); |
| 140 |
} catch (Exception e) { |
| 141 |
System.out.println("FAIL: got " + e); |
| 142 |
e.printStackTrace(); |
| 143 |
int_runner.finish(); |
| 144 |
target_set.join(); |
| 145 |
return; |
| 146 |
} |
| 147 |
try { |
| 148 |
StackTrace.StackFrameData frame = FindStackFrame(target_set, target); |
| 149 |
int depth = frame.depth; |
| 150 |
if (depth != 0) { throw new Error("Expected depth 0 but got " + depth); } |
| 151 |
int slot = FindSlot(frame); |
| 152 |
System.out.println("Setting TARGET to " + SET_VALUE); |
| 153 |
Locals.SetLocalVariableInt(target_set, depth, slot, SET_VALUE); |
| 154 |
} finally { |
| 155 |
Suspension.resume(target_set); |
| 156 |
int_runner.finish(); |
| 157 |
target_set.join(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public static int FindSlot(StackTrace.StackFrameData frame) throws Exception { |
| 162 |
long loc = frame.current_location; |
| 163 |
for (Locals.VariableDescription var : Locals.GetLocalVariableTable(frame.method)) { |
| 164 |
if (var.start_location <= loc && |
| 165 |
var.length + var.start_location > loc && |
| 166 |
var.name.equals(TARGET_VAR)) { |
| 167 |
return var.slot; |
| 168 |
} |
| 169 |
} |
| 170 |
throw new Error( |
| 171 |
"Unable to find variable " + TARGET_VAR + " in " + frame.method + " at loc " + loc); |
| 172 |
} |
| 173 |
|
| 174 |
private static StackTrace.StackFrameData FindStackFrame(Thread thr, Method target) { |
| 175 |
for (StackTrace.StackFrameData frame : StackTrace.GetStackTrace(thr)) { |
| 176 |
if (frame.method.equals(target)) { |
| 177 |
return frame; |
| 178 |
} |
| 179 |
} |
| 180 |
throw new Error("Unable to find stack frame in method " + target + " on thread " + thr); |
| 181 |
} |
| 182 |
|
| 183 |
public static native boolean isInterpreted(); |
| 184 |
public static native boolean isInOsrCode(String methodName); |
| 185 |
public static native boolean stopJit(); |
| 186 |
public static native boolean startJit(); |
| 187 |
public static native boolean hasJit(); |
| 188 |
public static native int getJitThreshold(); |
| 189 |
} |