LLVM  3.7.0
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/Transforms/IPO.h"
17 #include "llvm/ADT/STLExtras.h"
18 #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 using namespace llvm;
26 
27 namespace {
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 
39  int rand() {
40  next = next * 1103515245 + 12345;
41  return (unsigned int)(next / 65536) % 32768;
42  }
43  };
44 
45  struct MetaRenamer : public ModulePass {
46  static char ID; // Pass identification, replacement for typeid
47  MetaRenamer() : ModulePass(ID) {
49  }
50 
51  void getAnalysisUsage(AnalysisUsage &AU) const override {
52  AU.setPreservesAll();
53  }
54 
55  bool runOnModule(Module &M) override {
56  static const char *const metaNames[] = {
57  // See http://en.wikipedia.org/wiki/Metasyntactic_variable
58  "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
59  "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
60  };
61 
62  // Seed our PRNG with simple additive sum of ModuleID. We're looking to
63  // simply avoid always having the same function names, and we need to
64  // remain deterministic.
65  unsigned int randSeed = 0;
66  for (std::string::const_iterator I = M.getModuleIdentifier().begin(),
67  E = M.getModuleIdentifier().end(); I != E; ++I)
68  randSeed += *I;
69 
70  PRNG prng;
71  prng.srand(randSeed);
72 
73  // Rename all aliases
74  for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
75  AI != AE; ++AI) {
76  StringRef Name = AI->getName();
77  if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
78  continue;
79 
80  AI->setName("alias");
81  }
82 
83  // Rename all global variables
85  GI != GE; ++GI) {
86  StringRef Name = GI->getName();
87  if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
88  continue;
89 
90  GI->setName("global");
91  }
92 
93  // Rename all struct types
94  TypeFinder StructTypes;
95  StructTypes.run(M, true);
96  for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
97  StructType *STy = StructTypes[i];
98  if (STy->isLiteral() || STy->getName().empty()) continue;
99 
100  SmallString<128> NameStorage;
101  STy->setName((Twine("struct.") + metaNames[prng.rand() %
102  array_lengthof(metaNames)]).toStringRef(NameStorage));
103  }
104 
105  // Rename all functions
106  for (Module::iterator FI = M.begin(), FE = M.end();
107  FI != FE; ++FI) {
108  StringRef Name = FI->getName();
109  if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
110  continue;
111 
112  FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]);
113  runOnFunction(*FI);
114  }
115  return true;
116  }
117 
118  bool runOnFunction(Function &F) {
119  for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
120  AI != AE; ++AI)
121  if (!AI->getType()->isVoidTy())
122  AI->setName("arg");
123 
124  for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
125  BB->setName("bb");
126 
127  for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
128  if (!I->getType()->isVoidTy())
129  I->setName("tmp");
130  }
131  return true;
132  }
133  };
134 }
135 
136 char MetaRenamer::ID = 0;
137 INITIALIZE_PASS(MetaRenamer, "metarenamer",
138  "Assign new names to everything", false, false)
139 //===----------------------------------------------------------------------===//
140 //
141 // MetaRenamer - Rename everything with metasyntactic names.
142 //
144  return new MetaRenamer();
145 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
iterator end()
Definition: Function.h:459
arg_iterator arg_end()
Definition: Function.h:480
F(f)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
StructType - Class to represent struct types.
Definition: DerivedTypes.h:191
bool isLiteral() const
isLiteral - Return true if this type is uniqued by structural equivalence, false if it is a struct de...
Definition: DerivedTypes.h:246
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:242
global_iterator global_begin()
Definition: Module.h:552
INITIALIZE_PASS(MetaRenamer,"metarenamer","Assign new names to everything", false, false) ModulePass *llvm
iterator begin()
Definition: Function.h:457
LLVM_CONSTEXPR size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:247
alias_iterator alias_end()
Definition: Module.h:593
Represent the analysis usage information of a pass.
arg_iterator arg_begin()
Definition: Function.h:472
global_iterator global_end()
Definition: Module.h:554
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
void run(const Module &M, bool onlyNamed)
Definition: TypeFinder.cpp:23
Module.h This file contains the declarations for the Module class.
alias_iterator alias_begin()
Definition: Module.h:591
StringRef getName() const
getName - Return the name for this struct type if it has an identity.
Definition: Type.cpp:583
void setPreservesAll()
Set by analyses that do not transform their input at all.
static StringRef toStringRef(bool B)
Construct a string ref from a boolean.
Definition: StringExtras.h:32
void setName(StringRef Name)
setName - Change the name of this type to the specified name, or to a name with a suffix if there is ...
Definition: Type.cpp:439
iterator end()
Definition: Module.h:571
#define I(x, y, z)
Definition: MD5.cpp:54
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
iterator begin()
Definition: Module.h:569
size_t size() const
Definition: TypeFinder.h:56
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void initializeMetaRenamerPass(PassRegistry &)
TypeFinder - Walk over a module, identifying all of the types that are used by the module...
Definition: TypeFinder.h:30
ModulePass * createMetaRenamerPass()
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110