Skip to content

Commit 18c2c18

Browse files
committed
CHB:TYP: infrastructure to enable/disable typing rules
1 parent d7603ed commit 18c2c18

File tree

6 files changed

+193
-22
lines changed

6 files changed

+193
-22
lines changed

CodeHawk/CHB/bchlib/bCHFunctionData.ml

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,32 @@ object (self)
280280
__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
281281
^ "No stackvar annotation found at offset " ^ (string_of_int offset)]
282282

283+
method is_typing_rule_enabled (loc: string) (name: string): bool =
284+
match self#get_function_annotation with
285+
| None -> false
286+
| Some a ->
287+
List.fold_left
288+
(fun acc tra ->
289+
acc ||
290+
(if tra.tra_action = "enable" && tra.tra_name = name then
291+
(List.mem "all" tra.tra_locations)
292+
|| (List.mem loc tra.tra_locations)
293+
else
294+
false)) false a.typingrules
295+
296+
method is_typing_rule_disabled (loc: string) (name: string): bool =
297+
match self#get_function_annotation with
298+
| None -> false
299+
| Some a ->
300+
List.fold_left
301+
(fun acc tra ->
302+
acc ||
303+
(if tra.tra_action = "disable" && tra.tra_name = name then
304+
(List.mem "all" tra.tra_locations)
305+
|| (List.mem loc tra.tra_locations)
306+
else
307+
false)) false a.typingrules
308+
283309
method add_inlined_block (baddr:doubleword_int) =
284310
inlined_blocks <- baddr :: inlined_blocks
285311

@@ -592,6 +618,25 @@ let read_xml_stackvar_intro (node: xml_element_int): stackvar_intro_t traceresul
592618
svi_cast = svi_cast}
593619

594620

621+
let read_xml_typing_rule (node: xml_element_int): typing_rule_t traceresult =
622+
let get = node#getAttribute in
623+
let has = node#hasNamedAttribute in
624+
if not (has "name") then
625+
Error ["typingrule without name"]
626+
else if not (has "locs") then
627+
Error ["typingrule without location"]
628+
else if not (has "action") then
629+
Error ["typingrule without action"]
630+
else
631+
let name = get "name" in
632+
let action = get "action" in
633+
let locs = get "locs" in
634+
let locs = String.split_on_char ',' locs in
635+
Ok {tra_name = name;
636+
tra_action = action;
637+
tra_locations = locs}
638+
639+
595640
let read_xml_function_annotation (node: xml_element_int) =
596641
let get = node#getAttribute in
597642
let getc = node#getTaggedChild in
@@ -635,8 +680,27 @@ let read_xml_function_annotation (node: xml_element_int) =
635680
(rvintros#getTaggedChildren "vintro")
636681
else
637682
[] in
683+
let typingrules =
684+
if hasc "typing-rules" then
685+
let trules = getc "typing-rules" in
686+
List.fold_left
687+
(fun acc n ->
688+
TR.tfold
689+
~ok:(fun tr -> tr :: acc)
690+
~error:(fun e ->
691+
begin
692+
log_error_result __FILE__ __LINE__ e;
693+
acc
694+
end)
695+
(read_xml_typing_rule n))
696+
[]
697+
(trules#getTaggedChildren "typingrule")
698+
else
699+
[] in
638700
fndata#set_function_annotation
639-
{regvarintros = regvintros; stackvarintros = stackvintros}
701+
{regvarintros = regvintros;
702+
stackvarintros = stackvintros;
703+
typingrules = typingrules}
640704
else
641705
log_error_result
642706
~tag:"function annotation faddr not found"

CodeHawk/CHB/bchlib/bCHFunctionInterface.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ let record_function_interface_type_constraints
11391139
~tag:("function-interface type constraint")
11401140
__FILE__ __LINE__
11411141
[faddr ^ ": " ^ (type_constraint_to_string tyc)] in
1142-
store#add_constraint tyc
1142+
store#add_constraint faddr "exit" "INTF-freturn" tyc
11431143
| _ -> ());
11441144
(match fargs with
11451145
| None ->
@@ -1160,7 +1160,7 @@ let record_function_interface_type_constraints
11601160
~tag:("function-interface type constraint")
11611161
__FILE__ __LINE__
11621162
[faddr ^ ": " ^ (type_constraint_to_string tyc)] in
1163-
store#add_constraint tyc
1163+
store#add_constraint faddr "init" "INTF-fparam" tyc
11641164
| _ ->
11651165
chlog#add
11661166
"function interface type constraints"

CodeHawk/CHB/bchlib/bCHLibTypes.mli

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,17 @@ type stackvar_intro_t = {
15111511
svi_cast: bool
15121512
}
15131513

1514+
type typing_rule_t = {
1515+
tra_action: string;
1516+
tra_name: string;
1517+
tra_locations: string list
1518+
}
1519+
15141520

15151521
type function_annotation_t = {
15161522
regvarintros: regvar_intro_t list;
1517-
stackvarintros: stackvar_intro_t list
1523+
stackvarintros: stackvar_intro_t list;
1524+
typingrules: typing_rule_t list
15181525
}
15191526

15201527
class type function_data_int =
@@ -1567,6 +1574,8 @@ class type function_data_int =
15671574
method has_regvar_type_cast: doubleword_int -> bool
15681575
method has_stackvar_type_annotation: int -> bool
15691576
method has_stackvar_type_cast: int -> bool
1577+
method is_typing_rule_enabled: string -> string -> bool
1578+
method is_typing_rule_disabled: string -> string -> bool
15701579
method has_class_info: bool
15711580
method has_callsites: bool
15721581
method has_path_contexts: bool
@@ -3188,6 +3197,14 @@ type type_constraint_t =
31883197
| TyZeroCheck of type_term_t
31893198

31903199

3200+
type type_inference_rule_application_t = {
3201+
tir_faddr: string;
3202+
tir_loc: string;
3203+
tir_rule: string;
3204+
tir_constraint_ix: int
3205+
}
3206+
3207+
31913208
class type type_constraint_dictionary_int =
31923209
object
31933210

@@ -3246,17 +3263,21 @@ class type type_constraint_store_int =
32463263

32473264
method reset: unit
32483265

3249-
method add_constraint: type_constraint_t -> unit
3266+
method add_constraint:
3267+
string -> string -> string -> type_constraint_t -> unit
32503268

3251-
method add_var_constraint: type_variable_t -> unit
3269+
method add_var_constraint:
3270+
string -> string -> string -> type_variable_t -> unit
32523271

3253-
method add_term_constraint: type_term_t -> unit
3272+
method add_term_constraint:
3273+
string -> string -> string -> type_term_t -> unit
32543274

3255-
method add_zerocheck_constraint: type_variable_t -> unit
3275+
(* method add_zerocheck_constraint: type_variable_t -> unit *)
32563276

3257-
method add_subtype_constraint: type_term_t -> type_term_t -> unit
3277+
method add_subtype_constraint:
3278+
string -> string -> string -> type_term_t -> type_term_t -> unit
32583279

3259-
method add_ground_constraint: type_term_t -> type_term_t -> unit
3280+
(* method add_ground_constraint: type_term_t -> type_term_t -> unit *)
32603281

32613282
method get_function_type_constraints: string -> type_constraint_t list
32623283

@@ -3287,6 +3308,8 @@ class type type_constraint_store_int =
32873308

32883309
method resolve_local_stack_lhs_types: string -> (int * btype_t option) list
32893310

3311+
method write_xml: xml_element_int -> unit
3312+
32903313
method toPretty: pretty_t
32913314

32923315
end

CodeHawk/CHB/bchlib/bCHPreFileIO.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,13 @@ let get_resultdata_filename () =
439439
Filename.concat fdir (exename ^ "_data.xml")
440440

441441

442+
let get_typeconstraintstore_filename () =
443+
let exename = get_filename () in
444+
let fdir = get_results_dir () in
445+
let _ = create_directory fdir in
446+
Filename.concat fdir (exename ^ "_tcstore.xml")
447+
448+
442449
let get_x86dictionary_filename () =
443450
let exename = get_filename () in
444451
let fdir = get_results_dir () in
@@ -657,6 +664,18 @@ let save_resultdata_file (node:xml_element_int) =
657664
file_output#saveFile filename doc#toPretty
658665
end
659666

667+
668+
let save_typeconstraintstore (node: xml_element_int) =
669+
let filename = get_typeconstraintstore_filename () in
670+
let doc = xmlDocument () in
671+
let root = get_bch_root "type-constraint-store" in
672+
begin
673+
doc#setNode root;
674+
root#appendChildren [node];
675+
file_output#saveFile filename doc#toPretty
676+
end
677+
678+
660679
let save_cfgs (node:xml_element_int) =
661680
let filename = get_cfgs_filename () in
662681
let doc = xmlDocument () in

CodeHawk/CHB/bchlib/bCHPreFileIO.mli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Copyright (c) 2005-2019 Kestrel Technology LLC
88
Copyright (c) 2020 Henny Sipma
9-
Copyright (c) 2021-2024 Aarno Labs LLC
9+
Copyright (c) 2021-2025 Aarno Labs LLC
1010
1111
Permission is hereby granted, free of charge, to any person obtaining a copy
1212
of this software and associated documentation files (the "Software"), to deal
@@ -117,7 +117,8 @@ val save_userdata_function_summary_file: string -> xml_element_int -> unit
117117
val save_userdata_function_summaries_file: xml_element_int -> unit
118118

119119
val load_export_ordinal_table: string -> xml_element_int option
120-
val save_resultdata_file : xml_element_int -> unit
120+
val save_resultdata_file: xml_element_int -> unit
121+
val save_typeconstraintstore: xml_element_int -> unit
121122
val save_cfgs: xml_element_int -> unit
122123

123124
val save_executable_dump: xml_element_int -> unit

CodeHawk/CHB/bchlib/bCHTypeConstraintStore.ml

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ open CHUtils
3131

3232
(* chutil *)
3333
open CHLogger
34+
open CHXmlDocument
3435

3536
(* bchlib *)
3637
open BCHBCTypePretty
@@ -72,6 +73,8 @@ object (self)
7273
(* constraints that involve a global variable *)
7374
val gvartypes = H.create 5
7475

76+
val rules_applied = H.create 5
77+
7578
method reset =
7679
begin
7780
H.clear store;
@@ -81,9 +84,28 @@ object (self)
8184
H.clear gvartypes
8285
end
8386

84-
method add_constraint (c: type_constraint_t) =
87+
method private add_rule_applied
88+
(faddr: string)
89+
(loc: string)
90+
(rule: string)
91+
(ix: int) =
92+
let tir = {
93+
tir_faddr = faddr;
94+
tir_loc = loc;
95+
tir_rule = rule;
96+
tir_constraint_ix = ix
97+
} in
98+
let entry =
99+
if H.mem rules_applied faddr then
100+
H.find rules_applied faddr
101+
else
102+
[] in
103+
H.replace rules_applied faddr (tir :: entry)
104+
105+
method add_constraint
106+
(faddr: string) (loc: string) (rule: string) (c: type_constraint_t) =
85107
let index = tcd#index_type_constraint c in
86-
(* index the constraint *)
108+
let _ = self#add_rule_applied faddr loc rule index in
87109
if H.mem store index then
88110
()
89111
else
@@ -188,33 +210,50 @@ object (self)
188210
if H.mem iaddrentry iaddr then H.find iaddrentry iaddr else [] in
189211
H.replace iaddrentry iaddr (c :: entry)
190212

191-
method add_var_constraint (tyvar: type_variable_t) =
192-
self#add_constraint (TyVar (TyVariable tyvar))
213+
method add_var_constraint
214+
(faddr: string)
215+
(loc: string)
216+
(rule: string)
217+
(tyvar: type_variable_t) =
218+
self#add_constraint faddr loc rule (TyVar (TyVariable tyvar))
193219

194-
method add_term_constraint (t: type_term_t) =
220+
method add_term_constraint
221+
(faddr: string)
222+
(loc: string)
223+
(rule: string)
224+
(t: type_term_t) =
195225
match t with
196-
| TyVariable tv -> self#add_var_constraint tv
226+
| TyVariable tv -> self#add_var_constraint faddr loc rule tv
197227
| _ -> ()
198228

229+
(*
199230
method add_zerocheck_constraint (tyvar: type_variable_t) =
200231
begin
201232
self#add_var_constraint tyvar;
202233
self#add_constraint (TyZeroCheck (TyVariable tyvar))
203234
end
235+
*)
204236

205-
method add_subtype_constraint (t1: type_term_t) (t2: type_term_t) =
237+
method add_subtype_constraint
238+
(faddr: string)
239+
(loc: string)
240+
(rule: string)
241+
(t1: type_term_t)
242+
(t2: type_term_t) =
206243
begin
207-
self#add_term_constraint t1;
208-
self#add_term_constraint t2;
209-
self#add_constraint (TySub (t1, t2))
244+
self#add_term_constraint faddr loc rule t1;
245+
self#add_term_constraint faddr loc rule t2;
246+
self#add_constraint faddr loc rule (TySub (t1, t2))
210247
end
211248

249+
(*
212250
method add_ground_constraint (t1: type_term_t) (t2: type_term_t) =
213251
begin
214252
self#add_term_constraint t1;
215253
self#add_term_constraint t2;
216254
self#add_constraint (TyGround (t1, t2))
217255
end
256+
*)
218257

219258
method get_function_type_constraints (faddr: string): type_constraint_t list =
220259
if H.mem functiontypes faddr then
@@ -704,6 +743,31 @@ object (self)
704743
() in
705744
!result
706745

746+
method private write_xml_rules_applied (node: xml_element_int) =
747+
let ranode = xmlElement "rules-applied" in
748+
begin
749+
H.iter (fun faddr rules ->
750+
let fnode = xmlElement "function" in
751+
let _ = fnode#setAttribute "faddr" faddr in
752+
begin
753+
List.iter (fun rule ->
754+
let rnode = xmlElement "rule" in
755+
begin
756+
rnode#setAttribute "loc" rule.tir_loc;
757+
rnode#setAttribute "rule" rule.tir_rule;
758+
rnode#setIntAttribute "tc-ix" rule.tir_constraint_ix;
759+
fnode#appendChildren [rnode]
760+
end) rules;
761+
ranode#appendChildren [fnode]
762+
end) rules_applied;
763+
node#appendChildren [ranode]
764+
end
765+
766+
method write_xml (node: xml_element_int) =
767+
begin
768+
self#write_xml_rules_applied (node)
769+
end
770+
707771
method toPretty =
708772
let constraints = ref [] in
709773
let _ =

0 commit comments

Comments
 (0)