Skip to content

Commit 93fadb0

Browse files
committed
CHB:fix zero-offset accesses to global arrays
1 parent 69440e5 commit 93fadb0

File tree

8 files changed

+170
-96
lines changed

8 files changed

+170
-96
lines changed

CodeHawk/CHB/bchlib/bCHFloc.ml

Lines changed: 96 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,13 @@ let x2p = xpr_formatter#pr_expr
9090
let p2s = pretty_to_string
9191
let x2s x = p2s (x2p x)
9292

93-
let opt_size_to_string (s: int option) =
94-
match s with
95-
| Some i -> "size:" ^ (string_of_int i)
96-
| _ -> "size:None"
93+
let opti2s (i: int option) =
94+
if Option.is_some i then string_of_int (Option.get i) else "?"
9795

98-
let opt_type_to_string (t: btype_t option) =
99-
match t with
100-
| Some t -> "btype:" ^ (btype_to_string t)
101-
| _ -> "btype:None"
96+
let _ty2s (ty: btype_t) =
97+
if is_unknown_type ty then "?" else btype_to_string ty
98+
let optty2s (ty: btype_t option) =
99+
if Option.is_some ty then btype_to_string (Option.get ty) else "?"
102100

103101

104102
let log_error (tag: string) (msg: string): tracelogspec_t =
@@ -665,6 +663,14 @@ object (self)
665663
?(size=4)
666664
(var: variable_t)
667665
(numoffset: numerical_t): variable_t traceresult =
666+
let _ =
667+
log_diagnostics_result
668+
~msg:(p2s self#l#toPretty)
669+
~tag:"get-memory-variable-numoffset"
670+
__FILE__ __LINE__
671+
["size: " ^ (string_of_int size);
672+
"var: " ^ (p2s var#toPretty);
673+
"numoffset: " ^ (numoffset#toString)] in
668674
let inv = self#inv in
669675
let mk_memvar memref_r memoffset_r =
670676
let _ =
@@ -1512,9 +1518,9 @@ object (self)
15121518
~msg:(p2s self#l#toPretty)
15131519
~tag:"convert_xpr_to_c_expr"
15141520
__FILE__ __LINE__
1515-
[(opt_size_to_string size) ^ "; "
1516-
^ (opt_type_to_string xtype) ^ "; "
1517-
^ "x: " ^ (x2s x)] in
1521+
["size: " ^ (opti2s size);
1522+
"xtype: " ^ (optty2s xtype);
1523+
"x: " ^ (x2s x)] in
15181524
match xtype with
15191525
| None -> self#convert_xpr_offsets ~size x
15201526
| Some t ->
@@ -1598,8 +1604,8 @@ object (self)
15981604
TR.tmap (fun v -> XVar v) var_r
15991605
| _ ->
16001606
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
1601-
^ (opt_size_to_string size) ^ "; "
1602-
^ (opt_type_to_string xtype) ^ "; "
1607+
^ "size: " ^ (opti2s size) ^ "; "
1608+
^ "type: " ^ (optty2s xtype) ^ "; "
16031609
^ "addr: " ^ (x2s a)
16041610
^ ": Not yet handled"]
16051611

@@ -1609,8 +1615,8 @@ object (self)
16091615
| None -> self#convert_variable_offsets ~size v
16101616
| _ ->
16111617
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
1612-
^ (opt_size_to_string size) ^ "; "
1613-
^ (opt_type_to_string vtype) ^ "; "
1618+
^ "size: " ^ (opti2s size) ^ "; "
1619+
^ "type: " ^ (optty2s vtype) ^ "; "
16141620
^ "v: " ^ (p2s v#toPretty)
16151621
^ ": Not yet implemented"]
16161622

@@ -1672,14 +1678,22 @@ object (self)
16721678
memref_r memoff_r
16731679
| _ ->
16741680
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
1675-
^ (opt_size_to_string size) ^ "; "
1676-
^ (opt_type_to_string vtype) ^ "; "
1681+
^ "size: " ^ (opti2s size) ^ "; "
1682+
^ "vtype: " ^ (optty2s vtype) ^ "; "
16771683
^ "addr: " ^ (x2s a)
16781684
^ ": Not yet handled"]
16791685

16801686

16811687
method convert_variable_offsets
16821688
?(vtype=None) ?(size=None) (v: variable_t): variable_t traceresult =
1689+
let _ =
1690+
log_diagnostics_result
1691+
~msg:(p2s self#l#toPretty)
1692+
~tag:"convert-variable-offsets"
1693+
__FILE__ __LINE__
1694+
["vtype: " ^ (optty2s vtype);
1695+
"size: " ^ (opti2s size);
1696+
"v: " ^ (p2s v#toPretty)] in
16831697
if self#env#is_basevar_memory_variable v then
16841698
let basevar_r = self#env#get_memvar_basevar v in
16851699
let offset_r = self#env#get_memvar_offset v in
@@ -1737,6 +1751,12 @@ object (self)
17371751

17381752
method convert_value_offsets
17391753
?(size=None) (v: variable_t): variable_t traceresult =
1754+
let _ =
1755+
log_diagnostics_result
1756+
~msg:(p2s self#l#toPretty)
1757+
~tag:"convert-value-offsets"
1758+
__FILE__ __LINE__
1759+
["size: " ^ (opti2s size); "v: " ^ (p2s v#toPretty)] in
17401760
if self#env#is_basevar_memory_value v then
17411761
let basevar_r = self#env#get_memval_basevar v in
17421762
let offset_r = self#env#get_memval_offset v in
@@ -1812,6 +1832,12 @@ object (self)
18121832

18131833
method convert_xpr_offsets
18141834
?(xtype=None) ?(size=None) (x: xpr_t): xpr_t traceresult =
1835+
let _ =
1836+
log_diagnostics_result
1837+
~msg:(p2s self#l#toPretty)
1838+
~tag:"convert-xpr-offsets"
1839+
__FILE__ __LINE__
1840+
["xtype: " ^ (optty2s xtype); "size: " ^ (opti2s size); "x: " ^ (x2s x)] in
18151841
let rec aux exp =
18161842
match exp with
18171843
| XVar v when self#env#is_basevar_memory_value v ->
@@ -2349,41 +2375,51 @@ object (self)
23492375
rhs
23502376
| _ -> rhs in
23512377

2352-
let rhs =
2353-
(* if rhs is a composite symbolic expression, create a new variable
2378+
if self#f#env#is_global_variable lhs then
2379+
let _ =
2380+
log_diagnostics_result
2381+
~msg:(p2s self#l#toPretty)
2382+
~tag:("get_assign_cmds_r: abstract global variable")
2383+
__FILE__ __LINE__
2384+
["lhs: " ^ (p2s lhs#toPretty); "rhs: " ^ (x2s rhs)] in
2385+
[ABSTRACT_VARS [lhs]]
2386+
2387+
else
2388+
let rhs =
2389+
(* if rhs is a composite symbolic expression, create a new variable
23542390
for it *)
2355-
if self#is_composite_symbolic_value rhs then
2356-
XVar (self#env#mk_symbolic_value rhs)
2357-
else
2358-
rhs in
2359-
let reqN () = self#env#mk_num_temp in
2360-
let reqC = self#env#request_num_constant in
2361-
let (rhscmds, rhs_c) = xpr_to_numexpr reqN reqC rhs in
2362-
let cmds = rhscmds @ [ASSIGN_NUM (lhs, rhs_c)] in
2363-
let fndata = self#f#get_function_data in
2364-
match fndata#get_regvar_intro self#ia with
2365-
| Some rvi when rvi.rvi_cast && Option.is_some rvi.rvi_vartype ->
2366-
TR.tfold
2367-
~ok:(fun reg ->
2368-
let ty = Option.get rvi.rvi_vartype in
2369-
let tcvar =
2370-
self#f#env#mk_typecast_value self#cia rvi.rvi_name ty reg in
2371-
begin
2372-
log_result __FILE__ __LINE__
2373-
["Create typecast var for "
2374-
^ (register_to_string reg)
2375-
^ " at "
2376-
^ self#cia];
2377-
cmds @ [ASSIGN_NUM (lhs, NUM_VAR tcvar)]
2378-
end)
2379-
~error:(fun e ->
2380-
begin
2381-
log_error_result __FILE__ __LINE__
2382-
("expected a register variable" :: e);
2383-
cmds
2384-
end)
2385-
(self#f#env#get_register lhs)
2386-
| _ -> cmds
2391+
if self#is_composite_symbolic_value rhs then
2392+
XVar (self#env#mk_symbolic_value rhs)
2393+
else
2394+
rhs in
2395+
let reqN () = self#env#mk_num_temp in
2396+
let reqC = self#env#request_num_constant in
2397+
let (rhscmds, rhs_c) = xpr_to_numexpr reqN reqC rhs in
2398+
let cmds = rhscmds @ [ASSIGN_NUM (lhs, rhs_c)] in
2399+
let fndata = self#f#get_function_data in
2400+
match fndata#get_regvar_intro self#ia with
2401+
| Some rvi when rvi.rvi_cast && Option.is_some rvi.rvi_vartype ->
2402+
TR.tfold
2403+
~ok:(fun reg ->
2404+
let ty = Option.get rvi.rvi_vartype in
2405+
let tcvar =
2406+
self#f#env#mk_typecast_value self#cia rvi.rvi_name ty reg in
2407+
begin
2408+
log_result __FILE__ __LINE__
2409+
["Create typecast var for "
2410+
^ (register_to_string reg)
2411+
^ " at "
2412+
^ self#cia];
2413+
cmds @ [ASSIGN_NUM (lhs, NUM_VAR tcvar)]
2414+
end)
2415+
~error:(fun e ->
2416+
begin
2417+
log_error_result __FILE__ __LINE__
2418+
("expected a register variable" :: e);
2419+
cmds
2420+
end)
2421+
(self#f#env#get_register lhs)
2422+
| _ -> cmds
23872423

23882424
(* Note: recording of loads and stores is performed by the different
23892425
architectures directly in FnXXXDictionary.*)
@@ -2448,6 +2484,15 @@ object (self)
24482484
[OPERATION ({ op_name = unknown_write_symbol; op_args = op_args});
24492485
ASSIGN_NUM (lhs, rhs)]
24502486

2487+
else if self#f#env#is_global_variable lhs then
2488+
let _ =
2489+
log_diagnostics_result
2490+
~msg:(p2s self#l#toPretty)
2491+
~tag:("get_assign_cmds: abstract global variable")
2492+
__FILE__ __LINE__
2493+
["lhs: " ^ (p2s lhs#toPretty); "rhs: " ^ (x2s rhs_expr)] in
2494+
[ABSTRACT_VARS [lhs]]
2495+
24512496
else
24522497
rhsCmds @ [ASSIGN_NUM (lhs, rhs)]
24532498

CodeHawk/CHB/bchlib/bCHFunctionInfo.ml

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -851,29 +851,23 @@ object (self)
851851
let gvar =
852852
self#mk_variable
853853
(self#varmgr#make_global_variable gloc#address#to_numerical) in
854-
let ivar = self#mk_variable (varmgr#make_initial_memory_value gvar) in
855-
if dw#equal gloc#address then
856-
begin
857-
self#set_variable_name gvar gloc#name;
858-
self#set_variable_name ivar (gloc#name ^ "_in");
859-
Ok gvar
860-
end
861-
else
862-
tmap
863-
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": memref:global")
864-
(fun offset ->
865-
let gvar =
866-
self#mk_variable
867-
(self#varmgr#make_global_variable
868-
~size ~offset gloc#address#to_numerical) in
869-
let ivar = self#mk_variable (varmgr#make_initial_memory_value gvar) in
870-
let name = gloc#name ^ (memory_offset_to_string offset) in
871-
begin
872-
self#set_variable_name gvar name;
873-
self#set_variable_name ivar (name ^ "_in");
874-
gvar
875-
end)
876-
(gloc#address_memory_offset ~tgtbtype:btype loc (num_constant_expr base))
854+
let _ivar = self#mk_variable (varmgr#make_initial_memory_value gvar) in
855+
tmap
856+
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": memref:global")
857+
(fun offset ->
858+
let gvar =
859+
self#mk_variable
860+
(self#varmgr#make_global_variable
861+
~size ~offset gloc#address#to_numerical) in
862+
let ivar = self#mk_variable (varmgr#make_initial_memory_value gvar) in
863+
let name = gloc#name ^ (memory_offset_to_string offset) in
864+
begin
865+
self#set_variable_name gvar name;
866+
self#set_variable_name ivar (name ^ "_in");
867+
gvar
868+
end)
869+
(gloc#address_memory_offset
870+
~tgtsize:(Some size) ~tgtbtype:btype loc (num_constant_expr base))
877871
| _ ->
878872
let _ = memmap#add_location ~size:(Some size) ~btype dw in
879873
Ok (self#mk_variable (self#varmgr#make_global_variable dw#to_numerical))
@@ -1223,6 +1217,9 @@ object (self)
12231217
method get_memval_offset (v:variable_t): memory_offset_t traceresult =
12241218
varmgr#get_memval_offset v
12251219

1220+
method get_memvar_dependencies (v: variable_t): variable_t list =
1221+
varmgr#get_memvar_dependencies v
1222+
12261223
method get_constant_offsets (v: variable_t): numerical_t list traceresult =
12271224
let offset_r =
12281225
if self#is_initial_memory_value v then

CodeHawk/CHB/bchlib/bCHLibTypes.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,6 +3618,8 @@ object ('a)
36183618
Returns [Error] if this variable is not a register variable. *)
36193619
method get_register: register_t traceresult
36203620

3621+
method get_memvar_dependencies: variable_t list
3622+
36213623
(** Returns the memory reference associated with this memory variable.
36223624
36233625
Returns [Error] if this variable is not a memory variable. *)
@@ -4063,6 +4065,8 @@ object
40634065
initial-value memory variable. *)
40644066
method get_initial_memory_value_variable: variable_t -> variable_t traceresult
40654067

4068+
method get_memvar_dependencies: variable_t -> variable_t list
4069+
40664070

40674071
(** {2 Memory offsets} *)
40684072

@@ -4944,6 +4948,8 @@ class type function_environment_int =
49444948

49454949
method has_variable_index_offset: variable_t -> bool
49464950

4951+
method get_memvar_dependencies: variable_t -> variable_t list
4952+
49474953

49484954
(** {2 Memory offsets} *)
49494955

CodeHawk/CHB/bchlib/bCHVariable.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ object (self:'a)
155155
else
156156
name
157157

158+
method get_memvar_dependencies: variable_t list =
159+
match denotation with
160+
| MemoryVariable (_, _, offset) ->
161+
(match offset with
162+
| ArrayIndexOffset (x, _) -> Xprt.variables_in_expr x
163+
| BasePtrArrayIndexOffset (x, _) -> Xprt.variables_in_expr x
164+
| ConstantOffset (_, ArrayIndexOffset (x, _)) -> Xprt.variables_in_expr x
165+
| _ -> [])
166+
| _ -> []
167+
158168
method private get_memref_type (index: int) (_size: int): btype_t option =
159169
memrefmgr#get_memory_reference_type index
160170

@@ -562,6 +572,12 @@ object (self)
562572
None
563573
(self#get_variable v)
564574

575+
method get_memvar_dependencies (v: variable_t): variable_t list =
576+
tfold_default
577+
(fun var -> var#get_memvar_dependencies)
578+
[]
579+
(self#get_variable v)
580+
565581
method get_memvar_reference (v: variable_t): memory_reference_int traceresult =
566582
tbind
567583
~msg:(__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": " ^ (p2s v#toPretty))

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_20250821"
99-
~date:"2025-08-21"
98+
~version:"0.6.0_20250822"
99+
~date:"2025-08-22"
100100
~licensee: None
101101
~maxfilesize: None
102102
()

CodeHawk/CHB/bchlibarm32/bCHTranslateARMToCHIF.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,24 @@ let translate_arm_instruction
31723172
TR.tfold
31733173
~ok:(fun (memlhs, memcmds) ->
31743174
let cmds = floc#get_assign_commands_r (Ok memlhs) xrt_r in
3175-
let defcmds = floc#get_vardef_commands ~defs:[memlhs] ctxtiaddr in
3175+
let memvardeps = floc#f#env#get_memvar_dependencies memlhs in
3176+
let usehigh =
3177+
List.filter (fun v -> not (floc#f#env#is_function_initial_value v))
3178+
memvardeps in
3179+
let _ =
3180+
log_diagnostics_result
3181+
~msg:(p2s floc#l#toPretty)
3182+
~tag:"translate memlhs storeregister"
3183+
__FILE__ __LINE__
3184+
["memlhs: " ^ (p2s memlhs#toPretty);
3185+
"memvardeps: "
3186+
^ (String.concat
3187+
", " (List.map (fun v -> p2s v#toPretty) memvardeps));
3188+
"usehigh: "
3189+
^ (String.concat
3190+
", " (List.map (fun v -> p2s v#toPretty) usehigh))] in
3191+
let defcmds =
3192+
floc#get_vardef_commands ~defs:[memlhs] ~usehigh ctxtiaddr in
31763193
memcmds @ cmds @ defcmds)
31773194
~error:(fun e ->
31783195
let xrn_r = rn#to_expr floc in

0 commit comments

Comments
 (0)