blob: 88d595ee02abe9695972ab1d347b3a7eb27d6178 [file] [log] [blame]
Alex Rosenberg04b43aa2012-09-11 02:46:18 +00001//===- 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 Rosenbergaf2808c2012-09-14 19:19:57 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SmallString.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000018#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
Bryant Wongdef79b22017-03-23 23:21:07 +000020#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000021#include "llvm/IR/Argument.h"
22#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Function.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000025#include "llvm/IR/GlobalAlias.h"
26#include "llvm/IR/GlobalVariable.h"
27#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Module.h"
29#include "llvm/IR/Type.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000030#include "llvm/IR/TypeFinder.h"
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000031#include "llvm/Pass.h"
David Blaikiea373d182018-03-28 17:44:36 +000032#include "llvm/Transforms/Utils.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000033
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000034using namespace llvm;
35
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000036static 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 Rosenberg04b43aa2012-09-11 02:46:18 +000042namespace {
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 Gribenko0011bbf2012-11-15 16:51:49 +000054 int rand() {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000055 next = next * 1103515245 + 12345;
56 return (unsigned int)(next / 65536) % 32768;
57 }
58 };
59
Alex Rosenberg99805ed2015-08-26 06:11:38 +000060 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 };
Fangrui Songf78650a2018-07-30 19:41:25 +000071
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000072 struct MetaRenamer : public ModulePass {
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000073 // Pass identification, replacement for typeid
74 static char ID;
75
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000076 MetaRenamer() : ModulePass(ID) {
77 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
78 }
79
Craig Topper3e4c6972014-03-05 09:10:37 +000080 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bryant Wongdef79b22017-03-23 23:21:07 +000081 AU.addRequired<TargetLibraryInfoWrapperPass>();
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000082 AU.setPreservesAll();
83 }
84
Craig Topper3e4c6972014-03-05 09:10:37 +000085 bool runOnModule(Module &M) override {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000086 // 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 Rosenberg81cfed22015-08-26 06:11:41 +000090 for (auto C : M.getModuleIdentifier())
91 randSeed += C;
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000092
Alex Rosenberg99805ed2015-08-26 06:11:38 +000093 Renamer renamer(randSeed);
Alex Rosenberga0a19c12015-08-27 05:37:12 +000094
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000095 // Rename all aliases
Alex Rosenberg81cfed22015-08-26 06:11:41 +000096 for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000097 StringRef Name = AI->getName();
98 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
99 continue;
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000100
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000101 AI->setName("alias");
102 }
Alex Rosenberg5b3404a2015-08-26 06:11:36 +0000103
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000104 // Rename all global variables
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000105 for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000106 StringRef Name = GI->getName();
107 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
108 continue;
109
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000110 GI->setName("global");
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000111 }
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000112
113 // Rename all struct types
114 TypeFinder StructTypes;
115 StructTypes.run(M, true);
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000116 for (StructType *STy : StructTypes) {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000117 if (STy->isLiteral() || STy->getName().empty()) continue;
118
119 SmallString<128> NameStorage;
Alex Rosenberg99805ed2015-08-26 06:11:38 +0000120 STy->setName((Twine("struct.") +
121 renamer.newName()).toStringRef(NameStorage));
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000122 }
123
124 // Rename all functions
Bryant Wongdef79b22017-03-23 23:21:07 +0000125 const TargetLibraryInfo &TLI =
126 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000127 for (auto &F : M) {
128 StringRef Name = F.getName();
Bryant Wongdef79b22017-03-23 23:21:07 +0000129 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 Korobeynikov4ec3ae72013-01-23 15:03:08 +0000134 continue;
135
Davide Italiano72c42852017-08-01 05:14:45 +0000136 // 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 Rosenberg81cfed22015-08-26 06:11:41 +0000141 runOnFunction(F);
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000142 }
143 return true;
144 }
145
146 bool runOnFunction(Function &F) {
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000147 for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000148 if (!AI->getType()->isVoidTy())
149 AI->setName("arg");
150
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000151 for (auto &BB : F) {
152 BB.setName("bb");
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000153
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000154 for (auto &I : BB)
155 if (!I.getType()->isVoidTy())
156 I.setName("tmp");
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000157 }
158 return true;
159 }
160 };
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000161
162} // end anonymous namespace
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000163
164char MetaRenamer::ID = 0;
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000165
Bryant Wongdef79b22017-03-23 23:21:07 +0000166INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
167 "Assign new names to everything", false, false)
168INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
169INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
170 "Assign new names to everything", false, false)
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000171
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000172//===----------------------------------------------------------------------===//
173//
174// MetaRenamer - Rename everything with metasyntactic names.
175//
176ModulePass *llvm::createMetaRenamerPass() {
177 return new MetaRenamer();
178}