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 public class Main {
18   static volatile boolean done = false;
19 
main(String[] args)20   public static void main(String[] args) {
21     // Run a thread for 30 seconds, allocating memory and triggering garbage
22     // collection.
23     // Time is limited to 30 seconds to not make this test too long. The test used
24     // to trigger the failure around 1 every 10 runs.
25     Thread t = new Thread() {
26       public void run() {
27         long time = System.currentTimeMillis();
28         while (System.currentTimeMillis() - time < 30000) {
29           for (int j = 0; j < 10000; j++) {
30             o = new Object[1000];
31           }
32           Runtime.getRuntime().gc();
33           Thread.yield();
34         }
35         Main.done = true;
36       }
37       Object o;
38     };
39     // Make the thread a daemon to quit early in case of an
40     // exception thrown below.
41     t.setDaemon(true);
42     t.start();
43 
44     // Run 'foo' as long as the test runs.
45     while (!done) {
46       double res = foo(staticMain);
47       if (res != 529.0) {
48         throw new Error("Unexpected result " + res);
49       }
50     }
51   }
52 
foo(Main main)53   public static double foo(Main main) {
54     // Use up all D registers on arm64.
55     double d1 = main.field1;
56     double d2 = main.field2;
57     double d3 = main.field3;
58     double d4 = main.field4;
59     double d5 = main.field5;
60     double d6 = main.field6;
61     double d7 = main.field7;
62     double d8 = main.field8;
63     double d9 = main.field9;
64     double d10 = main.field10;
65     double d11 = main.field11;
66     double d12 = main.field12;
67     double d13 = main.field13;
68     double d14 = main.field14;
69     double d15 = main.field15;
70     double d16 = main.field16;
71     double d17 = main.field17;
72     double d18 = main.field18;
73     double d19 = main.field19;
74     double d20 = main.field20;
75     double d21 = main.field21;
76     double d22 = main.field22;
77     double d23 = main.field23;
78     double d24 = main.field24;
79     double d25 = main.field25;
80     double d26 = main.field26;
81     double d27 = main.field27;
82     double d28 = main.field28;
83     double d29 = main.field29;
84     double d30 = main.field30;
85     double d31 = main.field31;
86     double d32 = main.field32;
87 
88     // Trigger a read barrier. This used to make the test trip on ARM64 as
89     // the read barrier stub used to not restore the D registers.
90     double p = main.objectField.field1;
91 
92     return p + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + d10 + d11 + d12 +
93         d13 + d14 + d15 + d16 + d17 + d18 + d19 + d20 + d21 + d22 + d23 + d24 +
94         d25 + d26 + d27 + d28 + d29 + d30 + d31 + d32;
95   }
96 
97   // Initialize objects here and not in 'main' to avoid having
98   // these objects in roots.
99   public static Main staticMain = new Main();
100   static {
101     staticMain.objectField = new Main();
102   }
103 
104   public Main objectField;
105 
106   public double field1 = 1.0;
107   public double field2 = 2.0;
108   public double field3 = 3.0;
109   public double field4 = 4.0;
110   public double field5 = 5.0;
111   public double field6 = 6.0;
112   public double field7 = 7.0;
113   public double field8 = 8.0;
114   public double field9 = 9.0;
115   public double field10 = 10.0;
116   public double field11 = 11.0;
117   public double field12 = 12.0;
118   public double field13 = 13.0;
119   public double field14 = 14.0;
120   public double field15 = 15.0;
121   public double field16 = 16.0;
122   public double field17 = 17.0;
123   public double field18 = 18.0;
124   public double field19 = 19.0;
125   public double field20 = 20.0;
126   public double field21 = 21.0;
127   public double field22 = 22.0;
128   public double field23 = 23.0;
129   public double field24 = 24.0;
130   public double field25 = 25.0;
131   public double field26 = 26.0;
132   public double field27 = 27.0;
133   public double field28 = 28.0;
134   public double field29 = 29.0;
135   public double field30 = 30.0;
136   public double field31 = 31.0;
137   public double field32 = 32.0;
138 }
139