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 0 with the same raise.
  • sp_ceildiv: replace silent if(b==0)return 0 with 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 volatile from 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 -Werror under 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 raise instead of a / 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

@@ -41,13 +41,26 @@
4141 #define mrb_free(mrb, p) free(p)
4242 #define mrb_malloc_simple(mrb, sz) malloc(sz)
4343
44-/* Error handling */
45-#define mrb_raise(mrb, cls, msg) do { fprintf(stderr, "%s\n", msg); exit(1); } while(0)
46-#define mrb_int_zerodiv(mrb) do { fprintf(stderr, "divided by 0\n"); exit(1); } while(0)
47-#define E_RANGE_ERROR 0
48-#define E_RUNTIME_ERROR 0
49-#define E_ARGUMENT_ERROR 0
50-#define E_TYPE_ERROR 0
44+/* Error handling — sp_bigint.c is compiled as a separate TU and
45+ would otherwise hard-exit on internal errors (mini-gmp's zero-
46+ divisor check, allocation overflow, etc.). The mrb_raise macro
47+ now dispatches on the symbolic class enum: ZeroDivisionError is
48+ routed through sp_bigint_raise_zerodiv (defined non-static in
49+ sp_runtime.h, linked from gen.c) so it reaches spinel's longjmp
50+ rescue net and is catchable by `rescue ZeroDivisionError`.
51+ Other classes still hard-exit — they're internal invariants
52+ (allocation overflow) where graceful handling buys little. */
53+extern void sp_bigint_raise_zerodiv(const char *msg);
54+#define E_RANGE_ERROR 1
55+#define E_RUNTIME_ERROR 2
56+#define E_ARGUMENT_ERROR 3
57+#define E_TYPE_ERROR 4
58+#define E_ZERODIV_ERROR 5
59+#define mrb_raise(mrb, cls, msg) do { \
60+ if ((cls) == E_ZERODIV_ERROR) { sp_bigint_raise_zerodiv(msg); } \
61+ else { fprintf(stderr, "%s\n", msg); exit(1); } \
62+} while(0)
63+#define mrb_int_zerodiv(mrb) sp_bigint_raise_zerodiv("divided by 0")
5164
5265 /* mrb_value stub - tagged union */
5366 typedef struct mrb_value {