Skip to content

Commit aeb8b2c

Browse files
authored
Merge pull request #1684 from goblint/assert-ptr
Replace `int` -> `_Bool` in goblint stubs arguments
2 parents 1ce5f17 + 6d5350b commit aeb8b2c

File tree

22 files changed

+253
-247
lines changed

22 files changed

+253
-247
lines changed

lib/goblint/runtime/include/goblint.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
void __goblint_check(int exp);
2-
void __goblint_assume(int exp);
3-
void __goblint_assert(int exp);
1+
void __goblint_check(_Bool exp);
2+
void __goblint_assume(_Bool exp);
3+
void __goblint_assert(_Bool exp);
44

55
void __goblint_assume_join(/* pthread_t thread */); // undeclared argument to avoid pthread.h interfering with Linux kernel headers
66
void __goblint_globalize(void *ptr);

lib/goblint/runtime/src/goblint.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
// Empty implementations (instead of asserts) because annotating documentation promises no-op right now.
55

6-
void __goblint_check(int exp) {
6+
void __goblint_check(_Bool exp) {
77

88
}
99

10-
void __goblint_assume(int exp) {
10+
void __goblint_assume(_Bool exp) {
1111

1212
}
1313

14-
void __goblint_assert(int exp) {
14+
void __goblint_assert(_Bool exp) {
1515

1616
}
1717

lib/libc/stub/include/assert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef GOBLINT_NO_ASSERT
22

3-
void __goblint_assert(int expression);
3+
void __goblint_assert(_Bool expression);
44
#undef assert
55
#define assert(expression) __goblint_assert(expression)
66

src/analyses/abortUnless.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ struct
3030
if man.local then
3131
match f.sformals with
3232
| [arg] when isIntegralType arg.vtype ->
33-
(match man.ask (EvalInt (Lval (Var arg, NoOffset))) with
34-
| v when Queries.ID.is_bot v -> false
35-
| v -> BatOption.default false (Queries.ID.to_bool v))
33+
(match Queries.eval_bool (Analyses.ask_of_man man) (Lval (Var arg, NoOffset)) with
34+
| `Bot -> false
35+
| `Lifted b -> b
36+
| `Top -> false)
3637
| _ ->
3738
(* should not happen, man.local should always be false in this case *)
3839
false

src/analyses/assert.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ struct
1414
(* transfer functions *)
1515

1616
let assert_fn man e check refine =
17-
18-
let check_assert e st =
19-
match man.ask (Queries.EvalInt e) with
20-
| v when Queries.ID.is_bot v -> `Bot
21-
| v -> Option.map_default (fun b -> `Lifted b) `Top (Queries.ID.to_bool v)
22-
in
2317
let expr = CilType.Exp.show e in
2418
let warn warn_fn ?annot msg = if check then
2519
if get_bool "dbg.regression" then ( (* This only prints unexpected results (with the difference) as indicated by the comment behind the assert (same as used by the regression test script). *)
@@ -38,7 +32,7 @@ struct
3832
warn_fn msg
3933
in
4034
(* TODO: use format instead of %s for the following messages *)
41-
match check_assert e man.local with
35+
match Queries.eval_bool (Analyses.ask_of_man man) e with
4236
| `Lifted false ->
4337
warn (M.error ~category:Assert "%s") ~annot:"FAIL" ("Assertion \"" ^ expr ^ "\" will fail.");
4438
if refine then raise Analyses.Deadcode else man.local

src/analyses/memLeak.ml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,16 @@ struct
228228
warn_for_multi_threaded_due_to_abort man;
229229
state
230230
| Assert { exp; refine = true; _ } ->
231-
begin match man.ask (Queries.EvalInt exp) with
232-
| a when Queries.ID.is_bot a -> M.warn ~category:Assert "assert expression %a is bottom" d_exp exp
233-
| a ->
234-
begin match Queries.ID.to_bool a with
235-
| Some true -> ()
236-
| Some false ->
237-
(* If we know for sure that the expression in "assert" is false => need to check for memory leaks *)
238-
warn_for_multi_threaded_due_to_abort man;
239-
check_for_mem_leak man
240-
| None ->
241-
warn_for_multi_threaded_due_to_abort man;
242-
check_for_mem_leak man ~assert_exp_imprecise:true ~exp:(Some exp)
243-
end
231+
begin match Queries.eval_bool (Analyses.ask_of_man man) exp with
232+
| `Bot -> M.warn ~category:Assert "assert expression %a is bottom" d_exp exp
233+
| `Lifted true -> ()
234+
| `Lifted false ->
235+
(* If we know for sure that the expression in "assert" is false => need to check for memory leaks *)
236+
warn_for_multi_threaded_due_to_abort man;
237+
check_for_mem_leak man
238+
| `Top ->
239+
warn_for_multi_threaded_due_to_abort man;
240+
check_for_mem_leak man ~assert_exp_imprecise:true ~exp:(Some exp)
244241
end;
245242
state
246243
| ThreadExit _ ->

src/analyses/unassumeAnalysis.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ struct
261261
let e = List.fold_left (fun a {exp = b; _} -> Cil.(BinOp (LAnd, a, b, intType))) x.exp xs in
262262
M.info ~category:Witness "unassume invariant: %a" CilType.Exp.pretty e;
263263
if not !AnalysisState.postsolving then (
264-
if not (GobConfig.get_bool "ana.unassume.precheck" && Queries.ID.to_bool (man.ask (EvalInt e)) = Some false) then (
264+
if not (GobConfig.get_bool "ana.unassume.precheck" && Queries.eval_bool (Analyses.ask_of_man man) e = `Lifted false) then (
265265
let tokens = x.token :: List.map (fun {token; _} -> token) xs in
266266
man.emit (Unassume {exp = e; tokens});
267267
List.iter WideningTokenLifter.add tokens
@@ -280,9 +280,8 @@ struct
280280
let body man fd =
281281
let pres = FH.find_all fun_pres fd in
282282
let st = List.fold_left (fun acc pre ->
283-
let v = man.ask (EvalInt pre) in
284283
(* M.debug ~category:Witness "%a precondition %a evaluated to %a" CilType.Fundec.pretty fd CilType.Exp.pretty pre Queries.ID.pretty v; *)
285-
if Queries.ID.to_bool v = Some true then
284+
if Queries.eval_bool (Analyses.ask_of_man man) pre = `Lifted true then
286285
D.add pre acc
287286
else
288287
acc

src/domains/queries.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,12 @@ let may_be_equal = eval_int_binop (module MayBool) Eq
561561
(** Backwards-compatibility for former [MayBeLess] query. *)
562562
let may_be_less = eval_int_binop (module MayBool) Lt
563563

564+
let eval_bool (ask: ask) e: BoolDomain.FlatBool.t =
565+
let e' = CastE (TInt (IBool, []), e) in
566+
match ask.f (EvalInt e') with
567+
| v when ID.is_bot v || ID.is_bot_ikind v -> `Bot
568+
| v -> BatOption.map_default (fun b -> `Lifted b) `Top (ID.to_bool v)
569+
564570

565571
module Set = BatSet.Make (Any)
566572
module Hashtbl = BatHashtbl.Make (Any)

src/transform/expressionEvaluation.ml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,10 @@ struct
125125
value_after
126126

127127
method private try_ask location expression =
128-
match ~? (fun () -> (ask location).Queries.f (Queries.EvalInt expression)) with
129-
(* Inapplicable: Unreachable *)
130-
| Some x when Queries.ID.is_bot_ikind x -> None
131-
| Some x ->
132-
begin match Queries.ID.to_int x with
133-
(* Evaluable: Definite *)
134-
| Some i -> Some (Some (not (Z.equal i Z.zero)))
135-
(* Evaluable: Inconclusive *)
136-
| None -> Some None
137-
end
128+
match ~? (fun () -> Queries.eval_bool (ask location) expression) with
129+
| Some `Bot -> None (* Inapplicable: Unreachable *)
130+
| Some (`Lifted b) -> Some (Some b) (* Evaluable: Definite *)
131+
| Some `Top -> Some None (* Evaluable: Inconclusive *)
138132
(* Inapplicable: Unlisted *)
139133
| None
140134
| exception Not_found -> None

src/util/library/libraryFunctions.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ let intmax_t = lazy (
1616
!res
1717
)
1818

19+
let stripOuterBoolCast = function
20+
| CastE (TInt (IBool, _), e) -> e
21+
| Const (CInt (b, IBool, s)) -> Const (CInt (b, IInt, s))
22+
| e -> e
23+
1924
(** C standard library functions.
2025
These are specified by the C standard. *)
2126
let c_descs_list: (string * LibraryDesc.t) list = LibraryDsl.[
@@ -833,9 +838,9 @@ let linux_kernel_descs_list: (string * LibraryDesc.t) list = LibraryDsl.[
833838
(** Goblint functions. *)
834839
let goblint_descs_list: (string * LibraryDesc.t) list = LibraryDsl.[
835840
("__goblint_unknown", unknown [drop' [w]]);
836-
("__goblint_check", special [__ "exp" []] @@ fun exp -> Assert { exp; check = true; refine = false });
837-
("__goblint_assume", special [__ "exp" []] @@ fun exp -> Assert { exp; check = false; refine = true });
838-
("__goblint_assert", special [__ "exp" []] @@ fun exp -> Assert { exp; check = true; refine = get_bool "sem.assert.refine" });
841+
("__goblint_check", special [__ "exp" []] @@ fun exp -> Assert { exp = stripOuterBoolCast exp; check = true; refine = false });
842+
("__goblint_assume", special [__ "exp" []] @@ fun exp -> Assert { exp = stripOuterBoolCast exp; check = false; refine = true });
843+
("__goblint_assert", special [__ "exp" []] @@ fun exp -> Assert { exp = stripOuterBoolCast exp; check = true; refine = get_bool "sem.assert.refine" });
839844
("__goblint_globalize", special [__ "ptr" []] @@ fun ptr -> Globalize ptr);
840845
("__goblint_split_begin", unknown [drop "exp" []]);
841846
("__goblint_split_end", unknown [drop "exp" []]);

0 commit comments

Comments
 (0)