LLVM API Documentation

MetaRenamer.cpp
Go to the documentation of this file.
00001 //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass renames everything with metasyntatic names. The intent is to use
00011 // this pass after bugpoint reduction to conceal the nature of the original
00012 // program.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #include "llvm/Transforms/IPO.h"
00017 #include "llvm/ADT/STLExtras.h"
00018 #include "llvm/ADT/SmallString.h"
00019 #include "llvm/IR/DerivedTypes.h"
00020 #include "llvm/IR/Function.h"
00021 #include "llvm/IR/Module.h"
00022 #include "llvm/IR/Type.h"
00023 #include "llvm/IR/TypeFinder.h"
00024 #include "llvm/Pass.h"
00025 using namespace llvm;
00026 
00027 namespace {
00028 
00029   // This PRNG is from the ISO C spec. It is intentionally simple and
00030   // unsuitable for cryptographic use. We're just looking for enough
00031   // variety to surprise and delight users.
00032   struct PRNG {
00033     unsigned long next;
00034 
00035     void srand(unsigned int seed) {
00036       next = seed;
00037     }
00038 
00039     int rand() {
00040       next = next * 1103515245 + 12345;
00041       return (unsigned int)(next / 65536) % 32768;
00042     }
00043   };
00044 
00045   struct MetaRenamer : public ModulePass {
00046     static char ID; // Pass identification, replacement for typeid
00047     MetaRenamer() : ModulePass(ID) {
00048       initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
00049     }
00050 
00051     void getAnalysisUsage(AnalysisUsage &AU) const override {
00052       AU.setPreservesAll();
00053     }
00054 
00055     bool runOnModule(Module &M) override {
00056       static const char *const metaNames[] = {
00057         // See http://en.wikipedia.org/wiki/Metasyntactic_variable
00058         "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
00059         "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
00060       };
00061 
00062       // Seed our PRNG with simple additive sum of ModuleID. We're looking to
00063       // simply avoid always having the same function names, and we need to
00064       // remain deterministic.
00065       unsigned int randSeed = 0;
00066       for (std::string::const_iterator I = M.getModuleIdentifier().begin(),
00067            E = M.getModuleIdentifier().end(); I != E; ++I)
00068         randSeed += *I;
00069 
00070       PRNG prng;
00071       prng.srand(randSeed);
00072 
00073       // Rename all aliases
00074       for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
00075            AI != AE; ++AI) {
00076         StringRef Name = AI->getName();
00077         if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
00078           continue;
00079 
00080         AI->setName("alias");
00081       }
00082       
00083       // Rename all global variables
00084       for (Module::global_iterator GI = M.global_begin(), GE = M.global_end();
00085            GI != GE; ++GI) {
00086         StringRef Name = GI->getName();
00087         if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
00088           continue;
00089 
00090         GI->setName("global");
00091       }
00092 
00093       // Rename all struct types
00094       TypeFinder StructTypes;
00095       StructTypes.run(M, true);
00096       for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
00097         StructType *STy = StructTypes[i];
00098         if (STy->isLiteral() || STy->getName().empty()) continue;
00099 
00100         SmallString<128> NameStorage;
00101         STy->setName((Twine("struct.") + metaNames[prng.rand() %
00102                      array_lengthof(metaNames)]).toStringRef(NameStorage));
00103       }
00104 
00105       // Rename all functions
00106       for (Module::iterator FI = M.begin(), FE = M.end();
00107            FI != FE; ++FI) {
00108         StringRef Name = FI->getName();
00109         if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
00110           continue;
00111 
00112         FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]);
00113         runOnFunction(*FI);
00114       }
00115       return true;
00116     }
00117 
00118     bool runOnFunction(Function &F) {
00119       for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
00120            AI != AE; ++AI)
00121         if (!AI->getType()->isVoidTy())
00122           AI->setName("arg");
00123 
00124       for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
00125         BB->setName("bb");
00126 
00127         for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
00128           if (!I->getType()->isVoidTy())
00129             I->setName("tmp");
00130       }
00131       return true;
00132     }
00133   };
00134 }
00135 
00136 char MetaRenamer::ID = 0;
00137 INITIALIZE_PASS(MetaRenamer, "metarenamer", 
00138                 "Assign new names to everything", false, false)
00139 //===----------------------------------------------------------------------===//
00140 //
00141 // MetaRenamer - Rename everything with metasyntactic names.
00142 //
00143 ModulePass *llvm::createMetaRenamerPass() {
00144   return new MetaRenamer();
00145 }