Repository navigation
Bypass Ruby hooks when C extensions insert modules - #292
Conversation
…ule-mixins # Conflicts: # capi/core.c # capi/mr_capi.h # main.mere
284km
left a comment
There was a problem hiding this comment.
Reviewed a9af1d5. No Critical / Major / Minor findings.
rb_include_module,rb_prepend_moduleandrb_extend_objectnow insert the module through a new up-call (MR_MIXIN= 36) that changes the ancestry directly, as CRuby does, instead of calling Ruby'sinclude/prepend/extend. Soappend_features/included/prepend_features/prepended/extend_object/extendedhooks and Ruby overrides ofinclude/singleton_classon the target are not called. Withmr_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 andMERE_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-levelModule#prependon main gives the same wrong text, so this PR only inherits it.
- Suggestion (pre-existing, not from this PR): a cyclic
- The description has the local results our merge policy asks for: corpus
--both949/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'srb_errinfocontract gap is the known pre-existing one), and the C API spec replay identical to main. git merge-treeagainst current main: clean. No mere-pr-review-watch inline comments.
Merging.
284km
left a comment
There was a problem hiding this comment.
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 compiler9dec2ff687a08433247dbdb7729bc19c8522e538and pinned mgz661a01d8bb68f5acd68e3192bd47f081d179c436; 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.
| 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 |
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
C extensions calling
rb_include_module,rb_prepend_module, orrb_extend_objectcurrently 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
0b6e5f49integrates mainfd5f54b0;MR_MIXIN=36retains 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(revisiona704b7cd95a42cc0cf5f307137bf3aac0285b915, 84 default/bundled specs, no extras, Prism 1.8.2), ordinary O2 compiler9dec2ff6:run_corpus.sh --both: 949/949 in both hash modes; VM corpus: 949 VM / 0 fallback / 0 differences.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 baselinerb_errinforescue-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.a2322f53(runtime-identical to fd5). Existing io/thread wall limits and missing digest-extension coverage are retained; reference/Mere examples 2602/2420.a9e68981; installer and measured binary digests are recorded separately.Max family RSS (KiB), repetitions shown rather than selecting a favorable run:
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 isloop/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.