Skip to content

mruby-objectspace-weakmap: hide WeakKeyMap's values from ObjectSpace - #7841

Merged
matz merged 1 commit into
mruby:masterfrom
saddamr3e:weakkeymap-hide-values
Nov 18, 2026
Merged

matz merged 1 commit into
mruby:masterfrom
saddamr3e:weakkeymap-hide-values

Conversation

@saddamr3e

@saddamr3e saddamr3e commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

ASan (clang, full-core gembox), GC.start after shrinking the values Array of a WeakKeyMap:

ERROR: AddressSanitizer: heap-buffer-overflow WRITE of size 8
    #1 wm_drop          mrbgems/mruby-objectspace-weakmap/src/weakmap.c:72
    #2 wm_clear_dead    mrbgems/mruby-objectspace-weakmap/src/weakmap.c:87
    #3 gc_clear_weaks   src/gc.c:503
    #4 incremental_gc   src/gc.c:1751

WeakKeyMap#delete on such a map reads past the end first (heap-buffer-overflow READ in wm_value_at, weakmap.c:224).

WeakKeyMap keeps its values in an Array under an instance variable Ruby cannot name, and wm_value_at(), wm_drop() and wm_reserve() index it by entry position without checking its length. ObjectSpace.each_object(Array) still yields that Array, so Ruby can find it and shrink it:

m = ObjectSpace::WeakKeyMap.new
keep = (1..20).map { |i| "keep#{i}" }
keep.each { |k| m[k] = :v }
tmp = "gone".dup
m[tmp] = :v
vals = nil
ObjectSpace.each_object(Array) { |a| vals = a if a.size == 32 && a[0] == :v && a[20] == :v }
vals.clear
4.times { vals << nil }
tmp = nil
GC.start

wm_vals_new() now clears the Array's class pointer, as the core does for its own internal arrays and strings, so ObjectSpace.each_object skips it. Nothing in the gem reads the Array's class.

Validation: the new test in test/weakmap.rb (mruby-objectspace added as a test dependency) aborts mrbtest on master and passes with the fix. Full clang ASan mrbtest with -s: 3560 total, KO 0, Crash 0.

Summary by CodeRabbit

  • Bug Fixes
    • WeakKeyMap’s internal value storage is no longer exposed as a regular array during ObjectSpace array enumeration. Values remain available through map lookups, including after keys are removed, preserving expected WeakKeyMap behavior.

WeakKeyMap indexes the Array of its values by entry position without
checking its length, in #[], #delete, #clear and the GC's
wm_clear_dead(). ObjectSpace.each_object(Array) could find that Array,
and shrinking it had those reads and writes go past its end.
@saddamr3e
saddamr3e requested a review from matz as a code owner November 18, 2026 05:07
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The WeakKeyMap values Array is hidden from Ruby’s ObjectSpace enumeration by clearing its capacity field. A regression test checks Array enumeration, map lookups, deletion, and garbage collection.

Changes: WeakKeyMap backing array visibility

Layer / File(s) Summary
Hide and test the values Array
mrbgems/mruby-objectspace-weakmap/src/weakmap.c, mrbgems/mruby-objectspace-weakmap/test/weakmap.rb, mrbgems/mruby-objectspace-weakmap/mrbgem.rake
wm_vals_new clears the values Array capacity. The regression test checks ObjectSpace enumeration, map lookups, deletion, and garbage collection. The gem declares mruby-objectspace as a test dependency.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Possibly related PRs

  • mruby/mruby#7837: Introduces the WeakKeyMap internal Array that this change hides from ObjectSpace enumeration.

Suggested labels: mrbgems

Suggested reviewers: amatsuda

Merge Risk: Minimal · up to d6e4a

This change hides the WeakKeyMap's internal values Array from ObjectSpace enumeration. That prevents Ruby code from shrinking the Array and causing out-of-bounds access in the map. A regression test is included. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the gem (mruby-objectspace-weakmap) and the main change (hiding WeakKeyMap's values from ObjectSpace). The changeset matches this: wm_vals_new clears the values Array's capacity/class …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the gem (mruby-objectspace-weakmap) and the main change (hiding WeakKeyMap's values from ObjectSpace). The changeset matches this: wm_vals_new clears the values Array's capacity/class so ObjectSpace.each_object(Array) skips it, and a regression test checks this. The title is specific and accurate, and it does not need to cover the test or the gem dependency change.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4f53d3f2-a675-478a-abe0-9008f6a94263
Commits

Reviewing files that changed from the base of the PR and between a347559 and d6e4af6.

Files selected for processing: 3

  • mrbgems/mruby-objectspace-weakmap/mrbgem.rake
  • mrbgems/mruby-objectspace-weakmap/src/weakmap.c
  • mrbgems/mruby-objectspace-weakmap/test/weakmap.rb

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@matz
matz merged commit dbab565 into mruby:master Nov 18, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants