blob: c999bd008fefd3006a64e2fd6df2c7fffc49e94d [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/Type.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000023#include "llvm/IR/TypeFinder.h"
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000024#include "llvm/Pass.h"
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000025using namespace llvm;
26
27namespace {
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
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +000039 int rand() {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000040 next = next * 1103515245 + 12345;
41 return (unsigned int)(next / 65536) % 32768;
42 }
43 };
44
Alex Rosenberg99805ed2015-08-26 06:11:38 +000045 static const char *const metaNames[] = {
46 // See http://en.wikipedia.org/wiki/Metasyntactic_variable
47 "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
48 "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
49 };
50
51 struct Renamer {
52 Renamer(unsigned int seed) {
53 prng.srand(seed);
54 }
55
56 const char *newName() {
57 return metaNames[prng.rand() % array_lengthof(metaNames)];
58 }
59
60 PRNG prng;
61 };
62
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000063 struct MetaRenamer : public ModulePass {
64 static char ID; // Pass identification, replacement for typeid
65 MetaRenamer() : ModulePass(ID) {
66 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
67 }
68
Craig Topper3e4c6972014-03-05 09:10:37 +000069 void getAnalysisUsage(AnalysisUsage &AU) const override {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000070 AU.setPreservesAll();
71 }
72
Craig Topper3e4c6972014-03-05 09:10:37 +000073 bool runOnModule(Module &M) override {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000074 // Seed our PRNG with simple additive sum of ModuleID. We're looking to
75 // simply avoid always having the same function names, and we need to
76 // remain deterministic.
77 unsigned int randSeed = 0;
Alex Rosenberg81cfed22015-08-26 06:11:41 +000078 for (auto C : M.getModuleIdentifier())
79 randSeed += C;
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000080
Alex Rosenberg99805ed2015-08-26 06:11:38 +000081 Renamer renamer(randSeed);
Alex Rosenberga0a19c12015-08-27 05:37:12 +000082
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000083 // Rename all aliases
Alex Rosenberg81cfed22015-08-26 06:11:41 +000084 for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000085 StringRef Name = AI->getName();
86 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
87 continue;
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000088
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000089 AI->setName("alias");
90 }
Alex Rosenberg5b3404a2015-08-26 06:11:36 +000091
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000092 // Rename all global variables
Alex Rosenberg81cfed22015-08-26 06:11:41 +000093 for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000094 StringRef Name = GI->getName();
95 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
96 continue;
97
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000098 GI->setName("global");
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000099 }
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000100
101 // Rename all struct types
102 TypeFinder StructTypes;
103 StructTypes.run(M, true);
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000104 for (StructType *STy : StructTypes) {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000105 if (STy->isLiteral() || STy->getName().empty()) continue;
106
107 SmallString<128> NameStorage;
Alex Rosenberg99805ed2015-08-26 06:11:38 +0000108 STy->setName((Twine("struct.") +
109 renamer.newName()).toStringRef(NameStorage));
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000110 }
111
112 // Rename all functions
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000113 for (auto &F : M) {
114 StringRef Name = F.getName();
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000115 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
116 continue;
117
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000118 F.setName(renamer.newName());
119 runOnFunction(F);
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000120 }
121 return true;
122 }
123
124 bool runOnFunction(Function &F) {
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000125 for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000126 if (!AI->getType()->isVoidTy())
127 AI->setName("arg");
128
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000129 for (auto &BB : F) {
130 BB.setName("bb");
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000131
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000132 for (auto &I : BB)
133 if (!I.getType()->isVoidTy())
134 I.setName("tmp");
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000135 }
136 return true;
137 }
138 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000139}
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000140
141char MetaRenamer::ID = 0;
142INITIALIZE_PASS(MetaRenamer, "metarenamer",
143 "Assign new names to everything", false, false)
144//===----------------------------------------------------------------------===//
145//
146// MetaRenamer - Rename everything with metasyntactic names.
147//
148ModulePass *llvm::createMetaRenamerPass() {
149 return new MetaRenamer();
150}