]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Fix fuzzing targets with meson
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 27 Sep 2024 09:29:24 +0000 (11:29 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 28 Jan 2025 13:37:19 +0000 (14:37 +0100)
fuzzing/README.md
meson.build
meson_options.txt
pdns/dnsdistdist/meson.build

index ca26a8b100336885e2859a477897683095fa1d1c..0880ae35f3702f8698250b825276b1e891a3f8f6 100644 (file)
@@ -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:
 
index c789bd39230f1ec802998dfe00181191436fb872..6a67f093ec0d2033cfd1d7f495a595e8c0a4f9ba 100644 (file)
@@ -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,
index a4869ce62b8b618f992e0bf0393ab42fcebcbf3e..614946726fd07c2b5e711c77ad81393b7e90dcf8 100644 (file)
@@ -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')
index d514d005554e44684c2a7b68453d16527eb84658..75683f1ea4516ab7612e8d7c8929c6a3383ad18f 100644 (file)
@@ -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',