• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.lang.reflect.Constructor;
20 import java.lang.reflect.Executable;
21 import java.lang.reflect.Method;
22 import java.nio.ByteBuffer;
23 import java.util.concurrent.Semaphore;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.function.Function;
29 import java.util.function.Predicate;
30 import java.util.function.Supplier;
31 import java.util.function.Consumer;
32 
33 // TODO Rename test to set-get-local-prim
34 
35 public class Test1912 {
36   public static final String TARGET_VAR = "TARGET";
37 
38 
reportValue(Object val)39   public static void reportValue(Object val) {
40     if (val instanceof Character) {
41       val = "<Char: " + Character.getNumericValue(((Character)val).charValue()) + ">";
42     }
43     System.out.println("\tValue is '" + val + "' (class: " + val.getClass() + ")");
44   }
45 
BooleanMethod(Runnable safepoint)46   public static void BooleanMethod(Runnable safepoint) {
47     boolean TARGET = false;
48     safepoint.run();
49     reportValue(TARGET);
50   }
ByteMethod(Runnable safepoint)51   public static void ByteMethod(Runnable safepoint) {
52     byte TARGET = 8;
53     safepoint.run();
54     reportValue(TARGET);
55   }
CharMethod(Runnable safepoint)56   public static void CharMethod(Runnable safepoint) {
57     char TARGET = 'q';
58     safepoint.run();
59     reportValue(TARGET);
60   }
ShortMethod(Runnable safepoint)61   public static void ShortMethod(Runnable safepoint) {
62     short TARGET = 321;
63     safepoint.run();
64     reportValue(TARGET);
65   }
IntMethod(Runnable safepoint)66   public static void IntMethod(Runnable safepoint) {
67     int TARGET = 42;
68     safepoint.run();
69     reportValue(TARGET);
70   }
LongMethod(Runnable safepoint)71   public static void LongMethod(Runnable safepoint) {
72     long TARGET = 9001;
73     safepoint.run();
74     reportValue(TARGET);
75   }
FloatMethod(Runnable safepoint)76   public static void FloatMethod(Runnable safepoint) {
77     float TARGET = 1.618f;
78     safepoint.run();
79     reportValue(TARGET);
80   }
DoubleMethod(Runnable safepoint)81   public static void DoubleMethod(Runnable safepoint) {
82     double TARGET = 3.1415d;
83     safepoint.run();
84     reportValue(TARGET);
85   }
86 
87   public static interface SafepointFunction {
invoke( Thread thread, Method target, Locals.VariableDescription TARGET_desc, int depth)88     public void invoke(
89         Thread thread,
90         Method target,
91         Locals.VariableDescription TARGET_desc,
92         int depth) throws Exception;
93   }
94 
95   public static interface SetterFunction {
SetVar(Thread t, int depth, int slot, Object v)96     public void SetVar(Thread t, int depth, int slot, Object v);
97   }
98 
99   public static interface GetterFunction {
GetVar(Thread t, int depth, int slot)100     public Object GetVar(Thread t, int depth, int slot);
101   }
102 
NamedSet( final String type, final SetterFunction get, final Object v)103   public static SafepointFunction NamedSet(
104       final String type, final SetterFunction get, final Object v) {
105     return new SafepointFunction() {
106       public void invoke(Thread t, Method method, Locals.VariableDescription desc, int depth) {
107         try {
108           get.SetVar(t, depth, desc.slot, v);
109           System.out.println(this + " on " + method + " set value: " + v);
110         } catch (Exception e) {
111           System.out.println(
112               this + " on " + method + " failed to set value " + v + " due to " + e.getMessage());
113         }
114       }
115       public String toString() {
116         return "\"Set" + type + "\"";
117       }
118     };
119   }
120 
121   public static SafepointFunction NamedGet(final String type, final GetterFunction get) {
122     return new SafepointFunction() {
123       public void invoke(Thread t, Method method, Locals.VariableDescription desc, int depth) {
124         try {
125           Object res = get.GetVar(t, depth, desc.slot);
126           System.out.println(this + " on " + method + " got value: " + res);
127         } catch (Exception e) {
128           System.out.println(this + " on " + method + " failed due to " + e.getMessage());
129         }
130       }
131       public String toString() {
132         return "\"Get" + type + "\"";
133       }
134     };
135   }
136 
137   public static class TestCase {
138     public final Method target;
139 
140     public TestCase(Method target) {
141       this.target = target;
142     }
143 
144     public static class ThreadPauser implements Runnable {
145       public final Semaphore sem_wakeup_main;
146       public final Semaphore sem_wait;
147 
148       public ThreadPauser() {
149         sem_wakeup_main = new Semaphore(0);
150         sem_wait = new Semaphore(0);
151       }
152 
153       public void run() {
154         try {
155           sem_wakeup_main.release();
156           sem_wait.acquire();
157         } catch (Exception e) {
158           throw new Error("Error with semaphores!", e);
159         }
160       }
161 
162       public void waitForOtherThreadToPause() throws Exception {
163         sem_wakeup_main.acquire();
164       }
165 
166       public void wakeupOtherThread() throws Exception {
167         sem_wait.release();
168       }
169     }
170 
171     public void exec(final SafepointFunction safepoint) throws Exception {
172       System.out.println("Running " + target + " with " + safepoint + " on remote thread.");
173       final ThreadPauser pause = new ThreadPauser();
174       Thread remote = new Thread(
175           () -> {
176             try {
177               target.invoke(null, pause);
178             } catch (Exception e) {
179               throw new Error("Error invoking remote thread " + Thread.currentThread(), e);
180             }
181           },
182           "remote thread for " + target + " with " + safepoint);
183       remote.start();
184       pause.waitForOtherThreadToPause();
185       try {
186         Suspension.suspend(remote);
187         StackTrace.StackFrameData frame = findStackFrame(remote);
188         Locals.VariableDescription desc = findTargetVar(frame.current_location);
189         safepoint.invoke(remote, target, desc, frame.depth);
190       } finally {
191         Suspension.resume(remote);
192         pause.wakeupOtherThread();
193         remote.join();
194       }
195     }
196 
197     private Locals.VariableDescription findTargetVar(long loc) {
198       for (Locals.VariableDescription var : Locals.GetLocalVariableTable(target)) {
199         if (var.start_location <= loc &&
200             var.length + var.start_location > loc &&
201             var.name.equals(TARGET_VAR)) {
202           return var;
203         }
204       }
205       throw new Error(
206           "Unable to find variable " + TARGET_VAR + " in " + target + " at loc " + loc);
207     }
208 
209     private StackTrace.StackFrameData findStackFrame(Thread thr) {
210       for (StackTrace.StackFrameData frame : StackTrace.GetStackTrace(thr)) {
211         if (frame.method.equals(target)) {
212           return frame;
213         }
214       }
215       throw new Error("Unable to find stack frame in method " + target + " on thread " + thr);
216     }
217   }
218   public static Method getMethod(String name) throws Exception {
219     return Test1912.class.getDeclaredMethod(name, Runnable.class);
220   }
221 
222   public static void run() throws Exception {
223     Locals.EnableLocalVariableAccess();
224     final TestCase[] MAIN_TEST_CASES = new TestCase[] {
225       new TestCase(getMethod("IntMethod")),
226       new TestCase(getMethod("LongMethod")),
227       new TestCase(getMethod("FloatMethod")),
228       new TestCase(getMethod("DoubleMethod")),
229     };
230 
231     final SafepointFunction[] SAFEPOINTS = new SafepointFunction[] {
232       NamedGet("Int", Locals::GetLocalVariableInt),
233       NamedGet("Long", Locals::GetLocalVariableLong),
234       NamedGet("Float", Locals::GetLocalVariableFloat),
235       NamedGet("Double", Locals::GetLocalVariableDouble),
236       NamedSet("Int", Locals::SetLocalVariableInt, Integer.MAX_VALUE),
237       NamedSet("Long", Locals::SetLocalVariableLong, Long.MAX_VALUE),
238       NamedSet("Float", Locals::SetLocalVariableFloat, 9.2f),
239       NamedSet("Double", Locals::SetLocalVariableDouble, 12.4d),
240     };
241 
242     for (TestCase t: MAIN_TEST_CASES) {
243       for (SafepointFunction s : SAFEPOINTS) {
244         t.exec(s);
245       }
246     }
247 
248     // Test int for small values.
249     new TestCase(getMethod("BooleanMethod")).exec(
250         NamedSet("IntBoolSize", Locals::SetLocalVariableInt, 1));
251     new TestCase(getMethod("ByteMethod")).exec(
252       NamedSet("IntByteSize", Locals::SetLocalVariableInt, Byte.MAX_VALUE - 1));
253 
254     new TestCase(getMethod("CharMethod")).exec(
255       NamedSet("IntCharSize", Locals::SetLocalVariableInt, Character.MAX_VALUE - 1));
256     new TestCase(getMethod("ShortMethod")).exec(
257       NamedSet("IntShortSize", Locals::SetLocalVariableInt, Short.MAX_VALUE - 1));
258   }
259 }
260 
261