Skip to content

Build again for RV64 and the Wasm playground, and check both in CI - #416

Merged
284km merged 8 commits into
mainfrom
fix-rv64-wasm-build
Dec 1, 2026
Merged

284km merged 8 commits into
mainfrom
fix-rv64-wasm-build

Conversation

@284km

@284km 284km commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

Why

Nothing in CI builds mere-ruby for the two targets with no C library, and both had stopped building:

  • RV64 (mere -rv64 of tools/nothreads_tree.sh --coro's tree) no longer compiled. Three callbacks were applied as (map_get h 0) () / (vec_get v 0) () — a builtin's result applied in the same application — which the pinned Mere's RISC-V backend refuses (map_get has no RISC-V lowering yet). The first was Ruby's C API on CRuby's own headers: ruby's test extensions build unchanged and test/-ext- runs against them #87.
  • Past that, every RV64 program exited 1 before its first line: mr_gc_process_cpu_time is read at startup and around every collection, outside any Ruby frame, and the RISC-V backend refuses an extern it has no C for. NKF's cleanup after at_exit failed the same way, which also swallowed the report of any earlier exception.
  • Several C shims added since replaced answers the RISC-V backend gives on its own (it binds libm, file, descriptor, socket, spawn… externs by name and type), so Math.cbrt, File.stat/File::Stat, Process.clock_gettime (every clock), IO.popen, and require "etc"/tmpdir/fileutils (Etc's module body asks for the pathconf names, and a refusal inside a module body loses its name — it read (ruby exception raised) (StandardError)) all failed there.
  • Wasm: the playground's source no longer emitted (unbound variable: tc_number): tools/wasm_lexer.py's copy of number_nls_go still called tc_number, which 00145fd removed. Past that, the page's host lacked four imports the module grew. Meanwhile openssl_wasm_check.py in the corpus job kept passing against the module committed under docs/, not against the source.

What

  • Over-application (3 sites): let f = … in f (). Same behaviour on every backend. This kept the previous pin green; the compiler-side fix is RISC-V: lower a builtin applied to more or fewer arguments than its arity; pin RV refusals; build mere-ruby for RV64 in CI merelang/mere#96 (0.1.733, pinned in the second commit).

  • CPU clock: one reader, gc_cpu_seconds, used at all four sites. A refused extern is answered with the host's existing "no clock" contract (a negative Float), so startup and collections no longer fail.

  • CPU clock semantics where there is none (browser host, RV64), aligned with CRuby's own when its gc.c finds no clock (gc_clock_start/gc_clock_end/getrusage_time → 0):

    • GC::Profiler.enable works (it raised NotImplementedError), and each record has GC_TIME/GC_INVOKE_TIME 0.0.
    • Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID) raises Errno::EINVAL (clock_gettime(…)), as process.c's clock_failed does and as this method already did for the other clocks.
    • GC.stat(:time) is unchanged.
  • NKF cleanup at exit is guarded.

  • Fallbacks where a shim only refines an answer the machine can give:

    • Math.cbrt → libm's cbrt, without the glibc step.
    • File stat → the 13 whole-second fields (stat_fields_ns already falls back on "").
    • pathconf names → none, so Etc defines no PC_*.
    • Process.clock_gettime → Mere's time (), which every clock used before mr_clock_read, except CLOCK_THREAD_CPUTIME_ID: no other clock stands in for a CPU clock, so it is Errno::EINVAL, like the process CPU clock, the page's stub and CRuby.
    • Process.uid and the rest with no real answer stay NotImplementedError.
    • Each guard is a small function outside invoke_val_c0_inner: with the try_ors inside the dispatcher, an explicit GC.start loop cost +0.5–0.9% instructions per collection although those arms never ran. Outside, it costs nothing measurable.
    • Math.cbrt and the clock reader ask once, at first use, whether their shim is there, instead of a try_or per call (~200 instructions). It is at first use, not startup, because the page's host answers mr_math_cbrt by throwing, which no try_or catches.
  • A child's non-blocking stdio (Process.__std_blocking → IO.__fdblock → mr_fd_clear_nonblock) is left exactly as on main: the shim is called as it is, so on RV64 a spawn, system or IO.popen raises NotImplementedError, as it did before this PR. The emulator's host can set O_NONBLOCK (IO.pipe does, on both ends) but can neither clear nor read it: memu's fcntl(F_SETFL) only ever sets the flag, and its F_GETFL always answers read-write. The flag also belongs to the open file description, which aliases, inherited descriptors and children share, so no descriptor's state can be established there. Earlier revisions of this PR answered "clear" from local bookkeeping: first for every descriptor, then for ones this process hadn't made non-blocking, then tracked by description. Review showed each one handing a child a non-blocking stdout through a path the bookkeeping can't see (an inherited descriptor, an alias made non-blocking afterwards, a description a child inherited and changed). The last one also kept per-description marks for the life of the process. So the fallback is gone. A backtick on its one-call path still runs; once a descriptor above 2 is held, or off the main thread, it spawns and is refused the same way. KNOWN_GAPS records this.

  • tools/wasm_lexer.py: the copy follows the original again. Each original it copies (tc_strip, number_nls_go) is pinned by its SHA-256, so the next change to one stops the Wasm tree build naming the function, instead of leaving a stale copy.

  • docs/index.html: stubs for prism_error_spans, mr_fd_clear_nonblock, mr_sock_accept, and mr_clock_read. The clock stub uses the wall clock for kind 0, the page's monotonic clock for the monotonic kinds, and none for thread CPU time.

  • tools/wasm_page_check.sh (new): runs --check-imports, the CPU-clock probe (new semantics) and the OpenSSL/network Wasm checks on SMOKE_WASM, or by default on the committed docs/mere-ruby.wasm that the page loads. docs/build.sh runs it on what it builds.

  • docs/mere-ruby.wasm is rebuilt from this source with docs/build.sh (Mere 0.1.733, binaryen 132), replacing the Implement native and browser NKF and Kconv #80 build, which the new CPU-clock probe rejects. docs/nkf.wasm is unchanged; the NKF workflow rebuilds it and requires a byte-exact match.

  • tools/rv64_smoke.sh + tools/rv64_smoke.rb (new): compiles the --coro tree for RV64 and runs one program on memu's emulator. The program covers:

    • startup, a collection, require "etc", the fallbacks
    • the CPU-clock answers, process and thread (Errno::EINVAL)
    • a backtick on the one-call path (runs)
    • a system with redirected stdio, a spawn whose stdout is a pipe end, a plain system, IO.popen, and a backtick while a descriptor above 2 is held (each must refuse)
    • a clean exit

    It fails on a compile error, other output, any stderr, or a non-zero exit.

  • CI: two ubuntu-24.04 jobs with no needs, also listed in ci-timing's needs:

    • rv64-build: setup-mere, memu pinned by SHA, then tools/rv64_smoke.sh.
    • wasm-build: setup-mere, node 22, wabt, emit plus wat2wasm, then tools/wasm_page_check.sh twice: on the fresh module, and on the committed docs/mere-ruby.wasm the published page serves.
    • openssl_wasm_check.py moves out of the corpus job into wasm-build, which runs it on both modules; network_wasm_check.py joins it.
  • KNOWN_GAPS.md: the RISC-V section (what the backend answers itself, what falls back, what stays NotImplementedError) and the no-CPU-clock semantics.

Not in this PR: moving the shims behind one per-target host module with a source gate, and making class/module-body catches keep a refusal's name.

Mere 0.1.733

MERE_REF in ci.yml (and the pin the README names) is Mere 0.1.733 (merelang/mere#96, 72bb0d09). That release's RISC-V backend lowers a builtin applied to more (or fewer) arguments than its arity, as the other backends do. mere.toml keeps mere >= 0.1.731, the oldest compiler the source builds on; the gem-suite workflows pin a 0.1.732 compiler of their own. The three let f = … in f () rewrites keep the tree building on older compilers; the main CI job is pinned to 0.1.733 so the RV64 gate also exercises the compiler fix. The tree also compiles for RV64 with the rewrites undone on 0.1.733 (checked below, not committed).

Local results on Mere 0.1.733 (72bb0d09, built from source; macOS arm64)

main has not moved since the branch point (f0f372e7), so there is no merge. The rows below are from the latest head, with the O_NONBLOCK refusal and its smoke lines.

check result
sh run_corpus.sh --both (includes every source gate) 1042/1042 match ruby, hash index ON and OFF (660 s)
sh tools/iseq/vm_corpus.sh -j 3 1042 on the VM, 0 fallbacks, 0 FAIL (187 s)
tools/rv64_smoke.sh (memu 7564b424) ok: compile 37 s (max RSS 3.5 GB), 61 s in all
the same smoke on the earlier fallbacks each fails on the case it let through (the pipe-end spawn, the inherited-stdio system, …)
RV64 probe system with redirected stdio, spawn(…, out: w) on an IO.pipe end, plain system("true") and IO.popen(["echo", "x"]) raise NotImplementedError (mr_fd_clear_nonblock() function is unimplemented on this machine); a backtick runs until a descriptor above 2 is held, then raises the same
native: __fdblock is unchanged from main (the shim clears the flag) —
RV64 compile with the three rewrites undone (throwaway copy) compiles (43 s). On memu it runs a program that reaches all three callbacks — an overflowing to_f, and a NoMethodError's backtrace — and tools/rv64_smoke.rb, matching ruby
docs/build.sh (the page module rebuild; WASI SDK step skipped, docs/nkf.wasm unchanged) built 12,072,602 bytes; wasm_page_check.sh on it: imports ok, CPU clock answered as ruby (0.0, EINVAL), OpenSSL and network Wasm checks pass; the page's output matches the native build
python3 tools/ci_timing_check.py 21 tests OK (the needs list includes the new jobs)

On the previous pin (Mere 0.1.732, 9dec2ff6) the same set passed as well: corpus 1042/1042 both ways, VM 1042/0/0, rv64_smoke ok, Wasm page checks ok. On unmodified main, tools/rv64_smoke.sh fails at compile (map_get has no RISC-V lowering yet).

Native cost (measured on Mere 0.1.732, the same build of main vs this branch), alternating, median of 3. Each program prints the same output on both builds.

program instructions main this branch Δ max RSS main this branch Δ
strings (600k strings, 10 collections) 25.545 G 25.623 G +0.31% 133.6 MiB 135.4 MiB +1.33%
…same, MERE_RUBY_GC_TIGHT=1 25.887 G 25.884 G −0.01% 122.4 MiB 122.3 MiB −0.1%
trees (binary trees) 16.787 G 16.787 G 0.00% 115.0 MiB 115.0 MiB +0.01%
hashes 12.783 G 12.779 G −0.03% 220.9 MiB 221.0 MiB +0.03%
3000 × GC.start 27.306 G 27.259 G −0.17% 79.9 MiB 80.0 MiB +0.14%
300k × Math.cbrt 8.794 G 8.806 G +0.14% 77.8 MiB 77.9 MiB +0.12%
300k × Process.clock_gettime 98.581 G 98.581 G 0.00% 103.7 MiB 103.2 MiB −0.54%

The default-mode spread on strings is run-to-run: this branch's own three runs span 25.57–25.65 G and 140.0–141.9 MB. Under GC_TIGHT, the setting for comparing RSS between builds, the two builds are equal.

Nothing in CI built mere-ruby for the two targets with no C library, and
both had stopped building:

- RV64 no longer compiled. Three callbacks were applied as
  `(map_get h 0) ()` / `(vec_get v 0) ()`, a builtin's result applied in
  the same application, which the pinned Mere's RISC-V backend refuses.
  They are written `let f = ... in f ()` now (the same on every backend).
- Past that, every RV64 program exited 1 before its first line: the
  process CPU clock extern is read at startup and around every
  collection, outside any Ruby frame, and the machine has no C library.
  It is read through one function now, which answers the host's
  "no clock" (a negative Float) when the extern is refused. NKF's
  cleanup at exit is guarded the same way.
- Several C shims replaced answers the RISC-V backend gives itself.
  Where the shim only refines such an answer, its absence falls back to
  it: Math.cbrt is libm's cbrt, File::Stat has whole-second times,
  Process.clock_gettime reads the wall clock, IO's pathconf names are
  empty (so `require "etc"` -- and tmpdir and fileutils -- work), and a
  child's descriptors have no non-blocking flag to clear. Process.uid
  and the rest with no real answer stay NotImplementedError. Each
  guard is a small function outside the builtin dispatcher (a try_or
  inside it made every collection ~0.5% dearer though the arm never
  ran), and Math.cbrt and the clock reader ask once, at first use,
  whether their shim is there.
- Where there is no process CPU clock, the answers are now CRuby's own
  when its gc.c finds none: GC::Profiler enables and records 0.0, and
  Process.clock_gettime(:CLOCK_PROCESS_CPUTIME_ID) raises Errno::EINVAL
  as process.c's clock_failed does (it was NotImplementedError). The
  host contract is unchanged.
- The Wasm source no longer emitted: tools/wasm_lexer.py's copy of
  number_nls_go still called tc_number, removed in 00145fd. The copy
  follows the original again, and each original the script copies is
  pinned by its SHA-256, so the next change to one stops the build
  naming it. The page's host gained the four imports the module grew
  (prism_error_spans, mr_fd_clear_nonblock, mr_clock_read,
  mr_sock_accept).

CI gains two ubuntu-24.04 jobs with no dependencies: rv64-build compiles
the --coro tree for RV64 and runs tools/rv64_smoke.rb on memu's emulator
(startup, a collection, a module body's require, the fallbacks, the CPU
clock answers, a clean exit); wasm-build emits the playground's module
from the checked-out source and runs tools/wasm_page_check.sh on it. The
OpenSSL and network Wasm checks move there: in the corpus job they
tested the module committed under docs/, not the source.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 9ba10b19ad9613cf02f21b9f650c4fe44920d2d7 against current main f0f372e7dc00967ae881f599eae68758648a9148 in isolated checkouts. One Major (P2) finding: the new RV64 descriptor fallback reports that nonblocking mode was cleared although it remains set, causing redirected child output to fail. Details and a verified reproduction are inline.

The new source-built Wasm job, lexer-copy freshness pins, and strict RV64 output/status checks cover useful gaps. Independent validation on macOS arm64 used freshly built pinned Mere 9dec2ff687a08433247dbdb7729bc19c8522e538, pinned mgz 661a01d8, and memu 7564b424:

  • Current main's RV64 compile reproduces the map_get refusal in m_numeric.mere; this head's actual rv64_smoke.sh passes (RV64 emission: 46 seconds). The separate child-descriptor probes reproduce the finding with the same head and pins.
  • Fresh ordinary O2 native build succeeds. Ten original corpus files covering math/cbrt, GC/profiling, clocks and related behavior, plus two child-descriptor probes, match pinned CRuby 4.0.10 in default, hash-index-off and GC-tight modes: 36/36 exact exit/stdout/stderr comparisons. The descriptor failure is on RV64; these native controls pass.
  • Fresh source Wasm emission and wat2wasm assembly succeed. The complete wasm_page_check.sh passes: actual-page import instantiation, unavailable CPU-clock semantics, portable OpenSSL, and portable network checks. Additional Wasm probes pass for realtime/monotonic clock separation, unavailable thread CPU time, profiler clear/re-enable with zero timings, continuation lexing/interpolation and eval backtrace line numbers. Node 22.20.0; unoptimized module.
  • CI timing tests: 21 pass. GC roots, duplicate/dead definitions, shell syntax and diff whitespace checks pass.

This is focused review evidence, not a complete local corpus/upstream/platform acceptance run. The exact-head hosted RV64 and Wasm jobs have passed; other CI jobs are still executing or queued. The latest main CI run 46362164472 was inspected across all 25 jobs and retains the existing build-linux Module-gate failure. That baseline is not claimed repaired or waived here.

No merge attempted: this PR is a draft, its stated compiler prerequisite remains unmerged, and the recorded global merge holds remain active. COMMENT is used because the authenticated account owns this PR; the P2 issue should be fixed before merging.

Comment thread m_state.mere Outdated
let stat_times_or_none = fn (p: str) -> fn (follow: int) -> fn (fd: int) ->
try_or (fn (u9: unit) -> mr_stat_times p follow fd) "";
let pathconf_names_or_none = fn (u: unit) -> try_or (fn (u9: unit) -> fd_pathconf_names ()) "";
let fd_clear_nonblock_or_none = fn (fd: int) -> try_or (fn (u9: unit) -> mr_fd_clear_nonblock fd) 0;

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Major / P2] Actually clear O_NONBLOCK before reporting success on RV64

An absent C shim does not mean there is no flag to clear: the pinned RV prelude's io_set_nonblocking sets O_NONBLOCK through the emulator's host, and IO.pipe uses it on both ends. This fallback returns 0 without changing that flag. Process.__std_blocking then proceeds, and the __fdblock caller even removes the runtime's nonblocking bookkeeping while the host descriptor stays nonblocking.

Verified on this exact head with pinned Mere and memu: spawn /usr/bin/python3 with out: w from r, w = IO.pipe, and have the child inspect fcntl.fcntl(1, fcntl.F_GETFL) & os.O_NONBLOCK. RV64 prints True; CRuby 4.0.10 and the native O2 build print False. A child looping over os.write(1, remaining_bytes) to write 1 MiB produces only 65,536 bytes on RV64, then raises BlockingIOError and exits 1; both controls write all 1,048,576 bytes and exit 0.

Provide a real target operation to clear the flag (including emulator support), or retain an explicit unsupported result until it exists, rather than reporting a successful no-op. Please extend the RV64 smoke coverage with a redirected child/pipe case that checks the flag or writes beyond pipe capacity.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ae96536. You're right that the fallback reported a successful no-op.

There is no real clear operation on RV64 to call. The pinned RV prelude's io_set_nonblocking is the only fcntl path, and memu's fcntl(F_SETFL) handler only ever sets O_NONBLOCK; an F_SETFL without it returns 0 and changes nothing. So the fallback no longer answers "cleared":

  • fd_clear_nonblock_or_none (m_state.mere, now after fd_nb) keeps the explicit unsupported result for any descriptor this process made non-blocking. The fd_nb bookkeeping is filled only by this process's own io_set_nonblocking and is carried across dup/dup2. For those descriptors the shim is called unguarded, so on RV64 it refuses by name and Ruby sees NotImplementedError: mr_fd_clear_nonblock() function is unimplemented on this machine, as before this PR. spawn(cmd, out: w) with r, w = IO.pipe now raises that before any child starts.
  • A descriptor it never made non-blocking was clear, which is the shim's own 0. So system, a plain spawn, IO.popen (argv and string) and backticks still run on RV64; I checked all of them on memu.
  • tools/rv64_smoke.rb now runs system("true") (expects true) and spawn("true", out: w) on an IO.pipe end (expects NotImplementedError). The same smoke on the previous head fails on exactly that line: the spawn ran.
  • KNOWN_GAPS records the gap. Native is unchanged: your probe prints False and transfers all 1,048,576 bytes with exit 0, as CRuby does.

Rerun on Mere 0.1.733 (72bb0d09): run_corpus.sh --both 1042/1042, VM corpus 1042/0/0, rv64_smoke.sh ok, Wasm emit plus wasm_page_check.sh ok.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up in 58f8798, addressing the inherited-descriptor case from the latest review. You're right that a missing fd_nb entry proves nothing about an inherited descriptor. The emulator's host cannot read the flag either (memu's F_GETFL always answers read-write), so its state can't be established.

fd_clear_nonblock_or_none now answers "was clear" (the shim's own 0) only when this process opened the descriptor (fd_acc, which covers files, sysopen, pipes and dup/dup2 copies of those) and never made it non-blocking (fd_nb). Every other descriptor calls the shim unguarded and refuses as NotImplementedError: mr_fd_clear_nonblock() function is unimplemented on this machine. That covers pipe ends this process made non-blocking, and also inherited stdio.

On RV64 a plain system(cmd), spawn(cmd) or IO.popen that would hand the child the process's own stdio is therefore refused again, as it was before this PR. A spawn whose three standard descriptors are files the process opened still runs; I checked system("echo six", in: File.open(File::NULL), out: f, err: f) on memu. Backticks don't go through Process.__std_blocking on any platform here, so they are unchanged.

Gate:

  • tools/rv64_smoke.rb checks three cases: the file-redirected system runs (true), the pipe-end spawn refuses, and a plain system("true") refuses.
  • tools/rv64_smoke.sh runs the program twice: as started, and again with the emulator's own stdout made non-blocking by the launcher (perl fcntl(F_SETFL, … | O_NONBLOCK), verified to reach a host child as True).
  • The same smoke on ae96536 fails in both runs, on the inherited-stdio line (:spawned_with_inherited_stdio).

KNOWN_GAPS describes the rule. Rerun on Mere 0.1.733: run_corpus.sh --both 1042/1042, VM corpus 1042/0/0, rv64_smoke.sh ok (both runs), Wasm emit plus wasm_page_check.sh ok. Native is unchanged and matches CRuby on your probe.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alias case (reviews 6118985455 and 6122492601) fixed in 90508b3. You're right: O_NONBLOCK belongs to the open file description, and fd_nb was keyed by descriptor. I missed 6118985455 when it was posted; sorry for the extra round.

The runtime now tracks the description:

  • fd_desc maps each descriptor to a description id. dup and dup2 copy it (carry), and an open makes a new one.
  • IO.__fdnonblock records the flag on the description (desc_nb) as well as on the descriptor. Setting it through any alias, including before or after the dup and even after the original is closed, makes every alias "not known clear".
  • desc_ext marks a description something outside this bookkeeping may hold: a child it was already handed to (marked when __fdblock answers "clear" for it), or a descriptor opened through /dev/fd/N, /dev/std* or /proc/…. fd_keep descriptors, which every child inherits, count too.

fd_clear_nonblock_or_none answers the shim's 0 only for a descriptor that this process opened, whose description never had the flag set through any alias, and that nothing outside may hold. Everything else still refuses as NotImplementedError. A plain dup or dup2 of a blocking descriptor still runs; that matters because IO.new's validity probe is itself a dup.

Checked on memu (Mere 0.1.733, memu 7564b424):

  • Run: a fresh IO.for_fd(IO.sysopen(…)), a plain dup, a plain dup2 (reopen).
  • Refused: dup then non-blocking on the original (also with the original then closed), dup of a non-blocking descriptor, dup2 then non-blocking through either end, and a File handed to a second child.
  • tools/rv64_smoke.rb pins a plain dup (runs), a dup whose original is then made non-blocking (refused), and a second hand-off of the same files (refused).
  • Native is unchanged and matches CRuby, including a pipe end dup'd and then made non-blocking through the original: False, 1,048,576 bytes, exit 0.

Rerun: run_corpus.sh --both 1042/1042, VM corpus 1042/0/0, rv64_smoke.sh ok. docs/build.sh rebuilt the page module from this head, with all page checks passing and output matching native.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inheritance gap (review 6122907035) addressed in 0e30b09 by removing the fallback rather than extending it.

Every rule that answers "clear" from this process's bookkeeping has been beaten by a path the bookkeeping can't see: first inherited stdio, then aliases, now a description that an earlier child inherited through a kept descriptor and changed. The emulator's host can't read the flag either, so there is no state to reconcile against. Rather than track every hand-off and its aliases (and their lifetimes, see the memory thread), IO.__fdblock now calls mr_fd_clear_nonblock exactly as main does.

On RV64 that means a spawn, system or IO.popen raises NotImplementedError: mr_fd_clear_nonblock() function is unimplemented on this machine, as it did before this PR. That is the explicit unsupported result, with no fake "cleared". A backtick on its one-call path still runs; with a descriptor above 2 held, or off the main thread, it is refused the same way.

The descriptor bookkeeping (carry, the open paths, IO.__fdnonblock, fd_forget) is main's again, and native is unchanged on this path. tools/rv64_smoke.rb now expects the refusal for redirected stdio, a pipe end, a plain system, IO.popen and a held-descriptor backtick, and the one-call backtick to run. KNOWN_GAPS describes the limit.

Rerun on Mere 0.1.733: run_corpus.sh --both 1042/1042, VM corpus 1042/0/0, rv64_smoke.sh ok. docs/build.sh rebuilt the page module from this head, with all page checks passing and output matching native.

The pinned compiler (MERE_REF, and `mere >=` in mere.toml) moves to Mere
0.1.733, whose RISC-V backend lowers a builtin applied to more arguments
than its arity, as the other backends do. The three callbacks rewritten
as `let f = ... in f ()` stay as they are; RV64 also compiles with them
undone. The README names the same pin.
@284km
284km marked this pull request as ready for review November 30, 2026 12:49
mere.toml states the oldest Mere the source builds on, and with the three
`let f = ... in f ()` rewrites that is still 0.1.731: the gem-suite
workflows, which pin a 0.1.732 compiler of their own, refused the package
under a 0.1.733 floor. The main CI job keeps MERE_REF at 0.1.733, so the
RV64 gate also exercises that compiler's fix. The README says both.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of exact head f3986038ec12bc22a6ff301466f77ab2bf81c4f7 against current main f0f372e7dc00967ae881f599eae68758648a9148 in a fresh isolated checkout. The compiler prerequisite merelang/mere#96 is now merged, and this PR is ready for review. The changes since my previous review update the compiler pin and README; the runtime and smoke-test sources are unchanged.

The existing Major/P2 finding remains unresolved: RV64 child descriptors stay nonblocking. Independently rebuilt the exact new Mere pin 72bb0d0980f22c067c323695815c15bc20331898 (0.1.733) and reproduced both probes with pinned memu 7564b424: the redirected child observes O_NONBLOCK=True, and a 1 MiB write yields only 65,536 bytes, BlockingIOError, and child exit 1. Native and CRuby controls clear the flag and transfer all 1,048,576 bytes with child exit 0. The compiler update therefore does not fix the original finding. Please provide an actual clear operation, or preserve explicit unsupported behavior, and cover redirected child output in the RV64 gate. I have left the original thread intact and added no duplicate inline finding. No additional actionable code finding in this follow-up.

Independent validation on macOS arm64, with the exact pinned compiler freshly built, pinned mgz 661a01d8, and no reused runtime binary:

  • Fresh ordinary O2 native build succeeds. Ten original corpus files covering math/cbrt, GC/profiling and clocks, plus both descriptor probes, match CRuby 4.0.10 in default, hash-index-off and GC-tight modes: 36/36 exact exit/stdout/stderr comparisons.
  • The actual rv64_smoke.sh passes (RV64 emission: 50 seconds). Its startup, fallback and unavailable-clock assertions remain useful, but do not exercise the failing child/pipe behavior above.
  • Fresh Wasm source emission and wat2wasm assembly succeed. The complete wasm_page_check.sh passes on that fresh unoptimized module: actual-page import instantiation, unavailable CPU-clock semantics, portable OpenSSL, and portable network checks (Node 22.20.0).
  • CI timing tests: 21 pass. Freshly emitted-C GC-root accounting, dead-definition, StrBuf-region, shell syntax and diff whitespace checks pass.

These are focused correctness checks, not a complete local corpus, full upstream/platform or memory-acceptance run. Exact-head hosted RV64 and Wasm jobs have passed. Outstanding hosted checks at posting: Upstream Rake tests / macos-26 (in_progress), Small upstream gem tests / macos-26 (in_progress), build-macos (in_progress), build-linux (in_progress). Downstream CI coverage is not yet complete.

No merge: the reproduced P2 remains, hold_all_merges / new_failure_review_required remain active, and executing or missing downstream coverage is outside the unassigned-runner exception. Inspected all 25 jobs of latest main CI 46362164472; its build-linux Module gate remains a pre-existing failure (195 tests / 1,234 assertions, zero failures, two internal timeout errors in test_iclass_memory_leak and test_complemented_method_entry_memory_leak; no outer-limit event in the retained artifact). This review neither attributes that baseline to this PR nor clears the holds. Already-reviewed #410, #414 and #415 were also considered and remain on hold. COMMENT because the authenticated account owns the PR.

A child's standard three are made blocking before it starts
(Process.__std_blocking, as ruby clears them before exec). Where the
mr_fd_clear_nonblock shim is absent this answered 0 ("was clear"), but on
RV64 the emulator's host does set O_NONBLOCK -- IO.pipe sets it on both
ends through io_set_nonblocking -- and has no way to clear it: memu's
fcntl(F_SETFL) only ever sets the flag. A child spawned with `out: w` from
IO.pipe then saw a non-blocking stdout, and a 1 MiB write stopped at
65,536 bytes with EAGAIN.

There is no fallback that clears it, so a descriptor this process made
non-blocking (fd_nb, which only its own io_set_nonblocking fills and
dup/dup2 carry) still refuses by the shim's name, which reaches ruby as
NotImplementedError, as before. One it never made non-blocking was clear
(the shim's own 0), so system, a plain spawn, IO.popen and backticks still
run. tools/rv64_smoke.rb now runs a plain system and a spawn whose stdout
is a pipe end, which must refuse; KNOWN_GAPS records the gap.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of exact head ae965365ea9ae1f1cac94bf5075be8ae56e3f217 against current main f0f372e7dc00967ae881f599eae68758648a9148, using a fresh isolated checkout.

The existing Major / P2 finding is only partially fixed. The original nonblocking child-descriptor finding now correctly refuses pipe ends created by this interpreter, including their dup/dup2 copies, before starting a child. However, m_state.mere:6197–6199 still treats an absent fd_nb entry as evidence that the descriptor is blocking. An inherited standard descriptor can already have O_NONBLOCK set by the parent; it never passes through this process's IO.__fdnonblock, so its bookkeeping entry is absent. The fallback then catches the unavailable shim and reports success without clearing the actual flag.

Independently reproduced on this exact head and pinned memu: create an OS pipe in the launcher, set O_NONBLOCK on its write end with fcntl, and use that end as the emulator's stdout. Ruby code calling system('/usr/bin/python3', '-c', 'import fcntl,os; print(bool(fcntl.fcntl(1,fcntl.F_GETFL)&os.O_NONBLOCK))') prints True on RV64, while CRuby 4.0.10 and the fresh native O2 build print False. With the same launcher, a child looping over os.write to send 1 MiB, and a reader paused for 0.5 seconds after the child's initial flag line, RV64 transfers only 65,536 bytes, raises BlockingIOError, and exits 1 (system returns false). Both controls transfer 1,048,576 bytes and the child exits 0. The emulator/interpreter itself exits 0 in all three runs; this is a child-output failure, not a sandbox denial.

Please provide a real clear operation, or preserve explicit unsupported behavior for inherited descriptors whose blocking state cannot be established when the shim is absent. Add a smoke case starting the emulator with an inherited nonblocking standard descriptor. I have left the original thread intact and have not added a duplicate inline finding.

Independent validation on macOS arm64:

  • Reused the clean pinned compiler build at 72bb0d0980f22c067c323695815c15bc20331898 (Mere 0.1.733); freshly built the ordinary O2 native interpreter, RV64 program, and emulator. Pinned mgz 661a01d8 and memu 7564b424; no native interpreter binary reused.
  • Actual rv64_smoke.sh passes (RV64 emission: 42 seconds). Additional RV64 checks pass for plain spawn/wait, system, IO.popen and backticks. Pipe, dup and dup2 redirects each raise NotImplementedError before a child marker file is created. The inherited-descriptor probes above still fail the required behavior.
  • Ten original math/GC/profiler/clock corpus fixtures and the original flag/1-MiB pipe probes match CRuby 4.0.10 in default, hash-index-off and GC-tight modes: 36/36 exact native exit/stdout/stderr comparisons.
  • Fresh Wasm source emission and wat2wasm assembly succeed. The complete wasm_page_check.sh passes on that fresh unoptimized module: actual-page import instantiation, unavailable CPU-clock semantics, portable OpenSSL and portable network checks (Node 22.20.0).
  • CI timing tests: 21 pass. Freshly emitted-C GC-root accounting, dead-definition, StrBuf-region, shell syntax and diff whitespace checks pass.

These are focused correctness checks, not a full corpus, full upstream/platform or memory-acceptance run. Exact-head hosted RV64 and Wasm jobs pass. Outstanding hosted checks at the final inspection:

  • build-macos: in_progress
  • Small upstream gem tests / macos-26: in_progress
  • Upstream gem tests / macos-26: in_progress
  • build-linux: in_progress
  • unittest-reference: queued
  • Upstream Rake tests / macos-26: queued
  • Additional upstream gem tests / macos-26: queued
  • runtime-load-path (macos-15): queued

No merge: the reproduced P2 remains, the recorded hold_all_merges / new_failure_review_required flags remain active, and executing/missing downstream coverage is outside the unassigned-runner exception. Inspected all 25 jobs of latest main run 46425224035: 19 successful, four cancelled without assigned runners or steps, two skipped; the overall run reports failure. Its Linux/macOS build jobs pass. The earlier main Module timeout/RSS failures remain preserved; this run and these focused checks do not by themselves clear the recorded holds. Already-reviewed #410, #414 and #415 were also considered and remain subject to those holds; their skipped VM checks are not claimed as passes. COMMENT because the authenticated account owns this PR.

The previous commit answered "was clear" for any descriptor this process
had not made non-blocking itself. An inherited one -- the process's own
standard three -- can already be non-blocking: the parent may have set
it, and the emulator's host cannot read the flag (its F_GETFL answers
read-write) any more than clear it. A child given such a stdout by a
plain system() then stopped at 65,536 bytes of a 1 MiB write with EAGAIN.

Where mr_fd_clear_nonblock is absent, the only descriptor now answered
"clear" is one this process opened (fd_acc) and never made non-blocking
(fd_nb). Every other one refuses by the shim's name, as NotImplementedError:
on RV64 a plain system/spawn/IO.popen is refused again, as it was before
these changes, and one whose standard three are files the process opened
runs. tools/rv64_smoke.rb covers both, and tools/rv64_smoke.sh runs it a
second time with the emulator's own stdout non-blocking.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of exact head 58f879804e1ec7a386e22c6fa56547608b6b46f4 against current main f0f372e7dc00967ae881f599eae68758648a9148, using a fresh isolated checkout.

The inherited-descriptor case from the previous review is fixed: inherited stdout and already-nonblocking pipe ends, including their dup/dup2 copies, are refused before the child starts. Independent checks confirmed no child marker was created, both with ordinary stdout and with stdout made nonblocking by the launcher. The expanded actual rv64_smoke.sh passes both runs. The stricter fallback and regression coverage are useful improvements.

[Major / P2] The existing nonblocking-child finding remains reproducible through an alias changed after duplication. This is a remaining case of the original finding, so I have not added another inline thread.

m_state.mere:6200–6201 still treats fd_acc present plus fd_nb absent as proof that a descriptor is blocking. However, carry copies the current bookkeeping at dup time (main.mere:58535–58539), while __fdnonblock subsequently marks only the descriptor passed to it (main.mere:58668–58669). The kernel's O_NONBLOCK belongs to the shared open file description and changes for the earlier duplicate too. For example, with an existing FIFO:

require "io/nonblock"
f = IO.for_fd(IO.sysopen("output.fifo", "r+"), "r+")
g = f.dup
f.nonblock = true
# Spawn a child with out: g and with stdin/stderr redirected to opened files.

g has fd_acc but no fd_nb, so the fallback reports success without clearing its actual flag. On this exact head, a child inspecting stdout with Python's fcntl(F_GETFL) sees True on RV64; native and CRuby see False. A child looping over os.write to send 1 MiB through that FIFO, with the external reader delayed 0.5 seconds after the flag report, transfers only 8,192 bytes, raises BlockingIOError, and exits 1 on RV64 (system returns false). Fresh native O2 and CRuby 4.0.10 controls both transfer 1,048,576 bytes, child exit 0, and system returns true. All interpreter processes exit 0; this is an actual child-output failure, not a sandbox restriction.

Please track blocking state across aliases of the shared open file description, or conservatively preserve unsupported behavior when that state cannot be established. Cover duplication before setting nonblocking mode, in addition to duplication of an already-nonblocking descriptor. The same bookkeeping assumption is also used by dup2; audit that route with the fix.

Independent validation on macOS arm64:

  • Reused the clean pinned Mere 0.1.733 compiler build (72bb0d0980f22c067c323695815c15bc20331898), with mgz 661a01d8 and memu 7564b424. Fresh ordinary O2 native build and fresh exact-head RV64 program; the actual smoke script also rebuilt its emulator. Additional probes reused the earlier emulator built with these same pins.
  • Actual RV64 smoke passes both launches; additional pipe/dup/dup2/inherited refusal checks pass. The alias/FIFO probe above fails the required behavior.
  • Ten original math/GC/profiler/clock corpus fixtures plus the original flag and 1-MiB pipe probes match CRuby in default, hash-index-off and GC-tight modes: 36/36 exact exit/stdout/stderr comparisons.
  • CI timing tests: 21 pass. Fresh emitted-C GC-root accounting, dead-definition, StrBuf-region, shell syntax and diff whitespace checks pass.

These are focused review checks, not complete local corpus, upstream/platform or memory acceptance. Exact-head hosted RV64, Wasm, build-linux and build-macos jobs pass. At the immediate pre-post CI inspection: SKIPPED: CRuby tests / the evaluator against the ISeq VM. These checks are not claimed as passes; the hosted acceptance is incomplete.

No merge: the reproduced P2 remains, and the recorded hold_all_merges / new_failure_review_required flags remain active. Inspected all 25 jobs of latest main CI 46425224035: 19 successful, four cancelled, two skipped; overall failure, with both build jobs successful. Earlier main Module timeout/RSS failures remain preserved; these focused checks do not clear those holds. Already-reviewed #410, #414 and #415 were also considered: each currently has a skipped VM check and remains subject to the global holds. No open PRs remain in merelang/mere. COMMENT because the authenticated account owns this PR.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review of exact head 58f879804e1ec7a386e22c6fa56547608b6b46f4, compared with current main f0f372e7 (the merge base, so the branch includes every source gate on main). I built Mere 72bb0d09 (0.1.733) from source and used memu 7564b424. Everything ran on macOS arm64 in a fresh clone.

Summary: native behaviour is unchanged from main, and the final O_NONBLOCK rule is sound. I found three Minor issues (a fake thread-CPU-clock value on RV64, an inaccurate backtick guarantee, and the committed playground module no longer being checked) and one Suggestion. Details are inline.

Native: unchanged from main

  • With the same main source, the C emitted by the old pin (9dec2ff6, 0.1.732) and by the new pin (72bb0d09) is byte-identical. The MERE_REF bump changes nothing for native builds.

  • sh run_corpus.sh --both on this head: 1042/1042 match ruby, hash index ON and OFF. sh tools/iseq/vm_corpus.sh -j 3: 1042 on the VM, 0 fallbacks, 0 FAIL.

  • I wrote a probe for each changed native path:

    • Math.cbrt on 27, -8, 1e-300 and 0
    • the profiler's GC_TIME / GC_INVOKE_TIME
    • the process and thread CPU clocks, and the GETRUSAGE_BASED_ symbol
    • stat and fstat nanoseconds
    • Etc PC_*
    • a spawn(out: w) child checking O_NONBLOCK and writing 1 MiB
    • system(out: File)

    Its output is identical for CRuby 4.0.10, main and this head. The two mere-ruby builds also give the same output under MERE_RUBY_VM=1, MERE_RUBY_GC_STRESS=20, and both together.

  • Placement of the try_ors: every guard is emitted as its own static function containing the _setjmp (mu_math_cbrt, mu_stat_times_or_none__direct, mu_fd_clear_nonblock_or_none__direct, mu_gc_cpu_seconds__direct, …). LLVM never inlines a returns-twice function into its caller, so invoke_val_c0_inner gets no new setjmp.

  • Instruction counts, main against this head, median of 3, alternating:

    • 300k Math.cbrt: 11.013 G → 11.028 G (+0.14%)
    • 2000 × GC.start: 16.682 G → 16.623 G (−0.35%)
    • 200k monotonic clock_gettime plus 4k File.stat: 70.928 G → 70.963 G (+0.05%)

    This matches the author's figures: no measurable cost.

The O_NONBLOCK rule (after the bot's Major)
I checked whether any descriptor is wrongly answered "clear". Every place that sets O_NONBLOCK goes through IO.__fdnonblock, so it is recorded in fd_nb:

  • IO.pipe, FIFOs, and fcntl(F_SETFL, NONBLOCK) in __fd_fcntl
  • sockets (socket_ext), pty and io/console

carry copies fd_nb across dup and dup2, and fd_forget drops it on close. fd_acc only holds descriptors this process opened, and they open blocking. So "in fd_acc, not in fd_nb" is a fair proof of "clear". Inherited descriptors (0–2, and anything wrapped with IO.for_fd) refuse.

On native, the only change is that this one case is wrapped in try_or, and the shim is still called. I found no regression. The rule does have a side effect on backticks that the docs don't describe: see the inline comment on KNOWN_GAPS.

Gates

  • At this head, the hosted rv64-build and wasm-build jobs really ran. Their logs show prog.bin is 25281929 bytes / rv64_smoke: ok, and SMOKE_WASM=/home/runner/work/_temp/mere-ruby.wasm with both page-check lines printed.
  • Locally, tools/rv64_smoke.sh passes on this head (both runs), with max RSS 3.4 GB. My prog.bin is the same size as CI's.
  • rv64_smoke.sh can't pass vacuously. A compile error, a missing or extra line, any stderr, or a non-zero exit all fail it, and the expected lines come from the committed file.
  • SMOKE_WASM reaches openssl_wasm_check.py and network_wasm_check.py, which inherit it through subprocess, so in CI all three checks run on the fresh module.
  • Both new jobs are in ci-timing's needs and have no if: that could skip them.
  • tools/wasm_lexer.py: the SHA-256 pins make the script fail loudly, naming the function, when an original changes.

Findings

  • Minor: on RV64, CLOCK_THREAD_CPUTIME_ID returns the wall clock (epoch seconds), where CRuby, the playground stub and this build's own process CPU clock all raise EINVAL. (main.mere, inline)
  • Minor: KNOWN_GAPS and the PR body say backticks always run on RV64. They are refused whenever a descriptor above 2 is held (__bt_async → spawn → __std_blocking). Reproduced on memu. (inline)
  • Minor: tools/wasm_page_check.sh fails on its default target, the committed docs/mere-ruby.wasm from #80, and after this PR CI no longer checks the module the page serves. (inline)
  • Suggestion: the second smoke run (non-blocking stdout) takes the same decision path as the first and adds a full emulator run in CI. (inline)

I found no Critical or Major issues. The author's local results are in the PR body. Once the three Minors are resolved, I see nothing else blocking.

Comment thread main.mere Outdated
let clock_read_any = fn (kind: int) ->
let _ = (if vec_len clock_read_have > 0 then ()
else vec_push clock_read_have (try_or (fn (u9: unit) -> let _ = mr_clock_read 1 in 1) 0)) in
if vec_get clock_read_have 0 == 1 then mr_clock_read kind else time ();

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Minor] With no clock reader, CLOCK_THREAD_CPUTIME_ID returns the wall clock instead of raising Errno::EINVAL

When mr_clock_read is absent, every kind falls back to time (), including kind 4 (this thread's CPU time). On the RV64 build that gives a made-up value. I checked it on memu 7564b424 with the smoke's own prog.bin:

p Process.clock_gettime(Process::CLOCK_THREAD_CPUTIME_ID)          # => 1796069266.066078  (epoch seconds)
Process.clock_gettime(:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID)        # => Errno::EINVAL

That breaks the rule this PR sets for the CPU clocks in three ways:

  • on the same build, the process CPU clock raises Errno::EINVAL, but the thread CPU clock returns a large positive number;
  • the playground's new mr_clock_read stub returns -1 for kind 4, so the browser raises EINVAL where RV64 returns the wall clock;
  • CRuby raises through clock_failed for a clock it cannot read. It does not substitute another clock.

Code that measures CPU time with the thread clock (benchmark-style t1 - t0) would charge wall time, sleeps included, without any error.

Suggested fix: ... else if kind == 4 then 0.0 - 1.0 else time (). The prelude already turns a negative result into Errno::EINVAL. Then change KNOWN_GAPS ("reads the wall clock for every clock id") to name the exception, and consider a # => line in tools/rv64_smoke.rb for it. This branch is never taken on native, so native output is unchanged.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9ed6726. When mr_clock_read is absent, kind 4 (CLOCK_THREAD_CPUTIME_ID) now answers -1.0, which the prelude raises as Errno::EINVAL, the same as the process CPU clock on that build, the page's stub and CRuby's clock_failed. The other kinds still read time (). Native has the reader, so it never takes this branch (CLOCK_THREAD_CPUTIME_ID still returns a Float there, as in CRuby). KNOWN_GAPS now says the wall clock stands in for every clock id but the CPU ones, which raise. tools/rv64_smoke.rb has a # => Errno::EINVAL line for the thread clock, and rv64_smoke.sh passes on Mere 0.1.733.

Comment thread KNOWN_GAPS.md Outdated
inherited stdio, which the parent may have made non-blocking -- so a plain
`system("cmd")` is refused there. One whose standard three are all redirected
to files this process opened runs (`system(cmd, in: File.open(File::NULL),
out: f, err: f)`, with `f` a File opened for writing). Backticks do not clear the child's descriptors on any

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Minor] Backticks are refused on RV64 whenever the program holds a descriptor above 2, but this paragraph and the PR body say they always run

This sentence, the PR body's RV64 probe row ("backticks run") and the reply in the O_NONBLOCK thread all say backticks never go through Process.__std_blocking. That is only true on the one-call path. main.mere sends a backtick to Process.__bt_async in two cases: when it runs off the main thread, or when the program holds any descriptor above 2 that is not kept across exec (held9). __bt_async is spawn(cmd, out: file), so it goes through __std_blocking. With this head's rule, that refuses on fd 0 and 2, which are inherited.

On memu 7564b424 with this head's prog.bin:

p `echo plain`                      # => "plain\n"
f = File.open("bt.rb"); f.fileno    # (forces the descriptor open)
p `echo held`                       # NotImplementedError: mr_fd_clear_nonblock() function is unimplemented on this machine
r, w = IO.pipe
p `echo pipe_held`                  # same NotImplementedError

Refusing here is the safe choice, and I'm not asking for a different runtime rule. But the documented guarantee is wrong. "Backticks run" doesn't hold for any program that has an open File with a descriptor or an IO.pipe, or for one that runs inside a Thread. Please reword this paragraph and the PR body. A # => line in tools/rv64_smoke.rb for one held-descriptor backtick would keep the documented behaviour pinned.

@284km 284km Nov 30, 2026 •

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9ed6726. You're right; my earlier reply in the O_NONBLOCK thread was wrong about backticks too.

  • The KNOWN_GAPS paragraph now says a backtick runs on its one-call path, which clears nothing. Once the program holds a descriptor above 2 that isn't kept across exec (any open File or IO.pipe; that is held9), or calls it off the main thread, the backtick spawns through Process.__bt_async and is refused like a plain system.
  • The PR body's description and its RV64 probe row say the same. The runtime rule is unchanged.
  • tools/rv64_smoke.rb pins both cases: p `echo plain` before any file is opened (# => "plain\n"), and a backtick after the File::NULL descriptors are open (# => NotImplementedError).

Comment thread tools/wasm_page_check.sh
# not have is answered as ruby answers one it does not have, and the portable
# OpenSSL and network layers work there.
#
# sh tools/wasm_page_check.sh docs/mere-ruby.wasm

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Minor] The default target fails on this branch, and nothing in CI checks the module the page serves any more

Run as documented, with no SMOKE_WASM, this checks the committed docs/mere-ruby.wasm. That file was last rebuilt in #80 (2026-11-12), and on this head the check fails:

every import instantiates with index.html's actual host
(playground):1:in '<main>': undefined method '[]' for nil (NoMethodError)
the playground did not answer the unavailable CPU clock as ruby does

The PR also removes openssl_wasm_check.py from the corpus job, so wasm-build now checks only a freshly emitted, unoptimized module. The module the published page actually loads is not checked anywhere. KNOWN_GAPS now says the browser's profiler enables and records 0.0, and the served module doesn't do that.

Either of these would close the gap:

  • rebuild docs/mere-ruby.wasm in this PR (docs/build.sh, which runs this check on its output);
  • keep one cheap check of the committed module in CI (--check-imports at least), and have this script's default say that a rebuild comes first.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9ed6726, both ways. docs/mere-ruby.wasm is rebuilt from this source with docs/build.sh (Mere 0.1.733, binaryen 132), replacing the #80 build. That run's own wasm_page_check.sh passes on it (imports, CPU clock 0.0 / EINVAL, OpenSSL, network), and the page's output matches the native build. I skipped only the build_nkf_wasm.sh step: the WASI SDK isn't installed here, and docs/nkf.wasm is unchanged and still required to reproduce byte-for-byte by the NKF workflow. wasm-build now runs tools/wasm_page_check.sh a second time with no SMOKE_WASM, i.e. on the committed module the published page loads. That covers at least what the corpus job's openssl_wasm_check.py used to cover, plus imports, the CPU-clock probe and the network check. The script's header now says the default is the committed module, and that docs/build.sh rebuilds it and runs the script on the result.

Comment thread tools/rv64_smoke.sh Outdated
diff -u "$tmp/want.txt" "$tmp/got.txt" || ok=0
[ "$rc" -eq 0 ] || { echo "rv64_smoke: exit status $rc"; ok=0; }
[ -s "$tmp/stderr.txt" ] && { echo "rv64_smoke: stderr:"; cat "$tmp/stderr.txt"; ok=0; }
# ...and again with the emulator's own stdout non-blocking, as a parent can

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] The second run takes the same path as the first

fd_clear_nonblock_or_none never reads the flag. It decides from fd_acc / fd_nb, and the emulator's F_GETFL couldn't report the flag anyway. Here stdout is also redirected to a regular file (raw2.txt), where O_NONBLOCK has no effect on writes. So this run reaches the same NotImplementedError lines for the same reason as the first one. It would catch a future rule that special-cases 0/1/2, but the first run's system("true") line already catches that. In CI it adds a full second emulator run (about 20 s on its own). If you keep it, make it check something the first run can't: for example, give the emulator a non-blocking pipe as stdout and read from that pipe.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed; removed in 9ed6726. The rule decides from fd_acc/fd_nb and never reads the flag, so the second run took the first run's path, and the first run's plain system("true") line already pins the inherited-stdio refusal. If a future rule ever does read the flag, a non-blocking-pipe-as-stdout run that reads from the pipe, as you describe, would be the case to add. It's dropped for now, which saves about 20 s in CI.

…odule

- With no clock reader (RV64), CLOCK_THREAD_CPUTIME_ID fell back to the
  wall clock like every other kind, a made-up CPU time where this build's
  process CPU clock, the page's stub and ruby all raise. Kind 4 now
  answers -1.0, which the prelude raises as Errno::EINVAL. Native, where
  the reader is there, is unchanged.
- KNOWN_GAPS said backticks always run on RV64. They do on the one-call
  path; once the program holds a descriptor above 2 (any open File or
  IO.pipe) or runs one off the main thread, the backtick spawns through
  Process.__bt_async and is refused like a plain system. The paragraph
  says so, and tools/rv64_smoke.rb pins a plain and a held-descriptor
  backtick, and the thread CPU clock.
- tools/rv64_smoke.sh no longer runs the program a second time with a
  non-blocking stdout: the rule never reads the flag, so that run took
  the first run's path, and the first run's plain system() already pins
  the inherited-stdio refusal.
- docs/mere-ruby.wasm is rebuilt from this source by docs/build.sh (the
  #80 module failed the new CPU-clock probe), and CI's wasm-build job now
  also runs tools/wasm_page_check.sh on the committed module, the one the
  published page loads, so it is checked again as the corpus job's
  openssl_wasm_check.py did. The script's default target is documented.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of exact head 9ed6726c1d75d9d78466186102438134796e5cb9 against current main f0f372e7dc00967ae881f599eae68758648a9148, using a fresh isolated checkout.

The thread-CPU-clock, backtick-documentation, and committed-playground findings are fixed. The freshly emitted RV64 program returns EINVAL for the unavailable thread CPU clock, and the expanded smoke matches all expected lines, including plain/held-descriptor backticks. The rebuilt committed Wasm passes the complete page check locally. Hosted wasm-build logs also confirm that both the fresh-source module and the committed module were tested successfully. Removing the redundant second emulator launch is reasonable.

[Major / P2] The existing nonblocking-child finding remains unresolved for aliases changed after duplication. This is the remaining case of the original finding, previously detailed in the alias follow-up; no duplicate inline thread added.

The new commit does not change fd_clear_nonblock_or_none or the alias bookkeeping. carry copies fd_nb at duplication time, while a later __fdnonblock marks only its argument. O_NONBLOCK belongs to the shared open file description, so an earlier duplicate can have the flag set without an entry in fd_nb. Thus fd_acc present plus fd_nb absent is still insufficient proof that it is blocking, despite the other review's conclusion.

Independently reproduced again on this exact head: open a FIFO, duplicate its descriptor, set f.nonblock = true on the original, then pass the duplicate as child stdout with stdin/stderr redirected to opened files. The RV64 child sees O_NONBLOCK=True, transfers only 8,192 / 1,048,576 bytes, raises BlockingIOError, and exits 1. Fresh native O2 and CRuby 4.0.10 controls both see False, transfer 1,048,576 bytes, and exit 0. All interpreter processes exit 0; this is an actual child-output failure, not a sandbox restriction. Please track state across shared descriptor aliases, or conservatively retain unsupported behavior when it cannot be established; cover duplication before setting nonblocking mode and audit dup2 as well.

Independent validation on macOS arm64:

  • Reused the clean pinned Mere 0.1.733 compiler (72bb0d09), with pinned mgz 661a01d8 and memu 7564b424. Fresh ordinary O2 native interpreter, fresh RV64 program (25,282,145 bytes), and freshly compiled O2 emulator; no interpreter binary reused.
  • RV64 smoke: exact expected stdout, empty stderr, exit 0. Native thread CPU clock still returns a Float, matching CRuby.
  • Original math/GC/profiler/clock corpus fixtures plus child-descriptor probes: 36/36 exact native exit/stdout/stderr comparisons with CRuby, across default, hash-index-off and GC-tight modes.
  • Committed Wasm: complete page check passes with Node 22.20.0 (imports, CPU-clock semantics, portable OpenSSL and network). Fresh-source Wasm was verified through the exact-head hosted job; not rebuilt locally in this follow-up.
  • CI timing: 21 tests pass. Fresh emitted-C GC-root accounting, dead-definition, StrBuf-region, shell syntax and diff whitespace checks pass.

These are focused checks, not complete local corpus, upstream/platform or memory acceptance. Inspected all 25 jobs of latest main CI 46425224035: 19 successful, four cancelled, two skipped; overall failure, both build jobs successful. Historical main Module timeout/RSS failures remain preserved and the recorded hold_all_merges / new_failure_review_required flags remain active.

No merge: the reproduced P2 remains, global holds remain, and executing/missing coverage is outside the unassigned-runner exception. Already-reviewed #410, #414 and #415 were also considered: each has a skipped VM check and remains subject to those holds; other open mere-ruby PRs are drafts, and merelang/mere has no open PRs. COMMENT because the authenticated account owns this PR.

Outstanding hosted checks at the immediate pre-post inspection:

  • build-linux: IN_PROGRESS
  • corpus: QUEUED
  • CRuby tests / shard 0: IN_PROGRESS
  • CRuby tests / shard 1: IN_PROGRESS
  • CRuby tests / shard 2: IN_PROGRESS
  • CRuby tests / shard 3: IN_PROGRESS
  • CRuby tests / shard 4: QUEUED
  • CRuby tests / shard 5: QUEUED
  • CRuby tests / shard 6: QUEUED
  • CRuby tests / shard 7: QUEUED
  • CRuby tests / the evaluator against the ISeq VM: SKIPPED
  • Bootstrap tests / shard 2: QUEUED
  • Bootstrap tests / shard 3: QUEUED
  • Tool tests / shard 0: IN_PROGRESS

The fallback for an absent mr_fd_clear_nonblock answered "clear" for a
descriptor this process opened and never made non-blocking. But the flag
belongs to the open file description, which dup and dup2 share:
duplicating a descriptor and then setting non-blocking through the
original left the duplicate unrecorded, and a child handed the duplicate
got a non-blocking stdout (8,192 of 1 MiB written, then EAGAIN).

The runtime now keeps a description id per descriptor (fd_desc; dup and
dup2 copy it, an open makes a new one), records non-blocking by
description (desc_nb, set by IO.__fdnonblock), and marks a description
something outside its bookkeeping may hold (desc_ext): a child it was
handed to, or a descriptor opened through /dev/fd/N, /dev/std* or /proc/.
A descriptor is answered "clear" only if this process opened it, the flag
was never set through any alias, nothing outside may hold it and it is
not kept across exec; every other one refuses as NotImplementedError. A
plain dup or dup2 of a blocking descriptor still runs: IO.new's validity
probe is itself a dup.

tools/rv64_smoke.rb adds a second hand-off of the same files (refused), a
plain dup (runs) and a dup whose original is then made non-blocking
(refused). docs/mere-ruby.wasm is rebuilt from this source. Native, where
the shim clears the flag, is unchanged.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of exact head 90508b3fc9b3f5827aadc35cd357d96a727fc3e7 against current main f0f372e7dc00967ae881f599eae68758648a9148 in a fresh isolated checkout.

The description-level tracking fixes the previously reproduced dup-before-nonblock case: it now raises NotImplementedError before starting the child. Independently checked the original-closed variant, duplication after nonblocking, and dup2 with the flag changed through either end: all refuse safely; a plain blocking dup still runs. The expanded RV64 smoke passes with exact stdout, empty stderr and exit 0. The previous thread-clock, backtick-documentation and committed-playground fixes remain intact.

[Major / P2] The existing nonblocking-child finding still has an inheritance gap. This is a follow-up to the original finding, without a duplicate inline thread. fd_keep is checked only for the descriptor currently passed to __fdblock; descriptions inherited by a child outside its standard three are never marked in desc_ext.

Reproduced on this exact RV64 head:

  1. Open a FIFO read/write, duplicate it, and set f.close_on_exec = false on the original.
  2. Start a Python child with its standard three redirected to fresh files. It inherits f above descriptor 2 and sets O_NONBLOCK on that descriptor.
  3. Set f.close_on_exec = true again, then start a second child with the duplicate as stdout and fresh stdin/stderr. The same hole also exists while the original remains kept: the duplicate has no fd_keep entry.

The second child is incorrectly allowed: it observes O_NONBLOCK=True, writes only 8,192 / 1,048,576 bytes, raises BlockingIOError and exits 1. Fresh native O2 and CRuby 4.0.10 controls observe False, transfer 1,048,576 bytes, and exit 0. All interpreter processes exit 0. This is actual child-output failure, not a sandbox denial. Mark every description a child can inherit at handoff, including kept descriptors and their aliases, and retain that history after close_on_exec is restored; add this case to the RV64 gate.

One new Major / P2 finding is inline: the new description-mark maps retain closed pipes indefinitely, including on native targets. The prior alias fix should preserve shared state until the last local alias closes, then reclaim the obsolete description metadata.

Independent validation on macOS arm64:

  • Reused the clean pinned Mere 0.1.733 compiler (72bb0d09); pinned mgz 661a01d8 and memu 7564b424. Fresh ordinary O2 native interpreter, fresh RV64 program (25,284,237 bytes), and freshly compiled O2 emulator; no interpreter binary reused for correctness checks.
  • Related original math/GC/profiler/clock corpus fixtures and two child-output controls: 36/36 exact native exit/stdout/stderr comparisons with CRuby across default, hash-index-off and GC-tight modes.
  • The original alias/FIFO reproduction is now explicitly unsupported on RV64; native/CRuby still transfer the full MiB. The separate inherited-description reproduction above remains broken.
  • Complete committed-Wasm page check passes with Node 22.20.0: actual-page imports, unavailable CPU clock, portable OpenSSL and network. Fresh-source Wasm was not rebuilt locally in this follow-up; hosted assembly has succeeded, but the full hosted Wasm job is still pending at inspection.
  • CI timing: 21 tests pass. Fresh emitted-C GC-root accounting, dead definitions, StrBuf regions, shell syntax and diff whitespace checks pass.
  • Memory probe compares this fresh O2 build with the preserved O2 build of preceding head 9ed6726c, using the same compiler/dependency pins and GC-tight mode. The two source trees differ only in this update's six files. At 10,000 pipe open/close iterations, peak RSS is 104,415,232 vs 103,333,888 bytes; at 100,000, 134,365,184 vs 121,044,992 bytes. Both print ok and exit 0. Initial sandbox restrictions blocked only time/ps statistics; the RSS measurements above were rerun successfully through approved escalation.

These are focused review checks, not full corpus, upstream/platform or memory acceptance. Latest main CI 46425224035 was inspected across all 25 jobs: 19 successful, four cancelled, two skipped; overall failure, both build jobs successful. Historical Module RSS/timeout evidence and the recorded hold_all_merges / new_failure_review_required flags are preserved.

No merge: the two P2 issues remain, global holds remain active, and executing/missing downstream coverage is outside the unassigned-runner exception. Already-reviewed #410, #414 and #415 were also considered; each remains subject to the global holds and has a skipped VM check. Other open mere-ruby PRs are drafts; merelang/mere has no open PRs. COMMENT because the authenticated account owns this PR.

Outstanding hosted checks at the immediate pre-post inspection:

  • Upstream gem tests / ubuntu-24.04: IN_PROGRESS
  • Upstream gem tests / macos-26: IN_PROGRESS
  • Upstream Rake tests / macos-26: IN_PROGRESS
  • build-macos: IN_PROGRESS
  • unittest-reference: IN_PROGRESS
  • build-linux: IN_PROGRESS

Comment thread m_state.mere Outdated
let _ = (if map_has fd_path fd then map_delete fd_path fd else ()) in
let _ = (if map_has fd_acc fd then map_delete fd_acc fd else ()) in
let _ = (if map_has fd_keep fd then map_delete fd_keep fd else ()) in
let _ = (if map_has fd_desc fd then map_delete fd_desc fd else ()) in

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Major / P2] Reclaim description marks when the last local descriptor closes

fd_forget removes the descriptor-to-description entry, but never removes that description from desc_nb or desc_ext. IDs increase monotonically, so descriptor reuse cannot overwrite those marks. Every IO.pipe marks two new descriptions in desc_nb, even on native targets where the clear-nonblock shim exists; closing both ends leaves both global-map entries permanently reachable. File descriptions handed to children similarly accumulate in desc_ext.

A long-running program that repeatedly opens and closes pipes therefore grows memory with its entire operation history, rather than its live descriptors. Verified on the fresh ordinary O2 head with MERE_RUBY_GC_TIGHT=1: 100,000 iterations of r,w = IO.pipe; r.close; w.close, with GC.start every 1,000 iterations and at the end, peak at 134,365,184 RSS bytes versus 121,044,992 for preceding head 9ed6726c built with the same pinned compiler/dependency inputs. Both executions succeed; the extra retained state is introduced by this update. At 10,000 iterations the difference is only about 1 MB, consistent with the per-description growth.

Track the number of local aliases (or otherwise collect unreferenced description IDs), preserving the marks while any alias remains and deleting both marks once none does. The metadata is no longer needed after the last known descriptor closes; future opens receive new IDs. Please cover sustained pipe churn and the case where one duplicate remains open so cleanup does not reintroduce the alias bug.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0e30b09 by removal. fd_desc, desc_nb, desc_ext and desc_of are gone, together with the fallback that read them; see the reply in the O_NONBLOCK thread. carry, IO.__fdnonblock, the open paths and fd_forget are byte-for-byte main's again, so pipe churn keeps no per-description state, on native or RV64.

The previous commits answered "clear" from local bookkeeping where
mr_fd_clear_nonblock is absent: by descriptor, then by open file
description. Each was defeated by a path the bookkeeping cannot see --
a description inherited by an earlier child through a kept descriptor
and made non-blocking there -- because the emulator's host can neither
read nor clear the flag, and the flag belongs to a description that
aliases, inherited descriptors and children all share. The description
tables also kept an entry for every pipe ever opened, on native too.

So the fallback is removed: IO.__fdblock calls the shim as main does,
and on RV64 a spawn, system or IO.popen raises NotImplementedError, as
it did before these changes. The descriptor bookkeeping (carry, the
FD-open paths, IO.__fdnonblock, fd_forget) is main's again. KNOWN_GAPS
says so; tools/rv64_smoke.rb now expects the refusal for redirected
stdio, a pipe end, a plain system, IO.popen and a held-descriptor
backtick, and the one-call backtick to run. docs/mere-ruby.wasm is
rebuilt from this source.

@284km 284km left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of exact head 0e30b093423e4aeb5cef436d742d591df4d17638 against current main f0f372e7dc00967ae881f599eae68758648a9148, using a fresh isolated checkout.

Both outstanding Major / P2 findings are fixed by removal. The descriptor maps, fd_forget, the entire io_fd_prim implementation (including dup/dup2, nonblocking state and __fdblock), and m_eval_leaf.mere are identical to current main. The added description IDs and retained-mark maps are gone. RV64 now explicitly refuses child setup instead of reporting that O_NONBLOCK was cleared. This closes the alias and inherited-description paths from the earlier reviews; it also removes the introduced history-dependent metadata retention. The limitation is clearly documented and pinned by smoke expectations. No new actionable findings.

Independent validation on macOS arm64:

  • Reused the clean pinned Mere 0.1.733 compiler (72bb0d09), with mgz 661a01d8 and memu 7564b424; built a fresh ordinary O2 native interpreter, a fresh 25,281,241-byte RV64 program and a fresh O2 emulator.
  • RV64 smoke: exact expected stdout, empty stderr, exit 0. The previous dup-before-nonblock and kept-descriptor/child-inheritance reproductions now report explicit unsupported results before starting their child; no truncated-output child is allowed.
  • Native focused math/GC/profiler/clock fixtures and child-output controls: 36/36 exact exit/stdout/stderr comparisons against CRuby 4.0.10 across default, hash-index-off and GC-tight modes.
  • 100,000 pipe open/close iterations with periodic and final GC, under GC-tight: prints ok, exit 0. Removal of the description maps is verified statically; no new RSS measurement is claimed.
  • Committed playground module: complete page check passes on Node 22.20.0 (actual-page imports, unavailable CPU clock, portable OpenSSL and network). Current-head hosted RV64 and Wasm logs independently show successful RV64 smoke and complete checks on both freshly emitted and committed Wasm modules. Fresh-source Wasm was not rebuilt locally in this follow-up.
  • CI timing: 21 tests pass. Fresh emitted-C GC-root accounting, duplicate/dead definitions, StrBuf regions, shell syntax and diff whitespace checks pass. Both lexer-copy hash guards independently reject an altered original.

These are focused checks, not full local corpus/upstream/platform acceptance or a new RSS measurement. Latest main CI 46425224035 was inspected across all 25 jobs: 19 successful, four cancelled and two skipped, with overall failure; both build jobs succeeded. Historical memory/timeout failures and the recorded hold_all_merges / new_failure_review_required remain in force. No merge, and these focused results do not clear those holds. Already-reviewed #410, #414 and #415 were also considered; they remain held and each has a skipped VM check. No open merelang/mere PRs.

Outstanding hosted checks at posting: Additional upstream gem tests / macos-26 (in_progress); Upstream gem tests / macos-26 (in_progress); Upstream Rake tests / macos-26 (in_progress); Small upstream gem tests / macos-26 (in_progress); build-macos (in_progress); build-linux (in_progress). Running checks and downstream coverage not yet materialized are not covered by the unassigned-runner exception.

COMMENT because the authenticated account owns this PR. No other person's review threads were resolved.

@284km
284km merged commit b5c4249 into main Dec 1, 2026
28 of 38 checks passed
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.

1 participant