Commit 0408f5ac
0408f5ac26d4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Snapshot a mutable String pushed into a str_array, not its buffer alias
Pushing a mutable String builder (String.new + <<) into a str_array stored the raw sp_String pointer into a slot that holds const char *, aliasing the object-owned ->data buffer. The builder is unreferenced after the push, so a GC collects it and frees the buffer, leaving the array element dangling and the next mark walk scanning freed memory -- the #1071 use-after-free class. It also emitted an incompatible-pointer push the -Werror build rejects, and read back garbage lengths.
Route every str_array element store (<< and push in both statement and
expression form, and str_array literal elements) through the existing
mutable_str_to_cstr snapshot, which copies the builder's bytes into a
sweep-owned (0xfe) buffer that carries its own GC lifetime. Mirrors the
#1071 render-path fix (f7602f4).
Found while investigating #1314; that crash is a separate type-confusion in association preloading (model objects pushed into an int_array), so this does not close it.
Refs #1314
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 0408f5ac26d4
- Parent
- 61d1cdfec6f7
spinel_codegen.rbmodifiedView file
"0" end # arg0 coerced to `const char *`, snapshotting a mutable_str builder # via mutable_str_to_cstr. A StrArray stores `const char *`; pushing a # raw `sp_String *` (the mutable builder) both type-confuses the slot # and aliases the object-owned ->data buffer, so once the unreferenced # builder is collected the array element dangles and the GC mark walk # faults scanning it (the #1071 / #1314 use-after-free class). def compile_arg0_as_cstr(nid) args_id = @nd_arguments[nid] if args_id >= 0 arg_ids = get_args(args_id) if arg_ids.length > 0 return compile_expr_for_expected_type(arg_ids[0], "string") end end "0" end # Returns 1 if `nid` is a CallNode whose receiver AND first argument # are both ArrayNode literals (`[...]` or `%i[...]` / `%w[...]`). # Used by the int_array-vs-sym_array equality guard so that if lt == "str_array" @needs_str_array = 1 rc = compile_expr_gc_rooted(recv) return "(sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + "), " + rc + ")" return "(sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + "), " + rc + ")" end if lt == "sym_array" @needs_int_array = 1 # ...) compares like an array, not like int 0. Issue #619 puzzle 9. # The `<<` operator path further up already does this for its # str_array branch. return "(sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + "), " + rc + ")" return "(sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + "), " + rc + ")" end if mname == "pop" return "sp_StrArray_pop(" + rc + ")" k = k + 1 next end emit(" sp_StrArray_push(" + tmp + ", " + compile_expr(eid_sa) + ");") emit(" sp_StrArray_push(" + tmp + ", " + compile_expr_for_expected_type(eid_sa, "string") + ");") k = k + 1 end return tmp end if rt == "str_array" rc = compile_expr_gc_rooted(recv) emit(" sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + ");") emit(" sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + ");") return 1 end if rt == "float_array" end if rt == "str_array" rc = compile_expr_gc_rooted(recv) emit(" sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + ");") emit(" sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + ");") return 1 end if rt == "float_array"test/mutable_str_into_str_array.rbaddedView file
Loading diff…
test/mutable_str_into_str_array.rb.expectedaddedView file
Loading diff…