aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 1 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
aizatsky | 7dfb5734 | 2015-12-21 04:02:52 | [diff] [blame] | 5 | # Defines fuzzer_test. |
| 6 | # |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 7 | import("//build/config/features.gni") |
| 8 | import("//build/config/sanitizers/sanitizers.gni") |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 9 | import("//testing/test.gni") |
| 10 | |
| 11 | # fuzzer_test is used to define individual libfuzzer tests. |
| 12 | # |
| 13 | # Supported attributes: |
| 14 | # - (required) sources - fuzzer test source files |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 15 | # - deps - test dependencies |
| 16 | # - additional_configs - additional configs to be used for compilation |
| 17 | # - dict - a dictionary file for the fuzzer. |
mmoroz | 344a75ae | 2016-03-02 16:57:17 | [diff] [blame] | 18 | # - libfuzzer_options - options for the fuzzer (e.g. -max_len or -timeout). |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 19 | # - seed_corpus - a directory with seed corpus. |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 20 | # |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 21 | # If use_libfuzzer gn flag is defined, then proper fuzzer would be build. |
| 22 | # Without use_libfuzzer a unit-test style binary would be built on linux |
| 23 | # and the whole target is a no-op otherwise. |
| 24 | # |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 25 | # The template wraps test() target with appropriate dependencies. |
mmoroz | 344a75ae | 2016-03-02 16:57:17 | [diff] [blame] | 26 | # If any test run-time options are present (dict or libfuzzer_options), then a |
| 27 | # config (.options file) file would be generated or modified in root output |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 28 | # dir (next to test). |
| 29 | template("fuzzer_test") { |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 30 | if (!disable_libfuzzer && (use_libfuzzer || use_drfuzz || is_linux)) { |
| 31 | assert(defined(invoker.sources), "Need sources in $target_name.") |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 32 | |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 33 | test_deps = [ "//testing/libfuzzer:libfuzzer_main" ] |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 34 | |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 35 | if (defined(invoker.deps)) { |
| 36 | test_deps += invoker.deps |
| 37 | } |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 38 | |
mmoroz | 344a75ae | 2016-03-02 16:57:17 | [diff] [blame] | 39 | if (defined(invoker.libfuzzer_options) && !defined(invoker.dict)) { |
| 40 | # Copy libfuzzer_options to output if dict is not provided. |
| 41 | copy(target_name + "_libfuzzer_options_copy") { |
| 42 | sources = [ |
| 43 | invoker.libfuzzer_options, |
| 44 | ] |
| 45 | outputs = [ |
| 46 | "$root_build_dir/" + target_name + ".options", |
| 47 | ] |
| 48 | } |
| 49 | |
| 50 | test_deps += [ ":" + target_name + "_libfuzzer_options_copy" ] |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 51 | } |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 52 | |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 53 | if (defined(invoker.seed_corpus)) { |
| 54 | depfile = "$root_build_dir/$target_name" + ".seed_corpus.d" |
| 55 | out = "$root_build_dir/$target_name" + "_seed_corpus.zip" |
| 56 | |
| 57 | action(target_name + "_seed_corpus") { |
| 58 | script = "//testing/libfuzzer/archive_corpus.py" |
| 59 | args = [ |
| 60 | "--depfile", |
| 61 | rebase_path(depfile), |
| 62 | "--corpus", |
| 63 | rebase_path(invoker.seed_corpus), |
| 64 | "--output", |
| 65 | rebase_path(out), |
| 66 | "--fuzzer", |
| 67 | rebase_path("$root_build_dir/$target_name"), |
| 68 | ] |
| 69 | |
| 70 | depfile = depfile |
| 71 | outputs = [ |
| 72 | out, |
| 73 | ] |
aizatsky | 7951d34f | 2016-03-11 00:09:45 | [diff] [blame^] | 74 | deps = [ |
| 75 | "//testing/libfuzzer:seed_corpus", |
| 76 | ] |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | test_deps += [ ":" + target_name + "_seed_corpus" ] |
| 80 | } |
| 81 | |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 82 | if (defined(invoker.dict)) { |
mmoroz | 344a75ae | 2016-03-02 16:57:17 | [diff] [blame] | 83 | # Copy dictionary to output. |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 84 | copy(target_name + "_dict_copy") { |
| 85 | sources = [ |
| 86 | invoker.dict, |
| 87 | ] |
| 88 | outputs = [ |
mmoroz | 344a75ae | 2016-03-02 16:57:17 | [diff] [blame] | 89 | "$root_build_dir/" + target_name + ".dict", |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 90 | ] |
| 91 | } |
| 92 | |
mmoroz | 344a75ae | 2016-03-02 16:57:17 | [diff] [blame] | 93 | test_deps += [ ":" + target_name + "_dict_copy" ] |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 94 | |
mmoroz | 344a75ae | 2016-03-02 16:57:17 | [diff] [blame] | 95 | # Generate new .options file or update an existing one. |
| 96 | config_name = target_name + ".options" |
| 97 | action(config_name) { |
| 98 | script = "//testing/libfuzzer/gen_fuzzer_config.py" |
| 99 | args = [ |
| 100 | "--config", |
| 101 | rebase_path("$root_build_dir/" + config_name), |
| 102 | "--dict", |
| 103 | rebase_path("$root_build_dir/" + invoker.target_name + ".dict"), |
| 104 | ] |
| 105 | |
| 106 | if (defined(invoker.libfuzzer_options)) { |
| 107 | args += [ |
| 108 | "--libfuzzer_options", |
| 109 | rebase_path(invoker.libfuzzer_options), |
| 110 | ] |
| 111 | } |
| 112 | outputs = [ |
| 113 | "$root_build_dir/$config_name", |
| 114 | ] |
| 115 | } |
| 116 | test_deps += [ ":" + config_name ] |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 117 | } |
| 118 | |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 119 | test(target_name) { |
| 120 | forward_variables_from(invoker, |
| 121 | [ |
| 122 | "sources", |
| 123 | "include_dirs", |
| 124 | ]) |
| 125 | deps = test_deps |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 126 | |
| 127 | if (defined(invoker.additional_configs)) { |
| 128 | configs += invoker.additional_configs |
| 129 | } |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 130 | } |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 131 | } else { |
| 132 | # noop on unsupported platforms. |
| 133 | # mark attributes as used. |
| 134 | assert(invoker.sources == [] || invoker.sources != []) |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 135 | if (defined(invoker.additional_configs)) { |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 136 | assert( |
| 137 | invoker.additional_configs == [] || invoker.additional_configs != []) |
| 138 | } |
| 139 | if (defined(invoker.deps)) { |
| 140 | assert(invoker.deps == [] || invoker.deps != []) |
| 141 | } |
| 142 | if (defined(invoker.dict)) { |
| 143 | assert(invoker.dict == [] || invoker.dict != []) |
| 144 | } |
mmoroz | 344a75ae | 2016-03-02 16:57:17 | [diff] [blame] | 145 | if (defined(invoker.libfuzzer_options)) { |
| 146 | assert(invoker.libfuzzer_options == [] || invoker.libfuzzer_options != []) |
| 147 | } |
aizatsky | 198e302 | 2016-03-08 21:33:46 | [diff] [blame] | 148 | if (defined(invoker.seed_corpus)) { |
| 149 | assert(invoker.seed_corpus == [] || invoker.seed_corpus != []) |
| 150 | } |
aizatsky | 43d459a5 | 2016-02-13 01:22:18 | [diff] [blame] | 151 | |
| 152 | group(target_name) { |
aizatsky | cb1a4393 | 2015-12-14 20:59:14 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |