Skip to content

fix: carry mruby's method_missing splat-keyword fix (550d10a) on the pinned VM - #81

Merged
AdamMusa merged 12 commits into
mainfrom
fix/mruby-prepare-missing-splat-keywords
Nov 20, 2026
Merged

AdamMusa merged 12 commits into
mainfrom
fix/mruby-prepare-missing-splat-keywords

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Ships with the next runtime release. This changes how the VM is built, not the prebuilt VMs in the repo. Apps see nothing until the Apple, Android, Linux and Windows VMs are rebuilt from this tree and released. This PR does not touch ruby_runtime/*/Frameworks, jniLibs, linux/lib, windows/bin, ruby_runtime/android/**, CMakeLists or vm-manifest.json.

The bug

In mruby 449627d (the ruby_runtime/third_party/mruby gitlink), prepare_missing() in src/vm.c runs

mrb_assert(ci->nk == 15);
argv[1] = argv[ci->n];

When a call splats positional arguments, ci->n == CALL_MAXARGS (15) and the keyword Hash is already in argv[1]. The line then reads argv[15], past the frame. So a call to an undefined method written as name(*args, **opts), name(*args, k: 1), name(...), or with 15+ literal arguments and keywords, reaches method_missing with the wrong keywords: none, an earlier call's, or whatever was on the stack.

In Ruflet this hits v0.0.23 names reached through Kernel#method_missing (e.g. def eb(*a, **k) = elevatedbutton(*a, **k)), and the scoped helpers from #67 (stepper, table, form, ...) whenever a wrapper forwards to them with a splat.

Upstream: mruby/mruby#6584, fixed by mruby/mruby#6598 (commit 550d10adb76a1bde1954ea78e1b8b5451c74cec3, merged as 72c9e5d on 2026-03-09):

if (ci->n != CALL_MAXARGS) {
  argv[1] = argv[ci->n];    /* keyword arguments */
}

Why a patch and not a pin bump

The smallest pin move that contains the fix is 72c9e5d, and it is not a one-hunk change. git diff --stat 449627d 72c9e5d is 18 files, 1995 insertions and 223 deletions. Besides docs, CI files and AUTHORS, it includes the mruby 4.0.0 version bump (include/mruby/version.h: MRUBY_RUBY_VERSION "3.4" to "4.0"), tasks/presym.rake, include/mruby/error.h and other src/vm.c changes. Any later pin would also bring 9aaa7ce5fb (2026-07-28, drops the visibility check on the method_missing fallback) and 7b59b50c87 (2026-11-08, send passes keywords to method_missing). Each of those is a VM change to verify on its own. The fix needs one hunk.

A fork commit was also an option. I did not take it: the fork would have to be fetchable by everyone who populates third_party/mruby (there is no .gitmodules), every build_config/*.rb.lock would change, and the fix would show in review as an opaque SHA. With a patch, the gitlink and the lock files stay as they are.

What changes

  • ruby_runtime/vm/patches/mruby/0001-Fixed-heap-buffer-overflow-in-method_missing.patch is upstream 550d10a (author dearblue), cherry-picked onto 449627d with -x. It applies to 449627d with no offset or fuzz.
  • ruby_runtime/vm/patches/mruby_patches.rb applies it with git apply. It skips a patch the tree already has, and it raises when a patch neither applies nor is already there. GIT_CEILING_DIRECTORIES stops git from patching an enclosing checkout instead. When git cannot be run, it raises git is required to apply ruby_runtime/vm/patches, but it could not be run. Install git and put it on PATH. It also runs as a CLI: ruby ruby_runtime/vm/patches/mruby_patches.rb [--check] [MRUBY_DIR] (exit 0 applied, 1 pending with --check, 2 on error).
  • Every ruby_runtime/vm/build_config/*.rb begins with require_relative "../patches/mruby_patches" and RufletMrubyPatches.apply(MRUBY_ROOT). minirake loads the config before it computes tasks, so the tree is patched before anything compiles. The Linux and Windows with_host_build wrappers load the config, and Android's build_host_vm runs minirake with host_vm.rb before CMake compiles third_party/mruby/src.
  • tools/build_vm_artifacts.sh: the documented container IMAGE lists git.
  • tools/mruby_patches_test.rb (8 tests): loads every build config against stand-ins for MRuby and RufletMrubyPatches and asserts that apply(MRUBY_ROOT) runs before the build is declared; checks that the patch's changed lines are exactly upstream's; and covers idempotence, a misfit tree, a missing git (in-process and through the CLI), the enclosing-checkout guard, and the pinned tree (when RUFLET_MRUBY or third_party/mruby is populated).
  • CI: build-ruflet-client.yml runs ruby tools/mruby_patches_test.rb in Verify Ruflet Explorer, and a pull request that changes only that test triggers it. The push triggers are unchanged.

Android note for the 16 KB-alignment work (D2, on hold, untouched): the owner's uncommitted tools/build_android_vm_artifacts.sh runs CMake without minirake and only checks that build/host_vm exists. A build/host_vm generated after this change comes from a patched tree; one left over from before does not. One line before its CMake loop closes that: ruby "$RUNTIME/vm/patches/mruby_patches.rb" "$MRUBY".

Behavior changes

None of these affect writing or running Ruflet apps, or ruflet CLI output. They affect only VM builds (tools/build_*_vm_artifacts.sh, or minirake/rake with a vm/build_config/*.rb).

  1. A VM build patches third_party/mruby in place. Any minirake or rake run that loads a vm/build_config/*.rb (even minirake -T) rewrites third_party/mruby/src/vm.c once with the 550d10a hunk. After that, git -C ruby_runtime/third_party/mruby status shows M src/vm.c, and the ruflet checkout shows m ruby_runtime/third_party/mruby. The gitlink and the *.rb.lock files do not change. git -C ruby_runtime/third_party/mruby checkout -- src/vm.c undoes it. Justification: this is how the bug fix reaches every build path without moving the pin. A VM built without it reads past the call frame.
  2. The first build on an unpatched tree prints one stderr line, ruflet: applied mruby patch 0001-Fixed-heap-buffer-overflow-in-method_missing.patch. Later builds print nothing. Justification: it makes the in-place edit in (1) visible.
  3. Building the VM needs git on PATH. On main, a build without git works, because mruby reaches git only through shell backticks. With this change, the build stops when the config loads, with the "git is required" message above (the CLI exits 2). The documented container IMAGE now lists git. Justification: git apply provides the applied / pending / misfit check. Stopping with a clear message is better than shipping a VM without the fix.
  4. Once the runtime is rebuilt, method_missing receives the call's own keywords for name(*a, **k), name(*a, k: 1), name(...), name(0, *a, **k), name(*a, 9, **k, z: 0) and 15+ literal arguments with keywords. Today it gets none, an earlier call's, or stack garbage (see the probe below). This is the bug fix. send(:name, *a, **k) still passes the keywords as a trailing Hash, as on main: that is the pre-7b59b50 behaviour, which this PR does not change.

Verification (head 19e6fcd4; main merges re-verified below)

VMs are scratch macOS arm64 builds made with tools/build_apple_vm_artifacts.sh macos --output <scratch>, run from exported trees against fresh clones of mruby 449627d. The harness was linked with RUFLET_VM_LIBRARY=<scratch>/libruflet_vm.a. Nothing was written to ruby_runtime/*/Frameworks or a working checkout's third_party/mruby. This Mac has no Rosetta, so the scratch script copies build only arm64 (the for arch loop and the lipo inputs). The main VM was built from dd924eb3 and this PR's from 76e95c53. git diff shows no change to the VM inputs (ruby_runtime, packages/ruflet_core/lib, packages/ruflet_server/lib, packages/ruflet_record/lib) from those to main a309af55 and 19e6fcd4. The harness was rebuilt and run from exports of a309af55 and 19e6fcd4.

main a309af55 this PR
src/vm.c after the build unpatched, no ruflet: line patched once, diff is exactly the patch hunk
scoped_helper_test all checks passed (11 skipped), 76 checks all checks passed, 0 skipped, 87 checks
method_missing probe (13 call shapes, CRuby 0 bad) 9 bad 1 bad (send with splat keywords, see 4 above)
compat_test aborts at line 67 (SystemExit undefined) byte-identical
regexp_test aborts at line 132 byte-identical
icon_deferral_test, ruflet_gem_test, ruflet_record_gem_test, in-process bridge pass byte-identical

For regexp_test, a scratch copy with its aborting lines replaced by nil runs to the end on both VMs with identical output (1 of 108 assertions failed, already on main).

Also run on the final head:

  • ruby tools/mruby_patches_test.rb: 8 runs, 0 failures, 0 skips with RUFLET_MRUBY set, on Ruby 4.0.5 and 3.4.5; 1 skip (pinned tree) without it. CI's new step in Verify Ruflet Explorer ran it on 19e6fcd4: 8 runs, 0 failures, 1 skip.
  • Planted mutations, each caught: the hook commented out (ios.rb), the hook made if false (host_vm.rb), the require_relative commented out, apply given a different root, a wrong fix that keeps the needle (argv[ci->n + 1]), the git check dropped, and the ceiling guard dropped.
  • minirake -T with each of the six configs (desktop_linux and desktop_windows through the with_host_build wrapper) on a pristine 449627d: each applies the patch once (src/vm.c modified, the fixed line present), a second run prints nothing, and a relative MRUBY_CONFIG works. The lock files are unchanged. --check exits 0 on the patched tree and 1 on a pristine one.
  • Patch status: :pending on 449627d, :applied on 72c9e5d, and an error (build stops) on upstream master fc55b04.
  • Without git (env -i HOME=$HOME PATH=<dir with only ruby> ruby ruby_runtime/vm/patches/mruby_patches.rb <pristine tree>): it prints the "git is required" message, exits 2 and leaves src/vm.c untouched. --check does the same.
  • H1 goldens: ruby test/compat/compat.rb --jobs 4 checked 234 goldens, exit 0. ruby -Ilib -Itest test/compat_goldens_test.rb gave 242 runs, 0 failures.
  • ruby tools/build_apple_vm_artifacts_test.rb: 3 runs, 0 failures.
  • The merge of main a309af55 (Test every public Page method; check client actions against their services #88) adds only packages/ruflet_core/test files. Its three new test files pass on this head, each run in its own process.
  • iOS, Android (CMake), Linux and Windows VMs were not built here. Their configs load the same hook, and the minirake -T runs above show it patching the tree for each of them.

Release checklist

  • Rebuild the macOS and iOS VMs with sh tools/build_apple_vm_artifacts.sh apple --install, and Android, Linux and Windows with their builders (git on PATH), from a tree with this change. Commit the artifacts and manifests in the runtime release.
  • Check that ruby ruby_runtime/vm/patches/mruby_patches.rb --check exits 0 after the build.
  • Release the runtime before any template that relies on splat-forwarded helper keywords.
  • When the mruby pin moves to 72c9e5d or later, delete the patch. If it is left behind, it is skipped when the new tree has that exact hunk, and the build stops when the surrounding code has changed.

Merges of main up to bc45ad66 (head 1c4cbc62)

The gate merged main six times:

The PR's diff against main is byte-identical to 19e6fcd4's diff against a309af55. Main changed these VM inputs: packages/ruflet_core/lib (#86, #71, #82) and packages/ruflet_record/lib (#107; the macOS, iOS and host VMs link ruflet-record). It also changed two harness tests: client_state_readers_test is new (#71) and icon_deferral_test was extended (#82).

These were run on b01d8fd6, from an export with a fresh mruby 449627d:

  • ruby tools/mruby_patches_test.rb: 8 runs, 0 failures on Ruby 4.0.5 and 3.4.5. It skips 1 test without RUFLET_MRUBY and none with it. tools/build_apple_vm_artifacts_test.rb: 3 runs, 0 failures.
  • H1 goldens: compat.rb --jobs 4 checked 291 goldens, exit 0. compat_goldens_test.rb gave 299 runs, 0 failures.
  • host_vm (minirake with host_vm.rb): exit 0. src/vm.c was patched once, with a diff equal to the patch, and the build printed one ruflet: applied line. packages/ruflet_record/test/mruby_smoke.rb on that mruby prints ruflet_record mruby sqlite smoke: ok.
  • A scratch arm64 macOS VM was then built in the same tree, using the method above. It exited 0, and the build-config locks are unchanged.
    • The harness outputs for scoped_helper, compat, regexp, ruflet_gem, ruflet_record_gem, in_process and the probe are byte-identical to the 19e6fcd4 run.
    • The extended icon_deferral_test passes ("all checks passed").
    • client_state_readers_test passes (346 assertions, exit 0).

On 1c4cbc62, the VM inputs are identical to b01d8fd6 (git diff b01d8fd6 1c4cbc62 is empty for ruby_runtime, packages/ruflet_core, packages/ruflet_server, packages/ruflet_record, tools and .github), so the VM results above carry over. These were rerun on 1c4cbc62: mruby_patches_test.rb gave 8 runs, 0 failures, 1 skip on Ruby 4.0.5 and 3.4.5. build_apple_vm_artifacts_test.rb gave 3 runs, 0 failures. compat.rb --jobs 4 checked 292 goldens, exit 0. compat_goldens_test.rb gave 300 runs, 0 failures.

mruby 449627d's prepare_missing() copies the keyword Hash from
argv[ci->n]. For a call that splats positional arguments ci->n is
CALL_MAXARGS and the Hash is already in argv[1], so it reads argv[15],
past the frame. Any `name(*args, **opts)`, `name(*args, k: 1)` or
`name(...)` call to an undefined method reaches method_missing with
garbage keywords: they are dropped, or a TypeError/NoMethodError about
stack contents is raised.

Upstream fixed this in 550d10a (mruby/mruby#6598, issue #6584). Moving
the pin to a tree with it would also bring 9aaa7ce5fb and 7b59b50c87,
which change VM behaviour, so the pin stays and the upstream commit is
carried as a patch instead. Every build config applies it as minirake
loads the config, which covers both artifact scripts and rake run by
hand; the Android CMake build reads the tree host_vm has patched.
AdamMusa added a commit that referenced this pull request Nov 17, 2026
kernel_helper_shadowing_test gains
test_ruflet_and_ruflet_ui_build_what_ruflet_dsl_builds. The 63
ScopedControlForwarders methods are now reached only through Ruflet.*
and Ruflet::UI.*, and before this only respond_to? and a few names
pinned them. The test compares Ruflet.<name> and Ruflet::UI.<name> with
Ruflet::DSL.<name> for every scoped name over four shapes: keywords only,
an empty positional, a control positional, and a block that adds a
child. It adds the keywords a helper needs to build (body: for
nested_scroll_view, title: for stepper_step). It also checks that each
built patch shows key:, the positional control and the block's child,
so a dropped keyword, argument or block changes what is compared.

scoped_helper_test.rb now describes the mruby 449627d limit correctly.
A splat-forwarded call to a missing method can build without its
keywords, build with an earlier call's keywords, or raise. It names the
fix, upstream 550d10adb7, which #81 carries as a VM build
patch.

The probe now runs all three forms the gated checks use: `*a, **k`,
`*a` plus a literal keyword, and `...`. It runs them after a call that
leaves other keywords behind, and compares positional arguments and
keywords exactly. A VM built with the patch runs every check.

A new gated check covers the stale case: carouselview forwarded right
after a call with key: "earlier" must build with its own key: "k".

No lib change.
git_apply now raises 'git is required to apply ruby_runtime/vm/patches'
when system cannot run git, instead of reporting the patch as misfitting.
build_vm_artifacts.sh lists git in the container image it documents.

The tests load every build config against stand-ins for MRuby and
RufletMrubyPatches, so a commented-out or unreachable hook fails, and they
pin the patch's changed lines to upstream 550d10a.
build-ruflet-client.yml runs tools/mruby_patches_test.rb next to the
framework tests, and a pull request that changes only that test runs it.
@AdamMusa
AdamMusa marked this pull request as ready for review November 20, 2026 05:44
@AdamMusa
AdamMusa merged commit 4ee4596 into main Nov 20, 2026
12 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