1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.InvocationHandler; 18*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method; 19*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Proxy; 20*795d594fSAndroid Build Coastguard Worker import java.util.ArrayList; 21*795d594fSAndroid Build Coastguard Worker 22*795d594fSAndroid Build Coastguard Worker class OOMEHelper { 23*795d594fSAndroid Build Coastguard Worker int nullField; 24*795d594fSAndroid Build Coastguard Worker } 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard Worker /** 27*795d594fSAndroid Build Coastguard Worker * Test that null field access under an OOME situation works. 28*795d594fSAndroid Build Coastguard Worker * 29*795d594fSAndroid Build Coastguard Worker * The test relies on compile-time verification. This class is compile-time verifiable, so when 30*795d594fSAndroid Build Coastguard Worker * loaded at runtime will not transitively load referenced types eagerly. In that case, our code 31*795d594fSAndroid Build Coastguard Worker * to give descriptive NullPointerExceptions for the field access to the null "instance" of 32*795d594fSAndroid Build Coastguard Worker * OOMEHelper in nullAccess() will be the first attempting to load the class, and, under the 33*795d594fSAndroid Build Coastguard Worker * induced low-memory situation, will throw itself an OutOfMemoryError. 34*795d594fSAndroid Build Coastguard Worker */ 35*795d594fSAndroid Build Coastguard Worker public class OOMEOnNullAccess { 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Worker static ArrayList<Object> storage = new ArrayList<>(100000); 38*795d594fSAndroid Build Coastguard Worker exhaustJavaHeap(int size)39*795d594fSAndroid Build Coastguard Worker private static void exhaustJavaHeap(int size) { 40*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 41*795d594fSAndroid Build Coastguard Worker while (size > 0) { 42*795d594fSAndroid Build Coastguard Worker try { 43*795d594fSAndroid Build Coastguard Worker storage.add(new byte[size]); 44*795d594fSAndroid Build Coastguard Worker } catch (OutOfMemoryError e) { 45*795d594fSAndroid Build Coastguard Worker size = size/2; 46*795d594fSAndroid Build Coastguard Worker } 47*795d594fSAndroid Build Coastguard Worker } 48*795d594fSAndroid Build Coastguard Worker } 49*795d594fSAndroid Build Coastguard Worker main(String[] args)50*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) { 51*795d594fSAndroid Build Coastguard Worker // Stop the JIT to be sure nothing is running that could be resolving classes or causing 52*795d594fSAndroid Build Coastguard Worker // verification. 53*795d594fSAndroid Build Coastguard Worker Main.stopJit(); 54*795d594fSAndroid Build Coastguard Worker Main.waitForCompilation(); 55*795d594fSAndroid Build Coastguard Worker 56*795d594fSAndroid Build Coastguard Worker // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw 57*795d594fSAndroid Build Coastguard Worker // OOME to prevent GC thrashing, even if later allocations may succeed. 58*795d594fSAndroid Build Coastguard Worker Runtime.getRuntime().gc(); 59*795d594fSAndroid Build Coastguard Worker System.runFinalization(); 60*795d594fSAndroid Build Coastguard Worker // NOTE: There is a GC invocation in the exhaustJavaHeap(). So we don't need one here. 61*795d594fSAndroid Build Coastguard Worker 62*795d594fSAndroid Build Coastguard Worker int initial_size = 1024 * 1024; 63*795d594fSAndroid Build Coastguard Worker // Repeat to ensure there is no space left on the heap. 64*795d594fSAndroid Build Coastguard Worker exhaustJavaHeap(initial_size); 65*795d594fSAndroid Build Coastguard Worker exhaustJavaHeap(/*size*/ 4); 66*795d594fSAndroid Build Coastguard Worker exhaustJavaHeap(/*size*/ 4); 67*795d594fSAndroid Build Coastguard Worker 68*795d594fSAndroid Build Coastguard Worker 69*795d594fSAndroid Build Coastguard Worker try { 70*795d594fSAndroid Build Coastguard Worker nullAccess(null); 71*795d594fSAndroid Build Coastguard Worker storage.clear(); 72*795d594fSAndroid Build Coastguard Worker throw new RuntimeException("Did not receive exception!"); 73*795d594fSAndroid Build Coastguard Worker } catch (OutOfMemoryError oome) { 74*795d594fSAndroid Build Coastguard Worker storage.clear(); 75*795d594fSAndroid Build Coastguard Worker System.err.println("Received OOME"); 76*795d594fSAndroid Build Coastguard Worker } finally { 77*795d594fSAndroid Build Coastguard Worker // Even if it's an unexpected error, clear so that we can print things later. 78*795d594fSAndroid Build Coastguard Worker storage.clear(); 79*795d594fSAndroid Build Coastguard Worker } 80*795d594fSAndroid Build Coastguard Worker 81*795d594fSAndroid Build Coastguard Worker Main.startJit(); 82*795d594fSAndroid Build Coastguard Worker } 83*795d594fSAndroid Build Coastguard Worker nullAccess(OOMEHelper nullInstance)84*795d594fSAndroid Build Coastguard Worker public static int nullAccess(OOMEHelper nullInstance) { 85*795d594fSAndroid Build Coastguard Worker // Under AOT, this access is the first one to actually load the OOMEHelper class, so 86*795d594fSAndroid Build Coastguard Worker // we can pretty print the name and such. 87*795d594fSAndroid Build Coastguard Worker return nullInstance.nullField; 88*795d594fSAndroid Build Coastguard Worker } 89*795d594fSAndroid Build Coastguard Worker } 90