Skip to content

Bypass Ruby hooks when C extensions insert modules - #292

Merged
284km merged 4 commits into
mainfrom
codex-capi-native-module-mixins
Nov 25, 2026
Merged

284km merged 4 commits into
mainfrom
codex-capi-native-module-mixins

Conversation

@284km

@284km 284km commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

C extensions calling rb_include_module, rb_prepend_module, or rb_extend_object currently reach Ruby-level mixin methods and run overrides/hooks. Insert through the existing native machinery instead, preserving frozen checks and Enumerable bookkeeping. This is a prerequisite for the separate ActiveSupport deprecated-module proxy dispatch fix.

Runtime 0b6e5f49 integrates main fd5f54b0; MR_MIXIN=36 retains main's freeze/append opcodes 34/35. Corpus 1025 and twelve API checks cover all three entry points, target overrides, feature/notification hooks and frozen targets. There are no new global roots or retained frames.

Local validation uses clean ruby 4.0.10 via tools/ref_ruby.sh (revision a704b7cd95a42cc0cf5f307137bf3aac0285b915, 84 default/bundled specs, no extras, Prism 1.8.2), ordinary O2 compiler 9dec2ff6:

  • run_corpus.sh --both: 949/949 in both hash modes; VM corpus: 949 VM / 0 fallback / 0 differences.
  • All eight source gates, bracket depth 155/1024, default and explicit record hygiene pass. Vendored-header comparison: 194 headers, 10 patched, 22 sources unchanged.
  • capi_check: all six evaluator contracts pass, including 388 API checks, both normal and GC stress; all six negative controls fail as expected. The VM passes the other five contracts but the full API test retains the baseline rb_errinfo rescue-clear failure in normal/stress modes. Baseline was run with its own original fixture, not the new fixture. No full VM API all-pass claim. The isolated new checks plus loader-quiet control (13 checks) pass on reference/evaluator/VM and both Mere stress modes.
  • C API spec replay: 46 files / 92 per-side raw outputs byte-identical to comparison main a2322f53 (runtime-identical to fd5). Existing io/thread wall limits and missing digest-extension coverage are retained; reference/Mere examples 2602/2420.
  • Two alternating fixed-seed 12345 Rails replay pairs: deprecation + TimeWithZone remain 250 MATCH / 4 RED, with zero MISSING/UNCONFIRMED/BOTH/EXTRA and both files COMPLETE on both sides. The four deprecation REDs require the separate dispatch fix. Bounds: 600 s / 2 GiB family, jobs 2, MT_CPU=0. Explicitly reused the owned 85-package native install made by a9e68981; installer and measured binary digests are recorded separately.

Max family RSS (KiB), repetitions shown rather than selecting a favorable run:

File comparison main candidate
deprecation 311600 / 365376 346160 / 355904
TimeWithZone 456544 / 461376 456880 / 453024

The ranges overlap; no stable RSS improvement is claimed. Full execution metadata, UTC times, raw per-case results and spec-output digests are in capi/check/records/codex-capi-native-module-mixins.json; the loop record is loop/2026-11-25-codex-capi-native-module-mixins.md.

Separate documentation correction included in the same push: complete frozen-flags comparison evidence for merged #278, including its 45-file record and revised loop note. It does not change that merged implementation.

A third alternating RSS pair completed after submission, with the same measured binary digests. Its two files are COMPLETE on both sides and retain 250 MATCH / 4 RED, zero MISSING/UNCONFIRMED/BOTH/EXTRA. Third-sample RSS (main → candidate): deprecation 313824 → 312960 KiB; TimeWithZone 455104 → 455568 KiB. The committed portable record contains the first two pairs; these later samples are additional evidence and do not change the implementation or the recorded limitations.

@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 a9af1d5. No Critical / Major / Minor findings.

  • rb_include_module, rb_prepend_module and rb_extend_object now insert the module through a new up-call (MR_MIXIN = 36) that changes the ancestry directly, as CRuby does, instead of calling Ruby's include / prepend / extend. So append_features / included / prepend_features / prepended / extend_object / extended hooks and Ruby overrides of include / singleton_class on the target are not called. With mr_without("builtin") the old Ruby-level path stays.
  • Ran on the author's build at this head (clean tree, binary newer than every .mere / capi file), on the evaluator and MERE_RUBY_VM=1, against ruby 4.0.10: corpus 1025 (which builds a small C extension and covers the three entry points, hook bypass and frozen targets → FrozenError) is identical. I extended it with: a non-module argument (Integer, and a Class: TypeError: wrong argument type ... (expected Module)), a cyclic include, a cyclic prepend, including the same module twice (ancestors count 1), and prepend after include (order). All identical except one message:
    • Suggestion (pre-existing, not from this PR): a cyclic rb_prepend_module(m, m) says "cyclic include detected"; ruby says "cyclic prepend detected". Ruby-level Module#prepend on main gives the same wrong text, so this PR only inherits it.
  • The description has the local results our merge policy asks for: corpus --both 949/949 and VM 949 / 0 fallback / 0 differences, the 8 source gates, the bracket gate, record hygiene, headers_check, capi_check (all evaluator contracts with stress and negative controls; the VM's rb_errinfo contract gap is the known pre-existing one), and the C API spec replay identical to main.
  • git merge-tree against current main: clean. No mere-pr-review-watch inline comments.

Merging.

@284km
284km merged commit 0369440 into main Nov 25, 2026
10 of 19 checks passed

@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 a9af1d5db8d23ba35214f81c9d2162e233630313: one Major / P2 regression, described inline. The PR was merged externally as 03694405e7d3daa73225673f970fb42b3efe5a3e while this independent validation was running. The intervening review does not cover refinement arguments, so this is additional actionable feedback.

The dedicated native request correctly avoids the receiver overrides and Ruby callbacks covered by corpus 1025, and retains opcodes 34/35. However, it also bypasses the refinement-argument rejection; refinement methods can consequently escape into ordinary ancestry.

Validation performed here:

  • Fresh ordinary O2 builds of the exact head above and pre-merge current main ccb68952adab68d386f900b64281d76c1eed46e8, using compiler 9dec2ff687a08433247dbdb7729bc19c8522e538 and pinned mgz 661a01d8bb68f5acd68e3192bd47f081d179c436; reference Ruby 4.0.10.
  • An independent small C extension calls each of the three APIs. Current main rejects the refinement in evaluator and VM; reference Ruby rejects it with ArgumentError. Head instead exposes the refinement method in all three operations, in evaluator/VM, both normally and with GC stress.
  • Corpus 1025 outputs match reference on the fresh head's evaluator and VM in normal execution. Its evaluator GC-stress replay timed out at 60 seconds; full-corpus VM stress was not run. The separate refinement probes under both stressed engines completed. No broad API/corpus/upstream all-pass claim is made.
  • All eight source gates and bracket-depth check pass (155/1024). The earlier local GC-roots attempt timed out; its completed rerun passes.

No merge was performed by this review. The recorded global merge hold remains active, and the merged PR's core build/downstream and macOS checks include cancelled jobs; these are not passing coverage. The pre-existing full-VM rb_errinfo limitation disclosed in the PR remains separate from this newly reproduced regression.

Comment thread main.mere
let nb9 = (false, (Nil : str list), (Nil : Stmt list), env) in
let base9 = (if r.c == 2 then dispatch_prim0 env world nb9 target9 "singleton_class" Nil false
else scope_mod_val world target9) in
let module9 = mixin_module_name world (capi_in r.b) 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] Reject refinement arguments before native insertion

mixin_module_name accepts a refinement because it is present in modules, while append_features_raw / prepend_features_raw do not perform the refinement rejection previously provided by the Ruby-level include/prepend/extend paths. Consequently all three C APIs can install a refinement into ordinary ancestry and expose its methods outside using.

For example, create r = Module.new { refine(String) { def leaked; 123; end } }.refinements.first, then pass Class.new(String) and r to the fixture's include or prepend operation. The returned class's instances now answer leaked. Extending String.new with the same refinement also exposes it. Reference Ruby 4.0.10 rejects all three with ArgumentError: refinement module is not allowed; pre-merge current main ccb68952 rejects them too. The candidate exposes the method in both evaluator and VM.

Please reject a refinement argument in the native request before mutating ancestry, with the C API's ArgumentError, and cover all three operations in the fixture/corpus. This can use the existing refinement identity check without reintroducing Ruby callbacks.

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.

Repair submitted as #302 (runtime c15756b, head 442d14b); no push to this merged branch. Request 36 rejects refinement arguments with ArgumentError while preserving frozen-target priority and allowing ordinary modules into refinement targets. Extended corpus 1025 matches Ruby on evaluator/VM; full corpus 953/953 both modes, VM 953/0/0. All 30 insertion target checks pass on reference and both engines with GC stress. Evaluator full API 405 passes; VM retains the same unrelated errinfo-clear failure as fresh comparison main. Full spec 92 outputs are byte-identical before/after. Gates, headers, hygiene and three fixed-seed Rails RSS pairs are recorded in #302.

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