Skip to content

Commit

Permalink
Merge pull request #129 from AeneasVerif/dyn-checks-earlier
Browse files Browse the repository at this point in the history
Rework the removal of dynamic checks
  • Loading branch information
Nadrieril authored May 6, 2024
2 parents f8fab8d + 036d0d3 commit 1a205c5
Show file tree
Hide file tree
Showing 20 changed files with 432 additions and 379 deletions.
2 changes: 1 addition & 1 deletion charon-ml/src/CharonVersion.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(* This is an automatically generated file, generated from `charon/Cargo.toml`. *)
(* To re-generate this file, rune `make` in the root directory *)
let supported_charon_version = "0.1.1"
let supported_charon_version = "0.1.2"
3 changes: 3 additions & 0 deletions charon-ml/src/Expressions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ type binop =
| Add
| Sub
| Mul
| CheckedAdd
| CheckedSub
| CheckedMul
| Shl
| Shr
[@@deriving show, ord]
Expand Down
4 changes: 3 additions & 1 deletion charon-ml/src/ExpressionsUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ let unop_can_fail (unop : unop) : bool =

let binop_can_fail (binop : binop) : bool =
match binop with
| BitXor | BitAnd | BitOr | Eq | Lt | Le | Ne | Ge | Gt -> false
| BitXor | BitAnd | BitOr | Eq | Lt | Le | Ne | Ge | Gt | CheckedAdd
| CheckedSub | CheckedMul ->
false
| Div | Rem | Add | Sub | Mul | Shl | Shr -> true
3 changes: 3 additions & 0 deletions charon-ml/src/GAstOfJson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,9 @@ let binop_of_json (js : json) : (binop, string) result =
| `String "Add" -> Ok Add
| `String "Sub" -> Ok Sub
| `String "Mul" -> Ok Mul
| `String "CheckedAdd" -> Ok CheckedAdd
| `String "CheckedSub" -> Ok CheckedSub
| `String "CheckedMul" -> Ok CheckedMul
| `String "Shl" -> Ok Shl
| `String "Shr" -> Ok Shr
| _ -> Error ("binop_of_json failed on:" ^ show js)
Expand Down
3 changes: 3 additions & 0 deletions charon-ml/src/PrintExpressions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ let binop_to_string (binop : binop) : string =
| Add -> "+"
| Sub -> "-"
| Mul -> "*"
| CheckedAdd -> "checked.+"
| CheckedSub -> "checked.-"
| CheckedMul -> "checked.*"
| Shl -> "<<"
| Shr -> ">>"

Expand Down
2 changes: 1 addition & 1 deletion charon/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion charon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "charon"
version = "0.1.1"
version = "0.1.2"
authors = ["Son Ho <[email protected]>"]
edition = "2021"

Expand Down
22 changes: 15 additions & 7 deletions charon/src/ast/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,27 @@ pub enum BinOp {
Ne,
Ge,
Gt,
/// Can fail if the divisor is 0.
/// Fails if the divisor is 0, or if the operation is `int::MIN / -1`.
Div,
/// Can fail if the divisor is 0.
/// Fails if the divisor is 0, or if the operation is `int::MIN % -1`.
Rem,
/// Can overflow
/// Fails on overflow.
Add,
/// Can overflow
/// Fails on overflow.
Sub,
/// Can overflow
/// Fails on overflow.
Mul,
/// Can fail if the shift is too big
/// Returns `(result, did_overflow)`, where `result` is the result of the operation with
/// wrapping semantics, and `did_overflow` is a boolean that indicates whether the operation
/// overflowed. This operation does not fail.
CheckedAdd,
/// Like `CheckedAdd`.
CheckedSub,
/// Like `CheckedAdd`.
CheckedMul,
/// Fails if the shift is bigger than the bit-size of the type.
Shl,
/// Can fail if the shift is too big
/// Fails if the shift is bigger than the bit-size of the type.
Shr,
// No Offset binary operation: this is an operation on raw pointers
}
Expand Down
17 changes: 9 additions & 8 deletions charon/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ pub fn translate(
// we simply apply some micro-passes to make the code cleaner, before
// serializing the result.

// # Micro-pass: Remove overflow/div-by-zero/bounds checks since they are already part of the
// arithmetic/array operation in the semantics of (U)LLBC.
// **WARNING**: this pass uses the fact that the dynamic checks introduced by Rustc use a
// special "assert" construct. Because of this, it must happen *before* the
// [reconstruct_asserts] pass. See the comments in [crate::remove_dynamic_checks].
// **WARNING**: this pass relies on a precise structure of the MIR statements. Because of this,
// it must happen before passes that insert statements like [simplify_constants].
remove_dynamic_checks::transform(&mut ctx);

// # Micro-pass: desugar the constants to other values/operands as much
// as possible.
simplify_constants::transform(&mut ctx);
Expand Down Expand Up @@ -304,14 +313,6 @@ pub fn translate(
// which ignores this first variable. This micro-pass updates this.
update_closure_signatures::transform(&ctx, &mut llbc_funs);

// # Micro-pass: remove the dynamic checks for array/slice bounds
// and division by zero.
// **WARNING**: this pass uses the fact that the dynamic checks
// introduced by Rustc use a special "assert" construct. Because of
// this, it must happen *before* the [reconstruct_asserts] pass.
// See the comments in [crate::remove_dynamic_checks].
remove_dynamic_checks::transform(&mut ctx, &mut llbc_funs, &mut llbc_globals);

// # Micro-pass: reconstruct the asserts
reconstruct_asserts::transform(&mut ctx, &mut llbc_funs, &mut llbc_globals);

Expand Down
1 change: 1 addition & 0 deletions charon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#![recursion_limit = "256"]
#![feature(trait_alias)]
#![feature(let_chains)]
#![feature(if_let_guard)]
#![feature(iterator_try_collect)]

extern crate rustc_ast;
Expand Down
3 changes: 3 additions & 0 deletions charon/src/pretty/fmt_with_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,9 @@ impl std::fmt::Display for BinOp {
BinOp::Add => write!(f, "+"),
BinOp::Sub => write!(f, "-"),
BinOp::Mul => write!(f, "*"),
BinOp::CheckedAdd => write!(f, "checked.+"),
BinOp::CheckedSub => write!(f, "checked.-"),
BinOp::CheckedMul => write!(f, "checked.*"),
BinOp::Shl => write!(f, "<<"),
BinOp::Shr => write!(f, ">>"),
}
Expand Down
Loading

0 comments on commit 1a205c5

Please sign in to comment.