Skip to content

Commit f5c9cb2

Browse files
committed
Add basic memory tests and introspection external functions
1 parent 6c8a21e commit f5c9cb2

17 files changed

+219
-2
lines changed

src/cmd/cmd_sym.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ let link_state =
1717
Link.extern_module' Link.empty_state ~name:"symbolic" ~func_typ
1818
Symbolic_wasm_ffi.symbolic_extern_module
1919
in
20-
Link.extern_module' link_state ~name:"summaries" ~func_typ
21-
Symbolic_wasm_ffi.summaries_extern_module )
20+
let link_state =
21+
Link.extern_module' link_state ~name:"summaries" ~func_typ
22+
Symbolic_wasm_ffi.summaries_extern_module
23+
in
24+
Link.extern_module' link_state ~name:"introspection" ~func_typ
25+
Symbolic_wasm_ffi.introspection_extern_module )
2226

2327
let run_file ~unsafe ~optimize pc filename =
2428
let*/ m = Compile.File.until_typecheck ~unsafe filename in

src/concolic/concolic_wasm_ffi.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,7 @@ let summaries_extern_module =
169169
]
170170
in
171171
{ Link.functions }
172+
173+
let introspection_extern_module =
174+
let functions = [] in
175+
{ Link.functions }

src/intf/symbolic_memory_intf.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module type M = sig
2727
-> Smtml.Expr.t Symbolic_choice_without_memory.t
2828

2929
val free : t -> Smtml.Expr.t -> unit Symbolic_choice_without_memory.t
30+
31+
val pp : Fmt.formatter -> t -> unit
3032
end
3133

3234
module type S = sig
@@ -118,6 +120,8 @@ module type S = sig
118120

119121
val get_limit_max : t -> Smtml.Expr.t option
120122

123+
val pp : Fmt.formatter -> t -> unit
124+
121125
module ITbl : sig
122126
type 'a t
123127

src/intf/wasm_ffi_intf.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ module type S = sig
4848
val symbolic_extern_module : extern_func Link.extern_module
4949

5050
val summaries_extern_module : extern_func Link.extern_module
51+
52+
val introspection_extern_module : extern_func Link.extern_module
5153
end

src/symbolic/symbolic_memory.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ let free m base =
199199

200200
let realloc m base size = Hashtbl.replace m.chunks base size
201201

202+
let pp fmt m =
203+
let pp_parent v =
204+
Fmt.option
205+
~none:(fun fmt () -> Fmt.string fmt "None")
206+
(fun fmt _v -> Fmt.string fmt "Some")
207+
v
208+
in
209+
let pp_v fmt (a, b) = Fmt.pf fmt "0x%08ld %a" a Expr.pp b in
210+
Fmt.pf fmt "size: %a@;" Expr.pp m.size;
211+
Fmt.pf fmt "parent: %a@;" pp_parent m.parent;
212+
Fmt.pf fmt "data:@; @[<v>%a@]" (Fmt.hashtbl pp_v) m.data
213+
202214
module ITbl = Hashtbl.Make (struct
203215
include Int
204216

src/symbolic/symbolic_memory.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ val size_in_pages : t -> Smtml.Expr.t
6060

6161
val get_limit_max : t -> Smtml.Expr.t option
6262

63+
val pp : Fmt.formatter -> t -> unit
64+
6365
module ITbl : sig
6466
type 'a t
6567

src/symbolic/symbolic_memory_concretizing.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ module Backend = struct
145145
let+ base = address ptr in
146146
Hashtbl.replace m.chunks base size;
147147
Expr.ptr base (Symbolic_value.const_i32 0l)
148+
149+
let rec pp fmt m =
150+
let pp_parent =
151+
Fmt.option ~none:(fun fmt () -> Fmt.string fmt "None (root mem)") pp
152+
in
153+
let pp_v fmt (a, b) = Fmt.pf fmt "0x%08lx %a" a Expr.pp b in
154+
Fmt.pf fmt "@[<v>parent:@;@[<v 1> %a@]@;data:@;@[<v 1> %a@]@]" pp_parent
155+
m.parent (Fmt.hashtbl pp_v) m.data
148156
end
149157

150158
include Symbolic_memory_make.Make (Backend)

src/symbolic/symbolic_memory_make.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ module Make (Backend : M) = struct
134134

135135
let realloc m ~ptr ~size = Backend.realloc m.data ~ptr ~size
136136

137+
let pp fmt m =
138+
Fmt.pf fmt "@[<v 1>Memory:@;size: %a@;backend:@;@[<v 1> %a@]@]"
139+
Smtml.Expr.pp m.size Backend.pp m.data
140+
137141
(* TODO: Move this into a separate module? *)
138142
module ITbl = Hashtbl.Make (struct
139143
include Int

src/symbolic/symbolic_wasm_ffi.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,23 @@ let summaries_extern_module =
9898
]
9999
in
100100
{ Link.functions }
101+
102+
let introspection_extern_module =
103+
let dump_memory m : unit Choice.t =
104+
Fmt.epr "%a@." Memory.pp m;
105+
Choice.return ()
106+
in
107+
108+
let print_i32 i32 : Symbolic_value.int32 Choice.t =
109+
Fmt.pr "Print: %a@." Smtml.Expr.pp i32;
110+
Choice.return i32
111+
in
112+
let functions =
113+
[ ( "dump_memory"
114+
, Symbolic.P.Extern_func.Extern_func (Func (Mem Res, R0), dump_memory) )
115+
; ( "print_i32"
116+
, Symbolic.P.Extern_func.Extern_func
117+
(Func (Arg (I32, Res), R1 I32), print_i32) )
118+
]
119+
in
120+
{ Link.functions }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;; Reuse memory address chunk with different size
2+
(module
3+
(import "summaries" "alloc" (func $malloc (param i32 i32) (result i32)))
4+
(import "summaries" "dealloc" (func $free (param i32)))
5+
(import "symbolic" "assert" (func $assert (param i32)))
6+
(memory $m 1)
7+
(global $__heap_base (mut i32) (i32.const 0x00000000))
8+
(func $start (local i32)
9+
global.get $__heap_base
10+
i32.const 0x00000004
11+
call $malloc
12+
local.tee 0
13+
i32.const 0x0000002a
14+
i32.store
15+
local.get 0
16+
i32.load
17+
i32.const 0x0000002a
18+
i32.eq
19+
call $assert
20+
local.get 0
21+
call $free
22+
global.get $__heap_base
23+
i32.const 0x00000008
24+
call $malloc
25+
local.tee 0
26+
i32.const 0x00000054
27+
i32.store
28+
local.get 0
29+
i32.load
30+
i32.const 0x00000054
31+
i32.eq
32+
call $assert
33+
local.get 0
34+
call $free)
35+
(start $start))

0 commit comments

Comments
 (0)