Repository navigation
Read C API frozen state without Ruby callbacks - #278
Conversation
284km
left a comment
There was a problem hiding this comment.
First review of 68bdd47. No Critical, Major or Minor finding. Two Suggestions below.
What the change does
capi_describe_xnow sets the frozen bit of a native proxy fromval_is_frozenfor every kind of value. Before, a Class or Module always got 0, and other kinds outside GC called Ruby'sfrozen?.rb_obj_frozen_pnow returnsRB_OBJ_FROZEN(obj). Before, it sentfrozen?to the object.- This matches CRuby. There,
rb_obj_frozen_pandRB_OBJ_FROZENread theFL_FREEZEbit (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.
- A probe C extension (built with each side's own headers) reports
[rb_obj_frozen_p(o), RB_OBJ_FROZEN(o)]next to Ruby'so.frozen?. The probe has 66 cases. I ran it on the reference, on this head (evaluator,MERE_RUBY_VM=1, andMERE_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 andRange.new, a Data instance, a Regexp literal. C says true on every side, as in CRuby. - Strings:
+"x", a frozen literal,-"um#{1}",dupof a frozen string,clone, andclone(freeze: false). All match.Object, Array and Hashdup/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_FROZENsaid false afterfreeze. On this head it says true, as in CRuby. - A class whose
frozen?lies (trueon an unfrozen object,falseon 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_freezefrom 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_frozenon 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.
- Immediates and always-frozen values: Integer, Bignum, Float (flonum and a heap Float such as
- 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.
tools/capi_check.pywith 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 withMERE_RUBY_VM=1in 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_frozenis the same function that Ruby's builtinfrozen?uses. So, without an override, the C answer and Ruby's answer now come from one source.- The
in_gcpath already usedval_is_frozen, so GC behaviour does not change. The change removes a path that could enter Ruby (a userfrozen?) during a mirror refresh. - Freshness:
refresh_headtakes 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 oldrb_obj_frozen_pbumped 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_FREEZEfromOBJ_FREEZEinbignew_1. So reading the flag is right for it too. git merge-tree --write-tree origin/main origin/pr278is clean against main 45b0932.
Suggestions
- (Suggestion)
capi_describe_xno longer uses itsenvandin_gcparameters. The comment about GC is still useful. The parameters could go, or a short note could say they are kept for the callers. - (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_FREEZEwithFL_SETdirectly instead ofrb_obj_freeze. This PR does not change that path.
|
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. |
|
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.
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. |
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:
Maximum family RSS, KiB:
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.