|
| 1 | +import unittest |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from saw_client import * |
| 5 | +from saw_client.crucible import array, cry, cry_f |
| 6 | +from saw_client.mir import Contract, FreshVar, MIRType, SetupVal, u32, u64, void |
| 7 | + |
| 8 | + |
| 9 | +def ref_to_fresh(c : Contract, ty : MIRType, name : Optional[str] = None, |
| 10 | + read_only : bool = False) -> Tuple[FreshVar, SetupVal]: |
| 11 | + """Add to ``Contract`` ``c`` an allocation of a reference of type ``ty`` initialized to an unknown fresh value. |
| 12 | + If ``read_only == True`` then the allocated memory is immutable. |
| 13 | +
|
| 14 | + :returns A fresh variable bound to the reference's initial value and the newly allocated reference. (The fresh |
| 15 | + variable will be assigned ``name`` if provided/available.)""" |
| 16 | + var = c.fresh_var(ty, name) |
| 17 | + ptr = c.alloc(ty, points_to = var, read_only = read_only) |
| 18 | + return (var, ptr) |
| 19 | + |
| 20 | + |
| 21 | +class FContract(Contract): |
| 22 | + def specification(self) -> None: |
| 23 | + (x0, x0_p) = ref_to_fresh(self, u32, "x0", read_only = True) |
| 24 | + (x1, x1_p) = ref_to_fresh(self, u32, "x1", read_only = True) |
| 25 | + x = array(x0_p, x1_p) |
| 26 | + |
| 27 | + self.execute_func(x) |
| 28 | + |
| 29 | + self.returns_f('{x0} + {x1}') |
| 30 | + |
| 31 | + |
| 32 | +class GContract(Contract): |
| 33 | + def specification(self) -> None: |
| 34 | + x0_p = self.alloc(u32) |
| 35 | + (x1, x1_p) = ref_to_fresh(self, u32, "x1") |
| 36 | + x = array(x0_p, x1_p) |
| 37 | + |
| 38 | + self.execute_func(x) |
| 39 | + |
| 40 | + self.points_to(x0_p, cry_f('42 : [32]')) |
| 41 | + self.points_to(x1_p, cry_f('{x1} + 1')) |
| 42 | + self.returns(void) |
| 43 | + |
| 44 | + |
| 45 | +class HContract(Contract): |
| 46 | + def specification(self) -> None: |
| 47 | + self.execute_func() |
| 48 | + |
| 49 | + x0_ptr = self.alloc(u32, points_to = cry_f('27 : [32]'), read_only = True) |
| 50 | + x1_ptr = self.alloc(u32, points_to = cry_f('42 : [32]'), read_only = True) |
| 51 | + self.returns(array(x0_ptr, x1_ptr)) |
| 52 | + |
| 53 | + |
| 54 | +class IContract(Contract): |
| 55 | + def specification(self) -> None: |
| 56 | + self.execute_func(cry_f('[] : [0][32]')) |
| 57 | + |
| 58 | + self.returns(array(element_type = u64)) |
| 59 | + |
| 60 | + |
| 61 | +class MIRArraysTest(unittest.TestCase): |
| 62 | + def test_mir_points_to(self): |
| 63 | + connect(reset_server=True) |
| 64 | + if __name__ == "__main__": view(LogResults()) |
| 65 | + json_name = str(Path('tests', 'saw', 'test-files', 'mir_arrays.linked-mir.json')) |
| 66 | + mod = mir_load_module(json_name) |
| 67 | + |
| 68 | + f_result = mir_verify(mod, 'mir_arrays::f', FContract()) |
| 69 | + self.assertIs(f_result.is_success(), True) |
| 70 | + |
| 71 | + g_result = mir_verify(mod, 'mir_arrays::g', GContract()) |
| 72 | + self.assertIs(g_result.is_success(), True) |
| 73 | + |
| 74 | + h_result = mir_verify(mod, 'mir_arrays::h', HContract()) |
| 75 | + self.assertIs(h_result.is_success(), True) |
| 76 | + |
| 77 | + i_result = mir_verify(mod, 'mir_arrays::i', IContract()) |
| 78 | + self.assertIs(i_result.is_success(), True) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + unittest.main() |
0 commit comments