1 /*
2  * Copyright (C) 2018 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 import java.util.HashMap;
18 
19 public class Main {
main(String[] args)20   public static void main(String[] args) {
21     System.loadLibrary(args[0]);
22 
23     // Jit compile HashMap.hash method, so that instrumentation stubs
24     // will deoptimize it.
25     ensureJitCompiled(HashMap.class, "hash");
26 
27     Main key = new Main();
28     Integer value = new Integer(10);
29     HashMap<Main, Integer> map = new HashMap<>();
30     map.put(key, value);
31     Integer res = map.get(key);
32     if (!value.equals(res)) {
33       throw new Error("Expected 10, got " + res);
34     }
35   }
36 
hashCode()37   public int hashCode() {
38     // The call stack at this point is:
39     // Main.main
40     //  HashMap.put
41     //    HashMap.hash
42     //      Main.hashCode
43     //
44     // The opcode at HashMap.hash is invoke-virtual-quick which the
45     // instrumentation code did not expect and used to fetch the wrong
46     // method index for it.
47     deoptimizeAll();
48     return 42;
49   }
50 
deoptimizeAll()51   public static native void deoptimizeAll();
ensureJitCompiled(Class<?> cls, String methodName)52   public static native void ensureJitCompiled(Class<?> cls, String methodName);
53 }
54