@@ -28,6 +28,12 @@ module IntSet = Set.Make(
2828
2929module StringSet = Set.Make(String)
3030
31+ let stub_loc : loc = {
32+ loc_label = "";
33+ loc_addr = 0;
34+ loc_visible = true
35+ }
36+
3137let read_lines (filename : string) : string list =
3238 File.with_file_in filename (fun input ->
3339 List.of_enum (IO.lines_of input)
@@ -103,6 +109,14 @@ let string_to_int32 s =
103109let compare_loc l1 l2 =
104110 l1.loc_addr = l2.loc_addr && (l1.loc_label = l2.loc_label)
105111
112+ let get_tags i =
113+ match i with
114+ | SingleInstr (_, _, _, tags) -> tags
115+ | DoubleInstr (_, _, _, _, tags) -> tags
116+ | TripleInstr (_, _, _, _, _, tags) -> tags
117+ | FourInstr (_, _, _, _, _, _, tags) -> tags
118+ | FifInstr (_, _, _, _, _, _, _, tags) -> tags
119+
106120let get_loc i =
107121 match i with
108122 | SingleInstr (_, l, _, _) -> l
@@ -1445,34 +1459,10 @@ module Func_utils = struct
14451459 (*aux "S_0x80541C5" (Hashtbl.find func2cfg_table "S_0x80541C5")*)
14461460 Hashtbl.iter aux func2cfg_table
14471461
1448- let func2cfg (il : instr list) funcs =
1449- let func2il il =
1450- let func2il_table = Hashtbl.create 40 in
1451- let rec slice_il fl il =
1452- match (fl,il) with
1453- | ([], il') -> func2il_table
1454- | (hf::tf, []) -> func2il_table
1455- | (hf::tf, hi::ti) ->
1456- begin
1457- let f_ba = hf.func_begin_addr in
1458- let f_ea = hf.func_end_addr in
1459- let i_loc = get_loc hi in
1460- let i_addr = i_loc.loc_addr in
1461- if i_addr >= f_ba && i_addr < f_ea then
1462- begin
1463- if Hashtbl.mem func2il_table hf.func_name then
1464- let hf_il = Hashtbl.find func2il_table hf.func_name in
1465- Hashtbl.replace func2il_table hf.func_name (hi::hf_il)
1466- else
1467- Hashtbl.add func2il_table hf.func_name [hi];
1468- slice_il fl ti
1469- end
1470- else
1471- slice_il tf il
1472- end
1473- in
1474- slice_il funcs il
1475- in
1462+ let func2cfg
1463+ (il : instr list)
1464+ (funcs : func list)
1465+ : (string, cfgi) Hashtbl.t =
14761466 let is_ct op =
14771467 match op with
14781468 | Intel_OP io -> (
@@ -1494,7 +1484,74 @@ module Func_utils = struct
14941484 | _ -> None)
14951485 | _ -> None
14961486 in
1497- let add_edge curr_cfg (i_from:instr option) (i_to:instr option) : (instr option, instr option list) Hashtbl.t =
1487+ let fb2fn funcs d =
1488+ match List.find_opt (fun f -> f.func_begin_addr = d) funcs with
1489+ | Some f -> Some f.func_name
1490+ | None -> None
1491+ in
1492+ let func2il il =
1493+ let func2il_table = Hashtbl.create 40 in
1494+ let worklist = Queue.create () in
1495+ let rec slice_il
1496+ (fl : func list)
1497+ (il : instr list)
1498+ : (string, instr list) Hashtbl.t =
1499+ match (fl,il) with
1500+ | ([], il') -> func2il_table
1501+ | (hf :: tf, []) -> func2il_table
1502+ | (hf :: tf, hi :: ti) ->
1503+ begin
1504+ let f_ba = hf.func_begin_addr in
1505+ let f_ea = hf.func_end_addr in
1506+ let i_loc = get_loc hi in
1507+ let i_addr = i_loc.loc_addr in
1508+ if i_addr >= f_ba && i_addr < f_ea then
1509+ begin
1510+ if Hashtbl.mem func2il_table hf.func_name then
1511+ let hf_il = Hashtbl.find func2il_table hf.func_name in
1512+ Hashtbl.replace func2il_table hf.func_name (hi :: hf_il)
1513+ else Hashtbl.add func2il_table hf.func_name [hi];
1514+ let _ = match get_ct_des hi with
1515+ | Some d ->
1516+ if d >= f_ea then
1517+ begin
1518+ match fb2fn funcs d with
1519+ | Some fn ->
1520+ Queue.push (hf.func_name, fn) worklist
1521+ | None -> ()
1522+ end
1523+ | None -> () in
1524+ slice_il fl ti
1525+ end
1526+ else
1527+ slice_il tf il
1528+ end
1529+ in
1530+ let func2il' = slice_il funcs il in
1531+ while not (Queue.is_empty worklist) do
1532+ let (fn, fn2) = Queue.pop worklist in
1533+ match Hashtbl.find_opt func2il' fn with
1534+ | Some f_il ->
1535+ begin
1536+ match Hashtbl.find_opt func2il' fn2 with
1537+ | Some f_il2 ->
1538+ Hashtbl.replace func2il' fn (f_il @ f_il2);
1539+ Hashtbl.remove func2il' fn2
1540+ | None -> ()
1541+ end
1542+ | None -> ()
1543+ (*let f_il = Hashtbl.find func2il' fn in
1544+ let f_il2 = Hashtbl.find func2il' fn2 in
1545+ Hashtbl.replace func2il' fn (f_il @ f_il2);
1546+ Hashtbl.remove func2il' fn2*)
1547+ done;
1548+ func2il'
1549+ in
1550+ let add_edge
1551+ curr_cfg
1552+ (i_from : instr option)
1553+ (i_to : instr option)
1554+ : (instr option, instr option list) Hashtbl.t =
14981555 if Hashtbl.mem curr_cfg i_from then
14991556 let existing_edges = Hashtbl.find curr_cfg i_from in
15001557 Hashtbl.replace curr_cfg i_from (i_to :: existing_edges)
@@ -1504,7 +1561,7 @@ module Func_utils = struct
15041561 in
15051562 let func2cfg_table = Hashtbl.create 40 in
15061563 let func2il_table = func2il il in
1507- let rec create_cfg f (f_il: instr list) pred_cfg succ_cfg =
1564+ let rec create_cfg f (f_il : instr list) pred_cfg succ_cfg =
15081565 match f_il with
15091566 | [] ->
15101567 let ordered_il = List.rev (Hashtbl.find func2il_table f.func_name) in
0 commit comments