2aacef
From c37de74b2eeefcbcd3f6d7f218e33cda3c3b5397 Mon Sep 17 00:00:00 2001
2aacef
From: =?UTF-8?q?Michal=20Koutn=C3=BD?= <mkoutny@suse.com>
2aacef
Date: Thu, 6 Oct 2022 19:06:08 +0200
2aacef
Subject: [PATCH] meson: Store fuzz tests in structured way
2aacef
2aacef
Put fuzzer tests into dictionary that maps `fuzzer->list of inputs`
2aacef
instead of the flat list.
2aacef
This is just refactoring with no intentional .
2aacef
2aacef
(cherry picked from commit 7db5761ddaa53eba197b5abc485e3290f47c661f)
2aacef
2aacef
Related: #2175619
2aacef
---
2aacef
 meson.build           | 69 ++++++++++++++++++-------------------------
2aacef
 test/fuzz/meson.build | 22 ++++++++++----
2aacef
 2 files changed, 45 insertions(+), 46 deletions(-)
2aacef
2aacef
diff --git a/meson.build b/meson.build
2aacef
index 35704947e3..dc7388cfe3 100644
2aacef
--- a/meson.build
2aacef
+++ b/meson.build
2aacef
@@ -4013,19 +4013,14 @@ foreach tuple : fuzzers
2aacef
                 build_by_default : fuzzer_build)
2aacef
         fuzzer_exes += exe
2aacef
 
2aacef
-        if want_tests != 'false'
2aacef
+        if want_tests != 'false' and name in fuzz_regression_tests
2aacef
                 # Run the fuzz regression tests without any sanitizers enabled.
2aacef
                 # Additional invocations with sanitizers may be added below.
2aacef
-                foreach p : fuzz_regression_tests
2aacef
-                        b = p.split('/')[-2]
2aacef
-                        c = p.split('/')[-1]
2aacef
-
2aacef
-                        if b == name
2aacef
-                                test('@0@_@1@'.format(b, c),
2aacef
-                                     exe,
2aacef
-                                     suite : 'fuzzers',
2aacef
-                                     args : [project_source_root / p])
2aacef
-                        endif
2aacef
+                foreach fuzz_in : fuzz_regression_tests[name]
2aacef
+                        test('@0@_@1@'.format(name, fuzz_in),
2aacef
+                             exe,
2aacef
+                             suite : 'fuzzers',
2aacef
+                             args : [project_source_root / fuzz_testsdir / name / fuzz_in])
2aacef
                 endforeach
2aacef
         endif
2aacef
 endforeach
2aacef
@@ -4128,45 +4123,39 @@ endif
2aacef
 ############################################################
2aacef
 
2aacef
 # Enable tests for all supported sanitizers
2aacef
-foreach tuple : sanitizers
2aacef
+foreach tuple : fuzz_sanitizers
2aacef
         sanitizer = tuple[0]
2aacef
         build = tuple[1]
2aacef
 
2aacef
         if cc.has_link_argument('-fsanitize=@0@'.format(sanitizer))
2aacef
-                prev = ''
2aacef
-                foreach p : fuzz_regression_tests
2aacef
-                        b = p.split('/')[-2]
2aacef
-                        c = p.split('/')[-1]
2aacef
-
2aacef
-                        name = '@0@:@1@'.format(b, sanitizer)
2aacef
-
2aacef
-                        if name != prev
2aacef
-                                if want_tests == 'false'
2aacef
-                                        message('Not compiling @0@ because tests is set to false'.format(name))
2aacef
-                                elif fuzz_tests
2aacef
-                                        exe = custom_target(
2aacef
-                                                name,
2aacef
-                                                output : name,
2aacef
-                                                depends : build,
2aacef
-                                                command : [ln, '-fs',
2aacef
-                                                           build.full_path() / b,
2aacef
-                                                           '@OUTPUT@'],
2aacef
-                                                build_by_default : true)
2aacef
-                                else
2aacef
-                                        message('Not compiling @0@ because fuzz-tests is set to false'.format(name))
2aacef
-                                endif
2aacef
+                foreach fuzzer, fuzz_ins : fuzz_regression_tests
2aacef
+                        name = '@0@:@1@'.format(fuzzer, sanitizer)
2aacef
+                        if want_tests == 'false'
2aacef
+                                message('Not compiling @0@ because tests is set to false'.format(name))
2aacef
+                                continue
2aacef
                         endif
2aacef
-                        prev = name
2aacef
-
2aacef
-                        if fuzz_tests
2aacef
-                                test('@0@_@1@_@2@'.format(b, c, sanitizer),
2aacef
+                        if not fuzz_tests
2aacef
+                                message('Not compiling @0@ because fuzz-tests is set to false'.format(name))
2aacef
+                                continue
2aacef
+                        endif
2aacef
+                        exe = custom_target(
2aacef
+                                name,
2aacef
+                                output : name,
2aacef
+                                depends : build,
2aacef
+                                command : [ln, '-fs',
2aacef
+                                           build.full_path() / fuzzer,
2aacef
+                                           '@OUTPUT@'],
2aacef
+                                build_by_default : true)
2aacef
+
2aacef
+                        foreach fuzz_in : fuzz_ins
2aacef
+                                test('@0@_@1@_@2@'.format(fuzzer, fuzz_in, sanitizer),
2aacef
                                      env,
2aacef
                                      suite : 'fuzz+san',
2aacef
                                      env : ['UBSAN_OPTIONS=print_stacktrace=1:print_summary=1:halt_on_error=1'],
2aacef
                                      timeout : 60,
2aacef
                                      args : [exe.full_path(),
2aacef
-                                             project_source_root / p])
2aacef
-                        endif
2aacef
+                                             project_source_root / fuzz_testsdir / fuzzer / fuzz_in])
2aacef
+                        endforeach
2aacef
                 endforeach
2aacef
         endif
2aacef
 endforeach
2aacef
diff --git a/test/fuzz/meson.build b/test/fuzz/meson.build
2aacef
index 80362d4154..82738fd1b7 100644
2aacef
--- a/test/fuzz/meson.build
2aacef
+++ b/test/fuzz/meson.build
2aacef
@@ -16,24 +16,34 @@ sanitize_address_undefined = custom_target(
2aacef
                    ' '.join(cc.cmd_array()),
2aacef
                    cxx_cmd])
2aacef
 
2aacef
-sanitizers = [['address,undefined', sanitize_address_undefined]]
2aacef
+fuzz_sanitizers = [['address,undefined', sanitize_address_undefined]]
2aacef
+fuzz_testsdir = 'test/fuzz'
2aacef
 
2aacef
 if git.found() and fs.exists(project_source_root / '.git')
2aacef
         out = run_command(env, '-u', 'GIT_WORK_TREE',
2aacef
                           git, '--git-dir=@0@/.git'.format(project_source_root),
2aacef
-                          'ls-files', ':/test/fuzz/*/*',
2aacef
+                          'ls-files', ':/@0@/*/*'.format(fuzz_testsdir),
2aacef
                           check: true)
2aacef
 else
2aacef
-        out = run_command(sh, '-c', 'cd "@0@"; echo test/fuzz/*/*'.format(project_source_root), check: true)
2aacef
+        out = run_command(sh, '-c', 'cd "@0@"; echo @1@/*/*'.format(project_source_root, fuzz_testsdir), check: true)
2aacef
 endif
2aacef
 
2aacef
-fuzz_regression_tests = []
2aacef
+fuzz_regression_tests = {}
2aacef
 foreach p : out.stdout().split()
2aacef
         # Remove the last entry which is ''.
2aacef
         #
2aacef
         # Also, backslashes get mangled, so skip test. See
2aacef
         # https://github.com/mesonbuild/meson/issues/1564.
2aacef
-        if not p.contains('\\')
2aacef
-                fuzz_regression_tests += p
2aacef
+        if p.contains('\\')
2aacef
+                continue
2aacef
         endif
2aacef
+        fuzzer = p.split('/')[-2]
2aacef
+        fuzz_in = p.split('/')[-1]
2aacef
+        if fuzzer not in fuzz_regression_tests
2aacef
+                fuzz_regression_tests += {fuzzer: []}
2aacef
+        endif
2aacef
+        # Meson parser provision for: fuzz_regression_tests[fuzzer] += [fuzz_in]
2aacef
+        l = fuzz_regression_tests[fuzzer]
2aacef
+        l += [fuzz_in]
2aacef
+        fuzz_regression_tests += {fuzzer: l}
2aacef
 endforeach