From 8159d04cdb5c07b0cdcf4e0ebde575636f1b7c77 Mon Sep 17 00:00:00 2001 From: Jason Feng Date: Mon, 22 Jul 2024 15:54:38 -0400 Subject: [PATCH] Add J9HookInterface.J9HookUnreserve() to clear flag J9HOOK_FLAG_RESERVED J9HookRegister() sets flags J9HOOK_FLAG_HOOKED | J9HOOK_FLAG_RESERVED, J9HookUnregister() only clears J9HOOK_FLAG_HOOKED, add J9HookUnreserve() to clear J9HOOK_FLAG_RESERVED. This supports an event to be registered, unregistered/unreserved, and disabled. Otherwise, an unregistered event can't be disabled by J9HookDisable() because of the presence of J9HOOK_FLAG_RESERVED. Signed-off-by: Jason Feng --- include_core/omrhookable.h | 1 + util/hookable/hookable.cpp | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include_core/omrhookable.h b/include_core/omrhookable.h index 7231960978..fe7ae9b197 100644 --- a/include_core/omrhookable.h +++ b/include_core/omrhookable.h @@ -53,6 +53,7 @@ typedef struct J9HookInterface { void (*J9HookDispatch)(struct J9HookInterface **hookInterface, uintptr_t eventNum, void *eventData); intptr_t (*J9HookDisable)(struct J9HookInterface **hookInterface, uintptr_t eventNum); intptr_t (*J9HookReserve)(struct J9HookInterface **hookInterface, uintptr_t eventNum); + void (*J9HookUnreserve)(struct J9HookInterface **hookInterface, uintptr_t eventNum); intptr_t (*J9HookRegister)(struct J9HookInterface **hookInterface, uintptr_t eventNum, J9HookFunction function, void *userData, ...); intptr_t (*J9HookRegisterWithCallSite)(struct J9HookInterface **hookInterface, uintptr_t eventNum, J9HookFunction function, const char *callsite, void *userData, ...); void (*J9HookUnregister)(struct J9HookInterface **hookInterface, uintptr_t eventNum, J9HookFunction function, void *userData); diff --git a/util/hookable/hookable.cpp b/util/hookable/hookable.cpp index 92cf0634fd..db31414f63 100644 --- a/util/hookable/hookable.cpp +++ b/util/hookable/hookable.cpp @@ -41,6 +41,7 @@ static intptr_t J9HookDisable(struct J9HookInterface **hookInterface, uintptr_t static intptr_t J9HookIsEnabled(struct J9HookInterface **hookInterface, uintptr_t taggedEventNum); static void J9HookDispatch(struct J9HookInterface **hookInterface, uintptr_t taggedEventNum, void *eventData); static intptr_t J9HookReserve(struct J9HookInterface **hookInterface, uintptr_t taggedEventNum); +static void J9HookUnreserve(struct J9HookInterface **hookInterface, uintptr_t taggedEventNum); static uintptr_t J9HookAllocateAgentID(struct J9HookInterface **hookInterface); static void J9HookDeallocateAgentID(struct J9HookInterface **hookInterface, uintptr_t agentID); @@ -48,6 +49,7 @@ static const J9HookInterface hookFunctionTable = { J9HookDispatch, J9HookDisable, J9HookReserve, + J9HookUnreserve, J9HookRegister, J9HookRegisterWithCallSite, J9HookUnregister, @@ -345,6 +347,26 @@ J9HookReserve(struct J9HookInterface **hookInterface, uintptr_t taggedEventNum) return rc; } +/* + * Clear the J9HOOK_FLAG_RESERVED flag from an event. + * This flag can be removed regardless of current flag(s) of the event, + * J9HOOK_FLAG_RESERVED, J9HOOK_FLAG_HOOKED or J9HOOK_FLAG_DISABLED. + * + * This function should not be called directly. It should be called through the hook interface. + */ +static void +J9HookUnreserve(struct J9HookInterface **hookInterface, uintptr_t taggedEventNum) +{ + J9CommonHookInterface *commonInterface = (J9CommonHookInterface *)hookInterface; + uintptr_t eventNum = taggedEventNum & J9HOOK_EVENT_NUM_MASK; + + omrthread_monitor_enter(commonInterface->lock); + + HOOK_FLAGS(commonInterface, eventNum) &= ~J9HOOK_FLAG_RESERVED; + + omrthread_monitor_exit(commonInterface->lock); +} + static intptr_t J9HookRegisterWithCallSitePrivate(struct J9HookInterface **hookInterface, uintptr_t taggedEventNum, J9HookFunction function, const char *callsite, void *userData, uintptr_t agentID) {