Skip to content

Commit 568d90d

Browse files
committed
CHB: recognize function signature with varargs and printf/scanf format attribute
1 parent 4afc104 commit 568d90d

7 files changed

Lines changed: 140 additions & 11 deletions

File tree

CodeHawk/CHB/bchlib/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ XPRLIB = $(CODEHAWK)/CH/xprlib
1010

1111
CAMLDOC := ocamldoc
1212

13-
CAMLC := ocamlopt -I str -I cmi -I cmx \
13+
CAMLC := ocamlopt -I str -I cmi -I cmx \
1414
-I $(EXTLIB) \
1515
-I $(ZIPLIB) \
1616
-I $(ZARITHLIB) \
@@ -23,13 +23,13 @@ OCAMLDEP := ocamldep
2323

2424
MLIS := \
2525
bCHBCTypes \
26-
bCHBCAttributes \
2726
bCHBCSumTypeSerializer \
2827
bCHBCTypeTransformer \
2928
bCHLibTypes \
3029
bCHVersion \
3130
bCHXprUtil \
3231
bCHUtilities \
32+
bCHBCAttributesUtil \
3333
bCHSystemSettings \
3434
bCHCPURegisters \
3535
bCHBasicTypes \
@@ -82,6 +82,7 @@ MLIS := \
8282
bCHPrecondition \
8383
bCHPostcondition \
8484
bCHSideeffect \
85+
bCHBCAttributes \
8586
bCHFunctionSemantics \
8687
bCHFunctionSummary \
8788
bCHVariable \
@@ -120,6 +121,7 @@ SOURCES := \
120121
bCHVersion \
121122
bCHXprUtil \
122123
bCHUtilities \
124+
bCHBCAttributesUtil \
123125
bCHBasicTypes \
124126
bCHDoubleword \
125127
bCHSystemSettings \

CodeHawk/CHB/bchlib/bCHARMFunctionInterface.ml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
66
7-
Copyright (c) 2023-2024 Aarno Labs LLC
7+
Copyright (c) 2023-2026 Aarno Labs LLC
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal
@@ -410,6 +410,7 @@ let get_int_paramlocpart_next_state
410410

411411

412412
let get_arm_int_param_next_state
413+
?(fmt=NoFormat)
413414
(size: int)
414415
(name: string)
415416
(btype: btype_t)
@@ -419,7 +420,7 @@ let get_arm_int_param_next_state
419420
| Some reg ->
420421
let register = register_of_arm_register reg in
421422
let par: fts_parameter_t =
422-
mk_indexed_register_parameter ~btype ~name ~size register index in
423+
mk_indexed_register_parameter ~fmt ~btype ~name ~size register index in
423424
let ncr = get_next_core_reg reg in
424425
let naas =
425426
match ncr with
@@ -434,7 +435,7 @@ let get_arm_int_param_next_state
434435
(match aa_state.aas_next_offset with
435436
| Some offset ->
436437
let par: fts_parameter_t =
437-
mk_indexed_stack_parameter ~btype ~name offset index in
438+
mk_indexed_stack_parameter ~fmt ~btype ~name offset index in
438439
let naas =
439440
{aa_state with aas_next_offset = Some (offset + size)} in
440441
(par, naas)
@@ -577,7 +578,14 @@ let get_arm_struct_param_next_state
577578
(param, naas)
578579

579580

580-
let arm_vfp_params (funargs: bfunarg_t list): fts_parameter_t list =
581+
let arm_vfp_params
582+
?(attrs=[])
583+
?(varargs=false)
584+
(funargs: bfunarg_t list): fts_parameter_t list =
585+
let fmt (index: int) =
586+
match BCHBCAttributesUtil.get_format_archetype attrs index with
587+
| Some fmtstringtype -> fmtstringtype
588+
| _ -> NoFormat in
581589
let (_, _, params) =
582590
List.fold_left
583591
(fun (index, aa_state, params) (name, btype, _) ->
@@ -594,9 +602,14 @@ let arm_vfp_params (funargs: bfunarg_t list): fts_parameter_t list =
594602
| Ok btype, Ok tysize ->
595603
(* assume no packing at the argument top level *)
596604
let size = if tysize < 4 then 4 else tysize in
605+
let fmt =
606+
if varargs then
607+
fmt index
608+
else
609+
NoFormat in
597610
let (param, new_state) =
598611
if (is_int btype || is_pointer btype || is_enum btype) && size = 4 then
599-
get_arm_int_param_next_state size name btype aa_state index
612+
get_arm_int_param_next_state ~fmt size name btype aa_state index
600613
else if (is_int btype || is_pointer btype) then
601614
get_long_int_param_next_state size name btype aa_state index
602615
else if is_float btype then

CodeHawk/CHB/bchlib/bCHARMFunctionInterface.mli

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
66
7-
Copyright (c) 2023-2024 Aarno Labs LLC
7+
Copyright (c) 2023-2026 Aarno Labs LLC
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal
@@ -73,7 +73,8 @@ val arm_argument_state_to_string: arm_argument_state_t -> string
7373

7474
(** exposed for unit tests only *)
7575
val get_arm_int_param_next_state:
76-
int
76+
?fmt:formatstring_type_t
77+
-> int
7778
-> string
7879
-> btype_t
7980
-> arm_argument_state_t
@@ -98,7 +99,8 @@ val get_arm_struct_param_next_state:
9899
-> (fts_parameter_t * arm_argument_state_t)
99100

100101

101-
val arm_vfp_params: bfunarg_t list -> fts_parameter_t list
102+
val arm_vfp_params:
103+
?attrs:b_attributes_t -> ?varargs:bool -> bfunarg_t list -> fts_parameter_t list
102104

103105
val get_arm_format_spec_parameters:
104106
fts_parameter_t list -> argspec_int list -> fts_parameter_t list

CodeHawk/CHB/bchlib/bCHBCAttributes.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ open BCHFunctionInterface
4141
open BCHLibTypes
4242

4343

44+
4445
let convert_b_attributes_to_function_conditions
4546
(name: string)
4647
(fintf: function_interface_t)
@@ -121,6 +122,31 @@ let convert_b_attributes_to_function_conditions
121122
([], [])
122123
end) in
123124
(pre @ xpre, side @ xside, xpost)
125+
126+
| Attr (("format" | "chk_format"), params) ->
127+
let pre =
128+
(match params with
129+
| [ACons ("printf", []); AInt fmtrefindex; AInt _]
130+
| [ACons ("printf", []); AInt fmtrefindex] ->
131+
let fmtpar = get_par fmtrefindex in
132+
[XXOutputFormatString (ArgValue fmtpar)]
133+
| [ACons ("scanf", []); AInt fmtrefindex; AInt _]
134+
| [ACons ("scanf", []); AInt fmtrefindex] ->
135+
let fmtpar = get_par fmtrefindex in
136+
[XXInputFormatString (ArgValue fmtpar)]
137+
| _ ->
138+
begin
139+
log_diagnostics_result
140+
~msg:("attribute conversion for " ^ name ^ ": "
141+
^ "attribute parameters "
142+
^ (String.concat
143+
", " (List.map b_attrparam_to_string params)))
144+
~tag:"attribute conversion"
145+
__FILE__ __LINE__ [];
146+
[]
147+
end) in
148+
(pre, [] , [])
149+
124150
| Attr ("chk_pre", params) ->
125151
let pre =
126152
(match params with
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
(* =============================================================================
2+
CodeHawk Binary Analyzer
3+
Author: Henny Sipma
4+
------------------------------------------------------------------------------
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2026 Aarno Labs LLC
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
============================================================================= *)
27+
28+
(* bchlib *)
29+
open BCHBCTypes
30+
31+
32+
let get_format_archetype
33+
(attrs: b_attributes_t) (index: int): BCHLibTypes.formatstring_type_t option =
34+
List.fold_left (fun acc attr ->
35+
match acc with
36+
| Some _ -> acc
37+
| _ ->
38+
match attr with
39+
| Attr (("format" | "chk_format"), params) ->
40+
(match params with
41+
| [ACons ("printf", []); AInt fmtrefindex; AInt _]
42+
| [ACons ("printf", []); AInt fmtrefindex] ->
43+
if index = fmtrefindex then
44+
Some BCHLibTypes.PrintFormat
45+
else
46+
None
47+
| [ACons ("scanf", []); AInt fmtrefindex; AInt _]
48+
| [ACons ("scanf", []); AInt fmtrefindex] ->
49+
if index = fmtrefindex then
50+
Some BCHLibTypes.ScanFormat
51+
else
52+
None
53+
| _ -> None)
54+
| _ -> None) None attrs
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(* =============================================================================
2+
CodeHawk Binary Analyzer
3+
Author: Henny Sipma
4+
------------------------------------------------------------------------------
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2026 Aarno Labs LLC
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
============================================================================= *)
27+
28+
open BCHBCTypes
29+
open BCHLibTypes
30+
31+
32+
val get_format_archetype: b_attributes_t -> int -> formatstring_type_t option

CodeHawk/CHB/bchlib/bCHFunctionInterface.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ let bfuntype_to_function_interface
12591259
| None -> []
12601260
| Some funargs ->
12611261
(match system_settings#get_architecture with
1262-
| "arm" -> arm_vfp_params funargs
1262+
| "arm" -> arm_vfp_params ~attrs ~varargs funargs
12631263
| "x86" -> x86_cdecl_params funargs
12641264
| "mips" -> mips_params funargs
12651265
| arch ->

0 commit comments

Comments
 (0)