-
Notifications
You must be signed in to change notification settings - Fork 17
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
Showing
8 changed files
with
156 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
open Smtml | ||
|
||
(* TODO: These functions should be in smtml *) | ||
|
||
(* TODO: don't rebuild so many values it generates unecessary hc lookups *) | ||
let merge_extracts (e1, h, m1) (e2, m2, l) = | ||
let ty = Expr.ty e1 in | ||
if m1 = m2 && Expr.equal e1 e2 then | ||
if h - l = Ty.size ty then e1 else Expr.make (Extract (e1, h, l)) | ||
else | ||
Expr.(make (Concat (make (Extract (e1, h, m1)), make (Extract (e2, m2, l))))) | ||
|
||
let concat ~msb ~lsb offset = | ||
assert (offset > 0 && offset <= 8); | ||
match (Expr.view msb, Expr.view lsb) with | ||
| Val (Num (I8 i1)), Val (Num (I8 i2)) -> | ||
Symbolic_value.const_i32 Int32.(logor (shl (of_int i1) 8l) (of_int i2)) | ||
| Val (Num (I8 i1)), Val (Num (I32 i2)) -> | ||
let offset = offset * 8 in | ||
if offset < 32 then | ||
Symbolic_value.const_i32 Int32.(logor (shl (of_int i1) (of_int offset)) i2) | ||
else | ||
let i1' = Int64.of_int i1 in | ||
let i2' = Int64.of_int32 i2 in | ||
Symbolic_value.const_i64 Int64.(logor (shl i1' (of_int offset)) i2') | ||
| Val (Num (I8 i1)), Val (Num (I64 i2)) -> | ||
let offset = Int64.of_int (offset * 8) in | ||
Symbolic_value.const_i64 Int64.(logor (shl (of_int i1) offset) i2) | ||
| Extract (e1, h, m1), Extract (e2, m2, l) -> | ||
merge_extracts (e1, h, m1) (e2, m2, l) | ||
| Extract (e1, h, m1), Concat ({ node = Extract (e2, m2, l); _ }, e3) -> | ||
Expr.(make (Concat (merge_extracts (e1, h, m1) (e2, m2, l), e3))) | ||
| _ -> Expr.make (Concat (msb, lsb)) | ||
|
||
let extract v pos = | ||
match Expr.view v with | ||
| Val (Num (I8 _)) -> v | ||
| Val (Num (I32 i)) -> | ||
let i' = Int32.(to_int @@ logand 0xffl @@ shr_s i @@ of_int (pos * 8)) in | ||
Expr.value (Num (I8 i')) | ||
| Val (Num (I64 i)) -> | ||
let i' = Int64.(to_int @@ logand 0xffL @@ shr_s i @@ of_int (pos * 8)) in | ||
Expr.value (Num (I8 i')) | ||
| Cvtop | ||
(_, Zero_extend 24, ({ node = Symbol { ty = Ty_bitv 8; _ }; _ } as sym)) | ||
| Cvtop | ||
(_, Sign_extend 24, ({ node = Symbol { ty = Ty_bitv 8; _ }; _ } as sym)) | ||
-> | ||
sym | ||
| _ -> Expr.make (Extract (v, pos + 1, pos)) |
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,93 @@ | ||
(* SPDX-License-Identifier: AGPL-3.0-or-later *) | ||
(* Copyright © 2021-2024 OCamlPro *) | ||
(* Written by the Owi programmers *) | ||
|
||
[@@@warning "-69"] | ||
|
||
module Memsight_backend = struct | ||
open Smtml | ||
open Symbolic_choice_without_memory | ||
include Symbolic_memory_base | ||
|
||
type address = Symbolic_value.int32 | ||
|
||
type record = | ||
{ address : address | ||
; value : Symbolic_value.int32 | ||
; condition : Symbolic_value.vbool | ||
; time : int | ||
} | ||
|
||
type t = | ||
{ mutable records : record list | ||
; mutable time : int | ||
} | ||
|
||
let make () = { records = []; time = 0 } | ||
|
||
let clone { records; time } = { records; time } | ||
|
||
let address a = return a | ||
|
||
let address_i32 i = Symbolic_value.const_i32 i | ||
|
||
let load_byte mem addr = | ||
let open Symbolic_value in | ||
let v = Expr.value (Num (I8 0)) in | ||
List.fold_right | ||
(fun r acc -> | ||
Bool.select_expr | ||
(Bool.and_ (I32.eq addr r.address) r.condition) | ||
~if_true:r.value ~if_false:acc ) | ||
mem.records v | ||
|
||
let loadn mem addr n = | ||
let open Symbolic_value in | ||
let rec loop i acc = | ||
if i = n then acc | ||
else | ||
let addr' = I32.add addr (const_i32 (Int32.of_int i)) in | ||
let byte = load_byte mem addr' in | ||
loop (i + 1) (concat i ~msb:byte ~lsb:acc) | ||
in | ||
loop 1 (load_byte mem addr) | ||
|
||
let storen mem addr v n = | ||
let open Symbolic_value in | ||
mem.time <- succ mem.time; | ||
for i = n - 1 downto 0 do | ||
let record = | ||
{ address = I32.add addr (const_i32 (Int32.of_int i)) | ||
; value = extract v i | ||
; condition = Bool.const true | ||
; time = mem.time | ||
} | ||
in | ||
mem.records <- record :: mem.records | ||
done | ||
|
||
let validate_address _m a = return (Ok a) | ||
|
||
let free _ _ = | ||
Fmt.epr "TODO: Symbolic_memory.free@."; | ||
return () | ||
|
||
let realloc _m ~ptr ~size:_ = | ||
(* Fmt.epr "TODO: Symbolic_memory.replace_size@."; *) | ||
let+ base = select_i32 ptr in | ||
Expr.ptr base (Symbolic_value.const_i32 0l) | ||
|
||
let pp_record fmt { address; value; condition; time } = | ||
Fmt.pf fmt "%a, %a, %a, %d" Expr.pp address Expr.pp value Expr.pp condition | ||
time | ||
|
||
let pp fmt { time; records; _ } = | ||
Fmt.pf fmt "Time: %d@;" time; | ||
Fmt.pf fmt "Records:@; @[<v 2>%a@]" | ||
(Fmt.list (Fmt.parens pp_record)) | ||
records | ||
end | ||
|
||
module Memsight_backend' : Symbolic_memory_intf.M = Memsight_backend | ||
|
||
include Symbolic_memory_make.Make (Memsight_backend) |
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