Skip to content

Commit 63e20a1

Browse files
authored
Merge pull request #1904 from goblint/apron-unassume-extra-trivial
Fix `SharedFunctions.AssertionModule.exp_is_constraint`
2 parents 9724fee + 54c2bb9 commit 63e20a1

File tree

6 files changed

+72
-19
lines changed

6 files changed

+72
-19
lines changed

src/analyses/baseInvariant.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -870,15 +870,9 @@ struct
870870
in
871871
if eval_bool exp st = Some (not tv) then contra st (* we already know that the branch is dead *)
872872
else
873-
(* C11 6.5.13, 6.5.14, 6.5.3.3: LAnd, LOr and LNot also return 0 or 1 *)
874-
let is_cmp = function
875-
| UnOp (LNot, _, _)
876-
| BinOp ((Lt | Gt | Le | Ge | Eq | Ne | LAnd | LOr), _, _, _) -> true
877-
| _ -> false
878-
in
879873
match Cilfacade.get_ikind_exp exp with
880874
| ik ->
881-
let itv = if not tv || is_cmp exp then (* false is 0, but true can be anything that is not 0, except for comparisons which yield 1 *)
875+
let itv = if not tv || Cilfacade.exp_is_boolean exp then (* false is 0, but true can be anything that is not 0, except for comparisons which yield 1 *)
882876
ID.of_bool ik tv (* this will give 1 for true which is only ok for comparisons *)
883877
else
884878
ID.of_excl_list ik [Z.zero] (* Lvals, Casts, arithmetic operations etc. should work with true = non_zero *)

src/cdomains/apron/sharedFunctions.apron.ml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -544,24 +544,17 @@ struct
544544
module Tracked = Tracked
545545
module Convert = Convert (V) (Bounds) (Arg) (Tracked)
546546

547-
let rec exp_is_constraint = function
548-
(* constraint *)
549-
| BinOp ((Lt | Gt | Le | Ge | Eq | Ne), _, _, _) -> true
550-
| BinOp ((LAnd | LOr), e1, e2, _) -> exp_is_constraint e1 && exp_is_constraint e2
551-
| UnOp (LNot,e,_) -> exp_is_constraint e
552-
(* expression *)
553-
| _ -> false
554-
555547
(* TODO: move logic-handling assert_constraint from Apron back to here, after fixing affeq bot-bot join *)
556548

557549
(** Assert any expression. *)
558550
let assert_inv ask d e negate no_ov =
559551
let e' =
560-
if exp_is_constraint e then
552+
if Cilfacade.exp_is_boolean e then
561553
e
562554
else
563555
(* convert non-constraint expression, such that we assert(e != 0) *)
564556
BinOp (Ne, e, zero, intType)
557+
(* TODO: do this per non-constraint subexpression *)
565558
in
566559
assert_constraint ask d e' negate no_ov
567560

@@ -589,8 +582,8 @@ struct
589582
| exception Invalid_argument _ ->
590583
ID.top () (* real top, not a top of any ikind because we don't even know the ikind *)
591584
| ik ->
592-
if M.tracing then M.trace "relation" "eval_int: exp_is_constraint %a = %B" d_plainexp e (exp_is_constraint e);
593-
if exp_is_constraint e then
585+
if M.tracing then M.trace "relation" "eval_int: exp_is_constraint %a = %B" d_plainexp e (Cilfacade.exp_is_boolean e);
586+
if Cilfacade.exp_is_boolean e then
594587
match check_assert ask d e no_ov with
595588
| `True -> ID.of_bool ik true
596589
| `False -> ID.of_bool ik false

src/common/util/cilfacade.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ let makeBinOp binop e1 e2 =
441441
let (_, e) = Cabs2cil.doBinOp binop e1 t1 e2 t2 in
442442
e
443443

444+
(** Is effectively boolean-returning operation?
445+
These operations have type [int], but only have values 0 or 1. *)
446+
let exp_is_boolean = function
447+
(* C11 6.5.13, 6.5.14, 6.5.3.3: LAnd, LOr and LNot also return 0 or 1 *)
448+
| UnOp (LNot, _, _)
449+
| BinOp ((Lt | Gt | Le | Ge | Eq | Ne | LAnd | LOr), _, _, _) -> true
450+
| _ -> false
451+
444452
let anoncomp_name_regexp = Str.regexp {|^__anon\(struct\|union\)\(_\(.+\)\)?_\([0-9]+\)$|}
445453

446454
let split_anoncomp_name name =
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SKIP PARAM: --set ana.activated[+] apron --set ana.apron.domain polyhedra --set ana.activated[+] unassume --set witness.yaml.unassume 76-apron-unassume-extra-trivial.yml
2+
#include <assert.h>
3+
// Using polyhedra instead of octagon, because the former has no narrowing and really needs the witness.
4+
int main() {
5+
int i = 0;
6+
while (i < 100) { // TODO: location invariant before loop doesn't work anymore
7+
i++;
8+
}
9+
assert(i == 100);
10+
return 0;
11+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- entry_type: invariant_set
2+
metadata:
3+
format_version: "2.0"
4+
uuid: 0a72f7b3-7826-4f68-bc7b-25425e95946e
5+
creation_time: 2022-07-26T09:11:03Z
6+
producer:
7+
name: Goblint
8+
version: heads/yaml-witness-unassume-0-g48503c690-dirty
9+
command_line: '''./goblint'' ''--enable'' ''dbg.debug'' ''--enable'' ''dbg.regression''
10+
''--html'' ''--set'' ''ana.activated[+]'' ''apron'' ''--enable'' ''witness.yaml.enabled''
11+
''76-apron-unassume-extra-trivial.c'''
12+
task:
13+
input_files:
14+
- 76-apron-unassume-extra-trivial.c
15+
input_file_hashes:
16+
76-apron-unassume-extra-trivial.c: 71e40ed99b5217343d0831e293e7207e5bd30ce53f6ab73f0c1ef6ced1afcc60
17+
data_model: LP64
18+
language: C
19+
content:
20+
- invariant:
21+
type: loop_invariant
22+
location:
23+
file_name: 76-apron-unassume-extra-trivial.c
24+
line: 6
25+
column: 3
26+
function: main
27+
value: 100LL - (long long )i >= 0LL
28+
format: c_expression
29+
- invariant:
30+
type: loop_invariant
31+
location:
32+
file_name: 76-apron-unassume-extra-trivial.c
33+
line: 6
34+
column: 3
35+
function: main
36+
value: (long long )i >= 0LL
37+
format: c_expression
38+
- invariant:
39+
type: loop_invariant
40+
location:
41+
file_name: 76-apron-unassume-extra-trivial.c
42+
line: 6
43+
column: 3
44+
function: main
45+
value: '1'
46+
format: c_expression

tests/regression/56-witness/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
(run %{update_suite} apron-unassume-precheck -q)
2424
(run %{update_suite} apron-tracked-global-annot -q)
2525
(run %{update_suite} apron-unassume-no-strengthening -q)
26-
(run %{update_suite} apron-unassume-set-tokens -q)))))
26+
(run %{update_suite} apron-unassume-set-tokens -q)
27+
(run %{update_suite} apron-unassume-extra-trivial -q)))))
2728

2829
(cram
2930
(deps (glob_files *.c) (glob_files ??-*.yml) (glob_files strip-ghost-alloc.sh)))

0 commit comments

Comments
 (0)