diff options
| -rw-r--r-- | runtime/native/java_lang_Class.cc | 4 | ||||
| -rw-r--r-- | test/080-oom-throw/expected.txt | 1 | ||||
| -rw-r--r-- | test/080-oom-throw/src/Main.java | 43 |
3 files changed, 48 insertions, 0 deletions
diff --git a/runtime/native/java_lang_Class.cc b/runtime/native/java_lang_Class.cc index 3341f531e4..5438a6ddb4 100644 --- a/runtime/native/java_lang_Class.cc +++ b/runtime/native/java_lang_Class.cc @@ -428,6 +428,10 @@ static jobjectArray Class_getDeclaredMethodsUnchecked(JNIEnv* env, jobject javaT } auto ret = hs.NewHandle(mirror::ObjectArray<mirror::Method>::Alloc( soa.Self(), mirror::Method::ArrayClass(), num_methods)); + if (ret.Get() == nullptr) { + soa.Self()->AssertPendingOOMException(); + return nullptr; + } num_methods = 0; for (auto& m : klass->GetDeclaredMethods(kRuntimePointerSize)) { auto modifiers = m.GetAccessFlags(); diff --git a/test/080-oom-throw/expected.txt b/test/080-oom-throw/expected.txt index 904393bc3b..0967278314 100644 --- a/test/080-oom-throw/expected.txt +++ b/test/080-oom-throw/expected.txt @@ -1,3 +1,4 @@ Test reflection correctly threw +Test reflection2 correctly threw NEW_ARRAY correctly threw OOME NEW_INSTANCE correctly threw OOME diff --git a/test/080-oom-throw/src/Main.java b/test/080-oom-throw/src/Main.java index 0ae92a96dc..a6c18b75fc 100644 --- a/test/080-oom-throw/src/Main.java +++ b/test/080-oom-throw/src/Main.java @@ -53,6 +53,30 @@ public class Main { } } + public static Object eatAllMemory() { + Object[] result = null; + int size = 1000000; + while (result == null && size != 0) { + try { + result = new Object[size]; + } catch (OutOfMemoryError oome) { + size /= 2; + } + } + if (result != null) { + int index = 0; + while (index != result.length && size != 0) { + try { + result[index] = new byte[size]; + ++index; + } catch (OutOfMemoryError oome) { + size /= 2; + } + } + } + return result; + } + static boolean triggerArrayOOM() { ArrayMemEater.blowup(new char[128 * 1024][]); return ArrayMemEater.sawOome; @@ -74,6 +98,9 @@ public class Main { if (triggerReflectionOOM()) { System.out.println("Test reflection correctly threw"); } + if (triggerReflectionOOM2()) { + System.out.println("Test reflection2 correctly threw"); + } if (triggerArrayOOM()) { System.out.println("NEW_ARRAY correctly threw OOME"); @@ -125,4 +152,20 @@ public class Main { } return true; } + + static boolean triggerReflectionOOM2() { + Object memory = eatAllMemory(); + boolean result = false; + try { + Main.class.getDeclaredMethods(); + } catch (OutOfMemoryError e) { + result = true; + } + if (!result) { + boolean memoryWasAllocated = (memory != null); + memory = null; + System.out.println("memoryWasAllocated = " + memoryWasAllocated); + } + return result; + } } |