jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 17 | import java.lang.reflect.InvocationTargetException; |
| 18 | import java.lang.reflect.Method; |
| 19 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 20 | public class Main { |
| 21 | static class ArrayMemEater { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 22 | static boolean sawOome; |
| 23 | |
| 24 | static void blowup(char[][] holder) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 25 | try { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 26 | for (int i = 0; i < holder.length; ++i) { |
Nicolas Geoffray | 74d6a82 | 2014-10-03 10:54:19 +0000 | [diff] [blame] | 27 | holder[i] = new char[1024 * 1024]; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 28 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 29 | } catch (OutOfMemoryError oome) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 30 | ArrayMemEater.sawOome = true; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 31 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | static class InstanceMemEater { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 36 | static boolean sawOome; |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 37 | static InstanceMemEater hook; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 38 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 39 | InstanceMemEater next; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 40 | double d1, d2, d3, d4, d5, d6, d7, d8; // Bloat this object so we fill the heap faster. |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 42 | static InstanceMemEater allocate() { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 43 | try { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 44 | return new InstanceMemEater(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 45 | } catch (OutOfMemoryError e) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 46 | InstanceMemEater.sawOome = true; |
| 47 | return null; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 48 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 51 | static void confuseCompilerOptimization(InstanceMemEater instance) { |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 52 | hook = instance; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Lokesh Gidra | 5217396 | 2020-05-15 17:09:14 -0700 | [diff] [blame] | 56 | private static int exhaustJavaHeap(Object[] data, int index, int size) { |
Lokesh Gidra | b3146d0 | 2020-05-21 13:28:08 -0700 | [diff] [blame] | 57 | Runtime.getRuntime().gc(); |
Lokesh Gidra | 5217396 | 2020-05-15 17:09:14 -0700 | [diff] [blame] | 58 | while (index != data.length && size != 0) { |
| 59 | try { |
| 60 | data[index] = new byte[size]; |
| 61 | ++index; |
| 62 | } catch (OutOfMemoryError oome) { |
| 63 | size /= 2; |
| 64 | } |
| 65 | } |
| 66 | return index; |
| 67 | } |
| 68 | |
Vladimir Marko | a35510d | 2017-01-16 22:42:09 +0000 | [diff] [blame] | 69 | public static Object eatAllMemory() { |
| 70 | Object[] result = null; |
| 71 | int size = 1000000; |
Hans Boehm | 2a84650 | 2020-04-28 15:11:21 -0700 | [diff] [blame] | 72 | // Make sure that there is no reclaimable memory in the heap. Otherwise we may throw |
| 73 | // OOME to prevent GC thrashing, even if later allocations may succeed. |
| 74 | Runtime.getRuntime().gc(); |
| 75 | System.runFinalization(); |
Lokesh Gidra | b3146d0 | 2020-05-21 13:28:08 -0700 | [diff] [blame] | 76 | // NOTE: There is a GC invocation in exhaustJavaHeap. So we don't need one here. |
| 77 | |
Vladimir Marko | a35510d | 2017-01-16 22:42:09 +0000 | [diff] [blame] | 78 | while (result == null && size != 0) { |
| 79 | try { |
| 80 | result = new Object[size]; |
| 81 | } catch (OutOfMemoryError oome) { |
| 82 | size /= 2; |
| 83 | } |
| 84 | } |
| 85 | if (result != null) { |
| 86 | int index = 0; |
Lokesh Gidra | 5217396 | 2020-05-15 17:09:14 -0700 | [diff] [blame] | 87 | // Repeat to ensure there is no space left on the heap. |
| 88 | index = exhaustJavaHeap(result, index, size); |
Lokesh Gidra | b3146d0 | 2020-05-21 13:28:08 -0700 | [diff] [blame] | 89 | index = exhaustJavaHeap(result, index, /*size*/ 4); |
| 90 | index = exhaustJavaHeap(result, index, /*size*/ 4); |
Vladimir Marko | a35510d | 2017-01-16 22:42:09 +0000 | [diff] [blame] | 91 | } |
| 92 | return result; |
| 93 | } |
| 94 | |
Nicolas Geoffray | 74d6a82 | 2014-10-03 10:54:19 +0000 | [diff] [blame] | 95 | static boolean triggerArrayOOM() { |
| 96 | ArrayMemEater.blowup(new char[128 * 1024][]); |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 97 | return ArrayMemEater.sawOome; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 100 | static boolean triggerInstanceOOM() { |
| 101 | InstanceMemEater memEater = InstanceMemEater.allocate(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 102 | InstanceMemEater lastMemEater = memEater; |
| 103 | do { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 104 | lastMemEater.next = InstanceMemEater.allocate(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 105 | lastMemEater = lastMemEater.next; |
| 106 | } while (lastMemEater != null); |
| 107 | memEater.confuseCompilerOptimization(memEater); |
Mark Mendell | 8ed2e70 | 2014-08-18 22:19:06 -0400 | [diff] [blame] | 108 | InstanceMemEater.hook = null; |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 109 | return InstanceMemEater.sawOome; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | public static void main(String[] args) { |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 113 | if (triggerReflectionOOM()) { |
| 114 | System.out.println("Test reflection correctly threw"); |
| 115 | } |
Vladimir Marko | a35510d | 2017-01-16 22:42:09 +0000 | [diff] [blame] | 116 | if (triggerReflectionOOM2()) { |
| 117 | System.out.println("Test reflection2 correctly threw"); |
| 118 | } |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 119 | |
Nicolas Geoffray | 74d6a82 | 2014-10-03 10:54:19 +0000 | [diff] [blame] | 120 | if (triggerArrayOOM()) { |
Elliott Hughes | ff9af22 | 2013-04-11 18:13:31 -0700 | [diff] [blame] | 121 | System.out.println("NEW_ARRAY correctly threw OOME"); |
| 122 | } |
| 123 | |
| 124 | if (triggerInstanceOOM()) { |
| 125 | System.out.println("NEW_INSTANCE correctly threw OOME"); |
| 126 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 127 | } |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 128 | |
| 129 | static Object[] holder; |
| 130 | |
| 131 | public static void blowup() throws Exception { |
Mathieu Chartier | 30f530e | 2017-03-22 10:02:31 -0700 | [diff] [blame] | 132 | int size = 2 * 1024 * 1024; |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 133 | for (int i = 0; i < holder.length; ) { |
| 134 | try { |
| 135 | holder[i] = new char[size]; |
| 136 | i++; |
| 137 | } catch (OutOfMemoryError oome) { |
Mathieu Chartier | 30f530e | 2017-03-22 10:02:31 -0700 | [diff] [blame] | 138 | size = size / 16; |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 139 | if (size == 0) { |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | holder[0] = new char[100000]; |
| 145 | } |
| 146 | |
| 147 | static boolean triggerReflectionOOM() { |
| 148 | try { |
| 149 | Class<?> c = Main.class; |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 150 | Method m = c.getMethod("blowup"); |
Mathieu Chartier | a61894d | 2015-04-23 16:32:54 -0700 | [diff] [blame] | 151 | holder = new Object[1000000]; |
| 152 | m.invoke(null); |
| 153 | holder = null; |
| 154 | System.out.println("Didn't throw from blowup"); |
| 155 | } catch (OutOfMemoryError e) { |
| 156 | holder = null; |
| 157 | } catch (InvocationTargetException e) { |
| 158 | holder = null; |
| 159 | if (!(e.getCause() instanceof OutOfMemoryError)) { |
| 160 | System.out.println("InvocationTargetException cause not OOME " + e.getCause()); |
| 161 | return false; |
| 162 | } |
| 163 | } catch (Exception e) { |
| 164 | holder = null; |
| 165 | System.out.println("Unexpected exception " + e); |
| 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
Vladimir Marko | a35510d | 2017-01-16 22:42:09 +0000 | [diff] [blame] | 170 | |
| 171 | static boolean triggerReflectionOOM2() { |
| 172 | Object memory = eatAllMemory(); |
| 173 | boolean result = false; |
| 174 | try { |
| 175 | Main.class.getDeclaredMethods(); |
| 176 | } catch (OutOfMemoryError e) { |
| 177 | result = true; |
| 178 | } |
| 179 | if (!result) { |
| 180 | boolean memoryWasAllocated = (memory != null); |
| 181 | memory = null; |
| 182 | System.out.println("memoryWasAllocated = " + memoryWasAllocated); |
| 183 | } |
| 184 | return result; |
| 185 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 186 | } |