From 184e1bd4d049e35afc701e4ffedc771a59a472b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 13 Feb 2020 19:16:17 +0100 Subject: [PATCH] build: Support running tests with address sanitizer Make possible to run tests with address sanitizer to quickly check for memory errors, although we have to disable the error exit code in case of leaks because we have some which are due to something else down in the stack (and LSAN suppression files doesn't allow to define the stack to ignore as we can in valgrind). However, we'd abort in case of memory errors anyways, so this still helps to prevent major problems, while still logging the leaks. In order to run pam module tests with ASAN we need to manually pass the library to LD_PRELOAD, as we do for the wrapper. --- meson.build | 2 ++ tests/LSAN-leaks-suppress.txt | 4 ++++ tests/meson.build | 34 ++++++++++++++++++++++++++++------ tests/pam/meson.build | 11 ++++++++--- 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 tests/LSAN-leaks-suppress.txt diff --git a/meson.build b/meson.build index b86f376..9ad0e43 100644 --- a/meson.build +++ b/meson.build @@ -177,5 +177,7 @@ output += ' PAM module: ' + pam_dep.found().to_string() output += ' Manuals: ' + get_option('man').to_string() output += ' GTK Doc: ' + get_option('gtk_doc').to_string() output += ' XML Linter ' + xmllint.found().to_string() +output += '\nTest setup:\n' +output += ' With address sanitizer: ' + address_sanitizer.to_string() message('\n'+'\n'.join(output)+'\n') diff --git a/tests/LSAN-leaks-suppress.txt b/tests/LSAN-leaks-suppress.txt new file mode 100644 index 0000000..a5fc940 --- /dev/null +++ b/tests/LSAN-leaks-suppress.txt @@ -0,0 +1,4 @@ +leak:initialize_device +leak:usbi_alloc_device +leak:libusb-1.0.so.* +leak:PyMem_RawMalloc diff --git a/tests/meson.build b/tests/meson.build index 610acbc..289958a 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -16,6 +16,8 @@ python_tests = [ # } ] +address_sanitizer = get_option('b_sanitize') == 'address' + tests = [ 'fprintd', 'test_fprintd_utils', @@ -81,16 +83,36 @@ foreach pt: python_tests endforeach endforeach +timeout_multiplier = 1 +test_envs = [ + 'G_SLICE=always-malloc', + 'MALLOC_CHECK_=2', +] + +if address_sanitizer + timeout_multiplier = 3 + test_envs += [ + 'ADDRESS_SANITIZER=true', + 'ASAN_OPTIONS=@0@'.format(':'.join([ + 'abort_on_error=true', + 'symbolize=true', + ])), + 'LSAN_OPTIONS=@0@'.format(':'.join([ + 'exitcode=0', + 'strict_string_checks=true', + 'suppressions=@0@'.format( + files(meson.current_source_dir() / 'LSAN-leaks-suppress.txt')[0]), + ])), + ] +endif + add_test_setup('default_setup', is_default: true, - env: [ - 'G_SLICE=always-malloc', - 'MALLOC_CHECK_=2', - 'MALLOC_PERTURB_=55', - ], + env: test_envs, + timeout_multiplier: timeout_multiplier ) -if find_program('valgrind', required: false).found() +if not address_sanitizer and find_program('valgrind', required: false).found() glib_share = glib_dep.get_pkgconfig_variable('prefix') / 'share' / glib_dep.name() glib_suppressions = glib_share + '/valgrind/glib.supp' add_test_setup('valgrind', diff --git a/tests/pam/meson.build b/tests/pam/meson.build index f738f00..15a9e57 100644 --- a/tests/pam/meson.build +++ b/tests/pam/meson.build @@ -4,11 +4,16 @@ tests = [ 'test_pam_fprintd', ] -preloaded_libs = [ - 'pam_wrapper' -] +preloaded_libs = [] pam_tests_ld_preload = [] +if address_sanitizer + # ASAN has to be the first in list + preloaded_libs += 'asan' +endif + +preloaded_libs += 'pam_wrapper' + foreach libname: preloaded_libs lib = run_command(meson.get_compiler('c'), '-print-file-name=lib@0@.so'.format(libname)