Skip to content

Commit 532c114

Browse files
committed
proofs: support code coverage using cbmc-viewer
A coverage report summarizing what lines of source code were exercised by CBMC. Tested with cbmc-viewer 3.11.1
1 parent e726f8c commit 532c114

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

proofs/CMakeLists.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
find_program(CBMC_BIN cbmc)
2+
find_program(CBMC_VIEWER_BIN cbmc-viewer)
23

34
list(APPEND GENERAL_CBMC_OPTS
45
--bounds-check
@@ -28,6 +29,9 @@ list(APPEND GENERAL_CBMC_OPTS
2829
)
2930

3031
set(DEFAULT_TIMEOUT 30)
32+
set(CBMC_REPORT_PATH ${CMAKE_CURRENT_BINARY_DIR}/report)
33+
34+
file(MAKE_DIRECTORY ${CBMC_REPORT_PATH})
3135

3236
list(APPEND DEFAULT_LUA_CBMC_OPTS
3337
${GENERAL_CBMC_OPTS}
@@ -44,6 +48,7 @@ elseif (USE_LUAJIT)
4448
endif()
4549

4650
set(PROOF_TARGETS "")
51+
set(PROOF_REPORTS "")
4752

4853
function(create_proof)
4954
cmake_parse_arguments(
@@ -84,6 +89,56 @@ function(create_proof)
8489
TIMEOUT ${DEFAULT_TIMEOUT}
8590
)
8691
set(PROOF_TARGETS ${PROOF_TARGETS} ${test_name} PARENT_SCOPE)
92+
set(PROOF_REPORTS ${PROOF_REPORTS} ${test_name}-report PARENT_SCOPE)
93+
94+
set(PROPERTY_REPORT ${test_name}_property.json)
95+
set(COVERAGE_REPORT ${test_name}_coverage.json)
96+
set(RESULT_REPORT ${test_name}_result.json)
97+
98+
add_custom_target(${RESULT_REPORT}
99+
COMMAND ${CBMC_BIN} $<TARGET_FILE:${test_name}>
100+
${DEFAULT_CBMC_OPTS} --unwinding-assertions --trace --json-ui > ${RESULT_REPORT} || (exit 0)
101+
DEPENDS ${test_name}
102+
BYPRODUCTS ${RESULT_REPORT}
103+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
104+
COMMENT "Build ${RESULT_REPORT}"
105+
)
106+
107+
add_custom_target(${PROPERTY_REPORT}
108+
COMMAND ${CBMC_BIN} $<TARGET_FILE:${test_name}>
109+
${DEFAULT_CBMC_OPTS} --unwinding-assertions --show-properties --json-ui > ${PROPERTY_REPORT}
110+
DEPENDS ${test_name}
111+
BYPRODUCTS ${PROPERTY_REPORT}
112+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
113+
COMMENT "Build ${PROPERTY_REPORT}"
114+
)
115+
116+
add_custom_target(${COVERAGE_REPORT}
117+
COMMAND ${CBMC_BIN} $<TARGET_FILE:${test_name}>
118+
${DEFAULT_CBMC_OPTS} --cover location --json-ui > ${COVERAGE_REPORT}
119+
DEPENDS ${test_name}
120+
BYPRODUCTS ${COVERAGE_REPORT}
121+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
122+
COMMENT "Build ${COVERAGE_REPORT}"
123+
)
124+
125+
list(APPEND CBMC_VIEWER_COMMAND
126+
${CBMC_VIEWER_BIN}
127+
--goto $<TARGET_FILE:${test_name}>
128+
--result ${RESULT_REPORT}
129+
--coverage ${COVERAGE_REPORT}
130+
--property ${PROPERTY_REPORT}
131+
--reportdir ${CBMC_REPORT_PATH}/${test_name}
132+
--srcdir ${LUA_INCLUDE_DIR}
133+
--wkdir ${LUA_INCLUDE_DIR}
134+
)
135+
136+
add_custom_target(${test_name}-report
137+
COMMAND ${CBMC_VIEWER_COMMAND}
138+
DEPENDS ${RESULT_REPORT} ${PROPERTY_REPORT} ${COVERAGE_REPORT}
139+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
140+
COMMENT "Build CBMC code coverage report for ${test_name}"
141+
)
87142
endfunction()
88143

89144
##################################################################
@@ -96,3 +151,7 @@ add_custom_target(build_cbmc_proofs
96151
DEPENDS ${PROOF_TARGETS}
97152
COMMENT "Building CBMC proofs"
98153
)
154+
add_custom_target(build_cbmc_reports
155+
DEPENDS ${PROOF_REPORTS}
156+
COMMENT "Building CBMC reports"
157+
)

proofs/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
### Usage
44

55
```
6+
python -m venv venv_cbmc_viewer
7+
source venv_cbmc_viewer/bin/activate
8+
python3 -m pip install cbmc-viewer 3.11.1
9+
sudo apt install universal-ctags
10+
611
nix-shell -p cbmc
712
cmake -S . -B build -DUSE_LUA=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=goto-cc -DENABLE_PROOFS=ON -DENABLE_LUA_APICHECK=ON -DENABLE_LUA_ASSERT=ON
813
cmake --build build --target build_proofs --parallel
914
ctest --test-dir build --output-on-failure --timeout 15 --tests-regex luaL_makeseed --verbose
15+
cmake --build build -t luaL_makeseed-report --parallel
1016
```

0 commit comments

Comments
 (0)