Commit 62e964b6

62e964b6b327Merged

zhware <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix(codegen): root parts of >=5 string-concat chain to prevent UAF

Details

Commit
62e964b6b327
Parent
b4ef1d7089f0

spinel_codegen.rbmodifiedView file

@@ -6522,6 +6522,11 @@
65226522 rt = infer_type(@nd_receiver[nid])
65236523 if rt == "string"
65246524 @needs_string_helpers = 1
6525+ # Long string concat chains emit SP_GC_ROOT temps, so the
6526+ # enclosing function needs SP_GC_SAVE() in its header.
6527+ if mname == "+"
6528+ @needs_gc = 1
6529+ end
65256530 end
65266531 end
65276532 end
@@ -13057,18 +13062,33 @@
1305713062 return "sp_str_concat4(" + parts[0] + ", " + parts[1] + ", " + parts[2] + ", " + parts[3] + ")"
1305813063 end
1305913064 if parts.length >= 5
13060- # Variable-length: single malloc for N parts via sp_str_concat_arr
13065+ # Variable-length: single malloc for N parts via sp_str_concat_arr.
13066+ # Hoist each part into a rooted temp first — the compound-literal
13067+ # initializer order is unspecified, and any part that is a fresh
13068+ # GC string would otherwise sit unrooted on the C stack while
13069+ # later parts evaluate (and may trigger sp_gc_collect).
13070+ # @needs_gc is set in scan_features for any string `+`, ensuring
13071+ # SP_GC_SAVE() is in the function header before we emit roots here.
13072+ tnames = "".split(",")
13073+ k = 0
13074+ while k < parts.length
13075+ t = new_temp
13076+ emit(" const char * " + t + " = " + parts[k] + ";")
13077+ emit(" SP_GC_ROOT(" + t + ");")
13078+ tnames.push(t)
13079+ k = k + 1
13080+ end
1306113081 arr = "(const char *const[]){"
1306213082 k = 0
13063- while k < parts.length
13083+ while k < tnames.length
1306413084 if k > 0
1306513085 arr = arr + ", "
1306613086 end
13067- arr = arr + parts[k]
13087+ arr = arr + tnames[k]
1306813088 k = k + 1
1306913089 end
1307013090 arr = arr + "}"
13071- return "sp_str_concat_arr(" + arr + ", " + parts.length.to_s + ")"
13091+ return "sp_str_concat_arr(" + arr + ", " + tnames.length.to_s + ")"
1307213092 end
1307313093 return "sp_str_concat(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
1307413094 end