-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,078 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
authors = ["The Fe Developers <[email protected]>"] | ||
categories = ["cryptography::cryptocurrencies", "command-line-utilities", "development-tools"] | ||
description = "Fe formal verification utilities" | ||
edition = "2021" | ||
keywords = ["ethereum", "fe", "yul", "smart", "contract", "compiler"] | ||
license = "GPL-3.0-or-later" | ||
name = "fv" | ||
readme = "README.md" | ||
repository = "https://github.com/ethereum/fe" | ||
version = "0.14.0-alpha" | ||
|
||
[features] | ||
solc-backend = ["fe-driver/solc-backend"] | ||
kevm-backend = [] | ||
|
||
[dependencies] | ||
fe-driver = {path = "../driver", version = "^0.14.0-alpha"} | ||
fe-yulgen = {path = "../yulgen", version = "^0.14.0-alpha"} | ||
fe-test-files = {path = "../test-files", version = "^0.14.0-alpha"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
This crate contains utilities for building and running kevm specs. A system installation of [evm-semantics](https://github.com/kframework/evm-semantics) is required to run the specs. | ||
|
||
Once the evm-semantics project has been built, set the `KEVM_PATH` environment variable. | ||
|
||
e.g. `$ export KEVM_PATH=/path/to/evm-semantics` | ||
|
||
and then run the tests: `$ cargo test --features "solc-backend, kevm-backend"`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#![cfg(feature = "kevm-backend")] | ||
use fe_yulgen::Db; | ||
use std::path::Path; | ||
use std::process::Command; | ||
use std::{env, fs}; | ||
|
||
const SPECS_DIR: &str = "tests/specs/fe/"; | ||
|
||
pub fn kevm_path() -> String { | ||
env::var("KEVM_PATH").expect("`KEVM_PATH` not set") | ||
} | ||
|
||
pub fn run_spec(name: &str, src_path: &str, src: &str, spec: &str) -> Result<(), String> { | ||
let kevm_path = kevm_path(); | ||
|
||
let spec = build_spec(name, src_path, src, spec); | ||
let spec_path = Path::new(&kevm_path) | ||
.join(SPECS_DIR) | ||
.join(name) | ||
.with_extension("k"); | ||
fs::write(spec_path.clone(), spec).expect("unable to write file"); | ||
|
||
let path = env::var("PATH").expect("PATH is not set"); | ||
|
||
let out = Command::new("kevm") | ||
.arg("prove") | ||
.arg(spec_path.to_str().unwrap()) | ||
.arg("--backend haskell") | ||
.arg("--format-failures") | ||
.arg("--directory") | ||
.arg("tests/specs/erc20/verification/haskell") | ||
.env("PATH", format!(".build/usr/bin:{}", path)) | ||
.current_dir(&kevm_path) | ||
.output() | ||
.expect("failed to execute process"); | ||
|
||
if out.status.code() != Some(0) { | ||
Err(format!( | ||
"{}\n{}", | ||
String::from_utf8_lossy(&out.stderr), | ||
String::from_utf8_lossy(&out.stdout) | ||
)) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn build_spec(name: &str, src_path: &str, src: &str, spec: &str) -> String { | ||
let mut db = Db::default(); | ||
let module = fe_driver::compile_single_file(&mut db, src_path, src, true, true).unwrap(); | ||
|
||
// replace placeholders | ||
let mut new_spec = spec.to_owned().replace("$TEST_NAME", &name.to_uppercase()); | ||
for (name, contract) in module.contracts.iter() { | ||
new_spec = new_spec.replace( | ||
&format!("${}::RUNTIME", name), | ||
&format!("\"0x{}\"", contract.runtime_bytecode), | ||
) | ||
} | ||
|
||
new_spec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![cfg(feature = "kevm-backend")] | ||
|
||
/// Checks if a contract spec is valid. | ||
macro_rules! test_spec { | ||
($name:ident, $src_path:expr, $spec_path:expr) => { | ||
#[test] | ||
fn $name() { | ||
let src = fe_test_files::fixture(concat!("kspecs/", $src_path)); | ||
let spec = fe_test_files::fixture(concat!("kspecs/", $spec_path)); | ||
|
||
fv::run_spec(&stringify!($name).replace("_", "-"), $src_path, &src, &spec) | ||
.expect("spec failed"); | ||
} | ||
}; | ||
} | ||
|
||
/// Checks if a contract spec is invalid. | ||
macro_rules! test_spec_invalid { | ||
($name:ident, $src_path:expr, $spec_path:expr) => { | ||
#[test] | ||
fn $name() { | ||
let src = fe_test_files::fixture(concat!("kspecs/", $src_path)); | ||
let spec = fe_test_files::fixture(concat!("kspecs/", $spec_path)); | ||
|
||
match fv::run_spec(&stringify!($name).replace("_", "-"), $src_path, &src, &spec) { | ||
Ok(()) => panic!("spec is valid"), | ||
Err(output) => { | ||
// we want to make sure it didn't fail for some other reason | ||
if !output.contains("the claimed implication is not valid") { | ||
panic!("spec claim was not checked {}", output) | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
test_spec! { sanity_returns_42, "sanity/foo.fe", "sanity/returns_42.k" } | ||
test_spec! { sanity_returns_in, "sanity/foo.fe", "sanity/returns_in.k" } | ||
test_spec! { sanity_returns_in_cond1, "sanity/foo.fe", "sanity/returns_in_cond1.k" } | ||
test_spec! { sanity_returns_in_cond2, "sanity/foo.fe", "sanity/returns_in_cond2.k" } | ||
|
||
// these are just for extra sanity | ||
test_spec_invalid! { sanity_returns_42_invalid, "sanity/foo.fe", "sanity/returns_42_invalid.k" } | ||
test_spec_invalid! { sanity_returns_in_invalid, "sanity/foo.fe", "sanity/returns_in_invalid.k" } | ||
test_spec_invalid! { sanity_returns_in_cond2_invalid, "sanity/foo.fe", "sanity/returns_in_cond2_invalid.k" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::evm | ||
|
||
# always returns 42 | ||
contract Returns42: | ||
pub fn __call__(): | ||
unsafe: | ||
evm::mstore(0, 42) | ||
evm::return_mem(0, 32) | ||
|
||
# always returns `input` | ||
contract ReturnsIn: | ||
pub fn __call__(): | ||
unsafe: | ||
let input: u256 = evm::call_data_load(0) | ||
evm::mstore(0, input) | ||
evm::return_mem(0, 32) | ||
|
||
# returns `input`, except when `input == 42`, in which case it will return `26` | ||
contract ReturnsInCond: | ||
pub fn __call__(): | ||
unsafe: | ||
let input: u256 = evm::call_data_load(0) | ||
let output: u256 = input | ||
|
||
if input == 42: | ||
output = 26 | ||
|
||
evm::mstore(0, output) | ||
evm::return_mem(0, 32) |
Oops, something went wrong.