blob: 481c6aa29c3a1d12c38cb6f7a856d9108f6a1a10 [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Transforms/IPO.h"
Alex Rosenbergaf2808c2012-09-14 19:19:57 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallString.h"
Bryant Wongdef79b22017-03-23 23:21:07 +000019#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Function.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/Type.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000024#include "llvm/IR/TypeFinder.h"
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000025#include "llvm/Pass.h"
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000026using namespace llvm;
27
28namespace {
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
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +000040 int rand() {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000041 next = next * 1103515245 + 12345;
42 return (unsigned int)(next / 65536) % 32768;
43 }
44 };
45
Alex Rosenberg99805ed2015-08-26 06:11:38 +000046 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
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000064 struct MetaRenamer : public ModulePass {
65 static char ID; // Pass identification, replacement for typeid
66 MetaRenamer() : ModulePass(ID) {
67 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
68 }
69
Craig Topper3e4c6972014-03-05 09:10:37 +000070 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bryant Wongdef79b22017-03-23 23:21:07 +000071 AU.addRequired<TargetLibraryInfoWrapperPass>();
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000072 AU.setPreservesAll();
73 }
74
Craig Topper3e4c6972014-03-05 09:10:37 +000075 bool runOnModule(Module &M) override {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000076 // 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;
Alex Rosenberg81cfed22015-08-26 06:11:41 +000080 for (auto C : M.getModuleIdentifier())
81 randSeed += C;
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000082
Alex Rosenberg99805ed2015-08-26 06:11:38 +000083 Renamer renamer(randSeed);
Alex Rosenberga0a19c12015-08-27 05:37:12 +000084
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000085 // Rename all aliases
Alex Rosenberg81cfed22015-08-26 06:11:41 +000086 for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000087 StringRef Name = AI->getName();
88 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
89 continue;
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000090
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000091 AI->setName("alias");
92 }
Alex Rosenberg5b3404a2015-08-26 06:11:36 +000093
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000094 // Rename all global variables
Alex Rosenberg81cfed22015-08-26 06:11:41 +000095 for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000096 StringRef Name = GI->getName();
97 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
98 continue;
99
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000100 GI->setName("global");
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000101 }
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000102
103 // Rename all struct types
104 TypeFinder StructTypes;
105 StructTypes.run(M, true);
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000106 for (StructType *STy : StructTypes) {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000107 if (STy->isLiteral() || STy->getName().empty()) continue;
108
109 SmallString<128> NameStorage;
Alex Rosenberg99805ed2015-08-26 06:11:38 +0000110 STy->setName((Twine("struct.") +
111 renamer.newName()).toStringRef(NameStorage));
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000112 }
113
114 // Rename all functions
Bryant Wongdef79b22017-03-23 23:21:07 +0000115 const TargetLibraryInfo &TLI =
116 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000117 for (auto &F : M) {
118 StringRef Name = F.getName();
Bryant Wongdef79b22017-03-23 23:21:07 +0000119 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))
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000124 continue;
125
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000126 F.setName(renamer.newName());
127 runOnFunction(F);
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000128 }
129 return true;
130 }
131
132 bool runOnFunction(Function &F) {
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000133 for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000134 if (!AI->getType()->isVoidTy())
135 AI->setName("arg");
136
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000137 for (auto &BB : F) {
138 BB.setName("bb");
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000139
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000140 for (auto &I : BB)
141 if (!I.getType()->isVoidTy())
142 I.setName("tmp");
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000143 }
144 return true;
145 }
146 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000147}
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000148
149char MetaRenamer::ID = 0;
Bryant Wongdef79b22017-03-23 23:21:07 +0000150INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
151 "Assign new names to everything", false, false)
152INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
153INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
154 "Assign new names to everything", false, false)
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000155//===----------------------------------------------------------------------===//
156//
157// MetaRenamer - Rename everything with metasyntactic names.
158//
159ModulePass *llvm::createMetaRenamerPass() {
160 return new MetaRenamer();
161}