LLVM  6.0.0svn
MetaRenamer.cpp
Go to the documentation of this file.
1 //===- 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 
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallString.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/IR/TypeFinder.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Transforms/IPO.h"
26 using namespace llvm;
27 
28 namespace {
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 
40  int rand() {
41  next = next * 1103515245 + 12345;
42  return (unsigned int)(next / 65536) % 32768;
43  }
44  };
45 
46  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 
64  struct MetaRenamer : public ModulePass {
65  static char ID; // Pass identification, replacement for typeid
66  MetaRenamer() : ModulePass(ID) {
68  }
69 
70  void getAnalysisUsage(AnalysisUsage &AU) const override {
72  AU.setPreservesAll();
73  }
74 
75  bool runOnModule(Module &M) override {
76  // 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;
80  for (auto C : M.getModuleIdentifier())
81  randSeed += C;
82 
83  Renamer renamer(randSeed);
84 
85  // Rename all aliases
86  for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
87  StringRef Name = AI->getName();
88  if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
89  continue;
90 
91  AI->setName("alias");
92  }
93 
94  // Rename all global variables
95  for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
96  StringRef Name = GI->getName();
97  if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
98  continue;
99 
100  GI->setName("global");
101  }
102 
103  // Rename all struct types
104  TypeFinder StructTypes;
105  StructTypes.run(M, true);
106  for (StructType *STy : StructTypes) {
107  if (STy->isLiteral() || STy->getName().empty()) continue;
108 
109  SmallString<128> NameStorage;
110  STy->setName((Twine("struct.") +
111  renamer.newName()).toStringRef(NameStorage));
112  }
113 
114  // Rename all functions
115  const TargetLibraryInfo &TLI =
116  getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
117  for (auto &F : M) {
118  StringRef Name = F.getName();
119  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))
124  continue;
125 
126  // Leave @main alone. The output of -metarenamer might be passed to
127  // lli for execution and the latter needs a main entry point.
128  if (Name != "main")
129  F.setName(renamer.newName());
130 
131  runOnFunction(F);
132  }
133  return true;
134  }
135 
136  bool runOnFunction(Function &F) {
137  for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
138  if (!AI->getType()->isVoidTy())
139  AI->setName("arg");
140 
141  for (auto &BB : F) {
142  BB.setName("bb");
143 
144  for (auto &I : BB)
145  if (!I.getType()->isVoidTy())
146  I.setName("tmp");
147  }
148  return true;
149  }
150  };
151 }
152 
153 char MetaRenamer::ID = 0;
154 INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
155  "Assign new names to everything", false, false)
158  "Assign new names to everything", false, false)
159 //===----------------------------------------------------------------------===//
160 //
161 // MetaRenamer - Rename everything with metasyntactic names.
162 //
164  return new MetaRenamer();
165 }
uint64_t CallInst * C
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
arg_iterator arg_end()
Definition: Function.h:612
F(f)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Class to represent struct types.
Definition: DerivedTypes.h:201
global_iterator global_begin()
Definition: Module.h:555
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
ModulePass * createMetaRenamerPass()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
alias_iterator alias_end()
Definition: Module.h:596
Represent the analysis usage information of a pass.
arg_iterator arg_begin()
Definition: Function.h:603
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:208
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
global_iterator global_end()
Definition: Module.h:557
void run(const Module &M, bool onlyNamed)
Definition: TypeFinder.cpp:32
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:729
alias_iterator alias_begin()
Definition: Module.h:594
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
static StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:42
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
Assign new names to everything
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void initializeMetaRenamerPass(PassRegistry &)
INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer", "Assign new names to everything", false, false) INITIALIZE_PASS_END(MetaRenamer
TypeFinder - Walk over a module, identifying all of the types that are used by the module...
Definition: TypeFinder.h:31
metarenamer