Skip to content

Commit 1974ae0

Browse files
committed
CHB:USER: add remove-reaching-defs to function annotation
1 parent b621f7d commit 1974ae0

File tree

1 file changed

+110
-5
lines changed

1 file changed

+110
-5
lines changed

CodeHawk/CHB/bchlib/bCHFunctionData.ml

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
============================================================================= *)
2929

3030
(* chlib *)
31+
open CHLanguage
3132
open CHPretty
3233

3334
(* chutil *)
@@ -50,6 +51,7 @@ open BCHLibTypes
5051
module H = Hashtbl
5152
module TR = CHTraceResult
5253

54+
let p2s = CHPrettyUtil.pretty_to_string
5355

5456
let bd = BCHDictionary.bdictionary
5557

@@ -76,10 +78,41 @@ let stackvar_intro_to_string (svi: stackvar_intro_t) =
7678
(string_of_int svi.svi_offset) ^ ": " ^ svi.svi_name ^ ptype
7779

7880

81+
let reachingdef_spec_to_string (rds: reachingdef_spec_t) =
82+
let uselocs = "[" ^ (String.concat ", " rds.rds_uselocs) ^ "]" in
83+
let rdeflocs = "[" ^ (String.concat ", " rds.rds_rdeflocs) ^ "]" in
84+
rds.rds_variable ^ ": use: " ^ uselocs ^ "; remove-rdefs: " ^ rdeflocs
85+
86+
7987
let function_annotation_to_string (a: function_annotation_t) =
80-
(String.concat "\n" (List.map regvar_intro_to_string a.regvarintros))
81-
^ "\n"
82-
^ (String.concat "\n" (List.map stackvar_intro_to_string a.stackvarintros))
88+
let rvintros =
89+
if (List.length a.regvarintros) > 0 then
90+
("Register-variable-introductions: ["
91+
^ (String.concat "; " (List.map regvar_intro_to_string a.regvarintros))
92+
^ "]")
93+
else
94+
"" in
95+
let svintros =
96+
if (List.length a.stackvarintros) > 0 then
97+
("Stack-variable-introductions: ["
98+
^ (String.concat "; " (List.map stackvar_intro_to_string a.stackvarintros))
99+
^ "]")
100+
else
101+
"" in
102+
let rdefspecs =
103+
if (List.length a.reachingdefspecs) > 0 then
104+
("Reaching-def-specs: ["
105+
^ (String.concat "; " (List.map reachingdef_spec_to_string a.reachingdefspecs))
106+
^ "]")
107+
else
108+
"" in
109+
List.fold_left (fun acc s ->
110+
if s = "" then
111+
acc
112+
else if acc = "" then
113+
s
114+
else
115+
acc ^ "\n" ^ s) "" [rvintros; svintros; rdefspecs]
83116

84117

85118
class function_data_t (fa:doubleword_int) =
@@ -156,7 +189,8 @@ object (self)
156189
functionannotation <- Some a;
157190
chlog#add
158191
"function annotation"
159-
(LBLOCK [faddr#toPretty; NL; STR (function_annotation_to_string a)])
192+
(LBLOCK [STR "function "; faddr#toPretty; STR ": ";
193+
STR (function_annotation_to_string a)])
160194
end
161195

162196
method has_function_annotation: bool =
@@ -314,6 +348,36 @@ object (self)
314348
else
315349
BCHSystemSettings.system_settings#is_typing_rule_enabled name
316350

351+
method filter_deflocs
352+
(iaddr: string) (v: variable_t) (deflocs: symbol_t list): symbol_t list =
353+
match self#get_function_annotation with
354+
| None -> deflocs
355+
| Some a when (List.length a.reachingdefspecs) > 0 ->
356+
let vname = v#getName#getBaseName in
357+
let deflocs =
358+
List.fold_left
359+
(fun acc sym ->
360+
let symname = sym#getBaseName in
361+
if (List.fold_left
362+
(fun filteracc rds ->
363+
filteracc ||
364+
(if rds.rds_variable = vname then
365+
(List.mem iaddr rds.rds_uselocs)
366+
&& (List.mem symname rds.rds_rdeflocs)
367+
else
368+
false)) false a.reachingdefspecs) then
369+
let _ =
370+
log_result
371+
~msg:("iaddr: " ^ iaddr)
372+
~tag:"filter out reaching def"
373+
__FILE__ __LINE__
374+
["v: " ^ (p2s v#toPretty); "s; " ^ symname] in
375+
acc
376+
else
377+
sym :: acc) [] deflocs in
378+
List.rev deflocs
379+
| _ -> deflocs
380+
317381
method add_inlined_block (baddr:doubleword_int) =
318382
inlined_blocks <- baddr :: inlined_blocks
319383

@@ -645,6 +709,28 @@ let read_xml_typing_rule (node: xml_element_int): typing_rule_t traceresult =
645709
tra_locations = locs}
646710

647711

712+
let read_xml_reachingdef_spec
713+
(node: xml_element_int): reachingdef_spec_t traceresult =
714+
let get = node#getAttribute in
715+
let has = node#hasNamedAttribute in
716+
if not (has "var") then
717+
Error ["rdefspec without var"]
718+
else if not (has "uselocs") then
719+
Error ["rdefspec without uselocs"]
720+
else if not (has "rdeflocs") then
721+
Error ["rdefspec without rdeflocs"]
722+
else
723+
let var = get "var" in
724+
let uselocs = get "uselocs" in
725+
let uselocs = String.split_on_char ',' uselocs in
726+
let rdeflocs = get "rdeflocs" in
727+
let rdeflocs = String.split_on_char ',' rdeflocs in
728+
Ok {rds_variable = var;
729+
rds_uselocs = uselocs;
730+
rds_rdeflocs = rdeflocs
731+
}
732+
733+
648734
let read_xml_function_annotation (node: xml_element_int) =
649735
let get = node#getAttribute in
650736
let getc = node#getTaggedChild in
@@ -705,10 +791,29 @@ let read_xml_function_annotation (node: xml_element_int) =
705791
(trules#getTaggedChildren "typingrule")
706792
else
707793
[] in
794+
let rdefspecs =
795+
if hasc "remove-rdefs" then
796+
let rrds = getc "remove-rdefs" in
797+
List.fold_left
798+
(fun acc n ->
799+
TR.tfold
800+
~ok:(fun rds -> rds :: acc)
801+
~error:(fun e ->
802+
begin
803+
log_error_result __FILE__ __LINE__ e;
804+
acc
805+
end)
806+
(read_xml_reachingdef_spec n))
807+
[]
808+
(rrds#getTaggedChildren "remove-var-rdefs")
809+
else
810+
[] in
708811
fndata#set_function_annotation
709812
{regvarintros = regvintros;
710813
stackvarintros = stackvintros;
711-
typingrules = typingrules}
814+
typingrules = typingrules;
815+
reachingdefspecs = rdefspecs
816+
}
712817
else
713818
log_error_result
714819
~tag:"function annotation faddr not found"

0 commit comments

Comments
 (0)