Skip to content
15 changes: 12 additions & 3 deletions CodeHawk/CHB/bchlib/bCHFloc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,7 @@ object (self)
TR.tfold
~ok:(fun optty ->
match optty with
| Some ty -> Ok ty
| LhsResolved ty -> Ok ty
| _ ->
Error [
(elocm __LINE__);
Expand Down Expand Up @@ -3412,6 +3412,8 @@ object (self)
(List.length vars) > 0
&& List.for_all is_fixed_type (variables_in_expr x)

(* Note: the rhs gets reduced by taking the modulo with the operand max size.
Default operand width is 32 bits. *)
method get_assign_commands_r
?(signed=false)
?(size=4)
Expand Down Expand Up @@ -3448,13 +3450,20 @@ object (self)
if not signed then
match rhs with
| XConst (IntConst n) ->
let n =
let n' =
match size with
| 1 -> n#modulo numerical_e8
| 2 -> n#modulo numerical_e16
| 4 -> n#modulo numerical_e32
| _ -> n in
num_constant_expr n
let _ =
if not (n#equal n') then
log_diagnostics_result
~msg:(p2s self#l#toPretty)
~tag:"get_assign_commands_r: rhs reduced to fit size"
__FILE__ __LINE__
[n#toString ^ " truncated to " ^ n'#toString] in
num_constant_expr n'
| _ -> rhs
else
rhs in
Expand Down
21 changes: 11 additions & 10 deletions CodeHawk/CHB/bchlib/bCHFunctionData.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ object (self)
val mutable functiontype = t_unknown
val mutable callsites = 0
val mutable pathcontexts = [] (* (ctxt_iaddress_t, ctxt_iaddress_t list) list *)
val mutable reglhstypes = [] (* (register_t * iaddr * btype_t option) list *)
val mutable stacklhstypes = [] (* (offset * btype_t option) list *)
val mutable reglhstypes = [] (* (register_t * iaddr * lhs_type_result_t) list *)
val mutable stacklhstypes = [] (* (offset * lhs_type_result_t) list *)

method set_function_type (ty: btype_t) = functiontype <- ty

Expand Down Expand Up @@ -194,29 +194,30 @@ object (self)

method has_callsites = callsites > 0

method set_reglhs_types (regtypes: (register_t * string * btype_t option) list) =
method set_reglhs_types
(regtypes: (register_t * string * lhs_type_result_t) list) =
reglhstypes <- regtypes

method get_reglhs_types = reglhstypes

method get_reglhs_type (reg: register_t) (iaddr: string): btype_t option =
method get_reglhs_type (reg: register_t) (iaddr: string): lhs_type_result_t =
List.fold_left (fun acc (r, i, t) ->
match acc with
| Some _ -> acc
| LhsResolved _ | LhsConflict _ -> acc
| _ ->
if BCHCPURegisters.register_equal reg r && i = iaddr then t else acc)
None reglhstypes
LhsNoEvidence reglhstypes

method set_stack_offset_types (stacktypes: (int * btype_t option) list) =
method set_stack_offset_types (stacktypes: (int * lhs_type_result_t) list) =
stacklhstypes <- stacktypes

method get_stack_offset_types = stacklhstypes

method get_stack_offset_type (offset: int): btype_t option =
method get_stack_offset_type (offset: int): lhs_type_result_t =
List.fold_left (fun acc (o, t) ->
match acc with
| Some _ -> acc
| _ -> if offset == o then t else acc) None stacklhstypes
| LhsResolved _ | LhsConflict _ -> acc
| _ -> if offset == o then t else acc) LhsNoEvidence stacklhstypes

method add_name (s:string) =
let s = sanitize_function_name s in
Expand Down
28 changes: 18 additions & 10 deletions CodeHawk/CHB/bchlib/bCHLibTypes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,13 @@ type function_annotation_t = {
constglobalvars: string list
}


type lhs_type_result_t =
| LhsResolved of btype_t
| LhsNoEvidence
| LhsConflict of btype_t list


class type function_data_int =
object

Expand Down Expand Up @@ -1613,9 +1620,10 @@ class type function_data_int =
returns the number of remaining call sites.*)
method remove_callsite: int

method set_reglhs_types: ((register_t * string * btype_t option) list) -> unit
method set_reglhs_types:
((register_t * string * lhs_type_result_t) list) -> unit

method set_stack_offset_types: ((int * btype_t option) list) -> unit
method set_stack_offset_types: ((int * lhs_type_result_t) list) -> unit

(** {1 Function annotations} *)

Expand Down Expand Up @@ -1643,10 +1651,10 @@ class type function_data_int =

method get_inlined_blocks: doubleword_int list
method get_path_contexts: (string * string list) list
method get_reglhs_types: (register_t * string * btype_t option) list
method get_reglhs_type: register_t -> string -> btype_t option
method get_stack_offset_types: (int * btype_t option) list
method get_stack_offset_type: int -> btype_t option
method get_reglhs_types: (register_t * string * lhs_type_result_t) list
method get_reglhs_type: register_t -> string -> lhs_type_result_t
method get_stack_offset_types: (int * lhs_type_result_t) list
method get_stack_offset_type: int -> lhs_type_result_t

(* predicates *)

Expand Down Expand Up @@ -3526,14 +3534,14 @@ class type type_constraint_store_int =
format-string parameter, implying the function is variadic. *)

method resolve_reglhs_type:
register_t -> string -> string -> btype_t option
register_t -> string -> string -> lhs_type_result_t

method resolve_stack_lhs_type: int -> string -> btype_t option
method resolve_stack_lhs_type: int -> string -> lhs_type_result_t

method resolve_reglhs_types:
string -> (register_t * string * btype_t option) list
string -> (register_t * string * lhs_type_result_t) list

method resolve_local_stack_lhs_types: string -> (int * btype_t option) list
method resolve_local_stack_lhs_types: string -> (int * lhs_type_result_t) list

method write_xml: xml_element_int -> unit

Expand Down
35 changes: 19 additions & 16 deletions CodeHawk/CHB/bchlib/bCHTypeConstraintStore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ object (self)
(result, !returnresult, varargs)

method resolve_reglhs_type
(reg: register_t) (faddr: string) (iaddr: string): btype_t option =
(reg: register_t) (faddr: string) (iaddr: string): lhs_type_result_t =
let evaluation = self#evaluate_reglhs_type reg faddr iaddr in
let log_evaluation () =
log_diagnostics_result
Expand Down Expand Up @@ -825,26 +825,26 @@ object (self)
if result#isEmpty then
begin
log_evaluation ();
None
LhsNoEvidence
end
else
match result#singleton with
| Some ixty -> Some (bcd#get_typ ixty)
| Some ixty -> LhsResolved (bcd#get_typ ixty)
| _ ->
match result#toList with
| [ixty1; ixty2] ->
(match (bcd#get_typ ixty1), (bcd#get_typ ixty2) with
| TPtr (TVoid _, _), TPtr _ ->
Some (bcd#get_typ ixty2)
LhsResolved (bcd#get_typ ixty2)
| TPtr _, TPtr (TVoid _, _) ->
Some (bcd#get_typ ixty1)
LhsResolved (bcd#get_typ ixty1)
| TPtr (tty1, _), TPtr (tty2, _)
when is_struct_type tty1 && is_scalar tty2 ->
Some (bcd#get_typ ixty1)
LhsResolved (bcd#get_typ ixty1)
| TPtr (tty1, _), TPtr (tty2, _)
when is_struct_type tty2 && is_scalar tty1 ->
Some (bcd#get_typ ixty2)
| _ ->
LhsResolved (bcd#get_typ ixty2)
| (t1, t2) ->
begin
log_evaluation ();
log_diagnostics_result
Expand All @@ -855,7 +855,7 @@ object (self)
(List.map bcd#get_typ result#toList)
(fun ty -> STR (btype_to_string ty))
"[" "; " "]"))];
None
LhsConflict [t1; t2]
end)
| _ ->
begin
Expand All @@ -868,13 +868,13 @@ object (self)
(List.map bcd#get_typ result#toList)
(fun ty -> STR (btype_to_string ty))
"[" "; " "]"))];
None
LhsConflict (List.map bcd#get_typ result#toList)
end

end

method resolve_stack_lhs_type
(offset: int) (faddr: string): btype_t option =
(offset: int) (faddr: string): lhs_type_result_t =
let evaluation = self#evaluate_stack_lhs_type offset faddr in
let log_evaluation () =
log_diagnostics_result
Expand Down Expand Up @@ -992,10 +992,10 @@ object (self)
| _ -> ()) consts) vars) evaluation;
let btypes = result#fold (fun acc tix -> (bcd#get_typ tix) :: acc) [] in
match join_integer_btypes btypes with
| Some ty -> Some ty
| Some ty -> LhsResolved ty
| _ ->
match first_field_struct result with
| Some ty -> Some ty
| Some ty -> LhsResolved ty
| _ ->
begin
log_evaluation ();
Expand All @@ -1006,12 +1006,15 @@ object (self)
^ (p2s (pretty_print_list
(List.map bcd#get_typ result#toList)
(fun ty -> STR (btype_to_string ty)) "[" "; " "]"))];
None
(if result#isEmpty then
LhsNoEvidence
else
LhsConflict (List.map bcd#get_typ result#toList))
end
end

method resolve_reglhs_types (faddr: string):
(register_t * string * btype_t option) list =
(register_t * string * lhs_type_result_t) list =
let result = ref [] in
let _ =
if H.mem reglhss faddr then
Expand All @@ -1026,7 +1029,7 @@ object (self)
!result

method resolve_local_stack_lhs_types (faddr: string):
(int * btype_t option) list =
(int * lhs_type_result_t) list =
let result = ref [] in
let _ =
if H.mem stacklhss faddr then
Expand Down
1 change: 1 addition & 0 deletions CodeHawk/CHB/bchlib/bCHVersion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ object (self)
end



let version = new version_info_t
~version:"0.6.0_20261102"
~date:"2026-11-02"
Expand Down
59 changes: 48 additions & 11 deletions CodeHawk/CHB/bchlibarm32/bCHARMAnalysisResults.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ open CHXmlDocument

(* bchlib *)
open BCHBasicTypes
open BCHBCTypes
open BCHByteUtilities
open BCHFloc
open BCHFunctionInfo
Expand Down Expand Up @@ -248,40 +247,78 @@ object (self)

method write_xml_register_types
(node: xml_element_int)
(regtypes: (register_t * string * btype_t option) list) =
(regtypes: (register_t * string * lhs_type_result_t) list) =
let regnode = xmlElement "register-lhs-types" in
begin
List.iter (fun (reg, iaddr, optty) ->
List.iter (fun (reg, iaddr, lhs_type_result) ->
let inode = xmlElement "reglhs" in
begin
bd#write_xml_register inode reg;
inode#setAttribute "iaddr" iaddr;
(match optty with
| Some ty ->
(match lhs_type_result with
| LhsResolved ty ->
begin
log_result __FILE__ __LINE__
["reglhs: " ^ iaddr ^ ": " ^
(BCHBCTypePretty.btype_to_string ty)];
bcd#write_xml_typ inode ty
end
| _ -> ());
| LhsNoEvidence ->
begin
log_diagnostics_result __FILE__ __LINE__
["reglhs: " ^ iaddr ^ ": no evidence"];
inode#setAttribute "reason" "no-evidence"
end
| LhsConflict tylist ->
begin
log_diagnostics_result __FILE__ __LINE__
["reglhs: " ^ iaddr ^ ": type conflict: "
^ (String.concat
", " (List.map BCHBCTypePretty.btype_to_string tylist))];
inode#setAttribute "reason" "conflict";
List.iter (fun cty ->
let tynode = xmlElement "cty" in
begin
bcd#write_xml_typ tynode cty;
inode#appendChildren [tynode]
end) tylist
end);
regnode#appendChildren [inode]
end) regtypes;
node#appendChildren [regnode]
end

method write_xml_stack_types
(node: xml_element_int)
(stacktypes: (int * btype_t option) list) =
(stacktypes: (int * lhs_type_result_t) list) =
let stacknode = xmlElement "stack-variable-types" in
begin
List.iter (fun (offset, optty) ->
List.iter (fun (offset, lhs_type_result) ->
let inode = xmlElement "offset" in
begin
inode#setIntAttribute "off" offset;
(match optty with
| Some ty -> bcd#write_xml_typ inode ty
| _ -> ());
(match lhs_type_result with
| LhsResolved ty -> bcd#write_xml_typ inode ty
| LhsNoEvidence ->
begin
log_diagnostics_result __FILE__ __LINE__
["stacklhs: " ^ (string_of_int offset) ^ ": no evidence"];
inode#setAttribute "reason" "no-evidence"
end
| LhsConflict tylist ->
begin
log_diagnostics_result __FILE__ __LINE__
["stacklhs: " ^ (string_of_int offset) ^ ": conflict: "
^ (String.concat
", " (List.map BCHBCTypePretty.btype_to_string tylist))];
inode#setAttribute "reason" "conflict";
List.iter (fun cty ->
let tynode = xmlElement "cty" in
begin
bcd#write_xml_typ tynode cty;
inode#appendChildren [tynode]
end) tylist
end);
stacknode#appendChildren [inode]
end) stacktypes;
node#appendChildren [stacknode]
Expand Down
4 changes: 3 additions & 1 deletion CodeHawk/CHB/bchlibarm32/bCHARMDictionary.ml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ object (self)
| ARMRegBitSequence (r, lsb, widthm1) ->
(tags @ [ arm_reg_mfts#ts r ],[ lsb; widthm1 ])
| ARMAbsolute addr -> (tags, [bd#index_address addr])
| ARMLiteralAddress addr -> (tags, [bd#index_address addr])
| ARMLiteralAddress (addr, size) -> (tags, [bd#index_address addr; size])
| ARMMemMultiple (r, align, n, size) ->
(tags @ [ arm_reg_mfts#ts r ],[align; n; size])
| ARMOffsetAddress (r, align, offset, isadd, iswback, isindex, size) ->
Expand Down Expand Up @@ -493,6 +493,8 @@ object (self)
(ctags c, [di dt; oi dst; oi src; oi imm])
| VectorShiftRightNarrow (c, dt, dst, src, imm) ->
(ctags c, [di dt; oi dst; oi src; oi imm])
| VectorSquareRoot (c, dt, dst, src) ->
(ctags c, [di dt; oi dst; oi src])
| VStoreRegister (c, src, base, mem) ->
(ctags c, [oi src; oi base; oi mem])
| VectorStoreMultipleDecrementBefore (wb, c, rn, rl, mem) ->
Expand Down
Loading
Loading