blob: 0564ef352682a75ab845fa249b7278ac213dba59 [file] [log] [blame]
Mate Toth-Pal5176cb82023-10-18 17:35:57 +02001.. SPDX-License-Identifier: BSD-3-Clause
2.. SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
3
4****
5CBMC
6****
7
8CBMC is a Bounded Model Checker for C and C++ programs. For details see
9`CBMC Home`_
10
11CBMC in RMM
12===========
13
14CBMC needs to be run on a C program that has a single entry point. To test all
15the RMM ABI commands, for each command a testbench file is created. These files
16are generated by a script offline from the RMM MRS (Machine Readable
17Specification), and committed to the RMM repository under the folder
18``tools/cbmc/testbenches``
19
20.. note::
21
22 Currently only a subset of the ABI calls have a testbench implemented. Also
23 there are errors reported by CBMC for some of the testbenches. Further
24 testbenches and fixes are expected to be added later.
25
26These files contain asserts that correspond to
27the Faliure and Success conditions defined in the RMM specification. To read on
28further how such a file should be defined please refer to
29`Writing a good proof`_
30
31The recommended way for installing CBMC is via the pre-built package found at
32the `github release page`_. The same page contains the instructions for
33installing the different release packages.
34
35An example install command for Ubuntu linux is
36
37.. code-block:: bash
38
39 dpkg -i ubuntu-20.04-cbmc-5.95.1-Linux.deb
40
41The invocation of CBMC tool is integrated in the RMM CMake system. CBMC analysis
42can be run by passing specific targets (detailed later) to the build command. to
43make the targets available the RMM build must be configured with
44``-DRMM_CONFIG=host_defcfg -DHOST_VARIANT=host_cbmc`` options.
45
46The results of a CBMC run are generated in the
47``${RMM_BUILD_DIR}/tools/cbmc/cbmc_${MODE}_results`` directory. There are 3
48files, ``${TESTBENCH_FILE_NAME}.${MODE}.[cmd|error|output]`` generated for each
49RMM ABI under test, each one containing the CBMC command line, the CBMC
50executable's output to the standard error, and the output to the standard out
51respectively. There is also a single ``SUMMARY.${MODE}`` file is generated for
52each build.
53
54For an example build command please refer to :ref:`RMM Build Examples`
55
56The CMake system provides different modes in which CBMC can be called, along
57with their respective build targets.
58
59CBMC Assert
Mate Toth-Pal0361dcb2023-11-29 10:20:12 +010060-----------
Mate Toth-Pal5176cb82023-10-18 17:35:57 +020061
62In this mode CBMC is configured, so that it tries to find inputs that makes an
63assertion in the code to fail. If there is such an input, then CBMC provides a
64trace that leads to that assertion failure.
65
66To use this mode the target ``cbmc-assert`` must be passed to the build command.
67
68CBMC Analysis
Mate Toth-Pal0361dcb2023-11-29 10:20:12 +010069-------------
Mate Toth-Pal5176cb82023-10-18 17:35:57 +020070
71In this mode CBMC is configured to generate assertions for certain properties in
72the code. The properties are selected so that for example no buffer overflows,
73or arithmetic overflow errors can happen in the code. For more details please
74refer to `Automatically Generating Properties`_.
75Then CBMC is run in a configuration similar to the Assert mode, except that this
76time traces are not generated.
77
78To use this mode the target ``cbmc-analysis`` must be passed to the build
79command.
80
81CBMC Coverage
Mate Toth-Pal0361dcb2023-11-29 10:20:12 +010082-------------
Mate Toth-Pal5176cb82023-10-18 17:35:57 +020083
84This mode checks whether all the conditions for an ABI function are covered.
85The pre and post conditions for the command are expressed as boolean values in
86the testbench, and a ``__CPROVER_cover()`` macro is added for each condition
87that is expressed with the pre and post conditions. CBMC is configured to try
88to generate an input for each ``__CPROVER_cover()`` call that makes the code
89reach that call.
90
91To use this mode the target ``cbmc-coverage`` must be passed to the build
92command.
93
94.. note::
95
96 For all the modes the summary files are committed in the source tree as
97 baseline in ``tools/cbmc/testbenches_results/BASELINE.${MODE}``.
98
Mate Toth-Pal0361dcb2023-11-29 10:20:12 +010099cbmc-viewer
100===========
101
102cbmc-viewer is a python package that can parse the XML output of CBMC. It
103generates a html report that can be opened in a browser. The report contains a
104collapsible representation of assert traces, and clickable links to the source
105code locations associated with a specific trace item.
106
107The RMM cmake build system is capable of generating the cbmc-viewer report. If
108the option ``-DRMM_CBMC_VIEWER_OUTPUT=ON`` is passed to the RMM Cmake
109configuration command then the Cmake system calls cbmc-viewer and generates the
110report under ``${RMM_BUILD_DIR}/tools/cbmc/cbmc_${MODE}_results/report``
111
112Please note that the CMake build system currently only generates report for the
113``cbmc-assert`` target. The ``cbmc-coverage`` and ``cbmc-analysis`` targets
114doesn't generate trace, so generating a report wouldn't be useful.
115
116``cbmc-viewer`` can be installed using the following command:
117
118.. code-block:: bash
119
120 python3 -m pip install cbmc-viewer
121
122For further details and installation guide on cbmc-viewer please see the
123`cbmc-viewer github page`_.
124
125CBMC proof debugger
126===================
127
128CBMC proof debugger is an extension to a popular code editor that can be used to
129load the json summaries of a CBMC analysis that is generated by cbmc-viewer.
130The trace then can be explored in the built in debugger of the editor as if
131stepping through an actual code execution.
132
133For further details on installing and using the extension please see
134`CBMC proof debugger in the editor's extensions page`_.
135
Mate Toth-Pal5176cb82023-10-18 17:35:57 +0200136-----
137
138.. _CBMC Home: https://www.cprover.org/cbmc/
139.. _Writing a good proof: https://model-checking.github.io/cbmc-training/management/Write-a-good-proof.html
140.. _github release page: https://github.com/diffblue/cbmc/releases
141.. _Automatically Generating Properties: https://www.cprover.org/cprover-manual/properties/
Mate Toth-Pal0361dcb2023-11-29 10:20:12 +0100142.. _cbmc-viewer github page: https://github.com/model-checking/cbmc-viewer
143.. _CBMC proof debugger in the editor's extensions page: https://marketplace.visualstudio.com/items?itemName=model-checking.cbmc-proof-debugger