Commit 64a699fd
64a699fda165Merged
Ryan Seys <[email protected]> · 6 months ago · 9 files · +4 added · ~5 modified
Description
fix(codegen,runtime): raise ZeroDivisionError on integer division by zero
Previously a / 0, a % 0, a.divmod(0), a.ceildiv(0), and
a.pow(e, 0) triggered C undefined behaviour (SIGFPE on x86) for
mrb_int operands, or hard-exited via fprintf+exit for bigint
operands. Either way, the natural CRuby idiom didn't work:
def safe_div(a, b) a / b rescue ZeroDivisionError -1 end
This patch closes the gap end-to-end. Integer division-by-zero now raises ZeroDivisionError("divided by 0") through spinel's existing longjmp rescue net, where it can be caught like any other exception.
Runtime layer (lib/sp_runtime.h):
- sp_idiv, sp_imod: guard with
if (b == 0) sp_raise_cls(...). - sp_powmod: replace silent
if(mod==0)return 0with the same raise. - sp_ceildiv: replace silent
if(b==0)return 0with the same raise. - New non-static sp_bigint_raise_zerodiv wrapper for cross-TU use.
- Forward declaration of sp_raise_cls so the helpers can call it before its main definition appears further down the header.
Bigint runtime (lib/sp_bigint.c, lib/mruby_shim.h):
- sp_bigint_div / sp_bigint_mod: defensive
zero_p(&b->mpz)check before delegating to mini-gmp. Without this the mini-gmp internal assertion at udiv() line 2856 fires for zero-sized divisors, bypassing div_limb's mrb_raise path. - mruby_shim's mrb_raise macro now dispatches on the symbolic class enum: E_ZERODIV_ERROR routes through sp_bigint_raise_zerodiv (linked from gen.c) so it reaches spinel's rescue net rather than hard-exiting. Other E_* classes still fprintf+exit — they're internal invariants where graceful handling buys little.
Codegen (spinel_codegen.rb):
- Four sites previously emitted raw C
/or/=for integer operands, bypassing sp_idiv's guard. Replaced each with sp_idiv:- compile_int_method_expr divmod quotient (~23445)
- LocalVariableOrWriteNode
/=(~27790) - LocalVariableAndWriteNode
/=non-bigint case (~28106) - Remap-dispatch
/(~35397)
- Bigint operator dispatch and compound-assign sites cast away
volatilefrom local pointers when passing to the bigint runtime. Pre-existing latent issue: locals in setjmp-bearing functions are emitted volatile, but bigint runtime functions take plain pointers. Without this, my new bigint test trips-Werrorunder the test runner.
Float division by zero is intentionally unchanged — IEEE 754 returns Infinity/NaN per CRuby semantics; no exception expected.
New tests:
- test/integer_div_by_zero.rb: /, %, divmod, ceildiv, pow(.,0), /=, %=, bare-rescue, plus float-div-by-zero regression guard.
- test/bigint_div_by_zero.rb: same shape for bigint locals.
Doctrine update:
- test/endless_method_rescue.rb's comment about why the test uses
explicit
raiseinstead ofa / b— the SIGFPE rationale no longer applies; updated to point at the new test.
Tests: 348 pass, 0 fail, 0 error. Bootstrap: gen2.c == gen3.c. Hot-path probe (5M int divisions): ~10ms — branch predictor absorbs the b==0 check at near-zero cost.
Details
- Commit
- 64a699fda165
- Parent
- 2ff7ba46856e
lib/mruby_shim.hmodifiedView file
#define mrb_free(mrb, p) free(p)#define mrb_malloc_simple(mrb, sz) malloc(sz)/* Error handling */#define mrb_raise(mrb, cls, msg) do { fprintf(stderr, "%s\n", msg); exit(1); } while(0)#define mrb_int_zerodiv(mrb) do { fprintf(stderr, "divided by 0\n"); exit(1); } while(0)#define E_RANGE_ERROR 0#define E_RUNTIME_ERROR 0#define E_ARGUMENT_ERROR 0#define E_TYPE_ERROR 0/* Error handling — sp_bigint.c is compiled as a separate TU and would otherwise hard-exit on internal errors (mini-gmp's zero- divisor check, allocation overflow, etc.). The mrb_raise macro now dispatches on the symbolic class enum: ZeroDivisionError is routed through sp_bigint_raise_zerodiv (defined non-static in sp_runtime.h, linked from gen.c) so it reaches spinel's longjmp rescue net and is catchable by `rescue ZeroDivisionError`. Other classes still hard-exit — they're internal invariants (allocation overflow) where graceful handling buys little. */extern void sp_bigint_raise_zerodiv(const char *msg);#define E_RANGE_ERROR 1#define E_RUNTIME_ERROR 2#define E_ARGUMENT_ERROR 3#define E_TYPE_ERROR 4#define E_ZERODIV_ERROR 5#define mrb_raise(mrb, cls, msg) do { \ if ((cls) == E_ZERODIV_ERROR) { sp_bigint_raise_zerodiv(msg); } \ else { fprintf(stderr, "%s\n", msg); exit(1); } \} while(0)#define mrb_int_zerodiv(mrb) sp_bigint_raise_zerodiv("divided by 0")/* mrb_value stub - tagged union */typedef struct mrb_value {lib/sp_bigint.cmodifiedView file
Loading diff…
lib/sp_runtime.hmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/bigint_div_by_zero.rbaddedView file
Loading diff…
test/bigint_div_by_zero.rb.expectedaddedView file
Loading diff…
test/endless_method_rescue.rbmodifiedView file
Loading diff…
test/integer_div_by_zero.rbaddedView file
Loading diff…
test/integer_div_by_zero.rb.expectedaddedView file
Loading diff…