Skip to content

Commit f7d0489

Browse files
committed
CHB: add frozen variable to auxiliary variable
1 parent b61ab57 commit f7d0489

9 files changed

Lines changed: 148 additions & 9 deletions

‎CodeHawk/CHB/bchlib/bCHARMFunctionInterface.ml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
(* chutil *)
2929
open CHFormatStringParser
30+
open CHLogger
3031
open CHTraceResult
3132
open CHUtil
3233

@@ -584,7 +585,15 @@ let arm_vfp_params
584585
(funargs: bfunarg_t list): fts_parameter_t list =
585586
let fmt (index: int) =
586587
match BCHBCAttributesUtil.get_format_archetype attrs index with
587-
| Some fmtstringtype -> fmtstringtype
588+
| Some fmtstringtype ->
589+
let _ =
590+
log_diagnostics_result
591+
~tag:"arm_vfp_parameters"
592+
__FILE__ __LINE__
593+
["fmtstringtype: "
594+
^ (BCHFtsParameter.formatstring_type_to_string fmtstringtype);
595+
"attr count: " ^ (string_of_int (List.length attrs))] in
596+
fmtstringtype
588597
| _ -> NoFormat in
589598
let (_, _, params) =
590599
List.fold_left

‎CodeHawk/CHB/bchlib/bCHFloc.ml‎

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ object (self)
378378
| Some xpr ->
379379
match xpr with
380380
| XConst (IntConst n) ->
381+
(* The callee is a variadic function. Retrieve the actual format
382+
string and parse it to determine the number of variadic arguments
383+
in the call.*)
381384
let dw = BCHDoubleword.numerical_mod_to_doubleword n in
382385
if BCHStrings.string_table#has_string dw then
383386
let fmtstring = BCHStrings.string_table#get_string dw in
@@ -417,6 +420,82 @@ object (self)
417420
["No constant format string found in string table"];
418421
None
419422
end
423+
| XVar v when self#finfo#env#is_initial_register_value v ->
424+
(* There is no constant format string. Instead the format string
425+
argument is received from the caller's parameter. Retrieve the
426+
the specification of the parameter by externalizing the expression
427+
to a parameter. If the parameter spec (as obtained from a
428+
header file) is a retricted format string, reconstruct a minimal
429+
format string from its argument spec list, and parse in the
430+
same way as before.*)
431+
let bterm_o =
432+
(new arm_expression_externalizer_t finfo)#xpr_to_bterm
433+
t_charptr (XVar v) in
434+
(match bterm_o with
435+
| Some (ArgValue ftspar) ->
436+
(match ftspar.apar_fmt with
437+
| RestrictedPrintFormat sl when sl <> [] ->
438+
let fmtstring =
439+
String.concat " " (List.map (fun s -> "%" ^ s) sl) in
440+
let _ =
441+
log_diagnostics_result
442+
~tag:"get_annotated_format_arguments:restricted_format"
443+
~msg:finfo#get_name
444+
__FILE__ __LINE__
445+
["restricted fmtstring: " ^ fmtstring] in
446+
let fmtspec = parse_formatstring fmtstring false in
447+
if fmtspec#has_arguments then
448+
let argspecs = fmtspec#get_arguments in
449+
let argxprs = List.map (fun (_, x) -> x) callargs in
450+
(match CHUtil.list_slice argxprs
451+
index (List.length argspecs) with
452+
| Some fmtargs ->
453+
Some
454+
(List.map2
455+
(fun argspec argxpr ->
456+
let fw =
457+
if argspec#has_fieldwidth then
458+
argspec#get_fieldwidth
459+
else
460+
CHFormatStringParser.NoFieldwidth in
461+
(argspec#get_conversion, fw, argxpr))
462+
argspecs fmtargs)
463+
| _ ->
464+
begin
465+
log_diagnostics_result
466+
~tag:"get_annotated_format_arguments:no formatargs"
467+
~msg:finfo#get_name
468+
__FILE__ __LINE__
469+
["restricted fmtstring: no arguments: " ^ fmtstring];
470+
None
471+
end)
472+
else
473+
begin
474+
log_diagnostics_result
475+
~tag:"get_annotated_format_arguments:restricted_format"
476+
~msg:finfo#get_name
477+
__FILE__ __LINE__
478+
["restricted fmtstring: no arguments: " ^ fmtstring];
479+
None
480+
end
481+
| _ ->
482+
begin
483+
log_diagnostics_result
484+
~tag:"get_annotated_format_arguments: ftspar: no format"
485+
~msg:finfo#get_name
486+
__FILE__ __LINE__
487+
["param: " ^ (fts_parameter_to_string ftspar)];
488+
None
489+
end)
490+
| _ ->
491+
begin
492+
log_diagnostics_result
493+
~tag:"get_annotated_format_arguments:initial_register_value"
494+
~msg:finfo#get_name
495+
__FILE__ __LINE__
496+
["format string: " ^ (p2s v#toPretty)];
497+
None
498+
end)
420499
| _ ->
421500
begin
422501
log_diagnostics_result
@@ -3360,10 +3439,19 @@ object (self)
33603439
[ABSTRACT_VARS [lhs]]
33613440

33623441
else
3442+
let fndata = self#f#get_function_data in
33633443
let rhs =
3444+
if fndata#has_regvar_freeze self#ia then
3445+
let _ =
3446+
log_diagnostics_result
3447+
~tag:"get_assign_commands_r:freeze"
3448+
~msg:self#cia
3449+
__FILE__ __LINE__
3450+
["lhs: " ^ (p2s lhs#toPretty)] in
3451+
XVar (self#f#env#mk_frozen_value lhs self#cia)
33643452
(* if rhs is a composite symbolic expression, create a new variable
33653453
for it *)
3366-
if self#is_composite_symbolic_value rhs then
3454+
else if self#is_composite_symbolic_value rhs then
33673455
XVar (self#env#mk_symbolic_value rhs)
33683456
else
33693457
rhs in
@@ -3372,7 +3460,7 @@ object (self)
33723460
let (rhscmds, rhs_c) = xpr_to_numexpr reqN reqC rhs in
33733461
let cmds = rhscmds @ [ASSIGN_NUM (lhs, rhs_c)] in
33743462

3375-
let fndata = self#f#get_function_data in
3463+
33763464
match fndata#get_regvar_intro self#ia with
33773465
| Some rvi when rvi.rvi_cast && Option.is_some rvi.rvi_vartype ->
33783466
TR.tfold

‎CodeHawk/CHB/bchlib/bCHFunctionData.ml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ object (self)
293293
(fun rvi -> rvi.rvi_iaddr#equal iaddr && rvi.rvi_cast) a.regvarintros
294294
| _ -> false
295295

296+
method has_regvar_freeze (iaddr: doubleword_int): bool =
297+
match self#get_function_annotation with
298+
| Some a ->
299+
List.exists
300+
(fun rvi -> rvi.rvi_iaddr#equal iaddr && rvi.rvi_freeze) a.regvarintros
301+
| _ -> false
302+
296303
method has_stackvar_type_cast (offset: int): bool =
297304
match self#get_function_annotation with
298305
| Some a ->
@@ -683,6 +690,7 @@ let read_xml_regvar_intro (node: xml_element_int): regvar_intro_t traceresult =
683690
(fun dw ->
684691
let rvi_iaddr = dw in
685692
let rvi_name = get "name" in
693+
let rvi_freeze = (has "freeze") && ((get "freeze") = "yes") in
686694
let (rvi_vartype, rvi_cast) =
687695
if has "typename" then
688696
let iscast = (has "cast") && ((get "cast") = "yes") in
@@ -704,6 +712,7 @@ let read_xml_regvar_intro (node: xml_element_int): regvar_intro_t traceresult =
704712
Ok {rvi_iaddr = rvi_iaddr;
705713
rvi_name = rvi_name;
706714
rvi_cast = rvi_cast;
715+
rvi_freeze = rvi_freeze;
707716
rvi_vartype = rvi_vartype})
708717
(string_to_doubleword (get "iaddr"))
709718

‎CodeHawk/CHB/bchlib/bCHFunctionInfo.ml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,10 @@ object (self)
996996
(var:variable_t) (taddr:ctxt_iaddress_t) (jaddr:ctxt_iaddress_t) =
997997
self#mk_variable (varmgr#make_frozen_test_value var taddr jaddr)
998998

999+
method mk_frozen_value
1000+
(var: variable_t) (iaddr: ctxt_iaddress_t): variable_t =
1001+
self#mk_variable (varmgr#make_frozen_value var iaddr)
1002+
9991003
method mk_special_variable (name:string) =
10001004
self#mk_variable (varmgr#make_special_variable name)
10011005

‎CodeHawk/CHB/bchlib/bCHLibTypes.mli‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,8 @@ type regvar_intro_t = {
15071507
rvi_iaddr: doubleword_int;
15081508
rvi_name: string;
15091509
rvi_vartype: btype_t option;
1510-
rvi_cast: bool
1510+
rvi_cast: bool;
1511+
rvi_freeze: bool
15111512
}
15121513

15131514
(** Local stack variable introduced via userdata.
@@ -1619,6 +1620,7 @@ class type function_data_int =
16191620
method has_regvar_type_annotation: doubleword_int -> bool
16201621
method get_regvar_type_annotation: doubleword_int -> btype_t traceresult
16211622
method has_regvar_type_cast: doubleword_int -> bool
1623+
method has_regvar_freeze: doubleword_int -> bool
16221624
method get_regvar_intro: doubleword_int -> regvar_intro_t option
16231625

16241626
(** {2 Stack-variable introductions} *)
@@ -3756,6 +3758,13 @@ and constant_value_variable_t =
37563758
that is used as a predicate for a conditional jump instruction (or other
37573759
condition instruction) at address [j_iaddr]*)
37583760

3761+
| FrozenValue of variable_t * ctxt_iaddress_t
3762+
(** [FrozenValue (var, iaddr): frozen value of variable var at address iaddr.
3763+
The difference with a frozen test value is that this value is created
3764+
on demand to preserve a value to be used in invariants; the corresponding
3765+
variable does not get abstracted, as is the case with frozen test
3766+
variables.*)
3767+
37593768
| FunctionReturnValue of ctxt_iaddress_t
37603769
(** [FunctionReturnValue iaddr]: return value from call at instruction
37613770
address [iaddr]*)
@@ -4108,6 +4117,9 @@ object
41084117
-> ctxt_iaddress_t
41094118
-> assembly_variable_int
41104119

4120+
method make_frozen_value:
4121+
variable_t -> ctxt_iaddress_t -> assembly_variable_int
4122+
41114123
(** [make_bridge_value addr argix] returns a bridge value for the
41124124
variable [var] that is the stack argument with index [argix] (1-based)
41134125
for a call (x86 only).*)
@@ -5205,6 +5217,7 @@ class type function_environment_int =
52055217

52065218
method mk_frozen_test_value:
52075219
variable_t -> ctxt_iaddress_t -> ctxt_iaddress_t -> variable_t
5220+
method mk_frozen_value: variable_t -> ctxt_iaddress_t -> variable_t
52085221
method mk_special_variable: string -> variable_t
52095222
method mk_runtime_constant: string -> variable_t
52105223
method mk_return_value: ctxt_iaddress_t -> variable_t

‎CodeHawk/CHB/bchlib/bCHSumTypeSerializer.ml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ object
799799
| InitialRegisterValue _ -> "ir"
800800
| InitialMemoryValue _ -> "iv"
801801
| FrozenTestValue _ -> "ft"
802+
| FrozenValue _ -> "frv"
802803
| FunctionReturnValue _ -> "fr"
803804
| TypeCastValue _ -> "tc"
804805
| SyscallErrorReturnValue _ -> "ev"
@@ -814,7 +815,7 @@ object
814815
| ChifTemp -> "chiftemp"
815816

816817
method !tags = [
817-
"bv"; "chiftemp"; "ct"; "ev"; "fr"; "fp"; "ft"; "fv"; "ir";
818+
"bv"; "chiftemp"; "ct"; "ev"; "fp"; "fr"; "frv"; "ft"; "fv"; "ir";
818819
"iv"; "rt"; "se" ; "sp"; "sv"; "ssv"; "tc"]
819820

820821
end

‎CodeHawk/CHB/bchlib/bCHVarDictionary.ml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
Copyright (c) 2005-2019 Kestrel Technology LLC
88
Copyright (c) 2020 Henny Sipma
9-
Copyright (c) 2021-2025 Aarno Labs LLC
9+
Copyright (c) 2021-2026 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
@@ -253,6 +253,7 @@ object (self)
253253
| InitialMemoryValue v -> (tags, [xd#index_variable v])
254254
| FrozenTestValue (v, a1, a2) ->
255255
(tags @ [a1; a2], [xd#index_variable v])
256+
| FrozenValue (v, a) -> (tags @ [a], [xd#index_variable v])
256257
| FunctionReturnValue a -> (tags @ [a], [])
257258
| TypeCastValue (iaddr, name, ty, reg) ->
258259
(tags @ [iaddr; name], [bcd#index_typ ty; bd#index_register reg])
@@ -284,6 +285,7 @@ object (self)
284285
| "ir" -> InitialRegisterValue (bd#get_register (a 0), a 1)
285286
| "iv" -> InitialMemoryValue (xd#get_variable (a 0))
286287
| "ft" -> FrozenTestValue (xd#get_variable (a 0), t 1, t 2)
288+
| "frv" -> FrozenValue (xd#get_variable (a 0), t 1)
287289
| "fr" -> FunctionReturnValue (t 1)
288290
| "tc" -> TypeCastValue (t 1, t 2, bcd#get_typ (a 0), bd#get_register (a 1))
289291
| "ev" -> SyscallErrorReturnValue (t 1)

‎CodeHawk/CHB/bchlib/bCHVariable.ml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ object (self:'a)
107107
(if level = 0 then "" else "_" ^ (string_of_int level))
108108
| InitialMemoryValue v -> v#getName#getBaseName ^ "_in"
109109
| FrozenTestValue (fv,taddr,jaddr) ->
110-
fv#getName#getBaseName ^ "_val_" ^ taddr ^ "_amp_" ^ jaddr
110+
fv#getName#getBaseName ^ "_val_" ^ taddr ^ "_amp_" ^ jaddr
111+
| FrozenValue (fv, iaddr) ->
112+
fv#getName#getBaseName ^ "_val_" ^ iaddr
111113
| FunctionPointer (fname,cname,address) ->
112114
"fp_" ^ fname ^ "_" ^ cname ^ "_" ^ address
113115
| FunctionReturnValue address -> "rtn_" ^ address
@@ -196,6 +198,7 @@ object (self:'a)
196198
| InitialRegisterValue _ -> None
197199
| InitialMemoryValue _ -> None
198200
| FrozenTestValue _ -> None
201+
| FrozenValue _ -> None
199202
| FunctionPointer _ -> None
200203
| FunctionReturnValue _ -> None
201204
| TypeCastValue (_, _, ty, _) -> Some ty
@@ -327,6 +330,7 @@ object (self:'a)
327330
| InitialMemoryValue _
328331
| FunctionReturnValue _
329332
| TypeCastValue _
333+
| FrozenValue _
330334
| CallTargetValue _
331335
| SideEffectValue _
332336
| FieldValue _
@@ -697,6 +701,10 @@ object (self)
697701
(var:variable_t) (taddr:ctxt_iaddress_t) (jaddr:ctxt_iaddress_t) =
698702
self#mk_variable (AuxiliaryVariable (FrozenTestValue (var, taddr, jaddr)))
699703

704+
method make_frozen_value
705+
(var: variable_t) (iaddr: ctxt_iaddress_t): assembly_variable_int =
706+
self#mk_variable (AuxiliaryVariable (FrozenValue (var, iaddr)))
707+
700708
method make_bridge_value (address:ctxt_iaddress_t) (argnr:int) =
701709
self#mk_variable (AuxiliaryVariable (BridgeVariable (address, argnr)))
702710

@@ -883,6 +891,11 @@ object (self)
883891
| (TypeCastValue _, _) -> -1
884892
| (_, TypeCastValue _) -> 1
885893

894+
| (FrozenValue (_, iaddr1), FrozenValue (_, iaddr2)) ->
895+
Stdlib.compare iaddr1 iaddr2
896+
| (FrozenValue _, _) -> -1
897+
| (_, FrozenValue _) -> 1
898+
886899
| (FunctionReturnValue _, FunctionReturnValue _) -> Stdlib.compare ix1 ix2
887900
| (FunctionReturnValue _, _) -> -1
888901
| (_, FunctionReturnValue _) -> 1

‎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_20260619"
99-
~date:"2026-06-19"
98+
~version:"0.6.0_20260622"
99+
~date:"2026-06-22"
100100
~licensee: None
101101
~maxfilesize: None
102102
()

0 commit comments

Comments
 (0)