Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS 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 | |
| 5 | """Script for performing tasks that are useful for fuzzer development. |
| 6 | |
| 7 | Run "cros_fuzz" in the chroot for a list of command or "cros_fuzz $COMMAND |
| 8 | --help" for their full details. Below is a summary of commands that the script |
| 9 | can perform: |
| 10 | |
| 11 | coverage: Generate a coverage report for a given fuzzer (specified by "--fuzzer" |
| 12 | option). You almost certainly want to specify the package to build (using |
| 13 | the "--package" option) so that a coverage build is done, since a coverage |
| 14 | build is needed to generate a report. If your fuzz target is running on |
| 15 | ClusterFuzz already, you can use the "--download" option to download the |
| 16 | corpus from ClusterFuzz. Otherwise, you can use the "--corpus" option to |
| 17 | specify the path of the corpus to run the fuzzer on and generate a report. |
| 18 | The corpus will be copied to the sysroot so that the fuzzer can use it. |
| 19 | Note that "--download" and "--corpus" are mutually exclusive. |
| 20 | |
| 21 | reproduce: Runs the fuzzer specified by the "--fuzzer" option on a testcase |
| 22 | (path specified by the "--testcase" argument). Optionally does a build when |
| 23 | the "--package" option is used. The type of build can be specified using the |
| 24 | "--build_type" argument. |
| 25 | |
| 26 | download: Downloads the corpus from ClusterFuzz of the fuzzer specified by the |
| 27 | "--fuzzer" option. The path of the directory the corpus directory is |
| 28 | downloaded to can be specified using the "--directory" option. |
| 29 | |
| 30 | shell: Sets up the sysroot for fuzzing and then chroots into the sysroot giving |
| 31 | you a shell that is ready to fuzz. |
| 32 | |
| 33 | setup: Sets up the sysroot for fuzzing (done prior to doing "reproduce", "shell" |
| 34 | and "coverage" commands). |
| 35 | |
| 36 | cleanup: Undoes "setup". |
| 37 | |
| 38 | Note that cros_fuzz will print every shell command it runs if you set the |
| 39 | log-level to debug ("--log-level debug"). Otherwise it will print commands that |
| 40 | fail. |
| 41 | """ |
| 42 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 43 | import logging |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 44 | import os |
| 45 | import shutil |
| 46 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 47 | from chromite.third_party import lddtree |
| 48 | from chromite.third_party.pyelftools.elftools.elf.elffile import ELFFile |
| 49 | |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 50 | from chromite.lib import build_target_lib |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 51 | from chromite.lib import commandline |
| 52 | from chromite.lib import constants |
| 53 | from chromite.lib import cros_build_lib |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 54 | from chromite.lib import gs |
| 55 | from chromite.lib import osutils |
Manoj Gupta | e5e1e61 | 2019-10-21 12:39:57 -0700 | [diff] [blame] | 56 | from chromite.lib import portage_util |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 57 | |
Mike Frysinger | 03b983f | 2020-02-21 02:31:49 -0500 | [diff] [blame] | 58 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 59 | # Directory in sysroot's /tmp directory that this script will use for files it |
| 60 | # needs to write. We need a directory to write files to because this script uses |
| 61 | # external programs that must write and read to/from files and because these |
| 62 | # must be run inside the sysroot and thus are usually unable to read or write |
| 63 | # from directories in the chroot environment this script is executed in. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | SCRIPT_STORAGE_DIRECTORY = "fuzz" |
| 65 | SCRIPT_STORAGE_PATH = os.path.join("/", "tmp", SCRIPT_STORAGE_DIRECTORY) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 66 | |
| 67 | # Names of subdirectories in "fuzz" directory used by this script to store |
| 68 | # things. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | CORPUS_DIRECTORY_NAME = "corpus" |
| 70 | TESTCASE_DIRECTORY_NAME = "testcase" |
| 71 | COVERAGE_REPORT_DIRECTORY_NAME = "coverage-report" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 72 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 73 | # Constants for names of libFuzzer command line options. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | RUNS_OPTION_NAME = "runs" |
| 75 | MAX_TOTAL_TIME_OPTION_NAME = "max_total_time" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 76 | |
| 77 | # The default path a profraw file written by a clang coverage instrumented |
| 78 | # binary when run by this script (default is current working directory). |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | DEFAULT_PROFRAW_PATH = "/default.profraw" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 80 | |
| 81 | # Constants for libFuzzer command line values. |
| 82 | # 0 runs means execute everything in the corpus and do no mutations. |
| 83 | RUNS_DEFAULT_VALUE = 0 |
| 84 | # An arbitrary but short amount of time to run a fuzzer to get some coverage |
| 85 | # data (when a corpus hasn't been provided and we aren't told to download one. |
| 86 | MAX_TOTAL_TIME_DEFAULT_VALUE = 30 |
| 87 | |
| 88 | |
| 89 | class BuildType(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | """Class to hold the different kinds of build types.""" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 91 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | ASAN = "asan" |
| 93 | MSAN = "msan" |
| 94 | UBSAN = "ubsan" |
| 95 | COVERAGE = "coverage" |
| 96 | STANDARD = "" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | # Build types that users can specify. |
| 99 | CHOICES = (ASAN, MSAN, UBSAN, COVERAGE) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 100 | |
| 101 | |
| 102 | class SysrootPath(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | """Class for representing a path that is in the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 104 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 105 | Useful for dealing with paths that we must interact with when chrooted into |
| 106 | the sysroot and outside of it. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 107 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | For example, if we need to interact with the "/tmp" directory of the sysroot, |
| 109 | SysrootPath('/tmp').sysroot returns the path of the directory if we are in |
| 110 | chrooted into the sysroot, i.e. "/tmp". |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 111 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | SysrootPath('/tmp').chroot returns the path of the directory when in the |
| 113 | cros_sdk i.e. SYSROOT_DIRECTORY + "/tmp" (this will probably be |
| 114 | "/build/amd64-generic/tmp" in most cases). |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 115 | """ |
| 116 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 117 | # The actual path to the sysroot (from within the chroot). |
| 118 | path_to_sysroot = None |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | def __init__(self, path): |
| 121 | """Constructor. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 122 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | Args: |
| 124 | path: An absolute path representing something in the sysroot. |
| 125 | """ |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | assert path.startswith("/") |
| 128 | if self.IsPathInSysroot(path): |
| 129 | path = self.FromChrootPathInSysroot(os.path.abspath(path)) |
| 130 | self.path_list = path.split(os.sep)[1:] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 131 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 132 | @classmethod |
| 133 | def SetPathToSysroot(cls, board): |
| 134 | """Sets path_to_sysroot |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 135 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 136 | Args: |
| 137 | board: The board we will use for our sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 138 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | Returns: |
| 140 | The path to the sysroot (the value of path_to_sysroot). |
| 141 | """ |
| 142 | cls.path_to_sysroot = build_target_lib.get_default_sysroot_path(board) |
| 143 | return cls.path_to_sysroot |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | @property |
| 146 | def chroot(self): |
| 147 | """Get the path of the object in the Chrome OS SDK chroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 148 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | Returns: |
| 150 | The path this object represents when chrooted into the sysroot. |
| 151 | """ |
| 152 | assert ( |
| 153 | self.path_to_sysroot is not None |
| 154 | ), "set SysrootPath.path_to_sysroot" |
| 155 | return os.path.join(self.path_to_sysroot, *self.path_list) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | @property |
| 158 | def sysroot(self): |
| 159 | """Get the path of the object when in the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 160 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | Returns: |
| 162 | The path this object represents when in the Chrome OS SDK . |
| 163 | """ |
| 164 | return os.path.join("/", *self.path_list) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 165 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 166 | @classmethod |
| 167 | def IsPathInSysroot(cls, path): |
| 168 | """Is a path in the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 169 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | Args: |
| 171 | path: The path we are checking is in the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 172 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | Returns: |
| 174 | True if path is within the sysroot's path in the chroot. |
| 175 | """ |
| 176 | assert cls.path_to_sysroot |
| 177 | return path.startswith(cls.path_to_sysroot) |
| 178 | |
| 179 | @classmethod |
| 180 | def FromChrootPathInSysroot(cls, path): |
| 181 | """Converts a chroot-relative path that is in sysroot into sysroot-relative. |
| 182 | |
| 183 | Args: |
| 184 | path: The chroot-relative path we are converting to sysroot relative. |
| 185 | |
| 186 | Returns: |
| 187 | The sysroot relative version of |path|. |
| 188 | """ |
| 189 | assert cls.IsPathInSysroot(path) |
| 190 | common_prefix = os.path.commonprefix([cls.path_to_sysroot, path]) |
| 191 | return path[len(common_prefix) :] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 192 | |
| 193 | |
| 194 | def GetScriptStoragePath(relative_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | """Get the SysrootPath representing a script storage path. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 196 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | Get a path of a directory this script will store things in. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 198 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 199 | Args: |
| 200 | relative_path: The path relative to the root of the script storage |
| 201 | directory. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 202 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 203 | Returns: |
| 204 | The SysrootPath representing absolute path of |relative_path| in the script |
| 205 | storage directory. |
| 206 | """ |
| 207 | path = os.path.join(SCRIPT_STORAGE_PATH, relative_path) |
| 208 | return SysrootPath(path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 209 | |
| 210 | |
| 211 | def GetSysrootPath(path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | """Get the chroot-relative path of a path in the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 213 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 214 | Args: |
| 215 | path: An absolute path in the sysroot that we will get the path in the |
| 216 | chroot for. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | Returns: |
| 219 | The chroot-relative path of |path| in the sysroot. |
| 220 | """ |
| 221 | return SysrootPath(path).chroot |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 222 | |
| 223 | |
| 224 | def GetCoverageDirectory(fuzzer): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 225 | """Get a coverage report directory for a fuzzer |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 226 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | Args: |
| 228 | fuzzer: The fuzzer to get the coverage report directory for. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 229 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 230 | Returns: |
| 231 | The location of the coverage report directory for the |fuzzer|. |
| 232 | """ |
| 233 | relative_path = os.path.join(COVERAGE_REPORT_DIRECTORY_NAME, fuzzer) |
| 234 | return GetScriptStoragePath(relative_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 235 | |
| 236 | |
| 237 | def GetFuzzerSysrootPath(fuzzer): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 238 | """Get the path in the sysroot of a fuzzer. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 239 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 240 | Args: |
| 241 | fuzzer: The fuzzer to get the path of. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 242 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 243 | Returns: |
| 244 | The path of |fuzzer| in the sysroot. |
| 245 | """ |
| 246 | return SysrootPath(os.path.join("/", "usr", "libexec", "fuzzers", fuzzer)) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 247 | |
| 248 | |
| 249 | def GetProfdataPath(fuzzer): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | """Get the profdata file of a fuzzer. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 251 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 252 | Args: |
| 253 | fuzzer: The fuzzer to get the profdata file of. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 254 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | Returns: |
| 256 | The path of the profdata file that should be used by |fuzzer|. |
| 257 | """ |
| 258 | return GetScriptStoragePath("%s.profdata" % fuzzer) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 259 | |
| 260 | |
| 261 | def GetPathForCopy(parent_directory, chroot_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 262 | """Returns a path in the script storage directory to copy chroot_path. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 263 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 264 | Returns a SysrootPath representing the location where |chroot_path| should |
| 265 | copied. This path will be in the parent_directory which will be in the script |
| 266 | storage directory. |
| 267 | """ |
| 268 | basename = os.path.basename(chroot_path) |
| 269 | return GetScriptStoragePath(os.path.join(parent_directory, basename)) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 270 | |
| 271 | |
| 272 | def CopyCorpusToSysroot(src_corpus_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 273 | """Copies corpus into the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 274 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 275 | Copies corpus into the sysroot. Doesn't copy if corpus is already in sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 276 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 277 | Args: |
| 278 | src_corpus_path: A path (in the chroot) to a corpus that will be copied into |
| 279 | sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 280 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 281 | Returns: |
| 282 | The path in the sysroot that the corpus was copied to. |
| 283 | """ |
| 284 | if src_corpus_path is None: |
| 285 | return None |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 286 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 287 | if SysrootPath.IsPathInSysroot(src_corpus_path): |
| 288 | # Don't copy if |src_testcase_path| is already in sysroot. Just return it in |
| 289 | # the format expected by the caller. |
| 290 | return SysrootPath(src_corpus_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 291 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 292 | dest_corpus_path = GetPathForCopy(CORPUS_DIRECTORY_NAME, src_corpus_path) |
| 293 | osutils.RmDir(dest_corpus_path.chroot, ignore_missing=True) |
| 294 | shutil.copytree(src_corpus_path, dest_corpus_path.chroot) |
| 295 | return dest_corpus_path |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 296 | |
| 297 | |
| 298 | def CopyTestcaseToSysroot(src_testcase_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 299 | """Copies a testcase into the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 300 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 301 | Copies a testcase into the sysroot. Doesn't copy if testcase is already in |
| 302 | sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 303 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 304 | Args: |
| 305 | src_testcase_path: A path (in the chroot) to a testcase that will be copied |
| 306 | into sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 307 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 308 | Returns: |
| 309 | The path in the sysroot that the testcase was copied to. |
| 310 | """ |
| 311 | if SysrootPath.IsPathInSysroot(src_testcase_path): |
| 312 | # Don't copy if |src_testcase_path| is already in sysroot. Just return it in |
| 313 | # the format expected by the caller. |
| 314 | return SysrootPath(src_testcase_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 315 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 316 | dest_testcase_path = GetPathForCopy( |
| 317 | TESTCASE_DIRECTORY_NAME, src_testcase_path |
| 318 | ) |
| 319 | osutils.SafeMakedirsNonRoot(os.path.dirname(dest_testcase_path.chroot)) |
| 320 | osutils.SafeUnlink(dest_testcase_path.chroot) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 321 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 322 | shutil.copy(src_testcase_path, dest_testcase_path.chroot) |
| 323 | return dest_testcase_path |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 324 | |
| 325 | |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 326 | def sudo_run(*args, **kwargs): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 327 | """Wrapper around cros_build_lib.sudo_run. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 328 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 329 | Wrapper that calls cros_build_lib.sudo_run but sets debug_level by |
| 330 | default. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 331 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 332 | Args: |
| 333 | *args: Positional arguments to pass to cros_build_lib.sudo_run. |
| 334 | *kwargs: Keyword arguments to pass to cros_build_lib.sudo_run. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 335 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 336 | Returns: |
| 337 | The value returned by calling cros_build_lib.sudo_run. |
| 338 | """ |
| 339 | kwargs.setdefault("debug_level", logging.DEBUG) |
| 340 | return cros_build_lib.sudo_run(*args, **kwargs) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 341 | |
| 342 | |
| 343 | def GetLibFuzzerOption(option_name, option_value): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 344 | """Gets the libFuzzer command line option with the specified name and value. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 345 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 346 | Args: |
| 347 | option_name: The name of the libFuzzer option. |
| 348 | option_value: The value of the libFuzzer option. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 349 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 350 | Returns: |
| 351 | The libFuzzer option composed of |option_name| and |option_value|. |
| 352 | """ |
| 353 | return "-%s=%s" % (option_name, option_value) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 354 | |
| 355 | |
| 356 | def IsOptionLimit(option): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 357 | """Determines if fuzzer option limits fuzzing time.""" |
| 358 | for limit_name in [MAX_TOTAL_TIME_OPTION_NAME, RUNS_OPTION_NAME]: |
| 359 | if option.startswith("-%s" % limit_name): |
| 360 | return True |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 361 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 362 | return False |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 363 | |
| 364 | |
| 365 | def LimitFuzzing(fuzz_command, corpus): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 366 | """Limits how long fuzzing will go if unspecified. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 367 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 368 | Adds a reasonable limit on how much fuzzing will be done unless there already |
| 369 | is some kind of limit. Mutates fuzz_command. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 370 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 371 | Args: |
| 372 | fuzz_command: A command to run a fuzzer. Used to determine if a limit needs |
| 373 | to be set. Mutated if it is needed to specify a limit. |
| 374 | corpus: The corpus that will be passed to the fuzzer. If not None then |
| 375 | fuzzing is limited by running everything in the corpus once. |
| 376 | """ |
| 377 | if any(IsOptionLimit(x) for x in fuzz_command[1:]): |
| 378 | # Don't do anything if there is already a limit. |
| 379 | return |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | if corpus: |
| 382 | # If there is a corpus, just run everything in the corpus once. |
| 383 | fuzz_command.append( |
| 384 | GetLibFuzzerOption(RUNS_OPTION_NAME, RUNS_DEFAULT_VALUE) |
| 385 | ) |
| 386 | return |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 387 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 388 | # Since there is no corpus, just fuzz for 30 seconds. |
| 389 | logging.info( |
| 390 | "Limiting fuzzing to %s seconds.", MAX_TOTAL_TIME_DEFAULT_VALUE |
| 391 | ) |
| 392 | max_total_time_option = GetLibFuzzerOption( |
| 393 | MAX_TOTAL_TIME_OPTION_NAME, MAX_TOTAL_TIME_DEFAULT_VALUE |
| 394 | ) |
| 395 | fuzz_command.append(max_total_time_option) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 396 | |
| 397 | |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 398 | def GetFuzzExtraEnv(extra_options=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 399 | """Gets extra_env for fuzzing. |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 400 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 401 | Gets environment varaibles and values for running libFuzzer. Sets defaults and |
| 402 | allows user to specify extra sanitizer options. |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 403 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 404 | Args: |
| 405 | extra_options: A dict containing sanitizer options to set in addition to the |
| 406 | defaults. |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 407 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | Returns: |
| 409 | A dict containing environment variables and their values that can be used in |
| 410 | the environment libFuzzer runs in. |
| 411 | """ |
| 412 | if extra_options is None: |
| 413 | extra_options = {} |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 414 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 415 | # log_path must be set because Chrome OS's patched compiler changes it. |
| 416 | # disable odr violation since many fuzzers hit it and it is also disabled on |
| 417 | # clusterfuzz. |
| 418 | options_dict = {"log_path": "stderr", "detect_odr_violation": "0"} |
| 419 | options_dict.update(extra_options) |
| 420 | sanitizer_options = ":".join("%s=%s" % x for x in options_dict.items()) |
| 421 | sanitizers = ("ASAN", "MSAN", "UBSAN") |
| 422 | return {x + "_OPTIONS": sanitizer_options for x in sanitizers} |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 423 | |
| 424 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 425 | def RunFuzzer( |
| 426 | fuzzer, |
| 427 | corpus_path=None, |
| 428 | fuzz_args="", |
| 429 | testcase_path=None, |
| 430 | crash_expected=False, |
| 431 | ): |
| 432 | """Runs the fuzzer while chrooted into the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 433 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 434 | Args: |
| 435 | fuzzer: The fuzzer to run. |
| 436 | corpus_path: A path to a corpus (not necessarily in the sysroot) to run the |
| 437 | fuzzer on. |
| 438 | fuzz_args: Additional arguments to pass to the fuzzer when running it. |
| 439 | testcase_path: A path to a testcase (not necessarily in the sysroot) to run |
| 440 | the fuzzer on. |
| 441 | crash_expected: Is it normal for the fuzzer to crash on this run? |
| 442 | """ |
| 443 | logging.info("Running fuzzer: %s", fuzzer) |
| 444 | fuzzer_sysroot_path = GetFuzzerSysrootPath(fuzzer) |
| 445 | fuzz_command = [fuzzer_sysroot_path.sysroot] |
| 446 | fuzz_command += fuzz_args.split() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 447 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 448 | if testcase_path: |
| 449 | fuzz_command.append(testcase_path) |
| 450 | else: |
| 451 | LimitFuzzing(fuzz_command, corpus_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 452 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | if corpus_path: |
| 454 | fuzz_command.append(corpus_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 455 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 456 | if crash_expected: |
| 457 | # Don't return nonzero when fuzzer OOMs, leaks, or timesout, since we don't |
| 458 | # want an exception in those cases. The user may be trying to reproduce |
| 459 | # those issues. |
| 460 | fuzz_command += ["-error_exitcode=0", "-timeout_exitcode=0"] |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 461 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 462 | # We must set exitcode=0 or else the fuzzer will return nonzero on |
| 463 | # successful reproduction. |
| 464 | sanitizer_options = {"exitcode": "0"} |
| 465 | else: |
| 466 | sanitizer_options = {} |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | extra_env = GetFuzzExtraEnv(sanitizer_options) |
| 469 | RunSysrootCommand( |
| 470 | fuzz_command, extra_env=extra_env, debug_level=logging.INFO |
| 471 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 472 | |
| 473 | |
| 474 | def MergeProfraw(fuzzer): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 475 | """Merges profraw file from a fuzzer and creates a profdata file. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 476 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 477 | Args: |
| 478 | fuzzer: The fuzzer to merge the profraw file from. |
| 479 | """ |
| 480 | profdata_path = GetProfdataPath(fuzzer) |
| 481 | command = [ |
| 482 | "llvm-profdata", |
| 483 | "merge", |
| 484 | "-sparse", |
| 485 | DEFAULT_PROFRAW_PATH, |
| 486 | "-o", |
| 487 | profdata_path.sysroot, |
| 488 | ] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 489 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 490 | RunSysrootCommand(command) |
| 491 | return profdata_path |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 492 | |
| 493 | |
| 494 | def GenerateCoverageReport(fuzzer, shared_libraries): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 495 | """Generates an HTML coverage report from a fuzzer run. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 496 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 497 | Args: |
| 498 | fuzzer: The fuzzer to generate the coverage report for. |
| 499 | shared_libraries: Libraries loaded dynamically by |fuzzer|. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 500 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 501 | Returns: |
| 502 | The path of the coverage report. |
| 503 | """ |
| 504 | fuzzer_path = GetFuzzerSysrootPath(fuzzer).chroot |
| 505 | command = ["llvm-cov", "show", "-object", fuzzer_path] |
| 506 | for library in shared_libraries: |
| 507 | command += ["-object", library] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 508 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 509 | coverage_directory = GetCoverageDirectory(fuzzer) |
| 510 | command += [ |
| 511 | "-format=html", |
| 512 | "-instr-profile=%s" % GetProfdataPath(fuzzer).chroot, |
| 513 | "-output-dir=%s" % coverage_directory.chroot, |
| 514 | ] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 515 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 516 | # TODO(metzman): Investigate error messages printed by this command. |
| 517 | cros_build_lib.run(command, stderr=True, debug_level=logging.DEBUG) |
| 518 | return coverage_directory |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 519 | |
| 520 | |
| 521 | def GetSharedLibraries(binary_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 522 | """Gets the shared libraries used by a binary. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 523 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 524 | Gets the shared libraries used by the binary. Based on GetSharedLibraries from |
| 525 | src/tools/code_coverage/coverage_utils.py in Chromium. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 526 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 527 | Args: |
| 528 | binary_path: The path to the binary we want to find the shared libraries of. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 529 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 530 | Returns: |
| 531 | The shared libraries used by |binary_path|. |
| 532 | """ |
| 533 | logging.info("Finding shared libraries for targets (if any).") |
| 534 | shared_libraries = [] |
| 535 | elf_dict = lddtree.ParseELF( |
| 536 | binary_path.chroot, root=SysrootPath.path_to_sysroot |
| 537 | ) |
| 538 | for shared_library in elf_dict["libs"].values(): |
| 539 | shared_library_path = shared_library["path"] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 540 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 541 | if shared_library_path in shared_libraries: |
| 542 | continue |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 543 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 544 | assert os.path.exists(shared_library_path), ( |
| 545 | 'Shared library "%s" used by ' |
| 546 | "the given target(s) does not " |
| 547 | "exist." % shared_library_path |
| 548 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 549 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 550 | if IsInstrumentedWithClangCoverage(shared_library_path): |
| 551 | # Do not add non-instrumented libraries. Otherwise, llvm-cov errors out. |
| 552 | shared_libraries.append(shared_library_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 553 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 554 | logging.debug( |
| 555 | "Found shared libraries (%d): %s.", |
| 556 | len(shared_libraries), |
| 557 | shared_libraries, |
| 558 | ) |
| 559 | logging.info("Finished finding shared libraries for targets.") |
| 560 | return shared_libraries |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 561 | |
| 562 | |
| 563 | def IsInstrumentedWithClangCoverage(binary_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 564 | """Determines if a binary is instrumented with clang source based coverage. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 565 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 566 | Args: |
| 567 | binary_path: The path of the binary (executable or library) we are checking |
| 568 | is instrumented with clang source based coverage. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 569 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 570 | Returns: |
| 571 | True if the binary is instrumented with clang source based coverage. |
| 572 | """ |
| 573 | with open(binary_path, "rb") as file_handle: |
| 574 | elf_file = ELFFile(file_handle) |
| 575 | return elf_file.get_section_by_name(b"__llvm_covmap") is not None |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 576 | |
| 577 | |
| 578 | def RunFuzzerAndGenerateCoverageReport(fuzzer, corpus, fuzz_args): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 579 | """Runs a fuzzer generates a coverage report and returns the report's path. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 580 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 581 | Gets a coverage report for a fuzzer. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 582 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 583 | Args: |
| 584 | fuzzer: The fuzzer to run and generate the coverage report for. |
| 585 | corpus: The path to a corpus to run the fuzzer on. |
| 586 | fuzz_args: Additional arguments to pass to the fuzzer. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 587 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 588 | Returns: |
| 589 | The path to the coverage report. |
| 590 | """ |
| 591 | corpus_path = CopyCorpusToSysroot(corpus) |
| 592 | if corpus_path: |
| 593 | corpus_path = corpus_path.sysroot |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 594 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 595 | RunFuzzer(fuzzer, corpus_path=corpus_path, fuzz_args=fuzz_args) |
| 596 | MergeProfraw(fuzzer) |
| 597 | fuzzer_sysroot_path = GetFuzzerSysrootPath(fuzzer) |
| 598 | shared_libraries = GetSharedLibraries(fuzzer_sysroot_path) |
| 599 | return GenerateCoverageReport(fuzzer, shared_libraries) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 600 | |
| 601 | |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 602 | def RunSysrootCommand(command, **kwargs): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 603 | """Runs command while chrooted into sysroot and returns the output. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 604 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 605 | Args: |
| 606 | command: A command to run in the sysroot. |
| 607 | kwargs: Extra arguments to pass to cros_build_lib.sudo_run. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 608 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 609 | Returns: |
| 610 | The result of a call to cros_build_lib.sudo_run. |
| 611 | """ |
| 612 | command = ["chroot", SysrootPath.path_to_sysroot] + command |
| 613 | return sudo_run(command, **kwargs) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 614 | |
| 615 | |
| 616 | def GetBuildExtraEnv(build_type): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 617 | """Gets the extra_env for building a package. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 618 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 619 | Args: |
| 620 | build_type: The type of build we want to do. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 621 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 622 | Returns: |
| 623 | The extra_env to use when building. |
| 624 | """ |
| 625 | if build_type is None: |
| 626 | build_type = BuildType.ASAN |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 627 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 628 | use_flags = os.environ.get("USE", "").split() |
| 629 | # Check that the user hasn't already set USE flags that we can set. |
| 630 | # No good way to iterate over an enum in python2. |
| 631 | for use_flag in BuildType.CHOICES: |
| 632 | if use_flag in use_flags: |
| 633 | logging.warning( |
| 634 | "%s in USE flags. Please use --build_type instead.", use_flag |
| 635 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 636 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 637 | # Set USE flags. |
| 638 | fuzzer_build_type = "fuzzer" |
| 639 | use_flags += [fuzzer_build_type, build_type] |
| 640 | features_flags = os.environ.get("FEATURES", "").split() |
| 641 | if build_type == BuildType.COVERAGE: |
| 642 | # We must use ASan when doing coverage builds. |
| 643 | use_flags.append(BuildType.ASAN) |
| 644 | # Use noclean so that a coverage report can be generated based on the source |
| 645 | # code. |
| 646 | features_flags.append("noclean") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 647 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 648 | return { |
| 649 | "FEATURES": " ".join(features_flags), |
| 650 | "USE": " ".join(use_flags), |
| 651 | } |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 652 | |
| 653 | |
| 654 | def BuildPackage(package, board, build_type): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 655 | """Builds a package on a specified board. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 656 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 657 | Args: |
| 658 | package: The package to build. Nothing is built if None. |
| 659 | board: The board to build the package on. |
| 660 | build_type: The type of the build to do (e.g. asan, msan, ubsan, coverage). |
| 661 | """ |
| 662 | if package is None: |
| 663 | return |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 664 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 665 | logging.info("Building %s using %s.", package, build_type) |
| 666 | extra_env = GetBuildExtraEnv(build_type) |
| 667 | command = [ |
| 668 | "build_packages", |
| 669 | "--board", |
| 670 | board, |
| 671 | "--skip-chroot-upgrade", |
| 672 | package, |
| 673 | ] |
| 674 | # For msan builds, always use "--no-usepkg" since all package needs to be |
| 675 | # instrumented with msan. |
| 676 | if build_type == BuildType.MSAN: |
| 677 | command += ["--no-usepkg"] |
Manoj Gupta | 5ca1765 | 2019-05-13 11:15:33 -0700 | [diff] [blame] | 678 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 679 | # Print the output of the build command. Do this because it is familiar to |
| 680 | # devs and we don't want to leave them not knowing about the build's progress |
| 681 | # for a long time. |
| 682 | cros_build_lib.run(command, extra_env=extra_env) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 683 | |
| 684 | |
| 685 | def DownloadFuzzerCorpus(fuzzer, dest_directory=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 686 | """Downloads a corpus and returns its path. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 687 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 688 | Downloads a corpus to a subdirectory of dest_directory if specified and |
| 689 | returns path on the filesystem of the corpus. Asks users to authenticate |
| 690 | if permission to read from bucket is denied. |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 691 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 692 | Args: |
| 693 | fuzzer: The name of the fuzzer who's corpus we want to download. |
| 694 | dest_directory: The directory to download the corpus to. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 695 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 696 | Returns: |
| 697 | The path to the downloaded corpus. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 698 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 699 | Raises: |
| 700 | gs.NoSuchKey: A corpus for the fuzzer doesn't exist. |
| 701 | gs.GSCommandError: The corpus failed to download for another reason. |
| 702 | """ |
| 703 | if not fuzzer.startswith("chromeos_"): |
| 704 | # ClusterFuzz internally appends "chromeos_" to chromeos targets' names. |
| 705 | # Therefore we must do so in order to find the corpus. |
| 706 | fuzzer = "chromeos_%s" % fuzzer |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 707 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 708 | if dest_directory is None: |
| 709 | dest_directory = GetScriptStoragePath(CORPUS_DIRECTORY_NAME).chroot |
| 710 | osutils.SafeMakedirsNonRoot(dest_directory) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 711 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 712 | clusterfuzz_gcs_corpus_bucket = "chromeos-corpus" |
| 713 | suburl = "libfuzzer/%s" % fuzzer |
| 714 | gcs_path = gs.GetGsURL( |
| 715 | clusterfuzz_gcs_corpus_bucket, |
| 716 | for_gsutil=True, |
| 717 | public=False, |
| 718 | suburl=suburl, |
| 719 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 720 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 721 | dest_path = os.path.join(dest_directory, fuzzer) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 722 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 723 | try: |
| 724 | logging.info("Downloading corpus to %s.", dest_path) |
| 725 | ctx = gs.GSContext() |
| 726 | ctx.Copy( |
| 727 | gcs_path, |
| 728 | dest_directory, |
| 729 | recursive=True, |
| 730 | parallel=True, |
| 731 | debug_level=logging.DEBUG, |
| 732 | ) |
| 733 | logging.info("Finished downloading corpus.") |
| 734 | except gs.GSNoSuchKey as exception: |
| 735 | logging.error("Corpus for fuzzer: %s does not exist.", fuzzer) |
| 736 | raise exception |
| 737 | # Try to authenticate if we were denied permission to access the corpus. |
| 738 | except gs.GSCommandError as exception: |
| 739 | logging.error( |
| 740 | "gsutil failed to download the corpus. You may need to log in. See:\n" |
| 741 | "https://chromium.googlesource.com/chromiumos/docs/+/HEAD/gsutil.md" |
| 742 | "#setup\n" |
| 743 | "for instructions on doing this." |
| 744 | ) |
| 745 | raise exception |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 746 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 747 | return dest_path |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 748 | |
| 749 | |
| 750 | def Reproduce(fuzzer, testcase_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 751 | """Runs a fuzzer in the sysroot on a testcase. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 752 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 753 | Args: |
| 754 | fuzzer: The fuzzer to run. |
| 755 | testcase_path: The path (not necessarily in the sysroot) of the testcase to |
| 756 | run the fuzzer on. |
| 757 | """ |
| 758 | testcase_sysroot_path = CopyTestcaseToSysroot(testcase_path).sysroot |
| 759 | RunFuzzer(fuzzer, testcase_path=testcase_sysroot_path, crash_expected=True) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 760 | |
| 761 | |
| 762 | def SetUpSysrootForFuzzing(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 763 | """Sets up the the sysroot for fuzzing |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 764 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 765 | Prepares the sysroot for fuzzing. Idempotent. |
| 766 | """ |
| 767 | logging.info("Setting up sysroot for fuzzing.") |
| 768 | # TODO(metzman): Don't create devices or mount /proc, use platform2_test.py |
| 769 | # instead. |
| 770 | # Mount /proc in sysroot and setup dev there because they are needed by |
| 771 | # sanitizers. |
| 772 | proc_manager = ProcManager() |
| 773 | proc_manager.Mount() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 774 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 775 | # Setup devices in /dev that are needed by libFuzzer. |
| 776 | device_manager = DeviceManager() |
| 777 | device_manager.SetUp() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 778 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 779 | # Set up asan_symbolize.py, llvm-symbolizer, and llvm-profdata in the |
| 780 | # sysroot so that fuzzer output (including stack traces) can be symbolized |
| 781 | # and so that coverage reports can be generated. |
| 782 | tool_manager = ToolManager() |
| 783 | tool_manager.Install() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 784 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 785 | osutils.SafeMakedirsNonRoot(GetSysrootPath(SCRIPT_STORAGE_PATH)) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 786 | |
| 787 | |
| 788 | def CleanUpSysroot(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 789 | """Cleans up the the sysroot from SetUpSysrootForFuzzing. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 790 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 791 | Undoes SetUpSysrootForFuzzing. Idempotent. |
| 792 | """ |
| 793 | logging.info("Cleaning up the sysroot.") |
| 794 | proc_manager = ProcManager() |
| 795 | proc_manager.Unmount() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 796 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 797 | device_manager = DeviceManager() |
| 798 | device_manager.CleanUp() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 799 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 800 | tool_manager = ToolManager() |
| 801 | tool_manager.Uninstall() |
| 802 | osutils.RmDir(GetSysrootPath(SCRIPT_STORAGE_PATH), ignore_missing=True) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 803 | |
| 804 | |
| 805 | class ToolManager(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 806 | """Class that installs or uninstalls fuzzing tools to/from the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 807 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 808 | Install and Uninstall methods are idempotent. Both are safe to call at any |
| 809 | point. |
| 810 | """ |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 811 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 812 | # Path to asan_symbolize.py. |
| 813 | ASAN_SYMBOLIZE_PATH = os.path.join("/", "usr", "bin", "asan_symbolize.py") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 814 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 815 | # List of LLVM binaries we must install in sysroot. |
| 816 | LLVM_BINARY_NAMES = ["gdbserver", "llvm-symbolizer", "llvm-profdata"] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 817 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 818 | def __init__(self): |
| 819 | self.asan_symbolize_sysroot_path = GetSysrootPath( |
| 820 | self.ASAN_SYMBOLIZE_PATH |
| 821 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 822 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 823 | def Install(self): |
| 824 | """Installs tools to the sysroot.""" |
| 825 | # Install asan_symbolize.py. |
| 826 | sudo_run( |
| 827 | ["cp", self.ASAN_SYMBOLIZE_PATH, self.asan_symbolize_sysroot_path] |
| 828 | ) |
| 829 | # Install the LLVM binaries. |
| 830 | # TODO(metzman): Build these tools so that we don't mess up when board is |
| 831 | # for a different ISA. |
| 832 | for llvm_binary in self._GetLLVMBinaries(): |
| 833 | llvm_binary.Install() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 834 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 835 | def Uninstall(self): |
| 836 | """Uninstalls tools from the sysroot. Undoes Install.""" |
| 837 | # Uninstall asan_symbolize.py. |
| 838 | osutils.SafeUnlink(self.asan_symbolize_sysroot_path, sudo=True) |
| 839 | # Uninstall the LLVM binaries. |
| 840 | for llvm_binary in self._GetLLVMBinaries(): |
| 841 | llvm_binary.Uninstall() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 842 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 843 | def _GetLLVMBinaries(self): |
| 844 | """Creates LllvmBinary objects for each binary name in LLVM_BINARY_NAMES.""" |
| 845 | return [LlvmBinary(x) for x in self.LLVM_BINARY_NAMES] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 846 | |
| 847 | |
| 848 | class LlvmBinary(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 849 | """Class for representing installing/uninstalling an LLVM binary in sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 850 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 851 | Install and Uninstall methods are idempotent. Both are safe to call at any |
| 852 | time. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 853 | """ |
Manoj Gupta | feb1b7a | 2019-02-20 11:04:05 -0800 | [diff] [blame] | 854 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 855 | # Path to the lddtree chromite script. |
| 856 | LDDTREE_SCRIPT_PATH = os.path.join(constants.CHROMITE_BIN_DIR, "lddtree") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 857 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 858 | def __init__(self, binary): |
| 859 | self.binary = binary |
| 860 | self.install_dir = GetSysrootPath( |
| 861 | os.path.join("/", "usr", "libexec", binary) |
| 862 | ) |
| 863 | self.binary_dir_path = GetSysrootPath(os.path.join("/", "usr", "bin")) |
| 864 | self.binary_chroot_dest_path = os.path.join( |
| 865 | self.binary_dir_path, binary |
| 866 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 867 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 868 | def Uninstall(self): |
| 869 | """Removes an LLVM binary from sysroot. Undoes Install.""" |
| 870 | osutils.RmDir(self.install_dir, ignore_missing=True, sudo=True) |
| 871 | osutils.SafeUnlink(self.binary_chroot_dest_path, sudo=True) |
| 872 | |
| 873 | def Install(self): |
| 874 | """Installs (sets up) an LLVM binary in the sysroot. |
| 875 | |
| 876 | Sets up an llvm binary in the sysroot so that it can be run there. |
| 877 | """ |
| 878 | # Create a directory for installing |binary| and all of its dependencies in |
| 879 | # the sysroot. |
| 880 | binary_rel_path = ["usr", "bin", self.binary] |
| 881 | binary_chroot_path = os.path.join("/", *binary_rel_path) |
| 882 | if not os.path.exists(binary_chroot_path): |
| 883 | logging.warning( |
| 884 | "Cannot copy %s, file does not exist in chroot.", |
| 885 | binary_chroot_path, |
| 886 | ) |
| 887 | logging.warning( |
| 888 | "Functionality provided by %s will be missing.", |
| 889 | binary_chroot_path, |
| 890 | ) |
| 891 | return |
| 892 | |
| 893 | osutils.SafeMakedirsNonRoot(self.install_dir) |
| 894 | |
| 895 | # Copy the binary and everything needed to run it into the sysroot. |
| 896 | cmd = [ |
| 897 | self.LDDTREE_SCRIPT_PATH, |
| 898 | "-v", |
| 899 | "--generate-wrappers", |
| 900 | "--root", |
| 901 | "/", |
| 902 | "--copy-to-tree", |
| 903 | self.install_dir, |
| 904 | binary_chroot_path, |
| 905 | ] |
| 906 | sudo_run(cmd) |
| 907 | |
| 908 | # Create a symlink to the copy of the binary (we can't do lddtree in |
| 909 | # self.binary_dir_path). Note that symlink should be relative so that it |
| 910 | # will be valid when chrooted into the sysroot. |
| 911 | rel_path = os.path.relpath(self.install_dir, self.binary_dir_path) |
| 912 | link_path = os.path.join(rel_path, *binary_rel_path) |
| 913 | osutils.SafeSymlink(link_path, self.binary_chroot_dest_path, sudo=True) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 914 | |
| 915 | |
| 916 | class DeviceManager(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 917 | """Class that creates or removes devices from /dev in sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 918 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 919 | SetUp and CleanUp methods are idempotent. Both are safe to call at any point. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 920 | """ |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 921 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 922 | DEVICE_MKNOD_PARAMS = { |
| 923 | "null": (666, 3), |
| 924 | "random": (444, 8), |
| 925 | "urandom": (444, 9), |
| 926 | } |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 927 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 928 | MKNOD_MAJOR = "1" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 929 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 930 | def __init__(self): |
| 931 | self.dev_path_chroot = GetSysrootPath("/dev") |
| 932 | |
| 933 | def _GetDevicePath(self, device_name): |
| 934 | """Returns the path of |device_name| in sysroot's /dev.""" |
| 935 | return os.path.join(self.dev_path_chroot, device_name) |
| 936 | |
| 937 | def SetUp(self): |
| 938 | """Sets up devices in the sysroot's /dev. |
| 939 | |
| 940 | Creates /dev/null, /dev/random, and /dev/urandom. If they already exist then |
| 941 | recreates them. |
| 942 | """ |
| 943 | self.CleanUp() |
| 944 | osutils.SafeMakedirsNonRoot(self.dev_path_chroot) |
| 945 | for device, mknod_params in self.DEVICE_MKNOD_PARAMS.items(): |
| 946 | device_path = self._GetDevicePath(device) |
| 947 | self._MakeCharDevice(device_path, *mknod_params) |
| 948 | |
| 949 | def CleanUp(self): |
| 950 | """Cleans up devices in the sysroot's /dev. Undoes SetUp. |
| 951 | |
| 952 | Removes /dev/null, /dev/random, and /dev/urandom if they exist. |
| 953 | """ |
| 954 | for device in self.DEVICE_MKNOD_PARAMS: |
| 955 | device_path = self._GetDevicePath(device) |
| 956 | if os.path.exists(device_path): |
| 957 | # Use -r since dev/null is sometimes a directory. |
| 958 | sudo_run(["rm", "-r", device_path]) |
| 959 | |
| 960 | def _MakeCharDevice(self, path, mode, minor): |
| 961 | """Make a character device.""" |
| 962 | mode = str(mode) |
| 963 | minor = str(minor) |
| 964 | command = ["mknod", "-m", mode, path, "c", self.MKNOD_MAJOR, minor] |
| 965 | sudo_run(command) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 966 | |
| 967 | |
| 968 | class ProcManager(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 969 | """Class that mounts or unmounts /proc in sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 970 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 971 | Mount and Unmount are idempotent. Both are safe to call at any point. |
| 972 | """ |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 973 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 974 | PROC_PATH = "/proc" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 975 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 976 | def __init__(self): |
| 977 | self.proc_path_chroot = GetSysrootPath(self.PROC_PATH) |
| 978 | self.is_mounted = osutils.IsMounted(self.proc_path_chroot) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 979 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 980 | def Unmount(self): |
| 981 | """Unmounts /proc in chroot. Undoes Mount.""" |
| 982 | if not self.is_mounted: |
| 983 | return |
| 984 | osutils.UmountDir(self.proc_path_chroot, cleanup=False) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 985 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 986 | def Mount(self): |
| 987 | """Mounts /proc in chroot. Remounts it if already mounted.""" |
| 988 | self.Unmount() |
| 989 | osutils.MountDir( |
| 990 | self.PROC_PATH, |
| 991 | self.proc_path_chroot, |
| 992 | "proc", |
| 993 | debug_level=logging.DEBUG, |
| 994 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 995 | |
| 996 | |
| 997 | def EnterSysrootShell(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 998 | """Spawns and gives user access to a bash shell in the sysroot.""" |
| 999 | command = ["/bin/bash", "-i"] |
| 1000 | return RunSysrootCommand( |
| 1001 | command, |
| 1002 | extra_env=GetFuzzExtraEnv(), |
| 1003 | debug_level=logging.INFO, |
| 1004 | check=False, |
| 1005 | ).returncode |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1006 | |
| 1007 | |
| 1008 | def StripFuzzerPrefixes(fuzzer_name): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1009 | """Strip the prefix ClusterFuzz uses in case they are specified. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1010 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1011 | Strip the prefixes used by ClusterFuzz if the users has included them by |
| 1012 | accident. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1013 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1014 | Args: |
| 1015 | fuzzer_name: The fuzzer who's name may contain prefixes. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1016 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1017 | Returns: |
| 1018 | The name of the fuzz target without prefixes. |
| 1019 | """ |
| 1020 | initial_name = fuzzer_name |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1021 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1022 | def StripPrefix(prefix): |
| 1023 | if fuzzer_name.startswith(prefix): |
| 1024 | return fuzzer_name[len(prefix) :] |
| 1025 | return fuzzer_name |
| 1026 | |
| 1027 | clusterfuzz_prefixes = ["libFuzzer_", "chromeos_"] |
| 1028 | |
| 1029 | for prefix in clusterfuzz_prefixes: |
| 1030 | fuzzer_name = StripPrefix(prefix) |
| 1031 | |
| 1032 | if initial_name != fuzzer_name: |
| 1033 | logging.warning( |
| 1034 | "%s contains a prefix from ClusterFuzz (one or more of %s) that is not " |
| 1035 | "part of the fuzzer's name. Interpreting --fuzzer as %s.", |
| 1036 | initial_name, |
| 1037 | clusterfuzz_prefixes, |
| 1038 | fuzzer_name, |
| 1039 | ) |
| 1040 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1041 | return fuzzer_name |
| 1042 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1043 | |
| 1044 | def ExecuteShellCommand(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1045 | """Executes the "shell" command. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1046 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1047 | Sets up the sysroot for fuzzing and gives user access to a bash shell it |
| 1048 | spawns in the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1049 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1050 | Returns: |
| 1051 | The exit code of the shell command. |
| 1052 | """ |
| 1053 | SetUpSysrootForFuzzing() |
| 1054 | return EnterSysrootShell() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1055 | |
| 1056 | |
| 1057 | def ExecuteSetupCommand(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1058 | """Executes the "setup" command. Wrapper for SetUpSysrootForFuzzing. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1059 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1060 | Sets up the sysroot for fuzzing. |
| 1061 | """ |
| 1062 | SetUpSysrootForFuzzing() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1063 | |
| 1064 | |
| 1065 | def ExecuteCleanupCommand(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1066 | """Executes the "cleanup" command. Wrapper for CleanUpSysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1067 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1068 | Undoes pre-fuzzing setup. |
| 1069 | """ |
| 1070 | CleanUpSysroot() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1071 | |
| 1072 | |
| 1073 | def ExecuteCoverageCommand(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1074 | """Executes the "coverage" command. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1075 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1076 | Executes the "coverage" command by optionally doing a coverage build of a |
| 1077 | package, optionally downloading the fuzzer's corpus, optionally copying it |
| 1078 | into the sysroot, running the fuzzer and then generating a coverage report |
| 1079 | for the user to view. Causes program to exit if fuzzer is not instrumented |
| 1080 | with source based coverage. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1081 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1082 | Args: |
| 1083 | options: The parsed arguments passed to this program. |
| 1084 | """ |
| 1085 | BuildPackage(options.package, options.board, BuildType.COVERAGE) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1086 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1087 | fuzzer = StripFuzzerPrefixes(options.fuzzer) |
| 1088 | fuzzer_sysroot_path = GetFuzzerSysrootPath(fuzzer) |
| 1089 | if not IsInstrumentedWithClangCoverage(fuzzer_sysroot_path.chroot): |
| 1090 | # Don't run the fuzzer if it isn't instrumented with source based coverage. |
| 1091 | # Quit and let the user know how to build the fuzzer properly. |
| 1092 | cros_build_lib.Die( |
| 1093 | "%s is not instrumented with source based coverage.\nSpecify --package " |
| 1094 | 'to do a coverage build or build with USE flag: "coverage".', |
| 1095 | fuzzer, |
| 1096 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1097 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1098 | corpus = options.corpus |
| 1099 | if options.download: |
| 1100 | corpus = DownloadFuzzerCorpus(options.fuzzer) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1101 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1102 | # Set up sysroot for fuzzing. |
| 1103 | SetUpSysrootForFuzzing() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1104 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1105 | coverage_report_path = RunFuzzerAndGenerateCoverageReport( |
| 1106 | fuzzer, corpus, options.fuzz_args |
| 1107 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1109 | # Get path on host so user can access it with their browser. |
| 1110 | # TODO(metzman): Add the ability to convert to host paths to path_util. |
| 1111 | external_trunk_path = os.getenv("EXTERNAL_TRUNK_PATH") |
| 1112 | coverage_report_host_path = os.path.join( |
| 1113 | external_trunk_path, "chroot", coverage_report_path.chroot[1:] |
| 1114 | ) |
| 1115 | print( |
| 1116 | "Coverage report written to file://%s/index.html" |
| 1117 | % coverage_report_host_path |
| 1118 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1119 | |
| 1120 | |
| 1121 | def ExecuteDownloadCommand(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1122 | """Executes the "download" command. Wrapper around DownloadFuzzerCorpus.""" |
| 1123 | DownloadFuzzerCorpus(StripFuzzerPrefixes(options.fuzzer), options.directory) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1124 | |
| 1125 | |
| 1126 | def ExecuteReproduceCommand(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1127 | """Executes the "reproduce" command. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1129 | Executes the "reproduce" command by Running a fuzzer on a testcase. |
| 1130 | May build the fuzzer before running. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1131 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1132 | Args: |
| 1133 | options: The parsed arguments passed to this program. |
| 1134 | """ |
| 1135 | if options.build_type and not options.package: |
| 1136 | raise Exception( |
| 1137 | "Cannot specify --build_type without specifying --package." |
| 1138 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1139 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1140 | # Verify that "msan-fuzzer" profile is being used with msan. |
| 1141 | # Check presence of "-fsanitize=memory" in CFLAGS. |
| 1142 | if options.build_type == BuildType.MSAN: |
| 1143 | cmd = ["portageq-%s" % options.board, "envvar", "CFLAGS"] |
| 1144 | cflags = cros_build_lib.run( |
| 1145 | cmd, capture_output=True, encoding="utf-8" |
| 1146 | ).stdout.splitlines() |
| 1147 | check_string = "-fsanitize=memory" |
| 1148 | if not any(check_string in s for s in cflags): |
| 1149 | logging.error( |
| 1150 | "-fsanitize=memory not found in CFLAGS. " |
| 1151 | 'Use "setup_board --board=amd64-generic --profile=msan-fuzzer" ' |
| 1152 | "for MSan Fuzzing Builds." |
| 1153 | ) |
| 1154 | raise Exception("Incompatible profile used for msan fuzzing.") |
Manoj Gupta | 5ca1765 | 2019-05-13 11:15:33 -0700 | [diff] [blame] | 1155 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1156 | BuildPackage(options.package, options.board, options.build_type) |
| 1157 | SetUpSysrootForFuzzing() |
| 1158 | Reproduce(StripFuzzerPrefixes(options.fuzzer), options.testcase) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1159 | |
Manoj Gupta | e5e1e61 | 2019-10-21 12:39:57 -0700 | [diff] [blame] | 1160 | |
Manoj Gupta | ec08b81 | 2019-10-10 14:21:16 -0700 | [diff] [blame] | 1161 | def InstallBaseDependencies(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1162 | """Installs the base packages needed to chroot in board sysroot. |
Manoj Gupta | ec08b81 | 2019-10-10 14:21:16 -0700 | [diff] [blame] | 1163 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1164 | Args: |
| 1165 | options: The parsed arguments passed to this program. |
| 1166 | """ |
| 1167 | package = "virtual/implicit-system" |
| 1168 | if not portage_util.IsPackageInstalled( |
| 1169 | package, sysroot=SysrootPath.path_to_sysroot |
| 1170 | ): |
| 1171 | build_type = getattr(options, "build_type", None) |
| 1172 | BuildPackage(package, options.board, build_type) |
Manoj Gupta | e5e1e61 | 2019-10-21 12:39:57 -0700 | [diff] [blame] | 1173 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1174 | |
| 1175 | def ParseArgs(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1176 | """Parses program arguments. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1178 | Args: |
| 1179 | argv: The program arguments we want to parse. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1180 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1181 | Returns: |
| 1182 | An options object which will tell us which command to run and which options |
| 1183 | to use for that command. |
| 1184 | """ |
| 1185 | parser = commandline.ArgumentParser(description=__doc__) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1186 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1187 | parser.add_argument( |
| 1188 | "--board", |
| 1189 | default=cros_build_lib.GetDefaultBoard(), |
| 1190 | help="Board on which to run test.", |
| 1191 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1193 | subparsers = parser.add_subparsers(dest="command") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1195 | subparsers.add_parser("cleanup", help="Undo setup command.") |
| 1196 | coverage_parser = subparsers.add_parser( |
| 1197 | "coverage", help="Get a coverage report for a fuzzer." |
| 1198 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1199 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1200 | coverage_parser.add_argument("--package", help="Package to build.") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1201 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1202 | corpus_parser = coverage_parser.add_mutually_exclusive_group() |
| 1203 | corpus_parser.add_argument("--corpus", help="Corpus to run fuzzer on.") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1204 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1205 | corpus_parser.add_argument( |
| 1206 | "--download", |
| 1207 | action="store_true", |
| 1208 | help="Generate coverage report based on corpus from ClusterFuzz.", |
| 1209 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1210 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1211 | coverage_parser.add_argument( |
| 1212 | "--fuzzer", |
| 1213 | required=True, |
| 1214 | help="The fuzz target to generate a coverage report for.", |
| 1215 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1216 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1217 | coverage_parser.add_argument( |
| 1218 | "--fuzz-args", |
| 1219 | default="", |
| 1220 | help="Arguments to pass libFuzzer. " |
| 1221 | "Please use an equals sign or parsing will fail " |
| 1222 | '(i.e. --fuzzer_args="-rss_limit_mb=2048 -print_funcs=1").', |
| 1223 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1224 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1225 | download_parser = subparsers.add_parser( |
| 1226 | "download", help="Download a corpus." |
| 1227 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1228 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1229 | download_parser.add_argument( |
| 1230 | "--directory", help="Path to directory to download the corpus to." |
| 1231 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1232 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1233 | download_parser.add_argument( |
| 1234 | "--fuzzer", required=True, help="Fuzzer to download the corpus for." |
| 1235 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1236 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1237 | reproduce_parser = subparsers.add_parser( |
| 1238 | "reproduce", help="Run a fuzzer on a testcase." |
| 1239 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1240 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1241 | reproduce_parser.add_argument( |
| 1242 | "--testcase", required=True, help="Path of testcase to run fuzzer on." |
| 1243 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1244 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1245 | reproduce_parser.add_argument( |
| 1246 | "--fuzzer", required=True, help="Fuzzer to reproduce the crash on." |
| 1247 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1248 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1249 | reproduce_parser.add_argument("--package", help="Package to build.") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1250 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1251 | reproduce_parser.add_argument( |
| 1252 | "--build-type", |
| 1253 | choices=BuildType.CHOICES, |
| 1254 | help="Type of build.", |
| 1255 | type=str.lower, |
| 1256 | ) # Ignore sanitizer case. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1257 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1258 | subparsers.add_parser("setup", help="Set up the sysroot to test fuzzing.") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1259 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1260 | subparsers.add_parser( |
| 1261 | "shell", |
| 1262 | help="Set up sysroot for fuzzing and get a shell in the sysroot.", |
| 1263 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1264 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1265 | opts = parser.parse_args(argv) |
| 1266 | opts.Freeze() |
| 1267 | return opts |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1268 | |
| 1269 | |
| 1270 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1271 | """Parses arguments and executes a command. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1272 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1273 | Args: |
| 1274 | argv: The prorgram arguments. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1275 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1276 | Returns: |
| 1277 | 0 on success. Non-zero on failure. |
| 1278 | """ |
| 1279 | cros_build_lib.AssertInsideChroot() |
| 1280 | options = ParseArgs(argv) |
| 1281 | if options.board is None: |
| 1282 | logging.error('Please specify "--board" or set ".default_board".') |
| 1283 | return 1 |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1284 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1285 | SysrootPath.SetPathToSysroot(options.board) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1286 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1287 | InstallBaseDependencies(options) |
Manoj Gupta | ec08b81 | 2019-10-10 14:21:16 -0700 | [diff] [blame] | 1288 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1289 | if options.command == "cleanup": |
| 1290 | ExecuteCleanupCommand() |
| 1291 | elif options.command == "coverage": |
| 1292 | ExecuteCoverageCommand(options) |
| 1293 | elif options.command == "setup": |
| 1294 | ExecuteSetupCommand() |
| 1295 | elif options.command == "download": |
| 1296 | ExecuteDownloadCommand(options) |
| 1297 | elif options.command == "reproduce": |
| 1298 | ExecuteReproduceCommand(options) |
| 1299 | elif options.command == "shell": |
| 1300 | return ExecuteShellCommand() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1301 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1302 | return 0 |