Commit f4783256

f4783256b339Merged

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

Description

Make the non-builtin integer-overflow fallback width-correct (32-bit)

The portable fallback used when a compiler lacks _builtin*_overflow hardcoded the 64-bit sign bit ((mrb_uint)1 << 63) and the int64 MRB_INT_MAX/MIN bounds, and depended on mruby_shim.h's mrb_uint (= uint64_t). With mrb_int now intptr_t that path was wrong (and the 1<<63 shift is UB) on a 32-bit build.

Rewrite it self-contained and width-correct: compute in uintptr_t, take the sign bit at 1 << (sizeof(mrb_int)*8 - 1) (63 on 64-bit, 31 on 32-bit), and bound the multiply with INTPTR_MAX/MIN. No mruby_shim dependency. The active path (gcc/clang) is unchanged -- it uses _builtin*_overflow, which is already width-correct.

Verified by extracting the three helpers into a standalone test: at -m32, 2e9+2e9 and 1e6*1e6 correctly report overflow (they exceed INT32); at -m64 they correctly don't (fit INT64); INTPTR_MIN-1 overflows at both.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
f4783256b339
Parent
9ba2e7c2363b

lib/sp_runtime.hmodifiedView file

@@ -130,34 +130,36 @@
130130 return __builtin_mul_overflow(a, b, r);
131131 }
132132 #else
133-/* Portable fallback. mrb_int is int64_t (see typedef above), so the
134- add / sub fallbacks compute in mrb_uint (= uint64_t) -- unsigned
135- integer overflow is well-defined wrap-around in C -- and detect
136- signed overflow via the sign-bit XOR trick. Mul checks bounds
137- before multiplying because the 128-bit intermediate isn't
138- portable. */
139-#define SP_INT_OVERFLOW_MASK ((mrb_uint)1 << 63)
133+/* Portable fallback for compilers lacking __builtin_*_overflow.
134+ mrb_int is pointer-width (intptr_t), so compute in uintptr_t --
135+ unsigned overflow is well-defined wrap-around in C -- and detect
136+ signed overflow via the sign-bit XOR trick at the *correct* width
137+ (the sign bit is mrb_int's top bit: 63 on 64-bit, 31 on 32-bit).
138+ Bounds use INTPTR_MAX/MIN, not the int64 MRB_INT_* macros, so this
139+ path is self-contained and width-correct. Mul checks bounds before
140+ multiplying because a 2x-width intermediate isn't portable. */
141+#define SP_INT_OVF_SIGN ((uintptr_t)1 << (sizeof(mrb_int) * 8 - 1))
140142 static inline mrb_bool sp_int_add_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
141- mrb_uint x = (mrb_uint)a, y = (mrb_uint)b, z = x + y;
143+ uintptr_t x = (uintptr_t)a, y = (uintptr_t)b, z = x + y;
142144 *r = (mrb_int)z;
143- return !!(((x ^ z) & (y ^ z)) & SP_INT_OVERFLOW_MASK);
145+ return !!(((x ^ z) & (y ^ z)) & SP_INT_OVF_SIGN);
144146 }
145147 static inline mrb_bool sp_int_sub_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
146- mrb_uint x = (mrb_uint)a, y = (mrb_uint)b, z = x - y;
148+ uintptr_t x = (uintptr_t)a, y = (uintptr_t)b, z = x - y;
147149 *r = (mrb_int)z;
148- return !!(((x ^ z) & (~y ^ z)) & SP_INT_OVERFLOW_MASK);
150+ return !!(((x ^ z) & (~y ^ z)) & SP_INT_OVF_SIGN);
149151 }
150152 static inline mrb_bool sp_int_mul_overflow_p(mrb_int a, mrb_int b, mrb_int *r) {
151- if (a > 0 && b > 0 && a > MRB_INT_MAX / b) { *r = a * b; return TRUE; }
152- if (a < 0 && b > 0 && a < MRB_INT_MIN / b) { *r = a * b; return TRUE; }
153- if (a > 0 && b < 0 && b < MRB_INT_MIN / a) { *r = a * b; return TRUE; }
154- if (a < 0 && b < 0 && (a <= MRB_INT_MIN || b <= MRB_INT_MIN || -a > MRB_INT_MAX / -b)) {
153+ if (a > 0 && b > 0 && a > INTPTR_MAX / b) { *r = a * b; return TRUE; }
154+ if (a < 0 && b > 0 && a < INTPTR_MIN / b) { *r = a * b; return TRUE; }
155+ if (a > 0 && b < 0 && b < INTPTR_MIN / a) { *r = a * b; return TRUE; }
156+ if (a < 0 && b < 0 && (a <= INTPTR_MIN || b <= INTPTR_MIN || -a > INTPTR_MAX / -b)) {
155157 *r = a * b; return TRUE;
156158 }
157159 *r = a * b;
158160 return FALSE;
159161 }
160-#undef SP_INT_OVERFLOW_MASK
162+#undef SP_INT_OVF_SIGN
161163 #endif
162164
163165 /* Three modes selected by `--int-overflow=raise|wrap|promote` on the