-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1916 from GaloisInc/T1859-mir-verify-points-to
Support `mir_alloc`, `mir_alloc_mut`, and `mir_points_to`
- Loading branch information
Showing
24 changed files
with
476 additions
and
69 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
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
Binary file not shown.
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,13 @@ | ||
all: test.linked-mir.json | ||
|
||
test.linked-mir.json: test.rs | ||
saw-rustc $< | ||
$(MAKE) remove-unused-build-artifacts | ||
|
||
.PHONY: remove-unused-build-artifacts | ||
remove-unused-build-artifacts: | ||
rm -f test libtest.mir libtest.rlib | ||
|
||
.PHONY: clean | ||
clean: remove-unused-build-artifacts | ||
rm -f test.linked-mir.json |
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 @@ | ||
{"fns":[{"abi":{"kind":"Rust"},"args":[{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Ref::953fce25114368d0"},{"is_zst":false,"mut":{"kind":"Not"},"name":"_2","ty":"ty::u32"}],"body":{"blocks":[{"block":{"data":[{"kind":"Assign","lhs":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_3","ty":"ty::u32"}},"pos":"test.rs:6:10: 6:11","rhs":{"kind":"Use","usevar":{"data":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_2","ty":"ty::u32"}},"kind":"Copy"}}},{"kind":"Assign","lhs":{"data":[{"kind":"Deref"}],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Ref::953fce25114368d0"}},"pos":"test.rs:6:5: 6:11","rhs":{"kind":"Use","usevar":{"data":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_3","ty":"ty::u32"}},"kind":"Move"}}}],"terminator":{"kind":"Return","pos":"test.rs:7:2: 7:2"}},"blockid":"bb0"}],"vars":[{"is_zst":true,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::Tuple::e93222e871854c41"},{"is_zst":false,"mut":{"kind":"Mut"},"name":"_3","ty":"ty::u32"}]},"name":"test/398eaa54::write_to_ref","return_ty":"ty::Tuple::e93222e871854c41","spread_arg":null},{"abi":{"kind":"Rust"},"args":[{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Ref::e028c0f25e8b6323"}],"body":{"blocks":[{"block":{"data":[{"kind":"Assign","lhs":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::u32"}},"pos":"test.rs:2:5: 2:7","rhs":{"kind":"Use","usevar":{"data":{"data":[{"kind":"Deref"}],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Ref::e028c0f25e8b6323"}},"kind":"Copy"}}}],"terminator":{"kind":"Return","pos":"test.rs:3:2: 3:2"}},"blockid":"bb0"}],"vars":[{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::u32"}]},"name":"test/398eaa54::read_from_ref","return_ty":"ty::u32","spread_arg":null}],"adts":[],"statics":[],"vtables":[],"traits":[],"intrinsics":[{"inst":{"def_id":"test/398eaa54::write_to_ref","kind":"Item","substs":[]},"name":"test/398eaa54::write_to_ref"},{"inst":{"def_id":"test/398eaa54::read_from_ref","kind":"Item","substs":[]},"name":"test/398eaa54::read_from_ref"}],"tys":[{"name":"ty::u32","ty":{"kind":"Uint","uintkind":{"kind":"U32"}}},{"name":"ty::Ref::953fce25114368d0","ty":{"kind":"Ref","mutability":{"kind":"Mut"},"ty":"ty::u32"}},{"name":"ty::Tuple::e93222e871854c41","ty":{"kind":"Tuple","tys":[]}},{"name":"ty::Ref::e028c0f25e8b6323","ty":{"kind":"Ref","mutability":{"kind":"Not"},"ty":"ty::u32"}}],"roots":["test/398eaa54::read_from_ref","test/398eaa54::write_to_ref"]} |
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 @@ | ||
pub fn read_from_ref(x: &u32) -> u32 { | ||
*x | ||
} | ||
|
||
pub fn write_to_ref(x: &mut u32, y: u32) { | ||
*x = y | ||
} |
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,54 @@ | ||
enable_experimental; | ||
|
||
let read_from_ref_spec = do { | ||
ptr <- mir_alloc mir_u32; | ||
x <- mir_fresh_var "x" mir_u32; | ||
mir_points_to ptr (mir_term x); | ||
|
||
mir_execute_func [ptr]; | ||
|
||
mir_return (mir_term x); | ||
}; | ||
|
||
// This spec will cause SAW to read from uninitialized memory. | ||
|
||
let read_from_ref_spec_bad = do { | ||
ptr <- mir_alloc mir_u32; | ||
|
||
mir_execute_func [ptr]; | ||
|
||
mir_return (mir_term {{ 42 : [32] }}); | ||
}; | ||
|
||
let write_to_ref_spec = do { | ||
ptr <- mir_alloc_mut mir_u32; | ||
y <- mir_fresh_var "y" mir_u32; | ||
|
||
mir_execute_func [ptr, mir_term y]; | ||
|
||
mir_points_to ptr (mir_term y); | ||
}; | ||
|
||
// This spec contains a type error, as `write_to_ref` expects a mutable | ||
// reference, but the spec allocates an immutable reference. | ||
|
||
let write_to_ref_spec_bad = do { | ||
ptr <- mir_alloc mir_u32; | ||
y <- mir_fresh_var "y" mir_u32; | ||
|
||
mir_execute_func [ptr, mir_term y]; | ||
|
||
mir_points_to ptr (mir_term y); | ||
}; | ||
|
||
m <- mir_load_module "test.linked-mir.json"; | ||
|
||
mir_verify m "test::read_from_ref" [] false read_from_ref_spec z3; | ||
mir_verify m "test::write_to_ref" [] false write_to_ref_spec z3; | ||
|
||
fails ( | ||
mir_verify m "test::read_from_ref" [] false read_from_ref_spec_bad z3 | ||
); | ||
fails ( | ||
mir_verify m "test::write_to_ref" [] false write_to_ref_spec_bad z3 | ||
); |
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,3 @@ | ||
set -e | ||
|
||
$SAW test.saw |
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
1 change: 1 addition & 0 deletions
1
saw-remote-api/python/tests/saw/test-files/mir_points_to.linked-mir.json
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 @@ | ||
{"fns":[{"abi":{"kind":"Rust"},"args":[{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Ref::e028c0f25e8b6323"}],"body":{"blocks":[{"block":{"data":[{"kind":"Assign","lhs":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::u32"}},"pos":"mir_points_to.rs:2:5: 2:7","rhs":{"kind":"Use","usevar":{"data":{"data":[{"kind":"Deref"}],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Ref::e028c0f25e8b6323"}},"kind":"Copy"}}}],"terminator":{"kind":"Return","pos":"mir_points_to.rs:3:2: 3:2"}},"blockid":"bb0"}],"vars":[{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::u32"}]},"name":"mir_points_to/5452b69b::read_from_ref","return_ty":"ty::u32","spread_arg":null},{"abi":{"kind":"Rust"},"args":[{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Ref::953fce25114368d0"},{"is_zst":false,"mut":{"kind":"Not"},"name":"_2","ty":"ty::u32"}],"body":{"blocks":[{"block":{"data":[{"kind":"Assign","lhs":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_3","ty":"ty::u32"}},"pos":"mir_points_to.rs:6:10: 6:11","rhs":{"kind":"Use","usevar":{"data":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_2","ty":"ty::u32"}},"kind":"Copy"}}},{"kind":"Assign","lhs":{"data":[{"kind":"Deref"}],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Ref::953fce25114368d0"}},"pos":"mir_points_to.rs:6:5: 6:11","rhs":{"kind":"Use","usevar":{"data":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_3","ty":"ty::u32"}},"kind":"Move"}}}],"terminator":{"kind":"Return","pos":"mir_points_to.rs:7:2: 7:2"}},"blockid":"bb0"}],"vars":[{"is_zst":true,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::Tuple::e93222e871854c41"},{"is_zst":false,"mut":{"kind":"Mut"},"name":"_3","ty":"ty::u32"}]},"name":"mir_points_to/5452b69b::write_to_ref","return_ty":"ty::Tuple::e93222e871854c41","spread_arg":null}],"adts":[],"statics":[],"vtables":[],"traits":[],"intrinsics":[{"inst":{"def_id":"mir_points_to/5452b69b::read_from_ref","kind":"Item","substs":[]},"name":"mir_points_to/5452b69b::read_from_ref"},{"inst":{"def_id":"mir_points_to/5452b69b::write_to_ref","kind":"Item","substs":[]},"name":"mir_points_to/5452b69b::write_to_ref"}],"tys":[{"name":"ty::u32","ty":{"kind":"Uint","uintkind":{"kind":"U32"}}},{"name":"ty::Ref::e028c0f25e8b6323","ty":{"kind":"Ref","mutability":{"kind":"Not"},"ty":"ty::u32"}},{"name":"ty::Ref::953fce25114368d0","ty":{"kind":"Ref","mutability":{"kind":"Mut"},"ty":"ty::u32"}},{"name":"ty::Tuple::e93222e871854c41","ty":{"kind":"Tuple","tys":[]}}],"roots":["mir_points_to/5452b69b::read_from_ref","mir_points_to/5452b69b::write_to_ref"]} |
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 @@ | ||
pub fn read_from_ref(x: &u32) -> u32 { | ||
*x | ||
} | ||
|
||
pub fn write_to_ref(x: &mut u32, y: u32) { | ||
*x = y | ||
} |
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,56 @@ | ||
import unittest | ||
from pathlib import Path | ||
|
||
from saw_client import * | ||
from saw_client.crucible import cry, cry_f | ||
from saw_client.mir import Contract, FreshVar, MIRType, SetupVal, u32, void | ||
|
||
|
||
def ref_to_fresh(c : Contract, ty : MIRType, name : Optional[str] = None, | ||
read_only : bool = False) -> Tuple[FreshVar, SetupVal]: | ||
"""Add to ``Contract`` ``c`` an allocation of a reference of type ``ty`` initialized to an unknown fresh value. | ||
If ``read_only == True`` then the allocated memory is immutable. | ||
:returns A fresh variable bound to the reference's initial value and the newly allocated reference. (The fresh | ||
variable will be assigned ``name`` if provided/available.)""" | ||
var = c.fresh_var(ty, name) | ||
ptr = c.alloc(ty, points_to = var, read_only = read_only) | ||
return (var, ptr) | ||
|
||
|
||
class ReadFromRefContract(Contract): | ||
def specification(self) -> None: | ||
(x, x_p) = ref_to_fresh(self, u32, "x", read_only = True) | ||
|
||
self.execute_func(x_p) | ||
|
||
self.returns_f('{x}') | ||
|
||
|
||
class WriteToRefContract(Contract): | ||
def specification(self) -> None: | ||
ptr = self.alloc(u32, read_only = False) | ||
y = self.fresh_var(u32, 'y') | ||
|
||
self.execute_func(ptr, y) | ||
|
||
self.points_to(ptr, y) | ||
self.returns(void) | ||
|
||
|
||
class MIRPointsToTest(unittest.TestCase): | ||
def test_mir_points_to(self): | ||
connect(reset_server=True) | ||
if __name__ == "__main__": view(LogResults()) | ||
json_name = str(Path('tests', 'saw', 'test-files', 'mir_points_to.linked-mir.json')) | ||
mod = mir_load_module(json_name) | ||
|
||
read_from_ref_result = mir_verify(mod, 'mir_points_to::read_from_ref', ReadFromRefContract()) | ||
self.assertIs(read_from_ref_result.is_success(), True) | ||
|
||
write_to_ref_result = mir_verify(mod, 'mir_points_to::write_to_ref', WriteToRefContract()) | ||
self.assertIs(write_to_ref_result.is_success(), True) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.