Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrites x86 abi using the new infrastructure #1505

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ testsuite:
git clone https://github.com/BinaryAnalysisPlatform/bap-testsuite.git testsuite

check: testsuite
make REVISION=c40b332290bab -C testsuite
make REVISION=f0043aaa2cc -C testsuite

.PHONY: indent check-style status-clean

Expand Down
31 changes: 14 additions & 17 deletions lib/bap_c/bap_c_abi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,14 @@ module Arg = struct

let concat = List.reduce_exn ~f:Bil.concat

let registers ?limit file t =
let registers ?(rev=false) ?limit file t =
let* s = Arg.get () in
let* bits = size t in
let* regs_needed = registers_needed file bits in
let limit = Option.value limit ~default:regs_needed in
require (regs_needed <= limit) >>= fun () ->
let* args = Arena.popn ~n:regs_needed s file in
push_arg t @@ concat args
push_arg t @@ concat (if rev then List.rev args else args)

let align_even file =
let* s = Arg.get () in
Expand All @@ -556,15 +556,7 @@ module Arg = struct

let reference file t =
with_hidden @@ fun () ->
register file (`Pointer C.Type.Spec.{
t;
attrs = [];
qualifier = C.Type.Qualifier.{
const = false;
volatile = false;
restrict = false;
}
})
register file (C.Type.pointer t)

let update_stack f =
let* s = Arg.get () in
Expand All @@ -583,8 +575,16 @@ module Arg = struct
let* bits = size t in
update_stack @@ Stack.add t (data s.ruler t) bits

let hidden t =
with_hidden @@ fun () ->
memory (C.Type.pointer t)

let skip_memory bits = update_stack @@ Stack.skip bits

let rebase slots =
let* {target} = Arg.get () in
skip_memory (slots * Theory.Target.data_addr_size target)

let load t bits sp base =
let mem = Var.reify (Theory.Target.data t) in
let width = Theory.Target.data_addr_size t in
Expand Down Expand Up @@ -698,12 +698,9 @@ end

let define target ruler pass =
let open Bap_core_theory in
let target_name = Theory.Target.name target in
let abi_name =
let abi = Theory.Target.abi target in
if Theory.Abi.(abi = unknown)
then Format.asprintf "%a-unknown" KB.Name.pp target_name
else Format.asprintf "%a" KB.Name.pp target_name in
let abi = Theory.Target.abi target in
let abi_name = Format.asprintf "%s"
(KB.Name.unqualified (Theory.Abi.name abi)) in
let abi_processor = {
apply_attrs = (fun _ x -> x);
insert_args = fun _ attrs proto ->
Expand Down
25 changes: 22 additions & 3 deletions lib/bap_c/bap_c_abi.mli
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,13 @@ module Arg : sig
Rejects the computation if [arena] doesn't have the necessary
number of registers; the number of required registers is greater
than [limit]; or if the size of [t] is unknown.

If [rev] is true, then the allocated registers will be used in
the reversed order.

@since 2.5.0 accepts the optional [rev] parameter.
*)
val registers : ?limit:int -> arena -> ctype -> unit t
val registers : ?rev:bool -> ?limit:int -> arena -> ctype -> unit t


(** [align_even arena] ensures that the first available register in
Expand Down Expand Up @@ -312,6 +317,15 @@ module Arg : sig
pointer role. The size of [t] is not required. *)
val reference : arena -> ctype -> unit t


(** [hidden t] passes the argument of type [t] as a pointer
to [t] via the first available stack slot.

The computation is rejected if the target doesn't have a stack.

@since 2.5.0 *)
val hidden : ctype -> unit t

(** [memory t] passes the argument of type [t] in the next
available stack slot.

Expand All @@ -333,6 +347,11 @@ module Arg : sig
*)
val memory : ctype -> unit t

(** [rebase off] rebases the stack position by [n] words.

@since 2.5.0
*)
val rebase : int -> unit t

(** [split a1 a2 t] passes the lower half of the value
via registers in the arena [a1] and the higher via the registers
Expand Down Expand Up @@ -387,12 +406,12 @@ module Arg : sig

(** [size t] is the size in bits of an object of type [t].

The computation is rejected if the size is unknown.
The computation is rejected if the size is unknown, i.e., the
type is incomplete.

@since 2.5.0 *)
val size : ctype -> int t


(** [require cnd] rejects the computation if [cnd] doesn't hold.

@since 2.5.0 *)
Expand Down
2 changes: 2 additions & 0 deletions lib/bap_c/bap_c_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ let is_restrict : t -> Bool.t = function
| `Pointer {qualifier={restrict}} -> restrict


let is_void = function `Void -> true | _ -> false

let qualifier ?(const=false) ?(volatile=false) restrict =
Qualifier.{const; volatile; restrict}

Expand Down
4 changes: 4 additions & 0 deletions lib/bap_c/bap_c_type.mli
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ val is_volatile : t -> Bool.t
val is_restrict : t -> Bool.t


(** [is_void t] true iff [t] is [`Void] *)
val is_void : t -> Bool.t


(** {2 Basic Types} *)

(** [basic x] constructs a basic type.
Expand Down
2 changes: 2 additions & 0 deletions lib/x86_cpu/.merlin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REC
B ../bap_demangle
Loading