Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions spec/checker/tests/n2_min.rs
Original file line number Diff line number Diff line change
@@ -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}"
);
}
}
5 changes: 5 additions & 0 deletions spec/lean/RCE/Core/Vectors.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/lean/RCE/EmitPrint.lean
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn binop(op: Op, t: Option<(i128, i128)>, a: Val, b: Val) -> Result<Val, Trap> {
(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),
Expand Down
51 changes: 45 additions & 6 deletions spec/lean/RCE/Expr.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) :
Expand Down Expand Up @@ -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
14 changes: 12 additions & 2 deletions spec/lean/emit-test/Gen.lean
Original file line number Diff line number Diff line change
Expand Up @@ -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<Val> = vec![]; let mut os: Vec<Val> = vec![]; let r = {name}(&mut fs, &mut os, &vec![]).map(|_| (fs, os)); let e: Result<(Vec<Val>, Vec<Val>), 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
Expand Down
Loading