This is an archive of the discontinued LLVM Phabricator instance.

[fuzzer] Create user provided fuzzer writeable directories when requested if they dont exist
ClosedPublic

Authored by dgg5503 on Aug 27 2020, 12:42 PM.

Details

Summary

Currently, libFuzzer will exit with an error message if a non-existent
directory is provided for any of the appropriate arguments. For cases
where libFuzzer is used in a specialized embedded environment, it would
be much easier to have libFuzzer create the directories for the user.

This patch accommodates for this scenario by allowing the user to provide
the argument -create_missing_dirs=1 which makes libFuzzer attempt to
create the artifact_prefix, exact_artifact_path,
features_dir and/or corpus directory if they don't already exist rather
than throw an error and exit.

Split off from D84808 as requested here.

Diff Detail

Event Timeline

dgg5503 created this revision.Aug 27 2020, 12:42 PM
Herald added a subscriber: Restricted Project. · View Herald TranscriptAug 27 2020, 12:42 PM
dgg5503 requested review of this revision.Aug 27 2020, 12:42 PM
morehouse added inline comments.Aug 31 2020, 10:07 AM
compiler-rt/lib/fuzzer/FuzzerDriver.cpp
254

s/const int/bool

255

Does this check break the case where we run the fuzzer without giving a corpus directory?

compiler-rt/lib/fuzzer/FuzzerIO.cpp
148

Nit: We really only need one parameter since we can compute DirName inside this helper function.

compiler-rt/test/fuzzer/fuzzer-dirs.test
28

s/emtpy/empty

36

Does chmod return an error on Windows? If so, this test will fail.

44

Do we expect dummy_dir to be created, or not? And should we test this?

dgg5503 updated this revision to Diff 289056.Aug 31 2020, 6:56 PM
dgg5503 marked 4 inline comments as done.

Addressed @morehouse comments:

  • const int to bool for argument in ValidateDirectoryExists
  • Simplified MkDirRecursiveInner
  • Spelling
compiler-rt/lib/fuzzer/FuzzerDriver.cpp
255

This case does not break since if no corpus directory is provided (i.e. Inputs is empty), ValidateDirectoryExists wont be called. This can be seen here.

compiler-rt/test/fuzzer/fuzzer-dirs.test
36

On Windows, I use chmod provided by Git bash (version 2.27.0.windows.1) which I believe is from MSYS2. Here's what I get when looking at the error code:

$ mkdir testchmod
$ chmod u-w testchmod/
$ echo $?
0

I've realized now that this approach may not guarantee a 0 exit code if some other set of Windows ported GNU tools were used which correctly reported a non-zero exit code. Do you happen to know if there's a more robust approach we can use in order to trigger directory creation failure on both Windows and Linux? If not, could we always ignore the exit code from chmod since we know what output to expect anyway?

44

My intention with this test was just to make sure ".." and "." in paths worked as expected when creating directories but I overlooked the fact that dummy_dir was also created, thanks for pointing this out. I'll see if there's an elegant way of preventing dummy_dir and the likes from being created without making too many additions.

dgg5503 marked 4 inline comments as not done.Aug 31 2020, 6:58 PM

Unchecked some mistakenly checked Dones.

morehouse added inline comments.Sep 1 2020, 8:34 AM
compiler-rt/test/fuzzer/fuzzer-dirs.test
36

I think ignoring the exit code from chmod is sufficient.

44

mkdir -p also creates dummy_dir, so I think the behavior is fine. But maybe we should test it.

dgg5503 updated this revision to Diff 289213.Sep 1 2020, 10:25 AM

Address @morehouse comments:

  • Ignore chmod exit code for improved compatibility when running the test fuzzer-dirs.test on Windows
  • Check for dummy_dir creation
dgg5503 marked 4 inline comments as done.Sep 1 2020, 10:27 AM
This revision is now accepted and ready to land.Sep 1 2020, 11:18 AM
morehouse reopened this revision.Sep 1 2020, 12:07 PM
This revision is now accepted and ready to land.Sep 1 2020, 12:07 PM
dgg5503 updated this revision to Diff 289342.Sep 2 2020, 8:48 AM

Improved test compatibility for Windows.

compiler-rt/test/fuzzer/fuzzer-dirs.test
36

I took some time to update my Windows environment to debug this test. It looks like I need to split (chmod u-w %t.dir/access_restricted || true) into its own run line, otherwise, Windows tries to find and call (chmod.exe. After doing this, this portion of the test passes fine.

45

Unfortunately, this test doesn't pass because dummy_dir isn't created on Windows like it is on Linux using the path %t.dir/subdirb/../dummy_dir/././../subdirb/features/. Interestingly, Windows considers the path %t.dir/subdirb/../dummy_dir/././.. as existing so directory creation for dummy_dir is skipped completely since leaf directories are created as soon as the first parent directory exists in MkDirRecursiveInner. If we use %t.dir/subdirb/dummy_dir/././../subdirb/features/ instead, we'll get the same behavior on both Windows and Linux.