@@ -14,20 +14,31 @@ module Def = Bap_primus_lisp_def
14
14
module Type = Bap_primus_lisp_type
15
15
module Key = Bap_primus_lisp_program. Items
16
16
17
- module Meta = struct
18
- module State = struct
19
- type t = {
20
- binds : unit Theory.Value .t Map .M (Theory.Var .Top ).t;
21
- arith : (module Bitvec .S );
22
- scope : unit Theory .var list Map .M (Theory.Var .Top ).t;
23
- }
24
- end
25
- include Monad.State. T1 (State )(KB )
26
- include Monad.State. Make (State )(KB )
17
+ open KB.Syntax
18
+ open KB.Let
19
+
20
+ module State = struct
21
+ type t = {
22
+ binds : unit Theory.Value .t Map .M (Theory.Var .Top ).t;
23
+ arith : (module Bitvec .S );
24
+ scope : unit Theory .var list Map .M (Theory.Var .Top ).t;
25
+ }
26
+
27
+ let empty = {
28
+ binds = Map. empty (module Theory.Var. Top );
29
+ arith = (module Bitvec. M32 );
30
+ scope = Map. empty (module Theory.Var. Top );
31
+ }
32
+
33
+ let var = KB.Context. declare ~package: " bap" " lisp-interpter-state"
34
+ !! empty
35
+
36
+
37
+ let get = KB.Context. get var
38
+ let set = KB.Context. set var
39
+ let update = KB.Context. update var
27
40
end
28
41
29
- open Meta.Syntax
30
- open Meta.Let
31
42
32
43
type value = unit Theory.Value .t
33
44
type effect = unit Theory.Effect .t
@@ -48,6 +59,8 @@ let program =
48
59
~equal: Program. equal
49
60
~join: (fun x y -> Ok (Program. merge x y))
50
61
62
+
63
+
51
64
type program = {
52
65
prog : Program .t ;
53
66
places : unit Theory .var Map .M (KB .Name ).t;
@@ -60,14 +73,12 @@ let typed = KB.Class.property Theory.Source.cls "typed-program"
60
73
~equal: (fun x y ->
61
74
Program. equal x.prog y.prog)
62
75
63
- let fail s = Meta. lift (KB. fail s)
64
-
65
76
let unresolved name problem =
66
77
let msg =
67
78
Format. asprintf " Failed to find a definition for %a. %a"
68
79
KB.Name. pp name
69
80
Resolve. pp_resolution problem in
70
- fail (Unresolved_definition msg)
81
+ KB. fail (Unresolved_definition msg)
71
82
72
83
let resolve prog item name =
73
84
match Resolve. semantics prog item name () with
@@ -203,9 +214,8 @@ let sym str =
203
214
let v = update_value empty @@ fun v ->
204
215
KB.Value. put symbol v (Some str) in
205
216
match str with
206
- | "nil" -> Meta . return@@ set_static v Bitvec. zero
217
+ | "nil" -> KB . return@@ set_static v Bitvec. zero
207
218
| name ->
208
- Meta. lift @@
209
219
intern name >> |
210
220
set_static v
211
221
@@ -214,11 +224,10 @@ let static x =
214
224
KB.Value. get static_slot (res x)
215
225
216
226
let reify_sym x = match static x with
217
- | Some _ -> Meta . return x
227
+ | Some _ -> KB . return x
218
228
| None -> match KB.Value. get symbol (res x) with
219
- | None -> Meta . return x
229
+ | None -> KB . return x
220
230
| Some name ->
221
- Meta. lift @@
222
231
intern name >> |
223
232
set_static x
224
233
@@ -248,22 +257,21 @@ let lookup_parameter prog v =
248
257
let is_parameter prog v = Option. is_some (lookup_parameter prog v)
249
258
250
259
module Env = struct
251
- open Meta.State
252
260
253
261
let lookup v =
254
262
let v = Theory.Var. forget v in
255
- Meta. get () >> | fun {binds} ->
263
+ let + {binds} = State. get in
256
264
Map. find binds v
257
265
258
266
259
267
let set v x =
260
- Meta . update @@ fun s -> {
268
+ State . update @@ fun s -> {
261
269
s with binds = Map. set s.binds
262
270
(Theory.Var. forget v) x
263
271
}
264
272
265
273
let del v =
266
- Meta . update @@ fun s -> {
274
+ State . update @@ fun s -> {
267
275
s with binds = Map. remove s.binds (Theory.Var. forget v)
268
276
}
269
277
@@ -277,15 +285,15 @@ module Env = struct
277
285
Theory.Var. forget@@ Theory.Var. define (bits s) (KB.Name. to_string n)
278
286
279
287
let set_args ws bs =
280
- Meta. get () >> = fun s ->
288
+ let * s = State. get in
281
289
let binds,old =
282
290
List. fold bs ~init: (s.binds,[] ) ~f: (fun (s ,old ) (v ,x ) ->
283
291
let v = var ws v in
284
292
Map. set s v x,(v,Map. find s v) :: old) in
285
- Meta. put {s with binds} >> | fun () ->
293
+ State. set {s with binds} >> | fun () ->
286
294
List. rev old
287
295
288
- let del_args bs = Meta . update @@ fun s -> {
296
+ let del_args bs = State . update @@ fun s -> {
289
297
s with binds = List. fold bs ~init: s.binds ~f: (fun s (v ,x ) ->
290
298
match x with
291
299
| None -> Map. remove s v
@@ -298,51 +306,51 @@ module Scope = struct
298
306
299
307
let push orig =
300
308
let orig = forget orig in
301
- Meta. lift @@ Theory.Var. fresh (Theory.Var. sort orig) >> = fun v ->
302
- Meta . update (fun s -> {
309
+ Theory.Var. fresh (Theory.Var. sort orig) >> = fun v ->
310
+ State . update (fun s -> {
303
311
s with scope = Map. update s.scope orig ~f: (function
304
312
| None -> [v]
305
313
| Some vs -> v::vs)
306
314
}) >> | fun () ->
307
315
v
308
316
309
317
let lookup orig =
310
- let + {scope} = Meta . get () in
318
+ let + {scope} = State . get in
311
319
match Map. find scope orig with
312
320
| None | Some [] -> None
313
321
| Some (x :: _ ) -> Some x
314
322
315
323
let pop orig =
316
- Meta . update @@ fun s -> {
324
+ State . update @@ fun s -> {
317
325
s with scope = Map. change s.scope (forget orig) ~f: (function
318
326
| None | Some [] | Some [_] -> None
319
327
| Some (_ ::xs ) -> Some xs)
320
328
}
321
329
322
330
let clear =
323
- let * s = Meta . get () in
324
- let + () = Meta. put {
331
+ let * s = State . get in
332
+ let + () = State. set {
325
333
s with scope = Map. empty (module Theory.Var. Top )
326
334
} in
327
335
s.scope
328
336
329
- let restore scope = Meta . update @@ fun s -> {
337
+ let restore scope = State . update @@ fun s -> {
330
338
s with scope
331
339
}
332
340
333
341
end
334
342
335
343
module Prelude (CT : Theory.Core ) = struct
336
344
let null = KB.Object. null Theory.Program. cls
337
- let fresh = Meta. lift ( KB.Object. create Theory.Program. cls)
345
+ let fresh = KB.Object. create Theory.Program. cls
338
346
339
347
let rec seq = function
340
- | [] -> Meta. lift @@ CT. perform Theory.Effect.Sort. bot
348
+ | [] -> CT. perform Theory.Effect.Sort. bot
341
349
| [x] -> x
342
350
| x :: xs ->
343
351
let * xs = seq xs in
344
352
let * x = x in
345
- Meta. lift @@ CT. seq (KB. return x) (KB. return xs)
353
+ CT. seq (KB. return x) (KB. return xs)
346
354
347
355
let skip = CT. perform Theory.Effect.Sort. bot
348
356
let pass = CT. perform Theory.Effect.Sort. bot
@@ -355,10 +363,10 @@ module Prelude(CT : Theory.Core) = struct
355
363
let s = bits m in
356
364
let m = Bitvec. modulus m in
357
365
let x = Bitvec. (bigint x mod m) in
358
- pure @@ Meta. lift ( CT. int s x) >> | fun r ->
366
+ pure @@ CT. int s x >> | fun r ->
359
367
set_static r x
360
368
361
- let (:=) v x = Meta. lift @@ CT. set v x
369
+ let (:=) v x = CT. set v x
362
370
363
371
let full eff res =
364
372
seq eff >> = fun eff ->
@@ -367,14 +375,14 @@ module Prelude(CT : Theory.Core) = struct
367
375
368
376
let data xs =
369
377
let * data = seq xs in
370
- Meta. lift @@ CT. blk null ! data skip
378
+ CT. blk null ! data skip
371
379
372
380
let ctrl xs =
373
381
let * ctrl = seq xs in
374
- Meta. lift @@ CT. blk null pass ! ctrl
382
+ CT. blk null pass ! ctrl
375
383
376
384
let blk lbl xs = seq [
377
- Meta. lift @@ CT. blk lbl pass skip;
385
+ CT. blk lbl pass skip;
378
386
seq xs;
379
387
]
380
388
@@ -386,9 +394,9 @@ module Prelude(CT : Theory.Core) = struct
386
394
let coerce_bits s x f =
387
395
let open Theory.Value.Match in
388
396
let | () = can Theory.Bitv. refine x @@ fun x ->
389
- Meta. lift @@ CT. cast s CT. b0 ! x >> = f in
397
+ CT. cast s CT. b0 ! x >> = f in
390
398
let | () = can Theory.Bool. refine x @@ fun cnd ->
391
- Meta. lift @@ CT. ite ! cnd
399
+ CT. ite ! cnd
392
400
(CT. int s Bitvec. one)
393
401
(CT. int s Bitvec. zero) >> = fun x ->
394
402
f x in
@@ -398,7 +406,7 @@ module Prelude(CT : Theory.Core) = struct
398
406
let open Theory.Value.Match in
399
407
let | () = can Theory.Bool. refine x f in
400
408
let | () = can Theory.Bitv. refine x @@ fun x ->
401
- Meta. lift @@ CT. non_zero ! x >> = fun x -> f x in
409
+ CT. non_zero ! x >> = fun x -> f x in
402
410
undefined
403
411
404
412
let is_static eff = Option. is_some (static eff)
@@ -417,7 +425,7 @@ module Prelude(CT : Theory.Core) = struct
417
425
let word = Theory.Target. bits target in
418
426
let {prog; places} = program in
419
427
let var ?t n = make_var ?t places target n in
420
- let rec eval : ast -> unit Theory.Effect.t Meta .t = function
428
+ let rec eval : ast -> unit Theory.Effect.t KB .t = function
421
429
| {data =Int {data ={exp =x ; typ =Type m } } } -> bigint x m
422
430
| {data =Int {data ={exp =x } } } -> bigint x word
423
431
| {data =Var {data ={exp =n ; typ =Type t } } } -> lookup@@ var ~t n
@@ -443,15 +451,15 @@ module Prelude(CT : Theory.Core) = struct
443
451
else eval yes
444
452
| None ->
445
453
coerce_bool (res cnd) @@ fun cres ->
446
- Meta. lift @@ Theory.Var. fresh Theory.Bool. t >> = fun c ->
454
+ Theory.Var. fresh Theory.Bool. t >> = fun c ->
447
455
let * yes = eval yes in
448
456
let * nay = eval nay in
449
457
full [
450
458
!! cnd;
451
459
data [c := ! cres];
452
- Meta. lift @@ CT. branch (CT. var c) ! yes ! nay;
460
+ CT. branch (CT. var c) ! yes ! nay;
453
461
] @@
454
- Meta. lift @@ CT. ite (CT. var c) ! (res yes) ! (res nay)
462
+ CT. ite (CT. var c) ! (res yes) ! (res nay)
455
463
and rep cnd body =
456
464
let * r = eval cnd in
457
465
match static r with
@@ -466,10 +474,10 @@ module Prelude(CT : Theory.Core) = struct
466
474
let * head = fresh and * loop = fresh and * tail = fresh in
467
475
coerce_bool (res r) @@ fun cres ->
468
476
full [
469
- blk head [ctrl [Meta. lift @@ CT. goto tail]];
477
+ blk head [ctrl [CT. goto tail]];
470
478
blk loop [!! body];
471
479
blk tail [!! r; ctrl [
472
- Meta. lift @@ CT. branch ! cres (CT. goto head) skip
480
+ CT. branch ! cres (CT. goto head) skip
473
481
]]
474
482
] !! cres
475
483
and call ?(toplevel =false ) name xs =
@@ -490,27 +498,26 @@ module Prelude(CT : Theory.Core) = struct
490
498
| None ->
491
499
match Resolve. semantics prog Key. semantics name () with
492
500
| Some Ok (sema ,() ) ->
493
- Meta. lift@@
494
501
Def.Sema. apply sema defn xs
495
502
| Some (Error problem ) -> unresolved name problem
496
503
| None ->
497
504
let msg = Format. asprintf " No definition is found for %a"
498
505
KB.Name. pp name in
499
- fail (Unresolved_definition msg)
506
+ KB. fail (Unresolved_definition msg)
500
507
and app name xs =
501
508
map xs >> = fun (aeff ,xs ) ->
502
509
call name xs >> = fun peff ->
503
510
full [!! aeff; !! peff] !! (res peff)
504
511
and map args =
505
512
seq [] >> = fun eff ->
506
- Meta .List. fold args ~init: (eff,[] ) ~f: (fun (eff ,args ) arg ->
513
+ KB .List. fold args ~init: (eff,[] ) ~f: (fun (eff ,args ) arg ->
507
514
let * eff' = eval arg in
508
515
let + eff = seq [!! eff; !! eff'] in
509
516
(eff,forget (res eff')::args)) >> | fun (eff ,args ) ->
510
517
eff, List. rev args
511
518
and seq_ xs =
512
519
pure nil >> = fun init ->
513
- Meta .List. fold ~init xs ~f: (fun eff x ->
520
+ KB .List. fold ~init xs ~f: (fun eff x ->
514
521
let * eff' = eval x in
515
522
full [!! eff; !! eff'] !! (res eff'))
516
523
and msg fmt args =
@@ -524,7 +531,7 @@ module Prelude(CT : Theory.Core) = struct
524
531
| None -> Format. printf " @[<hv>%a@]" KB.Value. pp v);
525
532
Format. fprintf ppf " @\n " ;
526
533
!! aeff
527
- and err msg = fail (User_error msg)
534
+ and err msg = KB. fail (User_error msg)
528
535
and lookup v =
529
536
Scope. lookup v >> = function
530
537
| Some v -> lookup v
@@ -533,7 +540,7 @@ module Prelude(CT : Theory.Core) = struct
533
540
| Some v -> pure !! v
534
541
| None -> match lookup_parameter prog v with
535
542
| Some def -> eval def >> = assign target v
536
- | None -> pure@@ Meta. lift @@ CT. var v
543
+ | None -> pure@@ CT. var v
537
544
and set_ v x =
538
545
Scope. lookup v >> = function
539
546
| Some v -> eval x >> = assign target ~local: true v
@@ -690,17 +697,16 @@ let provide_semantics ?(stdout=Format.std_formatter) () =
690
697
KB. collect Property. args obj >> = fun args ->
691
698
KB. collect Theory.Unit. target unit >> = fun target ->
692
699
let bits = Theory.Target. bits target in
693
- let module Arith = Bitvec. Make (struct
694
- let modulus = Bitvec. modulus bits
695
- end ) in
696
- let meta = Meta.State. {
700
+ KB.Context. set State. var State. {
697
701
binds = Map. empty (module Theory.Var. Top );
698
702
scope = Map. empty (module Theory.Var. Top );
699
- arith = (module Arith );
700
- } in
701
- Theory. instance () >> = Theory. require >> = fun (module Core) ->
703
+ arith = (module (Bitvec. Make (struct
704
+ let modulus = Bitvec. modulus bits
705
+ end )));
706
+ } >> = fun () ->
707
+ let * (module Core ) = Theory. current in
702
708
let open Prelude (Core) in
703
- Meta. run ( reify stdout prog obj target name args) meta >> = fun ( res , _ ) ->
709
+ reify stdout prog obj target name args >> = fun res ->
704
710
KB. collect Disasm_expert.Basic.Insn. slot obj >> | function
705
711
| Some basic when Insn. (res <> empty) ->
706
712
Insn. with_basic res basic
0 commit comments