Skip to content

Commit c43d10f

Browse files
committed
Merge branch 'checks-constFold-overflow' into dash-dev
2 parents 92ce935 + da7ab8c commit c43d10f

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

goblint.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ dev-repo: "git+https://github.com/goblint/analyzer.git"
102102
x-maintenance-intent: ["(latest)" "(latest).(latest-1)"] # also keep previous minor version (with two releases per year, always keep a SV-COMP release)
103103
available: os-family != "bsd" & os-distribution != "alpine" & (arch != "arm64" | os = "macos")
104104
pin-depends: [
105-
[ "goblint-cil.2.0.9" "git+https://github.com/goblint/cil.git#e21285af8f4408ed5354b1674dab840c43e84712" ]
105+
[ "goblint-cil.2.0.9" "git+https://github.com/goblint/cil.git#2edc807cfc8ce80e74e932a8d84a809a44ec0c3d" ]
106106
# pinned for stability (https://github.com/goblint/analyzer/issues/1520), remove after new apron release
107107
[ "apron.v0.9.15" "git+https://github.com/antoinemine/apron.git#418a217c7a70dae3f422678f3aaba38ae374d91a" ]
108108
]

goblint.opam.locked

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ post-messages: [
148148
pin-depends: [
149149
[
150150
"goblint-cil.2.0.9"
151-
"git+https://github.com/goblint/cil.git#e21285af8f4408ed5354b1674dab840c43e84712"
151+
"git+https://github.com/goblint/cil.git#2edc807cfc8ce80e74e932a8d84a809a44ec0c3d"
152152
]
153153
[
154154
"apron.v0.9.15"

goblint.opam.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
x-maintenance-intent: ["(latest)" "(latest).(latest-1)"] # also keep previous minor version (with two releases per year, always keep a SV-COMP release)
44
available: os-family != "bsd" & os-distribution != "alpine" & (arch != "arm64" | os = "macos")
55
pin-depends: [
6-
[ "goblint-cil.2.0.9" "git+https://github.com/goblint/cil.git#e21285af8f4408ed5354b1674dab840c43e84712" ]
6+
[ "goblint-cil.2.0.9" "git+https://github.com/goblint/cil.git#2edc807cfc8ce80e74e932a8d84a809a44ec0c3d" ]
77
# pinned for stability (https://github.com/goblint/analyzer/issues/1520), remove after new apron release
88
[ "apron.v0.9.15" "git+https://github.com/antoinemine/apron.git#418a217c7a70dae3f422678f3aaba38ae374d91a" ]
99
]

src/analyses/base.ml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -821,21 +821,22 @@ struct
821821
in
822822
let r =
823823
(* query functions were no help ... now try with values*)
824-
match constFold true exp with
824+
match exp with (* TODO: support all cases from [constFold true] directly *)
825825
(* Integer literals *)
826826
(* seems like constFold already converts CChr to CInt *)
827827
| Const (CChr x) -> eval_rv ~man st (Const (charConstToInt x)) (* char becomes int, see Cil doc/ISO C 6.4.4.4.10 *)
828828
| Const (CInt (num,ikind,str)) -> Int (IntDomain.of_const (num,ikind,str)) (* no cast to ikind needed: CIL ensures that the literal fits ikind, either by silently truncating it (!) or having an explicit CastE around this *)
829+
| Const (CEnum (v, _, _)) -> eval_rv ~man st v (* based on [Cil.constFold true] *)
829830
| Const (CReal (_,fkind, Some str)) when not (Cilfacade.isComplexFKind fkind) -> Float (FD.of_string fkind str) (* prefer parsing from string due to higher precision *)
830831
| Const (CReal (num, fkind, None)) when not (Cilfacade.isComplexFKind fkind) && num = 0.0 -> Float (FD.of_const fkind num) (* constant 0 is ok, CIL creates these for zero-initializers; it is safe across float types *)
831832
| Const (CReal (_, fkind, None)) when not (Cilfacade.isComplexFKind fkind) -> assert false (* Cil does not create other CReal without string representation *)
832833
(* String literals *)
834+
| Const (CReal _) -> VD.top ()
833835
| Const (CStr (x,_)) -> Address (AD.of_string x) (* normal 8-bit strings, type: char* *)
834836
| Const (CWStr (xs,_) as c) -> (* wide character strings, type: wchar_t* *)
835837
let x = CilType.Constant.show c in (* escapes, see impl. of d_const in cil.ml *)
836838
let x = String.sub x 2 (String.length x - 3) in (* remove surrounding quotes: L"foo" -> foo *)
837839
Address (AD.of_string x) (* Address (AD.str_ptr ()) *)
838-
| Const _ -> VD.top ()
839840
(* Variables and address expressions *)
840841
| Lval lv ->
841842
eval_rv_base_lval ~eval_lv ~man st exp lv
@@ -945,12 +946,27 @@ struct
945946
Address (AD.map array_start (eval_lv ~man st lval))
946947
| CastE (_, t, Const (CStr (x,e))) -> (* VD.top () *) eval_rv ~man st (Const (CStr (x,e))) (* TODO safe? *)
947948
| CastE (kind, t, exp) ->
948-
let v = eval_rv ~man st exp in
949+
let v: VD.t = match exp with
950+
| AddrOf (Mem (CastE (_, TPtr (bt, _), z)), off) when isZero z -> (* offsetof hack, based on [Cil.constFold true] *)
951+
begin match Cilfacade.bytesOffsetOnly bt off with
952+
| bytes_offset -> Int (ID.of_int !kindOfSizeOf (Z.of_int bytes_offset))
953+
| exception SizeOfError _ -> eval_rv ~man st exp (* TODO: does this ever happen? *)
954+
end
955+
| _ -> eval_rv ~man st exp
956+
in
949957
VD.cast ~kind t v
950-
| SizeOf _
958+
| SizeOf t -> (* based on [Cil.constFold true] *)
959+
begin match Cilfacade.bytesSizeOf t with
960+
| bytes -> Int (ID.of_int !Cil.kindOfSizeOf (Z.of_int bytes))
961+
| exception SizeOfError _ -> Int (ID.top_of !Cil.kindOfSizeOf) (* TODO: does this ever happen? *)
962+
end
963+
| SizeOfE e -> (* based on [Cil.constFold true] and inlined SizeOf *)
964+
begin match Cilfacade.bytesSizeOf (Cilfacade.typeOf e) with
965+
| bytes -> Int (ID.of_int !Cil.kindOfSizeOf (Z.of_int bytes))
966+
| exception SizeOfError _ -> Int (ID.top_of !Cil.kindOfSizeOf) (* TODO: does this ever happen? *)
967+
end
951968
| Real _
952969
| Imag _
953-
| SizeOfE _
954970
| SizeOfStr _
955971
| AlignOf _
956972
| AlignOfE _

src/common/util/cilfacade.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ let init () =
8585
initCIL ();
8686
removeBranchingOnConstants := false;
8787
addReturnOnNoreturnFallthrough := true;
88-
lowerConstants := true;
88+
lowerConstants := false; (* disabled to prevent CIL from constFold-ing constant expressions which still need to be checked (e.g. for overflows) *) (* TODO: lowering enum constants, etc should be separate from general constFold in CIL *)
8989
Mergecil.ignore_merge_conflicts := true;
9090
(* lineDirectiveStyle := None; *)
9191
RmUnused.keepUnused := true;
@@ -405,11 +405,13 @@ let typeSigBlendAttributes baseAttrs =
405405
typeSigAddAttrs contageous
406406

407407

408+
(** @raise SizeOfError *)
408409
let bytesSizeOf t =
409410
let bits = bitsSizeOf t in
410411
assert (bits mod 8 = 0);
411412
bits / 8
412413

414+
(** @raise SizeOfError *)
413415
let bytesOffsetOnly t o =
414416
let bits_offset, _ = bitsOffset t o in
415417
assert (bits_offset mod 8 = 0);

tests/regression/70-transform/03-deadcode-globals.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121

2222
{
23-
return ((int )(x->field1 + 1));
23+
return ((int )(x->field1 + (int const )1));
2424
}
2525
}
2626
int const global1 = (int const )1;
27-
int const global2 = global1 + 7;
27+
int const global2 = global1 + (int const )7;
2828
int main(void)
2929
{
3030
struct1_named1 x ;

0 commit comments

Comments
 (0)