Skip to content

Commit 49219e9

Browse files
committed
cmake: integrate Lua API tests
The commit 8ad443d ("cmake: introduce a module to build fuzzing tests") has added tests for LuaJIT. These tests mostly written in C and tests LuaJIT via Lua C API. The patch integrates tests that written in Lua and tests LuaJIT via Lua API, see [1]. Depends on: 1. ligurio/lua-c-api-tests#121 2. tarantool#11056 NO_CHANGELOG=testing NO_DOC=testing
1 parent 526bc5a commit 49219e9

File tree

1 file changed

+89
-6
lines changed

1 file changed

+89
-6
lines changed

cmake/BuildLuaTests.cmake

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set(LUA_TESTS_PREFIX
77
# The test `luaL_loadbuffer_proto_test` is not enabled, because
88
# it is quite similar to `luaL_loadbuffer_fuzzer` located in
99
# test/fuzz/luaL_loadbuffer/.
10-
string(JOIN ";" LUA_TESTS
10+
string(JOIN ";" CAPI_TESTS
1111
ffi_cdef_proto_test
1212
lua_dump_test
1313
luaL_gsub_test
@@ -17,6 +17,21 @@ string(JOIN ";" LUA_TESTS
1717
torture_test
1818
)
1919

20+
string(JOIN ";" LAPI_TESTS
21+
bitop_arshift_test
22+
bitop_band_test
23+
bitop_bnot_test
24+
bitop_bor_test
25+
bitop_bswap_test
26+
bitop_bxor_test
27+
bitop_lshift_test
28+
bitop_rol_test
29+
bitop_ror_test
30+
bitop_rshift_test
31+
bitop_tobit_test
32+
bitop_tohex_test
33+
)
34+
2035
list(APPEND LUA_TESTS_CMAKE_FLAGS
2136
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
2237
"-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
@@ -31,6 +46,7 @@ list(APPEND LUA_TESTS_CMAKE_FLAGS
3146
"-DLUA_LIBRARIES=${LUAJIT_LIBRARIES}"
3247
"-DLUA_EXECUTABLE=${LUAJIT_TEST_BINARY}"
3348
"-DLUA_VERSION_STRING=\"tarantool\""
49+
"-DENABLE_LAPI_TESTS=ON"
3450
)
3551

3652
if (ENABLE_ASAN)
@@ -43,7 +59,7 @@ endif()
4359

4460
include(ExternalProject)
4561

46-
set(GIT_REF "68e62715310f197f489f03a33dce501c0a2e4450")
62+
set(GIT_REF "d4b91f6ac41ca28d19a4c7a44b85cd3c82f85479")
4763
# Git reference can be overridden with environment variable. It is
4864
# needed for checking build by project itself.
4965
if (DEFINED ENV{LUA_TESTS_GIT_REF})
@@ -64,10 +80,10 @@ ExternalProject_Add(${LUA_TESTS_TARGET_NAME}
6480
-B <BINARY_DIR>
6581
-G ${CMAKE_GENERATOR}
6682
"${LUA_TESTS_CMAKE_FLAGS}"
67-
BUILD_COMMAND cd <BINARY_DIR> && ${CMAKE_MAKE_PROGRAM} "${LUA_TESTS}"
83+
BUILD_COMMAND cd <BINARY_DIR> && ${CMAKE_MAKE_PROGRAM} "${CAPI_TESTS}"
6884
INSTALL_COMMAND ""
6985
CMAKE_GENERATOR ${CMAKE_GENERATOR}
70-
BUILD_BYPRODUCTS "${LUA_TESTS}"
86+
BUILD_BYPRODUCTS "${CAPI_TESTS}"
7187
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
7288
)
7389

@@ -81,7 +97,7 @@ add_dependencies(${LUA_TESTS_TARGET_NAME} libluajit_static)
8197
# environment variable `RUNS`.
8298
#
8399
# 1. https://github.com/ligurio/lua-c-api-tests/blob/ef74f0558bb913edef85e7a774fada4ecc82247b/tests/capi/CMakeLists.txt#L46-L58
84-
foreach(test ${LUA_TESTS})
100+
foreach(test ${CAPI_TESTS})
85101
set(test_title test/fuzz/${test})
86102
add_test(NAME ${test_title}
87103
COMMAND ${CMAKE_CTEST_COMMAND}
@@ -96,8 +112,75 @@ foreach(test ${LUA_TESTS})
96112
)
97113
endforeach()
98114

115+
set(DEFAULT_RUNS_NUMBER 5)
116+
117+
string(JOIN " " LIBFUZZER_OPTS
118+
-print_final_stats=1
119+
-print_pcs=1
120+
-reduce_inputs=1
121+
-reload=1
122+
-report_slow_units=5
123+
# Shell parameter expansion,
124+
# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
125+
-runs=\${RUNS:-${DEFAULT_RUNS_NUMBER}}
126+
)
127+
128+
make_lua_path(LUA_CPATH
129+
PATHS
130+
${PROJECT_BINARY_DIR}/luzer/build/luzer/?.so
131+
)
132+
133+
make_lua_path(LUA_PATH
134+
PATHS
135+
${PROJECT_BINARY_DIR}/luzer/?/init.lua
136+
${CMAKE_CURRENT_SOURCE_DIR}/?.lua
137+
)
138+
139+
list(APPEND LAPI_TEST_ENV
140+
"LUA_PATH=${LUA_PATH};"
141+
"LUA_CPATH=${LUA_CPATH};"
142+
"ASAN_OPTIONS=detect_odr_violation=0;"
143+
"LD_DYNAMIC_WEAK=1"
144+
)
145+
146+
function(create_lapi_test)
147+
cmake_parse_arguments(
148+
FUZZ
149+
""
150+
"FILENAME"
151+
""
152+
${ARGN}
153+
)
154+
get_filename_component(test_name ${FUZZ_FILENAME} NAME_WE)
155+
string(REPLACE "_test" "" test_prefix ${test_name})
156+
set(dict_path ${PROJECT_SOURCE_DIR}/corpus/${test_prefix}.dict)
157+
set(corpus_path ${PROJECT_SOURCE_DIR}/corpus/${test_prefix})
158+
if (EXISTS ${dict_path})
159+
set(LIBFUZZER_OPTS "${LIBFUZZER_OPTS} -dict=${dict_path}")
160+
endif ()
161+
if (EXISTS ${corpus_path})
162+
set(LIBFUZZER_OPTS "${LIBFUZZER_OPTS} ${corpus_path}")
163+
endif ()
164+
165+
add_test(NAME ${test_name}
166+
COMMAND ${BASH} -c "${LUAJIT_TEST_BINARY} ${FUZZ_FILENAME}
167+
${LIBFUZZER_OPTS}"
168+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
169+
)
170+
set_tests_properties(${test_name} PROPERTIES
171+
LABELS "lapi"
172+
ENVIRONMENT "${LAPI_TEST_ENV}"
173+
DEPENDS ${LUAJIT_TEST_BINARY}
174+
)
175+
endfunction()
176+
177+
foreach(test_name ${LAPI_TESTS})
178+
create_lapi_test(FILENAME ${LUA_TESTS_PREFIX}/src/tests/lapi/${test_name}.lua)
179+
endforeach()
180+
99181
unset(GIT_REF)
100-
unset(LUA_TESTS)
182+
unset(CAPI_TESTS)
183+
unset(LAPI_TESTS)
101184
unset(LUA_TESTS_CMAKE_FLAGS)
102185
unset(LUA_TESTS_PREFIX)
103186
unset(LUA_TESTS_TARGET_NAME)

0 commit comments

Comments
 (0)