Skip to content

8282475: SafeFetch should not rely on existence of Thread::current #7727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
6 changes: 2 additions & 4 deletions src/hotspot/cpu/aarch64/jniFastGetField_aarch64.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -32,7 +32,6 @@
#include "prims/jvm_misc.hpp"
#include "prims/jvmtiExport.hpp"
#include "runtime/safepoint.hpp"
#include "runtime/threadWXSetters.inline.hpp"

#define __ masm->

Expand Down Expand Up @@ -74,8 +73,7 @@ template<> struct BasicTypeToJni<T_DOUBLE> { static const jdouble jni_type; };

template<int BType, typename JniType = decltype(BasicTypeToJni<BType>::jni_type)>
JniType static_fast_get_field_wrapper(JNIEnv *env, jobject obj, jfieldID fieldID) {
JavaThread* thread = JavaThread::thread_from_jni_environment(env);
ThreadWXEnable wx(WXExec, thread);
os::ThreadWX::Enable wx(os::ThreadWX::Exec);
address get_field_addr = generated_fast_get_field[BType - T_BOOLEAN];
return ((JniType(*)(JNIEnv *env, jobject obj, jfieldID fieldID))get_field_addr)(env, obj, fieldID);
}
Expand Down
24 changes: 20 additions & 4 deletions src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, Red Hat Inc. All rights reserved.
* Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
Expand Down Expand Up @@ -205,7 +205,7 @@ bool PosixSignals::pd_hotspot_signal_handler(int sig, siginfo_t* info,
ucontext_t* uc, JavaThread* thread) {
// Enable WXWrite: this function is called by the signal handler at arbitrary
// point of execution.
ThreadWXEnable wx(WXWrite, thread);
os::ThreadWX::Enable wx(os::ThreadWX::Write);

// decide if this trap can be handled by a stub
address stub = NULL;
Expand Down Expand Up @@ -534,14 +534,30 @@ void os::verify_stack_alignment() {
}
#endif

#ifdef __APPLE__

static THREAD_LOCAL os::ThreadWX::WXMode _wx_state = os::ThreadWX::Write;

int os::extra_bang_size_in_bytes() {
// AArch64 does not require the additional stack bang.
return 0;
}

void os::current_thread_enable_wx(WXMode mode) {
pthread_jit_write_protect_np(mode == WXExec);
os::ThreadWX::WXMode os::ThreadWX::change(WXMode new_state) {
WXMode old = _wx_state;
_wx_state = new_state;
pthread_jit_write_protect_np(_wx_state == os::ThreadWX::Exec);
return old;
}

void os::ThreadWX::init() {
change(os::ThreadWX::Write);
}

void os::ThreadWX::assert_wx(WXMode expected) {
assert(_wx_state == expected, "wrong state");
}
#endif

extern "C" {
int SpinPause() {
Expand Down
33 changes: 33 additions & 0 deletions src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,37 @@
*(jlong *) dst = *(const jlong *) src;
}

#ifdef __APPLE__

class ThreadWX {
public:

enum WXMode {
Write,
Exec
};

// Enables write or execute access to writeable and executable pages.
// returns the previous state
static WXMode change(WXMode new_state);

// initializes the WXMode to WXWrite, as writeable pages are the default here
static void init();

static void assert_wx(WXMode expected);

// RAII object to set a specific WXMode and reset it to the previous mode
// on destruction
class Enable {
WXMode _old_mode;
public:
Enable(WXMode new_mode) :
_old_mode(change(new_mode))
{ }
~Enable() {
change(_old_mode);
}
};
};
#endif // __APPLE__
#endif // OS_CPU_BSD_AARCH64_OS_BSD_AARCH64_HPP
4 changes: 2 additions & 2 deletions src/hotspot/share/c1/c1_Runtime1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -1267,7 +1267,7 @@ void Runtime1::patch_code(JavaThread* current, Runtime1::StubID stub_id) {

// Enable WXWrite: the function is called by c1 stub as a runtime function
// (see another implementation above).
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
MACOS_AARCH64_ONLY(os::ThreadWX::Enable wx(os::ThreadWX::Write));

if (TracePatching) {
tty->print_cr("Deoptimizing because patch is needed");
Expand Down
5 changes: 2 additions & 3 deletions src/hotspot/share/code/nmethod.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -67,7 +67,6 @@
#include "runtime/sharedRuntime.hpp"
#include "runtime/signature.hpp"
#include "runtime/sweeper.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/vmThread.hpp"
#include "utilities/align.hpp"
#include "utilities/copy.hpp"
Expand Down Expand Up @@ -2881,7 +2880,7 @@ void nmethod::decode2(outputStream* ost) const {
#endif

// Decoding an nmethod can write to a PcDescCache (see PcDescCache::add_pc_desc)
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current());)
MACOS_AARCH64_ONLY(os::ThreadWX::Enable wx(os::ThreadWX::Write);)

st->cr();
this->print(st);
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/gc/shared/barrierSetNMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "logging/log.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/thread.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/os.hpp"
#include "utilities/debug.hpp"

int BarrierSetNMethod::disarmed_value() const {
Expand All @@ -52,7 +52,7 @@ bool BarrierSetNMethod::supports_entry_barrier(nmethod* nm) {
int BarrierSetNMethod::nmethod_stub_entry_barrier(address* return_address_ptr) {
// Enable WXWrite: the function is called directly from nmethod_entry_barrier
// stub.
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current()));
MACOS_AARCH64_ONLY(os::ThreadWX::Enable wx(os::ThreadWX::Write));

address return_address = *return_address_ptr;
AARCH64_PORT_ONLY(return_address = pauth_strip_pointer(return_address));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021, Red Hat, Inc. All rights reserved.
* Copyright (c) 2019, 2022, Red Hat, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,7 +33,7 @@
#include "gc/shenandoah/shenandoahThreadLocalData.hpp"
#include "memory/iterator.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/os.hpp"

bool ShenandoahBarrierSetNMethod::nmethod_entry_barrier(nmethod* nm) {
ShenandoahReentrantLock* lock = ShenandoahNMethod::lock_for_nmethod(nm);
Expand All @@ -46,7 +46,7 @@ bool ShenandoahBarrierSetNMethod::nmethod_entry_barrier(nmethod* nm) {
return true;
}

MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current());)
MACOS_AARCH64_ONLY(os::ThreadWX::Enable wx(os::ThreadWX::Write);)

if (nm->is_unloading()) {
// We don't need to take the lock when unlinking nmethods from
Expand Down
6 changes: 3 additions & 3 deletions src/hotspot/share/gc/z/zBarrierSetNMethod.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -29,7 +29,7 @@
#include "gc/z/zNMethod.hpp"
#include "gc/z/zThreadLocalData.hpp"
#include "logging/log.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/os.hpp"

bool ZBarrierSetNMethod::nmethod_entry_barrier(nmethod* nm) {
ZLocker<ZReentrantLock> locker(ZNMethod::lock_for_nmethod(nm));
Expand All @@ -41,7 +41,7 @@ bool ZBarrierSetNMethod::nmethod_entry_barrier(nmethod* nm) {
return true;
}

MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, Thread::current()));
MACOS_AARCH64_ONLY(os::ThreadWX::Enable wx(os::ThreadWX::Write));

if (nm->is_unloading()) {
// We don't need to take the lock when unlinking nmethods from
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/interpreter/interpreterRuntime.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -983,7 +983,7 @@ JRT_END

nmethod* InterpreterRuntime::frequency_counter_overflow(JavaThread* current, address branch_bcp) {
// Enable WXWrite: the function is called directly by interpreter.
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, current));
MACOS_AARCH64_ONLY(os::ThreadWX::Enable wx(os::ThreadWX::Write));

// frequency_counter_overflow_inner can throw async exception.
nmethod* nm = frequency_counter_overflow_inner(current, branch_bcp);
Expand Down
2 changes: 1 addition & 1 deletion src/hotspot/share/jvmci/jvmciCompilerToVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Handle JavaArgumentUnboxer::next_arg(BasicType expectedType) {

// Bring the JVMCI compiler thread into the VM state.
#define JVMCI_VM_ENTRY_MARK \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
MACOS_AARCH64_ONLY(os::ThreadWX::Enable __wx(os::ThreadWX::Write)); \
ThreadInVMfromNative __tiv(thread); \
HandleMarkCleaner __hm(thread); \
JavaThread* THREAD = thread; \
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/opto/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@
#include "runtime/handles.inline.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/javaCalls.hpp"
#include "runtime/os.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/signature.hpp"
#include "runtime/stackWatermarkSet.hpp"
#include "runtime/threadCritical.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vframeArray.hpp"
#include "runtime/vframe_hp.hpp"
Expand Down Expand Up @@ -1463,7 +1463,7 @@ address OptoRuntime::handle_exception_C(JavaThread* current) {
address OptoRuntime::rethrow_C(oopDesc* exception, JavaThread* thread, address ret_pc) {

// Enable WXWrite: the function called directly by compiled code.
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));
MACOS_AARCH64_ONLY(os::ThreadWX::Enable wx(os::ThreadWX::Write));

// ret_pc will have been loaded from the stack, so for AArch64 will be signed.
// This needs authenticating, but to do that here requires the fp of the previous frame.
Expand Down
13 changes: 6 additions & 7 deletions src/hotspot/share/prims/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3656,7 +3656,7 @@ static jint JNI_CreateJavaVM_inner(JavaVM **vm, void **penv, void *args) {

// Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
ThreadStateTransition::transition_from_vm(thread, _thread_in_native);
MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
MACOS_AARCH64_ONLY(os::ThreadWX::change(os::ThreadWX::Exec));
} else {
// If create_vm exits because of a pending exception, exit with that
// exception. In the future when we figure out how to reclaim memory,
Expand Down Expand Up @@ -3749,7 +3749,7 @@ static jint JNICALL jni_DestroyJavaVM_inner(JavaVM *vm) {
JavaThread* thread = JavaThread::current();

// We are going to VM, change W^X state to the expected one.
MACOS_AARCH64_ONLY(WXMode oldmode = thread->enable_wx(WXWrite));
MACOS_AARCH64_ONLY(os::ThreadWX::change(os::ThreadWX::Write));

ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
Threads::destroy_vm();
Expand Down Expand Up @@ -3806,7 +3806,7 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae
thread->record_stack_base_and_size();
thread->register_thread_stack_with_NMT();
thread->initialize_thread_current();
MACOS_AARCH64_ONLY(thread->init_wx());
MACOS_AARCH64_ONLY(os::ThreadWX::init());

if (!os::create_attached_thread(thread)) {
thread->smr_delete();
Expand Down Expand Up @@ -3877,7 +3877,7 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae
// Now leaving the VM, so change thread_state. This is normally automatically taken care
// of in the JVM_ENTRY. But in this situation we have to do it manually.
ThreadStateTransition::transition_from_vm(thread, _thread_in_native);
MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
MACOS_AARCH64_ONLY(os::ThreadWX::change(os::ThreadWX::Exec));

// Perform any platform dependent FPU setup
os::setup_fpu();
Expand Down Expand Up @@ -3930,7 +3930,7 @@ jint JNICALL jni_DetachCurrentThread(JavaVM *vm) {
}

// We are going to VM, change W^X state to the expected one.
MACOS_AARCH64_ONLY(thread->enable_wx(WXWrite));
MACOS_AARCH64_ONLY(os::ThreadWX::change(os::ThreadWX::Write));

// Safepoint support. Have to do call-back to safepoint code, if in the
// middle of a safepoint operation
Expand All @@ -3949,8 +3949,7 @@ jint JNICALL jni_DetachCurrentThread(JavaVM *vm) {
thread->smr_delete();

// Go to the execute mode, the initial state of the thread on creation.
// Use os interface as the thread is not a JavaThread anymore.
MACOS_AARCH64_ONLY(os::current_thread_enable_wx(WXExec));
MACOS_AARCH64_ONLY(os::ThreadWX::change(os::ThreadWX::Exec));

HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
return JNI_OK;
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/prims/jniCheck.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -104,7 +104,7 @@ extern "C" { \
if (env != xenv) { \
NativeReportJNIFatalError(thr, warn_wrong_jnienv); \
} \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thr)); \
MACOS_AARCH64_ONLY(os::ThreadWX::Enable __wx(os::ThreadWX::Write)); \
VM_ENTRY_BASE(result_type, header, thr)


Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/prims/jvmtiEnter.xsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<!--
Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.

This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -435,7 +435,7 @@ struct jvmtiInterface_1_ jvmti</xsl:text>
<xsl:if test="count(@impl)=0 or not(contains(@impl,'innative'))">
<xsl:text>JavaThread* current_thread = JavaThread::cast(this_thread);</xsl:text>
<xsl:value-of select="$space"/>
<xsl:text>MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));</xsl:text>
<xsl:text>MACOS_AARCH64_ONLY(os::ThreadWX::Enable __wx(os::ThreadWX::Write));</xsl:text>
<xsl:value-of select="$space"/>
<xsl:text>ThreadInVMfromNative __tiv(current_thread);</xsl:text>
<xsl:value-of select="$space"/>
Expand Down
8 changes: 4 additions & 4 deletions src/hotspot/share/prims/jvmtiEnv.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -176,7 +176,7 @@ JvmtiEnv::GetThreadLocalStorage(jthread thread, void** data_ptr) {
// other than the current thread is required we need to transition
// from native so as to resolve the jthread.

MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));
MACOS_AARCH64_ONLY(os::ThreadWX::Enable __wx(os::ThreadWX::Write));
ThreadInVMfromNative __tiv(current_thread);
VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
debug_only(VMNativeEntryWrapper __vew;)
Expand Down Expand Up @@ -3137,7 +3137,7 @@ JvmtiEnv::RawMonitorEnter(JvmtiRawMonitor * rmonitor) {
} else {
Thread* thread = Thread::current();
// 8266889: raw_enter changes Java thread state, needs WXWrite
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
MACOS_AARCH64_ONLY(os::ThreadWX::Enable __wx(os::ThreadWX::Write));
rmonitor->raw_enter(thread);
}
return JVMTI_ERROR_NONE;
Expand Down Expand Up @@ -3171,7 +3171,7 @@ jvmtiError
JvmtiEnv::RawMonitorWait(JvmtiRawMonitor * rmonitor, jlong millis) {
Thread* thread = Thread::current();
// 8266889: raw_wait changes Java thread state, needs WXWrite
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread));
MACOS_AARCH64_ONLY(os::ThreadWX::Enable __wx(os::ThreadWX::Write));
int r = rmonitor->raw_wait(millis, thread);

switch (r) {
Expand Down
Loading