Skip to content

Commit

Permalink
cmake: run tests with Valgrind
Browse files Browse the repository at this point in the history
This patch enables running tests with Valgrind. There is a
`VALGRIND_OPTS` variable [1] that we can set -- it makes the usage of
Valgrind more flexible -- we can define any necessary flags in the
command line (not at the building stage). By default, the suppression
files are set to <src/lj.supp> (original suppression file in LuaJIT) and
an additional one <src/lj_extra.supp> (maintained by us).

Also, this patch disables the following tests when running with Valgrind
due to failures:

The <tarantool-tests/lj-512-profiler-hook-finalizers.test.lua> test is
disabled due to its time sensitivity (it is not run the expected amount
of time with Valgrind).

These tests from the tarantool-tests suite are disabled due to
tarantool/tarantool#10803:
  - lj-726-profile-flush-close.test.lua
  - profilers/gh-5688-tool-cli-flag.test.lua
  - profilers/gh-7264-add-proto-trace-sysprof-default.test.lua
  - profilers/misclib-sysprof-lapi.test.lua

Timed out due to running under Valgrind:
  - tarantool-c-tests/gh-8594-sysprof-ffunc-crash.c_test
  - tarantool-tests/gh-7745-oom-on-trace.test.lua
  - tarantool-tests/lj-1034-tabov-error-frame.test.lua

[1]: https://valgrind.org/docs/manual/manual-core.html#manual-core.defopts

Part of tarantool/tarantool#3705

Reviewed-by: Sergey Bronnikov <[email protected]>
Reviewed-by: Sergey Kaplun <[email protected]>
Signed-off-by: Sergey Kaplun <[email protected]>
(cherry picked from commit 70cb132)
  • Loading branch information
mandesero authored and Buristan committed Jan 9, 2025
1 parent 30baddc commit 520e6fc
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ endif()
# ASan enabled.
option(LUAJIT_USE_ASAN "Build LuaJIT with AddressSanitizer" OFF)
if(LUAJIT_USE_ASAN)
if(LUAJIT_USE_VALGRIND)
message(FATAL_ERROR
"AddressSanitizer and Valgrind cannot be used simultaneously."
)
endif()
if(NOT LUAJIT_USE_SYSMALLOC)
message(WARNING
"Unfortunately, internal LuaJIT memory allocator is not instrumented yet,"
Expand Down
19 changes: 19 additions & 0 deletions src/lj_extra.supp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Valgrind suppression file maintained by the Tarantool team.

# Missed from the original suppression file.
# Unaligned access to the string, see <lj_str.c> for the details.
{
Optimized string compare
Memcheck:Addr4
fun:lj_getu32
fun:str_fastcmp
}

# Missed from the original suppression file.
# `lj_str_cmp()` may read up to 3 extra bytes from the end of the
# string. It is OK due to the aligned allocations.
{
Optimized string compare
Memcheck:Cond
fun:lj_str_cmp
}
29 changes: 29 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ add_custom_target(${PROJECT_NAME}-lint DEPENDS
)

set(LUAJIT_TEST_COMMAND "${LUAJIT_TEST_BINARY} -e dofile[[${LUAJIT_TEST_INIT}]]")

if(LUAJIT_USE_VALGRIND)
find_program(VALGRIND valgrind)
if(NOT VALGRIND)
message(FATAL_ERROR "`valgrind' not found when LUAJIT_USE_VALGRIND option "
"is enabled.")
endif()

if(NOT LUAJIT_USE_SYSMALLOC)
message(WARNING
"LUAJIT_USE_SYSMALLOC option is mandatory for Valgrind's memcheck tool"
" on x64 and the only way to get useful results from it for all other"
" architectures.")
endif()

list(APPEND LUAJIT_TEST_VALGRIND_SUPP
--suppressions=${LUAJIT_SOURCE_DIR}/lj.supp
--suppressions=${LUAJIT_SOURCE_DIR}/lj_extra.supp
)
# When set to the default value (zero), the return value from
# Valgrind will always be the return value of the process being
# simulated. Set the exit code to non-zero to automatically
# detect failing tests.
set(LUAJIT_TEST_VALGRIND_COMMAND
${VALGRIND} --error-exitcode=1 ${LUAJIT_TEST_VALGRIND_SUPP})
set(LUAJIT_TEST_COMMAND
"${LUAJIT_TEST_VALGRIND_COMMAND} ${LUAJIT_TEST_COMMAND}")
endif()

separate_arguments(LUAJIT_TEST_COMMAND)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
Expand Down
9 changes: 8 additions & 1 deletion test/tarantool-c-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,17 @@ foreach(test_source ${tests})

# Generate CMake tests.
set(test_title "test/${TEST_SUITE_NAME}/${exe}${C_TEST_SUFFIX}")
set(test_command ${CMAKE_CURRENT_BINARY_DIR}/${exe}${C_TEST_SUFFIX})

if(LUAJIT_USE_VALGRIND)
set(test_command ${LUAJIT_TEST_VALGRIND_COMMAND} ${test_command})
endif()

add_test(NAME ${test_title}
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${exe}${C_TEST_SUFFIX}
COMMAND ${test_command}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

set_tests_properties(${test_title} PROPERTIES
LABELS ${TEST_SUITE_NAME}
DEPENDS tarantool-c-tests-deps
Expand Down
9 changes: 9 additions & 0 deletions test/tarantool-c-tests/gh-8594-sysprof-ffunc-crash.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
* * https://github.com/tarantool/tarantool/issues/9387
*/

#define UNUSED(x) ((void)(x))

#define MESSAGE "Canary is alive"
#define LUACALL "local a = tostring('" MESSAGE "') return a"

Expand Down Expand Up @@ -248,6 +250,12 @@ static int tracer(pid_t chpid)

static int test_tostring_call(void *ctx)
{
#if LUAJIT_USE_VALGRIND
UNUSED(ctx);
UNUSED(tracer);
UNUSED(tracee);
return skip("Disabled with Valgrind (Timeout)");
#else
pid_t chpid = fork();
switch(chpid) {
case -1:
Expand All @@ -264,6 +272,7 @@ static int test_tostring_call(void *ctx)
default:
return tracer(chpid);
}
#endif
}

#else /* LUAJIT_OS == LUAJIT_OS_LINUX && LUAJIT_TARGET == LUAJIT_ARCH_X64 */
Expand Down
6 changes: 6 additions & 0 deletions test/tarantool-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ if(LUAJIT_DISABLE_SYSPROF)
list(APPEND LUA_TEST_ENV_MORE LUAJIT_DISABLE_SYSPROF=1)
endif()

# Needed to skip some long tests or tests using signals.
# See also https://github.com/tarantool/tarantool/issues/10803.
if(LUAJIT_USE_VALGRIND)
list(APPEND LUA_TEST_ENV_MORE LUAJIT_TEST_USE_VALGRIND=1)
endif()

set(TEST_SUITE_NAME "tarantool-tests")

# XXX: The call produces both test and target
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
local tap = require('tap')
local profile = require('jit.profile')

local test = tap.test('lj-512-profiler-hook-finalizers')
local test = tap.test('lj-512-profiler-hook-finalizers'):skipcond({
-- Disable the test since it is time-sensitive.
['Disabled with Valgrind'] = os.getenv('LUAJIT_TEST_USE_VALGRIND'),
})
test:plan(1)

-- Sampling interval in ms.
Expand Down
5 changes: 4 additions & 1 deletion test/tarantool-tests/lj-726-profile-flush-close.test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local tap = require('tap')

local test = tap.test('lj-726-profile-flush-close')
local test = tap.test('lj-726-profile-flush-close'):skipcond({
-- See also https://github.com/tarantool/tarantool/issues/10803.
['Disabled due to #10803'] = os.getenv('LUAJIT_TEST_USE_VALGRIND'),
})
test:plan(1)

local TEST_FILE = 'lj-726-profile-flush-close.profile'
Expand Down
2 changes: 2 additions & 0 deletions test/tarantool-tests/profilers/gh-5688-tool-cli-flag.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ local test = tap.test('gh-5688-tool-cli-flag'):skipcond({
-- See also https://github.com/LuaJIT/LuaJIT/issues/606.
['Disabled due to LuaJIT/LuaJIT#606'] = os.getenv('LUAJIT_TABLE_BUMP'),
['Sysprof is disabled'] = os.getenv('LUAJIT_DISABLE_SYSPROF'),
-- See also https://github.com/tarantool/tarantool/issues/10803.
['Disabled due to #10803'] = os.getenv('LUAJIT_TEST_USE_VALGRIND'),
})

test:plan(3)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ local test = tap.test('gh-7264-add-proto-trace-sysprof-default'):skipcond({
-- See also https://github.com/LuaJIT/LuaJIT/issues/606.
['Disabled due to LuaJIT/LuaJIT#606'] = os.getenv('LUAJIT_TABLE_BUMP'),
['Sysprof is disabled'] = os.getenv('LUAJIT_DISABLE_SYSPROF'),
-- See also https://github.com/tarantool/tarantool/issues/10803.
['Disabled due to #10803'] = os.getenv('LUAJIT_TEST_USE_VALGRIND'),
})

test:plan(2)
Expand Down
2 changes: 2 additions & 0 deletions test/tarantool-tests/profilers/misclib-sysprof-lapi.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local test = tap.test("misc-sysprof-lapi"):skipcond({
-- See also https://github.com/LuaJIT/LuaJIT/issues/606.
["Disabled due to LuaJIT/LuaJIT#606"] = os.getenv("LUAJIT_TABLE_BUMP"),
["Sysprof is disabled"] = os.getenv('LUAJIT_DISABLE_SYSPROF'),
-- See also https://github.com/tarantool/tarantool/issues/10803.
["Disabled due to #10803"] = os.getenv("LUAJIT_TEST_USE_VALGRIND"),
})

test:plan(19)
Expand Down

0 comments on commit 520e6fc

Please sign in to comment.