Skip to content

cgi/escape is ruby's C extension, linked in; a C-only feature is its extension in $LOADED_FEATURES - #286

Merged
284km merged 6 commits into
mainfrom
capi-cgi-escape
Nov 25, 2026
Merged

284km merged 6 commits into
mainfrom
capi-cgi-escape

Conversation

@284km

@284km 284km commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

This finishes what #253 left:

  • cgi/escape's methods were the Ruby fallback, so test_cgi_escape had 2 omits.
  • A library that is a C extension alone in ruby's build appeared twice in $LOADED_FEATURES.

What changes

  • cgi/escape.so is ruby's C extension.
    • capi/vendor/cgi_escape.c is ruby 4.0.10's ext/cgi/escape/escape.c, unchanged.
    • It is compiled in its own unit, capi/static_cgi_escape.c, because erb's escape.c has static functions of the same names.
    • It is registered as cgi/escape in mr_static_exts and shipped as the feature cgi/escape.so.
    • lib/cgi_escape_ext.rb is now 4.0.10's lib/cgi/escape.rb byte for byte, below its header. Its own require 'cgi/escape.so' loads the C. A build with no C side rescues the LoadError, as ruby's miniruby does.
  • CLASS_OF of a class is its metaclass, as in CRuby.
    • Before, it was Class, so escape.c's rb_cvar_get(CLASS_OF(CGI), :@@accept_charset) raised NameError.
    • Modules and singleton classes are unchanged.
  • A C-only feature appears once in $LOADED_FEATURES.
    • Examples: etc, zlib, stringio, io/wait, erb/escape, ...; the list is in cext_c_only, each checked with resolve_feature_path on ruby 4.0.10.
    • Its identity is <rubyarchdir>/<name>.bundle, and that is its only entry, also when it is required as name.so.
    • Its methods are C: source_location is nil, and Refinement#import_methods refuses them, as ruby does.
    • cgi/escape appears twice, as on ruby: the .rb, plus the extension that the .rb requires.
  • corpus 918 and 919; a capi/check contract for CLASS_OF of a class.

Two regressions in the first version, both fixed:

  • Corpus 403. require 'bigdecimal.so' inside bigdecimal.rb was treated as already loaded. Only C-only features and cgi/escape.so are now their extension by .so.
  • Corpus 579. import_methods imported Zlib's methods. cext_lib_file now recognises the extension path.

Checked

All on 81c42f5 (this branch with main ecd6507, including #284), mere 9dec2ff6, ruby 4.0.10.

  • run_corpus.sh --both: the gates, then 948/948 with the hash index on and off.
  • MERE_RUBY_VM=1 run_corpus.sh --both: 948/948.
  • unittest/run.sh on cgi/test_cgi_escape.rb, erb, ruby/test_encoding.rb and ruby/test_m17n.rb, on the evaluator and on the VM:
    • Both give 665 tests: MATCH 664, BOTH 1, RED 0. The BOTH is a test_m17n case that also fails on main.
    • test_cgi_escape alone: 435 tests, 526 assertions, 0 skips. Main's build gives 522 assertions and 2 skips.
  • capi_check: the contracts agree on both sides (api 376), hold under collection stress, and each negative control fails.
  • capi_spec_check: 2420 of 2602, the recorded count.
  • capi_mkmf_check: passes.
  • capi_gem_check: seventeen gems give the same output on both sides, and the other-ruby control fails.
  • capi/tools/headers_check.py (review of cgi/escape is ruby's C extension, linked in; a C-only feature is its extension in $LOADED_FEATURES #286: CI's build-linux stopped there):
    • cgi_escape.c was missing from VENDOR_PATHS; it is now added (fc68eb3).
    • Before the fix it failed locally with "capi/vendor/cgi_escape.c is not the tree's file".
    • After the fix: 194 headers, 10 of them patched, and 22 vendored sources unchanged.
  • Linux, since CI's Linux corpus had been skipped:
    • Built with this tree in docker ubuntu:24.04 (aarch64, clang, tools/build.sh --cc-only --fast).
    • Corpus 403, 579, 914, 918 and 919 match ruby's output on the evaluator and on the VM.
    • $LOADED_FEATURES lists .../aarch64-linux/cgi/escape.so and cgi/escape.rb, and stringio.so once.
  • What require adds, compared with ruby 4.0.10 and with main's build:
    • etc, zlib, stringio, io/wait, io/nonblock, date_core, pty, fcntl, json/ext/parser and rbconfig/sizeof now match ruby. Main also listed a .rb beside each.
    • erb/escape lists only its extension.
    • cgi/escape lists the extension and the .rb, in ruby's order.

Left

From the review (all on main too):

  • The internal name (sng:CGI) appears in error messages where ruby says #<Class:CGI>.
  • A metaclass has no FL_SINGLETON, and freezing a class does not freeze its metaclass.
  • io/nonblock, date_core, fcntl, digest/* and json/ext/* required with .so raise LoadError.
  • require "zlib" after require "zlib.so" returns true a second time.
  • require "date" does not list date_core.bundle.
  • A backtrace through a C-only library's Ruby starts at stringio.bundle:260.

From before:

These are not from this change and remain:

  • require "erb/escape" and "cgi/escape" also add set.rb.
  • digest's version.rb and loader.rb are not listed.
  • require "io/console" also lists stringio.
  • require "io/wait.so" raises LoadError where ruby returns false.
  • require "zlib.so" after require "zlib" returns true.
  • strscan is left out of the C-only list: ruby lists the strscan/strscan.rb that its extension requires.

Record: loop/2026-11-25-capi-cgi-escape.md.

🤖 Generated with Claude Code

…ed once

ext/cgi/escape/escape.c (4.0.10, unchanged) is compiled in its own unit
(capi/static_cgi_escape.c) and is the shipped cgi/escape.so, which ruby's
lib/cgi/escape.rb -- now 4.0.10's byte for byte -- requires. A feature that is
a C extension alone in ruby's build (etc, zlib, stringio, erb/escape, ...),
or a shipped one asked for with .so, is <rubyarchdir>/<name>.bundle and that
alone in $LOADED_FEATURES. CLASS_OF of a class is its metaclass, through
which escape.c reads @@accept_charset. corpus 918, 919.
…by .so

A shipped library of the bare name lists <name>.bundle when it starts, so
bigdecimal.rb's require 'bigdecimal.so' read as loaded and ran nothing
(corpus 403).
…ath, source_location is nil

Refinement#import_methods refused Zlib's methods by their .rb file (corpus
579); the file is the extension now.

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

Review of 534a430 (the code is 81c42f5 plus the loop record).

Summary

The C API change and the $LOADED_FEATURES change work as described on macOS, on both engines. They match ruby 4.0.10 in everything I probed that this PR claims. One Major finding blocks the merge: CI's build-linux job fails at the header check, so the Linux corpus never runs. The rest are Suggestions. Their causes were there before this PR.

Findings

Major: build-linux fails at capi/tools/headers_check.py.

  • The job says: capi/vendor/cgi_escape.c is not the tree's file.
  • I reproduced it on this head against a clean ruby 4.0.10 tree.
  • The file itself is fine: it is byte for byte ruby 4.0.10's ext/cgi/escape/escape.c (checked with diff).
  • The check compares each vendored file with the tree file of the same path, <tree>/cgi_escape.c. A renamed file needs an entry in VENDOR_PATHS, as erb_escape.c has. The fix is one line: 'cgi_escape.c': 'ext/cgi/escape/escape.c'.
  • Effect: the job stops at "Check Ruby's C API...". Every later step is skipped, including the Linux corpus on the evaluator and on the VM. So nothing on Linux has checked the .so side of the new $LOADED_FEATURES code. By reading, dlx9 comes from sys_os (), cext_lib_file and meth_source_location accept .so too, and I found no hard-coded .bundle.
  • The posted local results list capi_check, capi_spec_check, capi_mkmf_check and capi_gem_check. They do not list headers_check, which CI runs first in the same step.

Suggestion: the internal name (sng:CGI) can show in an error message.

  • With CGI::Escape.remove_class_variable(:@@accept_charset), CGI.unescape("%41") raises uninitialized class variable @@accept_charset in (sng:CGI). Ruby says in #<Class:CGI>.
  • This was already there: on main, Plain.singleton_class.class_variable_get(:@@nope) prints (sng:Plain) too. This PR only makes C reach it, through CLASS_OF. It is rare, but the message comes from class_variable_get on a singleton class, so it is easy to fix.

Suggestion: the metaclass is not yet a full CRuby metaclass to C.

  • FL_TEST(CLASS_OF(klass), FL_SINGLETON) is false; CRuby says true. capi_describe_x never sets bit 0x40, which mirror.c reads. So this was already true of every singleton class, including an object's. One effect: rb_class_new(CLASS_OF(klass)) does not raise "can't make subclass of singleton class".
  • OBJ_FROZEN(CLASS_OF(k)) is false after k.freeze; CRuby says true. From Ruby, k.singleton_class.frozen? is also false, on main too.
  • CLASS_OF of a module is still Module, and of a singleton class still Class. CRuby answers the module's or class's own singleton class. The PR says this; it is follow-up work.

Suggestion: list more of require "<name>.so" under "Left".

  • The description says a C-only feature has one entry "also when it is required as name.so". That holds for etc, stringio, pty and io/console. On this head (and on main) require "X.so" still raises LoadError for io/nonblock, date_core, fcntl, continuation, digest/md5, digest/sha1, digest/rmd160, json/ext/parser, json/ext/generator, rbconfig/sizeof and erb/escape. Ruby returns true. Only io/wait.so is in "Left".
  • require "zlib.so" then require "zlib" returns true twice and lists the extension only on the second call. Ruby returns true, then false.
  • require "date" does not list date_core.bundle; ruby lists it. This is the same on main.

Suggestion (cosmetic): backtraces now point into the extension file.

  • StringIO.new("").read(-1) now gives a first frame of <rubyarchdir>/stringio.bundle:260:in 'StringIO#__sio_len'. Before it was stringio.rb:260. Ruby shows the caller's line (-e:1:in 'StringIO#read'). Neither is ruby's answer. A file with a line number inside a .bundle may confuse a reader.

Not from this PR, noticed while probing. Both are the same on main:

  • A C singleton method named ancestors on a module, which calls rb_funcall(k, rb_intern("ancestors"), 0), gets the defining module's ancestors, not k's. A C const_get calling rb_const_get did something similar. An instance method inspect that calls inspect on another object is fine.
  • rb_method_boundp(CLASS_OF(Plain), rb_intern("new"), 0) is false; ruby says true.

What I checked

All of this was done by running, on the author's build (the worktree at this head, clean, binary newer than every .mere and capi change). I compared with ruby 4.0.10 and with a main build that does not have this PR.

  1. CLASS_OF on a class. I wrote a small C extension and built it twice, as capi_check does: once against this PR's capi/include and once against ruby's headers. I probed 30 C API paths on 13 receivers: classes, a subclass, Class, Object, BasicObject, String, an anonymous class, a Struct class, modules, and a singleton class.
    • These match ruby for every class: CLASS_OF is rb_singleton_class and is the same as RBASIC_CLASS; rb_obj_class; rb_class_name, rb_class2name, rb_obj_classname and rb_class_real of it (still Class); its superclass and ancestors; rb_obj_is_kind_of and rb_obj_is_instance_of.
    • These also match: rb_funcall on the class and on its metaclass; rb_method_boundp and rb_respond_to for a singleton method; rb_cvar_get, rb_cvar_set and rb_cvar_defined through the metaclass (own, inherited, and from an included module); rb_const_get and rb_ivar_get on the metaclass.
    • These now match too: rb_define_singleton_method; rb_extend_object; rb_define_method(CLASS_OF(klass), ...); rb_include_module(CLASS_OF(klass), ...).
    • Main was wrong on many of these. For example, rb_define_method(CLASS_OF(Plain), ...) defined the method on Class for every class. rb_include_module(CLASS_OF(Plain2), M) made Object.m work. rb_cvar_set put the variable on Class. So I found no caller that depended on the old answer.
    • The metaclass is still lazy as seen from Ruby. After C has taken CLASS_OF(Fresh), Fresh.singleton_methods is [], and Fresh.singleton_class? is false. Fresh.singleton_class is the same object C got. Class.subclasses and Object.subclasses contain no singleton class.
  2. $LOADED_FEATURES. Each case ran in its own process, with paths normalized. I tried cgi/escape, erb/escape, erb, cgi, erb then cgi, cgi/escape then erb, cgi/escape twice and then cgi/escape.so, cgi/escape.so then cgi/escape, erb/escape with and without .so, etc/etc.so, stringio.so/stringio, zlib, io/console, date, strscan, json, and the grep(/escape/) view.
    • erb and cgi need ruby's lib on -I, as the unittest runner gives it.
    • cgi/escape and erb/escape now give the same entries in the same order as ruby. The differences left are the ones in "Left" (set.rb, strscan, io/console's stringio) and the ones above.
    • require_relative of a local cgi/escape.rb, a require_relative'd file that requires cgi/escape, and -I dir with a local cgi/escape.rb that shadows the library all match ruby.
  3. CGI. I tried @@accept_charset set to EUC-JP, Shift_JIS, ISO-8859-1, US-ASCII, ASCII-8BIT and UTF-16LE, to a String, a Symbol and nil, and removed. Receivers were CGI, an instance of a class that includes CGI::Escape, and an object extended with it. I also tried explicit and bad encoding arguments, invalid bytes in all six methods, numeric references (out of range, NUL, surrogate, huge), EUC-JP/Shift_JIS/Latin-1 input, UTF-16 input, frozen receivers (identity and frozen state of the result), a String subclass (changed and unchanged), non-String arguments, and the h and escape_html aliases.
    • The evaluator and the VM print the same. Both match ruby except for the (sng:CGI) message above and a surrogate's inspect. The surrogate's bytes are the same; String#inspect of invalid UTF-8 differs, which is not from this PR.
    • Main differed from ruby on 13 lines here: owner and source_location, String-subclass results, TypeError messages and others.
  4. capi_check.py, run from a scratch copy of this head against a clean ruby 4.0.10 tree: the contracts agree (api 376, loader 48/49), they hold under GC stress, and each of the 6 negative controls fails. headers_check.py fails as in CI (above). I did not run capi_spec_check, capi_mkmf_check or capi_gem_check.
  5. Small runs. test/cgi/test_cgi_escape.rb gives 435 tests, 526 assertions, 0 skips on ruby, the evaluator and the VM (main: 522 assertions, 2 skips). Corpus 918, 919, 403 and 579 match ruby on both engines. I did not run the full corpus: a long test run was in progress on this machine.
  6. git merge-tree --write-tree origin/main origin/pr286 is clean.

Local results posted by the author

These are present, on 81c42f5 with main ecd6507: run_corpus.sh --both (gates, then 948/948 with the hash index on and off), MERE_RUBY_VM=1 948/948, the four capi checks, and cgi/erb/encoding/m17n on both engines (665 tests, MATCH 664, RED 0). headers_check is not among them.

CI

At the time of review, build-linux has failed (see Major). The unittest harness/plan, the Linux gem suites, runtime-load-path (Linux), linux-build-deps and NKF Wasm pass. build-macos, unittest-reference and the macOS suites are still pending.

Comment thread capi/static_cgi_escape.c
@@ -0,0 +1,13 @@
/* cgi/escape: ruby's ext/cgi/escape/escape.c (CGI::EscapeExt: escapeHTML,
* unescapeHTML, escape, unescape, escapeURIComponent, unescapeURIComponent),
* CRuby 4.0.10's, unchanged, in capi/vendor/cgi_escape.c, linked into this

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: this file is unchanged, but CI's capi/tools/headers_check.py cannot tell. It compares each vendored file with the tree's file of the same path, and <tree>/cgi_escape.c does not exist. So build-linux fails with capi/vendor/cgi_escape.c is not the tree's file, and the Linux corpus steps are skipped. Add 'cgi_escape.c': 'ext/cgi/escape/escape.c' to VENDOR_PATHS in capi/tools/headers_check.py, next to erb_escape.c.

…pe.c (review of #286)

The vendored file is compared with its path in the tree, as erb_escape.c is;
CI's build-linux stopped here. Left: the review's four suggestions.

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

Re-reviewed fc68eb3. The Major (build-linux failing in headers_check on capi/vendor/cgi_escape.c) is resolved: VENDOR_PATHS now maps cgi_escape.c to ext/cgi/escape/escape.c, like erb_escape.c. I ran this head's capi/tools/headers_check.py against a clean ruby 4.0.10 tree: 194 headers, 10 of them patched (headers.patch); 22 vendored sources unchanged.

Because CI's Linux corpus had been skipped, the author also built on ubuntu:24.04 (aarch64) and checked corpus 403, 579, 914, 918 and 919 on the evaluator and the VM against ruby: all identical, and $LOADED_FEATURES shows aarch64-linux/cgi/escape.so plus cgi/escape.rb, and a single stringio.so line. The four Suggestions are now listed under Left in the description and the loop note.

No other code changed since the last review (only the checker line and the record). The earlier results stand: corpus 948/948 on both engines, the 4 capi checks, cgi/erb/encoding/m17n 665 tests with RED 0, test_cgi_escape matching ruby with no omits. git merge-tree against current main: clean. No mere-pr-review-watch inline comments.

Merging.

@284km
284km merged commit a2322f5 into main Nov 25, 2026
4 of 17 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