Repository navigation
Recognize an Emscripten/WebAssembly target - #112911
Conversation
Adds `OS_WASM`/`ARCH_WASM` to the build system and the platform arms that go with them, so `emcmake cmake` configures this tree and the lower layers of it compile to `wasm64`. It is experimental and not in CI, like the Android and E2K targets; nothing here changes any platform that builds today. The build system: arch and target detection, `cmake/wasm/default_libs.cmake` (the Emscripten sysroot already supplies libc, libc++, libc++abi, compiler-rt and a pthread implementation, so unlike the other platforms this one does not use `-nodefaultlibs`), `emcc` handling in `cmake/tools.cmake` (it knows its own target and drives `wasm-ld` itself), the set of libraries a sandbox cannot have, `ucontext` fibers for Boost.Context and the Linux resolver configuration for c-ares. `wasm64` rather than `wasm32`: this tree assumes a 64-bit `size_t` and 64-bit pointers pervasively, from `1e12uz` literals in `Core/Defines.h` to sizeof-equality static_asserts in ProfileEvents. The platform arms: thread ids and names, `waitForPid`, `malloc_usable_size`, and stack traces - WebAssembly cannot walk its own call stack from user code and there is no libunwind for it. Signals get one capability macro, `OS_HAS_SIGNAL_HANDLERS` in `base/defines.h`, rather than a condition per call site; the timer-based query profiler follows it, since a per-thread timer has no way to deliver an expiry without signals. Two fixes that are not WebAssembly-specific but block this configuration: * The `USE_LIBFIU=0` branch of `FailPoint.cpp` is stale. It defines `enablePauseFailPoint`, `wait` and `enableFromGlobalConfig`, none of which the class declares any more, and omits `notifyPauseAndWaitForResume`, which it does. `-DENABLE_LIBFIU=OFF` does not build on any platform today. * `atomicRename.cpp` defines `RENAME_NOREPLACE`/`RENAME_EXCHANGE` unconditionally in its non-Linux branch, which collides with a libc that declares them without being able to honour them. No warnings are disabled: `-Weverything -Werror` is untouched, and the one diagnostic that came from a libc macro rather than from this code - musl spells `stderr` as `#define stderr (stderr)` - is suppressed at the single use, the way `cmake/warnings.cmake` prescribes. Verified with Emscripten 6.0.5. The whole tree configures. `base/base/libcommon.a` and `src/libclickhouse_common_io.a` build - 402 objects, confirmed as WebAssembly binaries - with the standard warning set and `-Werror`. On Linux the full `clickhouse` binary builds and behaves identically: stack traces, the query profiler and thread naming all unchanged. Two things still stand between this and a whole WebAssembly binary, both deliberately left out. `-Wunique-object-duplication` fires 214 times across 16 headers because Emscripten defaults to `-fvisibility=hidden`, which no other target here uses; clearing it means giving those statics default visibility, which is its own change and not this one. And the rest of the tree beyond `clickhouse_common_io` has not been attempted. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ding # Conflicts: # base/base/defines.h
|
Workflow [PR], commit [3cb7492] Summary: ✅
AI ReviewSummaryThe PR adds an experimental Final Verdict✅ No new findings on the current head. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 38/54 (70.37%) · Uncovered code |
\`cmake/arch.cmake\` matched \`CMAKE_SYSTEM_PROCESSOR\` first and only fell through to the \`Emscripten\` branch. That works with the default Emscripten toolchain, which sets the processor to \`x86\` - a value none of the host-CPU regexes claim - but it is fragile: \`-DEMSCRIPTEN_SYSTEM_PROCESSOR=x86_64\` (or \`arm64\`) makes a WebAssembly configure select \`ARCH_AMD64\` instead, and with it the host-only paths such as Boost.Context assembly and \`-march=\` flags. Test the target name first, and correct the comment, which claimed Emscripten leaves the processor as the host's. Verified with Emscripten 6.0.5: \`ARCH_WASM\` is set both with the default processor and with \`-DEMSCRIPTEN_SYSTEM_PROCESSOR=x86_64\`, and a native configure is unchanged.
|
🕵 AI review blocker addressed in The finding was right that the ordering is wrong, though not for the stated reason: Emscripten does not leave It is still fragile, because the processor is user-overridable ( Verified with Emscripten 6.0.5: |
The option only ever had one working value. Turning it off did not select a supported `wasm32` build - it dropped the ABI flags and left the tree, which assumes a 64-bit `size_t` and 64-bit pointers throughout, to fail somewhere unrelated later. Set the Memory64 flags unconditionally instead.
|
🕵 AI review major addressed in |
`clock_gettime_ns` throws `std::system_error` when the clock is rejected, so a clock id that
exists but does not work turns every timing call in the process into a throw. Emscripten has
two of them: `CLOCK_MONOTONIC_RAW` and `CLOCK_MONOTONIC_COARSE` are both defined and both
return EINVAL.
Both already have compile-time selection, and both miss this case for the same reason - they
test whether the macro is *defined*, which it is:
* `STOPWATCH_DEFAULT_CLOCK` picks `CLOCK_MONOTONIC_RAW` under `#ifdef`.
* `base/time.h` falls back for `CLOCK_MONOTONIC_COARSE` only under `#if !defined`. Some forty
call sites pass that macro explicitly, so overriding it there covers all of them.
Extend the existing conditions rather than adding a runtime fallback. Retrying on a different
clock after EINVAL would mean silently serving a monotonic reading from `CLOCK_REALTIME`, and
elapsed times could then go negative across an NTP step - worse than the throw it avoids.
Measured with Emscripten 6.0.5, running the real headers under node: before, both resolve to
ids 4 and 6 and `clock_gettime` fails on each; after, both resolve to id 1 and succeed. On the
host, `CLOCK_MONOTONIC_RAW` is still selected and the preprocessed output of `base/time.h` is
byte-identical, so no other platform changes.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ding The only conflict was in `src/Common/FailPoint.cpp`: master has landed the same adaptation of the `USE_LIBFIU=0` stubs to the renamed API that this branch carried, and additionally made the query-reachable stubs throw `SUPPORT_IS_DISABLED` instead of silently succeeding. Master's version is a strict superset, so it is taken as-is.
The AI review blocker was right. `untrackMemory` only had a size for the sized `operator delete` overloads on `OS_WASM`: the `malloc_usable_size` branch was excluded, so every unsized delete freed the object while leaving `CurrentMemoryTracker` incremented, which accumulates phantom memory and eventually raises a false `MEMORY_LIMIT_EXCEEDED`. The premise of the exclusion was wrong: Emscripten's musl-derived libc does declare `malloc_usable_size` in `<malloc.h>` and its allocator implements it - the header just was not being included, because the include was guarded on `OS_LINUX`. Include it on `OS_WASM` as well and drop the exclusion. Verified under Emscripten 6.0.5 that `malloc_usable_size` links and returns the usable size for both `wasm32` and `-sMEMORY64=1`.
…ot serve `ENABLE_WASMEDGE` was left on, so an `OS_WASM` configure still produced `ch_contrib::wasmedge` - a host WebAssembly runtime, inside a WebAssembly sandbox - and `dbms` would link it. Turn it off; `wasmtime`, the other engine, is Rust and `ENABLE_RUST` already covers it. Three more opt-outs are needed for `emcmake cmake` to configure at all now that the tree has moved on: - `ENABLE_SSH`: `libssh` needs raw sockets, and `contrib/libssh-cmake` fails with `Platform is not supported` because its config headers are pregenerated per platform. - `ENABLE_PROTOBUF`: `protoc` has to run on the host, and the nested native configure at the bottom of the top-level `CMakeLists.txt` is handed `emcc` as its host compiler, which then rejects the empty target. - `ENABLE_PARQUET`: Arrow and ORC hard-depend on the `protobuf` target. Verified with Emscripten 6.0.5: `emcmake cmake` now configures and generates cleanly, and `ninja clickhouse_common_io` builds.
`LONG_IS_A_DISTINCT_TYPE` was keyed off `!defined(__LP64__)`, but `wasm64` defines `__LP64__` and still has `__INT64_TYPE__` as `long long int`, so a 64-bit `long` there matches neither `Int64` nor `Int32` - exactly the Darwin situation, which the macro already handles. Without the `long` overloads, `itoa` calls from `writeIntText` and `toString` were ambiguous. The pointer width alone does not answer the question, so key the macro on the platform instead.
|
🕵 Both AI review findings are addressed, and the merge conflict with Blocker — unsized-delete memory accounting on The exclusion itself was based on a wrong premise of mine — Emscripten's musl-derived libc does declare Major — Re-running Also On the reported blind spot: this was verified locally with Emscripten 6.0.5. |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112911&sha=c41e24cb553693def4ded9a8f8151a651dd80531&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The server disappeared mid-run at test 7326/12303, with The whole run then aborted with This is unrelated to the changes here: every code path this PR touches on Linux is behind |
|
Investigated. There are two separate defects in the report, and neither is caused by this PR - your attribution is right, and the second one is why the report is empty. 1. Why the report has no evidence at all (the The run aborted at 09:58:52 and the job then hung for 2 h 17 min until praktika's own 9000 s timeout SIGKILLed it at 18:16:09. So The hang is in the hung-check teardown path:
This is the generic failure mode, not something specific to your branch: any 2. The server death itself. I cannot attribute it from this job, for the reason above - the err log and I found no open issue or PR for either half, matching what you saw. I am opening a separate PR for defect 1: bound the teardown so the abort path cannot outlive the job, and remove the runtime |
…HANDLERS` `ThreadStatus` installed an alternative stack and rewrote the `SIGSEGV` action on every thread, outside the new signal-capability fence. On a target without signal delivery those calls can only no-op or warn, so the whole `ThreadStack` machinery now follows `OS_HAS_SIGNAL_HANDLERS`, the same way `QueryProfiler` and `SignalHandlers` do.
|
🕵 Merged The two stress reds at
|
|
The single hung row is not an analyzer query. The The worker thread 20568 is where it is stuck: Timeline for query id
That is 569 s of retrying after the query was already cancelled, against a hung check that waits 90 s for the processlist to drain. Two open pull requests put
#112930 is a superset of #112144 for this failure, and its unconditional check reaches every user of the buffer, including On the first bullet: #113248 is still open, so that attribution holds and I have nothing pending there. |
|
The fix is up: #113393. It reaps the test process groups the abort path orphans, which is defect 1 above (the Defect 2, the server death itself, is not attributable from that job: no server process |
PR ClickHouse#112911 made `emcmake cmake` configure this tree and the layers up to `clickhouse_common_io` compile to `wasm64`. Pin that state in CI so it cannot regress: a `Build (wasm64)` job among the special builds, running in both the PR and master workflows. The job configures through `emcmake` (Emscripten's toolchain file supplies `emcc`/`em++`, so no compiler is passed) and builds exactly the targets that are known to work: `common` and `clickhouse_common_io`. The compiler cache is disabled because `emcc` is a Python driver that sccache cannot cache. The binary-builder image gets the Emscripten SDK pinned to 6.0.5, the version the target was verified with, and pre-generates the wasm64 variant of the system libraries (`-sMEMORY64`, `-fwasm-exceptions` and `-pthread` are ABI flags, see cmake/target.cmake), so CI builds do not compile them lazily at first use. Verified with the exact CI configure and build commands: 2539 objects across 28 contrib libraries, base and src compile with zero warnings, and both archives link; `llvm-readobj` confirms `Arch: wasm64`. The whole build takes ~30 seconds of wall time on a 96-core machine, making it one of the cheapest build jobs. Co-Authored-By: Claude Fable 5 <[email protected]>
Adds
OS_WASM/ARCH_WASMto the build system and the platform arms that go with them, soemcmake cmakeconfigures this tree and the lower layers of it compile towasm64. It is experimental and not in CI, like the Android and E2K targets; nothing here changes any platform that builds today.The build system: arch and target detection,
cmake/wasm/default_libs.cmake(the Emscripten sysroot already supplies libc, libc++, libc++abi, compiler-rt and a pthread implementation, so unlike the other platforms this one does not use-nodefaultlibs),emcchandling incmake/tools.cmake(it knows its own target and driveswasm-lditself), the set of libraries a sandbox cannot have,ucontextfibers for Boost.Context and the Linux resolver configuration for c-ares.wasm64rather thanwasm32, because this tree assumes a 64-bitsize_tand 64-bit pointers pervasively, from1e12uzliterals inCore/Defines.hto sizeof-equality static_asserts in ProfileEvents.The platform arms: thread ids and names,
waitForPid,malloc_usable_size, and stack traces — WebAssembly cannot walk its own call stack from user code and there is no libunwind for it. Signals get one capability macro,OS_HAS_SIGNAL_HANDLERSinbase/defines.h, rather than a condition per call site; the timer-based query profiler follows it, since a per-thread timer has no way to deliver an expiry without signals.Two fixes that are not WebAssembly-specific but block this configuration:
USE_LIBFIU=0branch ofFailPoint.cppis stale. It definesenablePauseFailPoint,waitandenableFromGlobalConfig, none of which the class declares any more, and omitsnotifyPauseAndWaitForResume, which it does.-DENABLE_LIBFIU=OFFdoes not build on any platform today.atomicRename.cppdefinesRENAME_NOREPLACE/RENAME_EXCHANGEunconditionally in its non-Linux branch, which collides with a libc that declares them without being able to honour them.No warnings are disabled.
-Weverything -Werroris untouched andWERRORis not weakened. The one diagnostic that came from a libc macro rather than from this code — musl spellsstderras#define stderr (stderr)— is suppressed at the single use, the waycmake/warnings.cmakeprescribes.Verification
With Emscripten 6.0.5:
base/base/libcommon.aandsrc/libclickhouse_common_io.abuild — 402 objects, confirmed as WebAssembly binaries withllvm-objdump— with the standard warning set and-Werror.On Linux the full
clickhousebinary builds and behaves identically to a build without this change: stack traces (symbolized, andsystem.errors.last_error_trace), the query profiler and thread naming are all unchanged.What is deliberately left out
-Wunique-object-duplicationfires 214 times across 16 headers (Interpreters/Context.h,Core/BaseSettings.h,Common/TargetSpecific.h, …) because Emscripten defaults to-fvisibility=hidden, which no other target here uses. Clearing it means giving those statics default visibility, which is its own change.clickhouse_common_iohas not been attempted.Common/Exception.cppcannot compile against a foreign libc++). Both are independent of this change and both have now been merged intomaster; the results above were measured with all three applied.Related: #112863
Related: #112865
Related: #112866
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added an experimental WebAssembly (
wasm64, via Emscripten) target:emcmake cmakenow configures the tree and the lower layers build. Also fixed-DENABLE_LIBFIU=OFF, which did not build on any platform.Documentation entry for user-facing changes