blob: c0b7edc547fd3b8e5efa717dcea8338781f712b9 [file] [log] [blame]
Alex Rosenberg04b43aa2012-09-11 02:46:18 +00001//===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Rosenberg04b43aa2012-09-11 02:46:18 +00006//
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 Rosenbergaf2808c2012-09-14 19:19:57 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SmallString.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/Twine.h"
Bryant Wongdef79b22017-03-23 23:21:07 +000019#include "llvm/Analysis/TargetLibraryInfo.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000020#include "llvm/IR/Argument.h"
21#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Function.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000024#include "llvm/IR/GlobalAlias.h"
25#include "llvm/IR/GlobalVariable.h"
26#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Module.h"
28#include "llvm/IR/Type.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000029#include "llvm/IR/TypeFinder.h"
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000030#include "llvm/Pass.h"
David Blaikiea373d182018-03-28 17:44:36 +000031#include "llvm/Transforms/Utils.h"
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000032
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000033using namespace llvm;
34
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000035static 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 Rosenberg04b43aa2012-09-11 02:46:18 +000041namespace {
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 Gribenko0011bbf2012-11-15 16:51:49 +000053 int rand() {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000054 next = next * 1103515245 + 12345;
55 return (unsigned int)(next / 65536) % 32768;
56 }
57 };
58
Alex Rosenberg99805ed2015-08-26 06:11:38 +000059 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 Songf78650a2018-07-30 19:41:25 +000070
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000071 struct MetaRenamer : public ModulePass {
Eugene Zelenko57bd5a02017-10-27 01:09:08 +000072 // Pass identification, replacement for typeid
73 static char ID;
74
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000075 MetaRenamer() : ModulePass(ID) {
76 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
77 }
78
Craig Topper3e4c6972014-03-05 09:10:37 +000079 void getAnalysisUsage(AnalysisUsage &AU) const override {
Bryant Wongdef79b22017-03-23 23:21:07 +000080 AU.addRequired<TargetLibraryInfoWrapperPass>();
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000081 AU.setPreservesAll();
82 }
83
Craig Topper3e4c6972014-03-05 09:10:37 +000084 bool runOnModule(Module &M) override {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000085 // 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 Rosenberg81cfed22015-08-26 06:11:41 +000089 for (auto C : M.getModuleIdentifier())
90 randSeed += C;
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000091
Alex Rosenberg99805ed2015-08-26 06:11:38 +000092 Renamer renamer(randSeed);
Alex Rosenberga0a19c12015-08-27 05:37:12 +000093
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000094 // Rename all aliases
Alex Rosenberg81cfed22015-08-26 06:11:41 +000095 for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +000096 StringRef Name = AI->getName();
97 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
98 continue;
Alex Rosenberg04b43aa2012-09-11 02:46:18 +000099
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000100 AI->setName("alias");
101 }
Alex Rosenberg5b3404a2015-08-26 06:11:36 +0000102
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000103 // Rename all global variables
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000104 for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000105 StringRef Name = GI->getName();
106 if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
107 continue;
108
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000109 GI->setName("global");
Anton Korobeynikov4ec3ae72013-01-23 15:03:08 +0000110 }
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000111
112 // Rename all struct types
113 TypeFinder StructTypes;
114 StructTypes.run(M, true);
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000115 for (StructType *STy : StructTypes) {
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000116 if (STy->isLiteral() || STy->getName().empty()) continue;
117
118 SmallString<128> NameStorage;
Alex Rosenberg99805ed2015-08-26 06:11:38 +0000119 STy->setName((Twine("struct.") +
120 renamer.newName()).toStringRef(NameStorage));
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000121 }
122
123 // Rename all functions
Bryant Wongdef79b22017-03-23 23:21:07 +0000124 const TargetLibraryInfo &TLI =
125 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000126 for (auto &F : M) {
127 StringRef Name = F.getName();
Bryant Wongdef79b22017-03-23 23:21:07 +0000128 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 Korobeynikov4ec3ae72013-01-23 15:03:08 +0000133 continue;
134
Davide Italiano72c42852017-08-01 05:14:45 +0000135 // 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 Rosenberg81cfed22015-08-26 06:11:41 +0000140 runOnFunction(F);
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000141 }
142 return true;
143 }
144
145 bool runOnFunction(Function &F) {
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000146 for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000147 if (!AI->getType()->isVoidTy())
148 AI->setName("arg");
149
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000150 for (auto &BB : F) {
151 BB.setName("bb");
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000152
Alex Rosenberg81cfed22015-08-26 06:11:41 +0000153 for (auto &I : BB)
154 if (!I.getType()->isVoidTy())
155 I.setName("tmp");
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000156 }
157 return true;
158 }
159 };
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000160
161} // end anonymous namespace
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000162
163char MetaRenamer::ID = 0;
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000164
Bryant Wongdef79b22017-03-23 23:21:07 +0000165INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
166 "Assign new names to everything", false, false)
167INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
168INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
169 "Assign new names to everything", false, false)
Eugene Zelenko57bd5a02017-10-27 01:09:08 +0000170
Alex Rosenberg04b43aa2012-09-11 02:46:18 +0000171//===----------------------------------------------------------------------===//
172//
173// MetaRenamer - Rename everything with metasyntactic names.
174//
175ModulePass *llvm::createMetaRenamerPass() {
176 return new MetaRenamer();
177}