Skip to content

Commit b94c7f0

Browse files
committed
add flow insensitive analysis for GOT rewriting
plus support swith statement jump for rewriting
1 parent 95410a6 commit b94c7f0

15 files changed

Lines changed: 964 additions & 328 deletions

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches:
66
- dev
77
- main
8+
- instrument-framework
89
jobs:
910
execute-tests-ubuntu22:
1011
runs-on: ubuntu-22.04

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
all:
44
test/test_all.py -a -c
55
test/test_instrument.sh 2>&1 | tee test.instrument
6-
rm src/points.ins src/fun.o
6+
rm src/points.ins src/fun.o || true
77
test/test_action.sh 2>&1 | tee test.all
88
test/test_coreutils.sh 2>&1 | tee test.coreutils
99

src/ail.ml

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,48 @@ open Cg
99
open Ail_utils
1010

1111

12-
1312
class ail =
1413
object (self)
1514
val mutable funcs : func list = []
1615
val mutable secs: section list = []
1716
val mutable intrs: string list = []
1817
val mutable instrs_list: instr list = []
1918
val mutable datas: string list = []
20-
val mutable g_bss: (string*string*string) list = []
19+
val mutable g_bss: (string * string * string) list = []
2120

22-
method sections =
21+
method sections : unit =
2322
let filelines = File.lines_of "sections.info"
2423
and help l =
2524
let items = Str.split (Str.regexp " +") l in
2625
let addr = int_of_string ("0x"^(List.nth items 1))
2726
and size = int_of_string ("0x"^(List.nth items 3))
2827
and secname = List.nth items 0 in
29-
secs <- {sec_name=secname; sec_begin_addr=addr;
30-
sec_size=size}::secs
28+
secs <- {
29+
sec_name = secname;
30+
sec_begin_addr = addr;
31+
sec_size = size
32+
} :: secs
3133
in
3234
Enum.iter help filelines
3335

34-
method externfuncs =
36+
method externfuncs : unit =
3537
let filelines = File.lines_of "externfuncs.info"
3638
and help l =
3739
let items = Str.split (Str.regexp " +") l in
3840
let addr = int_of_string ("0x"^(List.nth items 0))
3941
and func = List.nth items 1 in
40-
funcs <- {func_name=func; func_begin_addr=addr; func_end_addr = 0;
41-
is_lib=true}::funcs
42+
funcs <- {
43+
func_name = func;
44+
func_begin_addr = addr;
45+
func_end_addr = 0;
46+
is_lib = true
47+
} :: funcs
4248
in
4349
Enum.iter help filelines
4450

45-
(* in stripped binary, any user functions' information has been stripped
51+
(** in stripped binary, any user functions' information has been stripped
4652
* so slicing function class really does the job *)
47-
method userfuncs =
53+
method userfuncs : unit =
4854
let filelines = File.lines_of "userfuncs.info"
4955
and help l =
5056
if String.exists l "-0x" || String.exists l "+0x" then
@@ -58,28 +64,35 @@ object (self)
5864
let funname' = String.sub funname 1 (len-3) in
5965
if String.exists funname' "@@" then
6066
let fn = List.nth (Str.split (Str.regexp_string "@@") funname') 0 in
61-
funcs <- {func_name=fn; func_begin_addr=addr; func_end_addr = 0;
62-
is_lib=false}::funcs
67+
funcs <- {
68+
func_name = fn;
69+
func_begin_addr = addr;
70+
func_end_addr = 0;
71+
is_lib = false
72+
} :: funcs
6373
else
64-
funcs <- {func_name=funname'; func_begin_addr=addr; func_end_addr = 0;
65-
is_lib=false}::funcs
74+
funcs <- {
75+
func_name = funname';
76+
func_begin_addr = addr;
77+
func_end_addr = 0;
78+
is_lib = false
79+
} :: funcs
6680
end
6781
in
6882
Enum.iter help filelines
6983

70-
71-
method get_userfuncs =
84+
method get_userfuncs : func list =
7285
List.filter (fun f -> f.is_lib=false) funcs
7386

74-
method externdatas =
87+
method externdatas : unit =
7588
let filelines = File.lines_of "externdatas.info"
7689
and help l =
7790
let data = String.trim l in
7891
datas <- data::datas
7992
in
8093
Enum.iter help filelines
8194

82-
method global_bss =
95+
method global_bss : unit =
8396
let filelines = File.lines_of "globalbss.info"
8497
and help l =
8598
let items = Str.split (Str.regexp " +") l in
@@ -88,11 +101,11 @@ object (self)
88101
let addr' = String.uppercase_ascii addr
89102
and rtype = List.nth items 1
90103
and n = String.trim (List.nth items 2) in
91-
g_bss <- (addr', rtype, n)::g_bss
104+
g_bss <- (addr', rtype, n) :: g_bss
92105
in
93106
Enum.iter help filelines
94107

95-
method ail_dump =
108+
method ail_dump : unit =
96109
(* currently we just dump the extern function info *)
97110
let check_sym_func f =
98111
try
@@ -106,23 +119,23 @@ object (self)
106119
|> List.iter (fun l -> Printf.fprintf oc "extern %s\n" l.func_name));
107120
close_out oc
108121

109-
method ehframe_dump =
110-
ignore (Sys.command("cat eh_frame.data >> final.s"))
122+
method ehframe_dump : unit =
123+
ignore ( Sys.command ("cat eh_frame.data >> final.s") )
111124

112-
method excpt_tbl_dump =
113-
ignore (Sys.command("cat gcc_exception_table.data >> final.s"))
125+
method excpt_tbl_dump : unit =
126+
ignore ( Sys.command ("cat gcc_exception_table.data >> final.s") )
114127

115-
method post_process f (arch : string) =
116-
ignore (Sys.command ("python3 main_discover.py " ^ " " ^ f ^ " " ^ arch));
117-
ignore (Sys.command("python3 post_process.py "^arch));
118-
ignore (Sys.command("python3 post_process_lib.py"))
128+
method post_process (f : string) (arch : string) : unit =
129+
ignore ( Sys.command ("python3 main_discover.py " ^ " " ^ f ^ " " ^ arch) );
130+
ignore ( Sys.command ("python3 post_process.py "^arch) );
131+
ignore ( Sys.command ("python3 post_process_lib.py") )
119132
(*
120133
self#ehframe_dump;
121134
self#excpt_tbl_dump;
122135
*)
123136

124-
method pre_process =
125-
ignore (Sys.command("python3 pre_process.py"))
137+
method pre_process : unit =
138+
ignore ( Sys.command ("python3 pre_process.py") )
126139

127140
method instr_process (f : string) (arch : string) : unit =
128141
let open Disassemble_process in
@@ -138,9 +151,9 @@ object (self)
138151

139152
let fbl, bbl, cfg_t, cg, il', re, ufl = A.analyze_one il fl re in
140153

141-
let il', ufl', fch = S.apply il' ufl in
154+
let il', ufl', fch = S.apply il' ufl f in
142155

143-
let instrumented_il =
156+
let instrumented_il : instr list =
144157
I.apply ~instrs:il' ~fbl ~bbl ~funcs:ufl' ~fname_callsites:fch
145158
in
146159

src/ail_utils.ml

Lines changed: 87 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ module IntSet = Set.Make(
2828

2929
module StringSet = Set.Make(String)
3030

31+
let stub_loc : loc = {
32+
loc_label = "";
33+
loc_addr = 0;
34+
loc_visible = true
35+
}
36+
3137
let 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 =
103109
let 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+
106120
let 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

src/cfg.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class cfg =
290290
method visit il =
291291
let last_i = List.nth il (List.length il - 1) in
292292
end_loc <- get_loc last_i;
293-
let il' = List.map self#vinst il in
293+
let il' = List.map self#vinst' il in
294294
bl <- self#update_bl;
295295
self#fb_list bl;
296296
let bl' = List.sort (fun b1 b2 ->

0 commit comments

Comments
 (0)