about summary refs log tree commit diff
path: root/utils/optimin/src/OptiMin.cpp
diff options
context:
space:
mode:
authorAdrian Herrera <adrian.herrera02@gmail.com>2021-07-23 08:23:29 +0000
committerAdrian Herrera <adrian.herrera02@gmail.com>2021-07-28 06:50:41 +0000
commit7123dd9d299206e7e21cbc8152abb0666acb170f (patch)
tree9a667950690d10c3331784e56592299a2cb64570 /utils/optimin/src/OptiMin.cpp
parent99d24d13f1ad972ab0645cf81155b47a7e693a42 (diff)
downloadafl++-7123dd9d299206e7e21cbc8152abb0666acb170f.tar.gz
optimin: improve afl-showmap error messages
Diffstat (limited to 'utils/optimin/src/OptiMin.cpp')
-rw-r--r--utils/optimin/src/OptiMin.cpp40
1 files changed, 23 insertions, 17 deletions
diff --git a/utils/optimin/src/OptiMin.cpp b/utils/optimin/src/OptiMin.cpp
index 55e391df..dca433cc 100644
--- a/utils/optimin/src/OptiMin.cpp
+++ b/utils/optimin/src/OptiMin.cpp
@@ -14,6 +14,7 @@
 #include <llvm/ADT/StringMap.h>
 #include <llvm/Support/Chrono.h>
 #include <llvm/Support/CommandLine.h>
+#include <llvm/Support/Error.h>
 #include <llvm/Support/FileSystem.h>
 #include <llvm/Support/MemoryBuffer.h>
 #include <llvm/Support/Path.h>
@@ -180,16 +181,18 @@ static void GetWeights(const MemoryBuffer &MB, WeightsMap &Weights) {
 
 }
 
-[[nodiscard]] static std::error_code getAFLCoverage(const StringRef    Seed,
-                                                    AFLCoverageVector &Cov) {
+static Error getAFLCoverage(const StringRef Seed, AFLCoverageVector &Cov) {
 
   Optional<StringRef> Redirects[] = {None, None, None};
-  std::error_code     EC;
 
   // Create temporary output file
-  SmallString<64> OutputPath;
-  if (EC = sys::fs::createTemporaryFile("showmap", "txt", OutputPath))
-    return EC;
+  SmallString<32> OutputPath;
+  if (const auto EC = sys::fs::createTemporaryFile("showmap", "txt", OutputPath)) {
+
+      return createStringError(
+          EC, "Failed to create temporary file for afl-showmap output");
+
+  }
 
   // Prepare afl-showmap arguments
   SmallVector<StringRef, 12> AFLShowmapArgs{
@@ -210,14 +213,16 @@ static void GetWeights(const MemoryBuffer &MB, WeightsMap &Weights) {
   // Run afl-showmap
   const int RC = sys::ExecuteAndWait(AFLShowmapPath, AFLShowmapArgs,
                                      /*env=*/None, Redirects);
-  if (RC) return std::make_error_code(std::errc::executable_format_error);
+  if (RC)
+    return createStringError(inconvertibleErrorCode(), "afl-showmap failed");
 
   // Parse afl-showmap output
   const auto CovOrErr = MemoryBuffer::getFile(OutputPath);
-  if (EC = CovOrErr.getError()) {
+  if (const auto EC = CovOrErr.getError()) {
 
     sys::fs::remove(OutputPath);
-    return EC;
+    return createStringError(EC, "Failed to read afl-showmap output file `%s`",
+                             OutputPath.c_str());
 
   }
 
@@ -237,7 +242,8 @@ static void GetWeights(const MemoryBuffer &MB, WeightsMap &Weights) {
 
   }
 
-  return sys::fs::remove(OutputPath);
+  sys::fs::remove(OutputPath);
+  return Error::success();
 
 }
 
@@ -278,7 +284,7 @@ int main(int argc, char *argv[]) {
 
   cl::ParseCommandLineOptions(argc, argv, "Optimal corpus minimizer");
 
-  if (EC = sys::fs::create_directory(OutputDir)) {
+  if ((EC = sys::fs::create_directory(OutputDir))) {
 
     ErrMsg() << "Invalid output directory `" << OutputDir
              << "`: " << EC.message() << '\n';
@@ -316,7 +322,7 @@ int main(int argc, char *argv[]) {
     StartTimer(/*ShowProgBar=*/false);
 
     const auto WeightsOrErr = MemoryBuffer::getFile(WeightsFile);
-    if (EC = WeightsOrErr.getError()) {
+    if ((EC = WeightsOrErr.getError())) {
 
       ErrMsg() << "Failed to read weights from `" << WeightsFile
                << "`: " << EC.message() << '\n';
@@ -354,7 +360,7 @@ int main(int argc, char *argv[]) {
     }
 
     const auto &Path = Dir->path();
-    if (EC = sys::fs::status(Path, Status)) {
+    if ((EC = sys::fs::status(Path, Status))) {
 
       WarnMsg() << "Failed to access seed file `" << Path
                 << "`: " << EC.message() << ". Skipping...\n";
@@ -402,10 +408,10 @@ int main(int argc, char *argv[]) {
 
     // Execute seed
     Cov.clear();
-    if (EC = getAFLCoverage(SeedFile, Cov)) {
+    if (auto Err = getAFLCoverage(SeedFile, Cov)) {
 
-      ErrMsg() << "Failed to get coverage for seed " << SeedFile << ": "
-               << EC.message() << '\n';
+      ErrMsg() << "Failed to get coverage for seed `" << SeedFile << "`: "
+               << Err << '\n';
       return 1;
 
     }
@@ -519,7 +525,7 @@ int main(int argc, char *argv[]) {
     OutputSeed = OutputDir;
     sys::path::append(OutputSeed, sys::path::filename(Seed));
 
-    if (EC = sys::fs::copy_file(Seed, OutputSeed)) {
+    if ((EC = sys::fs::copy_file(Seed, OutputSeed))) {
 
       WarnMsg() << "Failed to copy `" << Seed << "` to `" << OutputDir
                 << "`: " << EC.message() << '\n';