Mate Toth-Pal | 5176cb8 | 2023-10-18 17:35:57 +0200 | [diff] [blame] | 1 | .. SPDX-License-Identifier: BSD-3-Clause |
| 2 | .. SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 3 | |
| 4 | **** |
| 5 | CBMC |
| 6 | **** |
| 7 | |
| 8 | CBMC is a Bounded Model Checker for C and C++ programs. For details see |
| 9 | `CBMC Home`_ |
| 10 | |
| 11 | CBMC in RMM |
| 12 | =========== |
| 13 | |
| 14 | CBMC needs to be run on a C program that has a single entry point. To test all |
| 15 | the RMM ABI commands, for each command a testbench file is created. These files |
| 16 | are generated by a script offline from the RMM MRS (Machine Readable |
| 17 | Specification), 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 | |
| 26 | These files contain asserts that correspond to |
| 27 | the Faliure and Success conditions defined in the RMM specification. To read on |
| 28 | further how such a file should be defined please refer to |
| 29 | `Writing a good proof`_ |
| 30 | |
| 31 | The recommended way for installing CBMC is via the pre-built package found at |
| 32 | the `github release page`_. The same page contains the instructions for |
| 33 | installing the different release packages. |
| 34 | |
| 35 | An 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 | |
| 41 | The invocation of CBMC tool is integrated in the RMM CMake system. CBMC analysis |
| 42 | can be run by passing specific targets (detailed later) to the build command. to |
| 43 | make the targets available the RMM build must be configured with |
| 44 | ``-DRMM_CONFIG=host_defcfg -DHOST_VARIANT=host_cbmc`` options. |
| 45 | |
| 46 | The results of a CBMC run are generated in the |
| 47 | ``${RMM_BUILD_DIR}/tools/cbmc/cbmc_${MODE}_results`` directory. There are 3 |
| 48 | files, ``${TESTBENCH_FILE_NAME}.${MODE}.[cmd|error|output]`` generated for each |
| 49 | RMM ABI under test, each one containing the CBMC command line, the CBMC |
| 50 | executable's output to the standard error, and the output to the standard out |
| 51 | respectively. There is also a single ``SUMMARY.${MODE}`` file is generated for |
| 52 | each build. |
| 53 | |
| 54 | For an example build command please refer to :ref:`RMM Build Examples` |
| 55 | |
| 56 | The CMake system provides different modes in which CBMC can be called, along |
| 57 | with their respective build targets. |
| 58 | |
| 59 | CBMC Assert |
Mate Toth-Pal | 0361dcb | 2023-11-29 10:20:12 +0100 | [diff] [blame] | 60 | ----------- |
Mate Toth-Pal | 5176cb8 | 2023-10-18 17:35:57 +0200 | [diff] [blame] | 61 | |
| 62 | In this mode CBMC is configured, so that it tries to find inputs that makes an |
| 63 | assertion in the code to fail. If there is such an input, then CBMC provides a |
| 64 | trace that leads to that assertion failure. |
| 65 | |
| 66 | To use this mode the target ``cbmc-assert`` must be passed to the build command. |
| 67 | |
| 68 | CBMC Analysis |
Mate Toth-Pal | 0361dcb | 2023-11-29 10:20:12 +0100 | [diff] [blame] | 69 | ------------- |
Mate Toth-Pal | 5176cb8 | 2023-10-18 17:35:57 +0200 | [diff] [blame] | 70 | |
| 71 | In this mode CBMC is configured to generate assertions for certain properties in |
| 72 | the code. The properties are selected so that for example no buffer overflows, |
| 73 | or arithmetic overflow errors can happen in the code. For more details please |
| 74 | refer to `Automatically Generating Properties`_. |
| 75 | Then CBMC is run in a configuration similar to the Assert mode, except that this |
| 76 | time traces are not generated. |
| 77 | |
| 78 | To use this mode the target ``cbmc-analysis`` must be passed to the build |
| 79 | command. |
| 80 | |
| 81 | CBMC Coverage |
Mate Toth-Pal | 0361dcb | 2023-11-29 10:20:12 +0100 | [diff] [blame] | 82 | ------------- |
Mate Toth-Pal | 5176cb8 | 2023-10-18 17:35:57 +0200 | [diff] [blame] | 83 | |
| 84 | This mode checks whether all the conditions for an ABI function are covered. |
| 85 | The pre and post conditions for the command are expressed as boolean values in |
| 86 | the testbench, and a ``__CPROVER_cover()`` macro is added for each condition |
| 87 | that is expressed with the pre and post conditions. CBMC is configured to try |
| 88 | to generate an input for each ``__CPROVER_cover()`` call that makes the code |
| 89 | reach that call. |
| 90 | |
| 91 | To use this mode the target ``cbmc-coverage`` must be passed to the build |
| 92 | command. |
| 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-Pal | 0361dcb | 2023-11-29 10:20:12 +0100 | [diff] [blame] | 99 | cbmc-viewer |
| 100 | =========== |
| 101 | |
| 102 | cbmc-viewer is a python package that can parse the XML output of CBMC. It |
| 103 | generates a html report that can be opened in a browser. The report contains a |
| 104 | collapsible representation of assert traces, and clickable links to the source |
| 105 | code locations associated with a specific trace item. |
| 106 | |
| 107 | The RMM cmake build system is capable of generating the cbmc-viewer report. If |
| 108 | the option ``-DRMM_CBMC_VIEWER_OUTPUT=ON`` is passed to the RMM Cmake |
| 109 | configuration command then the Cmake system calls cbmc-viewer and generates the |
| 110 | report under ``${RMM_BUILD_DIR}/tools/cbmc/cbmc_${MODE}_results/report`` |
| 111 | |
| 112 | Please note that the CMake build system currently only generates report for the |
| 113 | ``cbmc-assert`` target. The ``cbmc-coverage`` and ``cbmc-analysis`` targets |
| 114 | doesn'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 | |
| 122 | For further details and installation guide on cbmc-viewer please see the |
| 123 | `cbmc-viewer github page`_. |
| 124 | |
| 125 | CBMC proof debugger |
| 126 | =================== |
| 127 | |
| 128 | CBMC proof debugger is an extension to a popular code editor that can be used to |
| 129 | load the json summaries of a CBMC analysis that is generated by cbmc-viewer. |
| 130 | The trace then can be explored in the built in debugger of the editor as if |
| 131 | stepping through an actual code execution. |
| 132 | |
| 133 | For further details on installing and using the extension please see |
| 134 | `CBMC proof debugger in the editor's extensions page`_. |
| 135 | |
Mate Toth-Pal | 5176cb8 | 2023-10-18 17:35:57 +0200 | [diff] [blame] | 136 | ----- |
| 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-Pal | 0361dcb | 2023-11-29 10:20:12 +0100 | [diff] [blame] | 142 | .. _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 |