about summary refs log tree commit diff
path: root/instrumentation/afl-llvm-pass.so.cc
diff options
context:
space:
mode:
authorhexcoder- <heiko@hexco.de>2021-10-12 23:02:15 +0200
committerhexcoder- <heiko@hexco.de>2021-10-12 23:02:15 +0200
commit544a65db5470359c18436eca123282d74fa47f2e (patch)
tree846d8e31f9405be36c3018c6d57d06c64af8c44a /instrumentation/afl-llvm-pass.so.cc
parent8e662898095ed6ba283a87119e383948b83b8d75 (diff)
downloadafl++-544a65db5470359c18436eca123282d74fa47f2e.tar.gz
converted afl-llvm-pass to new pass manager
Diffstat (limited to 'instrumentation/afl-llvm-pass.so.cc')
-rw-r--r--instrumentation/afl-llvm-pass.so.cc59
1 files changed, 48 insertions, 11 deletions
diff --git a/instrumentation/afl-llvm-pass.so.cc b/instrumentation/afl-llvm-pass.so.cc
index ecf28f31..c2b87ecb 100644
--- a/instrumentation/afl-llvm-pass.so.cc
+++ b/instrumentation/afl-llvm-pass.so.cc
@@ -45,12 +45,15 @@ typedef long double max_align_t;
 #endif
 
 #include "llvm/IR/IRBuilder.h"
-#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/Passes/PassPlugin.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/IR/PassManager.h"
+//#include "llvm/IR/LegacyPassManager.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MathExtras.h"
-#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+//#include "llvm/Transforms/IPO/PassManagerBuilder.h"
 
 #if LLVM_VERSION_MAJOR > 3 || \
     (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 4)
@@ -68,17 +71,18 @@ using namespace llvm;
 
 namespace {
 
-class AFLCoverage : public ModulePass {
+//class AFLCoverage : public ModulePass {
+class AFLCoverage : public PassInfoMixin<AFLCoverage> {
 
  public:
-  static char ID;
-  AFLCoverage() : ModulePass(ID) {
+//  static char ID;
+  AFLCoverage() {
 
     initInstrumentList();
 
   }
 
-  bool runOnModule(Module &M) override;
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
 
  protected:
   uint32_t    ngram_size = 0;
@@ -92,7 +96,38 @@ class AFLCoverage : public ModulePass {
 
 }  // namespace
 
-char AFLCoverage::ID = 0;
+extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
+llvmGetPassPluginInfo() {
+  return {
+    LLVM_PLUGIN_API_VERSION, "AFLCoverage", "v0.1",
+    /* lambda to insert our pass into the pass pipeline. */
+    [](PassBuilder &PB) {
+#if 1
+       using OptimizationLevel = typename PassBuilder::OptimizationLevel;
+       PB.registerOptimizerLastEPCallback(
+         [](ModulePassManager &MPM, OptimizationLevel OL) {
+           MPM.addPass(AFLCoverage());
+         }
+       );
+/* TODO LTO registration */
+#else
+       using PipelineElement = typename PassBuilder::PipelineElement;
+       PB.registerPipelineParsingCallback(
+         [](StringRef Name, ModulePassManager &MPM, ArrayRef<PipelineElement>) {
+            if ( Name == "AFLCoverage" ) {
+              MPM.addPass(AFLCoverage);
+              return true;
+            } else {
+              return false;
+            }
+         }
+       );
+#endif
+    }
+  };
+}
+
+//char AFLCoverage::ID = 0;
 
 /* needed up to 3.9.0 */
 #if LLVM_VERSION_MAJOR == 3 && \
@@ -118,7 +153,7 @@ uint64_t PowerOf2Ceil(unsigned in) {
     (LLVM_VERSION_MAJOR == 4 && LLVM_VERSION_PATCH >= 1)
   #define AFL_HAVE_VECTOR_INTRINSICS 1
 #endif
-bool AFLCoverage::runOnModule(Module &M) {
+PreservedAnalyses AFLCoverage::run(Module &M, ModuleAnalysisManager &MAM) {
 
   LLVMContext &C = M.getContext();
 
@@ -133,6 +168,8 @@ bool AFLCoverage::runOnModule(Module &M) {
   u32             rand_seed;
   unsigned int    cur_loc = 0;
 
+  auto PA = PreservedAnalyses::none();
+
   /* Setup random() so we get Actually Random(TM) outputs from AFL_R() */
   gettimeofday(&tv, &tz);
   rand_seed = tv.tv_sec ^ tv.tv_usec ^ getpid();
@@ -969,10 +1006,10 @@ bool AFLCoverage::runOnModule(Module &M) {
 
   }
 
-  return true;
+  return PA;
 
 }
-
+#if 0
 static void registerAFLPass(const PassManagerBuilder &,
                             legacy::PassManagerBase &PM) {
 
@@ -985,4 +1022,4 @@ static RegisterStandardPasses RegisterAFLPass(
 
 static RegisterStandardPasses RegisterAFLPass0(
     PassManagerBuilder::EP_EnabledOnOptLevel0, registerAFLPass);
-
+#endif