package bap-trivial-condition-form

  1. Overview
  2. Docs
Eliminates complex conditionals in branches

Install

Dune Dependency

Authors

Maintainers

Sources

v2.5.0.tar.gz
sha256=9c126781385d2fa9b8edab22e62b25c70bf2f99f6ec78abb7e5e36d63cfa4174
md5=5abd9b3628b43f797326034f31ca574f

doc/src/bap-plugin-trivial_condition_form/trivial_condition_form_main.ml.html

Source file trivial_condition_form_main.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
open Core_kernel[@@warning "-D"]
open Bap.Std
open Format

include Self()



(** Trivial Condition Form (TCF) transformation.

    In the TCF a condition expression must be either a variable or a
    constant. The transformations detect non-trivial condition
    expressions and bind them to variables whose definitions are
    pushed to the block definition list. *)
module TCF = struct
  let blk_without_jmps = Term.filter jmp_t ~f:(fun _ -> false)
  let new_var () = Var.create ~is_virtual:true ~fresh:true "c" bool_t

  (* Pre: number of jumps is greater than 1
     post: number of jumps is the same, each jump is in TCF.*)
  let blk blk =
    Term.enum jmp_t blk |>
    Seq.fold ~init:(blk_without_jmps blk) ~f:(fun blk jmp ->
        match Jmp.cond jmp with
        | Bil.Int _ | Bil.Var _ -> Term.append jmp_t blk jmp
        | cond ->
          let var = new_var () in
          let def = Def.create var cond in
          let blk = Term.append def_t blk def in
          let jmp = Jmp.with_cond jmp (Bil.var var) in
          Term.append jmp_t blk jmp)

  let sub = Term.map blk_t ~f:(fun b ->
      if Term.length jmp_t b > 0 then blk b else b)

  let prog = Term.map sub_t ~f:sub

  let proj = Project.map_program ~f:prog
end


let main proj =
  info "translating the program into the Trivial Condition Form (TCF)";
  TCF.proj proj

open Config;;

manpage [
  `S "DESCRIPTION";
  `P "Ensures that all branching conditions are either a variable
or a constant. We call such representation a Trivial Condition Form
(TCF). During the translation all complex condition expressions are
hoisted into the assignment section of a block.";
];;


let () = when_ready (fun _ ->
    Project.register_pass ~runonce:true main)
OCaml

Innovation. Community. Security.