@@ -90,6 +90,63 @@ point into another memory object (such as could happen when running on a real
9090machine). To verify that pointers stay within the bounds of their pointees, the
9191CBMC option `--pointer-overflow-check` can be used.
9292
93+ ### Malloc modelling
94+
95+ CBMC ships a model of `malloc` that seeks to emulate the behaviour of the C
96+ standard library. This model is configurable to suit the assumptions the
97+ software under scrutiny may be making. One common assumption, matched by CBMC's
98+ default configuration, is that dynamic memory allocation always succeeds and
99+ `malloc` never returns a `NULL` pointer. Code making such an assumption will
100+ look as follows:
101+
102+ ```C
103+ int *p = malloc(sizeof(int));
104+ *p = 42; // unconditional dereference, no check for p being NULL
105+ ```
106+
107+ This extends to the case of ` malloc(0) ` , and CBMC returns a valid pointer to an
108+ object. The size of that object is zero, implying that any attempt to read from
109+ or write to this object will result in an out-of-bounds access. The ensuing
110+ undefined behaviour can be detected by running CBMC with ` --pointer-check ` .
111+
112+ In an actual execution, however, memory allocation may fail for a number of
113+ reasons and ` malloc ` would return a NULL pointer. CBMC's model can, therefore,
114+ also be configured to fail allocating memory when the requested allocation size
115+ is larger than representable under CBMC's object-offset model (as described in
116+ [ Memory and pointers in CBMC] ( #memory-and-pointers-in-cbmc ) ), or even
117+ non-deterministically fail (for any size). Any such failure can either result in
118+ calls to ` malloc ` returning ` NULL ` , or reporting such a call as a failed
119+ property. The following command line options facilitate the above failure
120+ configurations:
121+
122+ | Flag | Check |
123+ | ------------------------| ---------------------------------------------------|
124+ | ` --malloc-fail-null ` | return NULL when emulating an allocation failure |
125+ | ` --malloc-may-fail ` | non-deterministically fail to allocate |
126+
127+ Note that the use of ` --malloc-may-fail ` also requires ` --malloc-fail-null ` . The
128+ following code example demonstrates the effect of these options:
129+
130+ ``` C
131+ int error = 0 ;
132+ int *p = malloc(sizeof (int ));
133+ if (p != NULL )
134+ *p = 42 ;
135+ else
136+ error = 1 ;
137+ ```
138+
139+ Under CBMC's default model of ` malloc ` , the ` else ` branch is unreachable.
140+ When running CBMC with ` --malloc-fail-null --malloc-may-fail ` , ` p ` would
141+ non-deterministically be set to ` NULL ` , making all branches in the above code
142+ reachable.
143+
144+ These malloc failure options need to be set when the C library model is added to
145+ the program. Typically this is upon invoking CBMC, but if the user has chosen to
146+ do so via \ref goto-instrument (using ` goto-instrument --add-library ` ), then the
147+ malloc failure mode needs to be specified with that ` goto-instrument `
148+ invocation, i.e., as an option to ` goto-instrument ` .
149+
93150## Uninitialized pointers
94151
95152In verification tools, uninitialized variables are typically treated as having a
0 commit comments