diff --git a/CodeHawk/CHC/cchanalyze/cCHAssignmentTranslator.ml b/CodeHawk/CHC/cchanalyze/cCHAssignmentTranslator.ml index 1607ad76..a9e31431 100644 --- a/CodeHawk/CHC/cchanalyze/cCHAssignmentTranslator.ml +++ b/CodeHawk/CHC/cchanalyze/cCHAssignmentTranslator.ml @@ -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 _ = diff --git a/CodeHawk/CHC/cchanalyze/cCHFunctionTranslator.ml b/CodeHawk/CHC/cchanalyze/cCHFunctionTranslator.ml index d9c772c3..e0f9d1ab 100644 --- a/CodeHawk/CHC/cchanalyze/cCHFunctionTranslator.ml +++ b/CodeHawk/CHC/cchanalyze/cCHFunctionTranslator.ml @@ -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 = diff --git a/CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.ml b/CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.ml index 9fe147c4..2c9c6915 100644 --- a/CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.ml +++ b/CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.ml @@ -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 @@ -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 @@ -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, _, _) -> diff --git a/CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.mli b/CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.mli index 869e3c32..3d614b64 100644 --- a/CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.mli +++ b/CodeHawk/CHC/cchanalyze/cCHIntegerSemantics.mli @@ -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 diff --git a/CodeHawk/CHC/cchlib/cCHTypesUtil.ml b/CodeHawk/CHC/cchlib/cCHTypesUtil.ml index d42ecb42..eeb8b0f0 100644 --- a/CodeHawk/CHC/cchlib/cCHTypesUtil.ml +++ b/CodeHawk/CHC/cchlib/cCHTypesUtil.ml @@ -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 @@ -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 diff --git a/CodeHawk/CHC/cchlib/cCHTypesUtil.mli b/CodeHawk/CHC/cchlib/cCHTypesUtil.mli index c7140288..4c147574 100644 --- a/CodeHawk/CHC/cchlib/cCHTypesUtil.mli +++ b/CodeHawk/CHC/cchlib/cCHTypesUtil.mli @@ -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 diff --git a/CodeHawk/CHC/cchlib/cCHVersion.ml b/CodeHawk/CHC/cchlib/cCHVersion.ml index 744f9b99..aef1b4c9 100644 --- a/CodeHawk/CHC/cchlib/cCHVersion.ml +++ b/CodeHawk/CHC/cchlib/cCHVersion.ml @@ -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" diff --git a/CodeHawk/CHC/cchpre/cCHCheckValid.ml b/CodeHawk/CHC/cchpre/cCHCheckValid.ml index 575b1a01..cdc2fe52 100644 --- a/CodeHawk/CHC/cchpre/cCHCheckValid.ml +++ b/CodeHawk/CHC/cchpre/cCHCheckValid.ml @@ -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) diff --git a/CodeHawk/CHC/cchpre/cCHPrimaryProofObligations.ml b/CodeHawk/CHC/cchpre/cCHPrimaryProofObligations.ml index b22223e3..8a8741a5 100644 --- a/CodeHawk/CHC/cchpre/cCHPrimaryProofObligations.ml +++ b/CodeHawk/CHC/cchpre/cCHPrimaryProofObligations.ml @@ -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)) @@ -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