Fix Missing ReflectiveHandleScope in Class_newInstance
The Class_newInstance method (the native implementation of the
j.l.Class.newInstance function) incorrectly held an ArtMethod* for the
constructor over a suspend point. This could lead to an obsolete
method being called or (worst case) CHECK failures due to not
finishing the initialization of the class if it's made obsolete.
Test: ./test/run-test --host 2001
Bug: 145197371
Bug: 134162467
Change-Id: I7813977bfdd17165da810a1705197654eef024a1
diff --git a/runtime/native/java_lang_Class.cc b/runtime/native/java_lang_Class.cc
index 49e37fe..da87713 100644
--- a/runtime/native/java_lang_Class.cc
+++ b/runtime/native/java_lang_Class.cc
@@ -48,6 +48,7 @@
#include "nth_caller_visitor.h"
#include "obj_ptr-inl.h"
#include "reflection.h"
+#include "reflective_handle_scope-inl.h"
#include "scoped_fast_native_object_access-inl.h"
#include "scoped_thread_state_change-inl.h"
#include "well_known_classes.h"
@@ -900,11 +901,10 @@
return nullptr;
}
}
- ArtMethod* constructor = klass->GetDeclaredConstructor(
- soa.Self(),
- ScopedNullHandle<mirror::ObjectArray<mirror::Class>>(),
- kRuntimePointerSize);
- if (UNLIKELY(constructor == nullptr) || ShouldDenyAccessToMember(constructor, soa.Self())) {
+ StackArtMethodHandleScope<1> mhs(soa.Self());
+ ReflectiveHandle<ArtMethod> constructor(mhs.NewMethodHandle(klass->GetDeclaredConstructor(
+ soa.Self(), ScopedNullHandle<mirror::ObjectArray<mirror::Class>>(), kRuntimePointerSize)));
+ if (UNLIKELY(constructor == nullptr) || ShouldDenyAccessToMember(constructor.Get(), soa.Self())) {
soa.Self()->ThrowNewExceptionF("Ljava/lang/InstantiationException;",
"%s has no zero argument constructor",
klass->PrettyClass().c_str());