Skip to content

Read C API frozen state without Ruby callbacks - #278

Merged
284km merged 4 commits into
mainfrom
codex-capi-proxy-frozen-flags
Nov 25, 2026
Merged

284km merged 4 commits into
mainfrom
codex-capi-proxy-frozen-flags

Conversation

@284km

@284km 284km commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Frozen Class/Module proxies crossed the C API boundary with their frozen bit clear. Other proxy refreshes could call an overridden Ruby frozen? and store its answer in the native flag. Proxy refresh now reads intrinsic state, and rb_obj_frozen_p uses RB_OBJ_FROZEN as declared in the C API header. The old function used MR_SEND_KERNEL; the overridable callback was in proxy refresh.

Merged by the owner as main 779c7cc. The post-merge audit uses fresh ordinary O2 runtime 282be638, whose complete tracked tree is identical to that merge, with pinned comparison main 45b0932. Binary SHA-256: 886e74abeeb42aad5fd763d46ef414eda8a49f42f30ea2ad9cc52e422e7fd160. Compiler 9dec2ff687a08433247dbdb7729bc19c8522e538. Reference: clean tools/ref_ruby.sh, Ruby 4.0.10, revision a704b7cd95a42cc0cf5f307137bf3aac0285b915, 84 default/bundled specifications, zero extras, default Prism 1.8.2.

Completed post-merge checks:

  • Corpus --both: 939/939 ON/OFF, 1283.820s, maximum family RSS 1,216,592KiB. VM: 939 VM / 0 fallback / 0 differences, 234.762s, maximum family RSS 1,159,856KiB.
  • Eight prefix source gates, default hygiene, bracket 155/1024, and explicit corrected-record hygiene pass.
  • The formerly failing Linux source check is repaired by merged Check renamed vendored ERB source at its upstream path #273: 194 headers / 21 vendored sources verify. No ERB/native-gem implementation was changed here. Hosted CI is not claimed as all green.
  • Evaluator full capi_check: all six contracts, GC stress and all six negative controls pass their criteria. API counts are 356 on reference/Mere/stress.
  • VM contract audit correction: capi_check removes MERE_RUBY_* variables from child environments. The historical run labeled VM, and review step 3 using the parent environment, do not establish VM execution. Historical records are qualified rather than counted as VM proof.
  • The new audit uses an explicit --mere executable wrapper exporting MERE_RUBY_VM=1. Five full VM contracts pass normally and under GC stress; the full API script fails at its existing rb_protect/errinfo rescue-clear check, on both main45 and candidate, normally and under stress. Main fails at capi/check/api.rb:545, candidate at :559 (the same check shifted by 14 lines). This VM difference was reported to the owner; the VM implementation is unchanged. All six negative controls fail; no-builtin fails earlier at its native override check, independently of the later errinfo failure.
  • The 40 new frozen checks plus loader silence (41 checks) pass independently on reference, evaluator, evaluator under GC stress, VM and VM under GC stress. These focused successes do not replace the full-script VM nonpass.
  • C API spec replay: 46 files, all 92 per-side raw outputs byte-identical before/after; totals stay 2,602 reference / 2,420 Mere passing examples. Existing failures, io/thread wall limits and unavailable digest extension remain.
  • Three alternating fixed-seed 12345 Rails duration/erb_util pairs: 93 MATCH / 0 RED, all COMPLETE; other verdicts zero, existing order diagnostic 1. Bounds remain 600s / 2 GiB, jobs 2, MT_CPU=0.

Maximum family RSS, KiB:

File main45, three repeats candidate, three repeats
duration 311,216 /309,728 /321,040 310,064 /307,424 /315,216
erb_util 289,008 /289,888 /288,768 285,984 /283,760 /285,264

No stable memory improvement is claimed. These focused checks explicitly reuse the owned native-gem install made by fdda915, with installer and measured binary hashes separate. No full component run was started.

The original 6588c51 / base44aa511b cohort remains separately identified: corpus 894/894, VM corpus 894/ 0/ 0; six contract checks with 351 API checks (historical VM label not mode proof); spec 46 unchanged 2602/2420; original Object/Module failures and child-timeout variation retained. The final selected copied-ivar retries passed 1 test / 2 assertions on both builds and both engines, preserving 500,000 iterations and the 60-second child deadline. Those historical results are not relabeled as the latest integration.

The original record/loop are already in the repository. Corrected historical annotations and a separate main45 integration record are prepared locally for the next batched code update, respecting the request to avoid record-only pushes. No further push is made to this merged PR. Full mode-audit disclosure was also posted in comment 7155689240.

@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.

First review of 68bdd47. No Critical, Major or Minor finding. Two Suggestions below.

What the change does

  • capi_describe_x now sets the frozen bit of a native proxy from val_is_frozen for every kind of value. Before, a Class or Module always got 0, and other kinds outside GC called Ruby's frozen?.
  • rb_obj_frozen_p now returns RB_OBJ_FROZEN(obj). Before, it sent frozen? to the object.
  • This matches CRuby. There, rb_obj_frozen_p and RB_OBJ_FROZEN read the FL_FREEZE bit (or say true for a special constant). They never call a Ruby method.

Checked by running

I ran the binary built at this head. Its SHA-256 matches binary_sha256 in the record. I also ran the main build named as the baseline in the record (base 44aa511). The reference is Ruby 4.0.10.

  1. A probe C extension (built with each side's own headers) reports [rb_obj_frozen_p(o), RB_OBJ_FROZEN(o)] next to Ruby's o.frozen?. The probe has 66 cases. I ran it on the reference, on this head (evaluator, MERE_RUBY_VM=1, and MERE_RUBY_GC_STRESS=1), and on main.
    • Immediates and always-frozen values: Integer, Bignum, Float (flonum and a heap Float such as 1e300), static and dynamic Symbol, nil/true/false, Rational, Complex, Range literal and Range.new, a Data instance, a Regexp literal. C says true on every side, as in CRuby.
    • Strings: +"x", a frozen literal, -"um#{1}", dup of a frozen string, clone, and clone(freeze: false). All match. Object, Array and Hash dup/clone/clone(freeze: false) also match.
    • A Struct instance is not frozen by default in CRuby. C says false here too.
    • Class, Module, and the singleton class of a frozen object. On main, RB_OBJ_FROZEN said false after freeze. On this head it says true, as in CRuby.
    • A class whose frozen? lies (true on an unfrozen object, false on a frozen one). C ignores the override on this head, as in CRuby.
    • Method, UnboundMethod, Enumerator, Lazy, Thread, $stdout, Mutex, Time, an exception, Regexp.new, Proc, lambda, Binding, main, Set, ObjectSpace::WeakMap: all false, as in CRuby.
    • A stale mirror (a cached copy of the object header on the C side): C keeps a VALUE in a registered global, Ruby freezes the object, and a later C call reads it. Also: C reads, calls rb_funcall(o, "freeze"), and reads again. Both see the new state, for an Object, a String and a Class. On main the Class case stayed false.
    • rb_obj_freeze from C on an Object, String, Module and Array. Both C and Ruby then say frozen. On main the Module read was false.
    • A Bignum made in C (rb_str2inum, rb_uint2big) and a heap Float made in C (rb_float_new(1e300)): frozen.
    • rb_check_frozen on the lying class does not raise. On a frozen object it raises FrozenError.
    • The only difference from the reference on this head is Encoding::UTF_8. See Suggestion 2. Main has the same difference.
  2. Corpus 1020 prints the same as the reference on this head in both modes. On main it differs (Class and Module stay unfrozen, and the override is called). So the program does test the fix.
  3. tools/capi_check.py with this head's binary, from a clean copy of this head's sources and a clean Ruby 4.0.10 source tree. I ran it once as is and once with MERE_RUBY_VM=1 in the environment. Both passed: all six contract scripts agree with the reference, also under GC stress. The api contracts are 351 on the reference, on mere-ruby and under stress, so the 40 new checks are included. All six negative controls fail, as they must.

Checked by reading

  • val_is_frozen is the same function that Ruby's builtin frozen? uses. So, without an override, the C answer and Ruby's answer now come from one source.
  • The in_gc path already used val_is_frozen, so GC behaviour does not change. The change removes a path that could enter Ruby (a user frozen?) during a mirror refresh.
  • Freshness: refresh_head takes the frozen bit from the interpreter once per epoch. Every non-read-only call into the interpreter bumps the epoch, so a freeze done in Ruby is seen by the next C read. The old rb_obj_frozen_p bumped the epoch as a side effect (it made a call). The new one does not. I found no caller that relied on that side effect.
  • GC and memory: the change adds no table or cache. Nothing new needs sweeping.
  • A raw Bignum (one bignum.c made, not a proxy) gets FL_FREEZE from OBJ_FREEZE in bignew_1. So reading the flag is right for it too.
  • git merge-tree --write-tree origin/main origin/pr278 is clean against main 45b0932.

Suggestions

  1. (Suggestion) capi_describe_x no longer uses its env and in_gc parameters. The comment about GC is still useful. The parameters could go, or a short note could say they are kept for the callers.
  2. (Suggestion, not caused by this PR) Encoding::UTF_8.frozen? is false in mere-ruby. CRuby says true. C now agrees with mere-ruby's Ruby answer, so C and Ruby are consistent. The fix belongs to Ruby's side, in a separate PR.

Local results in the PR

The PR body and the record list: corpus --both 894/894; the VM corpus 894 / 0 fallback / 0 differences; the eight prefix gates, the bracket gate and record hygiene; capi_check on the evaluator and the VM with GC stress and the negative controls; the C API spec replay against main; the related Object/Module tests; and a focused Rails replay. They were measured on base 44aa511. Main has moved since then, but the merge is clean.

CI

build-linux failed in "Check Ruby's C API against the reference with shared C sources". The failure is headers_check: capi/vendor/erb_escape.c is not the tree's file. CI tested the merge with main 0cd831f. That main was before #273, which teaches headers_check.py the renamed erb_escape.c. This PR does not touch that file or that check. So the failure comes from main at that point, not from this PR. CI needs a new run against current main (a merge of main, or a new push). The Ubuntu gem suites, unittest-harness, unittest-plan, linux-build-deps, reactor and the Ubuntu runtime-load-path passed. build-macos, unittest-reference and the macOS suites were still pending when I looked.

Not checked

  • I did not run a corpus (a heavy job was running on the machine).
  • I did not run the C API spec replay or the Rails replay.
  • I did not check C code that sets FL_FREEZE with FL_SET directly instead of rb_obj_freeze. This PR does not change that path.

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

284km commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Review 6053160060 has no blocking findings; I have read it and both Suggestions. I am reintegrating main45 (including #273) in one validation batch. The formerly failing headers_check now passes: 194 headers / 21 vendored sources. The remote head stays 68bdd47 until the batch is complete.

One correction to the contract evidence: tools/capi_check.py strips all MERE_RUBY_* variables from its child environment. Invoking the tool with MERE_RUBY_VM=1, as in the historical vm-labeled run and review step 3, does not pin its children to the VM. I corrected the PR description and will qualify the historical record.

The new audit uses an explicit --mere executable wrapper that exports MERE_RUBY_VM=1. All 40 added frozen-state checks plus the loader-silence check pass on reference, evaluator, evaluator under GC stress, VM and VM under GC stress (41 checks each). The unchanged full API script fails later at the rb_protect/errinfo rescue-clear check: main45 fails at capi/check/api.rb:545 and the integrated candidate at :559 (the same line shifted by the new checks). Both report the stale ArgumentError after rescuing E2. This is a retained VM difference and belongs to the VM stream; it is not counted as passing contract coverage. The other five VM contracts pass, including GC stress. Full transcripts remain separate from focused success.

The integrated corpus, spec replay and repeated Rails RSS comparisons are finishing before the single push. The signatures in Suggestion 1 are retained for the existing GC-reader callers. Suggestion 2 is a separate Ruby-side conformance follow-up. Also, the old rb_obj_frozen_p used MR_SEND_KERNEL; the overridable Ruby callback was in proxy refresh, which the change removes.

@284km

284km commented Nov 25, 2026

Copy link
Copy Markdown
Owner Author

Post-merge audit is complete. Fresh O2 runtime 282be638 has exactly the same tracked tree as merged main 779c7cc. The description now separates this cohort from the original 6588c51 evidence and corrects the historical VM-mode claim.

  • Corpus --both 939/939 ON/OFF; VM 939 / 0 fallback / 0 differences. Eight source gates, default/explicit record hygiene, bracket 155/1024 and headers 194/21 pass.
  • Full evaluator capi_check passes all six contracts, GC stress and negative-control criteria, with 356 API checks.
  • Executable-pinned full VM replay retains the same rb_protect/errinfo failure on main45 and candidate, normally and under GC stress; the other five contracts pass. The 40 new frozen checks plus loader silence pass independently in all five reference/evaluator/VM/stress executions (41 checks each). The VM failure is not counted as passing coverage.
  • All 46 C API spec files complete the replay with 92 raw per-side outputs byte-identical before/after: 2602 reference / 2420 Mere passing examples. Original spec nonpasses and limits remain.
  • Three fixed-seed Rails duration/erb_util pairs retain 93 MATCH / 0 RED, all COMPLETE. Maximum RSS ranges are retained in the description; bounds and disclosed native-install reuse are unchanged.

Supplemental portable records and the corrected historical annotations are committed locally as 8959678e after explicit three-file hygiene passed. They will accompany a future code update, rather than a record-only push. No push has been made to the merged PR. The existing VM errinfo difference is reported here for the VM stream; this follow-up does not change VM code.

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