Rename helper method for setup-relative files (#926)

We are going to use this for more than just target exes, and in fact, it applies to any file that must occur in the setup directory/container.
This commit is contained in:
Joe Ranweiler
2021-05-26 11:49:52 -07:00
committed by GitHub
parent 2f1fc61f01
commit d2084f70b1
9 changed files with 32 additions and 30 deletions

View File

@ -156,7 +156,7 @@ def main() -> None:
of.tasks.create(
helper.job.job_id,
TaskType.generic_crash_report,
helper.target_exe_blob_name(args.target_exe, args.setup_dir),
helper.setup_relative_blob_name(args.target_exe, args.setup_dir),
containers,
pool_name=args.pool_name,
duration=args.duration,
@ -201,7 +201,7 @@ def main() -> None:
of.tasks.create(
helper.job.job_id,
TaskType.generic_generator,
helper.target_exe_blob_name(args.target_exe, args.setup_dir),
helper.setup_relative_blob_name(args.target_exe, args.setup_dir),
containers,
pool_name=args.pool_name,
target_options=target_command,

View File

@ -101,7 +101,7 @@ def main() -> None:
of.tasks.create(
helper.job.job_id,
TaskType.generic_crash_report,
helper.target_exe_blob_name(args.target_exe, args.setup_dir),
helper.setup_relative_blob_name(args.target_exe, args.setup_dir),
containers,
pool_name=args.pool_name,
duration=args.duration,
@ -132,7 +132,7 @@ def main() -> None:
of.tasks.create(
helper.job.job_id,
TaskType.generic_supervisor,
helper.target_exe_blob_name(args.target_exe, args.setup_dir),
helper.setup_relative_blob_name(args.target_exe, args.setup_dir),
containers,
pool_name=args.pool_name,
supervisor_exe="/onefuzz/honggfuzz/honggfuzz",

View File

@ -307,28 +307,30 @@ class JobHelper:
wait_for_stopped=self.wait_for_stopped,
)
def target_exe_blob_name(
self, target_exe: File, setup_dir: Optional[Directory]
def setup_relative_blob_name(
self, local_file: File, setup_dir: Optional[Directory]
) -> str:
# The target executable must end up in the setup container, and the
# `target_exe` value passed to the tasks must be the name of the target
# exe _as a blob_ in the setup container.
# The local file must end up as a remote blob in the setup container. The blob
# name, which is a virtual `setup`-relative path, must be passed to the tasks as a
# value relative to the local copy of the setup directory.
if setup_dir:
# If the user set a `setup_dir`, then `target_exe` must occur inside
# of it. When we upload the `setup_dir`, the blob name agrees with
# the `setup_dir`-relative path of the target.
resolved = os.path.relpath(target_exe, setup_dir)
if resolved.startswith("..") or resolved == target_exe:
# If we have a `setup_dir`, then `local_file` must occur inside of it. When we
# upload the `setup_dir`, the blob name will match the `setup_dir`-relative
# path to the file.
resolved = os.path.relpath(local_file, setup_dir)
if resolved.startswith("..") or resolved == local_file:
raise ValueError(
"target_exe (%s) is not within the setup directory (%s)"
% (target_exe, setup_dir)
"local file (%s) is not within the setup directory (%s)"
% (local_file, setup_dir)
)
return resolved
else:
# If no `setup_dir` was given, we will upload `target_exe` to the
# root of the setup container created for the user. In that case,
# the `target_exe` name is just the filename of `target_exe`.
return os.path.basename(target_exe)
# If no `setup_dir` was given, we will upload the file at `local_file` to the
# root of the setup container created for the user. In that case, the future,
# setup-relative path to `local_file` is just the filename of `local_file`.
return os.path.basename(local_file)
def add_tags(self, tags: Optional[Dict[str, str]]) -> None:
if tags:

View File

@ -116,7 +116,7 @@ class AFL(Command):
):
raise Exception("AFL requires at least one input")
target_exe_blob_name = helper.target_exe_blob_name(target_exe, setup_dir)
target_exe_blob_name = helper.setup_relative_blob_name(target_exe, setup_dir)
if afl_container is None:
afl_container = Container(

View File

@ -270,7 +270,7 @@ class Libfuzzer(Command):
helper.upload_inputs(inputs)
helper.wait_on(wait_for_files, wait_for_running)
target_exe_blob_name = helper.target_exe_blob_name(target_exe, setup_dir)
target_exe_blob_name = helper.setup_relative_blob_name(target_exe, setup_dir)
self._create_tasks(
job=helper.job,
@ -378,7 +378,7 @@ class Libfuzzer(Command):
helper.upload_inputs(inputs)
helper.wait_on(wait_for_files, wait_for_running)
target_exe_blob_name = helper.target_exe_blob_name(target_exe, setup_dir)
target_exe_blob_name = helper.setup_relative_blob_name(target_exe, setup_dir)
merge_containers = [
(ContainerType.setup, helper.containers[ContainerType.setup]),
@ -619,7 +619,7 @@ class Libfuzzer(Command):
helper.create_containers()
target_exe_blob_name = helper.target_exe_blob_name(target_exe, None)
target_exe_blob_name = helper.setup_relative_blob_name(target_exe, None)
wrapper_name = File(target_exe_blob_name + "-wrapper.sh")
@ -627,7 +627,7 @@ class Libfuzzer(Command):
if sysroot:
setup_path = File(os.path.join(tempdir, "setup.sh"))
with open(setup_path, "w", newline="\n") as handle:
sysroot_filename = helper.target_exe_blob_name(sysroot, None)
sysroot_filename = helper.setup_relative_blob_name(sysroot, None)
handle.write(
"#!/bin/bash\n"
"set -ex\n"

View File

@ -236,7 +236,7 @@ class OssFuzz(Command):
# All fuzzers are copied to the setup container root.
#
# Cast because `glob()` returns `str`.
fuzzer_blob_name = helper.target_exe_blob_name(fuzzer, None)
fuzzer_blob_name = helper.setup_relative_blob_name(fuzzer, None)
self.onefuzz.template.libfuzzer._create_tasks(
job=base_helper.job,

View File

@ -112,7 +112,7 @@ class Radamsa(Command):
):
raise Exception("Radamsa requires at least one input file")
target_exe_blob_name = helper.target_exe_blob_name(target_exe, setup_dir)
target_exe_blob_name = helper.setup_relative_blob_name(target_exe, setup_dir)
tools = Container(
"radamsa-linux" if helper.platform == OS.linux else "radamsa-win64"

View File

@ -239,7 +239,7 @@ class Regression(Command):
helper.setup_notifications(notification_config)
helper.upload_setup(setup_dir, target_exe)
target_exe_blob_name = helper.target_exe_blob_name(target_exe, setup_dir)
target_exe_blob_name = helper.setup_relative_blob_name(target_exe, setup_dir)
self.logger.info("creating regression task")
task = self.onefuzz.tasks.create(

View File

@ -63,10 +63,10 @@ class TestHelper(unittest.TestCase):
values[(filename, Directory("c:\\unused\\"))] = expected
for (args, expected) in values.items():
self.assertEqual(helper.target_exe_blob_name(*args), expected)
self.assertEqual(helper.setup_relative_blob_name(*args), expected)
with self.assertRaises(ValueError):
helper.target_exe_blob_name(
helper.setup_relative_blob_name(
File("dir/filename.txt"), Directory("other_dir")
)