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