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.lang.reflect.InvocationHandler;
18 import java.lang.reflect.Method;
19 import java.lang.reflect.Proxy;
20 import java.util.ArrayList;
21 
22 class OOMEHelper {
23     int nullField;
24 }
25 
26 /**
27  * Test that null field access under an OOME situation works.
28  *
29  * The test relies on compile-time verification. This class is compile-time verifiable, so when
30  * loaded at runtime will not transitively load referenced types eagerly. In that case, our code
31  * to give descriptive NullPointerExceptions for the field access to the null "instance" of
32  * OOMEHelper in nullAccess() will be the first attempting to load the class, and, under the
33  * induced low-memory situation, will throw itself an OutOfMemoryError.
34  */
35 public class OOMEOnNullAccess {
36 
37     static ArrayList<Object> storage = new ArrayList<>(100000);
38 
exhaustJavaHeap(int size)39     private static void exhaustJavaHeap(int size) {
40       Runtime.getRuntime().gc();
41       while (size > 0) {
42         try {
43           storage.add(new byte[size]);
44         } catch (OutOfMemoryError e) {
45           size = size/2;
46         }
47       }
48     }
49 
main(String[] args)50     public static void main(String[] args) {
51         // Stop the JIT to be sure nothing is running that could be resolving classes or causing
52         // verification.
53         Main.stopJit();
54         Main.waitForCompilation();
55 
56         // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw
57         // OOME to prevent GC thrashing, even if later allocations may succeed.
58         Runtime.getRuntime().gc();
59         System.runFinalization();
60         // NOTE: There is a GC invocation in the exhaustJavaHeap(). So we don't need one here.
61 
62         int initial_size = 1024 * 1024;
63         // Repeat to ensure there is no space left on the heap.
64         exhaustJavaHeap(initial_size);
65         exhaustJavaHeap(/*size*/ 4);
66         exhaustJavaHeap(/*size*/ 4);
67 
68 
69         try {
70             nullAccess(null);
71             storage.clear();
72             throw new RuntimeException("Did not receive exception!");
73         } catch (OutOfMemoryError oome) {
74             storage.clear();
75             System.err.println("Received OOME");
76         } finally {
77             // Even if it's an unexpected error, clear so that we can print things later.
78             storage.clear();
79         }
80 
81         Main.startJit();
82     }
83 
nullAccess(OOMEHelper nullInstance)84     public static int nullAccess(OOMEHelper nullInstance) {
85         // Under AOT, this access is the first one to actually load the OOMEHelper class, so
86         // we can pretty print the name and such.
87         return nullInstance.nullField;
88     }
89 }
90