lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
/*
 * Copyright (C) 2017 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.
 */
 
public class Main {
  public static void main(String[] args) throws Exception {
    System.loadLibrary(args[0]);
 
    $noinline$intUpdate(new Main());
    ensureJitCompiled(Main.class, "$noinline$intUpdate");
    $noinline$intUpdate(new SubMain());
    if (myIntStatic != 5000) {
      throw new Error("Expected 5000, got " + myIntStatic);
    }
 
    $noinline$objectUpdate(new Main());
    ensureJitCompiled(Main.class, "$noinline$objectUpdate");
    $noinline$objectUpdate(new SubMain());
 
    $noinline$loopIncrement(new Main());
    ensureJitCompiled(Main.class, "$noinline$loopIncrement");
    $noinline$loopIncrement(new SubMain());
 
    $noinline$objectReturned(new Main());
    ensureJitCompiled(Main.class, "$noinline$objectReturned");
    Object o = $noinline$objectReturned(new SubMain());
    // We used to get 0xebadde09 in 'o' here and therefore crash
    // both interpreter and compiled code.
    if (o instanceof Cloneable) {
      System.out.println("Unexpected object type " + o.getClass());
    }
  }
 
  public boolean doCheck() {
    return false;
  }
 
  public static void $noinline$intUpdate(Main m) {
    int a = 0;
    // We used to kill 'a' when the inline cache of 'doCheck' only
    // contains 'Main' (which makes the only branch using 'a' dead).
    // So the deoptimization at the inline cache was incorrectly assuming
    // 'a' was dead.
    for (int i = 0; i < 5000; i++) {
      if (m.doCheck()) {
        a++;
        // We make this branch the only true user of the 'a' phi. All other uses
        // of 'a' are phi updates.
        myIntStatic = a;
      } else if (myIntStatic == 42) {
        a = 1;
      }
    }
  }
 
  public static void $noinline$objectUpdate(Main m) {
    Object o = new Object();
    // We used to kill 'o' when the inline cache of 'doCheck' only
    // contains 'Main' (which makes the only branch using 'o' dead).
    // So the deoptimization at the inline cache was incorrectly assuming
    // 'o' was dead.
    // This lead to a NPE on the 'toString' call just after deoptimizing.
    for (int i = 0; i < 5000; i++) {
      if (m.doCheck()) {
        // We make this branch the only true user of the 'o' phi. All other uses
        // of 'o' are phi updates.
        o.toString();
      } else if (myIntStatic == 42) {
        o = m;
      }
    }
  }
 
  public static void $noinline$loopIncrement(Main m) {
    int k = 0;
    // We used to kill 'k' and replace it with 5000 when the inline cache
    // of 'doCheck' only contains 'Main'.
    // So the deoptimization at the inline cache was incorrectly assuming
    // 'k' was 5000.
    for (int i = 0; i < 5000; i++, k++) {
      if (m.doCheck()) {
        // We make this branch the only true user of the 'k' phi. All other uses
        // of 'k' are phi updates.
        myIntStatic = k;
      }
    }
    if (k != 5000) {
      throw new Error("Expected 5000, got " + k);
    }
  }
 
  public static Object $noinline$objectReturned(Main m) {
    Object o = new Object();
    // We used to kill 'o' when the inline cache of 'doCheck' only
    // contains 'Main' (which makes the only branch using 'o' dead).
    // So the deoptimization at the inline cache was incorrectly assuming
    // 'o' was dead.
    // We also need to make 'o' escape through a return instruction, as mterp
    // executes the same code for return and return-object, and the 0xebadde09
    // sentinel for dead value is only pushed to non-object dex registers.
    Object myReturnValue = null;
    for (int i = 0; i < 5000; i++) {
      if (m.doCheck()) {
        // We make this branch the only true user of the 'o' phi. All other uses
        // of 'o' are phi updates.
        myReturnValue = o;
      } else if (myIntStatic == 42) {
        o = m;
      }
    }
    return myReturnValue;
  }
 
  public static int myIntStatic = 0;
 
  public static native void ensureJitCompiled(Class<?> itf, String name);
}
 
class SubMain extends Main {
  public boolean doCheck() {
    return true;
  }
}