[fuzzer] move default sanitizer options to a separate file
[oota-llvm.git] / tools / verify-uselistorder / verify-uselistorder.cpp
index 37d7f5f52a51234cbbbb7345db8a5d3b1bb98abc..a6536083e88011724ad91a06c9ffb938639459b0 100644 (file)
 // provided IR, this tool shuffles the use-lists and then writes and reads to a
 // separate Module whose use-list orders are compared to the original.
 //
+// The shuffles are deterministic, but guarantee that use-lists will change.
+// The algorithm per iteration is as follows:
+//
+//  1. Seed the random number generator.  The seed is different for each
+//     shuffle.  Shuffle 0 uses default+0, shuffle 1 uses default+1, and so on.
+//
+//  2. Visit every Value in a deterministic order.
+//
+//  3. Assign a random number to each Use in the Value's use-list in order.
+//
+//  4. If the numbers are already in order, reassign numbers until they aren't.
+//
+//  5. Sort the use-list using Value::sortUseList(), which is a stable sort.
+//
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/DenseSet.h"
 #include "llvm/AsmParser/Parser.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/UseListOrder.h"
+#include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
@@ -31,6 +47,8 @@
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/SystemUtils.h"
+#include <random>
+#include <vector>
 
 using namespace llvm;
 
@@ -105,10 +123,10 @@ bool TempFile::init(const std::string &Ext) {
 
 bool TempFile::writeBitcode(const Module &M) const {
   DEBUG(dbgs() << " - write bitcode\n");
-  std::string ErrorInfo;
-  raw_fd_ostream OS(Filename.c_str(), ErrorInfo, sys::fs::F_None);
-  if (!ErrorInfo.empty()) {
-    DEBUG(dbgs() << "error: " << ErrorInfo << "\n");
+  std::error_code EC;
+  raw_fd_ostream OS(Filename, EC, sys::fs::F_None);
+  if (EC) {
+    DEBUG(dbgs() << "error: " << EC.message() << "\n");
     return true;
   }
 
@@ -118,10 +136,10 @@ bool TempFile::writeBitcode(const Module &M) const {
 
 bool TempFile::writeAssembly(const Module &M) const {
   DEBUG(dbgs() << " - write assembly\n");
-  std::string ErrorInfo;
-  raw_fd_ostream OS(Filename.c_str(), ErrorInfo, sys::fs::F_Text);
-  if (!ErrorInfo.empty()) {
-    DEBUG(dbgs() << "error: " << ErrorInfo << "\n");
+  std::error_code EC;
+  raw_fd_ostream OS(Filename, EC, sys::fs::F_Text);
+  if (EC) {
+    DEBUG(dbgs() << "error: " << EC.message() << "\n");
     return true;
   }
 
@@ -138,8 +156,9 @@ std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
     return nullptr;
   }
 
-  std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOr.get());
-  ErrorOr<Module *> ModuleOr = parseBitcodeFile(Buffer.get(), Context);
+  MemoryBuffer *Buffer = BufferOr.get().get();
+  ErrorOr<Module *> ModuleOr =
+      parseBitcodeFile(Buffer->getMemBufferRef(), Context);
   if (!ModuleOr) {
     DEBUG(dbgs() << "error: " << ModuleOr.getError().message() << "\n");
     return nullptr;
@@ -150,7 +169,7 @@ std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
 std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
   DEBUG(dbgs() << " - read assembly\n");
   SMDiagnostic Err;
-  std::unique_ptr<Module> M(ParseAssemblyFile(Filename, Err, Context));
+  std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
   if (!M.get())
     DEBUG(dbgs() << "error: "; Err.print("verify-use-list-order", dbgs()));
   return M;
@@ -178,9 +197,12 @@ ValueMapping::ValueMapping(const Module &M) {
       map(G.getInitializer());
   for (const GlobalAlias &A : M.aliases())
     map(A.getAliasee());
-  for (const Function &F : M)
+  for (const Function &F : M) {
     if (F.hasPrefixData())
       map(F.getPrefixData());
+    if (F.hasPrologueData())
+      map(F.getPrologueData());
+  }
 
   // Function bodies.
   for (const Function &F : M) {
@@ -310,38 +332,183 @@ static bool matches(const ValueMapping &LM, const ValueMapping &RM) {
   return true;
 }
 
-static bool verifyBitcodeUseListOrder(const Module &M) {
-  DEBUG(dbgs() << "*** verify-use-list-order: bitcode ***\n");
+static void verifyAfterRoundTrip(const Module &M,
+                                 std::unique_ptr<Module> OtherM) {
+  if (!OtherM)
+    report_fatal_error("parsing failed");
+  if (verifyModule(*OtherM, &errs()))
+    report_fatal_error("verification failed");
+  if (!matches(ValueMapping(M), ValueMapping(*OtherM)))
+    report_fatal_error("use-list order changed");
+}
+static void verifyBitcodeUseListOrder(const Module &M) {
+  errs() << "*** verify-use-list-order: bitcode ***\n";
   TempFile F;
   if (F.init("bc"))
-    return false;
+    report_fatal_error("failed to initialize bitcode file");
 
   if (F.writeBitcode(M))
-    return false;
+    report_fatal_error("failed to write bitcode");
 
   LLVMContext Context;
-  std::unique_ptr<Module> OtherM = F.readBitcode(Context);
-  if (!OtherM)
-    return false;
-
-  return matches(ValueMapping(M), ValueMapping(*OtherM));
+  verifyAfterRoundTrip(M, F.readBitcode(Context));
 }
 
-static bool verifyAssemblyUseListOrder(const Module &M) {
-  DEBUG(dbgs() << "*** verify-use-list-order: assembly ***\n");
+static void verifyAssemblyUseListOrder(const Module &M) {
+  errs() << "*** verify-use-list-order: assembly ***\n";
   TempFile F;
   if (F.init("ll"))
-    return false;
+    report_fatal_error("failed to initialize assembly file");
 
   if (F.writeAssembly(M))
-    return false;
+    report_fatal_error("failed to write assembly");
 
   LLVMContext Context;
-  std::unique_ptr<Module> OtherM = F.readAssembly(Context);
-  if (!OtherM)
-    return false;
+  verifyAfterRoundTrip(M, F.readAssembly(Context));
+}
+
+static void verifyUseListOrder(const Module &M) {
+  verifyBitcodeUseListOrder(M);
+  verifyAssemblyUseListOrder(M);
+}
+
+static void shuffleValueUseLists(Value *V, std::minstd_rand0 &Gen,
+                                 DenseSet<Value *> &Seen) {
+  if (!Seen.insert(V).second)
+    return;
+
+  if (auto *C = dyn_cast<Constant>(V))
+    if (!isa<GlobalValue>(C))
+      for (Value *Op : C->operands())
+        shuffleValueUseLists(Op, Gen, Seen);
+
+  if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
+    // Nothing to shuffle for 0 or 1 users.
+    return;
+
+  // Generate random numbers between 10 and 99, which will line up nicely in
+  // debug output.  We're not worried about collisons here.
+  DEBUG(dbgs() << "V = "; V->dump());
+  std::uniform_int_distribution<short> Dist(10, 99);
+  SmallDenseMap<const Use *, short, 16> Order;
+  auto compareUses =
+      [&Order](const Use &L, const Use &R) { return Order[&L] < Order[&R]; };
+  do {
+    for (const Use &U : V->uses()) {
+      auto I = Dist(Gen);
+      Order[&U] = I;
+      DEBUG(dbgs() << " - order: " << I << ", op = " << U.getOperandNo()
+                   << ", U = ";
+            U.getUser()->dump());
+    }
+  } while (std::is_sorted(V->use_begin(), V->use_end(), compareUses));
+
+  DEBUG(dbgs() << " => shuffle\n");
+  V->sortUseList(compareUses);
+
+  DEBUG({
+    for (const Use &U : V->uses()) {
+      dbgs() << " - order: " << Order.lookup(&U)
+             << ", op = " << U.getOperandNo() << ", U = ";
+      U.getUser()->dump();
+    }
+  });
+}
+
+static void reverseValueUseLists(Value *V, DenseSet<Value *> &Seen) {
+  if (!Seen.insert(V).second)
+    return;
+
+  if (auto *C = dyn_cast<Constant>(V))
+    if (!isa<GlobalValue>(C))
+      for (Value *Op : C->operands())
+        reverseValueUseLists(Op, Seen);
+
+  if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
+    // Nothing to shuffle for 0 or 1 users.
+    return;
+
+  DEBUG({
+    dbgs() << "V = ";
+    V->dump();
+    for (const Use &U : V->uses()) {
+      dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
+      U.getUser()->dump();
+    }
+    dbgs() << " => reverse\n";
+  });
+
+  V->reverseUseList();
+
+  DEBUG({
+    for (const Use &U : V->uses()) {
+      dbgs() << " - order: op = " << U.getOperandNo() << ", U = ";
+      U.getUser()->dump();
+    }
+  });
+}
+
+template <class Changer>
+static void changeUseLists(Module &M, Changer changeValueUseList) {
+  // Visit every value that would be serialized to an IR file.
+  //
+  // Globals.
+  for (GlobalVariable &G : M.globals())
+    changeValueUseList(&G);
+  for (GlobalAlias &A : M.aliases())
+    changeValueUseList(&A);
+  for (Function &F : M)
+    changeValueUseList(&F);
 
-  return matches(ValueMapping(M), ValueMapping(*OtherM));
+  // Constants used by globals.
+  for (GlobalVariable &G : M.globals())
+    if (G.hasInitializer())
+      changeValueUseList(G.getInitializer());
+  for (GlobalAlias &A : M.aliases())
+    changeValueUseList(A.getAliasee());
+  for (Function &F : M) {
+    if (F.hasPrefixData())
+      changeValueUseList(F.getPrefixData());
+    if (F.hasPrologueData())
+      changeValueUseList(F.getPrologueData());
+  }
+
+  // Function bodies.
+  for (Function &F : M) {
+    for (Argument &A : F.args())
+      changeValueUseList(&A);
+    for (BasicBlock &BB : F)
+      changeValueUseList(&BB);
+    for (BasicBlock &BB : F)
+      for (Instruction &I : BB)
+        changeValueUseList(&I);
+
+    // Constants used by instructions.
+    for (BasicBlock &BB : F)
+      for (Instruction &I : BB)
+        for (Value *Op : I.operands())
+          if ((isa<Constant>(Op) && !isa<GlobalValue>(*Op)) ||
+              isa<InlineAsm>(Op))
+            changeValueUseList(Op);
+  }
+
+  if (verifyModule(M, &errs()))
+    report_fatal_error("verification failed");
+}
+
+static void shuffleUseLists(Module &M, unsigned SeedOffset) {
+  errs() << "*** shuffle-use-lists ***\n";
+  std::minstd_rand0 Gen(std::minstd_rand0::default_seed + SeedOffset);
+  DenseSet<Value *> Seen;
+  changeUseLists(M, [&](Value *V) { shuffleValueUseLists(V, Gen, Seen); });
+  DEBUG(dbgs() << "\n");
+}
+
+static void reverseUseLists(Module &M) {
+  errs() << "*** reverse-use-lists ***\n";
+  DenseSet<Value *> Seen;
+  changeUseLists(M, [&](Value *V) { reverseValueUseLists(V, Seen); });
+  DEBUG(dbgs() << "\n");
 }
 
 int main(int argc, char **argv) {
@@ -360,34 +527,41 @@ int main(int argc, char **argv) {
   SMDiagnostic Err;
 
   // Load the input module...
-  std::unique_ptr<Module> M;
-  M.reset(ParseIRFile(InputFilename, Err, Context));
+  std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
 
   if (!M.get()) {
     Err.print(argv[0], errs());
     return 1;
   }
+  if (verifyModule(*M, &errs()))
+    report_fatal_error("verification failed");
 
-  DEBUG(dbgs() << "*** verify-use-list-order ***\n");
+  errs() << "*** verify-use-list-order ***\n";
+  // Can't verify if order isn't preserved.
   if (!shouldPreserveBitcodeUseListOrder()) {
-    // Can't verify if order isn't preserved.
-    DEBUG(dbgs() << "warning: cannot verify bitcode; "
-                    "try -preserve-bc-use-list-order\n");
-    return 0;
+    errs() << "warning: forcing -preserve-bc-use-list-order\n";
+    setPreserveBitcodeUseListOrder(true);
   }
+  if (!shouldPreserveAssemblyUseListOrder()) {
+    errs() << "warning: forcing -preserve-ll-use-list-order\n";
+    setPreserveAssemblyUseListOrder(true);
+  }
+
+  // Verify the use lists now and after reversing them.
+  verifyUseListOrder(*M);
+  reverseUseLists(*M);
+  verifyUseListOrder(*M);
 
   for (unsigned I = 0, E = NumShuffles; I != E; ++I) {
-    DEBUG(dbgs() << "*** iteration: " << I << " ***\n");
+    errs() << "*** shuffle iteration: " << I + 1 << " of " << E << " ***\n";
 
-    // Shuffle with a different seed each time so that use-lists that aren't
-    // modified the first time are likely to be modified the next time.
+    // Shuffle with a different (deterministic) seed each time.
     shuffleUseLists(*M, I);
-    if (!verifyBitcodeUseListOrder(*M))
-      report_fatal_error("bitcode use-list order changed");
 
-    if (shouldPreserveAssemblyUseListOrder())
-      if (!verifyAssemblyUseListOrder(*M))
-        report_fatal_error("assembly use-list order changed");
+    // Verify again before and after reversing.
+    verifyUseListOrder(*M);
+    reverseUseLists(*M);
+    verifyUseListOrder(*M);
   }
 
   return 0;