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

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](https://reviews.llvm.org/D84808#2208546).

Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D86733

GitOrigin-RevId: 711b9806547b0392ff636499cebfb73f72d4c595
diff --git a/FuzzerDriver.cpp b/FuzzerDriver.cpp
index 4669b12..2615014 100644
--- a/FuzzerDriver.cpp
+++ b/FuzzerDriver.cpp
@@ -250,11 +250,26 @@
   }
 }
 
-static void ValidateDirectoryExists(const std::string &Path) {
-  if (!Path.empty() && !IsDirectory(Path)) {
-    Printf("ERROR: The required directory \"%s\" does not exist\n", Path.c_str());
+static void ValidateDirectoryExists(const std::string &Path,
+                                    bool CreateDirectory) {
+  if (Path.empty()) {
+    Printf("ERROR: Provided directory path is an empty string\n");
     exit(1);
   }
+
+  if (IsDirectory(Path))
+    return;
+
+  if (CreateDirectory) {
+    if (!MkDirRecursive(Path)) {
+      Printf("ERROR: Failed to create directory \"%s\"\n", Path.c_str());
+      exit(1);
+    }
+    return;
+  }
+
+  Printf("ERROR: The required directory \"%s\" does not exist\n", Path.c_str());
+  exit(1);
 }
 
 std::string CloneArgsWithoutX(const Vector<std::string> &Args,
@@ -691,7 +706,7 @@
     std::string OutputCorpusDir = (*Inputs)[0];
     if (!IsFile(OutputCorpusDir)) {
       Options.OutputCorpus = OutputCorpusDir;
-      ValidateDirectoryExists(Options.OutputCorpus);
+      ValidateDirectoryExists(Options.OutputCorpus, Flags.create_missing_dirs);
     }
   }
   Options.ReportSlowUnits = Flags.report_slow_units;
@@ -705,11 +720,12 @@
     if (!IsSeparator(ArtifactPathDir[ArtifactPathDir.length() - 1])) {
       ArtifactPathDir = DirName(ArtifactPathDir);
     }
-    ValidateDirectoryExists(ArtifactPathDir);
+    ValidateDirectoryExists(ArtifactPathDir, Flags.create_missing_dirs);
   }
   if (Flags.exact_artifact_path) {
     Options.ExactArtifactPath = Flags.exact_artifact_path;
-    ValidateDirectoryExists(DirName(Options.ExactArtifactPath));
+    ValidateDirectoryExists(DirName(Options.ExactArtifactPath),
+                            Flags.create_missing_dirs);
   }
   Vector<Unit> Dictionary;
   if (Flags.dict)
@@ -735,7 +751,7 @@
     Options.DataFlowTrace = Flags.data_flow_trace;
   if (Flags.features_dir) {
     Options.FeaturesDir = Flags.features_dir;
-    ValidateDirectoryExists(Options.FeaturesDir);
+    ValidateDirectoryExists(Options.FeaturesDir, Flags.create_missing_dirs);
   }
   if (Flags.collect_data_flow)
     Options.CollectDataFlow = Flags.collect_data_flow;
diff --git a/FuzzerFlags.def b/FuzzerFlags.def
index 832224a..8114791 100644
--- a/FuzzerFlags.def
+++ b/FuzzerFlags.def
@@ -167,3 +167,7 @@
 FUZZER_FLAG_STRING(data_flow_trace, "Experimental: use the data flow trace")
 FUZZER_FLAG_STRING(collect_data_flow,
                    "Experimental: collect the data flow trace")
+
+FUZZER_FLAG_INT(create_missing_dirs, 0, "Automatically attempt to create "
+     "directories for arguments that would normally expect them to already "
+     "exist (i.e. artifact_prefix, exact_artifact_path, features_dir, corpus)")
diff --git a/FuzzerIO.cpp b/FuzzerIO.cpp
index cbb1dbe..c3330c3 100644
--- a/FuzzerIO.cpp
+++ b/FuzzerIO.cpp
@@ -144,6 +144,38 @@
   fflush(OutputFile);
 }
 
+static bool MkDirRecursiveInner(const std::string &Leaf) {
+  // Prevent chance of potential infinite recursion
+  if (Leaf == ".")
+    return true;
+
+  const std::string &Dir = DirName(Leaf);
+
+  if (IsDirectory(Dir)) {
+    MkDir(Leaf);
+    return IsDirectory(Leaf);
+  }
+
+  bool ret = MkDirRecursiveInner(Dir);
+  if (!ret) {
+    // Give up early if a previous MkDir failed
+    return ret;
+  }
+
+  MkDir(Leaf);
+  return IsDirectory(Leaf);
+}
+
+bool MkDirRecursive(const std::string &Dir) {
+  if (Dir.empty())
+    return false;
+
+  if (IsDirectory(Dir))
+    return true;
+
+  return MkDirRecursiveInner(Dir);
+}
+
 void RmDirRecursive(const std::string &Dir) {
   IterateDirRecursive(
       Dir, [](const std::string &Path) {},
diff --git a/FuzzerIO.h b/FuzzerIO.h
index 8def2e9..6e3a0b4 100644
--- a/FuzzerIO.h
+++ b/FuzzerIO.h
@@ -64,6 +64,7 @@
 void ListFilesInDirRecursive(const std::string &Dir, long *Epoch,
                              Vector<std::string> *V, bool TopDir);
 
+bool MkDirRecursive(const std::string &Dir);
 void RmDirRecursive(const std::string &Dir);
 
 // Iterate files and dirs inside Dir, recursively.