Skip to content

Commit 4794c7a

Browse files
committed
CHB: create more expressive memory-variable representation
1 parent 9dc36c5 commit 4794c7a

14 files changed

+386
-73
lines changed

CodeHawk/CHB/bchlib/bCHBCTypeUtil.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,10 @@ let get_struct_type_compinfo (ty: btype_t): bcompinfo_t =
928928
(LBLOCK [STR "Type is not a struct: "; btype_to_pretty ty]))
929929

930930

931+
let get_compinfo_by_key (ckey: int): bcompinfo_t =
932+
bcfiles#get_compinfo ckey
933+
934+
931935
let get_compinfo_field (c: bcompinfo_t) (fname: string): bfieldinfo_t =
932936
try
933937
List.find (fun finfo -> finfo.bfname = fname) c.bcfields

CodeHawk/CHB/bchlib/bCHBCTypeUtil.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ val get_compinfo_field: bcompinfo_t -> string -> bfieldinfo_t
234234

235235
val get_compinfo_scalar_type_at_offset: bcompinfo_t -> int -> btype_t option
236236

237+
val get_compinfo_by_key: int -> bcompinfo_t
238+
237239

238240
(** {2 Fieldinfos}*)
239241

CodeHawk/CHB/bchlib/bCHFloc.ml

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -686,8 +686,8 @@ object (self)
686686
match address with
687687
| XConst (IntConst n) ->
688688
log_tfold_default
689-
(mk_tracelog_spec
690-
~tag:"get_memory_variable_1"
689+
(log_error
690+
"get_memory_variable_1"
691691
(self#cia ^ ": constant: " ^ n#toString))
692692
(fun base ->
693693
if system_info#get_image_base#le base then
@@ -697,9 +697,24 @@ object (self)
697697
(default ())
698698
(numerical_to_doubleword n)
699699
| XVar v when self#f#env#is_memory_address_variable v ->
700-
self#f#env#mk_memory_address_deref_variable v
700+
log_tfold_default
701+
(log_error
702+
"get_memory_variable_1"
703+
(self#cia ^ ": memory address variable: " ^ (p2s var#toPretty)))
704+
(fun v -> v)
705+
(default ())
706+
(self#env#mk_memory_address_deref_variable v)
707+
| XOp (XPlus, [XVar v; XConst (IntConst n)])
708+
when self#f#env#is_memory_address_variable v ->
709+
log_tfold_default
710+
(log_error
711+
"get_memory_variable_1"
712+
(self#cia ^ ": memory address variable: " ^ (p2s var#toPretty)))
713+
(fun v -> v)
714+
(default ())
715+
(self#env#mk_memory_address_deref_variable ~offset:n#toInt v)
701716
| _ ->
702-
let (memref, memoffset) = self#decompose_address address in
717+
let (memref, memoffset) = self#decompose_address address in
703718
if is_constant_offset memoffset then
704719
let memvar =
705720
if memref#is_global_reference then
@@ -1069,7 +1084,7 @@ object (self)
10691084

10701085
method get_fts_parameter_expr (_p: fts_parameter_t) = None
10711086

1072-
method decompose_array_address
1087+
method decompose_memvar_address
10731088
(x: xpr_t): (memory_reference_int * memory_offset_t) option =
10741089
let _ = chlog#add "decompose_array_address" (LBLOCK [STR "xpr: "; x2p x]) in
10751090
let vars = vars_as_positive_terms x in
@@ -1078,15 +1093,15 @@ object (self)
10781093
match memaddrs with
10791094
| [base] ->
10801095
let (_, _, _, optty) =
1081-
self#f#env#varmgr#get_memory_address_meminfo base in
1096+
TR.tget_ok (self#f#env#varmgr#get_memory_address_meminfo base) in
10821097
let offset = simplify_xpr (XOp (XMinus, [x; XVar base])) in
10831098
Some (XVar base, offset, optty)
10841099
| _ ->
10851100
None in
10861101
match optbase with
10871102
| None -> None
10881103
| Some (_, _, None) -> None
1089-
| Some (XVar base, xoffset, Some ty) ->
1104+
| Some (XVar base, xoffset, Some ty) when is_array_type ty ->
10901105
let _ =
10911106
chlog#add
10921107
"decompose_array_address" (LBLOCK [STR "xoffset: "; x2p xoffset]) in
@@ -1112,6 +1127,25 @@ object (self)
11121127
(match (optmemref, memoffset) with
11131128
| (_, UnknownOffset) -> None
11141129
| (Some memref, memoffset) -> Some (memref, memoffset)
1130+
| _ ->
1131+
None)
1132+
| Some (XVar base, xoffset, Some ty) when is_struct_type ty ->
1133+
let _ =
1134+
chlog#add
1135+
"decompose_struct_address" (LBLOCK [STR "xoffset: "; x2p xoffset]) in
1136+
let optmemref = TR.to_option (self#env#mk_base_variable_reference base) in
1137+
let cinfo = get_struct_type_compinfo ty in
1138+
(match xoffset with
1139+
| XConst (IntConst n) ->
1140+
let optfinfo = get_struct_field_at_offset cinfo n#toInt in
1141+
(match optfinfo with
1142+
| None -> None
1143+
| Some (finfo, rem) when rem = 0 ->
1144+
let memoffset = FieldOffset ((finfo.bfname, cinfo.bckey), NoOffset) in
1145+
(match optmemref with
1146+
| Some memref -> Some (memref, memoffset)
1147+
| _ -> None)
1148+
| _ -> None)
11151149
| _ -> None)
11161150
| _ -> None
11171151

CodeHawk/CHB/bchlib/bCHFunctionInfo.ml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ module TR = CHTraceResult
8585
let bcd = BCHBCDictionary.bcdictionary
8686

8787
let x2p = xpr_formatter#pr_expr
88+
let p2s = pretty_to_string
8889

8990

9091
let log_error (tag: string) (msg: string): tracelogspec_t =
@@ -687,6 +688,10 @@ object (self)
687688
else
688689
Error ["env#get_variable: index not found: " ^ (string_of_int index)]
689690

691+
method get_variable_type (var: variable_t): btype_t option =
692+
varmgr#get_variable_type var
693+
694+
690695
(* -------------------------------------------------------- transactions -- *)
691696

692697
val mutable in_transaction = false
@@ -788,26 +793,25 @@ object (self)
788793
method mk_memory_address_deref_variable
789794
?(size=4)
790795
?(offset=0)
791-
(var: variable_t): variable_t =
792-
if offset = 0 then
793-
if self#is_memory_address_variable var then
794-
let (memrefix, memoffset, optname, _optty) =
795-
varmgr#get_memory_address_meminfo var in
796-
let memref = TR.tget_ok (varmgr#memrefmgr#get_memory_reference memrefix) in
797-
let v = self#mk_index_offset_memory_variable ~size memref memoffset in
798-
let _ =
799-
match optname with
800-
| Some name -> self#set_variable_name v name
801-
| _ -> () in
802-
v
803-
else
804-
raise
805-
(BCH_failure
806-
(LBLOCK [STR "Not a memory address variable"; var#toPretty]))
796+
(var: variable_t): variable_t traceresult =
797+
if self#is_memory_address_variable var then
798+
let memref_r = varmgr#make_memref_from_basevar var in
799+
let optty = tfold_default (fun memref -> memref#get_type) None memref_r in
800+
match optty with
801+
| None ->
802+
Error ["Unknown type for memory address variable: " ^ (p2s var#toPretty)]
803+
| Some ty when is_struct_type ty ->
804+
let memoffset = mk_maximal_memory_offset (mkNumerical offset) ty in
805+
tmap
806+
(fun memref ->
807+
self#mk_index_offset_memory_variable ~size memref memoffset)
808+
memref_r
809+
| Some ty ->
810+
Error [
811+
"mk_memory_address_deref_variable: type is not a struct type: "
812+
^ (p2s var#toPretty) ^ " (" ^ (btype_to_string ty) ^ ")"]
807813
else
808-
raise
809-
(BCH_failure
810-
(LBLOCK [STR "Nonstandard size or offset not yet supported"]))
814+
Error ["Not a memory address variable: " ^ (p2s var#toPretty)]
811815

812816
method mk_index_offset_global_memory_variable
813817
?(elementsize=4)

0 commit comments

Comments
 (0)