2020-11-09 07:23:58 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2018-01-19 09:54:30 +03:00
2022-10-06 17:48:01 +03:00
generate_directives_py = find_program ( 'generate-directives.py' )
2023-06-26 19:07:25 +03:00
fuzz_regression_tests = { }
2022-10-06 17:48:01 +03:00
2023-06-26 19:07:25 +03:00
directives = [ [ 'fuzz-network-parser' , 'directives.network' , networkd_network_gperf_gperf ] ,
[ 'fuzz-netdev-parser' , 'directives.netdev' , networkd_netdev_gperf_gperf ] ,
[ 'fuzz-link-parser' , 'directives.link' , udev_link_gperf_gperf ] ,
2022-10-06 17:48:01 +03:00
]
foreach tuple : directives
2023-06-26 19:07:25 +03:00
directive = custom_target (
tuple [ 1 ] ,
output : tuple [ 1 ] ,
command : [ generate_directives_py , tuple [ 2 ] ] ,
2022-10-06 17:48:01 +03:00
capture : true )
2023-06-26 19:07:25 +03:00
dict = { 'directives' : [ directive ] }
fuzz_regression_tests + = { tuple [ 0 ] : dict }
2022-10-06 17:48:01 +03:00
endforeach
2023-06-26 19:07:25 +03:00
unit_directives = [ ]
2022-10-06 17:48:01 +03:00
foreach section : [ 'Automount' , 'Mount' , 'Path' , 'Scope' , 'Service' , 'Slice' , 'Socket' , 'Swap' , 'Timer' ]
unit_type = section . to_lower ( )
sec_rx = section == 'Service' ? '(Service|Unit|Install)' : section
2023-06-26 19:07:25 +03:00
name = 'directives.@0@' . format ( unit_type )
unit_directives + = custom_target (
name ,
output : name ,
2022-10-06 17:48:01 +03:00
command : [ generate_directives_py , load_fragment_gperf_gperf , sec_rx , unit_type ] ,
capture : true )
endforeach
2023-06-26 19:07:25 +03:00
dict = { 'directives' : unit_directives }
fuzz_regression_tests + = { 'fuzz-unit-file' : dict }
2022-10-06 17:48:01 +03:00
############################################################
2023-08-10 19:11:25 +03:00
# TODO: Use native string formatting with meson >= 1.3.0
if get_option ( 'auto_features' ) . enabled ( )
sanitize_auto_features = 'enabled'
elif get_option ( 'auto_features' ) . disabled ( )
sanitize_auto_features = 'disabled'
else
sanitize_auto_features = 'auto'
endif
2023-11-15 13:41:45 +03:00
fuzz_c_args = get_option ( 'c_args' )
2023-11-15 16:47:17 +03:00
fuzz_cpp_args = cxx_cmd != '' ? get_option ( 'cpp_args' ) : [ ]
2023-11-15 13:41:45 +03:00
2020-05-20 12:33:12 +03:00
sanitize_address_undefined = custom_target (
'sanitize-address-undefined-fuzzers' ,
output : 'sanitize-address-undefined-fuzzers' ,
2018-01-19 09:54:30 +03:00
command : [ meson_build_sh ,
2019-02-27 19:19:07 +03:00
project_source_root ,
2018-01-19 09:54:30 +03:00
'@OUTPUT@' ,
'fuzzers' ,
2023-11-15 13:41:45 +03:00
' ' . join ( fuzz_c_args + '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION' ) ,
' ' . join ( fuzz_cpp_args + '-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION' ) ,
2023-11-10 18:38:01 +03:00
'-Dfuzz-tests=true -Db_lundef=false -Db_sanitize=address,undefined --optimization=@0@ @1@ --auto-features=@2@' . format (
2022-01-04 14:51:44 +03:00
get_option ( 'optimization' ) ,
2022-02-03 05:22:53 +03:00
get_option ( 'werror' ) ? '--werror' : '' ,
2023-08-10 19:11:25 +03:00
sanitize_auto_features
2022-01-04 14:51:44 +03:00
) ,
2018-10-03 22:52:44 +03:00
' ' . join ( cc . cmd_array ( ) ) ,
2018-10-10 12:50:57 +03:00
cxx_cmd ] )
2018-01-19 09:54:30 +03:00
2022-10-06 20:06:08 +03:00
fuzz_sanitizers = [ [ 'address,undefined' , sanitize_address_undefined ] ]
fuzz_testsdir = 'test/fuzz'
2018-03-14 16:27:04 +03:00
2023-03-02 00:54:06 +03:00
if git . found ( ) and fs . is_dir ( project_source_root / '.git' )
2021-05-14 15:16:17 +03:00
out = run_command ( env , '-u' , 'GIT_WORK_TREE' ,
git , '--git-dir=@0@/.git' . format ( project_source_root ) ,
2022-10-06 20:06:08 +03:00
'ls-files' , ':/@0@/*/*' . format ( fuzz_testsdir ) ,
2022-01-11 12:56:22 +03:00
check : true )
2018-08-10 18:15:05 +03:00
else
2022-10-06 20:06:08 +03:00
out = run_command ( sh , '-c' , 'cd "@0@"; echo @1@/*/*' . format ( project_source_root , fuzz_testsdir ) , check : true )
2018-08-10 18:15:05 +03:00
endif
2022-10-06 17:48:01 +03:00
# Add crafted fuzz inputs we have in the repo
2018-08-10 18:15:05 +03:00
foreach p : out . stdout ( ) . split ( )
# Remove the last entry which is ''.
#
# Also, backslashes get mangled, so skip test. See
# https://github.com/mesonbuild/meson/issues/1564.
2022-10-06 20:06:08 +03:00
if p . contains ( '\\' )
continue
2018-08-10 18:15:05 +03:00
endif
2023-06-14 17:05:52 +03:00
fuzzer = fs . name ( fs . parent ( p ) )
fuzz_in = fs . name ( p )
2023-06-26 19:07:25 +03:00
dict = fuzz_regression_tests . get ( fuzzer , { } )
dict + = { 'files' : dict . get ( 'files' , [ ] ) + files ( fuzzer / fuzz_in ) }
fuzz_regression_tests + = { fuzzer : dict }
2018-08-10 18:15:05 +03:00
endforeach