Skip to content

Commit e49da83

Browse files
committed
CHB: incorporate more type info in constructing memory offsets
1 parent d0266d2 commit e49da83

File tree

8 files changed

+96
-11
lines changed

8 files changed

+96
-11
lines changed

CodeHawk/CHB/bchlib/bCHFloc.ml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ object (self)
14991499
| XConst (IntConst n)
15001500
when n#equal (mkNumerical 0xffffffff) && is_int t ->
15011501
Ok (int_constant_expr (-1))
1502-
| _ -> self#convert_xpr_offsets ~size x
1502+
| _ -> self#convert_xpr_offsets ~xtype ~size x
15031503

15041504
method convert_addr_to_c_pointed_to_expr
15051505
?(size=None) ?(xtype=None) (a: xpr_t): xpr_t traceresult =
@@ -1656,12 +1656,13 @@ object (self)
16561656

16571657

16581658
method convert_variable_offsets
1659-
?(size=None) (v: variable_t): variable_t traceresult =
1659+
?(vtype=None) ?(size=None) (v: variable_t): variable_t traceresult =
16601660
if self#env#is_basevar_memory_variable v then
16611661
let basevar_r = self#env#get_memvar_basevar v in
16621662
let offset_r = self#env#get_memvar_offset v in
16631663
let cbasevar_r = TR.tbind self#convert_value_offsets basevar_r in
16641664
let basetype_r = TR.tbind self#get_variable_type cbasevar_r in
1665+
let optvtype = match vtype with Some t -> t | _ -> t_unknown in
16651666
let tgttype_r =
16661667
TR.tbind
16671668
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__))
@@ -1682,8 +1683,18 @@ object (self)
16821683
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__))
16831684
(fun tgttype ->
16841685
address_memory_offset
1685-
~tgtsize:size tgttype (num_constant_expr n)) tgttype_r
1686+
~tgtbtype:optvtype ~tgtsize:size tgttype (num_constant_expr n))
1687+
tgttype_r
16861688
| _ -> Ok offset) offset_r in
1689+
let _ =
1690+
log_diagnostics_result
1691+
~msg:(p2s self#l#toPretty)
1692+
~tag:"convert-variable-offsets"
1693+
__FILE__ __LINE__
1694+
["tgttype: " ^ (TR.tfold_default btype_to_string "?" tgttype_r);
1695+
"tgtbtype: " ^ (btype_to_string optvtype);
1696+
"offset : " ^ (TR.tfold_default memory_offset_to_string "?" offset_r);
1697+
"coffset: " ^ (TR.tfold_default memory_offset_to_string "?" coffset_r)] in
16871698
TR.tbind
16881699
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": " ^ (p2s v#toPretty))
16891700
(fun cbasevar ->
@@ -1776,7 +1787,8 @@ object (self)
17761787
["v: " ^ (p2s v#toPretty)] in
17771788
Ok v
17781789

1779-
method convert_xpr_offsets ?(size=None) (x: xpr_t): xpr_t traceresult =
1790+
method convert_xpr_offsets
1791+
?(xtype=None) ?(size=None) (x: xpr_t): xpr_t traceresult =
17801792
let rec aux exp =
17811793
match exp with
17821794
| XVar v when self#env#is_basevar_memory_value v ->
@@ -1789,10 +1801,16 @@ object (self)
17891801
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__))
17901802
(fun v -> XVar v) (self#convert_variable_offsets ~size v)
17911803
| XOp ((Xf "addressofvar"), [XVar v]) ->
1804+
let derefty =
1805+
match xtype with
1806+
| None -> None
1807+
| Some (TPtr (ty, _)) -> Some ty
1808+
| _ -> None in
17921809
let newx_r =
17931810
TR.tmap
17941811
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__))
1795-
(fun v -> XVar v) (self#convert_variable_offsets ~size v) in
1812+
(fun v ->
1813+
XVar v) (self#convert_variable_offsets ~vtype:derefty ~size v) in
17961814
TR.tmap
17971815
(fun newx ->
17981816
match newx with

CodeHawk/CHB/bchlib/bCHFunctionInfo.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,11 @@ object (self)
17181718
method add_reaching_def
17191719
(iaddr: string) (v: variable_t) (deflocs: symbol_t list) =
17201720
begin
1721+
let deflocs =
1722+
if fndata#has_function_annotation then
1723+
fndata#filter_deflocs iaddr v deflocs
1724+
else
1725+
deflocs in
17211726
self#fvarinv#add_reaching_def iaddr v deflocs;
17221727
List.iter (fun s ->
17231728
if (List.length s#getAttributes) = 0 then

CodeHawk/CHB/bchlib/bCHGlobalMemoryMap.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ object (self)
198198
~(tgtbtype: btype_t option)
199199
(c: bcompinfo_t)
200200
(xoffset: xpr_t): memory_offset_t traceresult =
201+
let _ =
202+
log_diagnostics_result
203+
~tag:"global:get-field-memory-offset-at"
204+
__FILE__ __LINE__
205+
["tgtsize: "
206+
^ (if Option.is_some tgtsize then
207+
string_of_int (Option.get tgtsize) else "?");
208+
"tgtbtype: "
209+
^ (if Option.is_some tgtbtype then
210+
btype_to_string (Option.get tgtbtype) else "?");
211+
"compinfo: " ^ c.bcname;
212+
"xoffset: " ^ (x2s xoffset)] in
201213
let is_void_tgtbtype =
202214
match tgtbtype with
203215
| Some (TVoid _) -> true
@@ -368,6 +380,20 @@ object (self)
368380
~(tgtbtype: btype_t option)
369381
(btype: btype_t)
370382
(xoffset: xpr_t): memory_offset_t traceresult =
383+
let _ =
384+
log_diagnostics_result
385+
~tag:"global:arrayvar-memory-offset"
386+
__FILE__ __LINE__
387+
["tgtsize: " ^ (if (Option.is_some tgtsize) then
388+
string_of_int (Option.get tgtsize)
389+
else
390+
"?");
391+
"tgtbtype: " ^ (if (Option.is_some tgtbtype) then
392+
btype_to_string (Option.get tgtbtype)
393+
else
394+
"?");
395+
"btype: " ^ (btype_to_string btype);
396+
"xoffset: " ^ (x2s xoffset)] in
371397
let iszero x =
372398
match x with
373399
| XConst (IntConst n) -> n#equal CHNumerical.numerical_zero

CodeHawk/CHB/bchlib/bCHLibTypes.mli

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,11 +1518,18 @@ type typing_rule_t = {
15181518
tra_locations: string list
15191519
}
15201520

1521+
type reachingdef_spec_t = {
1522+
rds_variable: string;
1523+
rds_uselocs: string list;
1524+
rds_rdeflocs: string list
1525+
}
1526+
15211527

15221528
type function_annotation_t = {
15231529
regvarintros: regvar_intro_t list;
15241530
stackvarintros: stackvar_intro_t list;
1525-
typingrules: typing_rule_t list
1531+
typingrules: typing_rule_t list;
1532+
reachingdefspecs: reachingdef_spec_t list
15261533
}
15271534

15281535
class type function_data_int =
@@ -1575,6 +1582,7 @@ class type function_data_int =
15751582
method has_regvar_type_cast: doubleword_int -> bool
15761583
method has_stackvar_type_annotation: int -> bool
15771584
method has_stackvar_type_cast: int -> bool
1585+
method filter_deflocs: string -> variable_t -> symbol_t list -> symbol_t list
15781586
method is_typing_rule_enabled: ?rdef:string option -> string -> string -> bool
15791587
method has_class_info: bool
15801588
method has_callsites: bool
@@ -6102,9 +6110,13 @@ class type floc_int =
61026110
?size:int option -> variable_t -> variable_t traceresult
61036111

61046112
method convert_variable_offsets:
6105-
?size:int option -> variable_t -> variable_t traceresult
6113+
?vtype:btype_t option
6114+
-> ?size:int option
6115+
-> variable_t
6116+
-> variable_t traceresult
61066117

6107-
method convert_xpr_offsets: ?size:int option -> xpr_t -> xpr_t traceresult
6118+
method convert_xpr_offsets:
6119+
?xtype:btype_t option -> ?size:int option -> xpr_t -> xpr_t traceresult
61086120

61096121
(* returns the variable associated with the address expression *)
61106122
method get_lhs_from_address: xpr_t -> variable_t

CodeHawk/CHB/bchlib/bCHMemoryReference.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,23 @@ and structvar_memory_offset
256256
~(tgtbtype: btype_t option)
257257
(btype: btype_t)
258258
(xoffset: xpr_t): memory_offset_t traceresult =
259+
let _ =
260+
log_diagnostics_result
261+
~tag:"structvar-memory-offset"
262+
__FILE__ __LINE__
263+
["tgtsize: "
264+
^ (if Option.is_some tgtsize then (string_of_int (Option.get tgtsize)) else "?");
265+
"tgtbtype: "
266+
^ (if Option.is_some tgtbtype then (btype_to_string (Option.get tgtbtype))
267+
else "?");
268+
"btype: " ^ (btype_to_string btype);
269+
"xoffset: " ^ (x2s xoffset)] in
259270
match xoffset with
271+
| XConst (IntConst n)
272+
when n#equal numerical_zero
273+
&& (Option.is_some tgtbtype)
274+
&& (btype_equal (Option.get tgtbtype) btype) ->
275+
Ok NoOffset
260276
| XConst (IntConst _) ->
261277
if is_struct_type btype then
262278
let compinfo = get_struct_type_compinfo btype in

CodeHawk/CHB/bchlib/bCHSystemSettings.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ let _ =
5959
("ADD-c", "enable");
6060
("AND-rdef", "enable");
6161
("ASR-rdef", "enable");
62+
("BXLR-rdef", "enable");
6263
("CMP-rdef", "enable");
6364
("LSL_rdef", "enable");
6465
("LSR_rdef", "enable");
@@ -111,6 +112,7 @@ let _ =
111112
("BL-sig-regarg", "enable");
112113
("BL-sig-stackarg", "enable");
113114
("BL-sig-rv", "enable");
115+
("BXLR-sig-rv", "enable");
114116
("LDR-array", "enable");
115117
("LDR-memop-tc", "enable");
116118
("LDR-stack-addr", "enable");

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_20250805"
99-
~date:"2025-08-05"
98+
~version:"0.6.0_20250810"
99+
~date:"2025-08-10"
100100
~licensee: None
101101
~maxfilesize: None
102102
()

CodeHawk/CHB/bchlibarm32/bCHARMInstructionAggregate.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ open BCHThumbITSequence
5050

5151
module TR = CHTraceResult
5252

53+
let p2s = CHPrettyUtil.pretty_to_string
54+
5355

5456
let arm_aggregate_kind_to_string (k: arm_aggregate_kind_t) =
5557
match k with
@@ -180,7 +182,11 @@ let make_arm_instruction_aggregate
180182
let agg =
181183
new arm_instruction_aggregate_t ~kind ~instrs ~entry ~exitinstr ~anchor in
182184
begin
183-
chlog#add "aggregate instruction" agg#toPretty;
185+
log_diagnostics_result
186+
~msg:(anchor#get_address#to_hex_string)
187+
~tag:"aggregate instruction"
188+
__FILE__ __LINE__
189+
[(p2s agg#toPretty)];
184190
agg
185191
end
186192

0 commit comments

Comments
 (0)