blob: f007b2535c88049b6f44aee93588009c9782a0de [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2009 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
Mathieu Chartiera61894d2015-04-23 16:32:54 -070017import java.lang.reflect.InvocationTargetException;
18import java.lang.reflect.Method;
19
jeffhao5d1ac922011-09-29 17:41:15 -070020public class Main {
21 static class ArrayMemEater {
Elliott Hughesff9af222013-04-11 18:13:31 -070022 static boolean sawOome;
23
24 static void blowup(char[][] holder) {
jeffhao5d1ac922011-09-29 17:41:15 -070025 try {
Elliott Hughesff9af222013-04-11 18:13:31 -070026 for (int i = 0; i < holder.length; ++i) {
Nicolas Geoffray74d6a822014-10-03 10:54:19 +000027 holder[i] = new char[1024 * 1024];
Elliott Hughesff9af222013-04-11 18:13:31 -070028 }
jeffhao5d1ac922011-09-29 17:41:15 -070029 } catch (OutOfMemoryError oome) {
Elliott Hughesff9af222013-04-11 18:13:31 -070030 ArrayMemEater.sawOome = true;
jeffhao5d1ac922011-09-29 17:41:15 -070031 }
jeffhao5d1ac922011-09-29 17:41:15 -070032 }
33 }
34
35 static class InstanceMemEater {
Elliott Hughesff9af222013-04-11 18:13:31 -070036 static boolean sawOome;
Mark Mendell8ed2e702014-08-18 22:19:06 -040037 static InstanceMemEater hook;
Elliott Hughesff9af222013-04-11 18:13:31 -070038
jeffhao5d1ac922011-09-29 17:41:15 -070039 InstanceMemEater next;
Elliott Hughesff9af222013-04-11 18:13:31 -070040 double d1, d2, d3, d4, d5, d6, d7, d8; // Bloat this object so we fill the heap faster.
jeffhao5d1ac922011-09-29 17:41:15 -070041
Elliott Hughesff9af222013-04-11 18:13:31 -070042 static InstanceMemEater allocate() {
jeffhao5d1ac922011-09-29 17:41:15 -070043 try {
Elliott Hughesff9af222013-04-11 18:13:31 -070044 return new InstanceMemEater();
jeffhao5d1ac922011-09-29 17:41:15 -070045 } catch (OutOfMemoryError e) {
Elliott Hughesff9af222013-04-11 18:13:31 -070046 InstanceMemEater.sawOome = true;
47 return null;
jeffhao5d1ac922011-09-29 17:41:15 -070048 }
jeffhao5d1ac922011-09-29 17:41:15 -070049 }
50
Elliott Hughesff9af222013-04-11 18:13:31 -070051 static void confuseCompilerOptimization(InstanceMemEater instance) {
Mark Mendell8ed2e702014-08-18 22:19:06 -040052 hook = instance;
jeffhao5d1ac922011-09-29 17:41:15 -070053 }
54 }
55
Nicolas Geoffray74d6a822014-10-03 10:54:19 +000056 static boolean triggerArrayOOM() {
57 ArrayMemEater.blowup(new char[128 * 1024][]);
Elliott Hughesff9af222013-04-11 18:13:31 -070058 return ArrayMemEater.sawOome;
jeffhao5d1ac922011-09-29 17:41:15 -070059 }
60
Elliott Hughesff9af222013-04-11 18:13:31 -070061 static boolean triggerInstanceOOM() {
62 InstanceMemEater memEater = InstanceMemEater.allocate();
jeffhao5d1ac922011-09-29 17:41:15 -070063 InstanceMemEater lastMemEater = memEater;
64 do {
Elliott Hughesff9af222013-04-11 18:13:31 -070065 lastMemEater.next = InstanceMemEater.allocate();
jeffhao5d1ac922011-09-29 17:41:15 -070066 lastMemEater = lastMemEater.next;
67 } while (lastMemEater != null);
68 memEater.confuseCompilerOptimization(memEater);
Mark Mendell8ed2e702014-08-18 22:19:06 -040069 InstanceMemEater.hook = null;
Elliott Hughesff9af222013-04-11 18:13:31 -070070 return InstanceMemEater.sawOome;
jeffhao5d1ac922011-09-29 17:41:15 -070071 }
72
73 public static void main(String[] args) {
Mathieu Chartiera61894d2015-04-23 16:32:54 -070074 if (triggerReflectionOOM()) {
75 System.out.println("Test reflection correctly threw");
76 }
77
Nicolas Geoffray74d6a822014-10-03 10:54:19 +000078 if (triggerArrayOOM()) {
Elliott Hughesff9af222013-04-11 18:13:31 -070079 System.out.println("NEW_ARRAY correctly threw OOME");
80 }
81
82 if (triggerInstanceOOM()) {
83 System.out.println("NEW_INSTANCE correctly threw OOME");
84 }
jeffhao5d1ac922011-09-29 17:41:15 -070085 }
Mathieu Chartiera61894d2015-04-23 16:32:54 -070086
87 static Object[] holder;
88
89 public static void blowup() throws Exception {
90 int size = 32 * 1024 * 1024;
91 for (int i = 0; i < holder.length; ) {
92 try {
93 holder[i] = new char[size];
94 i++;
95 } catch (OutOfMemoryError oome) {
96 size = size / 2;
97 if (size == 0) {
98 break;
99 }
100 }
101 }
102 holder[0] = new char[100000];
103 }
104
105 static boolean triggerReflectionOOM() {
106 try {
107 Class<?> c = Main.class;
108 Method m = c.getMethod("blowup", (Class[]) null);
109 holder = new Object[1000000];
110 m.invoke(null);
111 holder = null;
112 System.out.println("Didn't throw from blowup");
113 } catch (OutOfMemoryError e) {
114 holder = null;
115 } catch (InvocationTargetException e) {
116 holder = null;
117 if (!(e.getCause() instanceof OutOfMemoryError)) {
118 System.out.println("InvocationTargetException cause not OOME " + e.getCause());
119 return false;
120 }
121 } catch (Exception e) {
122 holder = null;
123 System.out.println("Unexpected exception " + e);
124 return false;
125 }
126 return true;
127 }
jeffhao5d1ac922011-09-29 17:41:15 -0700128}