From: "konieczkow (Maciej Gajewski) via ruby-core" Date: 2026-11-12T23:17:29+00:00 Subject: [ruby-core:127456] [Ruby Bug#22543] Segmentation fault when compiling `CONST ||= value`, `CONST &&= value` or `CONST += value` under `# shareable_constant_value: literal` (Prism compiler) Issue #22543 has been reported by konieczkow (Maciej Gajewski). ---------------------------------------- Bug #22543: Segmentation fault when compiling `CONST ||= value`, `CONST &&= value` or `CONST += value` under `# shareable_constant_value: literal` (Prism compiler) https://bugs.ruby-lang.org/issues/22543 * Author: konieczkow (Maciej Gajewski) * Status: Open * ruby -v: ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [x86_64-linux] * Backport: 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- Ruby 4.0 and the current master branch crash with a segmentation fault while they compile this file: ```ruby # shareable_constant_value: literal C ||= foo ``` ``` $ ruby min.rb min.rb: [BUG] Segmentation fault at 0x0000000000f57b0d ruby 4.0.9 (2026-10-23 revision 3e93b23afc) +PRISM [x86_64-linux] ``` The crash happens at compile time. `ruby --dump=insns min.rb` crashes in the same way. ## Expected result A `NameError`, because `foo` is not defined. Ruby 3.4.12 gives this result, and so does Ruby 4.0.9 with `--parser=parse.y`: ``` min.rb:2:in '
': undefined local variable or method 'foo' for main (NameError) ``` ## Conditions All three conditions are necessary: 1. `# shareable_constant_value: literal` is active. 2. A plain constant is assigned with `||=`, `&&=`, or a binary operator assignment such as `+=`. 3. The value is not a pure literal, for example `foo`, `[foo]`, or `[x]` with a local variable `x`. These variants do not crash: - a pure literal value: `C ||= [1]` - a constant path: `M::C ||= [foo]` - the modes `experimental_copy` and `experimental_everything` - `--parser=parse.y` ## Versions | Ruby | Platform | Result | |---|---|---| | 4.1.0dev (master acc624e47b, 2026-11-12, Docker image `ghcr.io/ruby/ruby:master-noble`) | aarch64-linux | segmentation fault | | 4.0.9 (official Docker image `ruby:4.0.9-slim`) | x86_64-linux | segmentation fault | | 4.0.9 (official Docker image `ruby:4.0.9-slim`) | aarch64-linux | segmentation fault | | 4.0.9 (built from the release tarball on Ubuntu 24.04) | aarch64-linux | segmentation fault | | 4.0.5 (installed with mise) | arm64-darwin25 | segmentation fault | | 4.0.0 (official Docker image `ruby:4.0.0-slim`) | aarch64-linux | segmentation fault | | 3.4.12 (official Docker image `ruby:3.4-slim`) | aarch64-linux | NameError, no crash | ## C level backtrace (4.0.9, x86_64-linux) ``` libruby.so.4.0(RB_FL_TEST_RAW+0x0) compile.c:1297 libruby.so.4.0(pm_compile_shareable_constant_value) prism_compile.c:5784 libruby.so.4.0(pm_compile_constant_or_write_node+0x1ae) prism_compile.c:5886 libruby.so.4.0(pm_compile_node+0x55ff) prism_compile.c:10250 libruby.so.4.0(pm_compile_node+0x4d68) prism_compile.c:10355 libruby.so.4.0(pm_compile_scope_node+0x144a) prism_compile.c:7060 libruby.so.4.0(pm_compile_node+0x49a1) prism_compile.c:10226 libruby.so.4.0(pm_iseq_compile_node) prism_compile.c:10531 libruby.so.4.0(pm_iseq_new_with_opt_try+0x10) iseq.c:1105 ``` ## Cause Line numbers refer to `prism_compile.c` in 4.0.9. `pm_compile_constant_and_write_node`, `pm_compile_constant_or_write_node` and `pm_compile_constant_operator_write_node` pass the constant name as a Symbol in the `path` argument of `pm_compile_shareable_constant_value` (lines 5849, 5886 and 5913): ```c VALUE name = ID2SYM(pm_constant_id_lookup(scope_node, node->name)); ... pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true); ``` The `literal` branch of `pm_compile_shareable_constant_value` tests an object flag on `path` (lines 5784 to 5787): ```c if (!RB_OBJ_SHAREABLE_P(path)) { RB_OBJ_SET_SHAREABLE(path); } PUSH_INSN1(ret, location, putobject, path); ``` `RB_OBJ_SHAREABLE_P` expands to `FL_TEST_RAW`, which requires a heap object. A build of 4.0.9 with `RUBY_DEBUG=1` confirms this. For `min.rb` it stops at this assertion: ``` ./include/ruby/internal/fl_type.h:465: Assertion Failed: RB_FL_TEST_RAW:RB_FL_ABLE(obj) ``` The Symbol for a constant name is normally a static Symbol, and a static Symbol is an immediate value. Ruby stores an immediate value in the `VALUE` itself, so the `VALUE` is not a pointer to an object in memory. The flag test uses the `VALUE` as a pointer and reads the object flags at that address. That address is not valid memory, so the read causes the segmentation fault. A dynamic Symbol is a heap object, so the crash does not happen when a dynamic Symbol with the name of the constant already exists. For example, 4.0.9 raises `NameError` and does not crash when the program first calls `"Dyn#{0}Sym".to_sym` and then evaluates `Dyn0Sym ||= foo` under the pragma. The fault address matches this explanation. On aarch64-linux the fault address is `0x0000000000f57b0c`. Its low byte is `0x0c`, which is `RUBY_SYMBOL_FLAG`, the tag that marks a Symbol. The rest of the address comes from the ID of the Symbol, so it differs between runs and builds. On x86_64-linux the fault address is one byte higher, `0x0000000000f57b0d`. The reason is that `RUBY_FL_SHAREABLE` is `1<<8`, so the test reads the second byte of the flags. The variants that do not crash never run the flag test on a Symbol. `pm_compile_constant_write_node` compiles a plain assignment such as `C = foo`. It passes `rb_id2str(name_id)` as `path` (line 5817), and this value is a String. The constant path functions compile an assignment such as `M::C ||= [foo]`. They pass the result of `pm_constant_path_path` as `path`, and this value is a String too. The modes `experimental_copy` and `experimental_everything` do not use `path`. With a pure literal value such as `[1]`, `pm_compile_shareable_constant_value` returns before it reaches the flag test. Commit https://github.com/ruby/ruby/commit/bc00c4468e0054ca896d2b83d3020180915f64cf added the flag test. The title of the commit is "use `SET_SHAREABLE`". The commit belongs to pull request https://github.com/ruby/ruby/pull/14886, which was merged on 2025-10-23. Ruby 4.0.0 contains this commit. Ruby 3.4 passes the same Symbol as `path`, but it only uses `path` as the operand of the `putobject` instruction. Ruby 3.4 never reads flags from `path`, so it does not crash. ## Proposed fix The three functions can pass the constant name as a String. `rb_sym2str(name)` returns the String that belongs to the Symbol. `pm_compile_constant_write_node` already passes a String for a plain assignment. The diff below is against master (b721561cf4). It changes the three call sites and adds a regression test. The existing tests cover `expr::C ||= ...` but no compound assignment to a plain constant. ```diff diff --git a/prism_compile.c b/prism_compile.c index aa82a77..77498ee 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -5930,7 +5930,7 @@ pm_compile_constant_and_write_node(rb_iseq_t *iseq, const pm_constant_and_write_ if (!popped) PUSH_INSN(ret, location, pop); if (shareability != 0) { - pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true); + pm_compile_shareable_constant_value(iseq, node->value, shareability, rb_sym2str(name), ret, scope_node, true); } else { PM_COMPILE_NOT_POPPED(node->value); @@ -5967,7 +5967,7 @@ pm_compile_constant_or_write_node(rb_iseq_t *iseq, const pm_constant_or_write_no PUSH_LABEL(ret, set_label); if (shareability != 0) { - pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true); + pm_compile_shareable_constant_value(iseq, node->value, shareability, rb_sym2str(name), ret, scope_node, true); } else { PM_COMPILE_NOT_POPPED(node->value); @@ -5994,7 +5994,7 @@ pm_compile_constant_operator_write_node(rb_iseq_t *iseq, const pm_constant_opera pm_compile_constant_read(iseq, name, &node->name_loc, location.node_id, ret, scope_node); if (shareability != 0) { - pm_compile_shareable_constant_value(iseq, node->value, shareability, name, ret, scope_node, true); + pm_compile_shareable_constant_value(iseq, node->value, shareability, rb_sym2str(name), ret, scope_node, true); } else { PM_COMPILE_NOT_POPPED(node->value); diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index c985eac..304568c 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -1778,6 +1778,40 @@ def test_shareable_constant_value_nonliteral end; end + def test_shareable_constant_value_literal_op_assign + assert_raise_separately(Ractor::IsolationError, /unshareable object to OP_ASGN_C/, + "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + # shareable_constant_value: literal + OP_ASGN_C ||= ["Not " + "shareable"] + end; + + assert_raise_separately(Ractor::IsolationError, /unshareable object to OP_ASGN_C/, + "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + # shareable_constant_value: literal + OP_ASGN_C = true + OP_ASGN_C &&= ["Not " + "shareable"] + end; + + assert_raise_separately(Ractor::IsolationError, /unshareable object to OP_ASGN_C/, + "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + # shareable_constant_value: literal + OP_ASGN_C = [].freeze + OP_ASGN_C += ["Not " + "shareable"] + end; + + a = eval_separately("#{<<~"begin;"}\n#{<<~'end;'}") + begin; + # shareable_constant_value: literal + var = [:shareable].freeze + OP_ASGN_C ||= var + end; + assert_equal([:shareable], a) + assert_ractor_shareable(a) + end + def test_shareable_constant_value_unfrozen assert_raise_separately(Ractor::Error, /does not freeze object correctly/, "#{<<~"begin;"}\n#{<<~'end;'}") ``` I tested the fix on aarch64-linux with two builds. The first build is 4.0.9 from the release tarball, with the same change at lines 5849, 5886 and 5913. The second build is master (b721561cf4). | Check | 4.0.9 with the fix | master with the fix | |---|---|---| | `C \|\|= foo`, `C &&= foo`, `C += foo`, `C \|\|= [foo]`, `C \|\|= [x]` | no crash | no crash | | `ruby --dump=insns min.rb` | no crash | no crash | | 14 cases with shareable and unshareable values, output compared with `--parser=parse.y` | 14 identical | 14 identical | | `test_parse.rb`, `test_compile_prism.rb`, `test_iseq.rb`, `test_ractor.rb` | 368 tests, 0 failures | 452 tests, 0 failures, 1 skip | | New test with the fix | passes | passes | | New test with the fix and `--parser=parse.y` | passes | passes | | New test without the fix | segmentation fault | segmentation fault | With the fix, `C ||= value` and `C &&= value` raise `Ractor::IsolationError` with the message "cannot assign unshareable object to C" when `value` is not shareable. `C += value` raises the same error when the operand `value` is not shareable. The parse.y compiler gives the same result in these cases. Ruby 4.0 needs a backport of the fix. Ruby 3.4 does not crash and does not need it. ## Related differences that this fix does not change I found two differences between Prism and parse.y near this code. They exist with and without the fix, and neither one causes the crash. Each of them may deserve a separate ticket. The first difference concerns `C += value` when the operand is shareable and the result is not: ```ruby # shareable_constant_value: literal C = [1] C += [2] p C, Ractor.shareable?(C) ``` Prism prints `[1, 2]` and `false`. parse.y raises `Ractor::IsolationError`. The reason is that parse.y rewrites `C += value` to `C = C + value` and checks the result, while Prism checks only the operand. Ruby 3.4.12, Ruby 4.0.9 and master with the fix all behave this way. The second difference concerns the path String of a constant path target such as `M::C = ["a" + "b"]`. On 4.0.9, Prism stores a String in the instruction sequence that has the shareable flag but is not frozen. `pm_constant_path_path` returns the result of `rb_ary_join`, and `RB_OBJ_SET_SHAREABLE(path)` then sets the flag without freezing the String. parse.y stores a frozen String, because `node_const_decl_val` in `compile.c` ends with `rb_fstring(path)`. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/