Skip to content

Commit 21c9890

Browse files
committed
Constrain the malloc/alloca size to fit our object:offset encoding
See diffblue#311 for an extended discussion.
1 parent 403f1e4 commit 21c9890

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/ansi-c/library/new.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ inline void *__new(__typeof__(sizeof(int)) malloc_size)
66
// This just does memory allocation.
77
__CPROVER_HIDE:;
88
void *res;
9+
// ensure that all bytes in the allocated memory can be addressed
10+
// using our object:offset encoding as specified in
11+
// flattening/pointer_logic.h; also avoid sign-extension issues
12+
// for 32-bit systems that yields a maximum allocation of 2^23-1,
13+
// i.e., just under 8MB
14+
__CPROVER_assume(malloc_size<(1ULL<<((sizeof(char*)-1)*8-1)));
915
res=__CPROVER_malloc(malloc_size);
1016

1117
// ensure it's not recorded as deallocated
@@ -31,6 +37,12 @@ inline void *__new_array(__CPROVER_size_t count, __CPROVER_size_t size)
3137
// The constructor call is done by the front-end.
3238
// This just does memory allocation.
3339
__CPROVER_HIDE:;
40+
// ensure that all bytes in the allocated memory can be addressed
41+
// using our object:offset encoding as specified in
42+
// flattening/pointer_logic.h; also avoid sign-extension issues
43+
// for 32-bit systems that yields a maximum allocation of 2^23-1,
44+
// i.e., just under 8MB
45+
__CPROVER_assume(size*count<(1ULL<<((sizeof(char*)-1)*8-1)));
3446
void *res;
3547
res=__CPROVER_malloc(size*count);
3648

src/ansi-c/library/stdlib.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ inline void *malloc(__CPROVER_size_t malloc_size);
6767
inline void *calloc(__CPROVER_size_t nmemb, __CPROVER_size_t size)
6868
{
6969
__CPROVER_HIDE:;
70+
// ensure that all bytes in the allocated memory can be addressed
71+
// using our object:offset encoding as specified in
72+
// flattening/pointer_logic.h; also avoid sign-extension issues
73+
// for 32-bit systems that yields a maximum allocation of 2^23-1,
74+
// i.e., just under 8MB
75+
__CPROVER_assume(nmemb*size<(1ULL<<((sizeof(char*)-1)*8-1)));
7076
void *res;
7177
res=malloc(nmemb*size);
7278
#ifdef __CPROVER_STRING_ABSTRACTION
@@ -92,6 +98,12 @@ inline void *malloc(__CPROVER_size_t malloc_size)
9298
// realistically, malloc may return NULL,
9399
// and __CPROVER_malloc doesn't, but no one cares
94100
__CPROVER_HIDE:;
101+
// ensure that all bytes in the allocated memory can be addressed
102+
// using our object:offset encoding as specified in
103+
// flattening/pointer_logic.h; also avoid sign-extension issues
104+
// for 32-bit systems that yields a maximum allocation of 2^23-1,
105+
// i.e., just under 8MB
106+
__CPROVER_assume(malloc_size<(1ULL<<((sizeof(char*)-1)*8-1)));
95107
void *malloc_res;
96108
malloc_res=__CPROVER_malloc(malloc_size);
97109

@@ -116,6 +128,12 @@ inline void *malloc(__CPROVER_size_t malloc_size)
116128
inline void *__builtin_alloca(__CPROVER_size_t alloca_size)
117129
{
118130
__CPROVER_HIDE:;
131+
// ensure that all bytes in the allocated memory can be addressed
132+
// using our object:offset encoding as specified in
133+
// flattening/pointer_logic.h; also avoid sign-extension issues
134+
// for 32-bit systems that yields a maximum allocation of 2^23-1,
135+
// i.e., just under 8MB
136+
__CPROVER_assume(alloca_size<(1ULL<<((sizeof(char*)-1)*8-1)));
119137
void *res;
120138
res=__CPROVER_malloc(alloca_size);
121139

0 commit comments

Comments
 (0)