Skip to content

Commit 628ce33

Browse files
committed
CHB: stack variable introductions
1 parent 0291bd7 commit 628ce33

File tree

2 files changed

+90
-9
lines changed

2 files changed

+90
-9
lines changed

CodeHawk/CHB/bchlib/bCHFunctionData.ml

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ let stackvar_intro_to_string (svi: stackvar_intro_t) =
7878

7979
let function_annotation_to_string (a: function_annotation_t) =
8080
(String.concat "\n" (List.map regvar_intro_to_string a.regvarintros))
81+
^ "\n"
8182
^ (String.concat "\n" (List.map stackvar_intro_to_string a.stackvarintros))
8283

8384

@@ -174,6 +175,16 @@ object (self)
174175
None a.regvarintros
175176
| _ -> None
176177

178+
method get_stackvar_intro (offset: int): stackvar_intro_t option =
179+
match self#get_function_annotation with
180+
| Some a ->
181+
List.fold_left (fun acc svi ->
182+
match acc with
183+
| Some _ -> acc
184+
| _ -> if svi.svi_offset = offset then Some svi else None)
185+
None a.stackvarintros
186+
| _ -> None
187+
177188
method has_regvar_type_annotation (iaddr: doubleword_int): bool =
178189
match self#get_function_annotation with
179190
| Some a ->
@@ -182,13 +193,28 @@ object (self)
182193
a.regvarintros
183194
| _ -> false
184195

196+
method has_stackvar_type_annotation (offset: int): bool =
197+
match self#get_function_annotation with
198+
| Some a ->
199+
List.exists
200+
(fun svi -> svi.svi_offset = offset && Option.is_some svi.svi_vartype)
201+
a.stackvarintros
202+
| _ -> false
203+
185204
method has_regvar_type_cast (iaddr: doubleword_int): bool =
186205
match self#get_function_annotation with
187206
| Some a ->
188207
List.exists
189208
(fun rvi -> rvi.rvi_iaddr#equal iaddr && rvi.rvi_cast) a.regvarintros
190209
| _ -> false
191210

211+
method has_stackvar_type_cast (offset: int): bool =
212+
match self#get_function_annotation with
213+
| Some a ->
214+
List.exists
215+
(fun svi -> svi.svi_offset = offset && svi.svi_cast) a.stackvarintros
216+
| _ -> false
217+
192218
method get_regvar_type_annotation (iaddr: doubleword_int): btype_t traceresult =
193219
let opttype =
194220
match self#get_function_annotation with
@@ -221,6 +247,39 @@ object (self)
221247
__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
222248
^ "No register var annotation found at " ^ iaddr#to_hex_string]
223249

250+
method get_stackvar_type_annotation (offset: int): btype_t traceresult =
251+
let opttype =
252+
match self#get_function_annotation with
253+
| None ->
254+
Some
255+
(Error [
256+
__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
257+
^ "Function " ^ faddr#to_hex_string ^ " does not have annotations"])
258+
| Some a ->
259+
List.fold_left
260+
(fun acc svi ->
261+
match acc with
262+
| Some _ -> acc
263+
| _ ->
264+
if svi.svi_offset = offset then
265+
match svi.svi_vartype with
266+
| Some t -> Some (Ok t)
267+
| _ ->
268+
Some
269+
(Error [
270+
__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
271+
^ "Stack var annotation at offset "
272+
^ (string_of_int offset)
273+
^ " does not have a type"])
274+
else
275+
acc) None a.stackvarintros in
276+
match opttype with
277+
| Some r -> r
278+
| None ->
279+
Error [
280+
__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
281+
^ "No stackvar annotation found at offset " ^ (string_of_int offset)]
282+
224283
method add_inlined_block (baddr:doubleword_int) =
225284
inlined_blocks <- baddr :: inlined_blocks
226285

@@ -504,31 +563,33 @@ let read_xml_stackvar_intro (node: xml_element_int): stackvar_intro_t traceresul
504563
else if not (has "name") then
505564
Error ["stackvar intro without name"]
506565
else
507-
let svi_offset = geti "offset" in
566+
let svi_offset = (-(geti "offset")) in
508567
let svi_name = get "name" in
509-
let svi_vartype =
568+
let (svi_vartype, svi_cast) =
510569
if has "typename" then
511570
let typename = get "typename" in
571+
let iscast = (has "cast") && ((get "cast") = "yes") in
512572
TR.tfold
513573
~ok:(fun btype ->
514574
if has "ptrto" && (get "ptrto") = "yes" then
515-
Some (t_ptrto btype)
575+
(Some (t_ptrto btype), iscast)
516576
else if has "arraysize" then
517577
let arraysize = geti "arraysize" in
518-
Some (t_array btype arraysize)
578+
(Some (t_array btype arraysize), iscast)
519579
else
520-
Some btype)
580+
(Some btype, iscast))
521581
~error:(fun e ->
522582
begin
523583
log_error_result __FILE__ __LINE__ e;
524-
None
584+
(None, false)
525585
end)
526586
(convert_string_to_type typename)
527587
else
528-
None in
588+
(None, false) in
529589
Ok {svi_offset = svi_offset;
530590
svi_name = svi_name;
531-
svi_vartype = svi_vartype}
591+
svi_vartype = svi_vartype;
592+
svi_cast = svi_cast}
532593

533594

534595
let read_xml_function_annotation (node: xml_element_int) =

CodeHawk/CHB/bchlib/bCHLibTypes.mli

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,8 @@ type regvar_intro_t = {
15071507
type stackvar_intro_t = {
15081508
svi_offset: int;
15091509
svi_name: string;
1510-
svi_vartype: btype_t option
1510+
svi_vartype: btype_t option;
1511+
svi_cast: bool
15111512
}
15121513

15131514

@@ -1551,7 +1552,9 @@ class type function_data_int =
15511552
method get_function_name: string (* demangled or combination of all names *)
15521553
method get_function_annotation: function_annotation_t option
15531554
method get_regvar_type_annotation: doubleword_int -> btype_t traceresult
1555+
method get_stackvar_type_annotation: int -> btype_t traceresult
15541556
method get_regvar_intro: doubleword_int -> regvar_intro_t option
1557+
method get_stackvar_intro: int -> stackvar_intro_t option
15551558
method get_inlined_blocks: doubleword_int list
15561559
method get_function_type: btype_t
15571560
method get_path_contexts: (string * string list) list
@@ -1562,6 +1565,8 @@ class type function_data_int =
15621565
method has_function_annotation: bool
15631566
method has_regvar_type_annotation: doubleword_int -> bool
15641567
method has_regvar_type_cast: doubleword_int -> bool
1568+
method has_stackvar_type_annotation: int -> bool
1569+
method has_stackvar_type_cast: int -> bool
15651570
method has_class_info: bool
15661571
method has_callsites: bool
15671572
method has_path_contexts: bool
@@ -4629,6 +4634,13 @@ class type function_environment_int =
46294634
-> numerical_t
46304635
-> variable_t
46314636

4637+
(** [mk_basevar_memory_variable offset] returns a memory variable with
4638+
[basevar] as base variable and offset [offset].
4639+
4640+
If [basevar] is not a valid base variable an error is returned.*)
4641+
method mk_basevar_memory_variable:
4642+
?size:int -> variable_t -> memory_offset_t -> variable_t traceresult
4643+
46324644
(** [mk_offset_memory_variable memref memoff] returns a memory variable
46334645
with [memref] as basis and a generic memory offset.
46344646
@@ -5919,6 +5931,14 @@ class type floc_int =
59195931
*)
59205932
method decompose_address: xpr_t -> (memory_reference_int * memory_offset_t)
59215933

5934+
method convert_value_offsets:
5935+
?size:int option -> variable_t -> variable_t traceresult
5936+
5937+
method convert_variable_offsets:
5938+
?size:int option -> variable_t -> variable_t traceresult
5939+
5940+
method convert_xpr_offsets: ?size:int option -> xpr_t -> xpr_t traceresult
5941+
59225942
(* returns the variable associated with the address expression *)
59235943
method get_lhs_from_address: xpr_t -> variable_t
59245944

0 commit comments

Comments
 (0)