From 4db947a3aaafb8de82725c8304d7d240256fb449 Mon Sep 17 00:00:00 2001 From: Nicola Del Gobbo Date: Tue, 17 Nov 2026 21:23:04 +0100 Subject: [PATCH] Add tests for Node-API thread-safe functions and uncaught exceptions - Implemented `test_ref_then_set.c` and `test_set_then_ref.c` to test reference handling in Node-API. - Created `binding.c` for testing thread-safe functions with libuv's threading library. - Added `binding.gyp` for building the thread-safe function tests. - Developed `test.js` to validate the behavior of thread-safe functions and their interactions with JavaScript callbacks. - Introduced `test_uncaught_exception.c` and corresponding JavaScript tests to verify uncaught exception handling in Node-API. - Added `test_uv_loop` to test the integration of Node-API with the libuv event loop. - Created `test_uv_threadpool_size` to validate the behavior of the thread pool size in Node-API. - Enhanced error handling and assertions in the async work implementation. --- .github/workflows/ci.yml | 15 +- CMakeLists.txt | 138 ++++-- CMakePresets.json | 17 + README.md | 91 +++- cmake/expect_abort.cmake | 18 + host/bootstrap.js | 109 +++-- host/main.c | 252 ++++++++--- include/qjsnapi.h | 48 ++- src/async.c | 167 ++++++++ src/env.c | 149 ++++++- src/lifetime.c | 27 +- src/loop.c | 136 ++++++ src/node_api.c | 56 +++ src/qjsnapi_internal.h | 76 +++- src/stubs_generated.c | 73 +--- src/tsfn.c | 397 ++++++++++++++++++ test/lsan.supp | 28 +- test/node/README.md | 53 ++- test/node/js-native-api/tests.cmake | 17 +- test/node/node-api/test_async/binding.gyp | 8 + .../node-api/test_async/test-async-hooks.js | 60 +++ test/node/node-api/test_async/test-loop.js | 14 + .../node/node-api/test_async/test-uncaught.js | 18 + test/node/node-api/test_async/test.js | 25 ++ test/node/node-api/test_async/test_async.c | 216 ++++++++++ .../test_async_cleanup_hook/binding.c | 101 +++++ .../test_async_cleanup_hook/binding.gyp | 9 + .../node-api/test_async_cleanup_hook/test.js | 5 + .../node-api/test_callback_scope/binding.c | 119 ++++++ .../node-api/test_callback_scope/binding.gyp | 9 + .../test_callback_scope/test-async-hooks.js | 39 ++ .../test_callback_scope/test-resolve-async.js | 6 + .../node/node-api/test_callback_scope/test.js | 17 + .../node/node-api/test_cleanup_hook/binding.c | 49 +++ .../node-api/test_cleanup_hook/binding.gyp | 9 + test/node/node-api/test_cleanup_hook/test.js | 12 + test/node/node-api/test_fatal/binding.gyp | 8 + test/node/node-api/test_fatal/test.js | 18 + test/node/node-api/test_fatal/test2.js | 18 + test/node/node-api/test_fatal/test_fatal.c | 48 +++ test/node/node-api/test_fatal/test_threads.js | 20 + .../test_fatal/test_threads_report.js | 36 ++ test/node/node-api/test_instance_data/addon.c | 22 + .../node-api/test_instance_data/binding.gyp | 24 ++ test/node/node-api/test_instance_data/test.js | 50 +++ .../test_instance_data/test_instance_data.c | 206 +++++++++ .../test_instance_data/test_ref_then_set.c | 10 + .../test_instance_data/test_set_then_ref.c | 10 + .../test_threadsafe_function/binding.c | 350 +++++++++++++++ .../test_threadsafe_function/binding.gyp | 22 + .../node-api/test_threadsafe_function/test.js | 225 ++++++++++ .../test_legacy_uncaught_exception.js | 22 + .../test_uncaught_exception.c | 62 +++ .../test_uncaught_exception.js | 7 + .../test_uncaught_exception_v9.js | 8 + .../uncaught_exception.js | 31 ++ test/node/node-api/test_uv_loop/binding.gyp | 8 + test/node/node-api/test_uv_loop/test.js | 5 + .../node-api/test_uv_loop/test_uv_loop.cc | 89 ++++ .../test_uv_threadpool_size/binding.gyp | 8 + .../test_uv_threadpool_size/node-options.js | 28 ++ .../node-api/test_uv_threadpool_size/test.js | 7 + .../test_uv_threadpool_size.c | 189 +++++++++ test/node/node-api/tests.cmake | 90 +++- test/unit/test_embed.c | 17 +- tools/sync_node_tests.py | 210 ++++++++- 66 files changed, 4127 insertions(+), 304 deletions(-) create mode 100644 cmake/expect_abort.cmake create mode 100644 src/async.c create mode 100644 src/loop.c create mode 100644 src/tsfn.c create mode 100644 test/node/node-api/test_async/binding.gyp create mode 100644 test/node/node-api/test_async/test-async-hooks.js create mode 100644 test/node/node-api/test_async/test-loop.js create mode 100644 test/node/node-api/test_async/test-uncaught.js create mode 100644 test/node/node-api/test_async/test.js create mode 100644 test/node/node-api/test_async/test_async.c create mode 100644 test/node/node-api/test_async_cleanup_hook/binding.c create mode 100644 test/node/node-api/test_async_cleanup_hook/binding.gyp create mode 100644 test/node/node-api/test_async_cleanup_hook/test.js create mode 100644 test/node/node-api/test_callback_scope/binding.c create mode 100644 test/node/node-api/test_callback_scope/binding.gyp create mode 100644 test/node/node-api/test_callback_scope/test-async-hooks.js create mode 100644 test/node/node-api/test_callback_scope/test-resolve-async.js create mode 100644 test/node/node-api/test_callback_scope/test.js create mode 100644 test/node/node-api/test_cleanup_hook/binding.c create mode 100644 test/node/node-api/test_cleanup_hook/binding.gyp create mode 100644 test/node/node-api/test_cleanup_hook/test.js create mode 100644 test/node/node-api/test_fatal/binding.gyp create mode 100644 test/node/node-api/test_fatal/test.js create mode 100644 test/node/node-api/test_fatal/test2.js create mode 100644 test/node/node-api/test_fatal/test_fatal.c create mode 100644 test/node/node-api/test_fatal/test_threads.js create mode 100644 test/node/node-api/test_fatal/test_threads_report.js create mode 100644 test/node/node-api/test_instance_data/addon.c create mode 100644 test/node/node-api/test_instance_data/binding.gyp create mode 100644 test/node/node-api/test_instance_data/test.js create mode 100644 test/node/node-api/test_instance_data/test_instance_data.c create mode 100644 test/node/node-api/test_instance_data/test_ref_then_set.c create mode 100644 test/node/node-api/test_instance_data/test_set_then_ref.c create mode 100644 test/node/node-api/test_threadsafe_function/binding.c create mode 100644 test/node/node-api/test_threadsafe_function/binding.gyp create mode 100644 test/node/node-api/test_threadsafe_function/test.js create mode 100644 test/node/node-api/test_threadsafe_function/test_legacy_uncaught_exception.js create mode 100644 test/node/node-api/test_threadsafe_function/test_uncaught_exception.c create mode 100644 test/node/node-api/test_threadsafe_function/test_uncaught_exception.js create mode 100644 test/node/node-api/test_threadsafe_function/test_uncaught_exception_v9.js create mode 100644 test/node/node-api/test_threadsafe_function/uncaught_exception.js create mode 100644 test/node/node-api/test_uv_loop/binding.gyp create mode 100644 test/node/node-api/test_uv_loop/test.js create mode 100644 test/node/node-api/test_uv_loop/test_uv_loop.cc create mode 100644 test/node/node-api/test_uv_threadpool_size/binding.gyp create mode 100644 test/node/node-api/test_uv_threadpool_size/node-options.js create mode 100644 test/node/node-api/test_uv_threadpool_size/test.js create mode 100644 test/node/node-api/test_uv_threadpool_size/test_uv_threadpool_size.c diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ece8a80..f228e1e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,13 +22,17 @@ env: # UBSan findings fail the test (-fno-sanitize-recover is set by CMake too). UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1 ASAN_OPTIONS: detect_leaks=1:strict_string_checks=1 + # A data race fails the test at the first report. + TSAN_OPTIONS: halt_on_error=1:second_deadlock_stack=1 jobs: # ---------------------------------------------------------------------------- # Linux and macOS: native builds with the system compiler. The Debug builds # keep QuickJS's assertion that JS_FreeRuntime finds no live object, so every # test is also a leak check; unix-asan adds ASan + UBSan to everything, - # QuickJS included. The node_parity_* tests run the same addons under Node.js. + # QuickJS included; unix-tsan runs ThreadSanitizer over the thread-pool and + # thread-safe function tests (and the rest). The node_parity_* tests run the + # same addons under Node.js. # ---------------------------------------------------------------------------- unix: name: ${{ matrix.name }} @@ -53,6 +57,10 @@ jobs: os: ubuntu-24.04 preset: unix-asan cc: gcc + - name: linux-gcc-tsan + os: ubuntu-24.04 + preset: unix-tsan + cc: gcc - name: macos-arm64-debug os: macos-latest preset: unix-debug @@ -77,6 +85,11 @@ jobs: node-version: ${{ env.NODE_VERSION }} - name: Install Ninja run: python -m pip install ninja + # The ubuntu-24.04 kernel randomizes mmap with 32 bits, more than the + # ThreadSanitizer runtime supports ("unexpected memory mapping"). + - name: Reduce ASLR entropy for ThreadSanitizer + if: matrix.preset == 'unix-tsan' + run: sudo sysctl vm.mmap_rnd_bits=28 - name: Configure run: | ${{ matrix.cc }} --version diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d207f9..ce3b4d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,8 @@ project(quickjs-napi LANGUAGES C) # ------------------------------------------------------------------------------ option(QJSNAPI_SANITIZE "Build everything (QuickJS included) with AddressSanitizer and UBSan" OFF) -option(QJSNAPI_LIBUV "Event loop integration through libuv (milestone M5)" OFF) +option(QJSNAPI_TSAN + "Build everything (QuickJS and libuv included) with ThreadSanitizer" OFF) option(QJSNAPI_BUILD_TESTS "Build the test addons and the test suite" ON) set(QJSNAPI_QUICKJS_DIR @@ -33,11 +34,6 @@ if(MSVC) "Addons built with MSVC (node-gyp) still load: Node-API is a C ABI.") endif() -if(QJSNAPI_LIBUV) - message(FATAL_ERROR "QJSNAPI_LIBUV belongs to milestone M5 and is not " - "available yet") -endif() - # Sanitizers are global so that QuickJS itself is instrumented too: a leak or # overflow inside the engine caused by a wrong JSValue lifetime must be # reported at its origin. @@ -51,6 +47,48 @@ if(QJSNAPI_SANITIZE) add_link_options(${_qjsnapi_san}) endif() endif() +if(QJSNAPI_TSAN) + if(QJSNAPI_SANITIZE) + message(FATAL_ERROR "QJSNAPI_TSAN and QJSNAPI_SANITIZE are exclusive") + endif() + add_compile_options(-fsanitize=thread -fno-omit-frame-pointer) + add_link_options(-fsanitize=thread) +endif() + +# ------------------------------------------------------------------------------ +# libuv +# ------------------------------------------------------------------------------ +# The event loop of Node-API (napi_get_uv_event_loop, async work, thread-safe +# functions), linked statically like in Node.js. Fetched at configure time; +# to build offline point FETCHCONTENT_SOURCE_DIR_LIBUV at a libuv checkout of +# the same tag. +include(FetchContent) +set(LIBUV_BUILD_SHARED OFF CACHE BOOL "" FORCE) +set(LIBUV_BUILD_TESTS OFF CACHE BOOL "" FORCE) +set(LIBUV_BUILD_BENCH OFF CACHE BOOL "" FORCE) +FetchContent_Declare( + libuv + GIT_REPOSITORY https://github.com/libuv/libuv.git + GIT_TAG v1.51.0 + GIT_SHALLOW TRUE) +if(POLICY CMP0169) + cmake_policy(SET CMP0169 OLD) # FetchContent_Populate, for EXCLUDE_FROM_ALL +endif() +FetchContent_GetProperties(libuv) +if(NOT libuv_POPULATED) + FetchContent_Populate(libuv) + add_subdirectory("${libuv_SOURCE_DIR}" "${libuv_BINARY_DIR}" EXCLUDE_FROM_ALL) +endif() +# Addons call uv_* too (node-gyp gives them uv.h): the executable exports +# libuv like Node-API. On Windows that needs __declspec(dllexport) on the +# definitions, which uv.h emits for BUILDING_UV_SHARED. +if(WIN32) + target_compile_definitions(uv_a PRIVATE BUILDING_UV_SHARED=1) + # MinGW headers (zig cc) define _WIN32_WINNT before libuv's own value. + if(CMAKE_C_COMPILER_ID MATCHES "Clang") + target_compile_options(uv_a PRIVATE -Wno-macro-redefined) + endif() +endif() # ------------------------------------------------------------------------------ # QuickJS @@ -112,6 +150,7 @@ endif() # libqjsnapi: the Node-API implementation # ------------------------------------------------------------------------------ set(QJSNAPI_SOURCES + src/async.c src/env.c src/error.c src/function.c @@ -119,10 +158,12 @@ set(QJSNAPI_SOURCES src/builtins.c src/lifetime.c src/loader.c + src/loop.c src/node_api.c src/object.c src/scope.c src/string.c + src/tsfn.c src/value.c src/stubs_generated.c) @@ -155,7 +196,7 @@ set_target_properties( target_compile_definitions(qjsnapi PRIVATE NAPI_VERSION=10) target_include_directories(qjsnapi PUBLIC include PRIVATE src "${QJSNAPI_GENERATED_DIR}") -target_link_libraries(qjsnapi PUBLIC qjs ${CMAKE_DL_LIBS}) +target_link_libraries(qjsnapi PUBLIC qjs uv_a ${CMAKE_DL_LIBS}) if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") target_compile_options(qjsnapi PRIVATE -Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes) @@ -164,8 +205,8 @@ endif() # ------------------------------------------------------------------------------ # Host executable # ------------------------------------------------------------------------------ -# Addons resolve napi_* from the process: every Node-API function must be -# exported by the executable. +# Addons resolve napi_* and uv_* from the process, as from node: every +# Node-API function and all of libuv must be exported by the executable. # - WHOLE_ARCHIVE keeps the objects of functions the host never calls. # - Linux/BSD: ENABLE_EXPORTS adds --export-dynamic. # - Windows: the definitions carry __declspec(dllexport) (NAPI_EXTERN), and @@ -193,6 +234,9 @@ target_include_directories(quickjs-napi PRIVATE "${QJSNAPI_GENERATED_DIR}") set_target_properties(quickjs-napi PROPERTIES C_STANDARD 17 ENABLE_EXPORTS ON) target_link_libraries( quickjs-napi PRIVATE "$") +# uv_a comes in through qjsnapi's link interface: whole archive as well. +set_target_properties(quickjs-napi PROPERTIES LINK_LIBRARY_OVERRIDE_uv_a + QJSNAPI_WHOLE_ARCHIVE) if(WIN32 AND NOT MSVC) # static libgcc / libwinpthread, as for the quickjs-build tools target_link_options(quickjs-napi PRIVATE -static) @@ -228,7 +272,7 @@ if(QJSNAPI_BUILD_TESTS) C_VISIBILITY_PRESET hidden LIBRARY_OUTPUT_DIRECTORY "${QJSNAPI_ADDON_DIR}" RUNTIME_OUTPUT_DIRECTORY "${QJSNAPI_ADDON_DIR}") - target_include_directories(${name} PRIVATE include) + target_include_directories(${name} PRIVATE include "${libuv_SOURCE_DIR}/include") target_compile_definitions(${name} PRIVATE BUILDING_NODE_EXTENSION NAPI_VERSION=10) if(WIN32) @@ -288,10 +332,13 @@ if(QJSNAPI_BUILD_TESTS) CXX_STANDARD 17 LIBRARY_OUTPUT_DIRECTORY "${_bin}/build/Release" RUNTIME_OUTPUT_DIRECTORY "${_bin}/build/Release") - target_include_directories(${_lib} PRIVATE include) + target_include_directories(${_lib} PRIVATE include "${libuv_SOURCE_DIR}/include") target_compile_definitions( ${_lib} PRIVATE BUILDING_NODE_EXTENSION NODE_GYP_MODULE_NAME=${T_TARGET} ${T_DEFINES}) + # node-gyp does not define NDEBUG in Release, and the tests rely on it: + # test_uv_loop calls its callback inside assert(). + target_compile_options(${_lib} PRIVATE -UNDEBUG) if(WIN32) target_link_libraries(${_lib} PRIVATE quickjs-napi) elseif(APPLE) @@ -301,36 +348,78 @@ if(QJSNAPI_BUILD_TESTS) foreach(_script ${T_SCRIPTS}) configure_file("${_src}/${_script}" "${_bin}/${_script}" COPYONLY) add_test(NAME node/${suite}/${dir}/${_script} - COMMAND quickjs-napi "${_bin}/${_script}" + COMMAND quickjs-napi + ${QJSNAPI_NODE_FLAGS_${suite}_${dir}_${_script}} + "${_bin}/${_script}" WORKING_DIRECTORY "${_bin}") endforeach() endfunction() - # A script that Node.js runs from a child process of itself (spawnSync): - # the child mode runs directly, the output is checked by regex. REQUIRE - # loads the script with require(), as the parent does. + # The supported options of a script's "// Flags:" line. + function(qjsnapi_node_flags suite dir script) + set(QJSNAPI_NODE_FLAGS_${suite}_${dir}_${script} ${ARGN} PARENT_SCOPE) + endfunction() + + # A script that Node.js runs from a child process of itself (spawnSync, + # fork): the child mode runs directly. REQUIRE loads the script with + # require(), as the parent does; ABORT expects an abnormal termination + # (cmake/expect_abort.cmake); PASS / FAIL check the output, otherwise the + # exit code decides. Named