code ran or SEGV ✦ ‘make test-all’ stopped in the middle (SEGV) ✦ Aug, 2024 ✦ Demo code ran in success! ✦ ‘make test-all’ ran to the end w/ failures/errors
‘make check’ with failures/errors ✦ “I have to give up on merging Namespace for Ruby 3.4” ✦ Jan, 2025 ✦ ‘make check’ passed without failures/errors ✦ ‘make exam’ with failures/errors RubyKaigi 2024 and Then (2)
rb_classext_struct (rb_classext_t) ✦ struct ✦ many other class de fi nitions ✦ struct RClass_and_rb_classext_t ✦ RClass + classext Ruby’s Class de fi nition RClass + rb_classext_t RClass rb_classext_t RClass_and_rb_classext_t
fi nition ✦ classext per Namespace by shallow copying the values (at fi rst) Namespaces and classexts RClass ns_classext_tbl rb_classext_t for root/builtin namespace (prime) rb_classext_t for main namespace (non-prime) rb_classext_t for namespace X (non-prime) rb_classext for namespac Y (non-prime
fi nition ✦ classext per Namespace by shallow copying the values (at fi rst) Namespaces and classexts RClass ns_classext_tbl rb_classext_t for root/builtin namespace (prime) rb_classext_t for main namespace (non-prime) rb_classext_t for namespace X (non-prime) rb_classext for namespac Y (non-prime My Idea at that time 2
‘set’ Set Set autoload :Set, ‘set’ Set require ‘set’ in main ns trigger deep copying autoload_table in main ns in main ns in main ns in root ns in root ns in root ns in root ns
keys and values of const_tbl are not copied Shallow copying const_tbl RClass ns_classext_tbl rb_classext_t (prime) const_tbl [A -> va] rb_classext_t (non-prime) const_tbl [A -> va] table shallow copy My Idea at that time 4
also exists in the const_tbl in the prime classext → SEGV! ✦ ASAN showed the root cause Deleting/Re-setting Constant on non-prime classext RClass ns_classext_tbl rb_classext_t (prime) const_tbl [A -> va] rb_classext_t (non-prime) const_tbl [] https://rubykaigi.org/2024/presentations/KJTsanaktsidis.html
are copied deeply to eliminate side-e ff ects ✦ Ruby::VERSION has test cases to delete/add its values, and it identi fi ed the bug deep copying const_tbl RClass ns_classext_tbl rb_classext_t (prime) const_tbl [A -> va] rb_classext_t (non-prime) const_tbl [A -> va’] table deep copy
on macOS ✦ without crash reports, without backtrace ✦ On Linux, ‘make exam’ also crashed with SEGV ✦ with broken crash reports, with broken backtrace “Killed: 9”
✦ Wrote many debug prints (100 or more) ✦ required ‘cgi/escape’ — OK ✦ required ‘erb/escape’ — Crashed! ✦ Both are extensions (.so fi les) “Killed: 9” The Silver Bullet - printf 🤪
dlopen() opens a fi le just once ✦ Second call on a fi le is just ignored 🥺 Loading Extensions in Namespace (1) util.so Namespace A Namespace B dlopen(“util.so") util_abc dlopen(“util.so")
and Namespace ✦ Di ff erent namespaces load di ff erent .so fi les Loading Extensions in Namespace (2) util.so Namespace A Namespace B dlopen(“a_util.so”) util_abc dlopen(“b_util.so”) util_abc My Idea at that time 5
Namespace ✦ “cgi/escape” in a → “a_escape.so” ✦ “erb/escape” in a → “a_escape.so” 🥶 ✦ What happens? when: 1. Overwriting the dlopen-ed .so fi le with others 2. Then calling dlopen() the .so fi le ✦ macOS: “Killed: 9” ✦ Linux: Nothing happened — then SEGV randomly Loading Extensions in Namespace (3)
name and Namespace ✦ feature name (fname): “cgi/escape”, “erb/escape” Loading Extensions in Namespace: Use Full Path cgi/escape Namespace A dlopen(“a_cgi+escape.so”) cgi_escape erb/escape erb_escape dlopen(“a_erb+escape.so”)
cme: callable method entry ✦ method: require, de fi ned_class: Kernel, owner: Kernel ✦ Bug #20767, caused by “Implicit Re fi nements” for namespace implementation ✦ Reproduced by 1. re fi ne Kernel#require (then using it) 2. alias #require to #original_require 3. rede fi ning Kernel#require Bugs of MRI (2) Invalid cme owner with re fi nement
prism called itself again and again ✦ Using “—parser=parse.y” solved this problem ✦ But why? ✦ I found a comment by ko1 in bootstraptest/test_ractor.rb ✦ “prism parser with -O0 build consumes a lot of machine stack” ✦ Compiler option “-O3” solved this problem… but why? Bugs? of MRI (3) Don’t use compiler option “-O0”
cache table) ✦ Rearchitecting subclasses data structure ✦ Identifying Namespaces to load .so/.rb fi les ✦ “Implicit Re fi nements” ✦ https://speakerdeck.com/tagomoris/namespace-now-and-then Other Major Things