-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fea965
commit 8db9645
Showing
25 changed files
with
482 additions
and
8 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,91 @@ | ||
# Symbolic interpreter | ||
|
||
## Basic example | ||
|
||
First, you need to perform a symbolic run and to store the output model in a file. Given the following `mini.wat` file containing symbols: | ||
|
||
<!-- $MDX file=mini.wat --> | ||
```wat | ||
(module | ||
(import "symbolic" "i32_symbol" (func $gen_i32 (result i32))) | ||
(func $start (local $x i32) | ||
(local.set $x (call $gen_i32)) | ||
(if (i32.lt_s (i32.const 5) (local.get $x)) (then | ||
unreachable | ||
))) | ||
(start $start) | ||
) | ||
``` | ||
|
||
You can get a model like this: | ||
|
||
```sh | ||
$ owi sym ./mini.wat > mini.model | ||
Reached problem! | ||
[13] | ||
``` | ||
|
||
Then you can replay the module execution with the values in the model like this: | ||
|
||
```sh | ||
$ owi replay --replay-file mini.model mini.wat | ||
invalid model: can not find the directive `model` while parsing the scfg config | ||
[78] | ||
``` | ||
## Man page | ||
```sh | ||
$ owi replay --help=plain | ||
NAME | ||
owi-replay - Replay a module containing symbols with concrete values | ||
in a replay file containing a model | ||
|
||
SYNOPSIS | ||
owi replay [OPTION]… ARG | ||
|
||
OPTIONS | ||
-d, --debug | ||
debug mode | ||
|
||
--optimize | ||
optimize mode | ||
|
||
-p, --profiling | ||
profiling mode | ||
|
||
--replay-file=VAL (required) | ||
Which replay file to use | ||
|
||
-u, --unsafe | ||
skip typechecking pass | ||
|
||
COMMON OPTIONS | ||
--help[=FMT] (default=auto) | ||
Show this help in format FMT. The value FMT must be one of auto, | ||
pager, groff or plain. With auto, the format is pager or plain | ||
whenever the TERM env var is dumb or undefined. | ||
|
||
--version | ||
Show version information. | ||
|
||
EXIT STATUS | ||
owi replay exits with: | ||
|
||
0 on success. | ||
|
||
123 on indiscriminate errors reported on standard error. | ||
|
||
124 on command line parsing errors. | ||
|
||
125 on unexpected internal errors (bugs). | ||
|
||
BUGS | ||
Email them to <[email protected]>. | ||
|
||
SEE ALSO | ||
owi(1) | ||
|
||
``` |
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,4 @@ | ||
(mdx | ||
(libraries owi) | ||
(deps %{bin:owi} mini.wat) | ||
(files README.md)) |
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,11 @@ | ||
(module | ||
(import "symbolic" "i32_symbol" (func $gen_i32 (result i32))) | ||
|
||
(func $start (local $x i32) | ||
(local.set $x (call $gen_i32)) | ||
(if (i32.lt_s (i32.const 5) (local.get $x)) (then | ||
unreachable | ||
))) | ||
|
||
(start $start) | ||
) |
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,116 @@ | ||
(* SPDX-License-Identifier: AGPL-3.0-or-later *) | ||
(* Copyright © 2021-2024 OCamlPro *) | ||
(* Written by the Owi programmers *) | ||
|
||
open Syntax | ||
|
||
let run_file ~unsafe ~optimize filename model = | ||
let next = | ||
let next = ref ~-1 in | ||
fun () -> | ||
incr next; | ||
!next | ||
in | ||
|
||
let assume_i32 _ = () in | ||
|
||
let assume_positive_i32 _ = () in | ||
|
||
let assert_i32 n = assert (not @@ Prelude.Int32.equal n 0l) in | ||
|
||
let symbol_i32 () = | ||
match model.(next ()) with Concrete_value.I32 n -> n | _ -> assert false | ||
in | ||
|
||
let symbol_char = symbol_i32 in | ||
|
||
let symbol_i8 = symbol_i32 in | ||
|
||
let symbol_i64 () = | ||
match model.(next ()) with Concrete_value.I64 n -> n | _ -> assert false | ||
in | ||
|
||
let symbol_f32 () = | ||
match model.(next ()) with Concrete_value.F32 n -> n | _ -> assert false | ||
in | ||
|
||
let symbol_f64 () = | ||
match model.(next ()) with Concrete_value.F64 n -> n | _ -> assert false | ||
in | ||
|
||
let replay_extern_module = | ||
let functions = | ||
[ ( "i8_symbol" | ||
, Concrete_value.Func.Extern_func (Func (UArg Res, R1 I32), symbol_i8) | ||
) | ||
; ( "char_symbol" | ||
, Concrete_value.Func.Extern_func (Func (UArg Res, R1 I32), symbol_char) | ||
) | ||
; ( "i32_symbol" | ||
, Concrete_value.Func.Extern_func (Func (UArg Res, R1 I32), symbol_i32) | ||
) | ||
; ( "i64_symbol" | ||
, Concrete_value.Func.Extern_func (Func (UArg Res, R1 I64), symbol_i64) | ||
) | ||
; ( "f32_symbol" | ||
, Concrete_value.Func.Extern_func (Func (UArg Res, R1 F32), symbol_f32) | ||
) | ||
; ( "f64_symbol" | ||
, Concrete_value.Func.Extern_func (Func (UArg Res, R1 F64), symbol_f64) | ||
) | ||
; ( "assume" | ||
, Concrete_value.Func.Extern_func (Func (Arg (I32, Res), R0), assume_i32) | ||
) | ||
; ( "assume_positive_i32" | ||
, Concrete_value.Func.Extern_func | ||
(Func (Arg (I32, Res), R0), assume_positive_i32) ) | ||
; ( "assert" | ||
, Concrete_value.Func.Extern_func (Func (Arg (I32, Res), R0), assert_i32) | ||
) | ||
] | ||
in | ||
{ Link.functions } | ||
in | ||
|
||
let link_state = | ||
Link.extern_module Link.empty_state ~name:"symbolic" replay_extern_module | ||
in | ||
|
||
let* m = | ||
Compile.File.until_binary_validate ~rac:false ~srac:false ~unsafe filename | ||
in | ||
let* m = Cmd_utils.add_main_as_start m in | ||
let* m, link_state = | ||
Compile.Binary.until_link ~unsafe link_state ~optimize ~name:None m | ||
in | ||
let c = Interpret.Concrete.modul link_state.envs m in | ||
c | ||
|
||
let cmd profiling debug unsafe optimize replay_file file = | ||
if profiling then Log.profiling_on := true; | ||
if debug then Log.debug_on := true; | ||
let* model = | ||
match Smtml.Model.Parse.Scfg.from_file replay_file with | ||
| Error msg -> Error (`Invalid_model msg) | ||
| Ok model -> | ||
let+ model = | ||
list_map | ||
(fun (_sym, v) -> | ||
match v with | ||
| Smtml.Value.False -> Ok (Concrete_value.I32 0l) | ||
| True -> Ok (Concrete_value.I32 1l) | ||
| Num (I8 n) -> Ok (Concrete_value.I32 (Int32.of_int n)) | ||
| Num (I32 n) -> Ok (Concrete_value.I32 n) | ||
| Num (I64 n) -> Ok (Concrete_value.I64 n) | ||
| Num (F32 n) -> Ok (Concrete_value.F32 (Float32.of_bits n)) | ||
| Num (F64 n) -> Ok (Concrete_value.F64 (Float64.of_bits n)) | ||
| Unit | Int _ | Real _ | Str _ | List _ | App _ | Nothing -> | ||
Error | ||
(`Invalid_model | ||
(Fmt.str "unexpected value type: %a" Smtml.Value.pp v) ) ) | ||
(Smtml.Model.get_bindings model) | ||
in | ||
Array.of_list model | ||
in | ||
let+ () = run_file ~unsafe ~optimize file model in | ||
Fmt.pr "All OK@." |
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,5 @@ | ||
(* SPDX-License-Identifier: AGPL-3.0-or-later *) | ||
(* Copyright © 2021-2024 OCamlPro *) | ||
(* Written by the Owi programmers *) | ||
|
||
val cmd : bool -> bool -> bool -> bool -> Fpath.t -> Fpath.t -> unit Result.t |
Oops, something went wrong.