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

@@ -16530,6 +16530,23 @@
1653016530 "0"
1653116531 end
1653216532
16533+ # arg0 coerced to `const char *`, snapshotting a mutable_str builder
16534+ # via mutable_str_to_cstr. A StrArray stores `const char *`; pushing a
16535+ # raw `sp_String *` (the mutable builder) both type-confuses the slot
16536+ # and aliases the object-owned ->data buffer, so once the unreferenced
16537+ # builder is collected the array element dangles and the GC mark walk
16538+ # faults scanning it (the #1071 / #1314 use-after-free class).
16539+ def compile_arg0_as_cstr(nid)
16540+ args_id = @nd_arguments[nid]
16541+ if args_id >= 0
16542+ arg_ids = get_args(args_id)
16543+ if arg_ids.length > 0
16544+ return compile_expr_for_expected_type(arg_ids[0], "string")
16545+ end
16546+ end
16547+ "0"
16548+ end
16549+
1653316550 # Returns 1 if `nid` is a CallNode whose receiver AND first argument
1653416551 # are both ArrayNode literals (`[...]` or `%i[...]` / `%w[...]`).
1653516552 # Used by the int_array-vs-sym_array equality guard so that
@@ -21057,7 +21074,7 @@
2105721074 if lt == "str_array"
2105821075 @needs_str_array = 1
2105921076 rc = compile_expr_gc_rooted(recv)
21060- return "(sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + "), " + rc + ")"
21077+ return "(sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + "), " + rc + ")"
2106121078 end
2106221079 if lt == "sym_array"
2106321080 @needs_int_array = 1
@@ -25831,7 +25848,7 @@
2583125848 # ...) compares like an array, not like int 0. Issue #619 puzzle 9.
2583225849 # The `<<` operator path further up already does this for its
2583325850 # str_array branch.
25834- return "(sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + "), " + rc + ")"
25851+ return "(sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + "), " + rc + ")"
2583525852 end
2583625853 if mname == "pop"
2583725854 return "sp_StrArray_pop(" + rc + ")"
@@ -35107,7 +35124,7 @@
3510735124 k = k + 1
3510835125 next
3510935126 end
35110- emit(" sp_StrArray_push(" + tmp + ", " + compile_expr(eid_sa) + ");")
35127+ emit(" sp_StrArray_push(" + tmp + ", " + compile_expr_for_expected_type(eid_sa, "string") + ");")
3511135128 k = k + 1
3511235129 end
3511335130 return tmp
@@ -39283,7 +39300,7 @@
3928339300 end
3928439301 if rt == "str_array"
3928539302 rc = compile_expr_gc_rooted(recv)
39286- emit(" sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + ");")
39303+ emit(" sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + ");")
3928739304 return 1
3928839305 end
3928939306 if rt == "float_array"
@@ -39523,7 +39540,7 @@
3952339540 end
3952439541 if rt == "str_array"
3952539542 rc = compile_expr_gc_rooted(recv)
39526- emit(" sp_StrArray_push(" + rc + ", " + compile_arg0(nid) + ");")
39543+ emit(" sp_StrArray_push(" + rc + ", " + compile_arg0_as_cstr(nid) + ");")
3952739544 return 1
3952839545 end
3952939546 if rt == "float_array"