Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 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 | |
Alex Rosenberg | af2808c | 2012-09-14 19:19:57 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/Twine.h" |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Argument.h" |
| 22 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DerivedTypes.h" |
| 24 | #include "llvm/IR/Function.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 25 | #include "llvm/IR/GlobalAlias.h" |
| 26 | #include "llvm/IR/GlobalVariable.h" |
| 27 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Module.h" |
| 29 | #include "llvm/IR/Type.h" |
Chandler Carruth | dcb603f | 2013-01-07 15:43:51 +0000 | [diff] [blame] | 30 | #include "llvm/IR/TypeFinder.h" |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils.h" |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 33 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 36 | static const char *const metaNames[] = { |
| 37 | // See http://en.wikipedia.org/wiki/Metasyntactic_variable |
| 38 | "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge", |
| 39 | "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam" |
| 40 | }; |
| 41 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | // This PRNG is from the ISO C spec. It is intentionally simple and |
| 45 | // unsuitable for cryptographic use. We're just looking for enough |
| 46 | // variety to surprise and delight users. |
| 47 | struct PRNG { |
| 48 | unsigned long next; |
| 49 | |
| 50 | void srand(unsigned int seed) { |
| 51 | next = seed; |
| 52 | } |
| 53 | |
Dmitri Gribenko | 0011bbf | 2012-11-15 16:51:49 +0000 | [diff] [blame] | 54 | int rand() { |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 55 | next = next * 1103515245 + 12345; |
| 56 | return (unsigned int)(next / 65536) % 32768; |
| 57 | } |
| 58 | }; |
| 59 | |
Alex Rosenberg | 99805ed | 2015-08-26 06:11:38 +0000 | [diff] [blame] | 60 | struct Renamer { |
| 61 | Renamer(unsigned int seed) { |
| 62 | prng.srand(seed); |
| 63 | } |
| 64 | |
| 65 | const char *newName() { |
| 66 | return metaNames[prng.rand() % array_lengthof(metaNames)]; |
| 67 | } |
| 68 | |
| 69 | PRNG prng; |
| 70 | }; |
| 71 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 72 | struct MetaRenamer : public ModulePass { |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 73 | // Pass identification, replacement for typeid |
| 74 | static char ID; |
| 75 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 76 | MetaRenamer() : ModulePass(ID) { |
| 77 | initializeMetaRenamerPass(*PassRegistry::getPassRegistry()); |
| 78 | } |
| 79 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 80 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 81 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 82 | AU.setPreservesAll(); |
| 83 | } |
| 84 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 85 | bool runOnModule(Module &M) override { |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 86 | // Seed our PRNG with simple additive sum of ModuleID. We're looking to |
| 87 | // simply avoid always having the same function names, and we need to |
| 88 | // remain deterministic. |
| 89 | unsigned int randSeed = 0; |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 90 | for (auto C : M.getModuleIdentifier()) |
| 91 | randSeed += C; |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 92 | |
Alex Rosenberg | 99805ed | 2015-08-26 06:11:38 +0000 | [diff] [blame] | 93 | Renamer renamer(randSeed); |
Alex Rosenberg | a0a19c1 | 2015-08-27 05:37:12 +0000 | [diff] [blame] | 94 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 95 | // Rename all aliases |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 96 | for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) { |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 97 | StringRef Name = AI->getName(); |
| 98 | if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1)) |
| 99 | continue; |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 100 | |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 101 | AI->setName("alias"); |
| 102 | } |
Alex Rosenberg | 5b3404a | 2015-08-26 06:11:36 +0000 | [diff] [blame] | 103 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 104 | // Rename all global variables |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 105 | for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) { |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 106 | StringRef Name = GI->getName(); |
| 107 | if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1)) |
| 108 | continue; |
| 109 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 110 | GI->setName("global"); |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 111 | } |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 112 | |
| 113 | // Rename all struct types |
| 114 | TypeFinder StructTypes; |
| 115 | StructTypes.run(M, true); |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 116 | for (StructType *STy : StructTypes) { |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 117 | if (STy->isLiteral() || STy->getName().empty()) continue; |
| 118 | |
| 119 | SmallString<128> NameStorage; |
Alex Rosenberg | 99805ed | 2015-08-26 06:11:38 +0000 | [diff] [blame] | 120 | STy->setName((Twine("struct.") + |
| 121 | renamer.newName()).toStringRef(NameStorage)); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Rename all functions |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 125 | const TargetLibraryInfo &TLI = |
| 126 | getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 127 | for (auto &F : M) { |
| 128 | StringRef Name = F.getName(); |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 129 | LibFunc Tmp; |
| 130 | // Leave library functions alone because their presence or absence could |
| 131 | // affect the behavior of other passes. |
| 132 | if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) || |
| 133 | TLI.getLibFunc(F, Tmp)) |
Anton Korobeynikov | 4ec3ae7 | 2013-01-23 15:03:08 +0000 | [diff] [blame] | 134 | continue; |
| 135 | |
Davide Italiano | 72c4285 | 2017-08-01 05:14:45 +0000 | [diff] [blame] | 136 | // Leave @main alone. The output of -metarenamer might be passed to |
| 137 | // lli for execution and the latter needs a main entry point. |
| 138 | if (Name != "main") |
| 139 | F.setName(renamer.newName()); |
| 140 | |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 141 | runOnFunction(F); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | bool runOnFunction(Function &F) { |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 147 | for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI) |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 148 | if (!AI->getType()->isVoidTy()) |
| 149 | AI->setName("arg"); |
| 150 | |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 151 | for (auto &BB : F) { |
| 152 | BB.setName("bb"); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 153 | |
Alex Rosenberg | 81cfed2 | 2015-08-26 06:11:41 +0000 | [diff] [blame] | 154 | for (auto &I : BB) |
| 155 | if (!I.getType()->isVoidTy()) |
| 156 | I.setName("tmp"); |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 157 | } |
| 158 | return true; |
| 159 | } |
| 160 | }; |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 161 | |
| 162 | } // end anonymous namespace |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 163 | |
| 164 | char MetaRenamer::ID = 0; |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 165 | |
Bryant Wong | def79b2 | 2017-03-23 23:21:07 +0000 | [diff] [blame] | 166 | INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer", |
| 167 | "Assign new names to everything", false, false) |
| 168 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
| 169 | INITIALIZE_PASS_END(MetaRenamer, "metarenamer", |
| 170 | "Assign new names to everything", false, false) |
Eugene Zelenko | 57bd5a0 | 2017-10-27 01:09:08 +0000 | [diff] [blame] | 171 | |
Alex Rosenberg | 04b43aa | 2012-09-11 02:46:18 +0000 | [diff] [blame] | 172 | //===----------------------------------------------------------------------===// |
| 173 | // |
| 174 | // MetaRenamer - Rename everything with metasyntactic names. |
| 175 | // |
| 176 | ModulePass *llvm::createMetaRenamerPass() { |
| 177 | return new MetaRenamer(); |
| 178 | } |