From 260eae2727c3e5e85e95c310efdebae221946a4b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 13 Nov 2026 17:24:54 +0000 Subject: [PATCH] Trap i64.MIN remainder overflow in the Lean operator. N2 requires i64.MIN % -1 to trap with overflow, the same as division. The expression operator returned 0, and the printed MiniRust remainder checked only the remainder in i128. Both now check the quotient against the static range first. Vectors cover the Lean operator, the core evaluator, the emit differential, and the reference interpreter. Co-authored-by: David Charlot --- spec/checker/tests/n2_min.rs | 20 +++++++++++++ spec/lean/RCE/Core/Vectors.lean | 5 ++++ spec/lean/RCE/EmitPrint.lean | 2 +- spec/lean/RCE/Expr.lean | 51 +++++++++++++++++++++++++++++---- spec/lean/emit-test/Gen.lean | 14 +++++++-- 5 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 spec/checker/tests/n2_min.rs diff --git a/spec/checker/tests/n2_min.rs b/spec/checker/tests/n2_min.rs new file mode 100644 index 00000000..0a586053 --- /dev/null +++ b/spec/checker/tests/n2_min.rs @@ -0,0 +1,20 @@ +//! Book 6.3 N2: `i64.MIN / -1` and `i64.MIN % -1` both trap. The quotient +//! `9223372036854775808` is outside i64. The Lean models state the same +//! vectors (`RCE/Expr.lean`, `RCE/Core/Vectors.lean`). + +#[test] +fn i64_min_div_and_rem_trap_overflow() { + let src = |op: &str| { + format!( + "fn main()\n let m: i64 = -9223372036854775807 - 1\n emit (m {op} -1)\n" + ) + }; + for op in ["/", "%"] { + let r = rce_spec_check::cli::run(&src(op), None).unwrap_or_else(|e| panic!("{op}: {e}")); + let text = r.lines.join("\n"); + assert!( + r.kept && text.contains("overflow: 9223372036854775808 is outside i64"), + "{op}: {text}" + ); + } +} diff --git a/spec/lean/RCE/Core/Vectors.lean b/spec/lean/RCE/Core/Vectors.lean index faf17b20..dab5e5e5 100644 --- a/spec/lean/RCE/Core/Vectors.lean +++ b/spec/lean/RCE/Core/Vectors.lean @@ -40,6 +40,11 @@ namespace RCE.Core private abbrev i8 : Ty := .int (-128) 127 private abbrev u8' : Ty := .int 0 255 example : eval [] 10 [] (.bin .rem i8 (.lit (.int (-128))) (.lit (.int (-1)))) = .err (.overflow 128 (-128) 127) := by decide +private abbrev i64 : Ty := .int (-9223372036854775808) 9223372036854775807 +example : eval [] 10 [] (.bin .div i64 (.lit (.int (-9223372036854775808))) (.lit (.int (-1)))) = .err (.overflow 9223372036854775808 (-9223372036854775808) 9223372036854775807) := by + rfl +example : eval [] 10 [] (.bin .rem i64 (.lit (.int (-9223372036854775808))) (.lit (.int (-1)))) = .err (.overflow 9223372036854775808 (-9223372036854775808) 9223372036854775807) := by + rfl example : eval [] 10 [] (.bin .band i8 (.lit (.int (-6))) (.lit (.int 13))) = .ok (.int 8) := by decide example : eval [] 10 [] (.bin .bor i8 (.lit (.int (-6))) (.lit (.int 13))) = .ok (.int (-1)) := by decide example : eval [] 10 [] (.bin .bxor i8 (.lit (.int (-6))) (.lit (.int 13))) = .ok (.int (-9)) := by decide diff --git a/spec/lean/RCE/EmitPrint.lean b/spec/lean/RCE/EmitPrint.lean index 69b319d2..dc69d6bd 100644 --- a/spec/lean/RCE/EmitPrint.lean +++ b/spec/lean/RCE/EmitPrint.lean @@ -18,7 +18,7 @@ fn binop(op: Op, t: Option<(i128, i128)>, a: Val, b: Val) -> Result { (Op::Sub, Some((lo, hi)), Val::Int(x), Val::Int(y)) => chk(lo, hi, x.checked_sub(y)), (Op::Mul, Some((lo, hi)), Val::Int(x), Val::Int(y)) => chk(lo, hi, x.checked_mul(y)), (Op::Div, Some((lo, hi)), Val::Int(x), Val::Int(y)) => if y == 0 { Err(Trap::DivZero) } else { chk(lo, hi, x.checked_div(y)) }, - (Op::Rem, Some((lo, hi)), Val::Int(x), Val::Int(y)) => if y == 0 { Err(Trap::DivZero) } else { chk(lo, hi, x.checked_rem(y)) }, + (Op::Rem, Some((lo, hi)), Val::Int(x), Val::Int(y)) => if y == 0 { Err(Trap::DivZero) } else { chk(lo, hi, x.checked_div(y))?; chk(lo, hi, x.checked_rem(y)) }, (Op::Lt, _, Val::Int(x), Val::Int(y)) => Ok(Val::Bool(x < y)), (Op::Eq, _, x, y) => Ok(Val::Bool(x == y)), _ => Err(Trap::BadIndex), diff --git a/spec/lean/RCE/Expr.lean b/spec/lean/RCE/Expr.lean index 1c7e8fc7..b0606001 100644 --- a/spec/lean/RCE/Expr.lean +++ b/spec/lean/RCE/Expr.lean @@ -56,7 +56,13 @@ def binop (op : BinOp) (t : Ty) (a b : Val) : Except Trap Val := | .sub, .int lo hi, .int x, .int y => chk lo hi (x - y) | .mul, .int lo hi, .int x, .int y => chk lo hi (x * y) | .div, .int lo hi, .int x, .int y => if y = 0 then .error .divZero else chk lo hi (tdiv x y) - | .rem, .int lo hi, .int x, .int y => if y = 0 then .error .divZero else chk lo hi (trem x y) + -- N2: `%` checks the quotient before the remainder, so `i64.MIN % -1` + -- traps with overflow, the same as `i64.MIN / -1`. + | .rem, .int lo hi, .int x, .int y => + if y = 0 then .error .divZero else + match chk lo hi (tdiv x y) with + | .error t => .error t + | .ok _ => chk lo hi (trem x y) | .lt, _, .int x, .int y => .ok (.bool (decide (x < y))) | .eq, _, x, y => .ok (.bool (decide (x = y))) | _, _, _, _ => .error .badIndex @@ -239,11 +245,30 @@ theorem chk_ty {lo hi r v} (h : chk lo hi r = .ok v) : v.hasTy (.int lo hi) := b /-- Operators preserve types (each checked result lies in its range). -/ theorem binop_ty {op ta t x y r} (ho : OpTy op ta t) (hx : x.hasTy ta) (hy : y.hasTy ta) (h : binop op ta x y = .ok r) : r.hasTy t := by - cases ho <;> cases x <;> cases y <;> simp [binop] at h <;> simp [Val.hasTy] at hx hy - all_goals first - | exact chk_ty h - | (cases h; simp [Val.hasTy]) - | (split at h <;> first | cases h | exact chk_ty h) + cases ho with + | @rem lo hi => + cases x with + | bool _ => simp [binop] at h + | int n1 => + cases y with + | bool _ => simp [binop] at h + | int n => + simp [Val.hasTy] at hx hy + by_cases hz : n = 0 + · simp [binop, hz] at h + · simp only [binop] at h + rw [if_neg hz] at h + cases hc : chk lo hi (tdiv n1 n) with + | error _ => simp [hc] at h + | ok _ => + simp [hc] at h + exact chk_ty h + | _ => + cases x <;> cases y <;> simp [binop] at h <;> simp [Val.hasTy] at hx hy + all_goals first + | exact chk_ty h + | (cases h; simp [Val.hasTy]) + | (split at h <;> first | cases h | exact chk_ty h) /-- **T5 Preservation** (expressions). -/ theorem preservation {Γ E e e' t} (hE : WtEnv Γ E) (hs : Step E e e') (ht : HasTy Γ e t) : @@ -376,4 +401,18 @@ theorem eval_ty {Γ E e t v} (hE : WtEnv Γ E) (ht : HasTy Γ e t) (h : eval E e · cases h · cases h +/-! N2: `i64.MIN / -1` and `i64.MIN % -1` both trap. The quotient + `9223372036854775808` is outside i64. -/ +def i64 : Ty := .int (-9223372036854775808) 9223372036854775807 + +example : binop .div i64 (.int (-9223372036854775808)) (.int (-1)) = .error .overflow := by + rfl + +example : binop .rem i64 (.int (-9223372036854775808)) (.int (-1)) = .error .overflow := by + rfl + +/-- A remainder whose quotient is in range keeps the dividend's sign. -/ +example : binop .rem i64 (.int (-7)) (.int 2) = .ok (.int (-1)) := by + rfl + end RCE diff --git a/spec/lean/emit-test/Gen.lean b/spec/lean/emit-test/Gen.lean index b052fe6c..f1782abe 100644 --- a/spec/lean/emit-test/Gen.lean +++ b/spec/lean/emit-test/Gen.lean @@ -51,10 +51,20 @@ def pRes : SRes → String | .ok fs os => s!"Ok(({pList fs}, {pList os}))" | .ref r => s!"Err({pReason r})" +/-- One fixed case: `name` emits `i64.MIN op -1`. The expected outcome is + `exec`, so the printed prelude is checked against the Lean operator. -/ +def edgeCase (name : String) (op : BinOp) : String × String := + let s : Stmt := .emit (.bin op RCE.i64 (.lit (.int (-9223372036854775808))) (.lit (.int (-1)))) + let exp := exec [] s [] [] + let call := s!" \{ let mut fs: Vec = vec![]; let mut os: Vec = vec![]; let r = {name}(&mut fs, &mut os, &vec![]).map(|_| (fs, os)); let e: Result<(Vec, Vec), Reason> = {pRes exp}; if r != e \{ bad += 1; eprintln!(\"{name}: \{:?} vs \{:?}\", r, e); } n += 1; if e.is_err() \{ kept += 1; } }\n" + (emitFn name s, call) + def build (nprog ninp : Nat) : String := Id.run do let mut g : G := ⟨20261012⟩ - let mut fns := "" - let mut tests := "" + let (fDiv, tDiv) := edgeCase "i64_min_div" .div + let (fRem, tRem) := edgeCase "i64_min_rem" .rem + let mut fns := fDiv ++ fRem + let mut tests := tDiv ++ tRem for p in List.range nprog do let (s, g1) := genS g 4; g := g1 fns := fns ++ emitFn s!"p{p}" s