CProver manual
|
This page describes how to use CBMC on static functions.
CBMC can check libraries and other codebases that expose several entry points. To do this, users typically write a harness that captures the entry points’ API contract, and that calls into the API with unconstrained values. The example below shows such a library and harness:
The harness sets up some assumptions and then calls into the API:
The following commands build and check this function:
For performance reasons, it might be desirable to analyze the API function independently of the static function. We can analyze the API function by “stubbing out” the static function, replacing it with a function that does nothing apart from asserting that its inputs satisfy the function’s contract. (“Stubbing out” a function is sometimes known as “modelling” or “abstracting it out”.) Add the following to harness.c
:
And build as follows, stripping the original static function out of its object file:
We should now also write a harness for private_function
. However, since that function is marked static
, it is not possible for functions in external files to call it. We can write and link a harness by stripping the static
attribute from private_function
using goto-cc’s --export-file-local-symbols
flag.
library_with_static.o
now contains an implementation of private_function()
with a mangled name. We can display the mangled name with goto-instrument:
When we write a harness for the static function, we ensure that we call the mangled name:
We can then link this harness to the object file with exported symbols and run CBMC as usual.
It is possible that CBMC will generate the same mangled name for two different static functions. This happens when the functions have the same name and are written in same-named files that live in different directories. In the following codebase, the two qux
functions will both have their names mangled to __CPROVER_file_local_b_c_qux
, and so any harness that requires both of those files will fail to link.
project | \_ foo | | | \_ a.c | \_ b.c <- contains static function "qux" | \_ bar | \_ c.c \_ b.c <- also contains static function "qux"
The solution is to use the --mangle-suffix
option to goto-cc. This allows you to specify a different suffix for name-mangling. By specifying a custom, different suffix for each of the two files, the mangled names are unique and the files can be successfully linked.
More examples are in regression/goto-cc-file-local
.
Last modified: 2022-09-29 15:10:20 -0400