Skip to content

Commit

Permalink
Merge pull request #16530 from knn-k/aarch64jit_write_protect
Browse files Browse the repository at this point in the history
Rewrite pthread_jit_write_protect_np()
  • Loading branch information
keithc-ca committed Jan 10, 2023
2 parents cc263cc + 3b7ff1e commit e71acbd
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 290 deletions.
14 changes: 3 additions & 11 deletions runtime/codert_vm/cnathelp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2022 IBM Corp. and others
* Copyright (c) 1991, 2023 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -35,10 +35,6 @@
#include "MethodMetaData.h"
#include "ut_j9codertvm.h"

#if defined(OSX) && defined(AARCH64)
#include <pthread.h> // for pthread_jit_write_protect_np
#endif

#undef DEBUG

extern "C" {
Expand Down Expand Up @@ -2241,9 +2237,7 @@ old_slow_jitResolveInterfaceMethod(J9VMThread *currentThread)
}
goto retry;
} else {
#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
indexAndLiteralsEA[2] = (UDATA)interfaceClass;
UDATA methodIndex = methodIndexAndArgCount >> J9_ITABLE_INDEX_SHIFT;
UDATA iTableOffset = 0;
Expand All @@ -2262,9 +2256,7 @@ old_slow_jitResolveInterfaceMethod(J9VMThread *currentThread)
iTableOffset = (methodIndex * sizeof(UDATA)) + sizeof(J9ITable);
}
indexAndLiteralsEA[3] = iTableOffset;
#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();
JIT_RETURN_UDATA(1);
}
done:
Expand Down
14 changes: 3 additions & 11 deletions runtime/codert_vm/decomp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 1991, 2022 IBM Corp. and others
* Copyright (c) 1991, 2023 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -38,10 +38,6 @@
#include "VMHelpers.hpp"
#include "OMR/Bytes.hpp"

#if defined(OSX) && defined(AARCH64)
#include <pthread.h> // for pthread_jit_write_protect_np
#endif

extern "C" {

/* Generic rounding macro - result is a UDATA */
Expand Down Expand Up @@ -262,9 +258,7 @@ jitResetAllMethods(J9VMThread *currentThread)
J9Method *method = clazz->ramMethods;
U_32 methodCount = clazz->romClass->romMethodCount;

#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();

while (methodCount != 0) {
UDATA extra = (UDATA)method->extra;
Expand All @@ -286,9 +280,7 @@ jitResetAllMethods(J9VMThread *currentThread)
methodCount -= 1;
}

#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

clazz = vmFuncs->allClassesNextDo(&state);
}
Expand Down
22 changes: 5 additions & 17 deletions runtime/compiler/aarch64/codegen/CallSnippet.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2022 IBM Corp. and others
* Copyright (c) 2019, 2023 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -35,10 +35,6 @@
#include "il/Node.hpp"
#include "il/Node_inlines.hpp"

#if defined(OSX)
#include <pthread.h> // for pthread_jit_write_protect_np
#endif

static uint8_t *storeArgumentItem(TR::InstOpCode::Mnemonic op, uint8_t *buffer, TR::RealRegister *reg, int32_t offset, TR::CodeGenerator *cg)
{
TR::RealRegister *stackPtr = cg->getStackPointerRegister();
Expand Down Expand Up @@ -927,9 +923,7 @@ uint8_t *TR::ARM64CallSnippet::generateVIThunk(TR::Node *callNode, int32_t argSi
cg->getDebug()->getName(dataType));
}

#if defined(OSX)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();

dispatcher = (uintptr_t)cg->symRefTab()->findOrCreateRuntimeHelper(helper)->getMethodAddress();

Expand Down Expand Up @@ -965,9 +959,7 @@ uint8_t *TR::ARM64CallSnippet::generateVIThunk(TR::Node *callNode, int32_t argSi
arm64CodeSync(thunk, codeSize);
#endif

#if defined(OSX)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

return returnValue;
}
Expand Down Expand Up @@ -1007,9 +999,7 @@ TR_J2IThunk *TR::ARM64CallSnippet::generateInvokeExactJ2IThunk(TR::Node *callNod
cg->getDebug()->getName(dataType));
}

#if defined(OSX)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();

dispatcher = (intptr_t)cg->symRefTab()->findOrCreateRuntimeHelper(helper)->getMethodAddress();

Expand Down Expand Up @@ -1042,9 +1032,7 @@ TR_J2IThunk *TR::ARM64CallSnippet::generateInvokeExactJ2IThunk(TR::Node *callNod
arm64CodeSync(thunk->entryPoint(), codeSize);
#endif

#if defined(OSX)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

return thunk;
}
Expand Down
54 changes: 13 additions & 41 deletions runtime/compiler/aarch64/runtime/Recomp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2022 IBM Corp. and others
* Copyright (c) 2019, 2023 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -29,10 +29,6 @@
#include "runtime/CodeCacheManager.hpp"
#include "runtime/J9Runtime.hpp"

#if defined(OSX)
#include <pthread.h> // for pthread_jit_write_protect_np
#endif

extern void arm64CodeSync(uint8_t *, uint32_t);

#define DEBUG_ARM64_RECOMP false
Expand Down Expand Up @@ -117,9 +113,7 @@ void J9::Recompilation::fixUpMethodCode(void *startPC)
newInstr, jitEntry, *jitEntry); fflush(stdout);
}

#if defined(OSX)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
// Other thread might try to do the same thing at the same time.
while ((preserved & B_INSTR_MASK) != TR::InstOpCode::getOpCodeBinaryEncoding(TR::InstOpCode::b))
{
Expand Down Expand Up @@ -147,9 +141,7 @@ void J9::Recompilation::fixUpMethodCode(void *startPC)
#endif
preserved = *jitEntry;
}
#if defined(OSX)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();
}
}

Expand Down Expand Up @@ -187,28 +179,20 @@ void J9::Recompilation::methodHasBeenRecompiled(void *oldStartPC, void *newStart
printf("\tsampling recomp, change instruction location (%p) of sampling branch to branch encoding 0x%x (to TR_ARM64samplingPatchCallSite)\n",
patchAddr, newInstr); fflush(stdout);
}
#if defined(OSX)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
*patchAddr = newInstr;
arm64CodeSync((uint8_t *)patchAddr, ARM64_INSTRUCTION_LENGTH);
#if defined(OSX)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

fixUpMethodCode(oldStartPC);

bytesToSaveAtStart = getJitEntryOffset(linkageInfo) + ARM64_INSTRUCTION_LENGTH;
}

bool codeMemoryWasAlreadyReleased = linkageInfo->hasBeenRecompiled(); // HCR - can recompile the same body twice
#if defined(OSX)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
linkageInfo->setHasBeenRecompiled();
#if defined(OSX)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

if (linkageInfo->isSamplingMethodBody() && !codeMemoryWasAlreadyReleased)
{
Expand Down Expand Up @@ -251,14 +235,10 @@ void J9::Recompilation::methodCannotBeRecompiled(void *oldStartPC, TR_FrontEnd *
printf("oldStartPC %p, patchAddr %p, target %lx\n", oldStartPC, patchAddr, target); fflush(stdout);
}

#if defined(OSX)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
*patchAddr = encodeDistanceInBranchInstruction(TR::InstOpCode::b, distance);
arm64CodeSync((uint8_t *)patchAddr, ARM64_INSTRUCTION_LENGTH);
#if defined(OSX)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

if (!methodInfo->hasBeenReplaced()) // HCR: VM presumably already has the method in its proper state
fej9->revertToInterpreted(methodInfo->getMethodInfo());
Expand All @@ -280,24 +260,16 @@ void J9::Recompilation::methodCannotBeRecompiled(void *oldStartPC, TR_FrontEnd *
printf("MethodCannotBeRecompiled sampling recomp sync compilation restoring preserved jitEntry of 0x%x at location %p\n",
*((int32_t *)((uint8_t *)oldStartPC + OFFSET_SAMPLING_PRESERVED_FROM_STARTPC)), startByte); fflush(stdout);
}
#if defined(OSX)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
*startByte = *((int32_t *)((uint8_t *)oldStartPC + OFFSET_SAMPLING_PRESERVED_FROM_STARTPC));
arm64CodeSync((uint8_t *)startByte, 4);
#if defined(OSX)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();
}
}

#if defined(OSX)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
linkageInfo->setHasFailedRecompilation();
#if defined(OSX)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();
}

void J9::Recompilation::invalidateMethodBody(void *startPC, TR_FrontEnd *fe)
Expand Down
35 changes: 8 additions & 27 deletions runtime/compiler/control/CompilationThread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corp. and others
* Copyright (c) 2000, 2023 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -147,10 +147,6 @@ static void printCompFailureInfo(TR::Compilation * comp, const char * reason);
#include "runtime/IProfiler.hpp"
#endif

#if defined(OSX) && defined(AARCH64)
#include <pthread.h> // for pthread_jit_write_protect_np
#endif

IDATA J9THREAD_PROC compilationThreadProc(void *jitconfig);
IDATA J9THREAD_PROC protectedCompilationThreadProc(J9PortLibrary *portLib, TR::CompilationInfoPerThread*compInfoPT/*void *vmthread*/);

Expand Down Expand Up @@ -5061,13 +5057,9 @@ TR::CompilationInfo::addMethodToBeCompiled(TR::IlGeneratorMethodDetails & detail
if (pc)
{
J9::PrivateLinkage::LinkageInfo *linkageInfo = J9::PrivateLinkage::LinkageInfo::get(pc);
#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
linkageInfo->setIsBeingRecompiled(); // mark that we try to compile it
#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

// Update persistentMethodInfo with the level we want to compile to
//
Expand Down Expand Up @@ -8303,11 +8295,9 @@ TR::CompilationInfoPerThreadBase::compile(J9VMThread * vmThread,
reloRuntime);
}

#if defined(OSX) && defined(AARCH64)
// Re-acquire execution permission of JIT code cache for this thread
// regardless of the previous protection status
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

vmThread->omrVMThread->vmState = oldState;
vmThread->jitMethodToBeCompiled = NULL;
Expand Down Expand Up @@ -9413,9 +9403,7 @@ TR::CompilationInfoPerThreadBase::performAOTLoad(
);
}

#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
TR_MethodMetaData *metaData = installAotCachedMethod(
vmThread,
_methodBeingCompiled->_aotCodeToBeRelocated,
Expand All @@ -9426,9 +9414,7 @@ TR::CompilationInfoPerThreadBase::performAOTLoad(
_methodBeingCompiled,
compiler
);
#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

_methodBeingCompiled->_newStartPC = metaData ? reinterpret_cast<void *>(metaData->startPC) : 0;

Expand Down Expand Up @@ -9896,10 +9882,7 @@ TR::CompilationInfoPerThreadBase::compile(
);
}


#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
// Put a metaData pointer into the Code Cache Header(s).
//
uint8_t *warmMethodHeader = compiler->cg()->getBinaryBufferStart() - sizeof(OMR::CodeCacheMethodHeader);
Expand All @@ -9917,9 +9900,7 @@ TR::CompilationInfoPerThreadBase::compile(
// and in fact it would be wrong to do so because code during chtable.commit is
// expecting something in the compiler object
}
#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();

setMetadata(metaData);

Expand Down
14 changes: 3 additions & 11 deletions runtime/compiler/env/J2IThunk.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2022 IBM Corp. and others
* Copyright (c) 2000, 2023 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -32,10 +32,6 @@
#include "env/VMJ9.h"
#include "env/VerboseLog.hpp"

#if defined(OSX) && defined(AARCH64)
#include <pthread.h> // for pthread_jit_write_protect_np
#endif

static int32_t
computeSignatureLength(char *signature)
{
Expand Down Expand Up @@ -68,15 +64,11 @@ TR_J2IThunk::allocate(
{
result = (TR_J2IThunk*)cg->allocateCodeMemory(totalSize, true, false);
}
#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(0);
#endif
omrthread_jit_write_protect_disable();
result->_codeSize = codeSize;
result->_totalSize = totalSize;
thunkTable->getTerseSignature(result->terseSignature(), terseSignatureBufLength, signature);
#if defined(OSX) && defined(AARCH64)
pthread_jit_write_protect_np(1);
#endif
omrthread_jit_write_protect_enable();
return result;
}

Expand Down

0 comments on commit e71acbd

Please sign in to comment.