summaryrefslogtreecommitdiff
path: root/lib/Transforms/Utils/MetaRenamer.cpp
blob: f8f57b385bc7a01aeac6a28b3a8e8c7b066f09bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass renames everything with metasyntatic names. The intent is to use
// this pass after bugpoint reduction to conceal the nature of the original
// program.
//
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/IPO.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Pass.h"
#include "llvm/TypeFinder.h"

using namespace llvm;

namespace {

  // This PRNG is from the ISO C spec. It is intentionally simple and
  // unsuitable for cryptographic use. We're just looking for enough
  // variety to surprise and delight users.
  struct PRNG {
    unsigned long next;

    void srand(unsigned int seed) {
      next = seed;
    }

    int rand() {
      next = next * 1103515245 + 12345;
      return (unsigned int)(next / 65536) % 32768;
    }
  };

  struct MetaRenamer : public ModulePass {
    static char ID; // Pass identification, replacement for typeid
    MetaRenamer() : ModulePass(ID) {
      initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
    }

    void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.setPreservesAll();
    }

    bool runOnModule(Module &M) {
      static const char *metaNames[] = {
        // See http://en.wikipedia.org/wiki/Metasyntactic_variable
        "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
        "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
      };

      // Seed our PRNG with simple additive sum of ModuleID. We're looking to
      // simply avoid always having the same function names, and we need to
      // remain deterministic.
      unsigned int randSeed = 0;
      for (std::string::const_iterator I = M.getModuleIdentifier().begin(),
           E = M.getModuleIdentifier().end(); I != E; ++I)
        randSeed += *I;

      PRNG prng;
      prng.srand(randSeed);

      // Rename all aliases
      for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
           AI != AE; ++AI)
        AI->setName("alias");

      // Rename all global variables
      for (Module::global_iterator GI = M.global_begin(), GE = M.global_end();
           GI != GE; ++GI)
        GI->setName("global");

      // Rename all struct types
      TypeFinder StructTypes;
      StructTypes.run(M, true);
      for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
        StructType *STy = StructTypes[i];
        if (STy->isLiteral() || STy->getName().empty()) continue;

        SmallString<128> NameStorage;
        STy->setName((Twine("struct.") + metaNames[prng.rand() %
                     array_lengthof(metaNames)]).toStringRef(NameStorage));
      }

      // Rename all functions
      for (Module::iterator FI = M.begin(), FE = M.end();
           FI != FE; ++FI) {
        FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]);
        runOnFunction(*FI);
      }
      return true;
    }

    bool runOnFunction(Function &F) {
      for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
           AI != AE; ++AI)
        if (!AI->getType()->isVoidTy())
          AI->setName("arg");

      for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
        BB->setName("bb");

        for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
          if (!I->getType()->isVoidTy())
            I->setName("tmp");
      }
      return true;
    }
  };
}

char MetaRenamer::ID = 0;
INITIALIZE_PASS(MetaRenamer, "metarenamer", 
                "Assign new names to everything", false, false)
//===----------------------------------------------------------------------===//
//
// MetaRenamer - Rename everything with metasyntactic names.
//
ModulePass *llvm::createMetaRenamerPass() {
  return new MetaRenamer();
}