Skip to content

Commit

Permalink
Merge pull request BinaryAnalysisPlatform#61 from ivg/public-API
Browse files Browse the repository at this point in the history
BAP Public API

resolves #4 and #11
  • Loading branch information
ivg committed Jan 13, 2015
2 parents 76c7cf2 + ab190eb commit 5c88397
Show file tree
Hide file tree
Showing 87 changed files with 4,156 additions and 151 deletions.
1 change: 1 addition & 0 deletions .merlin
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ S lib/bap_types

PKG core_kernel
PKG ocamlbuild
PKG jsonm
EXT ounit
EXT here
EXT nonrec
Expand Down
23 changes: 14 additions & 9 deletions .travis-ci.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
OPAM_DEPENDS="core_kernel oasis piqi zarith bitstring utop cmdliner"
SYS_DEPENDS="libgmp-dev time llvm-3.4-dev"
OPAM_DEPENDS="core_kernel.111.28.00 oasis zarith bitstring utop cmdliner faillib ezjsonm lwt-zmq uri.1.7.2 re cohttp.0.15.0"
SYS_DEPENDS="libgmp-dev time llvm-3.4-dev libzmq3-dev aspcud"

case "$OCAML_VERSION,$OPAM_VERSION" in
4.02.0,1.2.0) ppa=avsm/ocaml42+opam12 ;;
4.02.0,1.1.0) ppa=avsm/ocaml42+opam11 ;;
4.02.1,1.2.0) ppa=avsm/ocaml42+opam12 ;;
4.02.1,1.1.0) ppa=avsm/ocaml42+opam11 ;;
4.01.0,1.2.0) ppa=avsm/ocaml41+opam12 ;;
4.01.0,1.1.0) ppa=avsm/ocaml41+opam11 ;;
*) echo Unknown $OCAML_VERSION,$OPAM_VERSION; exit 1 ;;
esac


install_on_linux () {
echo "yes" | sudo add-apt-repository ppa:$ppa
echo 'yes' | sudo add-apt-repository ppa:$ppa
echo 'yes' | sudo apt-add-repository ppa:chris-lea/zeromq
sudo apt-get update -qq
sudo apt-get install -qq ocaml ocaml-native-compilers camlp4-extra opam $SYS_DEPENDS
opam init

if [ $OPAM_VERSION = "1.1.0" ]; then
opam init default https://opam.ocaml.org/1.1
else
opam init
fi
}

install_on_osx () {
Expand All @@ -40,13 +45,13 @@ ocaml -version
echo OPAM versions
opam --version
opam --git-version
opam repository list

opam init
opam install ${OPAM_DEPENDS}
eval `opam config env`

oasis setup
./configure --prefix=$(opam config var prefix) --enable-tests --enable-serialization --with-cxx=`which $CXX`
./configure --prefix=$(opam config var prefix) --enable-tests --with-cxx=`which $CXX`
make
make install
make test
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ compiler:
env:
- OCAML_VERSION=4.01.0 OPAM_VERSION=1.1.0
- OCAML_VERSION=4.01.0 OPAM_VERSION=1.2.0
- OCAML_VERSION=4.02.0 OPAM_VERSION=1.2.0 TEST_TARGETS=arm
- OCAML_VERSION=4.02.0 OPAM_VERSION=1.2.0 TEST_TARGETS=x86
- OCAML_VERSION=4.02.0 OPAM_VERSION=1.2.0 TEST_TARGETS=x86_64
- OCAML_VERSION=4.02.1 OPAM_VERSION=1.2.0 TEST_TARGETS=arm
- OCAML_VERSION=4.02.1 OPAM_VERSION=1.2.0 TEST_TARGETS=x86
- OCAML_VERSION=4.02.1 OPAM_VERSION=1.2.0 TEST_TARGETS=x86_64
40 changes: 35 additions & 5 deletions _oasis
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ BuildDepends:
comparelib.syntax,
core_kernel,
enumerate.syntax,
faillib.syntax,
fieldslib.syntax,
herelib.syntax,
herelib.syntax,
pa_bench.syntax,
pa_ounit.syntax,
pa_ounit.syntax,
sexplib.syntax,
variantslib.syntax
Expand Down Expand Up @@ -53,13 +51,21 @@ Flag benchmarks
Default: false

Flag disassemblers
Description: Builds C++ interface to dissassemblers backend
Description: Build C++ interface to dissassemblers backend
Default: true

Flag server
Description: Build BAP server
Default: true

Flag llvm
Description: Build with llvm backend
Default: true

Flag server
Description: Build BAP server
Default: true

Library bap
Path: lib/bap
FindLibName: bap
Expand Down Expand Up @@ -129,7 +135,7 @@ Library serialization
BuildTools: ocamlbuild, piqi
DataFiles: *.piqi
CompiledObject: best
BuildDepends: piqirun.pb, piqirun.ext, bap.types
BuildDepends: piqirun, bap.types
Modules: Bil_piqi,
Stmt_piqi,
Stmt_piqi_ext
Expand Down Expand Up @@ -250,6 +256,7 @@ Library benchmarks
Install: false
Modules: Bench_image


Library dwarf_test
Path: lib_test/bap_dwarf
Build$: flag(tests)
Expand All @@ -266,6 +273,29 @@ Library disasm_test
Install: false
Modules: Test_disasm

Library core_lwt
Path: lwt
Build$: flag(server)
CompiledObject: best
BuildDepends: lwt, lwt.unix, lwt.log
Modules: Core_lwt,
Core_lwt_basic,
Core_lwt_container,
Core_lwt_container_intf,
Core_lwt_extra,
Core_lwt_or_error,
Core_lwt_pool,
Core_lwt_stream


Executable "bap-server"
Path: src/server
Build$: flag(server)
CompiledObject: best
BuildDepends: bap, lwt-zmq, ezjsonm, uri, cohttp.lwt, core_lwt
Install: true
MainIs: start_server.ml

Executable readbin
Path: src/readbin
MainIs: readbin.ml
Expand Down
9 changes: 9 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ for i in "$@"; do
done

[ -f setup.ml -a setup.ml -nt _oasis ] || oasis -quiet setup


ocaml preconfig.ml

case `ocaml -vnum` in
4.01.*)
sed -i 's/mark_tag_used/ignore/' myocamlbuild.ml
;;
esac

ocaml setup.ml -configure "$@"
1 change: 1 addition & 0 deletions lib/bap_disasm/bap_disasm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type reg = Reg.t with bin_io, compare, sexp
type imm = Imm.t with bin_io, compare, sexp
type fmm = Fmm.t with bin_io, compare, sexp
type (+'a,+'k) insn
type kind = Insn.Kind.t with bin_io, compare, sexp


(** ARM instruction set *)
Expand Down
4 changes: 2 additions & 2 deletions lib/bap_disasm/bap_disasm_arm_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Arm = Bap_disasm_arm
let (%:) name typ = Var.create name typ


let nil = Arm.Reg.to_string `nil %: reg32_t
let nil = Arm.Reg.to_string `Nil %: reg32_t

let make_register reg ty = Arm.Reg.to_string reg %: ty
let reg32 reg = make_register reg reg32_t
Expand Down Expand Up @@ -80,7 +80,7 @@ let var_of_ccr : Arm.Reg.ccr -> var = function
| `ITSTATE -> itstate

let of_reg : Arm.Reg.t -> var = function
| `nil -> nil
| `Nil -> nil
| #Arm.Reg.gpr as reg -> var_of_gpr reg
| #Arm.Reg.ccr as reg -> var_of_ccr reg

Expand Down
2 changes: 1 addition & 1 deletion lib/bap_disasm/bap_disasm_arm_lifter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ let lift_mem ops insn =

(* POS_SIGN_BIT *)
| `LDRSHTi, [|dest1; _unknown; base; Imm imm_off; cond; _|] ->
let offset = Mem_shift.mem_offset_reg_or_imm_pos (Op.Reg `nil) imm_off in
let offset = Mem_shift.mem_offset_reg_or_imm_pos (Op.Reg `Nil) imm_off in
let insns =
Mem_shift.lift_r_exp ~dest1 ~base ~offset PostIndex Signed H Ld
in
Expand Down
2 changes: 1 addition & 1 deletion lib/bap_disasm/bap_disasm_arm_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ with bin_io, compare, sexp, enumerate


module Reg = struct
type nil = [ `nil ]
type nil = [ `Nil ]
with bin_io, compare, sexp, enumerate

(** General purpose registers *)
Expand Down
35 changes: 21 additions & 14 deletions lib/bap_disasm/bap_disasm_basic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type mem = Mem.t with sexp_of
type kind = Kind.t with compare, sexp

type pred = [
| `valid
| `Valid
| kind
] with sexp,compare

Expand Down Expand Up @@ -88,7 +88,7 @@ module Reg = struct
let data =
let reg_code = C.insn_op_reg_code dis.id ~insn ~oper in
let reg_name =
if reg_code = 0 then lazy "nil"
if reg_code = 0 then lazy "Nil"
else
let off = C.insn_op_reg_name dis.id ~insn ~oper in
lazy (Table.lookup dis.reg_table off) in
Expand Down Expand Up @@ -215,17 +215,17 @@ with bin_io, compare, sexp


let cpred_of_pred : pred -> C.pred = function
| `valid -> C.Is_true
| `conditional_branch -> C.Is_conditional_branch
| `unconditional_branch -> C.Is_unconditional_branch
| `indirect_branch -> C.Is_indirect_branch
| `return -> C.Is_return
| `call -> C.Is_call
| `barrier -> C.Is_barrier
| `terminator -> C.Is_terminator
| `may_affect_control_flow -> C.May_affect_control_flow
| `may_store -> C.May_store
| `may_load -> C.May_load
| `Valid -> C.Is_true
| `Conditional_branch -> C.Is_conditional_branch
| `Unconditional_branch -> C.Is_unconditional_branch
| `Indirect_branch -> C.Is_indirect_branch
| `Return -> C.Is_return
| `Call -> C.Is_call
| `Barrier -> C.Is_barrier
| `Terminator -> C.Is_terminator
| `May_affect_control_flow -> C.May_affect_control_flow
| `May_store -> C.May_store
| `May_load -> C.May_load

module Insn = struct
type ins_info = {
Expand Down Expand Up @@ -291,6 +291,13 @@ let compare_insn (i1 : ('a,'b) insn) (i2 : ('a,'b) insn) =
Insn.compare i1 i2
let sexp_of_insn : ('a,'b) insn -> Sexp.t = Insn.sexp_of_t


type full_insn = (asm,kinds) insn

let sexp_of_full_insn = sexp_of_insn



type (+'a,+'k) insns = (mem * ('a,'k) insn option) list

module Pred = struct
Expand Down Expand Up @@ -473,7 +480,7 @@ let insn_of_mem dis mem =
if Mem.(max_addr mem' = max_addr mem) then Ok `finished
else Mem.view mem ~from:Addr.(Mem.max_addr mem' ++ 1)
>>| fun r -> `left r in
run ~stop_on:[`valid] dis mem ~return ~init
run ~stop_on:[`Valid] dis mem ~return ~init
~hit:(fun s mem' insn _ ->
split mem' >>= fun r -> stop s (mem',Some insn,r))
~invalid:(fun s mem' _ ->
Expand Down
25 changes: 13 additions & 12 deletions lib/bap_disasm/bap_disasm_basic.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type kind = Bap_insn_kind.t with compare, sexp

(** predicate to drive the disassembler *)
type pred = [
| `valid (** stop on first valid insn *)
| `Valid (** stop on first valid insn *)
| kind (** stop on first insn of the specified kind *)
] with sexp

Expand All @@ -22,6 +22,11 @@ type imm with bin_io, compare, sexp
type fmm with bin_io, compare, sexp
type (+'a,+'k) insn
type (+'a,+'k) insns = (mem * ('a,'k) insn option) list
type empty (** set when information is not stored *)
type asm (** set when assembler information is stored *)
type kinds (** set when instruction kind information is stored *)

type full_insn = (asm,kinds) insn with sexp_of



Expand All @@ -43,13 +48,10 @@ type (+'a,+'k) insns = (mem * ('a,'k) insn option) list
store extra information about instruction kind.
Note: at some points you can have an access to this information
even if you don't enable it explicitely.
even if you don't enable it explicitly.
*)
type ('a,'k) t

type empty (** set when information is not stored *)
type asm (** set when assembler information is stored *)
type kinds (** set when instruction kind information is stored *)


(** Disassembler state.
Expand Down Expand Up @@ -109,15 +111,14 @@ val store_kinds : ('a,_) t -> ('a,kinds) t
monad, like [Or_error], or [Lwt]. Otherwise, just use [ident]
function and assume that ['s == 'r].
In a process of disassembly three user provided callbacks are
invoked by the engine. To each callback at least two parameters
are passed: [state] and [user_data]. [user_data] is arbitrary data
of type ['s] with which the folding over the memory is actually
In a process of disassembly user provided callbacks are invoked by
the engine. To each callback at least two parameters are passed:
[state] and [user_data]. [user_data] is arbitrary data of type ['s]
with which the folding over the memory is actually
performed. [state] incapsulates the current state of the
disassembler, and provides continuation functions, namely [stop],
[next] and [back], that drives the process of
disassembly. This functions are used to pass control back to the
disassembler.
[next] and [back], that drives the process of disassembly. This
functions are used to pass control back to the disassembler.
[stopped state user_data] is called when there is no more data to
disassemble. This handler is optional and defaults to [stop].
Expand Down
28 changes: 14 additions & 14 deletions lib/bap_disasm/bap_insn_kind.ml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
type branch = [
| `conditional_branch
| `unconditional_branch
| `indirect_branch
] with compare, enumerate, sexp
| `Conditional_branch
| `Unconditional_branch
| `Indirect_branch
] with bin_io, compare, enumerate, sexp

type affecting_control = [
| branch
| `return
| `call
| `barrier
| `terminator
| `may_affect_control_flow
] with compare, enumerate, sexp
| `Return
| `Call
| `Barrier
| `Terminator
| `May_affect_control_flow
] with bin_io, compare, enumerate, sexp

type having_side_effect = [
| `may_load
| `may_store
] with compare, enumerate, sexp
| `May_load
| `May_store
] with bin_io, compare, enumerate, sexp

type t = [
| affecting_control
| having_side_effect
] with compare, enumerate, sexp
] with bin_io, compare, enumerate, sexp
Loading

0 comments on commit 5c88397

Please sign in to comment.