Skip to content

Commit 8b821cc

Browse files
committed
CHB: add restricted format attribute
1 parent 352b1cb commit 8b821cc

7 files changed

Lines changed: 164 additions & 18 deletions

File tree

‎CodeHawk/CHB/bchlib/bCHBCAttributesUtil.ml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ let get_format_archetype
4040
match attr with
4141
| Attr ("format", params) ->
4242
(match params with
43+
| ACons ("printf", []) :: AInt fmtrefindex :: AInt _ :: rest
44+
when List.for_all (function AStr _ -> true | _ -> false) rest
45+
&& rest <> [] ->
46+
if index = fmtrefindex then
47+
let specs =
48+
List.map (function AStr s -> s | _ -> assert false) rest in
49+
Some (BCHLibTypes.RestrictedPrintFormat specs)
50+
else
51+
None
4352
| [ACons ("printf", []); AInt fmtrefindex; AInt _]
4453
| [ACons ("printf", []); AInt fmtrefindex] ->
4554
if index = fmtrefindex then

‎CodeHawk/CHB/bchlib/bCHFloc.ml‎

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -767,14 +767,80 @@ object (self)
767767
None)
768768
None
769769
(numerical_to_doubleword n)
770-
| _ ->
770+
| Some (XVar v) when self#f#env#is_initial_register_value v ->
771+
let bterm_o =
772+
(new arm_expression_externalizer_t self#f)#xpr_to_bterm
773+
t_charptr (XVar v) in
774+
(match bterm_o with
775+
| Some (ArgValue ftspar) ->
776+
(match ftspar.apar_fmt with
777+
| RestrictedPrintFormat sl when sl <> [] ->
778+
let fmtstring =
779+
String.concat " " (List.map (fun s -> "%" ^ s) sl) in
780+
let _ =
781+
log_diagnostics_result
782+
~tag:("update-arm-varargs:restricted format specifiers "
783+
^ "from enclosing function")
784+
~msg:self#cia
785+
__FILE__ __LINE__
786+
["using restricted format specifiers: " ^ fmtstring] in
787+
let fmtspec = parse_formatstring fmtstring false in
788+
if fmtspec#has_arguments then
789+
let fmtargs = fmtspec#get_arguments in
790+
let newfintf = add_format_spec_parameters fintf false fmtargs in
791+
Some (newfintf, sem)
792+
else
793+
None
794+
| _ ->
795+
let _ =
796+
log_diagnostics_result
797+
~tag:"update_arm_varargs:no format arg info available"
798+
~msg:self#cia
799+
__FILE__ __LINE__
800+
["argxpr: " ^ (x2s argxpr)] in
801+
None)
802+
| _ ->
803+
let _ =
804+
log_diagnostics_result
805+
~tag:"update_arm_varargs:unable to externalize argument"
806+
~msg:self#cia
807+
__FILE__ __LINE__
808+
["argxpr: " ^ (x2s argxpr)];
809+
None)
810+
| Some argxpr ->
771811
let _ =
772812
log_diagnostics_result
773-
~tag:"update-arm-varargs"
813+
~tag:"update_arm_varargs"
774814
~msg:self#cia
775815
__FILE__ __LINE__
776-
["format string address is not a constant"] in
816+
["argxpr: " ^ (x2s argxpr)] in
777817
None
818+
| _ ->
819+
(match lastpar.apar_fmt with
820+
| RestrictedPrintFormat sl when sl <> [] ->
821+
let fmtstring =
822+
String.concat " " (List.map (fun s -> "%" ^ s) sl) in
823+
let _ =
824+
log_diagnostics_result
825+
~tag:"update-arm-varargs"
826+
~msg:self#cia
827+
__FILE__ __LINE__
828+
["using restricted format specifiers: " ^ fmtstring] in
829+
let fmtspec = parse_formatstring fmtstring false in
830+
if fmtspec#has_arguments then
831+
let fmtargs = fmtspec#get_arguments in
832+
let newfintf = add_format_spec_parameters fintf false fmtargs in
833+
Some (newfintf, sem)
834+
else
835+
None
836+
| _ ->
837+
let _ =
838+
log_diagnostics_result
839+
~tag:"update-arm-varargs"
840+
~msg:self#cia
841+
__FILE__ __LINE__
842+
["format string address is not a constant"] in
843+
None)
778844

779845
method private update_mips_varargs
780846
(fintf: function_interface_t)
@@ -861,13 +927,31 @@ object (self)
861927
None
862928
(numerical_to_doubleword n)
863929
| _ ->
864-
let _ =
865-
log_diagnostics_result
866-
~tag:"update-mips-varargs"
867-
~msg:self#cia
868-
__FILE__ __LINE__
869-
["format string address is not a constant"] in
870-
None
930+
(match lastpar.apar_fmt with
931+
| RestrictedPrintFormat sl when sl <> [] ->
932+
let fmtstring =
933+
String.concat " " (List.map (fun s -> "%" ^ s) sl) in
934+
let _ =
935+
log_diagnostics_result
936+
~tag:"update-mips-varargs"
937+
~msg:self#cia
938+
__FILE__ __LINE__
939+
["using restricted format specifiers: " ^ fmtstring] in
940+
let fmtspec = parse_formatstring fmtstring false in
941+
if fmtspec#has_arguments then
942+
let fmtargs = fmtspec#get_arguments in
943+
let newfintf = add_format_spec_parameters fintf false fmtargs in
944+
Some (newfintf, sem)
945+
else
946+
None
947+
| _ ->
948+
let _ =
949+
log_diagnostics_result
950+
~tag:"update-mips-varargs"
951+
~msg:self#cia
952+
__FILE__ __LINE__
953+
["format string address is not a constant"] in
954+
None)
871955

872956
method private update_varargs
873957
(fintf: function_interface_t)

‎CodeHawk/CHB/bchlib/bCHFtsParameter.ml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ let formatstring_type_to_string (t:formatstring_type_t) =
9191
| NoFormat -> "no"
9292
| PrintFormat -> "print"
9393
| ScanFormat -> "scan"
94+
| RestrictedPrintFormat sl -> "print(" ^ (String.concat "," sl) ^ ")"
9495

9596

9697
let pld_position_to_string (p: pld_position_t) =
@@ -273,7 +274,11 @@ let read_xml_formatstring_type (s:string): formatstring_type_t traceresult =
273274
| "no" -> Ok NoFormat
274275
| "print" -> Ok PrintFormat
275276
| "scan" -> Ok ScanFormat
276-
| _ -> Error ["Formatstring type: " ^ s ^ " not recognized"]
277+
| _ ->
278+
Error [
279+
"Formatstring type: "
280+
^ s
281+
^ " not recognized (note: RestrictedPrintFormat is not supported in xml)"]
277282

278283

279284
let read_xml_roles (node:xml_element_int) =

‎CodeHawk/CHB/bchlib/bCHInterfaceDictionary.ml‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ let raise_tag_error (name:string) (tag:string) (accepted:string list) =
6969
class interface_dictionary_t:interface_dictionary_int =
7070
object (self)
7171

72+
val formatstring_type_table = mk_index_table "formatstring-type-table"
7273
val pld_position_table = mk_index_table "pld-position-table"
7374
val pld_position_list_table = mk_index_table "pld-position-table-list"
7475
val parameter_location_detail_table =
@@ -102,6 +103,7 @@ object (self)
102103

103104
initializer
104105
tables <- [
106+
formatstring_type_table;
105107
pld_position_table;
106108
pld_position_list_table;
107109
parameter_location_detail_table;
@@ -134,6 +136,25 @@ object (self)
134136
List.iter (fun t -> t#reset) tables
135137
end
136138

139+
method index_formatstring_type (f: formatstring_type_t) =
140+
let tags = [formatstring_type_mcts#ts f] in
141+
let key =
142+
match f with
143+
| NoFormat | PrintFormat | ScanFormat -> (tags, [])
144+
| RestrictedPrintFormat sl -> (tags @ sl, []) in
145+
formatstring_type_table#add key
146+
147+
method get_formatstring_type (index: int): formatstring_type_t =
148+
let name = "formatstring_type" in
149+
let (tags, _) = formatstring_type_table#retrieve index in
150+
let t = t name tags in
151+
match (t 0) with
152+
| "n" -> NoFormat
153+
| "p" -> PrintFormat
154+
| "s" -> ScanFormat
155+
| "rp" -> RestrictedPrintFormat (List.tl tags)
156+
| s -> raise_tag_error name s formatstring_type_mcts#tags
157+
137158
method index_pld_position (p: pld_position_t) =
138159
let tags = [pld_position_mcts#ts p] in
139160
let key =
@@ -266,9 +287,7 @@ object (self)
266287

267288
method index_fts_parameter (p:fts_parameter_t) =
268289
let iopt (i: int option) = match i with Some i -> i | _ -> (-1) in
269-
let tags = [
270-
arg_io_mfts#ts p.apar_io;
271-
formatstring_type_mfts#ts p.apar_fmt] in
290+
let tags = [arg_io_mfts#ts p.apar_io] in
272291
let args = [
273292
iopt p.apar_index;
274293
bd#index_string p.apar_name;
@@ -277,7 +296,8 @@ object (self)
277296
self#index_roles p.apar_roles;
278297
p.apar_size;
279298
self#index_parameter_location_list p.apar_location;
280-
self#index_parameter_destination_list p.apar_destinations
299+
self#index_parameter_destination_list p.apar_destinations;
300+
self#index_formatstring_type p.apar_fmt;
281301
] in
282302
fts_parameter_table#add (tags, args)
283303

@@ -292,7 +312,7 @@ object (self)
292312
apar_desc = bd#get_string (a 3);
293313
apar_roles = self#get_roles (a 4);
294314
apar_io = arg_io_mfts#fs (t 0);
295-
apar_fmt = formatstring_type_mfts#fs (t 1);
315+
apar_fmt = self#get_formatstring_type (a 8);
296316
apar_size = a 5;
297317
apar_location = self#get_parameter_location_list (a 6);
298318
apar_destinations = self#get_parameter_destination_list (a 7)

‎CodeHawk/CHB/bchlib/bCHLibTypes.mli‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,6 +2127,7 @@ type formatstring_type_t =
21272127
| NoFormat
21282128
| PrintFormat
21292129
| ScanFormat
2130+
| RestrictedPrintFormat of string list
21302131

21312132

21322133
(** Function type signature parameter.
@@ -2793,6 +2794,9 @@ class type interface_dictionary_int =
27932794

27942795
(** {1 Parameter location}*)
27952796

2797+
method index_formatstring_type: formatstring_type_t -> int
2798+
method get_formatstring_type: int -> formatstring_type_t
2799+
27962800
method index_pld_position: pld_position_t -> int
27972801
method get_pld_position: int -> pld_position_t
27982802

‎CodeHawk/CHB/bchlib/bCHSumTypeSerializer.ml‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ let arg_io_mfts: arg_io_t mfts_int =
8080
mk_mfts "arg_io_t" [ (ArgRead, "r"); (ArgReadWrite, "rw"); (ArgWrite, "w") ]
8181

8282

83+
(*
8384
let formatstring_type_mfts: formatstring_type_t mfts_int =
8485
mk_mfts
8586
"formatstring_type_t"
8687
[ (NoFormat, "n"); (PrintFormat, "p"); (ScanFormat, "s") ]
87-
88+
*)
8889

8990
let eflag_mfts: eflag_t mfts_int =
9091
mk_mfts
@@ -252,6 +253,27 @@ end
252253

253254
let register_mcts: register_t mfts_int = new register_mcts_t
254255

256+
257+
class formatstring_type_mcts_t: [formatstring_type_t] mfts_int =
258+
object
259+
260+
inherit [formatstring_type_t] mcts_t "formatstring_type"
261+
262+
method !ts (f: formatstring_type_t) =
263+
match f with
264+
| ScanFormat -> "s"
265+
| PrintFormat -> "p"
266+
| NoFormat -> "n"
267+
| RestrictedPrintFormat _ -> "rp"
268+
269+
method !tags = ["n"; "p"; "rp"; "s"]
270+
271+
end
272+
273+
let formatstring_type_mcts: formatstring_type_t mfts_int =
274+
new formatstring_type_mcts_t
275+
276+
255277
class pld_position_mcts_t: [pld_position_t] mfts_int =
256278
object
257279

‎CodeHawk/CHB/bchlib/bCHSumTypeSerializer.mli‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ val quote_status_mfts: quote_status_t mfts_int
4141
val format_args_kind_mfts: format_args_kind_t mfts_int
4242

4343
val arg_io_mfts: arg_io_t mfts_int
44-
val formatstring_type_mfts: formatstring_type_t mfts_int
44+
(* val formatstring_type_mfts: formatstring_type_t mfts_int *)
4545

4646
(** {1 Architecture-related} *)
4747

@@ -75,6 +75,8 @@ val register_mcts: register_t mfts_int
7575

7676
(** {1 Interface types} *)
7777

78+
val formatstring_type_mcts: formatstring_type_t mfts_int
79+
7880
val parameter_destination_mcts: parameter_destination_t mfts_int
7981
val pld_position_mcts: pld_position_t mfts_int
8082
val parameter_location_mcts: parameter_location_t mfts_int

0 commit comments

Comments
 (0)