8328679: Improve comment for UNSAFE_ENTRY_SCOPED in unsafe.cpp

Reviewed-by: jvernee, dholmes
This commit is contained in:
Maurizio Cimadamore 2024-03-22 15:09:27 +00:00
parent f207aa94f9
commit 709410d8a4

View File

@ -76,25 +76,26 @@
#define UNSAFE_LEAF(result_type, header) \
JVM_LEAF(static result_type, header)
// Note that scoped accesses (cf. scopedMemoryAccess.cpp) can install
// an async handshake on the entry to an Unsafe method. When that happens,
// it is expected that we are not allowed to touch the underlying memory
// that might have gotten unmapped. Therefore, we check at the entry
// to unsafe functions, if we have such async exception conditions,
// and return immediately if that is the case.
// All memory access methods (e.g. getInt, copyMemory) must use this macro.
// We call these methods "scoped" methods, as access to these methods is
// typically governed by a "scope" (a MemorySessionImpl object), and no
// access is allowed when the scope is no longer alive.
//
// We can't have safepoints in this code.
// It would be problematic if an async exception handshake were installed later on
// during another safepoint in the function, but before the memory access happens,
// as the memory will be freed after the handshake is installed. We must notice
// the installed handshake and return early before doing the memory access to prevent
// accesses to freed memory.
// Closing a scope object (cf. scopedMemoryAccess.cpp) can install
// an async exception during a safepoint. When that happens,
// scoped methods are not allowed to touch the underlying memory (as that
// memory might have been released). Therefore, when entering a scoped method
// we check if an async exception has been installed, and return immediately
// if that is the case.
//
// Note also that we MUST do a scoped memory access in the VM (or Java) thread
// state. Since we rely on a handshake to check for threads that are accessing
// scoped memory, and we need the handshaking thread to wait until we get to a
// safepoint, in order to make sure we are not in the middle of accessing memory
// that is about to be freed. (i.e. there can be no UNSAFE_LEAF_SCOPED)
// As a rule, we disallow safepoints in the middle of a scoped method.
// If an async exception handshake were installed in such a safepoint,
// memory access might still occur before the handshake is honored by
// the accessing thread.
//
// Corollary: as threads in native state are considered to be at a safepoint,
// scoped methods must NOT be executed while in the native thread state.
// Because of this, there can be no UNSAFE_LEAF_SCOPED.
#define UNSAFE_ENTRY_SCOPED(result_type, header) \
JVM_ENTRY(static result_type, header) \
if (thread->has_async_exception_condition()) {return (result_type)0;}