Skip to content

Commit cb4abb5

Browse files
committed
mir_fresh_cryptol_var
This also adds some very rudimentary test cases. A more impressive test case would involve ghost state, but we will need #1958 before we can write such a test case. Fixes #1970.
1 parent 3b17eb0 commit cb4abb5

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
all: test.linked-mir.json
2+
3+
test.linked-mir.json: test.rs
4+
saw-rustc $<
5+
$(MAKE) remove-unused-build-artifacts
6+
7+
.PHONY: remove-unused-build-artifacts
8+
remove-unused-build-artifacts:
9+
rm -f test libtest.mir libtest.rlib
10+
11+
.PHONY: clean
12+
clean: remove-unused-build-artifacts
13+
rm -f test.linked-mir.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"fns":[{"abi":{"kind":"Rust"},"args":[{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Array::a60250c8af2ca6f4"}],"body":{"blocks":[{"block":{"data":[{"kind":"Assign","lhs":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::Array::a60250c8af2ca6f4"}},"pos":"test.rs:2:5: 2:6","rhs":{"kind":"Use","usevar":{"data":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Array::a60250c8af2ca6f4"}},"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::Array::a60250c8af2ca6f4"}]},"name":"test/bfa24f84::id_array","return_ty":"ty::Array::a60250c8af2ca6f4","spread_arg":null},{"abi":{"kind":"Rust"},"args":[{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Tuple::f54c7b3282e27392"}],"body":{"blocks":[{"block":{"data":[{"kind":"Assign","lhs":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::Tuple::f54c7b3282e27392"}},"pos":"test.rs:6:5: 6:6","rhs":{"kind":"Use","usevar":{"data":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::Tuple::f54c7b3282e27392"}},"kind":"Copy"}}}],"terminator":{"kind":"Return","pos":"test.rs:7:2: 7:2"}},"blockid":"bb0"}],"vars":[{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::Tuple::f54c7b3282e27392"}]},"name":"test/bfa24f84::id_tuple","return_ty":"ty::Tuple::f54c7b3282e27392","spread_arg":null},{"abi":{"kind":"Rust"},"args":[{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::u32"}],"body":{"blocks":[{"block":{"data":[{"kind":"Assign","lhs":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::u32"}},"pos":"test.rs:10:5: 10:6","rhs":{"kind":"Use","usevar":{"data":{"data":[],"var":{"is_zst":false,"mut":{"kind":"Not"},"name":"_1","ty":"ty::u32"}},"kind":"Copy"}}}],"terminator":{"kind":"Return","pos":"test.rs:11:2: 11:2"}},"blockid":"bb0"}],"vars":[{"is_zst":false,"mut":{"kind":"Mut"},"name":"_0","ty":"ty::u32"}]},"name":"test/bfa24f84::id_u32","return_ty":"ty::u32","spread_arg":null}],"adts":[],"statics":[],"vtables":[],"traits":[],"intrinsics":[{"inst":{"def_id":"test/bfa24f84::id_array","kind":"Item","substs":[]},"name":"test/bfa24f84::id_array"},{"inst":{"def_id":"test/bfa24f84::id_tuple","kind":"Item","substs":[]},"name":"test/bfa24f84::id_tuple"},{"inst":{"def_id":"test/bfa24f84::id_u32","kind":"Item","substs":[]},"name":"test/bfa24f84::id_u32"}],"tys":[{"name":"ty::u32","ty":{"kind":"Uint","uintkind":{"kind":"U32"}}},{"name":"ty::usize","ty":{"kind":"Uint","uintkind":{"kind":"Usize"}}},{"name":"ty::Array::a60250c8af2ca6f4","ty":{"kind":"Array","size":{"rendered":{"kind":"usize","size":8,"val":"5"},"ty":"ty::usize"},"ty":"ty::u32"}},{"name":"ty::Tuple::f54c7b3282e27392","ty":{"kind":"Tuple","tys":["ty::u32","ty::u32"]}}],"roots":["test/bfa24f84::id_array","test/bfa24f84::id_tuple","test/bfa24f84::id_u32"]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub fn id_array(x: [u32; 5]) -> [u32; 5] {
2+
x
3+
}
4+
5+
pub fn id_tuple(x: (u32, u32)) -> (u32, u32) {
6+
x
7+
}
8+
9+
pub fn id_u32(x: u32) -> u32 {
10+
x
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
enable_experimental;
2+
3+
let id_spec cty = do {
4+
x <- mir_fresh_cryptol_var "x" cty;
5+
6+
mir_execute_func [mir_term x];
7+
8+
mir_return (mir_term x);
9+
};
10+
11+
m <- mir_load_module "test.linked-mir.json";
12+
13+
mir_verify m "test::id_array" [] false (id_spec {| [5][32] |}) z3;
14+
mir_verify m "test::id_tuple" [] false (id_spec {| ([32], [32]) |}) z3;
15+
mir_verify m "test::id_u32" [] false (id_spec {| [32] |}) z3;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
set -e
2+
3+
$SAW test.saw

src/SAWScript/Crucible/MIR/Builtins.hs

+17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module SAWScript.Crucible.MIR.Builtins
1515
, mir_assert
1616
, mir_execute_func
1717
, mir_find_adt
18+
, mir_fresh_cryptol_var
1819
, mir_fresh_expanded_value
1920
, mir_fresh_var
2021
, mir_load_module
@@ -221,6 +222,22 @@ mir_find_adt rm origName substs = do
221222
origDid <- findDefId crateDisambigs (Text.pack origName)
222223
findAdt col origDid (Mir.Substs substs)
223224

225+
-- | Generate a fresh term of the given Cryptol type. The name will be used when
226+
-- pretty-printing the variable in debug output.
227+
mir_fresh_cryptol_var ::
228+
Text ->
229+
Cryptol.Schema ->
230+
MIRSetupM TypedTerm
231+
mir_fresh_cryptol_var name s =
232+
MIRSetupM $
233+
do loc <- getW4Position "mir_fresh_var"
234+
case s of
235+
Cryptol.Forall [] [] ty ->
236+
do sc <- lift $ lift getSharedContext
237+
Setup.freshVariable sc name ty
238+
_ ->
239+
throwCrucibleSetup loc $ "Unsupported polymorphic Cryptol type schema: " ++ show s
240+
224241
-- | Create a MIR value entirely populated with fresh symbolic variables.
225242
-- For compound types such as structs and arrays, this will explicitly set
226243
-- each field or element to contain a fresh symbolic variable. The Text

src/SAWScript/Interpreter.hs

+9
Original file line numberDiff line numberDiff line change
@@ -3938,6 +3938,15 @@ primitives = Map.fromList
39383938
, "be found in the MIRModule, this will raise an error."
39393939
]
39403940

3941+
, prim "mir_fresh_cryptol_var" "String -> Type -> MIRSetup Term"
3942+
(pureVal mir_fresh_cryptol_var)
3943+
Experimental
3944+
[ "Create a fresh symbolic variable of the given Cryptol type for use"
3945+
, "within a MIR specification. The given name is used only for"
3946+
, "pretty-printing. Unlike 'mir_fresh_var', this can be used when"
3947+
, "there isn't an appropriate MIR type, such as the Cryptol Array type."
3948+
]
3949+
39413950
, prim "mir_fresh_expanded_value" "String -> MIRType -> MIRSetup MIRValue"
39423951
(pureVal mir_fresh_expanded_value)
39433952
Experimental

0 commit comments

Comments
 (0)