Unverified Commit a8fb540b by Melvin Klimke Committed by GitHub

Refactored Cconv

parent 4e30a70d
......@@ -2,6 +2,7 @@
(section etc)
(files
(src/config.json as config.json)
(src/utils/registers.json as registers.json)
)
)
......
......@@ -2,6 +2,10 @@ open Bap.Std
open Core_kernel
(** Returns a json object for registers.json. If not yet in memory, the json file is read. *)
val get_json: unit -> Yojson.Basic.t
(** Returns whether a variable is callee saved according to the calling convention
of the target architecture. Should only used for calls to functions outside
of the program, not for calls between functions inside the program. *)
......@@ -22,3 +26,24 @@ val is_return_register: Var.t -> Project.t -> Bool.t
TODO: Since we do not do name demangling here, check whether bap name demangling
yields different function names for the symbols. *)
val parse_dyn_syms: Project.t -> String.Set.t
(** Returns a string list of supported architectures from the registers.json. *)
val get_supported_architectures : unit -> string list
(** Calls objdump with customisable flag and error message. Returns output lines as string list. *)
val call_objdump : Project.t -> flag:string -> err:string -> string list
(** Infers the binary format using the file's symbol table. *)
val infer_bin_format_from_symbols : Project.t -> string
(** Returns the binary format by either an objdump call or via the file's symbol table. *)
val extract_bin_format : Project.t -> string
(** Returns a list of registers based on the file's binary format, architecture,
calling_convention and context (e.g. callee saved, parameter etc.) *)
val get_register_list : Project.t -> string -> string list
......@@ -10,23 +10,43 @@ An example looks like this:
"memcpy", "wmemcpy", "memmove", "wmemmove", "memcmp", "wmemcmp"],
"_comment": "any function that takes something of type size_t could be a possible candidate."
}, *)
let get_symbols_from_json json cwe =
let get_symbols_from_json (json : Yojson.Basic.t) cwe =
[json]
|> filter_member cwe
|> filter_member "symbols"
|> flatten
|> List.map ~f:to_string
let get_symbol_lists_from_json json cwe =
let get_symbol_lists_from_json (json : Yojson.Basic.t) cwe =
[json]
|> filter_member cwe
|> filter_member "pairs"
|> flatten
|> List.map ~f:(fun l -> List.map (to_list l) ~f:to_string)
let get_parameter_list_from_json json cwe =
let get_parameter_list_from_json (json : Yojson.Basic.t) cwe =
[json]
|> filter_member cwe
|> filter_member "parameters"
|> flatten
|> List.map ~f:to_string
let get_arch_from_json (bin_format : Yojson.Basic.t) ?(conv : string = "") (arch : string) : Yojson.Basic.t =
match arch with
| "x86" -> bin_format |> member arch |> member conv
| _ -> bin_format |> member arch
let get_bin_format_from_json (json : Yojson.Basic.t) (bin_format : string) : Yojson.Basic.t =
json |> member bin_format
let get_registers_from_json (arch : Yojson.Basic.t) (context : string) : string list =
arch |> member context |> to_list |> filter_string
let get_arch_list_from_json (json : Yojson.Basic.t) (bin_format : string) : (string * Yojson.Basic.t) list =
json |> member bin_format |> to_assoc
(* This module implements functionality related to parsing the JSON configuration file. *)
(** Returns pairs of symbols for a given CWE check. *)
val get_symbol_lists_from_json : Yojson.Basic.t -> string -> string list list
(** Returns symbols for a given CWE check. *)
val get_symbols_from_json : Yojson.Basic.t -> string -> string list
(** Returns parameters for a given CWE check. *)
val get_parameter_list_from_json : Yojson.Basic.t -> string -> string list
(** Returns an architecture's registers based on calling convention. *)
val get_arch_from_json : Yojson.Basic.t -> ?conv:string -> string -> Yojson.Basic.t
(** Returns json object containing either PE or ELF calling conventions for all architectures. *)
val get_bin_format_from_json : Yojson.Basic.t -> string -> Yojson.Basic.t
(** Returns registers for a given architecture and calling_convention specified by context. (e.g. callee saved, parameter etc.) *)
val get_registers_from_json : Yojson.Basic.t -> string -> string list
(** Returns a list of all architectures supported for a given binary format. (e.g. ELF) *)
val get_arch_list_from_json : Yojson.Basic.t -> string -> (string * Yojson.Basic.t) list
......@@ -13,8 +13,12 @@ let test_callee_saved () =
let project = Option.value_exn !example_project in
let register = Var.create "RBX" (Bil.Imm (Symbol_utils.arch_pointer_size_in_bytes project * 8)) in
let () = check "callee_saved_register" (is_callee_saved register project) in
let register = Var.create "R15" (Bil.Imm (Symbol_utils.arch_pointer_size_in_bytes project * 8)) in
let () = check "callee_saved_register" (is_callee_saved register project) in
let register = Var.create "RAX" (Bil.Imm (Symbol_utils.arch_pointer_size_in_bytes project * 8)) in
let () = check "caller_saved_register" (is_callee_saved register project = false) in
let register = Var.create "R8" (Bil.Imm (Symbol_utils.arch_pointer_size_in_bytes project * 8)) in
let () = check "caller_saved_register" (is_callee_saved register project = false) in
()
let test_parameter_register () =
......@@ -22,8 +26,12 @@ let test_parameter_register () =
let project = Option.value_exn !example_project in
let register = Var.create "RDX" (Bil.Imm (Symbol_utils.arch_pointer_size_in_bytes project * 8)) in
let () = check "return_register" (is_parameter_register register project) in
let register = Var.create "R9" (Bil.Imm (Symbol_utils.arch_pointer_size_in_bytes project * 8)) in
let () = check "return_register" (is_parameter_register register project) in
let register = Var.create "RAX" (Bil.Imm (Symbol_utils.arch_pointer_size_in_bytes project * 8)) in
let () = check "no_return_register" (is_parameter_register register project = false) in
let register = Var.create "R14" (Bil.Imm (Symbol_utils.arch_pointer_size_in_bytes project * 8)) in
let () = check "no_return_register" (is_parameter_register register project = false) in
()
let test_return_register () =
......@@ -35,6 +43,7 @@ let test_return_register () =
let () = check "no_return_register" (is_return_register register project = false) in
()
let test_parse_dyn_syms () =
(* this test assumes, that the example project is the arrays_x64.out binary from the artificial samples. *)
let project = Option.value_exn !example_project in
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment