Skip to content

Commit 03bd92c

Browse files
committed
add error handling
1 parent 6102ba5 commit 03bd92c

File tree

9 files changed

+78
-65
lines changed

9 files changed

+78
-65
lines changed

CodeHawk/CH/xprlib/xsimplify.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ exception XSimplificationProblem of CHPretty.pretty_t
4545

4646

4747
let xpr_to_pretty e = xpr_printer#pr_expr e
48-
let x2p = xpr_to_pretty
48+
(* let x2p = xpr_to_pretty *)
4949

5050

5151
type e_struct_t =
@@ -378,8 +378,6 @@ and reduce_minus (m: bool) (e1: xpr_t) (e2: xpr_t) =
378378

379379
(* ((&x + y) - z) ==> (&x + (y - z)) *)
380380
| (XOp (XPlus, [XOp ((Xf "addressofvar"), [x]); y]), _) ->
381-
let _ = pr_debug [STR " DEBUG: reduce_minus: ";
382-
STR "e1: "; x2p e1; STR "; e2: "; x2p e2; NL] in
383381
rs XPlus [XOp ((Xf "addressofvar"), [x]); XOp (XMinus, [y; e2])]
384382

385383
(* (x << 3) - x) --> (7 * x) *)

CodeHawk/CHB/bchcmdline/bCHXBinaryAnalyzer.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Copyright (c) 2005-2020 Kestrel Technology LLC
88
Copyright (c) 2020 Henny Sipma
9-
Copyright (c) 2021-2024 Aarno Labs LLC
9+
Copyright (c) 2021-2025 Aarno Labs LLC
1010
1111
Permission is hereby granted, free of charge, to any person obtaining a copy
1212
of this software and associated documentation files (the "Software"), to deal
@@ -604,6 +604,8 @@ let main () =
604604
pr_timing [STR "dictionary saved"];
605605
save_global_memory_map ();
606606
pr_timing [STR "global-locations saved"];
607+
arm_analysis_results#save;
608+
pr_timing [STR "analysis results saved"];
607609
save_interface_dictionary ();
608610
pr_timing [STR "interface dictionary saved"];
609611
save_bcdictionary ();

CodeHawk/CHB/bchlib/bCHFloc.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,9 @@ object (self)
718718
let addr = XOp (XPlus, [varx; num_constant_expr numoffset]) in
719719
let address = simplify_xpr (inv#rewrite_expr addr) in
720720
match address with
721+
| XConst (IntConst n) when n#equal CHNumerical.numerical_zero ->
722+
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
723+
^ "Address is zero"]
721724
| XConst (IntConst n) ->
722725
let dw = numerical_mod_to_doubleword n in
723726
if system_info#get_image_base#le dw then
@@ -1904,7 +1907,7 @@ object (self)
19041907
(* if rhs is the address of a global variable create an address-of
19051908
expression for that global variable. *)
19061909
match rhs with
1907-
| XConst (IntConst n) ->
1910+
| XConst (IntConst n) when n#gt CHNumerical.numerical_zero ->
19081911
let dw = numerical_mod_to_doubleword n in
19091912
if memmap#has_location dw then
19101913
TR.tfold
@@ -2091,7 +2094,7 @@ object (self)
20912094
| [RegisterParameter (r, _)] ->
20922095
let argvar = self#env#mk_register_variable r in
20932096
self#rewrite_variable_to_external argvar
2094-
| [GlobalParameter (a, _)] ->
2097+
| [GlobalParameter (a, _)] when not (a#equal wordzero) ->
20952098
let argvar = self#env#mk_global_variable a#to_numerical in
20962099
(match argvar with
20972100
| Error e ->
@@ -2133,7 +2136,7 @@ object (self)
21332136
method evaluate_summary_address_term (t:bterm_t) =
21342137
match t with
21352138
| ArgValue p -> self#evaluate_fts_address_argument p
2136-
| NumConstant num ->
2139+
| NumConstant num when num#gt CHNumerical.numerical_zero ->
21372140
log_tfold_default
21382141
(mk_tracelog_spec
21392142
~tag:"evaluate_summary_address_term"
@@ -2151,7 +2154,7 @@ object (self)
21512154
None)
21522155
None
21532156
(numerical_to_doubleword num)
2154-
| ArgAddressedValue (subT,NumConstant offset) ->
2157+
| ArgAddressedValue (subT, NumConstant offset) ->
21552158
let optBase = self#evaluate_summary_address_term subT in
21562159
begin
21572160
match optBase with

CodeHawk/CHB/bchlib/bCHGlobalMemoryMap.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,10 @@ object (self)
483483
?(initialvalue = None)
484484
?(size = None)
485485
(address: doubleword_int): global_location_int traceresult =
486-
if H.mem locations address#index then
486+
if address#lt (TR.tget_ok (BCHDoubleword.int_to_doubleword 20)) then
487+
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
488+
^ "Illegal global address: " ^ address#to_hex_string]
489+
else if H.mem locations address#index then
487490
begin
488491
ch_error_log#add
489492
"duplicate global location"

CodeHawk/CHB/bchlib/bCHTypeConstraintStore.ml

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -556,60 +556,64 @@ object (self)
556556
| None -> None
557557
| Some (TArray (TComp _ as ty, _, _) as tstructarray) ->
558558
let cinfo = get_struct_type_compinfo ty in
559-
let finfo0 = List.hd cinfo.bcfields in
560-
let ftype = resolve_type finfo0.bftype in
561-
(match ftype with
562-
| Error _ -> None
563-
| Ok ftype ->
564-
let _ixftype = bcd#index_typ ftype in
565-
let _ixctype = bcd#index_typ ty in
566-
let _ =
567-
chlog#add
568-
"first field struct check"
569-
(LBLOCK [
570-
INT offset;
571-
STR ": ";
572-
pretty_print_list
573-
s#toList
574-
(fun i -> STR (btype_to_string (bcd#get_typ i)))
575-
"{" "; " "}";
576-
STR ": compinfo: ";
577-
STR cinfo.bcname;
578-
STR ": first field type: ";
579-
STR (btype_to_string ftype)]) in
580-
(* TBD: restore this check in a better way
581-
if s#fold (fun acc i -> acc && (i = ixftype || i = ixctype)) true then
582-
Some tstructarray
559+
(match cinfo.bcfields with
560+
| [] -> None
561+
| finfo0::_ ->
562+
let ftype = resolve_type finfo0.bftype in
563+
(match ftype with
564+
| Error _ -> None
565+
| Ok ftype ->
566+
let _ixftype = bcd#index_typ ftype in
567+
let _ixctype = bcd#index_typ ty in
568+
let _ =
569+
chlog#add
570+
"first field struct check"
571+
(LBLOCK [
572+
INT offset;
573+
STR ": ";
574+
pretty_print_list
575+
s#toList
576+
(fun i -> STR (btype_to_string (bcd#get_typ i)))
577+
"{" "; " "}";
578+
STR ": compinfo: ";
579+
STR cinfo.bcname;
580+
STR ": first field type: ";
581+
STR (btype_to_string ftype)]) in
582+
(* TBD: restore this check in a better way
583+
if s#fold (fun acc i -> acc && (i = ixftype || i = ixctype)) true then
584+
Some tstructarray
583585
else
584-
None)*)
585-
Some tstructarray)
586+
None)*)
587+
Some tstructarray))
586588
| Some (TComp _ as ty) ->
587589
let cinfo = get_struct_type_compinfo ty in
588-
let finfo0 = List.hd cinfo.bcfields in
589-
let ftype = resolve_type finfo0.bftype in
590-
(match ftype with
591-
| Error _ -> None
592-
| Ok ftype ->
593-
let ixftype = bcd#index_typ ftype in
594-
let ixctype = bcd#index_typ ty in
595-
let _ =
596-
chlog#add
597-
"first field struct check (TComp case)"
598-
(LBLOCK [
599-
INT offset;
600-
STR ": ";
601-
pretty_print_list
602-
s#toList
603-
(fun i -> STR (btype_to_string (bcd#get_typ i)))
604-
"{" "; " "}";
605-
STR ": compinfo: ";
606-
STR cinfo.bcname;
607-
STR ": first field type: ";
608-
STR (btype_to_string ftype)]) in
609-
if s#fold (fun acc i -> acc && (i = ixftype || i = ixctype)) true then
610-
Some ftype
611-
else
612-
None)
590+
(match cinfo.bcfields with
591+
| [] -> None
592+
| finfo0::_ ->
593+
let ftype = resolve_type finfo0.bftype in
594+
(match ftype with
595+
| Error _ -> None
596+
| Ok ftype ->
597+
let ixftype = bcd#index_typ ftype in
598+
let ixctype = bcd#index_typ ty in
599+
let _ =
600+
chlog#add
601+
"first field struct check (TComp case)"
602+
(LBLOCK [
603+
INT offset;
604+
STR ": ";
605+
pretty_print_list
606+
s#toList
607+
(fun i -> STR (btype_to_string (bcd#get_typ i)))
608+
"{" "; " "}";
609+
STR ": compinfo: ";
610+
STR cinfo.bcname;
611+
STR ": first field type: ";
612+
STR (btype_to_string ftype)]) in
613+
if s#fold (fun acc i -> acc && (i = ixftype || i = ixctype)) true then
614+
Some ftype
615+
else
616+
None))
613617
| _ -> None in
614618
let result = new IntCollections.set_t in
615619
begin

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

CodeHawk/CHB/bchlibarm32/bCHARMOperand.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ object (self:'a)
487487
let ivax = floc#inv#rewrite_expr (XVar ivar) in
488488
let xoffset = simplify_xpr (XOp (XPlus, [rx; ivax])) in
489489
(match (xoffset, i) with
490+
| (XConst (IntConst n), 0) when n#equal CHNumerical.numerical_zero ->
491+
Error [__FILE__ ^ ":" ^ (string_of_int __LINE__) ^ ": "
492+
^ "Illegal address (zero) for ARMOffsetAddress"]
490493
| (XConst (IntConst n), 0) ->
491494
floc#env#mk_global_variable ~size n
492495
| _ ->

CodeHawk/CHB/bchlibarm32/bCHDisassembleARM.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ let disassemble_arm_section
163163
INT sectionsize]) in
164164
try
165165
begin
166-
while ch#pos + 2 < sectionsize do (* <= causes problems at section end *)
166+
while ch#pos < sectionsize do (* <= causes problems at section end *)
167167
let prevPos = ch#pos in
168168
let iaddr = sectionbase#add_int ch#pos in
169169
let _ =

CodeHawk/CHB/bchlibarm32/bCHFnARMDictionary.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,7 +2664,7 @@ object (self)
26642664
let xrn_r = rn#to_expr floc in
26652665
let xrm_r = rm#to_expr floc in
26662666
let xxrt_r = TR.tmap rewrite_expr xrt_r in
2667-
let xxrt_r = TR.tbind floc#convert_xpr_offsets xxrt_r in
2667+
let xxrtc_r = TR.tbind floc#convert_xpr_offsets xxrt_r in
26682668
let xxaddr_r = TR.tmap rewrite_expr xaddr_r in
26692669
let lhsvar_r = TR.tbind floc#get_var_at_address xxaddr_r in
26702670
let rdefs =
@@ -2674,7 +2674,7 @@ object (self)
26742674
get_rdef_r xxrt_r] in
26752675
let uses = [get_def_use_r vmem_r] in
26762676
let useshigh = [get_def_use_high_r vmem_r] in
2677-
let xprs_r = [xrn_r; xrm_r; xrt_r; xxrt_r; xaddr_r] in
2677+
let xprs_r = [xrn_r; xrm_r; xrt_r; xxrt_r; xxrtc_r; xaddr_r] in
26782678
let vars_r = [vmem_r; lhsvar_r] in
26792679
let _ =
26802680
floc#memrecorder#record_store_r

0 commit comments

Comments
 (0)