1 //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass renames everything with metasyntatic names. The intent is to use 11 // this pass after bugpoint reduction to conceal the nature of the original 12 // program. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/Analysis/TargetLibraryInfo.h" 19 #include "llvm/IR/DerivedTypes.h" 20 #include "llvm/IR/Function.h" 21 #include "llvm/IR/Module.h" 22 #include "llvm/IR/Type.h" 23 #include "llvm/IR/TypeFinder.h" 24 #include "llvm/Pass.h" 25 #include "llvm/Transforms/IPO.h" 26 using namespace llvm; 27 28 namespace { 29 30 // This PRNG is from the ISO C spec. It is intentionally simple and 31 // unsuitable for cryptographic use. We're just looking for enough 32 // variety to surprise and delight users. 33 struct PRNG { 34 unsigned long next; 35 36 void srand(unsigned int seed) { 37 next = seed; 38 } 39 40 int rand() { 41 next = next * 1103515245 + 12345; 42 return (unsigned int)(next / 65536) % 32768; 43 } 44 }; 45 46 static const char *const metaNames[] = { 47 // See http://en.wikipedia.org/wiki/Metasyntactic_variable 48 "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge", 49 "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam" 50 }; 51 52 struct Renamer { 53 Renamer(unsigned int seed) { 54 prng.srand(seed); 55 } 56 57 const char *newName() { 58 return metaNames[prng.rand() % array_lengthof(metaNames)]; 59 } 60 61 PRNG prng; 62 }; 63 64 struct MetaRenamer : public ModulePass { 65 static char ID; // Pass identification, replacement for typeid 66 MetaRenamer() : ModulePass(ID) { 67 initializeMetaRenamerPass(*PassRegistry::getPassRegistry()); 68 } 69 70 void getAnalysisUsage(AnalysisUsage &AU) const override { 71 AU.addRequired<TargetLibraryInfoWrapperPass>(); 72 AU.setPreservesAll(); 73 } 74 75 bool runOnModule(Module &M) override { 76 // Seed our PRNG with simple additive sum of ModuleID. We're looking to 77 // simply avoid always having the same function names, and we need to 78 // remain deterministic. 79 unsigned int randSeed = 0; 80 for (auto C : M.getModuleIdentifier()) 81 randSeed += C; 82 83 Renamer renamer(randSeed); 84 85 // Rename all aliases 86 for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) { 87 StringRef Name = AI->getName(); 88 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1)) 89 continue; 90 91 AI->setName("alias"); 92 } 93 94 // Rename all global variables 95 for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) { 96 StringRef Name = GI->getName(); 97 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1)) 98 continue; 99 100 GI->setName("global"); 101 } 102 103 // Rename all struct types 104 TypeFinder StructTypes; 105 StructTypes.run(M, true); 106 for (StructType *STy : StructTypes) { 107 if (STy->isLiteral() || STy->getName().empty()) continue; 108 109 SmallString<128> NameStorage; 110 STy->setName((Twine("struct.") + 111 renamer.newName()).toStringRef(NameStorage)); 112 } 113 114 // Rename all functions 115 const TargetLibraryInfo &TLI = 116 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 117 for (auto &F : M) { 118 StringRef Name = F.getName(); 119 LibFunc Tmp; 120 // Leave library functions alone because their presence or absence could 121 // affect the behavior of other passes. 122 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) || 123 TLI.getLibFunc(F, Tmp)) 124 continue; 125 126 F.setName(renamer.newName()); 127 runOnFunction(F); 128 } 129 return true; 130 } 131 132 bool runOnFunction(Function &F) { 133 for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI) 134 if (!AI->getType()->isVoidTy()) 135 AI->setName("arg"); 136 137 for (auto &BB : F) { 138 BB.setName("bb"); 139 140 for (auto &I : BB) 141 if (!I.getType()->isVoidTy()) 142 I.setName("tmp"); 143 } 144 return true; 145 } 146 }; 147 } 148 149 char MetaRenamer::ID = 0; 150 INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer", 151 "Assign new names to everything", false, false) 152 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 153 INITIALIZE_PASS_END(MetaRenamer, "metarenamer", 154 "Assign new names to everything", false, false) 155 //===----------------------------------------------------------------------===// 156 // 157 // MetaRenamer - Rename everything with metasyntactic names. 158 // 159 ModulePass *llvm::createMetaRenamerPass() { 160 return new MetaRenamer(); 161 } 162