Skip to content

Commit d2d183c

Browse files
authored
Merge pull request #1940 from goblint/addressdomain-of_int
Make int to pointer case more precise for non-zero ints, reuse `AD.of_int`
2 parents 62747a8 + 7881999 commit d2d183c

File tree

4 files changed

+12
-27
lines changed

4 files changed

+12
-27
lines changed

src/analyses/base.ml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,7 @@ struct
322322
in
323323
match addr with
324324
| Addr (x, o) -> AD.of_mval (x, addToOffset n (Some x.vtype) o)
325-
| NullPtr ->
326-
begin match ID.equal_to Z.zero n with
327-
| `Eq -> AD.null_ptr
328-
| `Neq -> AD.unknown_ptr
329-
| `Top -> AD.top_ptr
330-
end
325+
| NullPtr -> AD.of_int n
331326
| UnknownPtr
332327
| StrPtr _ ->
333328
begin match ID.equal_to Z.zero n with
@@ -2784,9 +2779,7 @@ struct
27842779
let p_addr =
27852780
match p_rv with
27862781
| Address a -> a
2787-
(* TODO: don't we already have logic for this? *)
2788-
| Int i when ID.equal_to Z.zero i = `Eq -> AD.null_ptr
2789-
| Int i -> AD.top_ptr
2782+
| Int i -> AD.of_int i
27902783
| _ -> AD.top_ptr (* TODO: why does this ever happen? *)
27912784
in
27922785
let p_addr' = AD.remove NullPtr p_addr in (* realloc with NULL is same as malloc, remove to avoid unknown value from NullPtr access *)

src/cdomain/value/cdomains/addressDomain.ml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,10 @@ struct
222222
let to_bool x = if is_null x then Some false else if is_not_null x then Some true else None
223223

224224
let of_int i =
225-
if ID.equal_to Z.zero i = `Eq then
226-
null_ptr
227-
else if ID.equal_to Z.one i = `Eq then
228-
not_null
229-
else
230-
match ID.to_excl_list i with
231-
| Some (xs, _) when List.exists BigIntOps.(equal (zero)) xs -> not_null
232-
| _ -> top_ptr
225+
match ID.equal_to Z.zero i with
226+
| `Eq -> null_ptr
227+
| `Neq -> not_null
228+
| `Top -> top_ptr
233229

234230
let to_int x =
235231
let ik = Cilfacade.ptr_ikind () in

src/cdomain/value/cdomains/valueDomain.ml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,7 @@ struct
522522
(* cast to voidPtr are ignored TODO what happens if our value does not fit? *)
523523
| TPtr (t,_) ->
524524
Address (match v with
525-
| Int x when ID.equal_to Z.zero x = `Eq -> AD.null_ptr
526-
| Int x -> AD.top_ptr
525+
| Int x -> AD.of_int x
527526
(* we ignore casts to void* (above)! TODO report UB! *)
528527
| Address x -> cast_addr t x
529528
(*| Address x -> x*)
@@ -613,10 +612,7 @@ struct
613612
| (Int x, Int y) -> (try Int (ID.join x y) with IntDomain.IncompatibleIKinds m -> Messages.warn ~category:Analyzer ~tags:[Category Imprecise] "%s" m; Top)
614613
| (Float x, Float y) -> Float (FD.join x y)
615614
| (Int x, Address y)
616-
| (Address y, Int x) -> Address (match ID.equal_to Z.zero x with (* TODO: AD.of_int? *)
617-
| `Eq -> AD.join AD.null_ptr y
618-
| `Neq -> AD.(join y not_null)
619-
| `Top -> AD.join y AD.top_ptr)
615+
| (Address y, Int x) -> Address (AD.join y (AD.of_int x))
620616
| (Address x, Address y) -> Address (AD.join x y)
621617
| (Struct x, Struct y) -> Struct (Structs.join x y)
622618
| (Union x, Union y) -> Union (Unions.join x y)
@@ -646,10 +642,7 @@ struct
646642
| (Float x, Float y) -> Float (FD.widen x y)
647643
(* TODO: symmetric widen, wtf? *)
648644
| (Int x, Address y)
649-
| (Address y, Int x) -> Address (match ID.equal_to Z.zero x with (* TODO: AD.of_int? *)
650-
| `Eq -> AD.widen AD.null_ptr (AD.join AD.null_ptr y)
651-
| `Neq -> AD.(widen y (join y not_null))
652-
| `Top -> AD.widen y (AD.join y AD.top_ptr))
645+
| (Address y, Int x) -> Address (AD.widen y (AD.join y (AD.of_int x)))
653646
| (Address x, Address y) -> Address (AD.widen x y)
654647
| (Struct x, Struct y) -> Struct (Structs.widen x y)
655648
| (Union x, Union y) -> Union (Unions.widen x y)

tests/regression/00-sanity/53-ptr-and-int.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ int main ()
99
// &x equaling the constant 3 is exceedingly unlikely
1010
__goblint_check(y == 1); //UNKNOWN!
1111

12+
int *p = three; // implicit cast of 3 to pointer
13+
__goblint_check(p);
14+
1215
return 0;
1316
}

0 commit comments

Comments
 (0)