-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for a `mir_array_value` function in the MIR backend, which allows constructing array `SetupValue`s. This largely behaves like `llvm_array_value`s in the LLVM backend, but with the following differences: * Unlike in the LLVM backend, MIR arrays can have length 0. To account for this possibility, `mir_array_value` has a `MIRType` argument to ensure that SAW can always infer the type of the array, even if there are no element values from which to infer the type. Similarly, the SAW remote API's `array()` function now has an optional `element_type` kwarg that can be used to specify the element type in the event that there are no element values. * Because only the MIR backend makes use of the element type, this is encoded in the `XSetupArray` extension field. This checks off one box in #1859.
- Loading branch information
1 parent
8b8ff9b
commit f12cdf4
Showing
23 changed files
with
323 additions
and
46 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
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 |
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
pub fn f(x: [&u32; 2]) -> u32 { | ||
x[0].wrapping_add(*x[1]) | ||
} | ||
|
||
pub fn g(x: [&mut u32; 2]) { | ||
*x[0] = 42; | ||
*x[1] = x[1].wrapping_add(1); | ||
} | ||
|
||
pub fn h() -> [&'static u32; 2] { | ||
[&27, &42] | ||
} | ||
|
||
pub fn i(_x: [u32; 0]) -> [u64; 0] { | ||
[] | ||
} |
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,80 @@ | ||
enable_experimental; | ||
|
||
let f_spec = do { | ||
x0_ref <- mir_alloc mir_u32; | ||
x0 <- mir_fresh_var "x0" mir_u32; | ||
mir_points_to x0_ref (mir_term x0); | ||
|
||
x1_ref <- mir_alloc mir_u32; | ||
x1 <- mir_fresh_var "x1" mir_u32; | ||
mir_points_to x1_ref (mir_term x1); | ||
|
||
let x = mir_array_value (mir_ref mir_u32) [x0_ref, x1_ref]; | ||
|
||
mir_execute_func [x]; | ||
|
||
mir_return (mir_term {{ x0 + x1 }}); | ||
}; | ||
|
||
let g_spec = do { | ||
x0_ref <- mir_alloc_mut mir_u32; | ||
|
||
x1_ref <- mir_alloc_mut mir_u32; | ||
x1 <- mir_fresh_var "x1" mir_u32; | ||
mir_points_to x1_ref (mir_term x1); | ||
|
||
let x = mir_array_value (mir_ref_mut mir_u32) [x0_ref, x1_ref]; | ||
|
||
mir_execute_func [x]; | ||
|
||
mir_points_to x0_ref (mir_term {{ 42 : [32] }}); | ||
mir_points_to x1_ref (mir_term {{ x1 + 1 }}); | ||
}; | ||
|
||
let h_spec = do { | ||
mir_execute_func []; | ||
|
||
x0_ref <- mir_alloc mir_u32; | ||
mir_points_to x0_ref (mir_term {{ 27 : [32] }}); | ||
|
||
x1_ref <- mir_alloc mir_u32; | ||
mir_points_to x1_ref (mir_term {{ 42 : [32] }}); | ||
|
||
let x = mir_array_value (mir_ref mir_u32) [x0_ref, x1_ref]; | ||
mir_return x; | ||
}; | ||
|
||
let i_spec = do { | ||
let x = mir_term {{ [] : [0][32] }}; | ||
mir_execute_func [x]; | ||
|
||
mir_return (mir_array_value mir_u64 []); | ||
}; | ||
|
||
let i_spec_bad1 = do { | ||
let x = mir_term {{ [42] : [1][32] }}; | ||
mir_execute_func [x]; | ||
|
||
mir_return (mir_array_value mir_u64 []); | ||
}; | ||
|
||
let i_spec_bad2 = do { | ||
let x = mir_term {{ [] : [0][32] }}; | ||
mir_execute_func [x]; | ||
|
||
mir_return (mir_array_value mir_u64 [mir_term {{ 42 : [64] }}]); | ||
}; | ||
|
||
m <- mir_load_module "test.linked-mir.json"; | ||
|
||
mir_verify m "test::f" [] false f_spec z3; | ||
mir_verify m "test::g" [] false g_spec z3; | ||
mir_verify m "test::h" [] false h_spec z3; | ||
mir_verify m "test::i" [] false i_spec z3; | ||
|
||
fails ( | ||
mir_verify m "test::i" [] false i_spec_bad1 z3 | ||
); | ||
fails ( | ||
mir_verify m "test::i" [] false i_spec_bad2 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
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_arrays.linked-mir.json
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
pub fn f(x: [&u32; 2]) -> u32 { | ||
x[0].wrapping_add(*x[1]) | ||
} | ||
|
||
pub fn g(x: [&mut u32; 2]) { | ||
*x[0] = 42; | ||
*x[1] = x[1].wrapping_add(1); | ||
} | ||
|
||
pub fn h() -> [&'static u32; 2] { | ||
[&27, &42] | ||
} | ||
|
||
pub fn i(_x: [u32; 0]) -> [u64; 0] { | ||
[] | ||
} |
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,82 @@ | ||
import unittest | ||
from pathlib import Path | ||
|
||
from saw_client import * | ||
from saw_client.crucible import array, cry, cry_f | ||
from saw_client.mir import Contract, FreshVar, MIRType, SetupVal, u32, u64, 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 FContract(Contract): | ||
def specification(self) -> None: | ||
(x0, x0_p) = ref_to_fresh(self, u32, "x0", read_only = True) | ||
(x1, x1_p) = ref_to_fresh(self, u32, "x1", read_only = True) | ||
x = array(x0_p, x1_p) | ||
|
||
self.execute_func(x) | ||
|
||
self.returns_f('{x0} + {x1}') | ||
|
||
|
||
class GContract(Contract): | ||
def specification(self) -> None: | ||
x0_p = self.alloc(u32) | ||
(x1, x1_p) = ref_to_fresh(self, u32, "x1") | ||
x = array(x0_p, x1_p) | ||
|
||
self.execute_func(x) | ||
|
||
self.points_to(x0_p, cry_f('42 : [32]')) | ||
self.points_to(x1_p, cry_f('{x1} + 1')) | ||
self.returns(void) | ||
|
||
|
||
class HContract(Contract): | ||
def specification(self) -> None: | ||
self.execute_func() | ||
|
||
x0_ptr = self.alloc(u32, points_to = cry_f('27 : [32]'), read_only = True) | ||
x1_ptr = self.alloc(u32, points_to = cry_f('42 : [32]'), read_only = True) | ||
self.returns(array(x0_ptr, x1_ptr)) | ||
|
||
|
||
class IContract(Contract): | ||
def specification(self) -> None: | ||
self.execute_func(cry_f('[] : [0][32]')) | ||
|
||
self.returns(array(element_type = u64)) | ||
|
||
|
||
class MIRArraysTest(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_arrays.linked-mir.json')) | ||
mod = mir_load_module(json_name) | ||
|
||
f_result = mir_verify(mod, 'mir_arrays::f', FContract()) | ||
self.assertIs(f_result.is_success(), True) | ||
|
||
g_result = mir_verify(mod, 'mir_arrays::g', GContract()) | ||
self.assertIs(g_result.is_success(), True) | ||
|
||
h_result = mir_verify(mod, 'mir_arrays::h', HContract()) | ||
self.assertIs(h_result.is_success(), True) | ||
|
||
i_result = mir_verify(mod, 'mir_arrays::i', IContract()) | ||
self.assertIs(i_result.is_success(), True) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
Oops, something went wrong.