@@ -3,6 +3,8 @@ open Bap.Std
3
3
open Bap_c_type
4
4
open Monads.Std
5
5
6
+ include Self ()
7
+
6
8
module Attrs = Bap_c_term_attributes
7
9
8
10
type ctype = t
@@ -94,13 +96,23 @@ let decay_arrays : proto -> proto = fun proto -> {
94
96
args = List.Assoc. map ~f: array_to_pointer proto.args;
95
97
}
96
98
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
101
113
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
104
116
let arg = Term. set_attr arg Attrs. data data in
105
117
let arg = Term. set_attr arg Attrs. t t in
106
118
arg
@@ -133,8 +145,6 @@ let get_prototype gamma name = match gamma name with
133
145
}
134
146
135
147
let create_api_processor size abi : Bap_api.t =
136
- let addr_size = size#pointer in
137
-
138
148
let stage1 gamma = object (self )
139
149
inherit Term. mapper as super
140
150
method! map_sub sub =
@@ -161,18 +171,24 @@ let create_api_processor size abi : Bap_api.t =
161
171
| Some {return; hidden; params} ->
162
172
let params = List. mapi params ~f: (fun i a -> i,a) in
163
173
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) |>
165
175
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
167
183
| Ok args ->
168
184
let ret = match return with
169
185
| None -> []
170
186
| Some ret ->
171
187
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
173
189
let hid = List. mapi hidden ~f: (fun i (t ,a ) ->
174
190
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
176
192
List. fold (args@ hid@ ret) ~init: sub ~f: (Term. append arg_t)
177
193
178
194
end in
@@ -228,10 +244,6 @@ module Arg = struct
228
244
module Data = Bap_c_data
229
245
end
230
246
231
- let next_multitude_of ~n x = (x + (n-1 )) land (lnot (n-1 ))
232
-
233
-
234
-
235
247
module Stack : sig
236
248
type t
237
249
@@ -277,10 +289,10 @@ module Arg = struct
277
289
(Theory.Target. data_addr_size target / 8 ) in
278
290
let align = function
279
291
| None ->
280
- next_multitude_of ~n: min_alignment
292
+ C.Size. next_multitude_of ~n: min_alignment
281
293
| Some {ctype} ->
282
294
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
284
296
match Theory.Target. reg target Theory.Role.Register. stack_pointer with
285
297
| None -> None
286
298
| Some sp -> Some {
@@ -344,7 +356,7 @@ module Arg = struct
344
356
let align n self = match Map. min_elt self.args with
345
357
| None -> None
346
358
| Some (k ,_ ) ->
347
- let k' = next_multitude_of ~n k in
359
+ let k' = C.Size. next_multitude_of ~n k in
348
360
if k = k' then Some (self,() )
349
361
else match Map. split self.args k' with
350
362
| _ ,None ,_ -> None
0 commit comments