Skip to content

Commit 21231f0

Browse files
bmourad01bmourad01
andauthored
Fix compatibility with WP init-mem parameter (#209)
* Collect code addrs for the verifier if `init-mem` is `true` * Fix failing test * Update * Fixing tests for latest BAP Co-authored-by: bmourad01 <[email protected]>
1 parent 5add860 commit 21231f0

File tree

10 files changed

+53
-38
lines changed

10 files changed

+53
-38
lines changed

bap-vibes/src/patch_c.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ let data_of_tgt (target : Theory.target) : Data_model.t KB.t =
16331633
Err.fail @@ Patch_c_error (
16341634
Format.asprintf "Unsupported target %a"
16351635
Theory.Target.pp target) in
1636-
if Theory.Target.matches target "arm" then
1636+
if Theory.Target.matches target "arm-family" then
16371637
if Theory.Target.bits target = 32
16381638
then KB.return Data_model.{sizes = `ILP32; schar = false}
16391639
else fail ()

bap-vibes/src/pipeline.ml

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ let run_KB_computation (f : Data.cls KB.obj KB.t) (state : KB.state)
114114

115115
(* This is the main CEGIS loop. It constructs a patched exe and verifies it.
116116
If its correct, it returns the filepath. If not, it runs again. *)
117-
let rec cegis ?count:(count=1) ?max_tries:(max_tries=None)
118-
~seed:(seed : Seeder.t) (config : Config.t) (orig_proj : project)
119-
(orig_prog : Program.t) (state : KB.state)
120-
: (string, Toplevel_error.t) result =
117+
let rec cegis ?(count : int = 1) ?(max_tries : int option = None)
118+
~(seed : Seeder.t) (config : Config.t) (orig_proj : project)
119+
(orig_prog : Program.t) (orig_code : Bap_wp.Utils.Code_addrs.t)
120+
(state : KB.state) : (string, Toplevel_error.t) result =
121121
Events.(send @@ Header "Starting CEGIS iteration");
122122
Events.(send @@ Info (Printf.sprintf "Iteration: %d" count));
123123

@@ -132,27 +132,29 @@ let rec cegis ?count:(count=1) ?max_tries:(max_tries=None)
132132

133133
(* Temporarily use the new KB state when loading the patched binary. *)
134134
Toplevel.set new_state;
135-
let+ _, patch_prog = Utils.load_exe tmp_patched_filepath in
135+
let+ patch_proj, patch_prog = Utils.load_exe tmp_patched_filepath in
136+
let patch_code =
137+
let open Bap_wp.Utils.Code_addrs in
138+
if (Config.wp_params config).init_mem
139+
then collect patch_proj
140+
else empty in
136141
Toplevel.set state;
137142

138143
let wp_params = Config.wp_params config in
139144
let target = Project.target orig_proj in
140145
if Config.perform_verification config then
141-
let verif_res =
142-
Verifier.verify target wp_params
143-
~orig_prog:(orig_prog, Config.exe config)
144-
~patch_prog:(patch_prog, tmp_patched_filepath)
145-
in
146+
(* let orig_code, patch_code = Addr.Set.empty, Addr.Set.empty in *)
147+
let verif_res = Verifier.verify target wp_params
148+
~orig_prog:(orig_prog, Config.exe config, orig_code)
149+
~patch_prog:(patch_prog, tmp_patched_filepath, patch_code) in
146150
match verif_res with
151+
| Error _ as err -> err
147152
| Ok Verifier.Done -> finalize_patched_exe value
148153
| Ok Verifier.Again ->
149-
begin
150-
let new_count = count + 1 in
151-
let new_seed = Seeder.extract_seed value new_state in
152-
cegis config orig_proj orig_prog state
153-
~count:new_count ~max_tries ~seed:new_seed
154-
end
155-
| Error e -> Error e
154+
let new_count = count + 1 in
155+
let new_seed = Seeder.extract_seed value new_state in
156+
cegis config orig_proj orig_prog orig_code state
157+
~count:new_count ~max_tries ~seed:new_seed
156158
else
157159
let warning =
158160
"WARNING: No verification performed. Patched binary may be incorrect."
@@ -173,11 +175,16 @@ let run (config : Config.t) : (string, Toplevel_error.t) result =
173175
let filepath = Config.exe config in
174176
Events.(send @@ Info (Format.sprintf "Loading into BAP: %s..." filepath));
175177
let+ orig_proj, orig_prog = Utils.load_exe filepath in
178+
let orig_code =
179+
let open Bap_wp.Utils.Code_addrs in
180+
if (Config.wp_params config).init_mem
181+
then collect orig_proj
182+
else empty in
176183

177184
let state = Toplevel.current () in
178185
let computation = init config orig_proj in
179186
let+ obj, new_state = run_KB_computation computation state in
180187
let seed = Seeder.extract_seed obj new_state in
181188

182189
let max_tries = Config.max_tries config in
183-
cegis config orig_proj orig_prog new_state ~max_tries ~seed
190+
cegis config orig_proj orig_prog orig_code new_state ~max_tries ~seed

bap-vibes/src/verifier.ml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,28 @@ let wp_verifier (p : Params.t) (inputs : Runner.input list) =
5252
*)
5353
let verify
5454
?(verifier = wp_verifier)
55-
~(orig_prog : Program.t * string)
56-
~(patch_prog : Program.t * string)
55+
~(orig_prog : Program.t * string * Bap_wp.Utils.Code_addrs.t)
56+
~(patch_prog : Program.t * string * Bap_wp.Utils.Code_addrs.t)
5757
(tgt : Theory.target)
5858
(params : Params.t) : (next_step, Toplevel_error.t) result =
59-
let prog1, name1 = orig_prog in
60-
let prog2, name2 = patch_prog in
59+
let prog1, name1, code1 = orig_prog in
60+
let prog2, name2, code2 = patch_prog in
6161
Events.(send @@ Header "Starting Verifier");
6262
Events.(send @@ Info "Beginning weakest-precondition analysis...");
6363
Events.(send @@ Info (sprintf "Original program: %s" name1));
6464
Events.(send @@ Info (sprintf "Patched program: %s" name2));
65-
let input1 = Runner.{program = prog1; target = tgt; filename = name1} in
66-
let input2 = Runner.{program = prog2; target = tgt; filename = name2} in
65+
let input1 = Runner.{
66+
program = prog1;
67+
target = tgt;
68+
filename = name1;
69+
code_addrs = code1;
70+
} in
71+
let input2 = Runner.{
72+
program = prog2;
73+
target = tgt;
74+
filename = name2;
75+
code_addrs = code2;
76+
} in
6777
let* status =
6878
verifier params [input1; input2] |>
6979
Result.map_error ~f:(fun e -> Toplevel_error.WP_failure e) in

bap-vibes/src/verifier.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ type next_step = Done | Again
4545
verifier's results. *)
4646
val verify :
4747
?verifier:(verifier) ->
48-
orig_prog:(Program.t * string) ->
49-
patch_prog:(Program.t * string) ->
48+
orig_prog:(Program.t * string * Utils.Code_addrs.t) ->
49+
patch_prog:(Program.t * string * Utils.Code_addrs.t) ->
5050
Theory.target ->
5151
Run_parameters.t ->
5252
(next_step, Toplevel_error.t) result

bap-vibes/tests/integration/test_minizinc.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ let ex1 : Ir.t = {
110110
blks = [blk1]
111111
}
112112

113-
let arm_tgt = Arm_target.LE.v7
113+
let arm_tgt = Arm_target.LE.v7a
114114
let arm_lang = Arm_target.llvm_a32
115115

116116
(* Ensure minizinc produces the expected output on our sample IR block. *)

bap-vibes/tests/unit/helpers.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ let dummy_code : Cabs.definition =
5959

6060
(* Get an empty program that can be used in tests. *)
6161
let prog_exn (proj : (Project.t * string, Error.t) result)
62-
: Program.t * string =
62+
: Program.t * string * Bap_wp.Utils.Code_addrs.t =
6363
let p, s = proj_exn proj in
64-
(Project.program p, s)
64+
(Project.program p, s, Bap_wp.Utils.Code_addrs.empty)
6565

6666
(* Some dummy values that can be used in tests. *)
6767
let the_target () = Theory.Target.read ~package:"bap" "armv7+le"

bap-vibes/tests/unit/test_parse_c.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ let test_call_hex _ =
132132
"int temp;
133133
if (temp == 0) { ((void (*)())0x3ec)(); }"
134134
"{
135-
if (~temp) {
135+
if (temp = 0) {
136136
jmp 0x3EC
137137
}
138138
}"
@@ -427,7 +427,7 @@ let test_bool_not _ =
427427
x = y;
428428
}"
429429
"{
430-
if (~x) {
430+
if (x = 0) {
431431
x := y
432432
}
433433
}"
@@ -437,7 +437,7 @@ let test_mixed_sorts _ =
437437
"int x, y;
438438
x += (y == 0);"
439439
"{
440-
x := x + pad:32[~y]
440+
x := x + pad:32[y = 0]
441441
}"
442442

443443
let suite = [

bap-vibes/tests/unit/test_substituter.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module Test_result = struct
3535
let result = KB.Class.property ~package cls "test-result" domain
3636
end
3737

38-
let x86_tgt = Theory.Target.get ~package:"bap" "amd64"
38+
let x86_tgt = Theory.Target.get ~package:"bap" "x86_64"
3939

4040
(* Very lax equality to avoid tid comparison failures *)
4141
let eq_elt e1 e2 =

resources/exes/arm-isel-jmp2/config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"max-tries": 1,
2+
"max-tries": 10,
33
"wp-params": {
44
"func": "main",
55
"postcond": "(assert (= R0_mod #x00000006))"
@@ -21,7 +21,6 @@
2121
"patch-size" : 128,
2222
"patch-vars": [
2323
{"name": "retvar",
24-
"at-entry": "R0",
2524
"at-exit": "R0"
2625
}
2726
]

resources/exes/arm-simple-sum/config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"max-tries": 1,
2+
"max-tries": 10,
33
"wp-params": {
44
"func": "main",
55
"postcond": "(assert (= R0_mod #x00000009))"
@@ -16,7 +16,6 @@
1616
"patch-size" : 128,
1717
"patch-vars": [
1818
{"name": "retvar",
19-
"at-entry": "R0",
2019
"at-exit": "R0"
2120
}
2221
]

0 commit comments

Comments
 (0)