From c46d60af17256b2475c2413c0e72bae3ea28a8dc Mon Sep 17 00:00:00 2001 From: Annabelle Huo Date: Tue, 21 Mar 2023 14:31:54 -0400 Subject: [PATCH] Remove setImmutableField on currentThread for JDK19 and up `Thread.currentThread` can be changed by JCL in JDK19 and up. Set it as volatile for JDK19 and up. Add `isJ9VMThreadCurrentThreadImmutable` in FrontEnd to query whether or not `Thread.currentThread` is immutable based on `JAVA_SPEC_VERSION`. Fixes #16258 Co-authored-by: Daryl Maier Signed-off-by: Annabelle Huo --- runtime/compiler/compile/J9SymbolReferenceTable.cpp | 9 ++++++++- runtime/compiler/env/VMJ9.cpp | 10 ++++++++++ runtime/compiler/env/VMJ9.h | 7 +++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/runtime/compiler/compile/J9SymbolReferenceTable.cpp b/runtime/compiler/compile/J9SymbolReferenceTable.cpp index 4d34b810d84..cbbe1428122 100644 --- a/runtime/compiler/compile/J9SymbolReferenceTable.cpp +++ b/runtime/compiler/compile/J9SymbolReferenceTable.cpp @@ -1356,7 +1356,14 @@ J9::SymbolReferenceTable::findOrCreateCurrentThreadSymbolRef() TR_J9VMBase *fej9 = (TR_J9VMBase *)(fe()); TR::Symbol * sym = TR::RegisterMappedSymbol::createMethodMetaDataSymbol(trHeapMemory(), "CurrentThread"); sym->setDataType(TR::Address); - sym->setImmutableField(); + if (fej9->isJ9VMThreadCurrentThreadImmutable()) + { + sym->setImmutableField(); + } + else + { + sym->setVolatile(); + } element(currentThreadSymbol) = new (trHeapMemory()) TR::SymbolReference(self(), currentThreadSymbol, sym); element(currentThreadSymbol)->setOffset(fej9->thisThreadGetCurrentThreadOffset()); } diff --git a/runtime/compiler/env/VMJ9.cpp b/runtime/compiler/env/VMJ9.cpp index 00cd757aad1..55d7b154b80 100644 --- a/runtime/compiler/env/VMJ9.cpp +++ b/runtime/compiler/env/VMJ9.cpp @@ -9376,6 +9376,16 @@ TR_J9VMBase::isSnapshotModeEnabled() #endif /* defined(J9VM_OPT_CRIU_SUPPORT) */ } +bool +TR_J9VMBase::isJ9VMThreadCurrentThreadImmutable() + { +#if JAVA_SPEC_VERSION >= 19 + return false; +#else + return true; +#endif /* JAVA_SPEC_VERSION >= 19 */ + } + // Native method bodies // #if defined(TR_HOST_X86) diff --git a/runtime/compiler/env/VMJ9.h b/runtime/compiler/env/VMJ9.h index b03ea9bc64c..60563a91ba2 100644 --- a/runtime/compiler/env/VMJ9.h +++ b/runtime/compiler/env/VMJ9.h @@ -1361,6 +1361,13 @@ class TR_J9VMBase : public TR_FrontEnd */ bool isSnapshotModeEnabled(); + /** + * \brief Answers whether or not Thread.currentThread() is immutable. + * + * \return True if Thread.currentThread() is immutable. + */ + virtual bool isJ9VMThreadCurrentThreadImmutable(); + protected: enum // _flags