Skip to content

Commit 398a301

Browse files
committed
CHB: add support for arrays of function pointers
1 parent 3fea317 commit 398a301

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

CodeHawk/CHB/bchlib/bCHBCTypeUtil.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,19 @@ let get_element_type (t: btype_t) =
243243
raise (BCH_failure (LBLOCK [STR "Not an array type"]))
244244

245245

246+
let get_array_length (t: btype_t): int traceresult =
247+
match t with
248+
| TArray (_, Some len, _) ->
249+
(match len with
250+
| Const (CInt (i64, _, _)) -> Ok (Int64.to_int i64)
251+
| _ ->
252+
Error ["Array does not have a constant length: " ^ (exp_to_string len)])
253+
| TArray _ ->
254+
Error ["Array does not have a length"]
255+
| _ ->
256+
Error ["get_array_length: not an array: " ^ (btype_to_string t)]
257+
258+
246259
(* ======================================================= size and alignment *)
247260

248261
let resolve_type (btype: btype_t) = bcfiles#resolve_type btype
@@ -1177,6 +1190,7 @@ let struct_field_categories (ty: btype_t): string list =
11771190
| Error e -> e
11781191
| Ok ty ->
11791192
match ty with
1193+
| TArray (TComp (ckey, _), _, _)
11801194
| TPtr (TPtr (TComp (ckey, _), _), _)
11811195
| TPtr (TComp (ckey, _), _) ->
11821196
let compinfo = bcfiles#get_compinfo ckey in
@@ -1187,6 +1201,8 @@ let struct_field_categories (ty: btype_t): string list =
11871201
| TPtr (TFun _, _) -> "address"
11881202
| _ -> "unknown") compinfo.bcfields
11891203

1204+
| TArray ((TFun _ | TPtr (TFun _, _)), _, _) -> ["address"]
1205+
11901206
| rty -> [btype_to_string ty; btype_to_string rty]
11911207

11921208

CodeHawk/CHB/bchlib/bCHBCTypeUtil.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ val size_of_int_ikind: ikind_t -> int
170170

171171
val size_of_float_fkind: fkind_t -> int
172172

173+
(** [get_array_length ty] returns the length (number of elements) of an array.
174+
175+
An error value is returned if the array does not have a constant length, or
176+
does not have a length at all, of if the type is not an array. *)
177+
val get_array_length: btype_t -> int traceresult
178+
173179
(** [size_of_btype ty] returns the size (in bytes) of type [ty].
174180
175181
An error value is returned if the size cannot be determined. This may

CodeHawk/CHB/bchlib/bCHCallbackTables.ml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,21 @@ object (self)
192192
let compinfo = bcfiles#get_compinfo ckey in
193193
List.iteri (fun i fld ->
194194
H.add table (i * 4) fld.bfname) compinfo.bcfields
195+
| TArray (TComp (ckey, _), _, _) ->
196+
let compinfo = bcfiles#get_compinfo ckey in
197+
List.iteri (fun i fld ->
198+
H.add table (i * 4) fld.bfname) compinfo.bcfields
199+
| TArray ((TFun _ | TPtr (TFun _, _)), _, _) ->
200+
H.add table 0 ("cbp_" ^ cba)
195201
| _ ->
196-
raise
197-
(BCH_failure
198-
(LBLOCK [
199-
STR "Unexpected type in creating callback table: ";
200-
btype_to_pretty recty])) in
202+
let msg =
203+
LBLOCK [
204+
STR "Unexpected type in creating callback table: ";
205+
btype_to_pretty recty] in
206+
begin
207+
ch_error_log#add "call-back-table problem" msg;
208+
raise (BCH_failure msg)
209+
end in
201210
table
202211

203212
val offsettypes =
@@ -214,9 +223,12 @@ object (self)
214223
let _ =
215224
match recty with
216225
| TFun _ -> H.add table 0 ty
217-
| TPtr (TFun (rty, args, b, attr), _) ->
226+
| TPtr (TFun (rty, args, b, attr), _)
227+
| TArray (TFun (rty, args, b, attr), _, _)
228+
| TArray (TPtr (TFun (rty, args, b, attr), _), _, _) ->
218229
H.add table 0 (TFun (rty, args, b, attr))
219-
| TPtr (TComp (ckey, _), _) ->
230+
| TArray (TComp (ckey, _), _, _)
231+
| TPtr (TComp (ckey, _), _) ->
220232
let compinfo = bcfiles#get_compinfo ckey in
221233
List.iteri (fun i fld ->
222234
let offset = i * 4 in

CodeHawk/CHB/bchlib/bCHVersion.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ end
9595

9696

9797
let version = new version_info_t
98-
~version:"0.6.0_20241119"
99-
~date:"2024-11-19"
98+
~version:"0.6.0_20241125"
99+
~date:"2024-11-25"
100100
~licensee: None
101101
~maxfilesize: None
102102
()

CodeHawk/CHB/bchlibelf/bCHELFHeader.ml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,15 @@ object(self)
558558
system_info#initialize_jumptables system_info#is_code_address xstrings
559559

560560
method private extract_call_back_table
561+
?(len=None)
561562
(callbacktable: call_back_table_int)
562563
(va: doubleword_int)
563564
(fieldkinds: string list) =
564565
let nullrecord = ref false in
566+
let count = ref 0 in
567+
let bound = match len with Some len -> len | _ -> BCHDoubleword.e15 in
565568
let currva = ref va in
566-
while not !nullrecord do
569+
while not !nullrecord && (!count < bound) do
567570
let cbvalues = ref [] in
568571
begin
569572
List.iteri (fun i s ->
@@ -582,7 +585,8 @@ object(self)
582585
| _ -> CBTag "**unknown**")
583586
| "value" -> CBValue (mkNumerical pv#to_int)
584587
| _ -> CBValue numerical_zero in
585-
cbvalues := ((i * 4), cbv) :: !cbvalues) fieldkinds;
588+
cbvalues := ((i * 4), cbv) :: !cbvalues) fieldkinds;
589+
count := !count + 1;
586590
(if List.for_all (fun (_, v) ->
587591
match v with
588592
| CBValue n -> n#equal numerical_zero || n#equal (mkNumerical (-1))
@@ -649,7 +653,12 @@ object(self)
649653
let fieldkinds = struct_field_categories varinfo.bvtype in
650654
let callbacktable = callbacktables#new_table addr varinfo.bvtype in
651655
let va = TR.tget_ok (string_to_doubleword addr) in
652-
self#extract_call_back_table callbacktable va fieldkinds
656+
if is_array_type varinfo.bvtype then
657+
let len = get_array_length varinfo.bvtype in
658+
self#extract_call_back_table
659+
~len:(TR.to_option len) callbacktable va fieldkinds
660+
else
661+
self#extract_call_back_table callbacktable va fieldkinds
653662
else
654663
chlog#add
655664
"call-back-table-variable"

0 commit comments

Comments
 (0)