Commit 3190f6ea

3190f6ea44c0Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): cls/builtin Method adapters honor promote ABI

The Method ABI widens to (void *, sp_Bigint *...) -> sp_Bigint * under promote mode (per previous commit). Adapters that wrap a class method or a builtin-array method as a Method value must match that ABI -- the inline cast site is now sp_Bigint * so a mismatched mrb_int adapter crashes via UB pointer-to-int truncation.

emit_cls_method_adapter:

  • Switches signature to (void*, sp_Bigint*..., sp_Bigint*) in promote mode, with per-param sp_bigint_to_int unwrap when the callee param is int/symbol/bool.
  • Wraps the callee return via sp_bigint_new_int when callee's declared return isn't already bigint, returns void-callee as sp_bigint_new_int(0).

emit_builtin_array_method_adapter:

  • Same widening for IntArray [], []=, push adapters: returns sp_bigint_new_int(get) for [] and accepts sp_bigint_to_int(_i) / sp_bigint_to_int(_v) for the underlying mrb_int IntArray API.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
3190f6ea44c0
Parent
6677f043517c

spinel_codegen.rbmodifiedView file

@@ -2147,35 +2147,82 @@
21472147 end
21482148 cast = "(sp_" + pfx + " *)"
21492149 adapter_name = "sp_" + pfx + "_" + sanitize_name(mname) + "_builtin_method_adapter"
2150+ promote_abi_b = (@int_overflow_mode == "promote") ? 1 : 0
2151+ if promote_abi_b == 1
2152+ @needs_bigint = 1
2153+ slot_c_b = "sp_Bigint *"
2154+ unbox_i = "sp_bigint_to_int(_i)"
2155+ unbox_v = "sp_bigint_to_int(_v)"
2156+ ret_box_v = "_v"
2157+ else
2158+ slot_c_b = "mrb_int"
2159+ unbox_i = "_i"
2160+ unbox_v = "_v"
2161+ ret_box_v = "_v"
2162+ end
21502163 if recv_type == "int_array" && mname == "[]"
2151- @lambda_funcs << "static mrb_int "
2164+ @lambda_funcs << "static "
2165+ @lambda_funcs << slot_c_b
2166+ @lambda_funcs << " "
21522167 @lambda_funcs << adapter_name
2153- @lambda_funcs << "(void *_self, mrb_int _i) {\n"
2154- @lambda_funcs << " return sp_IntArray_get("
2155- @lambda_funcs << cast
2156- @lambda_funcs << "_self, _i);\n"
2168+ @lambda_funcs << "(void *_self, "
2169+ @lambda_funcs << slot_c_b
2170+ @lambda_funcs << " _i) {\n"
2171+ if promote_abi_b == 1
2172+ @lambda_funcs << " return sp_bigint_new_int(sp_IntArray_get("
2173+ @lambda_funcs << cast
2174+ @lambda_funcs << "_self, "
2175+ @lambda_funcs << unbox_i
2176+ @lambda_funcs << "));\n"
2177+ else
2178+ @lambda_funcs << " return sp_IntArray_get("
2179+ @lambda_funcs << cast
2180+ @lambda_funcs << "_self, "
2181+ @lambda_funcs << unbox_i
2182+ @lambda_funcs << ");\n"
2183+ end
21572184 @lambda_funcs << "}\n"
21582185 return
21592186 end
21602187 if recv_type == "int_array" && mname == "[]="
2161- @lambda_funcs << "static mrb_int "
2188+ @lambda_funcs << "static "
2189+ @lambda_funcs << slot_c_b
2190+ @lambda_funcs << " "
21622191 @lambda_funcs << adapter_name
2163- @lambda_funcs << "(void *_self, mrb_int _i, mrb_int _v) {\n"
2192+ @lambda_funcs << "(void *_self, "
2193+ @lambda_funcs << slot_c_b
2194+ @lambda_funcs << " _i, "
2195+ @lambda_funcs << slot_c_b
2196+ @lambda_funcs << " _v) {\n"
21642197 @lambda_funcs << " sp_IntArray_set("
21652198 @lambda_funcs << cast
2166- @lambda_funcs << "_self, _i, _v);\n"
2167- @lambda_funcs << " return _v;\n"
2199+ @lambda_funcs << "_self, "
2200+ @lambda_funcs << unbox_i
2201+ @lambda_funcs << ", "
2202+ @lambda_funcs << unbox_v
2203+ @lambda_funcs << ");\n"
2204+ @lambda_funcs << " return "
2205+ @lambda_funcs << ret_box_v
2206+ @lambda_funcs << ";\n"
21682207 @lambda_funcs << "}\n"
21692208 return
21702209 end
21712210 if recv_type == "int_array" && mname == "push"
2172- @lambda_funcs << "static mrb_int "
2211+ @lambda_funcs << "static "
2212+ @lambda_funcs << slot_c_b
2213+ @lambda_funcs << " "
21732214 @lambda_funcs << adapter_name
2174- @lambda_funcs << "(void *_self, mrb_int _v) {\n"
2215+ @lambda_funcs << "(void *_self, "
2216+ @lambda_funcs << slot_c_b
2217+ @lambda_funcs << " _v) {\n"
21752218 @lambda_funcs << " sp_IntArray_push("
21762219 @lambda_funcs << cast
2177- @lambda_funcs << "_self, _v);\n"
2178- @lambda_funcs << " return _v;\n"
2220+ @lambda_funcs << "_self, "
2221+ @lambda_funcs << unbox_v
2222+ @lambda_funcs << ");\n"
2223+ @lambda_funcs << " return "
2224+ @lambda_funcs << ret_box_v
2225+ @lambda_funcs << ";\n"
21792226 @lambda_funcs << "}\n"
21802227 return
21812228 end
@@ -2216,25 +2263,38 @@
22162263 pt_list = cls_cmeth_ptypes_get(ci, j)
22172264 base_name = "sp_" + cname + "_cls_" + sanitize_name(mname)
22182265 adapter_name = "sp_" + cname + "_" + sanitize_name(mname) + "_cls_method_adapter"
2219- # Method ABI is `(void *, mrb_int...)` regardless of the callee's
2220- # actual param types. `--int-overflow=promote` widens cls method
2221- # params (and possibly the return) to bigint, so the adapter
2222- # wraps each mrb_int arg via sp_bigint_new_int and unwraps a
2223- # bigint return via sp_bigint_to_int before handing back.
2224- adapter_rt = rt
2225- if base_type(rt) == "bigint"
2226- adapter_rt = "int"
2266+ # Method ABI in promote mode is `(void *, sp_Bigint *...) -> sp_Bigint*`.
2267+ # In raise/wrap mode it's `(void *, mrb_int...) -> mrb_int`.
2268+ # The adapter unwraps incoming sp_Bigint* args to whatever the
2269+ # callee param expects, and (re)wraps the callee return to match
2270+ # the ABI return type.
2271+ promote_abi = (@int_overflow_mode == "promote") ? 1 : 0
2272+ if promote_abi == 1
22272273 @needs_bigint = 1
2274+ adapter_rt_c = "sp_Bigint *"
2275+ slot_c = "sp_Bigint *"
2276+ else
2277+ adapter_rt_c = "mrb_int"
2278+ slot_c = "mrb_int"
22282279 end
2229- sig = c_type(adapter_rt) + " " + adapter_name + "(void *_unused"
2280+ sig = adapter_rt_c + " " + adapter_name + "(void *_unused"
22302281 body_args = ""
22312282 pi2 = 0
22322283 while pi2 < pt_list.length
2233- sig = sig + ", mrb_int _a" + pi2.to_s
2284+ sig = sig + ", " + slot_c + " _a" + pi2.to_s
22342285 arg_expr = "_a" + pi2.to_s
2235- if pi2 < pt_list.length && base_type(pt_list[pi2]) == "bigint"
2236- arg_expr = "sp_bigint_new_int(_a" + pi2.to_s + ")"
2237- @needs_bigint = 1
2286+ param_b = base_type(pt_list[pi2])
2287+ if promote_abi == 1
2288+ # Slot is sp_Bigint*. Unwrap to mrb_int when the callee
2289+ # param expects a non-bigint primitive.
2290+ if param_b == "int" || param_b == "symbol" || param_b == "bool"
2291+ arg_expr = "sp_bigint_to_int(_a" + pi2.to_s + ")"
2292+ end
2293+ else
2294+ if param_b == "bigint"
2295+ arg_expr = "sp_bigint_new_int(_a" + pi2.to_s + ")"
2296+ @needs_bigint = 1
2297+ end
22382298 end
22392299 if pi2 == 0
22402300 body_args = arg_expr
@@ -2253,19 +2313,39 @@
22532313 @lambda_funcs << "("
22542314 @lambda_funcs << body_args
22552315 @lambda_funcs << ");\n"
2256- @lambda_funcs << " return 0;\n"
2257- elsif base_type(rt) == "bigint"
2258- @lambda_funcs << " return sp_bigint_to_int("
2259- @lambda_funcs << base_name
2260- @lambda_funcs << "("
2261- @lambda_funcs << body_args
2262- @lambda_funcs << "));\n"
2316+ if promote_abi == 1
2317+ @lambda_funcs << " return sp_bigint_new_int(0);\n"
2318+ else
2319+ @lambda_funcs << " return 0;\n"
2320+ end
2321+ elsif promote_abi == 1
2322+ if base_type(rt) == "bigint"
2323+ @lambda_funcs << " return "
2324+ @lambda_funcs << base_name
2325+ @lambda_funcs << "("
2326+ @lambda_funcs << body_args
2327+ @lambda_funcs << ");\n"
2328+ else
2329+ @lambda_funcs << " return sp_bigint_new_int((mrb_int)"
2330+ @lambda_funcs << base_name
2331+ @lambda_funcs << "("
2332+ @lambda_funcs << body_args
2333+ @lambda_funcs << "));\n"
2334+ end
22632335 else
2264- @lambda_funcs << " return "
2265- @lambda_funcs << base_name
2266- @lambda_funcs << "("
2267- @lambda_funcs << body_args
2268- @lambda_funcs << ");\n"
2336+ if base_type(rt) == "bigint"
2337+ @lambda_funcs << " return sp_bigint_to_int("
2338+ @lambda_funcs << base_name
2339+ @lambda_funcs << "("
2340+ @lambda_funcs << body_args
2341+ @lambda_funcs << "));\n"
2342+ else
2343+ @lambda_funcs << " return "
2344+ @lambda_funcs << base_name
2345+ @lambda_funcs << "("
2346+ @lambda_funcs << body_args
2347+ @lambda_funcs << ");\n"
2348+ end
22692349 end
22702350 @lambda_funcs << "}\n"
22712351 nil