Skip to content

Commit 25b13ec

Browse files
committed
CHB: some infrastructure for stackframe management
1 parent fe5c470 commit 25b13ec

File tree

5 files changed

+226
-12
lines changed

5 files changed

+226
-12
lines changed

CodeHawk/CHB/bchlib/bCHFunctionInfo.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,7 @@ end
16031603

16041604
class function_info_t
16051605
(faddr: doubleword_int)
1606+
(fndata: function_data_int)
16061607
(varmgr: variable_manager_int)
16071608
(invio: invariant_io_int)
16081609
(varinvio: var_invariant_io_int):function_info_int =
@@ -1650,7 +1651,7 @@ object (self)
16501651
val mutable call_targets_set = false
16511652
val nonreturning_calls = new StringCollections.set_t
16521653
val mutable invariants_to_be_reset = false
1653-
val stackframe = mk_function_stackframe varmgr
1654+
val stackframe = mk_function_stackframe fndata varmgr
16541655
val proofobligations =
16551656
mk_proofobligations faddr (mk_xpodictionary varmgr#vard#xd)
16561657

@@ -2610,7 +2611,8 @@ let load_function_info ?(reload=false) (faddr:doubleword_int) =
26102611
let varmgr = make_variable_manager faddr xvard in
26112612
let invio = read_invs fname varmgr#vard in
26122613
let varinvio = read_varinvs fname varmgr#vard in
2613-
let finfo = new function_info_t faddr varmgr invio varinvio in
2614+
let fndata = functions_data#get_function faddr in
2615+
let finfo = new function_info_t faddr fndata varmgr invio varinvio in
26142616
let _ =
26152617
match extract_function_info_file fname with
26162618
| Some node -> finfo#read_xml node

CodeHawk/CHB/bchlib/bCHFunctionStackframe.ml

Lines changed: 165 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
66
7-
Copyright (c) 2023 Aarno Labs LLC
7+
Copyright (c) 2023-2025 Aarno Labs LLC
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal
@@ -33,19 +33,22 @@ open CHUtils
3333

3434
(* chutil *)
3535
open CHLogger
36+
open CHTraceResult
3637
open CHXmlDocument
3738

3839
(* xprlib *)
3940
open XprTypes
4041

4142
(* bchlib *)
4243
open BCHBasicTypes
44+
open BCHBCTypePretty
4345
open BCHBCTypes
4446
open BCHBCTypeUtil
4547
open BCHCPURegisters
4648
open BCHLibTypes
4749

4850
module H = Hashtbl
51+
module TR = CHTraceResult
4952

5053

5154
let bd = BCHDictionary.bdictionary
@@ -117,11 +120,98 @@ object (_self:'a)
117120
end
118121

119122

120-
class stackframe_t (varmgr: variable_manager_int):stackframe_int =
123+
let stackslot_rec_to_pretty (sslot: stackslot_rec_t): pretty_t =
124+
LBLOCK [
125+
STR "offset: "; INT sslot.sslot_offset; NL;
126+
STR "name : "; STR sslot.sslot_name; NL;
127+
STR "type : "; STR (btype_to_string sslot.sslot_btype); NL;
128+
(match sslot.sslot_size with
129+
| Some s -> LBLOCK [STR "size : "; INT s; NL]
130+
| _ -> STR "");
131+
(match sslot.sslot_desc with
132+
| Some desc -> LBLOCK [STR "desc : "; STR desc; NL]
133+
| _ -> STR "")
134+
]
135+
136+
137+
let opt_size_to_string (optsize: int option): string =
138+
match optsize with
139+
| Some s -> (string_of_int s)
140+
| _ -> "?"
141+
142+
143+
class stackslot_t (sslot: stackslot_rec_t): stackslot_int =
144+
object
145+
146+
method sslot_rec = sslot
147+
148+
method name = sslot.sslot_name
149+
150+
method offset = sslot.sslot_offset
151+
152+
method btype = sslot.sslot_btype
153+
154+
method size = sslot.sslot_size
155+
156+
method desc = sslot.sslot_desc
157+
158+
method contains_offset (_offset: int) = false
159+
160+
method frame2object_offset_value (_xpr: xpr_t) = Error ["Not yet implemenented"]
161+
162+
method frame_offset_memory_offset
163+
?(tgtsize=None)
164+
?(tgtbtype=t_unknown)
165+
(_xpr: xpr_t): memory_offset_t traceresult =
166+
Error ["Not yet implemented for btype: "
167+
^ (btype_to_string tgtbtype)
168+
^ " and size "
169+
^ (opt_size_to_string tgtsize)]
170+
171+
method object_offset_memory_offset
172+
?(tgtsize=None)
173+
?(tgtbtype=t_unknown)
174+
(_xpr: xpr_t): memory_offset_t traceresult =
175+
Error ["Not yet implemented: "
176+
^ (btype_to_string tgtbtype)
177+
^ " and size "
178+
^ (opt_size_to_string tgtsize)]
179+
180+
method write_xml(_node: xml_element_int) = ()
181+
182+
end
183+
184+
185+
class stackframe_t
186+
(fndata: function_data_int)
187+
(varmgr: variable_manager_int):stackframe_int =
121188
object (self)
122189

123190
val saved_registers = H.create 3 (* reg -> saved_register_t *)
124191
val accesses = H.create 3 (* offset -> (iaddr, stack_access_t) list *)
192+
val stackslots =
193+
let slots = H.create 3 in (* offset -> stackslot *)
194+
begin
195+
(match fndata#get_function_annotation with
196+
| Some fnannot ->
197+
List.iter (fun svintro ->
198+
let ty = match svintro.svi_vartype with
199+
| Some ty -> ty
200+
| _ -> t_unknown in
201+
let size = TR.to_option (size_of_btype ty) in
202+
let sslot = {
203+
sslot_offset = svintro.svi_offset;
204+
sslot_name = svintro.svi_name;
205+
sslot_btype = ty;
206+
sslot_desc = Some ("svintro");
207+
sslot_size = size} in
208+
let stackslot = new stackslot_t sslot in
209+
let _ =
210+
chlog#add "stack slot added" (stackslot_rec_to_pretty sslot) in
211+
H.add slots svintro.svi_offset stackslot) fnannot.stackvarintros
212+
| _ -> ());
213+
slots
214+
end
125215

126216
method private vard = varmgr#vard
127217

@@ -136,6 +226,79 @@ object (self)
136226
[] in
137227
H.replace accesses offset ((iaddr, acc) :: entry)
138228

229+
method containing_stackslot (offset: int): stackslot_int option =
230+
H.fold (fun _ sslot acc ->
231+
match acc with
232+
| Some _ -> acc
233+
| _ -> if sslot#contains_offset offset then Some sslot else None)
234+
stackslots None
235+
236+
method add_stackslot
237+
?(name = None)
238+
?(btype = t_unknown)
239+
?(size = None)
240+
?(desc = None)
241+
(offset: int): stackslot_int traceresult =
242+
if offset <= 0 then
243+
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
244+
^ "Illegal offset for stack slot: "
245+
^ (string_of_int offset)
246+
^ ". Offset should be greater than zero."]
247+
else if H.mem stackslots offset then
248+
begin
249+
log_error_result
250+
~tag:"duplicate stack slot"
251+
~msg:("Stack slot at offset "
252+
^ (string_of_int offset)
253+
^ "already exists")
254+
__FILE__ __LINE__ [];
255+
Ok (H.find stackslots offset)
256+
end
257+
else
258+
match self#containing_stackslot offset with
259+
| Some sslot ->
260+
let msg =
261+
"Stackslot at offset "
262+
^ (string_of_int offset)
263+
^ " overlaps with "
264+
^ sslot#name
265+
^ " ("
266+
^ (string_of_int sslot#offset)
267+
^ (match sslot#size with
268+
| Some s -> ", size: " ^ (string_of_int s)
269+
| _ -> "")
270+
^ ")" in
271+
begin
272+
log_error_result
273+
~tag:"overlapping stackslot"
274+
~msg
275+
__FILE__ __LINE__ [];
276+
Error [msg]
277+
end
278+
| _ ->
279+
let sname =
280+
match name with
281+
| Some name -> name
282+
| _ -> "var_" ^ (string_of_int offset) in
283+
let ssrec = {
284+
sslot_name = sname;
285+
sslot_offset = offset;
286+
sslot_btype = btype;
287+
sslot_size = size;
288+
sslot_desc = desc
289+
} in
290+
let sslot = new stackslot_t ssrec in
291+
begin
292+
H.add stackslots offset sslot;
293+
chlog#add
294+
"stackframe:add stackslot"
295+
(LBLOCK [
296+
INT offset;
297+
STR ": ";
298+
STR sslot#name]);
299+
Ok sslot
300+
end
301+
139302
method add_register_spill
140303
~(offset: int) (reg: register_t) (iaddr:ctxt_iaddress_t) =
141304
let spill = RegisterSpill (offset, reg) in

CodeHawk/CHB/bchlib/bCHFunctionStackframe.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
------------------------------------------------------------------------------
55
The MIT License (MIT)
66
7-
Copyright (c) 2023-2024 Aarno Labs LLC
7+
Copyright (c) 2023-2025 Aarno Labs LLC
88
99
Permission is hereby granted, free of charge, to any person obtaining a copy
1010
of this software and associated documentation files (the "Software"), to deal
@@ -25,9 +25,13 @@
2525
SOFTWARE.
2626
============================================================================= *)
2727

28+
(* chlib *)
29+
open CHPretty
2830

2931
(* bchlib *)
3032
open BCHLibTypes
3133

34+
val stackslot_rec_to_pretty: stackslot_rec_t -> pretty_t
3235

33-
val mk_function_stackframe: variable_manager_int -> stackframe_int
36+
val mk_function_stackframe:
37+
function_data_int -> variable_manager_int -> stackframe_int

CodeHawk/CHB/bchlib/bCHGlobalMemoryMap.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,12 @@ object (self)
541541
^ "Illegal global address: " ^ address#to_hex_string]
542542
else if H.mem locations address#index then
543543
begin
544-
ch_error_log#add
545-
"duplicate global location"
546-
(LBLOCK [
547-
STR "Global location at address ";
548-
address#toPretty;
549-
STR " already exists"]);
544+
log_error_result
545+
~tag:"duplicate global location"
546+
~msg:("Global location at address "
547+
^ address#to_hex_string
548+
^ "already exists")
549+
__FILE__ __LINE__ [];
550550
Ok (H.find locations address#index)
551551
end
552552
else

CodeHawk/CHB/bchlib/bCHLibTypes.mli

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5189,6 +5189,41 @@ class type xpodictionary_int =
51895189
end
51905190

51915191

5192+
type stackslot_rec_t = {
5193+
sslot_name: string;
5194+
sslot_offset: int;
5195+
sslot_btype: btype_t;
5196+
sslot_size: int option;
5197+
sslot_desc: string option
5198+
}
5199+
5200+
5201+
class type stackslot_int =
5202+
object
5203+
5204+
method sslot_rec: stackslot_rec_t
5205+
method name: string
5206+
method offset: int
5207+
method btype: btype_t
5208+
method size: int option
5209+
method desc: string option
5210+
method contains_offset: int -> bool
5211+
method frame2object_offset_value: xpr_t -> xpr_t traceresult
5212+
method frame_offset_memory_offset:
5213+
?tgtsize:int option
5214+
-> ?tgtbtype:btype_t
5215+
-> xpr_t
5216+
-> memory_offset_t traceresult
5217+
method object_offset_memory_offset:
5218+
?tgtsize:int option
5219+
-> ?tgtbtype:btype_t
5220+
-> xpr_t
5221+
-> memory_offset_t traceresult
5222+
5223+
method write_xml: xml_element_int -> unit
5224+
end
5225+
5226+
51925227
class type stackframe_int =
51935228
object
51945229

@@ -5197,6 +5232,16 @@ class type stackframe_int =
51975232
method add_register_restore:
51985233
offset:int -> register_t -> ctxt_iaddress_t -> unit
51995234

5235+
method add_stackslot:
5236+
?name: string option
5237+
-> ?btype: btype_t
5238+
-> ?size: int option
5239+
-> ?desc: string option
5240+
-> int
5241+
-> stackslot_int traceresult
5242+
5243+
method containing_stackslot: int -> stackslot_int option
5244+
52005245
method add_load:
52015246
offset:int
52025247
-> size:int option

0 commit comments

Comments
 (0)