Skip to content

Commit 2e3112e

Browse files
committed
makes argument passing well-typed
Now the type of the argument and the type of its right-hand side agree and the argument type is derived from the C type, not C datum.
1 parent dbf22ef commit 2e3112e

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

lib/bap_c/bap_c_abi.ml

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ open Bap.Std
33
open Bap_c_type
44
open Monads.Std
55

6+
include Self()
7+
68
module Attrs = Bap_c_term_attributes
79

810
type ctype = t
@@ -94,13 +96,23 @@ let decay_arrays : proto -> proto = fun proto -> {
9496
args = List.Assoc.map ~f:array_to_pointer proto.args;
9597
}
9698

97-
let create_arg i addr_size intent name t (data,exp) sub =
98-
let typ = match data with
99-
| Bap_c_data.Imm (sz,_) -> Type.Imm (Size.in_bits sz)
100-
| _ -> Type.Imm (Size.in_bits addr_size) in
99+
let coerce ltyp rtyp exp = match ltyp,rtyp with
100+
| Type.Mem _,_| _,Type.Mem _
101+
| Type.Unk,_ | _, Type.Unk -> exp
102+
| Imm m, Imm n -> match Int.compare m n with
103+
| 0 -> exp
104+
| 1 -> Bil.(cast unsigned m exp)
105+
| _ -> Bil.(cast low m exp)
106+
107+
108+
let create_arg size i intent name t (data,exp) sub =
109+
let ltyp = match size#bits t with
110+
| None -> Type.imm (Size.in_bits size#pointer)
111+
| Some m -> Type.imm m in
112+
let rtyp = Type.infer_exn exp in
101113
let name = if String.is_empty name then sprintf "arg%d" (i+1) else name in
102-
let var = Var.create (Sub.name sub ^ "_" ^ name) typ in
103-
let arg = Arg.create ~intent var exp in
114+
let var = Var.create (Sub.name sub ^ "_" ^ name) ltyp in
115+
let arg = Arg.create ~intent var @@ coerce ltyp rtyp exp in
104116
let arg = Term.set_attr arg Attrs.data data in
105117
let arg = Term.set_attr arg Attrs.t t in
106118
arg
@@ -133,8 +145,6 @@ let get_prototype gamma name = match gamma name with
133145
}
134146

135147
let create_api_processor size abi : Bap_api.t =
136-
let addr_size = size#pointer in
137-
138148
let stage1 gamma = object(self)
139149
inherit Term.mapper as super
140150
method! map_sub sub =
@@ -161,18 +171,24 @@ let create_api_processor size abi : Bap_api.t =
161171
| Some {return; hidden; params} ->
162172
let params = List.mapi params ~f:(fun i a -> i,a) in
163173
List.map2 params t.Bap_c_type.Proto.args ~f:(fun (i,a) (n,t) ->
164-
create_arg i addr_size (arg_intent t) n t a sub) |>
174+
create_arg size i (arg_intent t) n t a sub) |>
165175
function
166-
| Unequal_lengths -> super#map_sub sub
176+
| Unequal_lengths ->
177+
error "The ABI processor generated an incorrect number of \
178+
argument terms for the subroutine %s: %d <> %d"
179+
(Sub.name sub)
180+
(List.length params)
181+
(List.length t.args);
182+
sub
167183
| Ok args ->
168184
let ret = match return with
169185
| None -> []
170186
| Some ret ->
171187
let t = t.Bap_c_type.Proto.return in
172-
[create_arg 0 addr_size Out "result" t ret sub] in
188+
[create_arg size 0 Out "result" t ret sub] in
173189
let hid = List.mapi hidden ~f:(fun i (t,a) ->
174190
let n = "hidden" ^ if i = 0 then "" else Int.to_string i in
175-
create_arg 0 addr_size Both n t a sub) in
191+
create_arg size 0 Both n t a sub) in
176192
List.fold (args@hid@ret) ~init:sub ~f:(Term.append arg_t)
177193

178194
end in
@@ -228,10 +244,6 @@ module Arg = struct
228244
module Data = Bap_c_data
229245
end
230246

231-
let next_multitude_of ~n x = (x + (n-1)) land (lnot (n-1))
232-
233-
234-
235247
module Stack : sig
236248
type t
237249

@@ -277,10 +289,10 @@ module Arg = struct
277289
(Theory.Target.data_addr_size target / 8) in
278290
let align = function
279291
| None ->
280-
next_multitude_of ~n:min_alignment
292+
C.Size.next_multitude_of ~n:min_alignment
281293
| Some {ctype} ->
282294
let m = Size.in_bytes (ruler#alignment ctype) in
283-
next_multitude_of ~n:(max min_alignment m) in
295+
C.Size.next_multitude_of ~n:(max min_alignment m) in
284296
match Theory.Target.reg target Theory.Role.Register.stack_pointer with
285297
| None -> None
286298
| Some sp -> Some {
@@ -344,7 +356,7 @@ module Arg = struct
344356
let align n self = match Map.min_elt self.args with
345357
| None -> None
346358
| Some (k,_) ->
347-
let k' = next_multitude_of ~n k in
359+
let k' = C.Size.next_multitude_of ~n k in
348360
if k = k' then Some (self,())
349361
else match Map.split self.args k' with
350362
| _,None,_ -> None

0 commit comments

Comments
 (0)