Skip to content

Commit 783c338

Browse files
committed
Add new Loc expression to model abstract locations (#382)
1 parent 124e2d3 commit 783c338

File tree

9 files changed

+59
-7
lines changed

9 files changed

+59
-7
lines changed

src/smtml/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
interpret
2929
interpret_intf
3030
lexer
31+
loc
3132
log
3233
logic
3334
mappings

src/smtml/expr.ml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and expr =
1010
{ base : Bitvector.t
1111
; offset : t
1212
}
13+
| Loc of Loc.t
1314
| Symbol of Symbol.t
1415
| List of t list
1516
| App of Symbol.t * t list
@@ -33,6 +34,7 @@ module Expr = struct
3334
let equal (e1 : expr) (e2 : expr) : bool =
3435
match (e1, e2) with
3536
| Val v1, Val v2 -> Value.equal v1 v2
37+
| Loc a, Loc b -> Loc.compare a b = 0
3638
| Ptr { base = b1; offset = o1 }, Ptr { base = b2; offset = o2 } ->
3739
Bitvector.equal b1 b2 && phys_equal o1 o2
3840
| Symbol s1, Symbol s2 -> Symbol.equal s1 s2
@@ -58,8 +60,9 @@ module Expr = struct
5860
| Concat (e1, e3), Concat (e2, e4) -> phys_equal e1 e2 && phys_equal e3 e4
5961
| Binder (binder1, vars1, e1), Binder (binder2, vars2, e2) ->
6062
Binder.equal binder1 binder2 && list_eq vars1 vars2 && phys_equal e1 e2
61-
| ( ( Val _ | Ptr _ | Symbol _ | List _ | App _ | Unop _ | Binop _ | Triop _
62-
| Relop _ | Cvtop _ | Naryop _ | Extract _ | Concat _ | Binder _ )
63+
| ( ( Val _ | Ptr _ | Loc _ | Symbol _ | List _ | App _ | Unop _ | Binop _
64+
| Triop _ | Relop _ | Cvtop _ | Naryop _ | Extract _ | Concat _
65+
| Binder _ )
6366
, _ ) ->
6467
false
6568

@@ -68,6 +71,7 @@ module Expr = struct
6871
match e with
6972
| Val v -> h v
7073
| Ptr { base; offset } -> h (base, offset.tag)
74+
| Loc l -> h l
7175
| Symbol s -> h s
7276
| List v -> h v
7377
| App (x, es) -> h (x, es)
@@ -107,6 +111,7 @@ let rec ty (hte : t) : Ty.t =
107111
match view hte with
108112
| Val x -> Value.type_of x
109113
| Ptr _ -> Ty_bitv 32
114+
| Loc _ -> Ty_app
110115
| Symbol x -> Symbol.type_of x
111116
| List _ -> Ty_list
112117
| App (sym, _) -> begin match sym.ty with Ty_none -> Ty_app | ty -> ty end
@@ -135,6 +140,7 @@ let rec is_symbolic (v : t) : bool =
135140
match view v with
136141
| Val _ -> false
137142
| Symbol _ -> true
143+
| Loc _ -> false
138144
| Ptr { offset; _ } -> is_symbolic offset
139145
| List vs -> List.exists is_symbolic vs
140146
| App (_, vs) -> List.exists is_symbolic vs
@@ -153,7 +159,7 @@ let get_symbols (hte : t list) =
153159
let tbl = Hashtbl.create 64 in
154160
let rec symbols (hte : t) =
155161
match view hte with
156-
| Val _ -> ()
162+
| Val _ | Loc _ -> ()
157163
| Ptr { offset; _ } -> symbols offset
158164
| Symbol s -> Hashtbl.replace tbl s ()
159165
| List es -> List.iter symbols es
@@ -186,6 +192,7 @@ let rec pp fmt (hte : t) =
186192
match view hte with
187193
| Val v -> Value.pp fmt v
188194
| Ptr { base; offset } -> Fmt.pf fmt "(Ptr %a %a)" Bitvector.pp base pp offset
195+
| Loc l -> Fmt.pf fmt "(loc %a)" Loc.pp l
189196
| Symbol s -> Fmt.pf fmt "@[<hov 1>%a@]" Symbol.pp s
190197
| List v -> Fmt.pf fmt "@[<hov 1>[%a]@]" (Fmt.list ~sep:Fmt.comma pp) v
191198
| App (s, v) ->
@@ -247,7 +254,7 @@ module Set = struct
247254
let tbl = Hashtbl.create 64 in
248255
let rec symbols hte =
249256
match view hte with
250-
| Val _ -> ()
257+
| Val _ | Loc _ -> ()
251258
| Ptr { offset; _ } -> symbols offset
252259
| Symbol s -> Hashtbl.replace tbl s ()
253260
| List es -> List.iter symbols es
@@ -281,6 +288,8 @@ let value (v : Value.t) : t = make (Val v) [@@inline]
281288

282289
let ptr base offset = make (Ptr { base = Bitvector.of_int32 base; offset })
283290

291+
let loc l = make (Loc l)
292+
284293
let list l = make (List l)
285294

286295
let app symbol args = make (App (symbol, args))
@@ -574,7 +583,7 @@ let rec concat (msb : t) (lsb : t) : t =
574583

575584
let rec simplify_expr ?(in_relop = false) (hte : t) : t =
576585
match view hte with
577-
| Val _ | Symbol _ -> hte
586+
| Val _ | Symbol _ | Loc _ -> hte
578587
| Ptr { base; offset } ->
579588
let offset = simplify_expr ~in_relop offset in
580589
if not in_relop then make (Ptr { base; offset })

src/smtml/expr.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and expr = private
2020
{ base : Bitvector.t (** Base address. *)
2121
; offset : t (** Offset from base. *)
2222
}
23+
| Loc of Loc.t (** Abstract location *)
2324
| Symbol of Symbol.t (** A symbolic variable. *)
2425
| List of t list (** A list of expressions. *)
2526
| App of Symbol.t * t list (** Function application. *)
@@ -90,6 +91,9 @@ val value : Value.t -> t
9091
address and offset. *)
9192
val ptr : int32 -> t -> t
9293

94+
(** [loc l] constructs an abstract location *)
95+
val loc : Loc.t -> t
96+
9397
(** [list l] constructs a list expression with the given list of expressions *)
9498
val list : t list -> t
9599

src/smtml/expr_raw.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ include (
99
{ base : Bitvector.t
1010
; offset : t
1111
}
12+
| Loc of Loc.t
1213
| Symbol of Symbol.t
1314
| List of t list
1415
| App of Symbol.t * t list
@@ -50,6 +51,8 @@ include (
5051

5152
val ptr : int32 -> t -> t
5253

54+
val loc : Loc.t -> t
55+
5356
val list : t list -> t
5457

5558
val symbol : Symbol.t -> t

src/smtml/expr_raw.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and expr = private
1616
{ base : Bitvector.t (** Base address. *)
1717
; offset : t (** Offset from base. *)
1818
}
19+
| Loc of Loc.t (** Abstract location *)
1920
| Symbol of Symbol.t (** A symbolic variable. *)
2021
| List of t list (** A list of expressions. *)
2122
| App of Symbol.t * t list (** Function application. *)
@@ -86,6 +87,9 @@ val value : Value.t -> t
8687
address and offset. *)
8788
val ptr : int32 -> t -> t
8889

90+
(** [loc l] constructs an abstract location *)
91+
val loc : Loc.t -> t
92+
8993
(** [list l] constructs a list expression with the given list of expressions *)
9094
val list : t list -> t
9195

src/smtml/loc.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(* SPDX-License-Identifier: MIT *)
2+
(* Copyright (C) 2023-2024 formalsec *)
3+
(* Written by the Smtml programmers *)
4+
5+
type t = int
6+
7+
let fresh =
8+
let next = ref 0 in
9+
fun () ->
10+
let id = !next in
11+
incr next;
12+
id
13+
14+
let compare = Int.compare
15+
16+
let equal a b = compare a b = 0
17+
18+
let hash a = a
19+
20+
let pp = Fmt.int

src/smtml/loc.mli

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type t
2+
3+
val fresh : unit -> t
4+
5+
val compare : t -> t -> int
6+
7+
val equal : t -> t -> bool
8+
9+
val hash : t -> int
10+
11+
val pp : t Fmt.t

src/smtml/mappings.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ module Make (M_with_make : M_with_make) : S_with_fresh = struct
787787
let ctx, vars = encode_exprs ctx vars in
788788
let ctx, body = encode_expr ctx body in
789789
(ctx, M.exists vars body)
790-
| List _ | Binder _ ->
790+
| List _ | Binder _ | Loc _ ->
791791
Fmt.failwith "Cannot encode expression: %a" Expr.pp hte
792792

793793
and encode_exprs ctx (es : Expr.t list) : symbol_ctx * M.term list =

src/smtml/rewrite.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ let rewrite_ty unknown_ty tys =
5858
let rec rewrite_expr (type_map, expr_map) hte =
5959
debug "rewrite_expr: %a@." (fun k -> k Expr.pp hte);
6060
match Expr.view hte with
61-
| Val _ -> hte
61+
| Val _ | Loc _ -> hte
6262
| Ptr { base; offset } ->
6363
let base = Bitvector.to_int32 base in
6464
Expr.ptr base (rewrite_expr (type_map, expr_map) offset)

0 commit comments

Comments
 (0)