Structures

A C structure is a group of objects, and cerastes treats it that way: each member is an object of its own, which a contract can own or read separately. This chapter updates a structure through a pointer, owning just the member it writes, and then passes structures by value.

Owning one member

#include <cerastes.h>
#include <limits.h>

struct point {
  int x;
  int y;
};

void move_right(struct point *p, int dx)
    CERASTES_REQUIRES(cerastes_owns(p->x) && 0 <= dx && p->x <= INT_MAX - dx)
    CERASTES_ENSURES(cerastes_owns(p->x) && p->x == cerastes_old(p->x) + dx)
{
  p->x += dx;
}

int use_move(void)
    CERASTES_ENSURES(r, r == 5)
{
  struct point pt = {.x = 2, .y = 7};
  move_right(&pt, 3);
  CERASTES_CHECK(pt.y == 7);
  return pt.x;
}
structures_point.c: verified (2 functions; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

move_right writes only p->x, so its contract owns only p->x. The caller keeps pt.y, and the check after the call shows what that buys: pt.y is still 7, because move_right had no right to change it.

The precondition also bounds dx and p->x so that the addition cannot overflow. It is written so that nothing in it overflows either: INT_MAX - dx is safe because dx is not negative.

cerastes_owns(*p) of a pointer to a structure owns every member at once. Owning exactly the members a function touches is more work to write, and it tells each caller more.

Writing what you do not own

Write the other member too, and the contract no longer covers the body:

#include <cerastes.h>
#include <limits.h>

struct point {
  int x;
  int y;
};

void move_right(struct point *p, int dx)
    CERASTES_REQUIRES(cerastes_owns(p->x) && 0 <= dx && p->x <= INT_MAX - dx)
    CERASTES_ENSURES(cerastes_owns(p->x) && p->x == cerastes_old(p->x) + dx)
{
  p->x += dx;
  p->y = 0;
}

int use_move(void)
    CERASTES_ENSURES(r, r == 5)
{
  struct point pt = {.x = 2, .y = 7};
  move_right(&pt, 3);
  CERASTES_CHECK(pt.y == 7);
  return pt.x;
}
structures_point_write_y_error.c:19: error: permission: writes `p->y` without owning it: the object it points to may have ended

As with any pointer, the fix is either in the contract, if the function is meant to change p->y, or in the body, if it is not.

Structures as values

A structure can also be passed and returned by value. It is then a value, not an object in memory, and its contract names its members directly:

#include <cerastes.h>

struct point {
  int x;
  int y;
};

struct point mirrored(struct point a)
    CERASTES_ENSURES(r, r.x == a.y && r.y == a.x)
{
  struct point m = {a.y, a.x};
  return m;
}

int use_mirrored(void)
    CERASTES_ENSURES(r, r == 3)
{
  struct point a = {3, 4};
  struct point b = mirrored(a);
  return b.y;
}
structures_copy.c: verified (2 functions; one translation unit: assumes that functions defined elsewhere satisfy the contracts declared here)

In the postcondition, r is the returned structure and r.x its member. The braced initializer gives every member a value; a structure local declared without an initializer has none, and reading one of its members first is an uninitialized error.

What you have learned

  • Each member of a structure is a separate object: a contract can own or read p->x alone, and the caller keeps the rest.

  • cerastes_owns(*p) of a structure owns all of its members.

  • Structures passed and returned by value are described member by member.

Where to read more