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/Transforms/IPO.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallString.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 using namespace llvm; 26 27 namespace { 28 29 // This PRNG is from the ISO C spec. It is intentionally simple and 30 // unsuitable for cryptographic use. We're just looking for enough 31 // variety to surprise and delight users. 32 struct PRNG { 33 unsigned long next; 34 35 void srand(unsigned int seed) { 36 next = seed; 37 } 38 39 int rand() { 40 next = next * 1103515245 + 12345; 41 return (unsigned int)(next / 65536) % 32768; 42 } 43 }; 44 45 struct MetaRenamer : public ModulePass { 46 static char ID; // Pass identification, replacement for typeid 47 MetaRenamer() : ModulePass(ID) { 48 initializeMetaRenamerPass(*PassRegistry::getPassRegistry()); 49 } 50 51 void getAnalysisUsage(AnalysisUsage &AU) const { 52 AU.setPreservesAll(); 53 } 54 55 bool runOnModule(Module &M) { 56 static const char *metaNames[] = { 57 // See http://en.wikipedia.org/wiki/Metasyntactic_variable 58 "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge", 59 "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam" 60 }; 61 62 // Seed our PRNG with simple additive sum of ModuleID. We're looking to 63 // simply avoid always having the same function names, and we need to 64 // remain deterministic. 65 unsigned int randSeed = 0; 66 for (std::string::const_iterator I = M.getModuleIdentifier().begin(), 67 E = M.getModuleIdentifier().end(); I != E; ++I) 68 randSeed += *I; 69 70 PRNG prng; 71 prng.srand(randSeed); 72 73 // Rename all aliases 74 for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end(); 75 AI != AE; ++AI) 76 AI->setName("alias"); 77 78 // Rename all global variables 79 for (Module::global_iterator GI = M.global_begin(), GE = M.global_end(); 80 GI != GE; ++GI) 81 GI->setName("global"); 82 83 // Rename all struct types 84 TypeFinder StructTypes; 85 StructTypes.run(M, true); 86 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) { 87 StructType *STy = StructTypes[i]; 88 if (STy->isLiteral() || STy->getName().empty()) continue; 89 90 SmallString<128> NameStorage; 91 STy->setName((Twine("struct.") + metaNames[prng.rand() % 92 array_lengthof(metaNames)]).toStringRef(NameStorage)); 93 } 94 95 // Rename all functions 96 for (Module::iterator FI = M.begin(), FE = M.end(); 97 FI != FE; ++FI) { 98 FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]); 99 runOnFunction(*FI); 100 } 101 return true; 102 } 103 104 bool runOnFunction(Function &F) { 105 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); 106 AI != AE; ++AI) 107 if (!AI->getType()->isVoidTy()) 108 AI->setName("arg"); 109 110 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { 111 BB->setName("bb"); 112 113 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 114 if (!I->getType()->isVoidTy()) 115 I->setName("tmp"); 116 } 117 return true; 118 } 119 }; 120 } 121 122 char MetaRenamer::ID = 0; 123 INITIALIZE_PASS(MetaRenamer, "metarenamer", 124 "Assign new names to everything", false, false) 125 //===----------------------------------------------------------------------===// 126 // 127 // MetaRenamer - Rename everything with metasyntactic names. 128 // 129 ModulePass *llvm::createMetaRenamerPass() { 130 return new MetaRenamer(); 131 } 132