From 104efd46d2d8d1da72e4420333c9a77c91efa386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 30 Oct 2026 15:31:33 +0100 Subject: [PATCH 1/2] Add `bare::kit::exports` target for downstream symbol stripping --- CMakeLists.txt | 2 +- package-lock.json | 20 +---- package.json | 1 - src/CMakeLists.txt | 138 +++++++++++++++++++++++++++++++--- src/builtins.json | 2 - src/exports.json | 31 ++++++++ src/{linux => unix}/symbols.c | 4 +- src/win32/bare-kit.def | 30 -------- src/worklet.js | 3 +- test/CMakeLists.txt | 45 +++++++++-- test/worklet-global-scope.c | 33 ++++++++ 11 files changed, 235 insertions(+), 74 deletions(-) create mode 100644 src/exports.json rename src/{linux => unix}/symbols.c (71%) delete mode 100644 src/win32/bare-kit.def create mode 100644 test/worklet-global-scope.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e01a1d..43dc26d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ if(APPLE) enable_language(OBJC) endif() -fetch_package("github:holepunchto/bare@1.36.1") +fetch_package("github:holepunchto/bare@1.36.2") add_subdirectory(src) diff --git a/package-lock.json b/package-lock.json index d1df0f5..7474030 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,6 @@ "license": "Apache-2.0", "dependencies": { "bare-console": "^6.0.0", - "bare-crypto": "^1.2.8", "bare-fs": "^4.0.1", "bare-logger": "^2.0.0", "bare-module": "^7.5.0", @@ -89,6 +88,7 @@ "version": "1.3.0", "resolved": "https://registry.npmjs.org/bare-assert/-/bare-assert-1.3.0.tgz", "integrity": "sha512-tF3A1xa5WHzOwn2Y6Pm+mV1NGOztWUKyUQycmtMCnIcEx/0xv5MMgv2qerXQVvhkv6IZJNaEbiGBJNFD6RmunA==", + "dev": true, "license": "Apache-2.0", "dependencies": { "bare-inspect": "^3.1.5", @@ -160,24 +160,6 @@ "bare-type": "^1.1.0" } }, - "node_modules/bare-crypto": { - "version": "1.15.4", - "resolved": "https://registry.npmjs.org/bare-crypto/-/bare-crypto-1.15.4.tgz", - "integrity": "sha512-4luuiLnxIJvM7ZyyW6pXbaaGoQDZcIkfcVZrPzJ3oIpdi2UIP37dROmzBWqI2lnrzfKoQlKWM8ryHmyG2svViw==", - "license": "Apache-2.0", - "dependencies": { - "bare-assert": "^1.2.0", - "bare-stream": "^2.6.3" - }, - "peerDependencies": { - "bare-buffer": "*" - }, - "peerDependenciesMeta": { - "bare-buffer": { - "optional": true - } - } - }, "node_modules/bare-debug-log": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/bare-debug-log/-/bare-debug-log-2.0.1.tgz", diff --git a/package.json b/package.json index 4a0536c..bda66db 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,6 @@ "homepage": "https://github.com/holepunchto/bare-kit#readme", "dependencies": { "bare-console": "^6.0.0", - "bare-crypto": "^1.2.8", "bare-fs": "^4.0.1", "bare-logger": "^2.0.0", "bare-module": "^7.5.0", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 16b22a3..a84ba76 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,8 @@ add_bare_bundle( add_library(bare_kit OBJECT) +add_library(bare::kit ALIAS bare_kit) + set_target_properties( bare_kit PROPERTIES @@ -37,7 +39,7 @@ target_include_directories( bare_kit PUBLIC ../include - $ + $ ) if(APPLE) @@ -120,16 +122,16 @@ endif() target_link_libraries( bare_kit INTERFACE - $ + $ PUBLIC rlimit_static ) -if(LINUX) +if(APPLE OR LINUX) target_sources( bare_kit PRIVATE - linux/symbols.c + unix/symbols.c ) target_link_libraries( @@ -145,10 +147,106 @@ else() ) endif() -link_bare_modules(bare_kit WORKING_DIRECTORY ..) +link_bare_modules( + bare_kit + WORKING_DIRECTORY .. + EXCLUDE + bare-buffer + bare-hrtime + bare-inspect + bare-logger + bare-module + bare-module-lexer + bare-path + bare-structured-clone + bare-system-logger + bare-timers + bare-type + bare-type-stripper + bare-url +) + +file(READ exports.json exports_json) + +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS exports.json) + +set(exports) + +string(JSON symbols LENGTH "${exports_json}") + +math(EXPR last_symbol "${symbols} - 1") + +foreach(i RANGE ${last_symbol}) + string(JSON symbol GET "${exports_json}" ${i}) + + list(APPEND exports ${symbol}) +endforeach() + +if(ANDROID) + list(APPEND exports JNI_OnLoad) +endif() + +add_library(bare_kit_exports INTERFACE) + +add_library(bare::kit::exports ALIAS bare_kit_exports) + +if(WIN32) + set(exports_file "${CMAKE_CURRENT_BINARY_DIR}/bare-kit.def") + + list(TRANSFORM exports PREPEND " ") + list(JOIN exports "\n" exports) + + file(CONFIGURE OUTPUT "${exports_file}" CONTENT "EXPORTS\n${exports}\n" @ONLY) + + target_sources( + bare_kit_exports + INTERFACE + "${exports_file}" + ) +elseif(APPLE) + set(exports_file "${CMAKE_CURRENT_BINARY_DIR}/bare-kit.exp") + + list(TRANSFORM exports PREPEND "_") + list(JOIN exports "\n" exports) + + file(CONFIGURE OUTPUT "${exports_file}" CONTENT "${exports}\n" @ONLY) + + target_link_options( + bare_kit_exports + INTERFACE + "-Wl,-exported_symbols_list,${exports_file}" + ) +else() + set(exports_file "${CMAKE_CURRENT_BINARY_DIR}/bare-kit.map") + + list(TRANSFORM exports PREPEND " ") + list(JOIN exports ";\n" exports) + + file(CONFIGURE OUTPUT "${exports_file}" CONTENT "{\n global:\n${exports};\n\n local:\n *;\n};\n" @ONLY) + + target_link_options( + bare_kit_exports + INTERFACE + "-Wl,--version-script=${exports_file}" + ) +endif() + +set_target_properties( + bare_kit_exports + PROPERTIES + INTERFACE_LINK_DEPENDS "${exports_file}" +) + +target_link_libraries( + bare_kit_exports + INTERFACE + bare::exports +) add_library(bare_kit_shared SHARED) +add_library(bare::kit::shared ALIAS bare_kit_shared) + set_target_properties( bare_kit_shared PROPERTIES @@ -156,10 +254,16 @@ set_target_properties( ) if(WIN32) - target_sources( + target_link_libraries( bare_kit_shared PUBLIC - win32/bare-kit.def + bare_kit_exports + ) +else() + target_link_libraries( + bare_kit_shared + PRIVATE + bare_kit_exports ) endif() @@ -175,6 +279,20 @@ target_link_libraries( bare_kit ) +if(APPLE) + target_link_options( + bare_kit_shared + PRIVATE + $<$:-Wl,-dead_strip,-S,-x> + ) +elseif(ANDROID OR LINUX) + target_link_options( + bare_kit_shared + PRIVATE + $<$:-Wl,--gc-sections,--strip-all> + ) +endif() + if(ANDROID) target_link_options( bare_kit_shared @@ -193,6 +311,8 @@ endif() add_library(bare_kit_static STATIC) +add_library(bare::kit::static ALIAS bare_kit_static) + set_target_properties( bare_kit_static PROPERTIES @@ -201,10 +321,10 @@ set_target_properties( ) if(WIN32) - target_sources( + target_link_libraries( bare_kit_static PUBLIC - win32/bare-kit.def + bare_kit_exports ) endif() diff --git a/src/builtins.json b/src/builtins.json index da81b5d..09901d8 100644 --- a/src/builtins.json +++ b/src/builtins.json @@ -1,13 +1,11 @@ [ { "addon": "bare-buffer" }, - { "addon": "bare-crypto" }, { "addon": "bare-fs" }, { "addon": "bare-hrtime" }, { "addon": "bare-inspect" }, { "addon": "bare-logger" }, { "addon": "bare-module" }, { "addon": "bare-module-lexer" }, - { "addon": "bare-os" }, { "addon": "bare-path" }, { "addon": "bare-structured-clone" }, { "addon": "bare-system-logger" }, diff --git a/src/exports.json b/src/exports.json new file mode 100644 index 0000000..c5adc5e --- /dev/null +++ b/src/exports.json @@ -0,0 +1,31 @@ +[ + "bare_worklet_optimize_for_memory", + "bare_worklet_teardown", + "bare_worklet_create", + "bare_worklet_destroy", + "bare_worklet_on_suspend", + "bare_worklet_on_wakeup", + "bare_worklet_on_idle", + "bare_worklet_on_resume", + "bare_worklet_on_exit", + "bare_worklet_get_data", + "bare_worklet_set_data", + "bare_worklet_start", + "bare_worklet_suspend", + "bare_worklet_resume", + "bare_worklet_wakeup", + "bare_worklet_terminate", + "bare_ipc_create", + "bare_ipc_close", + "bare_ipc_end", + "bare_ipc_destroy", + "bare_ipc_read", + "bare_ipc_write", + "bare_ipc_poll_create", + "bare_ipc_poll_destroy", + "bare_ipc_poll_get_data", + "bare_ipc_poll_set_data", + "bare_ipc_poll_get_ipc", + "bare_ipc_poll_start", + "bare_ipc_poll_stop" +] diff --git a/src/linux/symbols.c b/src/unix/symbols.c similarity index 71% rename from src/linux/symbols.c rename to src/unix/symbols.c index 8dc902a..1991ebc 100644 --- a/src/linux/symbols.c +++ b/src/unix/symbols.c @@ -7,8 +7,8 @@ void bare_kit__expose_symbols(void) { - // As glibc ignores `-z global`, promote the library to the global scope in - // case it was loaded with `RTLD_LOCAL`. + // Addons resolve the symbols of Bare from the global scope, so promote the + // library to it in case it was loaded with `RTLD_LOCAL`. Dl_info info; if (dladdr((void *) bare_kit__expose_symbols, &info) == 0 || info.dli_fname == NULL) return; diff --git a/src/win32/bare-kit.def b/src/win32/bare-kit.def deleted file mode 100644 index 0d6bf0e..0000000 --- a/src/win32/bare-kit.def +++ /dev/null @@ -1,30 +0,0 @@ -EXPORTS - bare_worklet_optimize_for_memory - bare_worklet_teardown - bare_worklet_create - bare_worklet_destroy - bare_worklet_on_suspend - bare_worklet_on_wakeup - bare_worklet_on_idle - bare_worklet_on_resume - bare_worklet_on_exit - bare_worklet_get_data - bare_worklet_set_data - bare_worklet_start - bare_worklet_suspend - bare_worklet_resume - bare_worklet_wakeup - bare_worklet_terminate - bare_ipc_create - bare_ipc_close - bare_ipc_end - bare_ipc_destroy - bare_ipc_read - bare_ipc_write - bare_ipc_poll_create - bare_ipc_poll_destroy - bare_ipc_poll_get_data - bare_ipc_poll_set_data - bare_ipc_poll_get_ipc - bare_ipc_poll_start - bare_ipc_poll_stop diff --git a/src/worklet.js b/src/worklet.js index 51465b9..4dd3c89 100644 --- a/src/worklet.js +++ b/src/worklet.js @@ -1,6 +1,5 @@ const path = require('bare-path') const fs = require('bare-fs') -const crypto = require('bare-crypto') const { pathToFileURL } = require('bare-url') const URL = require('bare-url') const Module = require('bare-module') @@ -157,7 +156,7 @@ function extract(resources, filename, readResource) { // Worklets may extract the same resources at once, so each file is moved into // place whole. function write(target, data) { - const tmp = target + '.' + crypto.randomBytes(8).toString('hex') + const tmp = target + '.' + Math.random().toString(16).slice(2) fs.mkdirSync(path.dirname(target), { recursive: true }) fs.writeFileSync(tmp, Buffer.from(data)) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 70f30e5..042e5d7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -69,18 +69,11 @@ target_link_libraries( # binding would. add_library(test_embedder SHARED) -if(WIN32) - target_sources( - test_embedder - PRIVATE - ../src/win32/bare-kit.def - ) -endif() - target_link_libraries( test_embedder PRIVATE bare_kit + bare_kit_exports ) if(ANDROID) @@ -127,6 +120,42 @@ foreach(test IN ITEMS worklet-addon-dynamic worklet-addon-embedded) endif() endforeach() +if(APPLE OR LINUX) + add_executable(test_worklet-global-scope worklet-global-scope.c) + + target_include_directories( + test_worklet-global-scope + PRIVATE + ../include + ) + + target_compile_definitions( + test_worklet-global-scope + PRIVATE + BARE_KIT="$" + ) + + target_link_libraries( + test_worklet-global-scope + PRIVATE + ${CMAKE_DL_LIBS} + ) + + add_dependencies(test_worklet-global-scope bare_kit_shared) + + add_test( + NAME worklet-global-scope + COMMAND test_worklet-global-scope + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ) + + set_tests_properties( + worklet-global-scope + PROPERTIES + TIMEOUT 120 + ) +endif() + foreach(test IN ITEMS worklet-addon-static worklet-addon-shared worklet-addon-dynamic worklet-addon-embedded) target_compile_definitions( test_${test} diff --git a/test/worklet-global-scope.c b/test/worklet-global-scope.c new file mode 100644 index 0000000..b4507fd --- /dev/null +++ b/test/worklet-global-scope.c @@ -0,0 +1,33 @@ +#include +#include +#include + +#include + +// The host loads the library locally, yet the addons of a worklet must resolve +// the symbols of Bare from the global scope. + +int +main() { + int e; + + void *library = dlopen(BARE_KIT, RTLD_NOW | RTLD_LOCAL); + assert(library != NULL); + + assert(dlsym(RTLD_DEFAULT, "js_create_string_utf8") == NULL); + + int (*worklet_create)(const bare_worklet_options_t *, bare_worklet_t **) = dlsym(library, "bare_worklet_create"); + void (*worklet_destroy)(bare_worklet_t *) = dlsym(library, "bare_worklet_destroy"); + int (*worklet_teardown)(void) = dlsym(library, "bare_worklet_teardown"); + + bare_worklet_t *worklet; + e = worklet_create(NULL, &worklet); + assert(e == 0); + + assert(dlsym(RTLD_DEFAULT, "js_create_string_utf8") != NULL); + + worklet_destroy(worklet); + + e = worklet_teardown(); + assert(e == 0); +} From b91ad56efc954f26a4df6e4fa25d652abc835aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Fri, 30 Oct 2026 15:38:29 +0100 Subject: [PATCH 2/2] Add missing `_GNU_SOURCE` --- test/worklet-global-scope.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/worklet-global-scope.c b/test/worklet-global-scope.c index b4507fd..d5d6b37 100644 --- a/test/worklet-global-scope.c +++ b/test/worklet-global-scope.c @@ -1,3 +1,5 @@ +#define _GNU_SOURCE + #include #include #include