symbol.c: skip tombstones when migrating to the hash table - #7018
Merged
Conversation
Symbol GC sweeps a dynamic symbol by freeing its name buffer and storing `NULL` in `mrb->symtbl[i]` as a tombstone. `sym_check()`, `find_symbol_linear()` and the hash table rebuild at the end of `mrb_symbol_gc()` all check for that `NULL`, but `migrate_to_hash_table()` untags every entry unconditionally and dereferences the null pointer. The migration only runs while the table is still in linear mode, so this is reachable when `MRB_SYMBOL_MAX` is smaller than `MRB_SYMBOL_LINEAR_THRESHOLD`, a plausible choice for a memory constrained target. Symbol GC then leaves tombstones long before `mrb->symidx` reaches the migration threshold, and the first migration afterwards crashes in `mrb_packed_int_decode()`. `ht->symlink` and `ht->buckets` come from `mrb_calloc()`, so a skipped index keeps the zero that means "no chain", which is the same invariant the rebuild in `mrb_symbol_gc()` already relies on.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe symbol-table migration to hash mode now skips tombstone entries with ChangesSymbol table migration
Estimated code review effort: 2 (Simple) | ~5 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
migrate_to_hash_table()dereferences the tombstones symbol GC leaves behind.Symbol GC sweeps a dynamic symbol by freeing its name buffer and storing
NULLinmrb->symtbl[i]as a tombstone (phase 3 ofmrb_symbol_gc()). Every other reader ofsymtblchecks for thatNULL:sym_check()if (tagged_ptr == NULL) return FALSE;find_symbol_linear()if (mrb->symtbl[i] == NULL) continue;mrb_symbol_gc()if (mrb->symtbl[i] == NULL) continue;migrate_to_hash_table()does not. It walks the whole table to build the buckets anduntags each entry unconditionally, so the first migration from linear mode to hash mode
after a sweep reads through a null pointer.
Reachability
Migration fires at
mrb->symidx >= MRB_SYMBOL_LINEAR_THRESHOLD(256 by default), symbolGC at
mrb->dynamic_sym_count >= MRB_SYMBOL_MAX(4096 by default).symidxnevershrinks and
symidx >= dynamic_sym_countalways holds, so on a default build the tablehas been in hash mode for a long time before the first sweep and
migrate_to_hash_table()is never reached again.
The bug is therefore reachable only when
MRB_SYMBOL_MAX < MRB_SYMBOL_LINEAR_THRESHOLD,which is a configuration a memory constrained target would plausibly pick: a small symbol
budget is exactly the reason to set
MRB_SYMBOL_MAXat all. WithMRB_SYMBOL_MAX=16:$ ./build/host/bin/mruby -e '2000.times { |i| "filler-symbol-name-#{i}".to_sym }'The failure is deterministic, not allocation dependent. Building the same tree with a
larger budget makes it go away:
MRB_SYMBOL_MAXload of null pointer, then SEGV inmrb_packed_int_decodeFix
Skip tombstones the way the rebuild in
mrb_symbol_gc()already does.ht->symlinkandht->bucketscome frommrb_calloc(), so a skipped index keeps the zero that means"no chain", which is the same invariant that rebuild relies on.
No test is added: the crash needs a non default
MRB_SYMBOL_MAX, so it cannot beexpressed in
test/t/.Verification
Reproduced on master with a clang
address,undefinedbuild:With the patch applied the same build runs the snippet to completion, exit 0, with no
UBSan report.
rake teston a plain host build passes (1942 and 105 assertions, 0 KO,0 crash).
Summary by CodeRabbit