From: Remi Gacogne Date: Fri, 27 Sep 2024 09:29:24 +0000 (+0200) Subject: Fix fuzzing targets with meson X-Git-Tag: dnsdist-2.0.0-alpha1~128^2~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7579c48c8218d83d1fedd50b22feece54215c91e;p=thirdparty%2Fpdns.git Fix fuzzing targets with meson --- diff --git a/fuzzing/README.md b/fuzzing/README.md index ca26a8b100..0880ae35f3 100644 --- a/fuzzing/README.md +++ b/fuzzing/README.md @@ -25,12 +25,15 @@ By default the targets are linked against a standalone target, `standalone_fuzz_target_runner.cc`, which does no fuzzing but makes it easy to check a given test file, or just that the fuzzing targets can be built properly. -This behaviour can be changed via the `LIB_FUZZING_ENGINE` variable, for example -by setting it to `-lFuzzer`, building with clang by setting `CC=clang CXX=clang++` -before running the `configure` and adding `-fsanitize=fuzzer-no-link` to `CFLAGS` -and `CXXFLAGS`. Doing so instructs the compiler to instrument the code for -efficient fuzzing but not to link directly with `-lFuzzer`, which would make -the compilation tests done during the configure phase fail. +This behaviour can be changed via: +- either the `LIB_FUZZING_ENGINE` variable when building with `./configure` +- or the `-Dfuzzer_ldflags` option when building with `meson` + +For example, setting `LIB_FUZZING_ENGINE` to `-lFuzzer`, then building with clang +by setting `CC=clang CXX=clang++` before running the `configure`, and adding +`-fsanitize=fuzzer-no-link` to `CFLAGS` and `CXXFLAGS`, instructs the compiler +to instrument the code for efficient fuzzing but not to link directly with +`-lFuzzer`, which would make the compilation tests done during the configure phase fail. Sanitizers ---------- @@ -86,6 +89,15 @@ LIB_FUZZING_ENGINE="/usr/lib/clang/11.0.1/lib/linux/libclang_rt.fuzzer-x86_64.a" make -C pdns -j2 fuzz_targets ``` +or, if you are using `meson` to build the authoritative server instead of `./configure`: + +``` +env CC=clang CXX=clang++ \ + CFLAGS=-fsanitize=fuzzer-no-link CXXFLAGS=-fsanitize=fuzzer-no-link \ + meson setup .. -Dfuzz-targets=true -Dfuzzer_ldflags=/usr/lib/clang/18/lib/linux/libclang_rt.fuzzer-x86_64.a -Db_sanitize=address,undefined +ninja +``` + Now you're ready to run one of the fuzzing targets. First, copy the starting corpus: diff --git a/meson.build b/meson.build index c789bd3923..6a67f093ec 100644 --- a/meson.build +++ b/meson.build @@ -924,6 +924,15 @@ if get_option('unit-tests') endif if get_option('fuzz-targets') + fuzz_extra_sources = [] + fuzzer_ldflags = [] + # https://github.com/harfbuzz/harfbuzz/pull/2549/files + if get_option('fuzzer_ldflags') == '' + fuzz_extra_sources += src_dir / 'standalone_fuzz_target_runner.cc' + else + fuzzer_ldflags += get_option('fuzzer_ldflags') + endif + fuzz_targets = [ 'moadnsparser', 'packetcache', @@ -936,7 +945,9 @@ if get_option('fuzz-targets') foreach target: fuzz_targets source_file = src_dir / 'fuzz_' + target.underscorify() + '.cc' tools += { - 'fuzz-target-' + target: { 'main': source_file } + 'fuzz-target-' + target: { 'main': source_file, + 'link-args': fuzzer_ldflags, + 'files-extra': fuzz_extra_sources } } endforeach endif @@ -964,6 +975,7 @@ foreach tool, info: tools export_dynamic = 'export-dynamic' in info ? info['export-dynamic'] : false files_extra = 'files-extra' in info ? info['files-extra'] : [] deps_extra = 'deps-extra' in info ? info['deps-extra'] : [] + link_args = 'link-args' in info ? info['link-args'] : [] set_variable( var_name, @@ -973,6 +985,7 @@ foreach tool, info: tools config_h, files_extra, export_dynamic: export_dynamic, + link_args: link_args, dependencies: [ deps, libpdns_common, diff --git a/meson_options.txt b/meson_options.txt index a4869ce62b..614946726f 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -15,6 +15,7 @@ option('unit-tests', type: 'boolean', value: false, description: 'Build and run option('unit-tests-backends', type: 'boolean', value: false, description: 'Build and run backend unit tests') option('reproducible', type: 'boolean', value: false, description: 'Reproducible builds (for distro maintainers, makes debugging difficult)') option('fuzz-targets', type: 'boolean', value: false, description: 'Enable fuzzing targets') +option('fuzzer_ldflags', type: 'string', value: '', description: 'Linker flags used for the fuzzing targets (a path to the libFuzzer static library, for example)') option('verbose-logging', type: 'boolean', value: false, description: 'Enable verbose logging') option('experimental-pkcs11', type: 'feature', value: 'disabled', description: 'PKCS11 support') option('experimental-gss-tsig', type: 'feature', value: 'disabled', description: 'GSS-TSIG support') diff --git a/pdns/dnsdistdist/meson.build b/pdns/dnsdistdist/meson.build index d514d00555..75683f1ea4 100644 --- a/pdns/dnsdistdist/meson.build +++ b/pdns/dnsdistdist/meson.build @@ -421,7 +421,7 @@ if get_option('fuzz-targets') tools += { 'fuzz-target-dnsdistcache' : { 'main': src_dir / 'fuzz_dnsdistcache.cc', - 'link_flags': fuzzer_ldflags, + 'link-args': fuzzer_ldflags, 'files-extra': fuzz_extra_sources }, } @@ -429,7 +429,7 @@ if get_option('fuzz-targets') tools += { 'fuzz-target-xsk' : { 'main': src_dir / 'fuzz_xsk.cc', - 'link_flags': fuzzer_ldflags, + 'link-args': fuzzer_ldflags, 'files-extra': fuzz_extra_sources + [ src_dir / 'dnslabeltext.cc', src_dir / 'dnsname.cc',