Skip to content

wasm: provide swapon/swapoff; main-thread pthread_getattr_np - #13

Open
tombl wants to merge 2 commits into
masterfrom
ryan/wasm-swap-stack
Open

tombl wants to merge 2 commits into
masterfrom
ryan/wasm-swap-stack

Conversation

@tombl

@tombl tombl commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

Both changes undo our port's own hacks in generic files.

  • Provide swapon and swapoff. The #ifndef __wasm__ around src/linux/swap.c came from our own 19c33f6 ("disable/remove all instances of mmap…"), which swept these up with mmap. They are ordinary syscalls: with ARCH_NO_SWAP the kernel's swapon is a cond_syscall that returns ENOSYS. This restores the file byte-for-byte to upstream. Nothing in the distro needs it, since util-linux passes --disable-swapon on purpose. It's kept because it removes a needless deviation. The commit is byte-identical to Ryan Rasti's ryanrasti/tombl-musl@2e9ff1cc, so he remains the author.
  • pthread_getattr_np for the main thread. Our own 40851de ("replace fork calls with clone, and further tidy after mmap/fork removal") turned the generic file's mremap probe into commented-out code plus a_crash(), so asking for the main thread's stack crashed. MariaDB does that at startup. This version restores src/thread/pthread_getattr_np.c exactly to upstream, and gives wasm32 its own src/thread/wasm32/pthread_getattr_np.c. musl's build drops the generic object when an arch file of the same name exists, and the build log shows the wasm32 file compiled.
    • The main thread runs on the stack wasm-ld places in linear memory, so the address is __stack_high and the size is __stack_high - __stack_low.
    • It fails with ENOSYS when the thread asking about itself isn't within those bounds. A child cloned without CLONE_VM from the main thread keeps the main thread's descriptor but runs on the stack its parent supplied.
    • The bounds are linker-defined only for static links, which are all this port builds. The references are strong, since weak ones resolved to null in the real build.

Test (basic-init pthread-getattr-main, in distro#206). Before (main's pins): init dies with SIGSEGV (Attempted to kill init! exitcode=0x0000000b). After: pass.

The getattr commit is authored by Thomas Stokes with Co-authored-by: Ryan Rasti. It is based on ryanrasti/tombl-musl@cc3a733e, which reported a fixed 1 MiB below a frame address saved by __init_libc.

@tombl
tombl force-pushed the ryan/wasm-swap-stack branch 4 times, most recently from da8561f to e44f9f9 Compare October 25, 2026 14:05
ryanrasti and others added 2 commits October 25, 2026 23:58
These were hidden along with mmap and its relatives, but they are system
calls of their own, which the kernel has and refuses with ENOSYS or EPERM
without swap. util-linux's swapon and swapoff need the symbols to link.

Ported from ryanrasti/tombl-musl@2e9ff1cc.
The port made the generic pthread_getattr_np crash for the main thread,
since there is no mremap to probe its stack with. MariaDB asks for its
stack bounds at startup.

Restore the generic file and give wasm32 its own. The main thread runs on
the stack that wasm-ld places in linear memory, sized by -z stack-size,
and the linker defines __stack_low and __stack_high around it. Report
__stack_high as the address, since _a_stackaddr is the top of the stack,
and the distance between the two as the size.

A child cloned from the main thread without CLONE_VM keeps the main
thread's descriptor but runs on the stack its parent supplied, so fail
with ENOSYS when a thread asking about itself is not running within the
bounds. The linker defines the bounds only for static links, which are
all this port builds, so a PIC link fails to resolve them.

Based on ryanrasti/tombl-musl@cc3a733e by Ryan Rasti, which reported a
fixed 1 MiB below a frame address saved by __init_libc.

Co-authored-by: Ryan Rasti <ryanrasti@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants