8167372: Add code to check for getting oops while thread is in native

Add asserts that detect when a thread is getting oops while in native

Reviewed-by: coleenp, shade, jiangli, gtriantafill
This commit is contained in:
Harold Seigel 2017-12-15 15:13:18 -05:00
parent 4db5d786b7
commit dd8b189097
2 changed files with 18 additions and 0 deletions

View File

@ -47,6 +47,7 @@ jobject JNIHandles::make_local(oop obj) {
} else { } else {
Thread* thread = Thread::current(); Thread* thread = Thread::current();
assert(Universe::heap()->is_in_reserved(obj), "sanity check"); assert(Universe::heap()->is_in_reserved(obj), "sanity check");
assert(!current_thread_in_native(), "must not be in native");
return thread->active_handles()->allocate_handle(obj); return thread->active_handles()->allocate_handle(obj);
} }
} }
@ -59,6 +60,8 @@ jobject JNIHandles::make_local(Thread* thread, oop obj) {
return NULL; // ignore null handles return NULL; // ignore null handles
} else { } else {
assert(Universe::heap()->is_in_reserved(obj), "sanity check"); assert(Universe::heap()->is_in_reserved(obj), "sanity check");
assert(thread->is_Java_thread(), "not a Java thread");
assert(!current_thread_in_native(), "must not be in native");
return thread->active_handles()->allocate_handle(obj); return thread->active_handles()->allocate_handle(obj);
} }
} }
@ -70,6 +73,7 @@ jobject JNIHandles::make_local(JNIEnv* env, oop obj) {
} else { } else {
JavaThread* thread = JavaThread::thread_from_jni_environment(env); JavaThread* thread = JavaThread::thread_from_jni_environment(env);
assert(Universe::heap()->is_in_reserved(obj), "sanity check"); assert(Universe::heap()->is_in_reserved(obj), "sanity check");
assert(!current_thread_in_native(), "must not be in native");
return thread->active_handles()->allocate_handle(obj); return thread->active_handles()->allocate_handle(obj);
} }
} }
@ -77,6 +81,7 @@ jobject JNIHandles::make_local(JNIEnv* env, oop obj) {
jobject JNIHandles::make_global(Handle obj) { jobject JNIHandles::make_global(Handle obj) {
assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
assert(!current_thread_in_native(), "must not be in native");
jobject res = NULL; jobject res = NULL;
if (!obj.is_null()) { if (!obj.is_null()) {
// ignore null handles // ignore null handles
@ -93,6 +98,7 @@ jobject JNIHandles::make_global(Handle obj) {
jobject JNIHandles::make_weak_global(Handle obj) { jobject JNIHandles::make_weak_global(Handle obj) {
assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC");
assert(!current_thread_in_native(), "must not be in native");
jobject res = NULL; jobject res = NULL;
if (!obj.is_null()) { if (!obj.is_null()) {
// ignore null handles // ignore null handles
@ -265,6 +271,13 @@ void JNIHandles::verify() {
weak_oops_do(&verify_handle); weak_oops_do(&verify_handle);
} }
// This method is implemented here to avoid circular includes between
// jniHandles.hpp and thread.hpp.
bool JNIHandles::current_thread_in_native() {
Thread* thread = Thread::current();
return (thread->is_Java_thread() &&
JavaThread::current()->thread_state() == _thread_in_native);
}
void jni_handles_init() { void jni_handles_init() {

View File

@ -48,6 +48,10 @@ class JNIHandles : AllStatic {
template<bool external_guard> inline static oop resolve_impl(jobject handle); template<bool external_guard> inline static oop resolve_impl(jobject handle);
template<bool external_guard> static oop resolve_jweak(jweak handle); template<bool external_guard> static oop resolve_jweak(jweak handle);
// This method is not inlined in order to avoid circular includes between
// this header file and thread.hpp.
static bool current_thread_in_native();
public: public:
// Low tag bit in jobject used to distinguish a jweak. jweak is // Low tag bit in jobject used to distinguish a jweak. jweak is
// type equivalent to jobject, but there are places where we need to // type equivalent to jobject, but there are places where we need to
@ -230,6 +234,7 @@ inline oop JNIHandles::guard_value(oop value) {
template<bool external_guard> template<bool external_guard>
inline oop JNIHandles::resolve_impl(jobject handle) { inline oop JNIHandles::resolve_impl(jobject handle) {
assert(handle != NULL, "precondition"); assert(handle != NULL, "precondition");
assert(!current_thread_in_native(), "must not be in native");
oop result; oop result;
if (is_jweak(handle)) { // Unlikely if (is_jweak(handle)) { // Unlikely
result = resolve_jweak<external_guard>(handle); result = resolve_jweak<external_guard>(handle);