From bc29c86cdfb7278901770cfefc4e9cb1fb81d2b3 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 15 Nov 2026 18:38:46 +0000 Subject: [PATCH] src: share callback lookup between callback methods DynamicLibrary::UnregisterCallback(), RefCallback(), and UnrefCallback() each duplicated the closed-library check, pointer argument validation, and callback map lookup. Move them into a shared FindCallback() helper. Assisted-by: Claude Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/67496 Reviewed-By: Vladimir Morozov Reviewed-By: Yagiz Nizipli --- src/node_ffi.cc | 110 +++++++++++++++--------------------------------- src/node_ffi.h | 13 +++++- 2 files changed, 46 insertions(+), 77 deletions(-) diff --git a/src/node_ffi.cc b/src/node_ffi.cc index 9b0703b84e9..d32a33f0912 100644 --- a/src/node_ffi.cc +++ b/src/node_ffi.cc @@ -1339,25 +1339,27 @@ void DynamicLibrary::RegisterCallback(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, ret)); } -void DynamicLibrary::UnregisterCallback( - const FunctionCallbackInfo& args) { +DynamicLibrary* DynamicLibrary::FindCallback( + const FunctionCallbackInfo& args, + bool require_function, + CallbackMap::iterator* out) { Environment* env = Environment::GetCurrent(args); DynamicLibrary* lib = Unwrap(args.This()); if (lib->is_closed()) { THROW_ERR_FFI_LIBRARY_CLOSED(env); - return; + return nullptr; } if (args.Length() < 1 || !args[0]->IsBigInt()) { THROW_ERR_INVALID_ARG_TYPE(env, "The first argument must be a bigint"); - return; + return nullptr; } uintptr_t raw_ptr; if (!GetValidatedPointerAddress(env, args[0], "first argument") .To(&raw_ptr)) { - return; + return nullptr; } void* ptr = reinterpret_cast(raw_ptr); @@ -1365,6 +1367,27 @@ void DynamicLibrary::UnregisterCallback( if (existing == lib->callbacks_.end()) { THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found"); + return nullptr; + } + + // The callback function may already have been collected after a previous + // unrefCallback() call. The persistent handle is empty in that case, and + // ClearWeak() or SetWeak() on an empty handle dereferences a null slot. + // There is also no function left to make strong again. + if (require_function && existing->second->fn.IsEmpty()) { + THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found"); + return nullptr; + } + + *out = existing; + return lib; +} + +void DynamicLibrary::UnregisterCallback( + const FunctionCallbackInfo& args) { + CallbackMap::iterator existing; + DynamicLibrary* lib = FindCallback(args, false, &existing); + if (lib == nullptr) { return; } @@ -1377,82 +1400,17 @@ void DynamicLibrary::UnregisterCallback( } void DynamicLibrary::RefCallback(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - DynamicLibrary* lib = Unwrap(args.This()); - - if (lib->is_closed()) { - THROW_ERR_FFI_LIBRARY_CLOSED(env); - return; + CallbackMap::iterator existing; + if (FindCallback(args, true, &existing) != nullptr) { + existing->second->fn.ClearWeak(); } - - if (args.Length() < 1 || !args[0]->IsBigInt()) { - THROW_ERR_INVALID_ARG_TYPE(env, "The first argument must be a bigint"); - return; - } - - uintptr_t raw_ptr; - if (!GetValidatedPointerAddress(env, args[0], "first argument") - .To(&raw_ptr)) { - return; - } - - void* ptr = reinterpret_cast(raw_ptr); - auto existing = lib->callbacks_.find(ptr); - - if (existing == lib->callbacks_.end()) { - THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found"); - return; - } - - // The callback function may already have been collected after a previous - // unrefCallback() call. The persistent handle is empty in that case, and - // ClearWeak() on an empty handle dereferences a null slot. There is also no - // function left to make strong again. - if (existing->second->fn.IsEmpty()) { - THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found"); - return; - } - - existing->second->fn.ClearWeak(); } void DynamicLibrary::UnrefCallback(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - DynamicLibrary* lib = Unwrap(args.This()); - - if (lib->is_closed()) { - THROW_ERR_FFI_LIBRARY_CLOSED(env); - return; + CallbackMap::iterator existing; + if (FindCallback(args, true, &existing) != nullptr) { + existing->second->fn.SetWeak(); } - - if (args.Length() < 1 || !args[0]->IsBigInt()) { - THROW_ERR_INVALID_ARG_TYPE(env, "The first argument must be a bigint"); - return; - } - - uintptr_t raw_ptr; - if (!GetValidatedPointerAddress(env, args[0], "first argument") - .To(&raw_ptr)) { - return; - } - - void* ptr = reinterpret_cast(raw_ptr); - auto existing = lib->callbacks_.find(ptr); - - if (existing == lib->callbacks_.end()) { - THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found"); - return; - } - - // The callback function may already have been collected by a previous - // unrefCallback() call. The persistent handle is empty in that case, and - // SetWeak() on an empty handle dereferences a null slot. - if (existing->second->fn.IsEmpty()) { - THROW_ERR_INVALID_ARG_VALUE(env, "Callback not found"); - return; - } - - existing->second->fn.SetWeak(); } Local DynamicLibrary::GetConstructorTemplate( diff --git a/src/node_ffi.h b/src/node_ffi.h index 86737273e28..969491ff545 100644 --- a/src/node_ffi.h +++ b/src/node_ffi.h @@ -158,6 +158,17 @@ class DynamicLibrary : public BaseObject { SET_SELF_SIZE(DynamicLibrary) private: + using CallbackMap = std::unordered_map>; + + // Validates that args[0] is a pointer to a callback registered with the + // receiving library and stores its entry in `out`. When `require_function` + // is true, a callback whose function has already been collected is also + // rejected. Returns the library, or nullptr after throwing. + static DynamicLibrary* FindCallback( + const v8::FunctionCallbackInfo& args, + bool require_function, + CallbackMap::iterator* out); + void Close(); v8::Maybe ResolveSymbol(Environment* env, const std::string& name); struct PreparedFunction { @@ -194,7 +205,7 @@ class DynamicLibrary : public BaseObject { // FFIFunctionInfo. std::unordered_map> function_wrappers_; std::shared_ptr> pointer_functions_; - std::unordered_map> callbacks_; + CallbackMap callbacks_; }; void GetInt8(const v8::FunctionCallbackInfo& args);