xref: /aosp_15_r20/art/test/1916-get-set-current-frame/src/art/Test1916.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
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.lang.reflect.Constructor;
20*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Executable;
21*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method;
22*795d594fSAndroid Build Coastguard Worker import java.nio.ByteBuffer;
23*795d594fSAndroid Build Coastguard Worker import java.util.concurrent.Semaphore;
24*795d594fSAndroid Build Coastguard Worker import java.util.Arrays;
25*795d594fSAndroid Build Coastguard Worker import java.util.Collection;
26*795d594fSAndroid Build Coastguard Worker import java.util.List;
27*795d594fSAndroid Build Coastguard Worker import java.util.Set;
28*795d594fSAndroid Build Coastguard Worker import java.util.function.Function;
29*795d594fSAndroid Build Coastguard Worker import java.util.function.Predicate;
30*795d594fSAndroid Build Coastguard Worker import java.util.function.Supplier;
31*795d594fSAndroid Build Coastguard Worker import java.util.function.Consumer;
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker public class Test1916 {
34*795d594fSAndroid Build Coastguard Worker     public static final int SET_VALUE = 1337;
35*795d594fSAndroid Build Coastguard Worker     public static final String TARGET_VAR = "TARGET";
36*795d594fSAndroid Build Coastguard Worker 
reportValue(Object val)37*795d594fSAndroid Build Coastguard Worker     public static void reportValue(Object val) {
38*795d594fSAndroid Build Coastguard Worker         System.out.println("\tValue is '" + val + "'");
39*795d594fSAndroid Build Coastguard Worker     }
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker     public static class IntRunner implements Runnable {
42*795d594fSAndroid Build Coastguard Worker         private volatile boolean continueBusyLoop;
43*795d594fSAndroid Build Coastguard Worker         private volatile boolean inBusyLoop;
IntRunner()44*795d594fSAndroid Build Coastguard Worker         public IntRunner() {
45*795d594fSAndroid Build Coastguard Worker             this.continueBusyLoop = true;
46*795d594fSAndroid Build Coastguard Worker             this.inBusyLoop = false;
47*795d594fSAndroid Build Coastguard Worker         }
run()48*795d594fSAndroid Build Coastguard Worker         public void run() {
49*795d594fSAndroid Build Coastguard Worker             int TARGET = 42;
50*795d594fSAndroid Build Coastguard Worker             // We will suspend the thread during this loop.
51*795d594fSAndroid Build Coastguard Worker             while (continueBusyLoop) {
52*795d594fSAndroid Build Coastguard Worker                 inBusyLoop = true;
53*795d594fSAndroid Build Coastguard Worker             }
54*795d594fSAndroid Build Coastguard Worker             reportValue(TARGET);
55*795d594fSAndroid Build Coastguard Worker         }
waitForBusyLoopStart()56*795d594fSAndroid Build Coastguard Worker         public void waitForBusyLoopStart() { while (!inBusyLoop) {} }
finish()57*795d594fSAndroid Build Coastguard Worker         public void finish() { continueBusyLoop = false; }
58*795d594fSAndroid Build Coastguard Worker     }
59*795d594fSAndroid Build Coastguard Worker 
run()60*795d594fSAndroid Build Coastguard Worker     public static void run() throws Exception {
61*795d594fSAndroid Build Coastguard Worker         Locals.EnableLocalVariableAccess();
62*795d594fSAndroid Build Coastguard Worker         runGet();
63*795d594fSAndroid Build Coastguard Worker         runSet();
64*795d594fSAndroid Build Coastguard Worker     }
65*795d594fSAndroid Build Coastguard Worker 
runGet()66*795d594fSAndroid Build Coastguard Worker     public static void runGet() throws Exception {
67*795d594fSAndroid Build Coastguard Worker         Method target = IntRunner.class.getDeclaredMethod("run");
68*795d594fSAndroid Build Coastguard Worker         // Get Int
69*795d594fSAndroid Build Coastguard Worker         IntRunner int_runner = new IntRunner();
70*795d594fSAndroid Build Coastguard Worker         Thread target_get = new Thread(int_runner, "GetLocalInt - Target");
71*795d594fSAndroid Build Coastguard Worker         target_get.start();
72*795d594fSAndroid Build Coastguard Worker         int_runner.waitForBusyLoopStart();
73*795d594fSAndroid Build Coastguard Worker         try {
74*795d594fSAndroid Build Coastguard Worker             Suspension.suspend(target_get);
75*795d594fSAndroid Build Coastguard Worker         } catch (Exception e) {
76*795d594fSAndroid Build Coastguard Worker             System.out.println("FAIL: got " + e);
77*795d594fSAndroid Build Coastguard Worker             e.printStackTrace();
78*795d594fSAndroid Build Coastguard Worker             int_runner.finish();
79*795d594fSAndroid Build Coastguard Worker             target_get.join();
80*795d594fSAndroid Build Coastguard Worker             return;
81*795d594fSAndroid Build Coastguard Worker         }
82*795d594fSAndroid Build Coastguard Worker         try {
83*795d594fSAndroid Build Coastguard Worker             StackTrace.StackFrameData frame = FindStackFrame(target_get, target);
84*795d594fSAndroid Build Coastguard Worker             int depth = frame.depth;
85*795d594fSAndroid Build Coastguard Worker             if (depth != 0) { throw new Error("Expected depth 0 but got " + depth); }
86*795d594fSAndroid Build Coastguard Worker             int slot = FindSlot(frame);
87*795d594fSAndroid Build Coastguard Worker             int value = Locals.GetLocalVariableInt(target_get, depth, slot);
88*795d594fSAndroid Build Coastguard Worker             System.out.println("From GetLocalInt(), value is " + value);
89*795d594fSAndroid Build Coastguard Worker         } finally {
90*795d594fSAndroid Build Coastguard Worker             Suspension.resume(target_get);
91*795d594fSAndroid Build Coastguard Worker             int_runner.finish();
92*795d594fSAndroid Build Coastguard Worker             target_get.join();
93*795d594fSAndroid Build Coastguard Worker         }
94*795d594fSAndroid Build Coastguard Worker     }
95*795d594fSAndroid Build Coastguard Worker 
runSet()96*795d594fSAndroid Build Coastguard Worker     public static void runSet() throws Exception {
97*795d594fSAndroid Build Coastguard Worker         Method target = IntRunner.class.getDeclaredMethod("run");
98*795d594fSAndroid Build Coastguard Worker         // Set Int
99*795d594fSAndroid Build Coastguard Worker         IntRunner int_runner = new IntRunner();
100*795d594fSAndroid Build Coastguard Worker         Thread target_set = new Thread(int_runner, "SetLocalInt - Target");
101*795d594fSAndroid Build Coastguard Worker         target_set.start();
102*795d594fSAndroid Build Coastguard Worker         int_runner.waitForBusyLoopStart();
103*795d594fSAndroid Build Coastguard Worker         try {
104*795d594fSAndroid Build Coastguard Worker             Suspension.suspend(target_set);
105*795d594fSAndroid Build Coastguard Worker         } catch (Exception e) {
106*795d594fSAndroid Build Coastguard Worker             System.out.println("FAIL: got " + e);
107*795d594fSAndroid Build Coastguard Worker             e.printStackTrace();
108*795d594fSAndroid Build Coastguard Worker             int_runner.finish();
109*795d594fSAndroid Build Coastguard Worker             target_set.join();
110*795d594fSAndroid Build Coastguard Worker             return;
111*795d594fSAndroid Build Coastguard Worker         }
112*795d594fSAndroid Build Coastguard Worker         try {
113*795d594fSAndroid Build Coastguard Worker             StackTrace.StackFrameData frame = FindStackFrame(target_set, target);
114*795d594fSAndroid Build Coastguard Worker             int depth = frame.depth;
115*795d594fSAndroid Build Coastguard Worker             if (depth != 0) { throw new Error("Expected depth 0 but got " + depth); }
116*795d594fSAndroid Build Coastguard Worker             int slot = FindSlot(frame);
117*795d594fSAndroid Build Coastguard Worker             System.out.println("Setting TARGET to " + SET_VALUE);
118*795d594fSAndroid Build Coastguard Worker             Locals.SetLocalVariableInt(target_set, depth, slot, SET_VALUE);
119*795d594fSAndroid Build Coastguard Worker         } finally {
120*795d594fSAndroid Build Coastguard Worker             Suspension.resume(target_set);
121*795d594fSAndroid Build Coastguard Worker             int_runner.finish();
122*795d594fSAndroid Build Coastguard Worker             target_set.join();
123*795d594fSAndroid Build Coastguard Worker         }
124*795d594fSAndroid Build Coastguard Worker     }
125*795d594fSAndroid Build Coastguard Worker 
FindSlot(StackTrace.StackFrameData frame)126*795d594fSAndroid Build Coastguard Worker     public static int FindSlot(StackTrace.StackFrameData frame) throws Exception {
127*795d594fSAndroid Build Coastguard Worker         long loc = frame.current_location;
128*795d594fSAndroid Build Coastguard Worker         for (Locals.VariableDescription var : Locals.GetLocalVariableTable(frame.method)) {
129*795d594fSAndroid Build Coastguard Worker             if (var.start_location <= loc &&
130*795d594fSAndroid Build Coastguard Worker                     var.length + var.start_location > loc &&
131*795d594fSAndroid Build Coastguard Worker                     var.name.equals(TARGET_VAR)) {
132*795d594fSAndroid Build Coastguard Worker                 return var.slot;
133*795d594fSAndroid Build Coastguard Worker             }
134*795d594fSAndroid Build Coastguard Worker         }
135*795d594fSAndroid Build Coastguard Worker         throw new Error(
136*795d594fSAndroid Build Coastguard Worker                 "Unable to find variable " + TARGET_VAR + " in " + frame.method + " at loc " + loc);
137*795d594fSAndroid Build Coastguard Worker     }
138*795d594fSAndroid Build Coastguard Worker 
FindStackFrame(Thread thr, Method target)139*795d594fSAndroid Build Coastguard Worker     private static StackTrace.StackFrameData FindStackFrame(Thread thr, Method target) {
140*795d594fSAndroid Build Coastguard Worker         for (StackTrace.StackFrameData frame : StackTrace.GetStackTrace(thr)) {
141*795d594fSAndroid Build Coastguard Worker             if (frame.method.equals(target)) {
142*795d594fSAndroid Build Coastguard Worker                 return frame;
143*795d594fSAndroid Build Coastguard Worker             }
144*795d594fSAndroid Build Coastguard Worker         }
145*795d594fSAndroid Build Coastguard Worker         throw new Error("Unable to find stack frame in method " + target + " on thread " + thr);
146*795d594fSAndroid Build Coastguard Worker     }
147*795d594fSAndroid Build Coastguard Worker }
148*795d594fSAndroid Build Coastguard Worker 
149