Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
Expand All @@ -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
Expand Down
138 changes: 114 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -112,17 +150,20 @@ endif()
# libqjsnapi: the Node-API implementation
# ------------------------------------------------------------------------------
set(QJSNAPI_SOURCES
src/async.c
src/env.c
src/error.c
src/function.c
src/buffer.c
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)

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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 "$<LINK_LIBRARY:QJSNAPI_WHOLE_ARCHIVE,qjsnapi>")
# 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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 <script>:child when the script also runs as a
# normal test; NAME replaces <script> in the test name.
function(qjsnapi_node_child_run suite dir script)
cmake_parse_arguments(T "REQUIRE" "" "ARGS;PASS;FAIL" ${ARGN})
cmake_parse_arguments(T "REQUIRE;ABORT" "NAME" "ARGS;PASS;FAIL" ${ARGN})
set(_bin "${QJSNAPI_NODE_TESTS_BIN}/${suite}/${dir}")
configure_file("${QJSNAPI_NODE_TESTS_SRC}/${suite}/${dir}/${script}"
"${_bin}/${script}" COPYONLY)
set(_flags ${QJSNAPI_NODE_FLAGS_${suite}_${dir}_${script}})
if(T_REQUIRE)
set(_cmd quickjs-napi
"${QJSNAPI_NODE_TESTS_BIN}/common/require-as-module.js"
"${_bin}/${script}" ${T_ARGS})
set(_args ${_flags} "${QJSNAPI_NODE_TESTS_BIN}/common/require-as-module.js"
"${_bin}/${script}" ${T_ARGS})
else()
set(_args ${_flags} "${_bin}/${script}" ${T_ARGS})
endif()
if(T_ABORT)
list(JOIN _args "|" _joined)
set(_cmd "${CMAKE_COMMAND}" "-DEXE=$<TARGET_FILE:quickjs-napi>"
"-DARGS=${_joined}" -P
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/expect_abort.cmake")
else()
set(_cmd quickjs-napi ${_args})
endif()
if(T_NAME)
set(_name node/${suite}/${dir}/${T_NAME})
else()
set(_cmd quickjs-napi "${_bin}/${script}" ${T_ARGS})
set(_name node/${suite}/${dir}/${script})
if(TEST ${_name})
set(_name "${_name}:child")
endif()
endif()
set(_name node/${suite}/${dir}/${script})
add_test(NAME ${_name} COMMAND ${_cmd} WORKING_DIRECTORY "${_bin}")
set_tests_properties(${_name} PROPERTIES
PASS_REGULAR_EXPRESSION "${T_PASS}")
if(T_PASS)
set_tests_properties(${_name} PROPERTIES
PASS_REGULAR_EXPRESSION "${T_PASS}")
endif()
if(T_FAIL)
set_tests_properties(${_name} PROPERTIES
FAIL_REGULAR_EXPRESSION "${T_FAIL}")
endif()
endfunction()

# An addon loaded alone in a fresh process (Node: spawnSync with
# `-e require(addon)`); the output must match PASS.
function(qjsnapi_node_addon_run suite dir target)
cmake_parse_arguments(T "" "" "PASS" ${ARGN})
set(_bin "${QJSNAPI_NODE_TESTS_BIN}/${suite}/${dir}")
string(REPLACE "-" "_" _suite_id "${suite}")
set(_name node/${suite}/${dir}/${target}.node)
add_test(NAME ${_name}
COMMAND quickjs-napi
"${QJSNAPI_NODE_TESTS_BIN}/common/require-as-module.js"
"$<TARGET_FILE:node_${_suite_id}_${dir}_${target}>"
WORKING_DIRECTORY "${_bin}")
set_tests_properties(${_name} PROPERTIES PASS_REGULAR_EXPRESSION "${T_PASS}")
endfunction()

include("${QJSNAPI_NODE_TESTS_SRC}/js-native-api/tests.cmake")
include("${QJSNAPI_NODE_TESTS_SRC}/node-api/tests.cmake")

Expand All @@ -356,7 +445,8 @@ if(QJSNAPI_BUILD_TESTS)
# Reference run: the same addons and script under Node.js. Skipped with
# sanitizers: ASan-instrumented addons cannot be loaded by a regular node.
find_program(QJSNAPI_NODE node)
if(QJSNAPI_NODE AND NOT CMAKE_CROSSCOMPILING AND NOT QJSNAPI_SANITIZE)
if(QJSNAPI_NODE AND NOT CMAKE_CROSSCOMPILING AND NOT QJSNAPI_SANITIZE
AND NOT QJSNAPI_TSAN)
foreach(_t hello values binary node_api)
add_test(NAME node_parity_${_t}
COMMAND "${QJSNAPI_NODE}" --expose-gc
Expand Down
17 changes: 17 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
"cacheVariables": {
"QJSNAPI_SANITIZE": "ON"
}
},
{
"name": "unix-tsan",
"displayName": "Linux/macOS - Debug + ThreadSanitizer",
"inherits": "unix-debug",
"cacheVariables": {
"QJSNAPI_TSAN": "ON"
}
}
],
"buildPresets": [
Expand All @@ -95,6 +103,10 @@
{
"name": "unix-asan",
"configurePreset": "unix-asan"
},
{
"name": "unix-tsan",
"configurePreset": "unix-tsan"
}
],
"testPresets": [
Expand Down Expand Up @@ -129,6 +141,11 @@
"name": "unix-asan",
"inherits": "test-base",
"configurePreset": "unix-asan"
},
{
"name": "unix-tsan",
"inherits": "test-base",
"configurePreset": "unix-tsan"
}
]
}
Loading
Loading