Skip to content

Commit e726f8c

Browse files
committed
proofs: initial commit with infrastructure
The patch adds an infrastructure for CBMC proofs and a single proof for the Lua C function `luaL_makeseed()`. Checking has failed due to arithmetic overflow on unsigned in `luai_makeseed()`. Currently, the PUC Rio Lua is supported, LuaJIT is not. Tested with CBMC 6.7.1
1 parent 854eba8 commit e726f8c

File tree

6 files changed

+154
-6
lines changed

6 files changed

+154
-6
lines changed

CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ option(ENABLE_BUILD_PROTOBUF "Enable building Protobuf library" ON)
2121
option(ENABLE_BONUS_TESTS "Enable bonus tests" OFF)
2222
option(ENABLE_INTERNAL_TESTS "Enable internal tests" OFF)
2323
option(ENABLE_LAPI_TESTS "Enable Lua API tests" OFF)
24+
option(ENABLE_PROOFS "Enable CBMC proofs" OFF)
2425

2526
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
2627
set(CMAKE_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_INCLUDE_PATH})
@@ -43,6 +44,13 @@ if (USE_LUAJIT AND NOT LUA_VERSION)
4344
set(LUA_VERSION "v2.1")
4445
endif()
4546

47+
if(ENABLE_PROOFS)
48+
# The latest PUC Rio Lua version checked by CBMC.
49+
if (USE_LUA)
50+
set(LUA_VERSION "a5522f06d2679b8f18534fd6a9968f7eb539dc31")
51+
endif()
52+
endif()
53+
4654
if (USE_LUA)
4755
include(BuildLua)
4856
build_lua(${LUA_VERSION})
@@ -73,6 +81,13 @@ if (ENABLE_LUAJIT_RANDOM_RA AND NOT IS_LUAJIT)
7381
message(FATAL_ERROR "Option ENABLE_LUAJIT_RANDOM_RA is LuaJIT-specific.")
7482
endif()
7583

84+
enable_testing()
85+
86+
if(ENABLE_PROOFS)
87+
add_subdirectory(proofs)
88+
return()
89+
endif()
90+
7691
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
7792
NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
7893
message(FATAL_ERROR
@@ -95,8 +110,6 @@ if(ENABLE_COV)
95110
include(CodeCoverage)
96111
endif()
97112

98-
enable_testing()
99-
100113
add_subdirectory(extra)
101114
add_subdirectory(libluamut)
102115
add_subdirectory(tests)

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
# lunapark
2121

22-
is a set of fuzzing tests for C implementations of Lua runtime (PUC Rio Lua and
23-
LuaJIT).
22+
is a set of fuzzing tests and CBMC proofs for C implementations
23+
of Lua runtimes (PUC Rio Lua and LuaJIT).
2424

2525
<details>
2626
<summary>If you are interested to know why it is called Lunapark</summary>
@@ -63,6 +63,7 @@ is LuaJIT-specific.
6363
library is used.
6464
- `ENABLE_INTERNAL_TESTS` enables internal tests.
6565
- `ENABLE_LAPI_TESTS` enables Lua API tests.
66+
- `ENABLE_PROOFS` enables building CBMC proofs instead tests.
6667

6768
### Running
6869

cmake/BuildLua.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ macro(build_lua LUA_VERSION)
1111
if (ENABLE_LUA_APICHECK)
1212
set(CFLAGS "${CFLAGS} -DLUA_USE_APICHECK")
1313
endif (ENABLE_LUA_APICHECK)
14-
set(CFLAGS "${CFLAGS} -fsanitize=fuzzer-no-link")
15-
set(LDFLAGS "-fsanitize=fuzzer-no-link")
14+
if(NOT ENABLE_PROOFS)
15+
set(CFLAGS "${CFLAGS} -fsanitize=fuzzer-no-link")
16+
set(LDFLAGS "-fsanitize=fuzzer-no-link")
17+
endif()
1618
if (OSS_FUZZ)
1719
set(LDFLAGS "${CFLAGS} ${LDFLAGS}")
1820
endif (OSS_FUZZ)

proofs/CMakeLists.txt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
find_program(CBMC_BIN cbmc)
2+
3+
list(APPEND GENERAL_CBMC_OPTS
4+
--bounds-check
5+
--conversion-check
6+
--div-by-zero-check
7+
--drop-unused-functions
8+
--float-overflow-check
9+
--flush
10+
--fpa
11+
--malloc-fail-null
12+
--malloc-may-fail
13+
--memory-cleanup-check
14+
--memory-leak-check
15+
--nan-check
16+
--object-bits 16
17+
--pointer-check
18+
--pointer-overflow-check
19+
--pointer-primitive-check
20+
--refine
21+
--signed-overflow-check
22+
--stack-trace
23+
--trace
24+
--undefined-shift-check
25+
--unsigned-overflow-check
26+
--verbosity 10
27+
--z3
28+
)
29+
30+
set(DEFAULT_TIMEOUT 30)
31+
32+
list(APPEND DEFAULT_LUA_CBMC_OPTS
33+
${GENERAL_CBMC_OPTS}
34+
)
35+
36+
list(APPEND DEFAULT_LUAJIT_CBMC_OPTS
37+
${GENERAL_CBMC_OPTS}
38+
)
39+
40+
if(USE_LUA)
41+
set(DEFAULT_CBMC_OPTS ${DEFAULT_LUA_CBMC_OPTS})
42+
elseif (USE_LUAJIT)
43+
set(DEFAULT_CBMC_OPTS ${DEFAULT_LUAJIT_CBMC_OPTS})
44+
endif()
45+
46+
set(PROOF_TARGETS "")
47+
48+
function(create_proof)
49+
cmake_parse_arguments(
50+
CBMC
51+
""
52+
"FILENAME"
53+
""
54+
${ARGN}
55+
)
56+
57+
get_filename_component(test_name ${CBMC_FILENAME} NAME_WE)
58+
message(STATUS "Building a proof ${test_name}")
59+
60+
# Build a CBMC proof binary.
61+
add_executable(${test_name} ${CBMC_FILENAME})
62+
target_link_libraries(${test_name} PUBLIC ${LUA_LIBRARIES})
63+
target_include_directories(${test_name} PRIVATE
64+
${LUA_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
65+
target_compile_options(${test_name} PRIVATE
66+
-Wall -Wextra -Wpedantic -Wno-unused-parameter -g)
67+
add_dependencies(${test_name} ${LUA_LIBRARIES})
68+
69+
# Create a CTest-based test.
70+
set(proof_name "${test_name}_proof")
71+
string(JOIN " " CBMC_COMMAND
72+
${CBMC_BIN}
73+
$<TARGET_FILE:${test_name}>
74+
${DEFAULT_CBMC_OPTS}
75+
# Notice that `--cover location` cannot be used together
76+
# with `--unwinding-assertions`.)
77+
--unwinding-assertions
78+
)
79+
add_test(NAME ${proof_name}
80+
COMMAND ${SHELL} -c "${CBMC_COMMAND}"
81+
)
82+
set_tests_properties(${proof_name} PROPERTIES
83+
LABELS proof
84+
TIMEOUT ${DEFAULT_TIMEOUT}
85+
)
86+
set(PROOF_TARGETS ${PROOF_TARGETS} ${test_name} PARENT_SCOPE)
87+
endfunction()
88+
89+
##################################################################
90+
# Lua C API functions
91+
##################################################################
92+
93+
create_proof(FILENAME luaL_makeseed.c)
94+
95+
add_custom_target(build_cbmc_proofs
96+
DEPENDS ${PROOF_TARGETS}
97+
COMMENT "Building CBMC proofs"
98+
)

proofs/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## CBMC proofs
2+
3+
### Usage
4+
5+
```
6+
nix-shell -p cbmc
7+
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
8+
cmake --build build --target build_proofs --parallel
9+
ctest --test-dir build --output-on-failure --timeout 15 --tests-regex luaL_makeseed --verbose
10+
```

proofs/luaL_makeseed.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* SPDX-License-Identifier: ISC
3+
*
4+
* Copyright 2023-2026, Sergey Bronnikov.
5+
*/
6+
7+
#include "lua.h"
8+
#include "lauxlib.h"
9+
10+
/* unsigned int luaL_makeseed (lua_State *L); */
11+
/* [-0, +0, -] */
12+
static void
13+
__luaL_makeseed(lua_State *L)
14+
{
15+
luaL_makeseed(L);
16+
}
17+
18+
int
19+
main()
20+
{
21+
__luaL_makeseed(NULL);
22+
23+
return 0;
24+
}

0 commit comments

Comments
 (0)