Project

General

Profile

Actions

Bug #22500

closed

Keyword arguments can be freed during argument setup when a method has more than 31 keywords

Bug #22500: Keyword arguments can be freed during argument setup when a method has more than 31 keywords

Added by louim (Louis-Michel Couture) 16 days ago. Updated 3 days ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-10-26T11:21:04Z master 3e63685771) +PRISM [arm64-darwin27]
[ruby-core:127314]

Description

# 32 optional keywords with static defaults, then a 33rd whose default is not static.
kws = (0...32).map { |i| "k#{i}: nil" }.join(", ")
eval "def m(#{kws}, k32: Object.new) = k1"

GC.stress = true
10.times { m(k1: Object.new).inspect }
GC.stress = false
puts "ok"
[BUG] try to mark T_NONE object

It runs clean if k0 is passed instead of k1, or with 31 keywords instead of 33.

Without GC.stress, we saw this as wrong values in CI on 4.0.6, with no crash. A keyword local of Temporalio::Worker#initialize (34 keywords) held an unrelated String in place of the Array passed for it.

vm_callee_setup_arg and vm_call_iseq_setup_kwparm_kwarg call args_setup_kw_parameters with the keyword locals above cfp->sp. Once a passed value is moved into its local, that slot holds the only reference. When an optional keyword at index 31 or higher is left to a default that is not static, args_setup_kw_parameters_not_found allocates the tracking set with rb_ident_set_new(). A GC started by that allocation does not mark the locals and frees the values.

#22335 has the same root cause. Its fix removed the method call from this window but kept the allocation.

PR: https://github.com/ruby/ruby/pull/19679 extends cfp->sp over the keyword locals during setup, as setup_parameters_complex does, but only for methods with more than 31 keywords. Shorter methods keep the current fast path. It adds a regression test to test/ruby/test_keyword.rb.

I investigated this with the help of an AI assistant (Claude Code), since I don't know the Ruby VM internals well. That includes the analysis of the mechanism and the patch, so please review them with that in mind.

Reproduced on master, 3.3.12, 3.4.9, 4.0.6, 4.0.8 and 4.0.9.


Files

crash_log.txt (3.68 KB) crash_log.txt louim (Louis-Michel Couture), 11/03/2026 04:02 PM

Updated by louim (Louis-Michel Couture) 15 days ago Actions #1

  • Status changed from Open to Closed

Applied in changeset git|e143d2ff450622d6cd1036eada031eb45186161c.


[Bug #22500] Extend sp over keyword locals for methods with more than 31 keywords

The fast paths in vm_callee_setup_arg and vm_call_iseq_setup_kwparm_kwarg
call args_setup_kw_parameters with the keyword locals above cfp->sp. With
more than 31 keywords, leaving one past the 31st to a default that is not
static allocates a set to track it. Passed values have been moved into the
locals by then, so a GC started by that allocation frees them.

Extend cfp->sp over the locals during setup, as setup_parameters_complex
does, for methods with more than VM_KW_SPECIFIED_BITS_MAX keywords.

Co-Authored-By: Claude Opus 5.5

Updated by byroot (Jean Boussier) 15 days ago Actions #2

  • Backport changed from 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN to 3.3: WONTFIX, 3.4: REQUIRED, 4.0: REQUIRED

Updated by k0kubun (Takashi Kokubun) 3 days ago Actions #3 [ruby-core:127517]

  • Backport changed from 3.3: WONTFIX, 3.4: REQUIRED, 4.0: REQUIRED to 3.3: WONTFIX, 3.4: REQUIRED, 4.0: DONE
Actions

Also available in: PDF Atom