|
| 1 | +from pathlib import Path |
| 2 | +import unittest |
| 3 | +from saw_client import * |
| 4 | +from saw_client.llvm import Contract, FreshVar, LLVMType, SetupVal, array_ty, cryptol, i64, void |
| 5 | +from saw_client.option import LaxPointerOrdering |
| 6 | + |
| 7 | + |
| 8 | +LEN = 42 |
| 9 | + |
| 10 | + |
| 11 | +def ptr_to_fresh(c : Contract, ty : LLVMType, name : Optional[str] = None, |
| 12 | + read_only : bool = False) -> Tuple[FreshVar, SetupVal]: |
| 13 | + """Add to ``Contract`` ``c`` an allocation of a pointer of type ``ty`` initialized to an unknown fresh value. |
| 14 | + If ``read_only == True`` then the allocated memory is immutable. |
| 15 | +
|
| 16 | + :returns A fresh variable bound to the pointers initial value and the newly allocated pointer. (The fresh |
| 17 | + variable will be assigned ``name`` if provided/available.)""" |
| 18 | + var = c.fresh_var(ty, name) |
| 19 | + ptr = c.alloc(ty, points_to = var, read_only = read_only) |
| 20 | + return (var, ptr) |
| 21 | + |
| 22 | + |
| 23 | +class ZipWithAddContract(Contract): |
| 24 | + def specification(self): |
| 25 | + array_t = array_ty(LEN, i64) |
| 26 | + c_ptr = self.alloc(array_t) |
| 27 | + (a, a_ptr) = ptr_to_fresh(self, name='a', ty=array_t, read_only=True) |
| 28 | + (b, b_ptr) = ptr_to_fresh(self, name='b', ty=array_t, read_only=True) |
| 29 | + |
| 30 | + self.execute_func(c_ptr, a_ptr, b_ptr) |
| 31 | + |
| 32 | + self.points_to(c_ptr, cryptol(f'zipWith`{{ {LEN} }} (+) {a.name()} {b.name()}')) |
| 33 | + self.returns(void) |
| 34 | + |
| 35 | + |
| 36 | +class LLVMPointerTest(unittest.TestCase): |
| 37 | + |
| 38 | + def test_llvm_pointer(self): |
| 39 | + connect(reset_server=True) |
| 40 | + if __name__ == "__main__": view(LogResults()) |
| 41 | + bcname = str(Path('tests','saw','test-files', 'llvm_lax_pointer_ordering.bc')) |
| 42 | + mod = llvm_load_module(bcname) |
| 43 | + |
| 44 | + set_option(LaxPointerOrdering(), True) |
| 45 | + |
| 46 | + result = llvm_verify(mod, 'zip_with_add', ZipWithAddContract()) |
| 47 | + self.assertIs(result.is_success(), True) |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + unittest.main() |
0 commit comments