Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2018 The ChromiumOS Authors |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 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. |
Maksim Ivanov | b4a6c4f | 2022-12-09 02:32:39 +0000 | [diff] [blame] | 418 | # handle_sigtrap is useful for catching int3 in assertion checks in ChromeOS |
| 419 | # code. |
| 420 | options_dict = { |
| 421 | "log_path": "stderr", |
| 422 | "detect_odr_violation": "0", |
| 423 | "handle_sigtrap": "1", |
| 424 | } |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 425 | options_dict.update(extra_options) |
| 426 | sanitizer_options = ":".join("%s=%s" % x for x in options_dict.items()) |
| 427 | sanitizers = ("ASAN", "MSAN", "UBSAN") |
| 428 | return {x + "_OPTIONS": sanitizer_options for x in sanitizers} |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 429 | |
| 430 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 431 | def RunFuzzer( |
| 432 | fuzzer, |
| 433 | corpus_path=None, |
| 434 | fuzz_args="", |
| 435 | testcase_path=None, |
| 436 | crash_expected=False, |
| 437 | ): |
| 438 | """Runs the fuzzer while chrooted into the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 439 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | Args: |
| 441 | fuzzer: The fuzzer to run. |
| 442 | corpus_path: A path to a corpus (not necessarily in the sysroot) to run the |
| 443 | fuzzer on. |
| 444 | fuzz_args: Additional arguments to pass to the fuzzer when running it. |
| 445 | testcase_path: A path to a testcase (not necessarily in the sysroot) to run |
| 446 | the fuzzer on. |
| 447 | crash_expected: Is it normal for the fuzzer to crash on this run? |
| 448 | """ |
| 449 | logging.info("Running fuzzer: %s", fuzzer) |
| 450 | fuzzer_sysroot_path = GetFuzzerSysrootPath(fuzzer) |
| 451 | fuzz_command = [fuzzer_sysroot_path.sysroot] |
| 452 | fuzz_command += fuzz_args.split() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 453 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 454 | if testcase_path: |
| 455 | fuzz_command.append(testcase_path) |
| 456 | else: |
| 457 | LimitFuzzing(fuzz_command, corpus_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 458 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 459 | if corpus_path: |
| 460 | fuzz_command.append(corpus_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 461 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 462 | if crash_expected: |
| 463 | # Don't return nonzero when fuzzer OOMs, leaks, or timesout, since we don't |
| 464 | # want an exception in those cases. The user may be trying to reproduce |
| 465 | # those issues. |
| 466 | fuzz_command += ["-error_exitcode=0", "-timeout_exitcode=0"] |
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 | # We must set exitcode=0 or else the fuzzer will return nonzero on |
| 469 | # successful reproduction. |
| 470 | sanitizer_options = {"exitcode": "0"} |
| 471 | else: |
| 472 | sanitizer_options = {} |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 473 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 474 | extra_env = GetFuzzExtraEnv(sanitizer_options) |
| 475 | RunSysrootCommand( |
| 476 | fuzz_command, extra_env=extra_env, debug_level=logging.INFO |
| 477 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 478 | |
| 479 | |
| 480 | def MergeProfraw(fuzzer): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 481 | """Merges profraw file from a fuzzer and creates a profdata file. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 482 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 483 | Args: |
| 484 | fuzzer: The fuzzer to merge the profraw file from. |
| 485 | """ |
| 486 | profdata_path = GetProfdataPath(fuzzer) |
| 487 | command = [ |
| 488 | "llvm-profdata", |
| 489 | "merge", |
| 490 | "-sparse", |
| 491 | DEFAULT_PROFRAW_PATH, |
| 492 | "-o", |
| 493 | profdata_path.sysroot, |
| 494 | ] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 495 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 496 | RunSysrootCommand(command) |
| 497 | return profdata_path |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 498 | |
| 499 | |
| 500 | def GenerateCoverageReport(fuzzer, shared_libraries): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 501 | """Generates an HTML coverage report from a fuzzer run. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 502 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 503 | Args: |
| 504 | fuzzer: The fuzzer to generate the coverage report for. |
| 505 | shared_libraries: Libraries loaded dynamically by |fuzzer|. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 506 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 507 | Returns: |
| 508 | The path of the coverage report. |
| 509 | """ |
| 510 | fuzzer_path = GetFuzzerSysrootPath(fuzzer).chroot |
| 511 | command = ["llvm-cov", "show", "-object", fuzzer_path] |
| 512 | for library in shared_libraries: |
| 513 | command += ["-object", library] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 514 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 515 | coverage_directory = GetCoverageDirectory(fuzzer) |
| 516 | command += [ |
| 517 | "-format=html", |
| 518 | "-instr-profile=%s" % GetProfdataPath(fuzzer).chroot, |
| 519 | "-output-dir=%s" % coverage_directory.chroot, |
| 520 | ] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 521 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 522 | # TODO(metzman): Investigate error messages printed by this command. |
| 523 | cros_build_lib.run(command, stderr=True, debug_level=logging.DEBUG) |
| 524 | return coverage_directory |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 525 | |
| 526 | |
| 527 | def GetSharedLibraries(binary_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 528 | """Gets the shared libraries used by a binary. |
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 | Gets the shared libraries used by the binary. Based on GetSharedLibraries from |
| 531 | src/tools/code_coverage/coverage_utils.py in Chromium. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 532 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | Args: |
| 534 | 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] | 535 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 536 | Returns: |
| 537 | The shared libraries used by |binary_path|. |
| 538 | """ |
| 539 | logging.info("Finding shared libraries for targets (if any).") |
| 540 | shared_libraries = [] |
| 541 | elf_dict = lddtree.ParseELF( |
| 542 | binary_path.chroot, root=SysrootPath.path_to_sysroot |
| 543 | ) |
| 544 | for shared_library in elf_dict["libs"].values(): |
| 545 | shared_library_path = shared_library["path"] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 546 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 547 | if shared_library_path in shared_libraries: |
| 548 | continue |
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 | assert os.path.exists(shared_library_path), ( |
| 551 | 'Shared library "%s" used by ' |
| 552 | "the given target(s) does not " |
| 553 | "exist." % shared_library_path |
| 554 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 555 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 556 | if IsInstrumentedWithClangCoverage(shared_library_path): |
| 557 | # Do not add non-instrumented libraries. Otherwise, llvm-cov errors out. |
| 558 | shared_libraries.append(shared_library_path) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 559 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 560 | logging.debug( |
| 561 | "Found shared libraries (%d): %s.", |
| 562 | len(shared_libraries), |
| 563 | shared_libraries, |
| 564 | ) |
| 565 | logging.info("Finished finding shared libraries for targets.") |
| 566 | return shared_libraries |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 567 | |
| 568 | |
| 569 | def IsInstrumentedWithClangCoverage(binary_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 570 | """Determines if a binary is instrumented with clang source based coverage. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 571 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 572 | Args: |
| 573 | binary_path: The path of the binary (executable or library) we are checking |
| 574 | is instrumented with clang source based coverage. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 575 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 576 | Returns: |
| 577 | True if the binary is instrumented with clang source based coverage. |
| 578 | """ |
| 579 | with open(binary_path, "rb") as file_handle: |
| 580 | elf_file = ELFFile(file_handle) |
| 581 | 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] | 582 | |
| 583 | |
| 584 | def RunFuzzerAndGenerateCoverageReport(fuzzer, corpus, fuzz_args): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 585 | """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] | 586 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 587 | Gets a coverage report for a fuzzer. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 588 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 589 | Args: |
| 590 | fuzzer: The fuzzer to run and generate the coverage report for. |
| 591 | corpus: The path to a corpus to run the fuzzer on. |
| 592 | fuzz_args: Additional arguments to pass to the fuzzer. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 593 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 594 | Returns: |
| 595 | The path to the coverage report. |
| 596 | """ |
| 597 | corpus_path = CopyCorpusToSysroot(corpus) |
| 598 | if corpus_path: |
| 599 | corpus_path = corpus_path.sysroot |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 600 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 601 | RunFuzzer(fuzzer, corpus_path=corpus_path, fuzz_args=fuzz_args) |
| 602 | MergeProfraw(fuzzer) |
| 603 | fuzzer_sysroot_path = GetFuzzerSysrootPath(fuzzer) |
| 604 | shared_libraries = GetSharedLibraries(fuzzer_sysroot_path) |
| 605 | return GenerateCoverageReport(fuzzer, shared_libraries) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 606 | |
| 607 | |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 608 | def RunSysrootCommand(command, **kwargs): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 609 | """Runs command while chrooted into sysroot and returns the output. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 610 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 611 | Args: |
Alex Klein | 361062b | 2023-04-05 09:45:28 -0600 | [diff] [blame] | 612 | command: A command to run in the sysroot. |
| 613 | **kwargs: Extra arguments to pass to cros_build_lib.sudo_run. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 614 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 615 | Returns: |
| 616 | The result of a call to cros_build_lib.sudo_run. |
| 617 | """ |
| 618 | command = ["chroot", SysrootPath.path_to_sysroot] + command |
| 619 | return sudo_run(command, **kwargs) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 620 | |
| 621 | |
| 622 | def GetBuildExtraEnv(build_type): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 623 | """Gets the extra_env for building a package. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 624 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 625 | Args: |
| 626 | build_type: The type of build we want to do. |
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 | Returns: |
| 629 | The extra_env to use when building. |
| 630 | """ |
| 631 | if build_type is None: |
| 632 | build_type = BuildType.ASAN |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 633 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 634 | use_flags = os.environ.get("USE", "").split() |
| 635 | # Check that the user hasn't already set USE flags that we can set. |
| 636 | # No good way to iterate over an enum in python2. |
| 637 | for use_flag in BuildType.CHOICES: |
| 638 | if use_flag in use_flags: |
| 639 | logging.warning( |
| 640 | "%s in USE flags. Please use --build_type instead.", use_flag |
| 641 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 642 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 643 | # Set USE flags. |
| 644 | fuzzer_build_type = "fuzzer" |
| 645 | use_flags += [fuzzer_build_type, build_type] |
| 646 | features_flags = os.environ.get("FEATURES", "").split() |
| 647 | if build_type == BuildType.COVERAGE: |
| 648 | # We must use ASan when doing coverage builds. |
| 649 | use_flags.append(BuildType.ASAN) |
| 650 | # Use noclean so that a coverage report can be generated based on the source |
| 651 | # code. |
| 652 | features_flags.append("noclean") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 653 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 654 | return { |
| 655 | "FEATURES": " ".join(features_flags), |
| 656 | "USE": " ".join(use_flags), |
| 657 | } |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 658 | |
| 659 | |
| 660 | def BuildPackage(package, board, build_type): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 661 | """Builds a package on a specified board. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 662 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 663 | Args: |
| 664 | package: The package to build. Nothing is built if None. |
| 665 | board: The board to build the package on. |
| 666 | build_type: The type of the build to do (e.g. asan, msan, ubsan, coverage). |
| 667 | """ |
| 668 | if package is None: |
| 669 | return |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 670 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 671 | logging.info("Building %s using %s.", package, build_type) |
| 672 | extra_env = GetBuildExtraEnv(build_type) |
| 673 | command = [ |
| 674 | "build_packages", |
| 675 | "--board", |
| 676 | board, |
| 677 | "--skip-chroot-upgrade", |
| 678 | package, |
| 679 | ] |
| 680 | # For msan builds, always use "--no-usepkg" since all package needs to be |
| 681 | # instrumented with msan. |
| 682 | if build_type == BuildType.MSAN: |
| 683 | command += ["--no-usepkg"] |
Manoj Gupta | 5ca1765 | 2019-05-13 11:15:33 -0700 | [diff] [blame] | 684 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 685 | # Print the output of the build command. Do this because it is familiar to |
| 686 | # devs and we don't want to leave them not knowing about the build's progress |
| 687 | # for a long time. |
| 688 | cros_build_lib.run(command, extra_env=extra_env) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 689 | |
| 690 | |
| 691 | def DownloadFuzzerCorpus(fuzzer, dest_directory=None): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 692 | """Downloads a corpus and returns its path. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 693 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 694 | Downloads a corpus to a subdirectory of dest_directory if specified and |
| 695 | returns path on the filesystem of the corpus. Asks users to authenticate |
| 696 | if permission to read from bucket is denied. |
Jonathan Metzman | b2c3373 | 2018-11-08 11:33:35 -0800 | [diff] [blame] | 697 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 698 | Args: |
| 699 | fuzzer: The name of the fuzzer who's corpus we want to download. |
| 700 | dest_directory: The directory to download the corpus to. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 701 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 702 | Returns: |
| 703 | The path to the downloaded corpus. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 704 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 705 | Raises: |
| 706 | gs.NoSuchKey: A corpus for the fuzzer doesn't exist. |
| 707 | gs.GSCommandError: The corpus failed to download for another reason. |
| 708 | """ |
| 709 | if not fuzzer.startswith("chromeos_"): |
| 710 | # ClusterFuzz internally appends "chromeos_" to chromeos targets' names. |
| 711 | # Therefore we must do so in order to find the corpus. |
| 712 | fuzzer = "chromeos_%s" % fuzzer |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 713 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 714 | if dest_directory is None: |
| 715 | dest_directory = GetScriptStoragePath(CORPUS_DIRECTORY_NAME).chroot |
| 716 | osutils.SafeMakedirsNonRoot(dest_directory) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 717 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 718 | clusterfuzz_gcs_corpus_bucket = "chromeos-corpus" |
| 719 | suburl = "libfuzzer/%s" % fuzzer |
| 720 | gcs_path = gs.GetGsURL( |
| 721 | clusterfuzz_gcs_corpus_bucket, |
| 722 | for_gsutil=True, |
| 723 | public=False, |
| 724 | suburl=suburl, |
| 725 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 726 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 727 | dest_path = os.path.join(dest_directory, fuzzer) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 728 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 729 | try: |
| 730 | logging.info("Downloading corpus to %s.", dest_path) |
| 731 | ctx = gs.GSContext() |
| 732 | ctx.Copy( |
| 733 | gcs_path, |
| 734 | dest_directory, |
| 735 | recursive=True, |
| 736 | parallel=True, |
| 737 | debug_level=logging.DEBUG, |
| 738 | ) |
| 739 | logging.info("Finished downloading corpus.") |
| 740 | except gs.GSNoSuchKey as exception: |
| 741 | logging.error("Corpus for fuzzer: %s does not exist.", fuzzer) |
| 742 | raise exception |
| 743 | # Try to authenticate if we were denied permission to access the corpus. |
| 744 | except gs.GSCommandError as exception: |
| 745 | logging.error( |
| 746 | "gsutil failed to download the corpus. You may need to log in. See:\n" |
| 747 | "https://chromium.googlesource.com/chromiumos/docs/+/HEAD/gsutil.md" |
| 748 | "#setup\n" |
| 749 | "for instructions on doing this." |
| 750 | ) |
| 751 | raise exception |
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 | return dest_path |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 754 | |
| 755 | |
| 756 | def Reproduce(fuzzer, testcase_path): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 757 | """Runs a fuzzer in the sysroot on a testcase. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 758 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 759 | Args: |
| 760 | fuzzer: The fuzzer to run. |
| 761 | testcase_path: The path (not necessarily in the sysroot) of the testcase to |
| 762 | run the fuzzer on. |
| 763 | """ |
| 764 | testcase_sysroot_path = CopyTestcaseToSysroot(testcase_path).sysroot |
| 765 | RunFuzzer(fuzzer, testcase_path=testcase_sysroot_path, crash_expected=True) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 766 | |
| 767 | |
| 768 | def SetUpSysrootForFuzzing(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 769 | """Sets up the the sysroot for fuzzing |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 770 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 771 | Prepares the sysroot for fuzzing. Idempotent. |
| 772 | """ |
| 773 | logging.info("Setting up sysroot for fuzzing.") |
| 774 | # TODO(metzman): Don't create devices or mount /proc, use platform2_test.py |
| 775 | # instead. |
| 776 | # Mount /proc in sysroot and setup dev there because they are needed by |
| 777 | # sanitizers. |
| 778 | proc_manager = ProcManager() |
| 779 | proc_manager.Mount() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 780 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 781 | # Setup devices in /dev that are needed by libFuzzer. |
| 782 | device_manager = DeviceManager() |
| 783 | device_manager.SetUp() |
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 | # Set up asan_symbolize.py, llvm-symbolizer, and llvm-profdata in the |
| 786 | # sysroot so that fuzzer output (including stack traces) can be symbolized |
| 787 | # and so that coverage reports can be generated. |
| 788 | tool_manager = ToolManager() |
| 789 | tool_manager.Install() |
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 | osutils.SafeMakedirsNonRoot(GetSysrootPath(SCRIPT_STORAGE_PATH)) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 792 | |
| 793 | |
| 794 | def CleanUpSysroot(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 795 | """Cleans up the the sysroot from SetUpSysrootForFuzzing. |
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 | Undoes SetUpSysrootForFuzzing. Idempotent. |
| 798 | """ |
| 799 | logging.info("Cleaning up the sysroot.") |
| 800 | proc_manager = ProcManager() |
| 801 | proc_manager.Unmount() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 802 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 803 | device_manager = DeviceManager() |
| 804 | device_manager.CleanUp() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 805 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 806 | tool_manager = ToolManager() |
| 807 | tool_manager.Uninstall() |
| 808 | osutils.RmDir(GetSysrootPath(SCRIPT_STORAGE_PATH), ignore_missing=True) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 809 | |
| 810 | |
| 811 | class ToolManager(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 812 | """Class that installs or uninstalls fuzzing tools to/from the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 813 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 814 | Install and Uninstall methods are idempotent. Both are safe to call at any |
| 815 | point. |
| 816 | """ |
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 | # Path to asan_symbolize.py. |
| 819 | ASAN_SYMBOLIZE_PATH = os.path.join("/", "usr", "bin", "asan_symbolize.py") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 820 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 821 | # List of LLVM binaries we must install in sysroot. |
| 822 | LLVM_BINARY_NAMES = ["gdbserver", "llvm-symbolizer", "llvm-profdata"] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 823 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 824 | def __init__(self): |
| 825 | self.asan_symbolize_sysroot_path = GetSysrootPath( |
| 826 | self.ASAN_SYMBOLIZE_PATH |
| 827 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 828 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 829 | def Install(self): |
| 830 | """Installs tools to the sysroot.""" |
| 831 | # Install asan_symbolize.py. |
| 832 | sudo_run( |
| 833 | ["cp", self.ASAN_SYMBOLIZE_PATH, self.asan_symbolize_sysroot_path] |
| 834 | ) |
| 835 | # Install the LLVM binaries. |
| 836 | # TODO(metzman): Build these tools so that we don't mess up when board is |
| 837 | # for a different ISA. |
| 838 | for llvm_binary in self._GetLLVMBinaries(): |
| 839 | llvm_binary.Install() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 840 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 841 | def Uninstall(self): |
| 842 | """Uninstalls tools from the sysroot. Undoes Install.""" |
| 843 | # Uninstall asan_symbolize.py. |
| 844 | osutils.SafeUnlink(self.asan_symbolize_sysroot_path, sudo=True) |
| 845 | # Uninstall the LLVM binaries. |
| 846 | for llvm_binary in self._GetLLVMBinaries(): |
| 847 | llvm_binary.Uninstall() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 848 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 849 | def _GetLLVMBinaries(self): |
| 850 | """Creates LllvmBinary objects for each binary name in LLVM_BINARY_NAMES.""" |
| 851 | return [LlvmBinary(x) for x in self.LLVM_BINARY_NAMES] |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 852 | |
| 853 | |
| 854 | class LlvmBinary(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 855 | """Class for representing installing/uninstalling an LLVM binary in sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 856 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 857 | Install and Uninstall methods are idempotent. Both are safe to call at any |
| 858 | time. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 859 | """ |
Manoj Gupta | feb1b7a | 2019-02-20 11:04:05 -0800 | [diff] [blame] | 860 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 861 | # Path to the lddtree chromite script. |
Mike Frysinger | 164ec03 | 2023-03-27 16:15:14 -0400 | [diff] [blame] | 862 | LDDTREE_SCRIPT_PATH = constants.CHROMITE_BIN_DIR / "lddtree" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 863 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 864 | def __init__(self, binary): |
| 865 | self.binary = binary |
| 866 | self.install_dir = GetSysrootPath( |
| 867 | os.path.join("/", "usr", "libexec", binary) |
| 868 | ) |
| 869 | self.binary_dir_path = GetSysrootPath(os.path.join("/", "usr", "bin")) |
| 870 | self.binary_chroot_dest_path = os.path.join( |
| 871 | self.binary_dir_path, binary |
| 872 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 873 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 874 | def Uninstall(self): |
| 875 | """Removes an LLVM binary from sysroot. Undoes Install.""" |
| 876 | osutils.RmDir(self.install_dir, ignore_missing=True, sudo=True) |
| 877 | osutils.SafeUnlink(self.binary_chroot_dest_path, sudo=True) |
| 878 | |
| 879 | def Install(self): |
| 880 | """Installs (sets up) an LLVM binary in the sysroot. |
| 881 | |
| 882 | Sets up an llvm binary in the sysroot so that it can be run there. |
| 883 | """ |
| 884 | # Create a directory for installing |binary| and all of its dependencies in |
| 885 | # the sysroot. |
| 886 | binary_rel_path = ["usr", "bin", self.binary] |
| 887 | binary_chroot_path = os.path.join("/", *binary_rel_path) |
| 888 | if not os.path.exists(binary_chroot_path): |
| 889 | logging.warning( |
| 890 | "Cannot copy %s, file does not exist in chroot.", |
| 891 | binary_chroot_path, |
| 892 | ) |
| 893 | logging.warning( |
| 894 | "Functionality provided by %s will be missing.", |
| 895 | binary_chroot_path, |
| 896 | ) |
| 897 | return |
| 898 | |
| 899 | osutils.SafeMakedirsNonRoot(self.install_dir) |
| 900 | |
| 901 | # Copy the binary and everything needed to run it into the sysroot. |
| 902 | cmd = [ |
| 903 | self.LDDTREE_SCRIPT_PATH, |
| 904 | "-v", |
| 905 | "--generate-wrappers", |
| 906 | "--root", |
| 907 | "/", |
| 908 | "--copy-to-tree", |
| 909 | self.install_dir, |
| 910 | binary_chroot_path, |
| 911 | ] |
| 912 | sudo_run(cmd) |
| 913 | |
| 914 | # Create a symlink to the copy of the binary (we can't do lddtree in |
| 915 | # self.binary_dir_path). Note that symlink should be relative so that it |
| 916 | # will be valid when chrooted into the sysroot. |
| 917 | rel_path = os.path.relpath(self.install_dir, self.binary_dir_path) |
| 918 | link_path = os.path.join(rel_path, *binary_rel_path) |
| 919 | osutils.SafeSymlink(link_path, self.binary_chroot_dest_path, sudo=True) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 920 | |
| 921 | |
| 922 | class DeviceManager(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 923 | """Class that creates or removes devices from /dev in sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 924 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 925 | 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] | 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 | DEVICE_MKNOD_PARAMS = { |
| 929 | "null": (666, 3), |
| 930 | "random": (444, 8), |
| 931 | "urandom": (444, 9), |
| 932 | } |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 933 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 934 | MKNOD_MAJOR = "1" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 935 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 936 | def __init__(self): |
| 937 | self.dev_path_chroot = GetSysrootPath("/dev") |
| 938 | |
| 939 | def _GetDevicePath(self, device_name): |
| 940 | """Returns the path of |device_name| in sysroot's /dev.""" |
| 941 | return os.path.join(self.dev_path_chroot, device_name) |
| 942 | |
| 943 | def SetUp(self): |
| 944 | """Sets up devices in the sysroot's /dev. |
| 945 | |
| 946 | Creates /dev/null, /dev/random, and /dev/urandom. If they already exist then |
| 947 | recreates them. |
| 948 | """ |
| 949 | self.CleanUp() |
| 950 | osutils.SafeMakedirsNonRoot(self.dev_path_chroot) |
| 951 | for device, mknod_params in self.DEVICE_MKNOD_PARAMS.items(): |
| 952 | device_path = self._GetDevicePath(device) |
| 953 | self._MakeCharDevice(device_path, *mknod_params) |
| 954 | |
| 955 | def CleanUp(self): |
| 956 | """Cleans up devices in the sysroot's /dev. Undoes SetUp. |
| 957 | |
| 958 | Removes /dev/null, /dev/random, and /dev/urandom if they exist. |
| 959 | """ |
| 960 | for device in self.DEVICE_MKNOD_PARAMS: |
| 961 | device_path = self._GetDevicePath(device) |
| 962 | if os.path.exists(device_path): |
| 963 | # Use -r since dev/null is sometimes a directory. |
| 964 | sudo_run(["rm", "-r", device_path]) |
| 965 | |
| 966 | def _MakeCharDevice(self, path, mode, minor): |
| 967 | """Make a character device.""" |
| 968 | mode = str(mode) |
| 969 | minor = str(minor) |
| 970 | command = ["mknod", "-m", mode, path, "c", self.MKNOD_MAJOR, minor] |
| 971 | sudo_run(command) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 972 | |
| 973 | |
| 974 | class ProcManager(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 975 | """Class that mounts or unmounts /proc in sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 976 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 977 | Mount and Unmount are idempotent. Both are safe to call at any point. |
| 978 | """ |
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 | PROC_PATH = "/proc" |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 981 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 982 | def __init__(self): |
| 983 | self.proc_path_chroot = GetSysrootPath(self.PROC_PATH) |
| 984 | self.is_mounted = osutils.IsMounted(self.proc_path_chroot) |
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 Unmount(self): |
| 987 | """Unmounts /proc in chroot. Undoes Mount.""" |
| 988 | if not self.is_mounted: |
| 989 | return |
| 990 | osutils.UmountDir(self.proc_path_chroot, cleanup=False) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 991 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 992 | def Mount(self): |
| 993 | """Mounts /proc in chroot. Remounts it if already mounted.""" |
| 994 | self.Unmount() |
| 995 | osutils.MountDir( |
| 996 | self.PROC_PATH, |
| 997 | self.proc_path_chroot, |
| 998 | "proc", |
| 999 | debug_level=logging.DEBUG, |
| 1000 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1001 | |
| 1002 | |
| 1003 | def EnterSysrootShell(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1004 | """Spawns and gives user access to a bash shell in the sysroot.""" |
| 1005 | command = ["/bin/bash", "-i"] |
| 1006 | return RunSysrootCommand( |
| 1007 | command, |
| 1008 | extra_env=GetFuzzExtraEnv(), |
| 1009 | debug_level=logging.INFO, |
| 1010 | check=False, |
| 1011 | ).returncode |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1012 | |
| 1013 | |
| 1014 | def StripFuzzerPrefixes(fuzzer_name): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1015 | """Strip the prefix ClusterFuzz uses in case they are specified. |
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 | Strip the prefixes used by ClusterFuzz if the users has included them by |
| 1018 | accident. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1019 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1020 | Args: |
| 1021 | fuzzer_name: The fuzzer who's name may contain prefixes. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1022 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1023 | Returns: |
| 1024 | The name of the fuzz target without prefixes. |
| 1025 | """ |
| 1026 | initial_name = fuzzer_name |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1027 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1028 | def StripPrefix(prefix): |
| 1029 | if fuzzer_name.startswith(prefix): |
| 1030 | return fuzzer_name[len(prefix) :] |
| 1031 | return fuzzer_name |
| 1032 | |
| 1033 | clusterfuzz_prefixes = ["libFuzzer_", "chromeos_"] |
| 1034 | |
| 1035 | for prefix in clusterfuzz_prefixes: |
| 1036 | fuzzer_name = StripPrefix(prefix) |
| 1037 | |
| 1038 | if initial_name != fuzzer_name: |
| 1039 | logging.warning( |
| 1040 | "%s contains a prefix from ClusterFuzz (one or more of %s) that is not " |
| 1041 | "part of the fuzzer's name. Interpreting --fuzzer as %s.", |
| 1042 | initial_name, |
| 1043 | clusterfuzz_prefixes, |
| 1044 | fuzzer_name, |
| 1045 | ) |
| 1046 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1047 | return fuzzer_name |
| 1048 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1049 | |
| 1050 | def ExecuteShellCommand(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1051 | """Executes the "shell" command. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1052 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1053 | Sets up the sysroot for fuzzing and gives user access to a bash shell it |
| 1054 | spawns in the sysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1055 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1056 | Returns: |
| 1057 | The exit code of the shell command. |
| 1058 | """ |
| 1059 | SetUpSysrootForFuzzing() |
| 1060 | return EnterSysrootShell() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1061 | |
| 1062 | |
| 1063 | def ExecuteSetupCommand(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1064 | """Executes the "setup" command. Wrapper for SetUpSysrootForFuzzing. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1065 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1066 | Sets up the sysroot for fuzzing. |
| 1067 | """ |
| 1068 | SetUpSysrootForFuzzing() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1069 | |
| 1070 | |
| 1071 | def ExecuteCleanupCommand(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1072 | """Executes the "cleanup" command. Wrapper for CleanUpSysroot. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1073 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1074 | Undoes pre-fuzzing setup. |
| 1075 | """ |
| 1076 | CleanUpSysroot() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1077 | |
| 1078 | |
| 1079 | def ExecuteCoverageCommand(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1080 | """Executes the "coverage" command. |
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 | Executes the "coverage" command by optionally doing a coverage build of a |
| 1083 | package, optionally downloading the fuzzer's corpus, optionally copying it |
| 1084 | into the sysroot, running the fuzzer and then generating a coverage report |
| 1085 | for the user to view. Causes program to exit if fuzzer is not instrumented |
| 1086 | with source based coverage. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1087 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1088 | Args: |
| 1089 | options: The parsed arguments passed to this program. |
| 1090 | """ |
| 1091 | BuildPackage(options.package, options.board, BuildType.COVERAGE) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1092 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1093 | fuzzer = StripFuzzerPrefixes(options.fuzzer) |
| 1094 | fuzzer_sysroot_path = GetFuzzerSysrootPath(fuzzer) |
| 1095 | if not IsInstrumentedWithClangCoverage(fuzzer_sysroot_path.chroot): |
| 1096 | # Don't run the fuzzer if it isn't instrumented with source based coverage. |
| 1097 | # Quit and let the user know how to build the fuzzer properly. |
| 1098 | cros_build_lib.Die( |
| 1099 | "%s is not instrumented with source based coverage.\nSpecify --package " |
| 1100 | 'to do a coverage build or build with USE flag: "coverage".', |
| 1101 | fuzzer, |
| 1102 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1103 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1104 | corpus = options.corpus |
| 1105 | if options.download: |
| 1106 | corpus = DownloadFuzzerCorpus(options.fuzzer) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1107 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1108 | # Set up sysroot for fuzzing. |
| 1109 | SetUpSysrootForFuzzing() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1110 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1111 | coverage_report_path = RunFuzzerAndGenerateCoverageReport( |
| 1112 | fuzzer, corpus, options.fuzz_args |
| 1113 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1114 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1115 | # Get path on host so user can access it with their browser. |
| 1116 | # TODO(metzman): Add the ability to convert to host paths to path_util. |
| 1117 | external_trunk_path = os.getenv("EXTERNAL_TRUNK_PATH") |
| 1118 | coverage_report_host_path = os.path.join( |
| 1119 | external_trunk_path, "chroot", coverage_report_path.chroot[1:] |
| 1120 | ) |
| 1121 | print( |
| 1122 | "Coverage report written to file://%s/index.html" |
| 1123 | % coverage_report_host_path |
| 1124 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1125 | |
| 1126 | |
| 1127 | def ExecuteDownloadCommand(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1128 | """Executes the "download" command. Wrapper around DownloadFuzzerCorpus.""" |
| 1129 | DownloadFuzzerCorpus(StripFuzzerPrefixes(options.fuzzer), options.directory) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1130 | |
| 1131 | |
| 1132 | def ExecuteReproduceCommand(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1133 | """Executes the "reproduce" command. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1134 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1135 | Executes the "reproduce" command by Running a fuzzer on a testcase. |
| 1136 | May build the fuzzer before running. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1137 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1138 | Args: |
| 1139 | options: The parsed arguments passed to this program. |
| 1140 | """ |
| 1141 | if options.build_type and not options.package: |
| 1142 | raise Exception( |
| 1143 | "Cannot specify --build_type without specifying --package." |
| 1144 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1146 | # Verify that "msan-fuzzer" profile is being used with msan. |
| 1147 | # Check presence of "-fsanitize=memory" in CFLAGS. |
| 1148 | if options.build_type == BuildType.MSAN: |
| 1149 | cmd = ["portageq-%s" % options.board, "envvar", "CFLAGS"] |
| 1150 | cflags = cros_build_lib.run( |
| 1151 | cmd, capture_output=True, encoding="utf-8" |
| 1152 | ).stdout.splitlines() |
| 1153 | check_string = "-fsanitize=memory" |
| 1154 | if not any(check_string in s for s in cflags): |
| 1155 | logging.error( |
| 1156 | "-fsanitize=memory not found in CFLAGS. " |
| 1157 | 'Use "setup_board --board=amd64-generic --profile=msan-fuzzer" ' |
| 1158 | "for MSan Fuzzing Builds." |
| 1159 | ) |
| 1160 | raise Exception("Incompatible profile used for msan fuzzing.") |
Manoj Gupta | 5ca1765 | 2019-05-13 11:15:33 -0700 | [diff] [blame] | 1161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1162 | BuildPackage(options.package, options.board, options.build_type) |
| 1163 | SetUpSysrootForFuzzing() |
| 1164 | Reproduce(StripFuzzerPrefixes(options.fuzzer), options.testcase) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1165 | |
Manoj Gupta | e5e1e61 | 2019-10-21 12:39:57 -0700 | [diff] [blame] | 1166 | |
Manoj Gupta | ec08b81 | 2019-10-10 14:21:16 -0700 | [diff] [blame] | 1167 | def InstallBaseDependencies(options): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1168 | """Installs the base packages needed to chroot in board sysroot. |
Manoj Gupta | ec08b81 | 2019-10-10 14:21:16 -0700 | [diff] [blame] | 1169 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1170 | Args: |
| 1171 | options: The parsed arguments passed to this program. |
| 1172 | """ |
| 1173 | package = "virtual/implicit-system" |
| 1174 | if not portage_util.IsPackageInstalled( |
| 1175 | package, sysroot=SysrootPath.path_to_sysroot |
| 1176 | ): |
| 1177 | build_type = getattr(options, "build_type", None) |
| 1178 | BuildPackage(package, options.board, build_type) |
Manoj Gupta | e5e1e61 | 2019-10-21 12:39:57 -0700 | [diff] [blame] | 1179 | |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1180 | |
| 1181 | def ParseArgs(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1182 | """Parses program arguments. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1183 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1184 | Args: |
| 1185 | argv: The program arguments we want to parse. |
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 | Returns: |
| 1188 | An options object which will tell us which command to run and which options |
| 1189 | to use for that command. |
| 1190 | """ |
| 1191 | parser = commandline.ArgumentParser(description=__doc__) |
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 | parser.add_argument( |
| 1194 | "--board", |
| 1195 | default=cros_build_lib.GetDefaultBoard(), |
| 1196 | help="Board on which to run test.", |
| 1197 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1198 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1199 | subparsers = parser.add_subparsers(dest="command") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1200 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1201 | subparsers.add_parser("cleanup", help="Undo setup command.") |
| 1202 | coverage_parser = subparsers.add_parser( |
| 1203 | "coverage", help="Get a coverage report for a fuzzer." |
| 1204 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1205 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1206 | coverage_parser.add_argument("--package", help="Package to build.") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1207 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1208 | corpus_parser = coverage_parser.add_mutually_exclusive_group() |
| 1209 | corpus_parser.add_argument("--corpus", help="Corpus to run fuzzer on.") |
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 | corpus_parser.add_argument( |
| 1212 | "--download", |
| 1213 | action="store_true", |
| 1214 | help="Generate coverage report based on corpus from ClusterFuzz.", |
| 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 | "--fuzzer", |
| 1219 | required=True, |
| 1220 | help="The fuzz target to generate a coverage report for.", |
| 1221 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1222 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1223 | coverage_parser.add_argument( |
| 1224 | "--fuzz-args", |
| 1225 | default="", |
| 1226 | help="Arguments to pass libFuzzer. " |
| 1227 | "Please use an equals sign or parsing will fail " |
| 1228 | '(i.e. --fuzzer_args="-rss_limit_mb=2048 -print_funcs=1").', |
| 1229 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1230 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1231 | download_parser = subparsers.add_parser( |
| 1232 | "download", help="Download a corpus." |
| 1233 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1234 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1235 | download_parser.add_argument( |
| 1236 | "--directory", help="Path to directory to download the corpus to." |
| 1237 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1238 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1239 | download_parser.add_argument( |
| 1240 | "--fuzzer", required=True, help="Fuzzer to download the corpus for." |
| 1241 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1242 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1243 | reproduce_parser = subparsers.add_parser( |
| 1244 | "reproduce", help="Run a fuzzer on a testcase." |
| 1245 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1246 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1247 | reproduce_parser.add_argument( |
| 1248 | "--testcase", required=True, help="Path of testcase to run fuzzer on." |
| 1249 | ) |
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 | "--fuzzer", required=True, help="Fuzzer to reproduce the crash on." |
| 1253 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1254 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1255 | reproduce_parser.add_argument("--package", help="Package to build.") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1256 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1257 | reproduce_parser.add_argument( |
| 1258 | "--build-type", |
| 1259 | choices=BuildType.CHOICES, |
| 1260 | help="Type of build.", |
| 1261 | type=str.lower, |
| 1262 | ) # Ignore sanitizer case. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1263 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1264 | subparsers.add_parser("setup", help="Set up the sysroot to test fuzzing.") |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1265 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1266 | subparsers.add_parser( |
| 1267 | "shell", |
| 1268 | help="Set up sysroot for fuzzing and get a shell in the sysroot.", |
| 1269 | ) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1270 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1271 | opts = parser.parse_args(argv) |
| 1272 | opts.Freeze() |
| 1273 | return opts |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1274 | |
| 1275 | |
| 1276 | def main(argv): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1277 | """Parses arguments and executes a command. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1278 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1279 | Args: |
| 1280 | argv: The prorgram arguments. |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1281 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1282 | Returns: |
| 1283 | 0 on success. Non-zero on failure. |
| 1284 | """ |
| 1285 | cros_build_lib.AssertInsideChroot() |
| 1286 | options = ParseArgs(argv) |
| 1287 | if options.board is None: |
| 1288 | logging.error('Please specify "--board" or set ".default_board".') |
| 1289 | return 1 |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1290 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1291 | SysrootPath.SetPathToSysroot(options.board) |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1292 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1293 | InstallBaseDependencies(options) |
Manoj Gupta | ec08b81 | 2019-10-10 14:21:16 -0700 | [diff] [blame] | 1294 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1295 | if options.command == "cleanup": |
| 1296 | ExecuteCleanupCommand() |
| 1297 | elif options.command == "coverage": |
| 1298 | ExecuteCoverageCommand(options) |
| 1299 | elif options.command == "setup": |
| 1300 | ExecuteSetupCommand() |
| 1301 | elif options.command == "download": |
| 1302 | ExecuteDownloadCommand(options) |
| 1303 | elif options.command == "reproduce": |
| 1304 | ExecuteReproduceCommand(options) |
| 1305 | elif options.command == "shell": |
| 1306 | return ExecuteShellCommand() |
Jonathan Metzman | d5ee1c6 | 2018-11-05 10:33:08 -0800 | [diff] [blame] | 1307 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1308 | return 0 |