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"
18#include "llvm/Transforms/IPO.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Function.h"
21#include "llvm/Module.h"
22#include "llvm/Pass.h"
23#include "llvm/Type.h"
24#include "llvm/TypeFinder.h"
25
26using 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
40    int rand(void) {
41      next = next * 1103515245 + 12345;
42      return (unsigned int)(next / 65536) % 32768;
43    }
44  };
45
46  struct MetaRenamer : public ModulePass {
47    static char ID; // Pass identification, replacement for typeid
48    MetaRenamer() : ModulePass(ID) {
49      initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
50    }
51
52    void getAnalysisUsage(AnalysisUsage &AU) const {
53      AU.setPreservesAll();
54    }
55
56    bool runOnModule(Module &M) {
57      static const char *metaNames[] = {
58        // See http://en.wikipedia.org/wiki/Metasyntactic_variable
59        "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
60        "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
61      };
62
63      // Seed our PRNG with simple additive sum of ModuleID. We're looking to
64      // simply avoid always having the same function names, and we need to
65      // remain deterministic.
66      unsigned int randSeed = 0;
67      for (std::string::const_iterator I = M.getModuleIdentifier().begin(),
68           E = M.getModuleIdentifier().end(); I != E; ++I)
69        randSeed += *I;
70
71      PRNG prng;
72      prng.srand(randSeed);
73
74      // Rename all aliases
75      for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
76           AI != AE; ++AI)
77        AI->setName("alias");
78
79      // Rename all global variables
80      for (Module::global_iterator GI = M.global_begin(), GE = M.global_end();
81           GI != GE; ++GI)
82        GI->setName("global");
83
84      // Rename all struct types
85      TypeFinder StructTypes;
86      StructTypes.run(M, true);
87      for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
88        StructType *STy = StructTypes[i];
89        if (STy->isLiteral() || STy->getName().empty()) continue;
90
91        SmallString<128> NameStorage;
92        STy->setName((Twine("struct.") + metaNames[prng.rand() %
93                     array_lengthof(metaNames)]).toStringRef(NameStorage));
94      }
95
96      // Rename all functions
97      for (Module::iterator FI = M.begin(), FE = M.end();
98           FI != FE; ++FI) {
99        FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]);
100        runOnFunction(*FI);
101      }
102      return true;
103    }
104
105    bool runOnFunction(Function &F) {
106      for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
107           AI != AE; ++AI)
108        if (!AI->getType()->isVoidTy())
109          AI->setName("arg");
110
111      for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
112        BB->setName("bb");
113
114        for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
115          if (!I->getType()->isVoidTy())
116            I->setName("tmp");
117      }
118      return true;
119    }
120  };
121}
122
123char MetaRenamer::ID = 0;
124INITIALIZE_PASS(MetaRenamer, "metarenamer",
125                "Assign new names to everything", false, false)
126//===----------------------------------------------------------------------===//
127//
128// MetaRenamer - Rename everything with metasyntactic names.
129//
130ModulePass *llvm::createMetaRenamerPass() {
131  return new MetaRenamer();
132}
133