Skip to content

Commit b68dfb9

Browse files
committed
Hook up oss-fuzz test cases as tests
This is a bit painful because a separate build of systemd is necessary. The tests are guarded by tests!=false and slow-tests==true. Running them is not slow, but compilation certainly is. If this proves unwieldy, we can add a separate option controlling those builds later. The build for each sanitizer has its own directory, and we build all fuzzer tests there, and then pull them out one-by-one by linking into the target position as necessary. It would be nicer to just build the desired fuzzer, but we need to build the whole nested build as one unit. [I also tried making systemd and nested meson subproject. This would work nicely, but meson does not allow that because the nested target names are the same as the outer project names. If that is ever fixed, that would be the way to go.] v2: - make sure things still work if memory sanitizer is not available v3: - switch to syntax which works with meson 0.42.1 found in Ubuntu
1 parent d385cd0 commit b68dfb9

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

meson.build

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ substs.set('RC_LOCAL_SCRIPT_PATH_STOP', get_option('halt-l
259259
cc = meson.get_compiler('c')
260260
pkgconfig = import('pkgconfig')
261261
check_compilation_sh = find_program('tools/meson-check-compilation.sh')
262+
meson_build_sh = find_program('tools/meson-build.sh')
262263

263264
if get_option('tests') != 'false'
264265
cxx = find_program('c++', required : false)
@@ -385,6 +386,20 @@ foreach arg : ['-Wl,-z,relro',
385386
endif
386387
endforeach
387388

389+
# Check if various sanitizers are supported
390+
sanitizers = []
391+
foreach arg : ['address']
392+
393+
have = run_command(check_compilation_sh,
394+
cc.cmd_array(), '-x', 'c',
395+
'-fsanitize=@0@'.format(arg),
396+
'-include', link_test_c).returncode() == 0
397+
message('@0@ sanitizer supported: @1@'.format(arg, have ? 'yes' : 'no'))
398+
if have
399+
sanitizers += arg
400+
endif
401+
endforeach
402+
388403
if get_option('buildtype') != 'debug'
389404
foreach arg : ['-ffunction-sections',
390405
'-fdata-sections']
@@ -517,6 +532,7 @@ awk = find_program('awk')
517532
m4 = find_program('m4')
518533
stat = find_program('stat')
519534
git = find_program('git', required : false)
535+
env = find_program('env')
520536

521537
meson_make_symlink = meson.source_root() + '/tools/meson-make-symlink.sh'
522538
mkdir_p = 'mkdir -p $DESTDIR/@0@'
@@ -1201,10 +1217,11 @@ endforeach
12011217

12021218
want_tests = get_option('tests')
12031219
install_tests = get_option('install-tests')
1220+
slow_tests = get_option('slow-tests')
12041221
tests = []
12051222
fuzzers = []
12061223

1207-
conf.set10('SYSTEMD_SLOW_TESTS_DEFAULT', get_option('slow-tests'))
1224+
conf.set10('SYSTEMD_SLOW_TESTS_DEFAULT', slow_tests)
12081225

12091226
#####################################################################
12101227

@@ -2572,6 +2589,50 @@ endforeach
25722589

25732590
############################################################
25742591

2592+
prev = ''
2593+
foreach p : fuzz_regression_tests
2594+
a = p.split('/')[-3]
2595+
b = p.split('/')[-2]
2596+
c = p.split('/')[-1]
2597+
2598+
if a == 'address'
2599+
build = sanitize_address
2600+
else
2601+
error('unknown sanitizer @0@'.format(a))
2602+
endif
2603+
2604+
name = '@1@:@0@'.format(a, b)
2605+
2606+
if name != prev
2607+
if want_tests == 'false'
2608+
message('Not compiling @0@ because tests is set to false'.format(name))
2609+
elif not sanitizers.contains(a)
2610+
message('Not compiling @0@ because @1@ sanitizer is not available'.format(name, a))
2611+
elif slow_tests
2612+
exe = custom_target(
2613+
name,
2614+
output : name,
2615+
depends : build,
2616+
command : [env, 'ln', '-fs',
2617+
join_paths(build.full_path(), b),
2618+
'@OUTPUT@'],
2619+
build_by_default : true)
2620+
else
2621+
message('Not compiling @0@ because slow-tests is set to false'.format(name))
2622+
endif
2623+
endif
2624+
prev = name
2625+
2626+
if want_tests != 'false' and slow_tests
2627+
test(c, env, args : [exe.full_path(),
2628+
join_paths(meson.source_root(),
2629+
'test/fuzz-regressions',
2630+
p)])
2631+
endif
2632+
endforeach
2633+
2634+
############################################################
2635+
25752636
if git.found()
25762637
all_files = run_command(
25772638
git,

test/fuzz-regressions/meson.build

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# SPDX-License-Identifier: LGPL-2.1+
2+
#
3+
# Copyright 2018 Zbigniew Jędrzejewski-Szmek
4+
#
5+
# systemd is free software; you can redistribute it and/or modify it
6+
# under the terms of the GNU Lesser General Public License as published by
7+
# the Free Software Foundation; either version 2.1 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# systemd is distributed in the hope that it will be useful, but
11+
# WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
# Lesser General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Lesser General Public License
16+
# along with systemd; If not, see <http://www.gnu.org/licenses/>.
17+
18+
sanitize_address = custom_target(
19+
'sanitize-address-fuzzers',
20+
output : 'sanitize-address-fuzzers',
21+
command : [meson_build_sh,
22+
meson.source_root(),
23+
'@OUTPUT@',
24+
'fuzzers',
25+
'-Db_lundef=false -Db_sanitize=address'])
26+
27+
fuzz_regression_tests = '''
28+
address/fuzz-dns-packet/oss-fuzz-5465
29+
address/fuzz-dns-packet/issue-7888
30+
'''.split()

test/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,5 @@ if conf.get('ENABLE_HWDB') == 1
228228
hwdb_test_sh,
229229
timeout : 90)
230230
endif
231+
232+
subdir('fuzz-regressions')

tools/meson-build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
set -eux
3+
4+
src="$1"
5+
dst="$2"
6+
target="$3"
7+
options="$4"
8+
9+
[ -d "$dst" ] || meson "$src" "$dst" $options
10+
ninja -C "$dst" "$target"

0 commit comments

Comments
 (0)