Skip to content

Commit 11bfc3e

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

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/ansi-c/library/new.c

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

1319
// ensure it's not recorded as deallocated
@@ -35,6 +41,12 @@ inline void *__new_array(__CPROVER_size_t count, __CPROVER_size_t size)
3541
// The constructor call is done by the front-end.
3642
// This just does memory allocation.
3743
__CPROVER_HIDE:;
44+
// ensure that all bytes in the allocated memory can be addressed
45+
// using our object:offset encoding as specified in
46+
// flattening/pointer_logic.h; also avoid sign-extension issues
47+
// for 32-bit systems that yields a maximum allocation of 2^23-1,
48+
// i.e., just under 8MB
49+
__CPROVER_assume(size * count < (1UL << ((sizeof(char *) - 1) * 8 - 1)));
3850
void *res;
3951
res = __CPROVER_allocate(size*count, 0);
4052

src/ansi-c/library/stdlib.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ inline void *malloc(__CPROVER_size_t malloc_size)
106106
// realistically, malloc may return NULL,
107107
// and __CPROVER_allocate doesn't, but no one cares
108108
__CPROVER_HIDE:;
109+
// ensure that all bytes in the allocated memory can be addressed
110+
// using our object:offset encoding as specified in
111+
// flattening/pointer_logic.h; also avoid sign-extension issues
112+
// for 32-bit systems that yields a maximum allocation of 2^23-1,
113+
// i.e., just under 8MB
114+
__CPROVER_assume(malloc_size < (1UL << ((sizeof(char *) - 1) * 8 - 1)));
109115
void *malloc_res;
110116
malloc_res = __CPROVER_allocate(malloc_size, 0);
111117

@@ -132,6 +138,12 @@ __CPROVER_bool __VERIFIER_nondet___CPROVER_bool();
132138
inline void *__builtin_alloca(__CPROVER_size_t alloca_size)
133139
{
134140
__CPROVER_HIDE:;
141+
// ensure that all bytes in the allocated memory can be addressed
142+
// using our object:offset encoding as specified in
143+
// flattening/pointer_logic.h; also avoid sign-extension issues
144+
// for 32-bit systems that yields a maximum allocation of 2^23-1,
145+
// i.e., just under 8MB
146+
__CPROVER_assume(alloca_size < (1UL << ((sizeof(char *) - 1) * 8 - 1)));
135147
void *res;
136148
res = __CPROVER_allocate(alloca_size, 0);
137149

0 commit comments

Comments
 (0)