Skip to content

Commit b08a71d

Browse files
Initial superset disasm integration
1 parent 44a8d3b commit b08a71d

22 files changed

+3384
-3
lines changed

lib/bap/bap.mli

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6040,6 +6040,19 @@ module Std : sig
60406040
unlifted instructions. *)
60416041
val errors : t -> error list
60426042
end
6043+
6044+
module SupersetDisasm : sig
6045+
type 'a t
6046+
val raw_superset :
6047+
?backend:string -> data:'a ->
6048+
?f:(mem * Basic.full_insn option -> 'a t -> 'a t) ->
6049+
string -> 'a t
6050+
val trimmed_superset :
6051+
data:'a -> ?f:('a t -> mem -> Basic.full_insn option -> (addr option * edge) list -> 'a t) list ->
6052+
backend:string -> string -> 'a t
6053+
val converged_superset : unit t -> unit t
6054+
end
6055+
60436056
end
60446057

60456058
(** Assembly instruction.

lib/bap_disasm/abstract_ssa.ml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
open Bap_types.Std
2+
open Bap_image_std
3+
open Core_kernel.Std
4+
5+
let stmt_def_vars =
6+
object(self)
7+
inherit [Exp.Set.t] Stmt.visitor
8+
method enter_move def use accu =
9+
if not Var.(is_virtual def) then
10+
Set.add accu Exp.(Bil.Var def)
11+
else accu
12+
end
13+
14+
let stmt_use_vars =
15+
object(self)
16+
inherit [Exp.Set.t] Stmt.visitor
17+
method enter_move def use accu =
18+
Set.add accu use
19+
end
20+
21+
22+
let stmt_def_freevars =
23+
object(self)
24+
inherit [Var.Set.t] Stmt.visitor
25+
method enter_move def use accu =
26+
if not Var.(is_virtual def) then
27+
Set.add accu def
28+
else accu
29+
end
30+
31+
let stmt_use_freevars =
32+
object(self)
33+
inherit [Var.Set.t] Stmt.visitor
34+
method enter_move def use accu =
35+
let free_vars =
36+
Set.filter ~f:(fun v -> not Var.(is_virtual v)) (Exp.free_vars use)
37+
in Set.union accu free_vars
38+
end
39+
40+
let def_ssa bil =
41+
stmt_def_vars#run bil Exp.Set.empty
42+
43+
let use_ssa bil =
44+
stmt_use_vars#run bil Exp.Set.empty
45+
46+
let def_freevars bil =
47+
stmt_def_freevars#run bil Var.Set.empty
48+
49+
let use_freevars bil =
50+
stmt_use_freevars#run bil Var.Set.empty

lib/bap_disasm/bap_disasm_std.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Disasm_expert = struct
1313
type nonrec lifter = lifter
1414
module Basic = Bap_disasm_basic
1515
module Recursive = Bap_disasm_rec
16+
module SupersetDisasm = Bap_disasm_superset
1617
module Linear = Bap_disasm_linear_sweep
1718
module Kind = Bap_insn_kind
1819
module Insn = Bap_disasm_basic.Insn

lib/bap_disasm/bap_disasm_superset.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type 'a t = 'a Superset.t
2+
let raw_superset = Superset.superset_disasm_of_file
3+
let trimmed_superset = Trim.trimmed_disasm_of_file
4+
let converged_superset s = Trim.Default.trim Features.(apply_featurepmap Features.default_features s)

lib/bap_disasm/builder.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
open Core_kernel.Std
2+
open Bap.Std
3+
open Superset
4+
open Trim
5+
6+
module Builder
7+
(Superset : Superset_intf)
8+
(Reducer : Reducer) = struct
9+
10+
end

lib/bap_disasm/cfg_dot_layout.ml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
open Core_kernel.Std
2+
open Bap_types.Std
3+
open Bap_image_std
4+
open Graphlib.Std
5+
6+
type colored_superset = Superset_risg.t * Addr.Hash_set.t String.Map.t
7+
* Superset.elem Addr.Map.t
8+
9+
module Make(T : sig val instance : colored_superset end) = struct
10+
open T
11+
module Dottable = struct
12+
type t = colored_superset
13+
14+
module V = struct
15+
type t = Superset_risg.G.V.t
16+
end
17+
18+
module E = struct
19+
type t = Superset_risg.G.E.t
20+
let src (s,_) = s
21+
let dst (_,d) = d
22+
end
23+
24+
let iter_vertex f (g, _, _) =
25+
Superset_risg.G.iter_vertex f g
26+
27+
let iter_edges_e f (g, _, _) =
28+
Superset_risg.G.iter_edges_e f g
29+
30+
let graph_attributes _ = [
31+
`Fontsize 14;
32+
]
33+
let default_vertex_attributes gr = [
34+
`Shape `Box;
35+
(*`Height 1.0*.Memory.(length mem);*)
36+
`Fontsize 14;
37+
`Fontcolor 0x666699;
38+
`Fontname "Monospace";
39+
`Width 1.0
40+
]
41+
42+
let red = 0xff0000
43+
let green = 0x009900
44+
let yellow = 0xffff00
45+
let blue = 0x0000ff
46+
let orange = 0xff6600
47+
let purple = 0x660066
48+
let brown = 0x663300
49+
let cyan = 0x0099cc
50+
51+
let vertex_name name =
52+
let fmt = Format.str_formatter in
53+
Addr.(pp_generic ~prefix:`none ~suffix:`none ~format:`dec
54+
fmt name);
55+
Format.flush_str_formatter ()
56+
57+
let vertex_attributes v =
58+
let default_attrs =
59+
[
60+
`Label ((vertex_name v));
61+
] in
62+
let g, colors, insn_map = instance in
63+
let contains name =
64+
match Map.find colors name with
65+
| Some(s) ->
66+
Hash_set.mem s v
67+
| None -> false in
68+
let find_update default_attrs name color =
69+
if contains name then
70+
`Color color :: default_attrs
71+
else default_attrs in
72+
let default_attrs =
73+
find_update default_attrs "False Negatives" red in
74+
let default_attrs =
75+
find_update default_attrs "True Positives" green in
76+
let default_attrs =
77+
find_update default_attrs "False Positives" yellow in
78+
let default_attrs =
79+
match List.hd default_attrs with
80+
| Some (`Color _) ->
81+
default_attrs
82+
| _ -> `Color 0X660000 :: default_attrs in
83+
match Map.find insn_map v with
84+
| Some(mem,insn) ->
85+
let len = float_of_int Memory.(length mem) in
86+
`Height (1.0 *. len) ::
87+
default_attrs
88+
| None -> default_attrs
89+
90+
91+
let get_subgraph _ = None
92+
let default_edge_attributes _ = [
93+
`Penwidth 1.0;
94+
`Arrowsize 0.5;
95+
`Headport `N;
96+
`Tailport `S;
97+
`Labelfloat true;
98+
]
99+
100+
let edge_attributes (src,dst) =
101+
(*let color,weight = match kind,arity with
102+
| `Fall,`Many -> 0x660000, 4
103+
| `Fall,`Mono -> 0x000066, 8
104+
| `Cond,_ -> 0x006600, 2
105+
| `Jump,_ -> 0x000066, 2 in*)
106+
[
107+
(*`Color color;*)
108+
(*`Weight weight;*)
109+
]
110+
end
111+
module Dot = Graph.Graphviz.Dot(Dottable)
112+
113+
include Dot
114+
end

lib/bap_disasm/common.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
open Bap_types.Std
2+
open Bap_image_std
3+
open Core_kernel.Std
4+
5+
let img_of_filename filename =
6+
let img, errs = Image.create filename |> ok_exn in
7+
List.iter errs ~f:(fun err ->
8+
(Error.pp Format.std_formatter err);
9+
);
10+
img
11+
12+
let create_memory arch min_addr data =
13+
let data = Bigstring.of_string data in
14+
Memory.create (Arch.endian arch) min_addr data

0 commit comments

Comments
 (0)