Skip to content
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

Support macOS W^X #28

Open
wants to merge 2 commits into
base: macos-aarch64
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/hotspot/share/c1/c1_Runtime1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,9 +1258,14 @@ JRT_END
#else // DEOPTIMIZE_WHEN_PATCHING

JRT_ENTRY(void, Runtime1::patch_code(JavaThread* thread, 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, thread));

RegisterMap reg_map(thread, false);

NOT_PRODUCT(_patch_code_slowcase_cnt++;)

if (TracePatching) {
tty->print_cr("Deoptimizing because patch is needed");
}
Expand Down
7 changes: 6 additions & 1 deletion src/hotspot/share/gc/shared/barrierSetNMethod.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, 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,6 +29,7 @@
#include "gc/shared/barrierSetNMethod.hpp"
#include "logging/log.hpp"
#include "runtime/thread.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "utilities/debug.hpp"

int BarrierSetNMethod::disarmed_value() const {
Expand All @@ -48,6 +49,10 @@ 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()));

address return_address = *return_address_ptr;
CodeBlob* cb = CodeCache::find_blob(return_address);
assert(cb != NULL, "invariant");
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/interpreter/interpreterRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,9 @@ JRT_END


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

nmethod* nm = frequency_counter_overflow_inner(thread, branch_bcp);
assert(branch_bcp != NULL || nm == NULL, "always returns null for non OSR requests");
if (branch_bcp != NULL && nm != NULL) {
Expand Down
11 changes: 6 additions & 5 deletions src/hotspot/share/jvmci/jvmciCompilerToVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,12 @@ Handle JavaArgumentUnboxer::next_arg(BasicType expectedType) {
}

// Bring the JVMCI compiler thread into the VM state.
#define JVMCI_VM_ENTRY_MARK \
ThreadInVMfromNative __tiv(thread); \
ResetNoHandleMark rnhm; \
HandleMarkCleaner __hm(thread); \
Thread* THREAD = thread; \
#define JVMCI_VM_ENTRY_MARK \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
ThreadInVMfromNative __tiv(thread); \
ResetNoHandleMark rnhm; \
HandleMarkCleaner __hm(thread); \
Thread* THREAD = thread; \
debug_only(VMNativeEntryWrapper __vew;)

// Native method block that transitions current thread to '_thread_in_vm'.
Expand Down
5 changes: 5 additions & 0 deletions src/hotspot/share/opto/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#include "runtime/sharedRuntime.hpp"
#include "runtime/signature.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 @@ -1464,6 +1465,10 @@ address OptoRuntime::handle_exception_C(JavaThread* thread) {
// *THIS IS NOT RECOMMENDED PROGRAMMING STYLE*
//
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));

#ifndef PRODUCT
SharedRuntime::_rethrow_ctr++; // count rethrows
#endif
Expand Down
8 changes: 8 additions & 0 deletions src/hotspot/share/prims/jni.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012 Red Hat, Inc.
* Copyright (c) 2021, Azul Systems, 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 @@ -3793,6 +3794,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(thread, _thread_in_vm, _thread_in_native);
MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
} 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 @@ -3948,6 +3950,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());

if (!os::create_attached_thread(thread)) {
thread->smr_delete();
Expand Down Expand Up @@ -4021,6 +4024,7 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae
// needed.

ThreadStateTransition::transition(thread, _thread_in_vm, _thread_in_native);
MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));

// Perform any platform dependent FPU setup
os::setup_fpu();
Expand Down Expand Up @@ -4091,6 +4095,10 @@ jint JNICALL jni_DetachCurrentThread(JavaVM *vm) {
thread->exit(false, JavaThread::jni_detach);
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));

HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
return JNI_OK;
}
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/prims/jniCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ extern "C" { \
if (env != xenv) { \
NativeReportJNIFatalError(thr, warn_wrong_jnienv); \
} \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thr)); \
VM_ENTRY_BASE(result_type, header, thr)


Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/prims/jvmtiEnter.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ struct jvmtiInterface_1_ jvmti</xsl:text>
<xsl:if test="count(@impl)=0 or not(contains(@impl,'innative'))">
<xsl:text>JavaThread* current_thread = (JavaThread*)this_thread;</xsl:text>
<xsl:value-of select="$space"/>
<xsl:text>MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, current_thread));</xsl:text>
<xsl:value-of select="$space"/>
<xsl:text>ThreadInVMfromNative __tiv(current_thread);</xsl:text>
<xsl:value-of select="$space"/>
<xsl:text>VM_ENTRY_BASE(jvmtiError, </xsl:text>
Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/prims/jvmtiEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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));
ThreadInVMfromNative __tiv(current_thread);
VM_ENTRY_BASE(jvmtiError, JvmtiEnv::GetThreadLocalStorage , current_thread)
debug_only(VMNativeEntryWrapper __vew;)
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/prims/unsafe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ UNSAFE_ENTRY(void, Unsafe_CopyMemory0(JNIEnv *env, jobject unsafe, jobject srcOb
{
GuardUnsafeAccess guard(thread);
if (StubRoutines::unsafe_arraycopy() != NULL) {
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXExec, thread));
StubRoutines::UnsafeArrayCopy_stub()(src, dst, sz);
} else {
Copy::conjoint_memory_atomic(src, dst, sz);
Expand Down Expand Up @@ -457,12 +458,14 @@ UNSAFE_LEAF (void, Unsafe_WriteBack0(JNIEnv *env, jobject unsafe, jlong line)) {
}
#endif

MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXExec, Thread::current()));
assert(StubRoutines::data_cache_writeback() != NULL, "sanity");
(StubRoutines::DataCacheWriteback_stub())(addr_from_java(line));
} UNSAFE_END

static void doWriteBackSync0(bool is_pre)
{
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXExec, Thread::current()));
assert(StubRoutines::data_cache_writeback_sync() != NULL, "sanity");
(StubRoutines::DataCacheWritebackSync_stub())(is_pre);
}
Expand Down
5 changes: 3 additions & 2 deletions src/hotspot/share/prims/whitebox.inline.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, 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 @@ -31,7 +31,8 @@
// Entry macro to transition from JNI to VM state.

#define WB_ENTRY(result_type, header) JNI_ENTRY(result_type, header) \
ClearPendingJniExcCheck _clearCheck(env);
ClearPendingJniExcCheck _clearCheck(env); \
MACOS_AARCH64_ONLY(ThreadWXEnable _wx(WXWrite, thread));

#define WB_END JNI_END

Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/runtime/deoptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "runtime/stubRoutines.hpp"
#include "runtime/thread.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/vframe.hpp"
#include "runtime/vframeArray.hpp"
#include "runtime/vframe_hp.hpp"
Expand Down Expand Up @@ -2324,6 +2325,9 @@ Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int tr
}

Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* thread, jint trap_request, jint exec_mode) {
// Enable WXWrite: current function is called from methods compiled by C2 directly
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));

if (TraceDeoptimization) {
tty->print("Uncommon trap ");
}
Expand Down
12 changes: 11 additions & 1 deletion src/hotspot/share/runtime/interfaceSupport.inline.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, Azul Systems, 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 @@ -32,6 +33,7 @@
#include "runtime/safepointMechanism.inline.hpp"
#include "runtime/safepointVerifiers.hpp"
#include "runtime/thread.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/vmOperations.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/histogram.hpp"
Expand Down Expand Up @@ -368,6 +370,8 @@ class RuntimeHistogramElement : public HistogramElement {
#define VM_LEAF_BASE(result_type, header) \
TRACE_CALL(result_type, header) \
debug_only(NoHandleMark __hm;) \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, \
Thread::current())); \
os::verify_stack_alignment(); \
/* begin of body */

Expand All @@ -392,6 +396,7 @@ class RuntimeHistogramElement : public HistogramElement {

#define JRT_ENTRY(result_type, header) \
result_type header { \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
ThreadInVMfromJava __tiv(thread); \
VM_ENTRY_BASE(result_type, header, thread) \
debug_only(VMEntryWrapper __vew;)
Expand All @@ -418,6 +423,7 @@ class RuntimeHistogramElement : public HistogramElement {

#define JRT_ENTRY_NO_ASYNC(result_type, header) \
result_type header { \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
ThreadInVMfromJavaNoAsyncException __tiv(thread); \
VM_ENTRY_BASE(result_type, header, thread) \
debug_only(VMEntryWrapper __vew;)
Expand All @@ -427,6 +433,7 @@ class RuntimeHistogramElement : public HistogramElement {
#define JRT_BLOCK_ENTRY(result_type, header) \
result_type header { \
TRACE_CALL(result_type, header) \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
HandleMarkCleaner __hm(thread);

#define JRT_BLOCK \
Expand Down Expand Up @@ -456,6 +463,7 @@ extern "C" { \
result_type JNICALL header { \
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
assert( !VerifyJNIEnvThread || (thread == Thread::current()), "JNIEnv is only valid in same thread"); \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
ThreadInVMfromNative __tiv(thread); \
debug_only(VMNativeEntryWrapper __vew;) \
VM_ENTRY_BASE(result_type, header, thread)
Expand All @@ -480,6 +488,7 @@ extern "C" { \
extern "C" { \
result_type JNICALL header { \
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
ThreadInVMfromNative __tiv(thread); \
debug_only(VMNativeEntryWrapper __vew;) \
VM_ENTRY_BASE(result_type, header, thread)
Expand All @@ -489,6 +498,7 @@ extern "C" { \
extern "C" { \
result_type JNICALL header { \
JavaThread* thread = JavaThread::current(); \
MACOS_AARCH64_ONLY(ThreadWXEnable __wx(WXWrite, thread)); \
ThreadInVMfromNative __tiv(thread); \
debug_only(VMNativeEntryWrapper __vew;) \
VM_ENTRY_BASE(result_type, header, thread)
Expand Down
8 changes: 6 additions & 2 deletions src/hotspot/share/runtime/javaCalls.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, Azul Systems, 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 @@ -78,7 +79,6 @@ JavaCallWrapper::JavaCallWrapper(const methodHandle& callee_method, Handle recei
}
}


// Make sure to set the oop's after the thread transition - since we can block there. No one is GC'ing
// the JavaCallWrapper before the entry frame is on the stack.
_callee_method = callee_method();
Expand Down Expand Up @@ -112,12 +112,16 @@ JavaCallWrapper::JavaCallWrapper(const methodHandle& callee_method, Handle recei
if (_anchor.last_Java_sp() == NULL) {
_thread->record_base_of_stack_pointer();
}

MACOS_AARCH64_ONLY(_thread->enable_wx(WXExec));
}


JavaCallWrapper::~JavaCallWrapper() {
assert(_thread == JavaThread::current(), "must still be the same thread");

MACOS_AARCH64_ONLY(_thread->enable_wx(WXWrite));

// restore previous handle block & Java frame linkage
JNIHandleBlock *_old_handles = _thread->active_handles();
_thread->set_active_handles(_handles);
Expand Down
10 changes: 10 additions & 0 deletions src/hotspot/share/runtime/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ enum ThreadPriority { // JLS 20.20.1-3
CriticalPriority = 11 // Critical thread priority
};

enum WXMode {
WXWrite,
WXExec
};

// Executable parameter flag for os::commit_memory() and
// os::commit_memory_or_exit().
const bool ExecMem = true;
Expand Down Expand Up @@ -898,6 +903,11 @@ class os: AllStatic {
bool _done;
};

#if defined(__APPLE__) && defined(AARCH64)
// Enables write or execute access to writeable and executable pages.
static void current_thread_enable_wx(WXMode mode);
#endif // __APPLE__ && AARCH64

#ifndef _WINDOWS
// Suspend/resume support
// Protocol:
Expand Down
11 changes: 11 additions & 0 deletions src/hotspot/share/runtime/safefetch.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,27 @@
#define SHARE_RUNTIME_SAFEFETCH_INLINE_HPP

#include "runtime/stubRoutines.hpp"
#include "runtime/threadWXSetters.inline.hpp"

// Safefetch allows to load a value from a location that's not known
// to be valid. If the load causes a fault, the error value is returned.
inline int SafeFetch32(int* adr, int errValue) {
assert(StubRoutines::SafeFetch32_stub(), "stub not yet generated");
#if defined(__APPLE__) && defined(AARCH64)
Thread* thread = Thread::current_or_null_safe();
assert(thread != NULL, "required for W^X management");
ThreadWXEnable wx(WXExec, thread);
#endif // __APPLE__ && AARCH64
return StubRoutines::SafeFetch32_stub()(adr, errValue);
}

inline intptr_t SafeFetchN(intptr_t* adr, intptr_t errValue) {
assert(StubRoutines::SafeFetchN_stub(), "stub not yet generated");
#if defined(__APPLE__) && defined(AARCH64)
Thread* thread = Thread::current_or_null_safe();
assert(thread != NULL, "required for W^X management");
ThreadWXEnable wx(WXExec, thread);
#endif // __APPLE__ && AARCH64
return StubRoutines::SafeFetchN_stub()(adr, errValue);
}

Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/runtime/safepoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "runtime/synchronizer.hpp"
#include "runtime/thread.inline.hpp"
#include "runtime/threadSMR.hpp"
#include "runtime/threadWXSetters.inline.hpp"
#include "runtime/timerTrace.hpp"
#include "services/runtimeService.hpp"
#include "utilities/events.hpp"
Expand Down Expand Up @@ -850,6 +851,9 @@ void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) {
assert(thread->is_Java_thread(), "polling reference encountered by VM thread");
assert(thread->thread_state() == _thread_in_Java, "should come from Java code");

// Enable WXWrite: the function is called implicitly from java code.
MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));

if (log_is_enabled(Info, safepoint, stats)) {
Atomic::inc(&_nof_threads_hit_polling_page);
}
Expand Down
Loading