diff --git a/src/hotspot/cpu/aarch64/jniFastGetField_aarch64.cpp b/src/hotspot/cpu/aarch64/jniFastGetField_aarch64.cpp index 35b0ea9667238..0f96d00f493f5 100644 --- a/src/hotspot/cpu/aarch64/jniFastGetField_aarch64.cpp +++ b/src/hotspot/cpu/aarch64/jniFastGetField_aarch64.cpp @@ -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. * @@ -32,7 +32,6 @@ #include "prims/jvm_misc.hpp" #include "prims/jvmtiExport.hpp" #include "runtime/safepoint.hpp" -#include "runtime/threadWXSetters.inline.hpp" #define __ masm-> @@ -74,8 +73,7 @@ template<> struct BasicTypeToJni { static const jdouble jni_type; }; template::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); } diff --git a/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp b/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp index d9a54bf695d4e..d6171cb8b2bdf 100644 --- a/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp +++ b/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.cpp @@ -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. @@ -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; @@ -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() { diff --git a/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.hpp b/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.hpp index 14638bc68d65a..8d5d1826488e5 100644 --- a/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.hpp +++ b/src/hotspot/os_cpu/bsd_aarch64/os_bsd_aarch64.hpp @@ -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 diff --git a/src/hotspot/share/c1/c1_Runtime1.cpp b/src/hotspot/share/c1/c1_Runtime1.cpp index 03cb4a961f40f..bced8f9ec747d 100644 --- a/src/hotspot/share/c1/c1_Runtime1.cpp +++ b/src/hotspot/share/c1/c1_Runtime1.cpp @@ -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 @@ -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"); diff --git a/src/hotspot/share/code/nmethod.cpp b/src/hotspot/share/code/nmethod.cpp index 543892bc434aa..2c4fdec199ca0 100644 --- a/src/hotspot/share/code/nmethod.cpp +++ b/src/hotspot/share/code/nmethod.cpp @@ -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 @@ -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" @@ -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); diff --git a/src/hotspot/share/gc/shared/barrierSetNMethod.cpp b/src/hotspot/share/gc/shared/barrierSetNMethod.cpp index efe2ff7b9e309..9f4ff3144b1d6 100644 --- a/src/hotspot/share/gc/shared/barrierSetNMethod.cpp +++ b/src/hotspot/share/gc/shared/barrierSetNMethod.cpp @@ -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 { @@ -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)); diff --git a/src/hotspot/share/gc/shenandoah/shenandoahBarrierSetNMethod.cpp b/src/hotspot/share/gc/shenandoah/shenandoahBarrierSetNMethod.cpp index ebc288efda973..d7c2b1c86af37 100644 --- a/src/hotspot/share/gc/shenandoah/shenandoahBarrierSetNMethod.cpp +++ b/src/hotspot/share/gc/shenandoah/shenandoahBarrierSetNMethod.cpp @@ -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 @@ -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); @@ -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 diff --git a/src/hotspot/share/gc/z/zBarrierSetNMethod.cpp b/src/hotspot/share/gc/z/zBarrierSetNMethod.cpp index 9916178cc2c54..591d533b5839b 100644 --- a/src/hotspot/share/gc/z/zBarrierSetNMethod.cpp +++ b/src/hotspot/share/gc/z/zBarrierSetNMethod.cpp @@ -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 @@ -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 locker(ZNMethod::lock_for_nmethod(nm)); @@ -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 diff --git a/src/hotspot/share/interpreter/interpreterRuntime.cpp b/src/hotspot/share/interpreter/interpreterRuntime.cpp index 5e61410ddc204..a3d015031a886 100644 --- a/src/hotspot/share/interpreter/interpreterRuntime.cpp +++ b/src/hotspot/share/interpreter/interpreterRuntime.cpp @@ -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 @@ -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); diff --git a/src/hotspot/share/jvmci/jvmciCompilerToVM.cpp b/src/hotspot/share/jvmci/jvmciCompilerToVM.cpp index 6f7269193f653..c4dd0fedeeffd 100644 --- a/src/hotspot/share/jvmci/jvmciCompilerToVM.cpp +++ b/src/hotspot/share/jvmci/jvmciCompilerToVM.cpp @@ -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; \ diff --git a/src/hotspot/share/opto/runtime.cpp b/src/hotspot/share/opto/runtime.cpp index c20b55a1b3391..155c4230f9ab6 100644 --- a/src/hotspot/share/opto/runtime.cpp +++ b/src/hotspot/share/opto/runtime.cpp @@ -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" @@ -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. diff --git a/src/hotspot/share/prims/jni.cpp b/src/hotspot/share/prims/jni.cpp index 13a2ff9dc8718..f156261e2d9dd 100644 --- a/src/hotspot/share/prims/jni.cpp +++ b/src/hotspot/share/prims/jni.cpp @@ -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, @@ -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(); @@ -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(); @@ -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(); @@ -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 @@ -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; diff --git a/src/hotspot/share/prims/jniCheck.cpp b/src/hotspot/share/prims/jniCheck.cpp index 529e7cc4fe890..104804807573c 100644 --- a/src/hotspot/share/prims/jniCheck.cpp +++ b/src/hotspot/share/prims/jniCheck.cpp @@ -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 @@ -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) diff --git a/src/hotspot/share/prims/jvmtiEnter.xsl b/src/hotspot/share/prims/jvmtiEnter.xsl index 932c7a82fe04f..de8d1aebbc886 100644 --- a/src/hotspot/share/prims/jvmtiEnter.xsl +++ b/src/hotspot/share/prims/jvmtiEnter.xsl @@ -1,6 +1,6 @@