Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CodeHawk/CHC/cchanalyze/cCHAssignmentTranslator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ object (self)
(List.map (fun v -> (p2s v#toPretty)) memoryvars) in
CCMD (ABSTRACT_VARS memoryvars)
else if env#has_constant_offset chifVar then
make_c_cmd (ASSIGN_NUM (chifVar, numExp))
(* a store into a bit-field keeps the low bits *)
(match CCHIntegerSemantics.bitfield_store_correction lhs with
| Some c ->
make_c_cmd_block
((make_c_cmd (ASSIGN_NUM (chifVar, numExp)))
:: (CCHIntegerSemantics.correction_code env chifVar c))
| None -> make_c_cmd (ASSIGN_NUM (chifVar, numExp)))
else
let memoryvars = env#get_memory_variables_with_base chifVar in
let _ =
Expand Down
6 changes: 3 additions & 3 deletions CodeHawk/CHC/cchanalyze/cCHFunctionTranslator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ object
refute the unsigned wrap-around correction branches. *)
let rangeconstraints =
List.fold_left (fun acc (_vname, vtype, _, vInit) ->
match fenv#get_type_unrolled vtype with
| TInt (ik, _) ->
match integer_kind_of_type vtype with
| Some ik ->
(make_range_assert
env vInit (get_safe_lowerbound ik) (get_safe_upperbound ik))
:: acc
| _ -> acc) [] (formals @ globals) in
| None -> acc) [] (formals @ globals) in
let (tmps,rangecode) = env#end_transaction in
let rangecode = List.map make_c_cmd rangecode in
let rangeconstraints =
Expand Down
48 changes: 39 additions & 9 deletions CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,17 @@ let is_proven_safe_in_context
| pos -> List.for_all (fun po -> po#is_safe) pos


(* Integer kind of the type of [e]; for an enumerated type its compatible
integer type. *)
let integer_kind_of_exp (env: c_environment_int) (e: exp): ikind option =
match type_of_exp env#get_fdecls e with
| Ok t ->
(match CCHFileEnvironment.file_environment#get_type_unrolled t with
| TInt (ik, _) -> Some ik
| _ -> None)
| Ok t -> integer_kind_of_type t
| Error _ -> None


let unsigned_ikind (ty: typ): ikind option =
match CCHFileEnvironment.file_environment#get_type_unrolled ty with
| TInt (ik, _) when (not (is_signed_type ik)) && ik <> IBool -> Some ik
match integer_kind_of_type ty with
| Some ik when (not (is_signed_type ik)) && ik <> IBool -> Some ik
| _ -> None


Expand Down Expand Up @@ -271,9 +270,8 @@ let get_correction
mk_range_correction numerical_zero max max#neg numerical_zero false false
| None -> None)
| CastE (ty, e1) ->
(match (CCHFileEnvironment.file_environment#get_type_unrolled ty,
integer_kind_of_exp env e1) with
| (TInt (ikto, _), Some ikfrom) ->
(match (integer_kind_of_type ty, integer_kind_of_exp env e1) with
| (Some ikto, Some ikfrom) ->
let slo = get_safe_lowerbound ikfrom in
let shi = get_safe_upperbound ikfrom in
if ikto = IBool then
Expand Down Expand Up @@ -302,6 +300,38 @@ let get_correction
| _ -> None


(* Store into a bit-field of width w < N. The value stored is the
rhs converted to the declared type of the field (CIL inserts that cast),
so it lies in the range of that type; the bit-field keeps its low w bits:
reduction modulo 2^w into [0, 2^w - 1] for an unsigned field (C11
6.3.1.3p2), and into [-2^(w-1), 2^(w-1) - 1] for a signed field
(implementation-defined, 6.3.1.3p3; gcc: modulo 2^w, as for the other
conversions to signed types). There is no proof obligation for this
value change, so the correction is never dropped. *)
let bitfield_store_correction (lhs: lval): correction_t option =
let rec last_field (off: offset) =
match off with
| NoOffset -> None
| Field ((fname, ckey), NoOffset) -> Some (fname, ckey)
| Field (_, o) | Index (_, o) -> last_field o in
match last_field (snd lhs) with
| None -> None
| Some (fname, ckey) ->
let finfo =
CCHFileEnvironment.file_environment#get_field_info ckey fname in
(match (finfo.fbitfield, integer_kind_of_type finfo.ftype) with
| (Some w, Some ik) when ik <> IBool && w > 0 && w < get_safe_bit_width ik ->
let (lo, hi) =
if is_signed_type ik then
let p = power_of_two (w - 1) in
(p#neg, p#sub numerical_one)
else
(numerical_zero, (power_of_two w)#sub numerical_one) in
mk_range_correction
lo hi (get_safe_lowerbound ik) (get_safe_upperbound ik) false false
| _ -> None)


let fold_constant (c: correction_t) (n: numerical_t): numerical_t =
match c with
| RangeCorrection (lo, hi, _, _) ->
Expand Down
8 changes: 8 additions & 0 deletions CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.mli
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ val get_correction:
c_environment_int -> program_context_int -> exp -> correction_t option


(** [bitfield_store_correction lhs] returns the correction for a store into
[lhs] if [lhs] designates a bit-field of width [w] smaller than the width
of its declared integer type: the stored value is reduced modulo [2^w]
into the range of the bit-field (C11 6.3.1.3p2; for a signed bit-field
implementation-defined, 6.3.1.3p3, gcc semantics). [None] otherwise. *)
val bitfield_store_correction: lval -> correction_t option


(** [unsigned_ikind ty] returns the integer kind of [ty] if it is an
unsigned integer type other than [_Bool]; otherwise [None]. *)
val unsigned_ikind: typ -> ikind option
Expand Down
15 changes: 14 additions & 1 deletion CodeHawk/CHC/cchlib/cCHTypesUtil.ml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ let get_typ_attributes (t:typ) =
| TBuiltin_va_list a -> a


(* The integer kind of an integer or enumerated type. An object of enumerated
type has the representation and range of its compatible integer type
(C11 6.7.2.2p4) and may hold any value of that type, not only the
enumeration constants; CIL's ekind is gcc's choice (unsigned int if no
enumeration constant is negative, a narrower type for packed enums). *)
let integer_kind_of_type (t: typ): ikind option =
match fenv#get_type_unrolled t with
| TInt (ik, _) -> Some ik
| TEnum (ename, _) -> Some (fenv#get_enum ename).ekind
| _ -> None


let is_enum_constant (ename:string) (exp:exp) =
let einfo = fenv#get_enum ename in
let eexps = List.map (fun (_, _, e, _) -> e) einfo.eitems in
Expand Down Expand Up @@ -1061,7 +1073,8 @@ and range_of_type (fdecls:cfundeclarations_int) (t:typ) =
| XConst (IntConst n) -> get_type_range (8 * n#toInt) signed
| _ -> get_type_range max_bits signed
end
| TEnum _ -> range_of_type fdecls (TInt (IInt,[]))
| TEnum (ename, _) ->
range_of_type fdecls (TInt ((fenv#get_enum ename).ekind, []))
| TPtr _
| TFun _
| TArray _ -> get_type_range system_settings#get_wordsize false
Expand Down
5 changes: 5 additions & 0 deletions CodeHawk/CHC/cchlib/cCHTypesUtil.mli
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ val max_size_of_exp_type: cfundeclarations_int -> exp -> xpr_t option

val range_of_type: cfundeclarations_int -> typ -> type_range_int

(** [integer_kind_of_type t] is [Some ik] for an integer type [TInt ik] and
for an enumerated type whose compatible integer type (CIL [ekind], gcc's
choice) is [ik] (C11 6.7.2.2p4); [None] for other types. *)
val integer_kind_of_type: typ -> ikind option

val byte_offset_of_field: cfundeclarations_int -> fieldinfo -> xpr_t

val get_pointer_expr_target_type: cfundeclarations_int -> exp -> typ traceresult
4 changes: 2 additions & 2 deletions CodeHawk/CHC/cchlib/cCHVersion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ object (self)
end

let version = new version_info_t
~version:"0.3.0_20261113"
~date:"2026-11-13"
~version:"0.3.0_20261114"
~date:"2026-11-14"
4 changes: 0 additions & 4 deletions CodeHawk/CHC/cchpre/cCHCheckValid.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1040,10 +1040,6 @@ let check_ppo_validity
^ " belongs to enumeration "
^ ename)

| PCast (TEnum (ename,_), TInt (ik, _), _) when enum_fits_kind ename ik ->
let its = int_type_to_string in
make ("all enum values in " ^ ename ^ " fit into " ^ (its ik))

| PCast (_, TEnum (ename,_), (UnOp (LNot, _, _)))
when (is_enum_constant ename zero) && (is_enum_constant ename one) ->
make ("zero and one belong to enumeration " ^ ename)
Expand Down
17 changes: 17 additions & 0 deletions CodeHawk/CHC/cchpre/cCHPrimaryProofObligations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,19 @@ and create_po_exp
let _ =
match (exptyp_r, tgttyp) with
| (Ok (TInt (ik1, _)), TInt (ik2,_ )) -> add_int_cast ik1 ik2 e
(* conversions from or to an enumerated type are conversions from or to
its compatible integer type (C11 6.7.2.2p4; a conversion to an
enumerated type also gets the enumeration-membership check *)
| (Ok (TEnum _ as t1), (TInt _ | TEnum _))
| (Ok (TInt _ as t1), TEnum _) ->
begin
(match (integer_kind_of_type t1, integer_kind_of_type tgttyp) with
| (Some ik1, Some ik2) -> add_int_cast ik1 ik2 e
| _ -> ());
(match tgttyp with
| TEnum _ -> add (PCast (t1, tgttyp, e))
| _ -> ())
end
| (Ok (TPtr (tt1, _)), TPtr (tt2, _)) ->
add (PPointerCast (tt1,tt2,e)) (* may need additional int cast *)
| (Ok exptyp, _) -> add (PCast (exptyp, tgttyp, e))
Expand Down Expand Up @@ -994,6 +1007,10 @@ and get_output_format_proofobligations env argspec arg =
let get_po ty2 =
match (argtype, ty2) with
| (Ok (TInt (ik1, _)), TInt (ik2, _)) -> get_int_cast ik1 ik2 arg
| (Ok (TEnum _ as t1), TInt (ik2, _)) ->
(match integer_kind_of_type t1 with
| Some ik1 -> get_int_cast ik1 ik2 arg
| None -> [PFormatCast (t1, ty2, arg)])
| (Ok ty, _) -> [PFormatCast (ty, ty2, arg)]
| (Error err, _) ->
begin
Expand Down
Loading