Skip to content

Commit 46e56bd

Browse files
Merge pull request #1262 from goblint/issue_1259
Fix `BlobSize` for calloc
2 parents 9c808c9 + d1d85b3 commit 46e56bd

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/analyses/base.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,9 @@ struct
11391139

11401140
(* interpreter end *)
11411141

1142+
let is_not_alloc_var ctx v =
1143+
not (ctx.ask (Queries.IsAllocVar v))
1144+
11421145
let is_not_heap_alloc_var ctx v =
11431146
let is_alloc = ctx.ask (Queries.IsAllocVar v) in
11441147
not is_alloc || (is_alloc && not (ctx.ask (Queries.IsHeapVar v)))
@@ -1277,7 +1280,7 @@ struct
12771280
(* If there's a non-heap var or an offset in the lval set, we answer with bottom *)
12781281
(* If we're asking for the BlobSize from the base address, then don't check for offsets => we want to avoid getting bot *)
12791282
if AD.exists (function
1280-
| Addr (v,o) -> is_not_heap_alloc_var ctx v || (if not from_base_addr then o <> `NoOffset else false)
1283+
| Addr (v,o) -> is_not_alloc_var ctx v || (if not from_base_addr then o <> `NoOffset else false)
12811284
| _ -> false) a then
12821285
Queries.Result.bot q
12831286
else (
@@ -1289,9 +1292,15 @@ struct
12891292
else
12901293
a
12911294
in
1292-
let r = get ~full:true (Analyses.ask_of_ctx ctx) ctx.global ctx.local a None in
1295+
let r = get ~full:true (Analyses.ask_of_ctx ctx) ctx.global ctx.local a None in
12931296
(* ignore @@ printf "BlobSize %a = %a\n" d_plainexp e VD.pretty r; *)
12941297
(match r with
1298+
| Array a ->
1299+
(* unroll into array for Calloc calls *)
1300+
(match ValueDomain.CArrays.get (Queries.to_value_domain_ask (Analyses.ask_of_ctx ctx)) a (None, (IdxDom.of_int (Cilfacade.ptrdiff_ikind ()) BI.zero)) with
1301+
| Blob (_,s,_) -> `Lifted s
1302+
| _ -> Queries.Result.top q
1303+
)
12951304
| Blob (_,s,_) -> `Lifted s
12961305
| _ -> Queries.Result.top q)
12971306
)

src/analyses/memOutOfBounds.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,17 @@ struct
6969
in
7070
host_contains_a_ptr host || offset_contains_a_ptr offset
7171

72-
let points_to_heap_only ctx ptr =
72+
let points_to_alloc_only ctx ptr =
7373
match ctx.ask (Queries.MayPointTo ptr) with
7474
| a when not (Queries.AD.is_top a)->
7575
Queries.AD.for_all (function
76-
| Addr (v, o) -> ctx.ask (Queries.IsHeapVar v)
76+
| Addr (v, o) -> ctx.ask (Queries.IsAllocVar v)
7777
| _ -> false
7878
) a
7979
| _ -> false
8080

8181
let get_size_of_ptr_target ctx ptr =
82-
if points_to_heap_only ctx ptr then
82+
if points_to_alloc_only ctx ptr then
8383
(* Ask for BlobSize from the base address (the second component being set to true) in order to avoid BlobSize giving us bot *)
8484
ctx.ask (Queries.BlobSize {exp = ptr; base_address = true})
8585
else
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//PARAM: --set ana.activated[+] useAfterFree --set ana.activated[+] threadJoins --set ana.activated[+] memOutOfBounds --enable ana.int.interval --set ana.base.arrays.domain partitioned
2+
#include <pthread.h>
3+
#include <goblint.h>
4+
5+
int main(int argc, char **argv)
6+
{
7+
int* ptrCalloc = calloc(100UL,8UL);
8+
*ptrCalloc = 8; //NOWARN
9+
}

0 commit comments

Comments
 (0)