The heap
Memory from malloc is the hardest kind to get right in C. An allocation can
fail, it must be freed exactly once, and only by someone who owns it, and every
allocation must be freed. cerastes proves all of these. This chapter allocates a
single object, hands it from one function to another, and shows the three
mistakes cerastes catches.
An allocation and its owner
#include <cerastes.h>
#include <stdlib.h>
int *make_cell(int v)
CERASTES_ENSURES(r, r == NULL || (cerastes_owns_block(r) && *r == v))
{
int *p = (int *)malloc(sizeof(int));
if (p == NULL) {
return NULL;
}
*p = v;
return p;
}
void drop_cell(int *p)
CERASTES_REQUIRES(p == NULL || cerastes_owns_block(p))
{
free(p);
}
int main(void)
{
int *p = make_cell(7);
if (p == NULL) {
return 1;
}
CERASTES_CHECK(*p == 7);
drop_cell(p);
return 0;
}
heap_cell.c: verified (3 functions; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)
malloc can return NULL, and make_cell tests for it before it writes.
cerastes_owns_block(r) in its postcondition hands the whole allocation to
the caller: everything free needs, not just the right to read and write
*r. drop_cell takes it back with the same words in its precondition and
frees it. main tests the result, uses it, and passes it on.
malloc and free get their contracts from vlibc, the C library contracts
cerastes is built with. An allocation is one object of the type the result is
converted to, at once: (int *)malloc(sizeof(int)).
Forgetting the null test
Leave out the test in make_cell, and the write may go through a null
pointer:
#include <cerastes.h>
#include <stdlib.h>
int *make_cell(int v)
CERASTES_ENSURES(r, r == NULL || (cerastes_owns_block(r) && *r == v))
{
int *p = (int *)malloc(sizeof(int));
*p = v;
return p;
}
void drop_cell(int *p)
CERASTES_REQUIRES(p == NULL || cerastes_owns_block(p))
{
free(p);
}
int main(void)
{
int *p = make_cell(7);
if (p == NULL) {
return 1;
}
CERASTES_CHECK(*p == 7);
drop_cell(p);
return 0;
}
heap_cell_no_null_test_error.c:13: error: null-dereference: dereferences `p`, which may be null
Forgetting to free
A function that still owns an allocation when it returns, and does not hand it
to its caller, has lost it for good. That is a leak, reported at the closing
brace, and main is no exception:
#include <cerastes.h>
#include <stdlib.h>
int *make_cell(int v)
CERASTES_ENSURES(r, r == NULL || (cerastes_owns_block(r) && *r == v))
{
int *p = (int *)malloc(sizeof(int));
if (p == NULL) {
return NULL;
}
*p = v;
return p;
}
void drop_cell(int *p)
CERASTES_REQUIRES(p == NULL || cerastes_owns_block(p))
{
free(p);
}
int main(void)
{
int *p = make_cell(7);
if (p == NULL) {
return 1;
}
CERASTES_CHECK(*p == 7);
return 0;
}
heap_cell_no_free_error.c:34: error: leak: `main` may return while it still owns an allocation that its `ensures` does not hand back
Freeing twice
After free, the allocation is gone and nobody owns it. Hand it to drop_cell
a second time:
#include <cerastes.h>
#include <stdlib.h>
int *make_cell(int v)
CERASTES_ENSURES(r, r == NULL || (cerastes_owns_block(r) && *r == v))
{
int *p = (int *)malloc(sizeof(int));
if (p == NULL) {
return NULL;
}
*p = v;
return p;
}
void drop_cell(int *p)
CERASTES_REQUIRES(p == NULL || cerastes_owns_block(p))
{
free(p);
}
int main(void)
{
int *p = make_cell(7);
if (p == NULL) {
return 1;
}
CERASTES_CHECK(*p == 7);
drop_cell(p);
drop_cell(p);
return 0;
}
heap_cell_double_free_error.c:34: error: permission: calls `drop_cell`, which needs to own the whole allocation `p` points to, without owning it
The second drop_cell(p) asks for the whole allocation, and main gave it
away in the first call: a permission error at the call. Two calls of free(p)
in one function are reported as invalid-free at the second one, since the
allocation may already be freed.
A structure on the heap
An allocated structure works the same way, and its members are objects like those of any structure:
#include <cerastes.h>
#include <stdlib.h>
struct point {
int x;
int y;
};
struct point *make_point(int x, int y)
CERASTES_ENSURES(r, r == NULL || (cerastes_owns_block(r) && r->x == x && r->y == y))
{
struct point *p = (struct point *)malloc(sizeof(struct point));
if (p == NULL) {
return NULL;
}
p->x = x;
p->y = y;
return p;
}
int main(void)
{
struct point *p = make_point(3, 4);
if (p == NULL) {
return 1;
}
int sum = p->x + p->y;
free(p);
return sum == 7 ? 0 : 1;
}
heap_struct.c: verified (2 functions; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)
Here main frees the allocation itself: owning the whole block, from
the postcondition of make_point, is what allows it.
An owner you cannot forget
A function that allocates and frees in the same place can mark the variable that
holds the allocation CERASTES_OWNING. The variable then owns the block, and
cerastes checks that it no longer does when the variable’s scope ends:
#include <cerastes.h>
#include <stdlib.h>
int round_trip(int v)
CERASTES_ENSURES(r, r == v || r == -1)
{
CERASTES_OWNING int *p = (int *)malloc(sizeof(int));
if (p == NULL) {
return -1;
}
*p = v;
int w = *p;
free(p);
return w;
}
heap_owner.c: verified (1 function; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)
round_trip frees p before it returns on the path where the allocation
succeeded, and returns without allocating on the other. Add a path that returns
early and forgets the free, and cerastes reports it where the variable’s scope
ends, at the return that leaves it, instead of at the end of the function:
#include <cerastes.h>
#include <stdlib.h>
int round_trip(int v)
CERASTES_ENSURES(r, r == v || r == -1 || r == -2)
{
CERASTES_OWNING int *p = (int *)malloc(sizeof(int));
if (p == NULL) {
return -1;
}
*p = v;
if (v < 0) {
return -2;
}
int w = *p;
free(p);
return w;
}
heap_owner_leak_error.c:18: error: leak: owning `p` still holds its block
The same words appear when the variable is assigned a new allocation while it holds one. A variable that is not marked is checked at the end of the function only, which is why this chapter’s earlier leak is reported at the closing brace.
An owner that outlives a call
A global can own an allocation too. cerastes::owning on a global says that the
block it points to belongs to it across calls. So a function whose contract
names the global with cerastes_owns(g) receives that block with it, and must give
back a global that is null or holds a whole block:
#include <cerastes.h>
#include <stdlib.h>
static int *g CERASTES_OWNING = NULL;
void install(int v)
CERASTES_REQUIRES(cerastes_owns(g))
CERASTES_ENSURES(cerastes_owns(g))
{
free(g);
g = (int *)malloc(sizeof(int));
if (g != NULL) {
*g = v;
}
}
int main(void)
{
install(1);
install(2);
return 0;
}
main calls install twice: the second call frees the first block before it
allocates another. main then returns while g still holds its block. That is not a
leak: a global owner may keep its block for the whole program. A function that frees
the block and forgets to reset the global is reported at its return:
#include <cerastes.h>
#include <stdlib.h>
static int *g CERASTES_OWNING = NULL;
void discard(void)
CERASTES_REQUIRES(cerastes_owns(g))
CERASTES_ENSURES(cerastes_owns(g))
{
free(g);
}
int main(void)
{
discard();
return 0;
}
heap_global_owner_error.c:16: error: permission: returns while owning global `g` does not hold its block: set it to `NULL` after freeing it
heap_global_owner_error.c:13: note: this is the postcondition
A structure that owns its buffer
A structure can own an allocation through one of its members. CERASTES_OWNING on
the member data says that the block it points to belongs to the structure. So
whoever owns the structure owns the buffer too: buf_make hands both to its caller
(its result is owning), and buf_destroy, which takes the structure’s block,
takes the buffer with it, without saying so in its contract:
#include <cerastes.h>
#include <stdlib.h>
struct buf {
int *data CERASTES_OWNING;
int len;
};
CERASTES_OWNING struct buf *buf_make(int v)
{
struct buf *b = (struct buf *)malloc(sizeof(struct buf));
if (b == NULL) {
return NULL;
}
b->len = 1;
b->data = (int *)malloc(sizeof(int));
if (b->data != NULL) {
*b->data = v;
}
return b;
}
void buf_destroy(struct buf *b)
CERASTES_REQUIRES(b == NULL || cerastes_owns_block(b))
{
if (b != NULL) {
free(b->data);
free(b);
}
}
int main(void)
{
struct buf *b = buf_make(7);
buf_destroy(b);
return 0;
}
A destructor must therefore free the buffer before the structure. One that frees only the structure still holds the buffer when it returns, and nothing else can free it:
#include <cerastes.h>
#include <stdlib.h>
struct buf {
int *data CERASTES_OWNING;
int len;
};
CERASTES_OWNING struct buf *buf_make(int v)
{
struct buf *b = (struct buf *)malloc(sizeof(struct buf));
if (b == NULL) {
return NULL;
}
b->len = 1;
b->data = (int *)malloc(sizeof(int));
if (b->data != NULL) {
*b->data = v;
}
return b;
}
void buf_destroy(struct buf *b)
CERASTES_REQUIRES(b == NULL || cerastes_owns_block(b))
{
if (b != NULL) {
free(b);
}
}
int main(void)
{
struct buf *b = buf_make(7);
buf_destroy(b);
return 0;
}
heap_owning_field_error.c:33: error: leak: `buf_destroy` may return while it still owns an allocation that its `ensures` does not hand back
A function that borrows the structure with cerastes_owns(*b) borrows the buffer
too, and may free it and store a new one, but a pointer it stores into the member
must be null or a whole allocation it holds.
That freedom has a price for the caller. A pointer taken into the buffer before the
call, int *t = b→data;, may not be used after it, because the callee may have
replaced the block. A function that promises not to replace it says so with
CERASTES_NOT_INVALIDATING on the parameter: whatever the contract takes through the
parameter comes back, and the pointer to the buffer is unchanged. bump below does,
and use writes through t after the call (The
not_invalidating marker has the rules, and what the verifier checks about the body):
#include <cerastes.h>
#include <stdlib.h>
struct buf {
int *data CERASTES_OWNING;
int n;
};
void bump(struct buf *p CERASTES_OWNING CERASTES_NOT_INVALIDATING)
{
if (p != NULL) {
p->n = 0;
}
}
int use(void)
CERASTES_ENSURES(r, r == 0 || r == -1)
{
struct buf *b CERASTES_OWNING = (struct buf *)calloc(1, sizeof(struct buf));
if (b == NULL) {
return -1;
}
b->data = (int *)calloc(1, sizeof(int));
if (b->data == NULL) {
free(b);
return -1;
}
int *t = b->data;
bump(b);
*t = 1;
free(b->data);
free(b);
return 0;
}
A buffer is often allocated first and filled later, and then the function that fills it is handed a buffer that has no value yet. By default that is reported, since a contract that owns the structure also owns a buffer with a value:
#include <cerastes.h>
#include <stdlib.h>
struct buf {
int *data CERASTES_OWNING;
int n;
};
void buf_load(struct buf *b)
CERASTES_REQUIRES(cerastes_owns(*b))
CERASTES_ENSURES(cerastes_owns(*b))
{
if (b->data != NULL) {
*b->data = 1;
}
}
int main(void)
{
struct buf s = { NULL, 0 };
s.data = (int *)malloc(sizeof(int));
buf_load(&s);
free(s.data);
return 0;
}
heap_marked_buffer_error.c:26: error: uninitialized: calls `buf_load`, which needs `*b->data` to have a value; it may not have one here
CERASTES_REF_TO_UNINIT on the member, beside CERASTES_OWNING, says that its buffer
may hold no value. buf_load may now be given the buffer as it is; it writes it, and
a function that read it first would be reported at the read
(Marked owning members has the rules):
#include <cerastes.h>
#include <stdlib.h>
struct buf {
int *data CERASTES_OWNING CERASTES_REF_TO_UNINIT;
int n;
};
void buf_load(struct buf *b)
CERASTES_REQUIRES(cerastes_owns(*b))
CERASTES_ENSURES(cerastes_owns(*b))
{
if (b->data != NULL) {
*b->data = 1;
}
}
int main(void)
{
struct buf s = { NULL, 0 };
s.data = (int *)malloc(sizeof(int));
buf_load(&s);
free(s.data);
return 0;
}
What you have learned
-
The result of
mallocmust be tested againstNULLbefore it is used. -
cerastes_owns_block(p)is ownership of a whole allocation; a contract uses it to hand an allocation to the caller or to take one. -
Every allocation is freed exactly once by its owner; one that is not is a
leak, at the end of the function that loses it. -
A local marked
CERASTES_OWNINGowns its allocation, and a path that leaves its scope while it still holds the block is aleakat that path. -
A global marked
CERASTES_OWNINGowns its allocation across calls: a contract’scerastes_owns(g)carries the block with it, andmainmay end while it holds one. -
A member marked
CERASTES_OWNINGowns its allocation for its structure: whoever owns the structure owns the buffer, and a destructor frees both. -
A member marked
CERASTES_OWNINGandCERASTES_REF_TO_UNINITowns a buffer that may hold no value yet, so it can be handed to the function that writes it. -
A parameter marked
CERASTES_NOT_INVALIDATINGleaves the buffers it borrows in place, so a caller’s pointer into one stays valid across the call.
That is the end of the tutorial for this version. The
reference on allocated memory covers calloc, loops
over allocations, the attribute cerastes::owning
(owning locals,
owning globals and
owning fields in full, with the mapping of
P4417R0’s owners) and
vlibc’s own allocator (vlibc_malloc and
vlibc_free, a heap of its own whose blocks only vlibc_free frees), and
What is not supported yet says what is still
rejected: pointer arithmetic among it. Arrays, ranges and facts about every element
of an array are the next chapter, Arrays and quantifiers.