Commit d1eb0011
d1eb0011c397Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified
Description
Merge pull request #388 from OriPekelman/fix/poly-ivar-write-and-nil-return-dispatch
fix(codegen): two silent miscompiles on poly receivers
Details
- Commit
- d1eb0011c397
- Parent
- 75c55c6cee80
- Merge parent
- 356f32c6672a
spinel_codegen.rbmodifiedView file
@@ -28558,6 +28558,15 @@
2855828558
@needs_rb_value = 12855928559
return "poly"2856028560
end28561+
# Setters: mname ends with "=" and at least one class has an28562+
# attr_writer for the bare name. Return type is the ivar type28563+
# (Ruby returns the rhs from `x = v`); without this, the result28564+
# tmp's C type defaults to `mrb_int` and `tmp = rhs` mismatches28565+
# for non-int slots.28566+
setter_bname = ""28567+
if mname.length > 1 && mname[mname.length - 1] == "="28568+
setter_bname = mname[0, mname.length - 1]28569+
end2856128570
common = ""2856228571
ci = 02856328572
while ci < @cls_names.length@@ -28567,6 +28576,9 @@
2856728576
elsif cls_has_attr_reader(ci, mname) == 12856828577
# An attr_reader returns the ivar type. Issue #119.2856928578
rt = cls_ivar_type(ci, "@" + mname)28579+
elsif setter_bname != "" && cls_has_attr_writer(ci, setter_bname) == 128580+
# An attr_writer setter returns the ivar's type.28581+
rt = cls_ivar_type(ci, "@" + setter_bname)2857028582
end2857128583
if rt != ""2857228584
if common == ""@@ -28851,6 +28863,39 @@
2885128863
else2885228864
emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";")2885328865
end28866+
elsif mname.length > 1 && mname[mname.length - 1] == "=" && cls_has_attr_writer(i, mname[0, mname.length - 1]) == 128867+
# An auto-registered attr_writer setter (`obj.x = v`) on a28868+
# poly-typed receiver. Without this arm the cls_id dispatch28869+
# finds neither a real method nor an attr_reader, falls28870+
# through silently, and the assignment never executes —28871+
# `obj.x = v` becomes a no-op.28872+
bname = mname[0, mname.length - 1]28873+
raw_val = arg_compiled.length > 0 ? arg_compiled[0] : "0"28874+
raw_t = arg_types.length > 0 ? arg_types[0] : ""28875+
slot_t = cls_ivar_type(i, "@" + bname)28876+
# Match the rhs to the slot's concrete C type for *this arm*.28877+
# Three cases:28878+
# slot poly + arg concrete -> box the arg.28879+
# slot concrete + arg poly -> unbox the arg.28880+
# else -> direct assign.28881+
# Different arms in the dispatch may take different branches28882+
# (e.g. one slot is `int`, another is `string`).28883+
store_val = raw_val28884+
if slot_t == "poly" && raw_t != "poly" && raw_t != ""28885+
store_val = box_value_to_poly(raw_t, raw_val)28886+
elsif slot_t != "poly" && raw_t == "poly"28887+
store_val = unbox_poly_to(slot_t, raw_val)28888+
end28889+
ivar_lhs = "((sp_" + cname + " *)" + recv_tmp + ".v.p)->" + sanitize_ivar("@" + bname)28890+
# `x = v` returns v in Ruby. The result tmp's type is set by28891+
# poly_dispatch_return_type (poly when slot types diverge,28892+
# otherwise the common slot type). Box / coerce the stored28893+
# value into the tmp's expected shape.28894+
rhs_for_tmp = store_val28895+
if is_poly_ret == 128896+
rhs_for_tmp = box_val_to_poly(store_val, slot_t)28897+
end28898+
emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") { " + ivar_lhs + " = " + store_val + "; " + tmp + " = " + rhs_for_tmp + "; }")2885428899
end2885528900
i = i + 12885628901
end@@ -29254,7 +29299,13 @@
2925429299
return "sp_box_bool(" + val + ")"2925529300
end2925629301
if at == "nil"29257-
return "sp_box_nil()"29302+
# `val` may be a side-effecting call (e.g. an arm in a29303+
# cls_id-dispatched table where the user-defined override29304+
# returns nil because its body is a `puts` / similar). Drop29305+
# the value but keep the side effect via the comma operator;29306+
# otherwise the generated dispatch silently elides the call29307+
# for any subclass whose method's static return type is nil.29308+
return "((void)(" + val + "), sp_box_nil())"2925829309
end2925929310
if at == "symbol"2926029311
return "sp_box_sym(" + val + ")"test/poly_attr_writer.rbaddedView file
Loading diff…
test/poly_attr_writer.rb.expectedaddedView file
Loading diff…
test/poly_nil_return_dispatch.rbaddedView file
Loading diff…
test/poly_nil_return_dispatch.rb.expectedaddedView file
Loading diff…