Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/MetaRenamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
2
//
3
// 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
6
//
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
15
#include "llvm/ADT/STLExtras.h"
16
#include "llvm/ADT/SmallString.h"
17
#include "llvm/ADT/StringRef.h"
18
#include "llvm/ADT/Twine.h"
19
#include "llvm/Analysis/TargetLibraryInfo.h"
20
#include "llvm/IR/Argument.h"
21
#include "llvm/IR/BasicBlock.h"
22
#include "llvm/IR/DerivedTypes.h"
23
#include "llvm/IR/Function.h"
24
#include "llvm/IR/GlobalAlias.h"
25
#include "llvm/IR/GlobalVariable.h"
26
#include "llvm/IR/Instruction.h"
27
#include "llvm/IR/Module.h"
28
#include "llvm/IR/Type.h"
29
#include "llvm/IR/TypeFinder.h"
30
#include "llvm/Pass.h"
31
#include "llvm/Transforms/Utils.h"
32
33
using namespace llvm;
34
35
static 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
41
namespace {
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
2
    void srand(unsigned int seed) {
50
2
      next = seed;
51
2
    }
52
53
9
    int rand() {
54
9
      next = next * 1103515245 + 12345;
55
9
      return (unsigned int)(next / 65536) % 32768;
56
9
    }
57
  };
58
59
  struct Renamer {
60
2
    Renamer(unsigned int seed) {
61
2
      prng.srand(seed);
62
2
    }
63
64
9
    const char *newName() {
65
9
      return metaNames[prng.rand() % array_lengthof(metaNames)];
66
9
    }
67
68
    PRNG prng;
69
  };
70
71
  struct MetaRenamer : public ModulePass {
72
    // Pass identification, replacement for typeid
73
    static char ID;
74
75
2
    MetaRenamer() : ModulePass(ID) {
76
2
      initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
77
2
    }
78
79
2
    void getAnalysisUsage(AnalysisUsage &AU) const override {
80
2
      AU.addRequired<TargetLibraryInfoWrapperPass>();
81
2
      AU.setPreservesAll();
82
2
    }
83
84
2
    bool runOnModule(Module &M) override {
85
2
      // Seed our PRNG with simple additive sum of ModuleID. We're looking to
86
2
      // simply avoid always having the same function names, and we need to
87
2
      // remain deterministic.
88
2
      unsigned int randSeed = 0;
89
2
      for (auto C : M.getModuleIdentifier())
90
107
        randSeed += C;
91
2
92
2
      Renamer renamer(randSeed);
93
2
94
2
      // Rename all aliases
95
3
      for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; 
++AI1
) {
96
1
        StringRef Name = AI->getName();
97
1
        if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
98
0
          continue;
99
1
100
1
        AI->setName("alias");
101
1
      }
102
2
103
2
      // Rename all global variables
104
4
      for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; 
++GI2
) {
105
2
        StringRef Name = GI->getName();
106
2
        if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
107
0
          continue;
108
2
109
2
        GI->setName("global");
110
2
      }
111
2
112
2
      // Rename all struct types
113
2
      TypeFinder StructTypes;
114
2
      StructTypes.run(M, true);
115
2
      for (StructType *STy : StructTypes) {
116
2
        if (STy->isLiteral() || STy->getName().empty()) 
continue0
;
117
2
118
2
        SmallString<128> NameStorage;
119
2
        STy->setName((Twine("struct.") +
120
2
          renamer.newName()).toStringRef(NameStorage));
121
2
      }
122
2
123
2
      // Rename all functions
124
2
      const TargetLibraryInfo &TLI =
125
2
          getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
126
11
      for (auto &F : M) {
127
11
        StringRef Name = F.getName();
128
11
        LibFunc Tmp;
129
11
        // Leave library functions alone because their presence or absence could
130
11
        // affect the behavior of other passes.
131
11
        if (Name.startswith("llvm.") || 
(10
!Name.empty()10
&&
Name[0] == 110
) ||
132
11
            
TLI.getLibFunc(F, Tmp)10
)
133
3
          continue;
134
8
135
8
        // Leave @main alone. The output of -metarenamer might be passed to
136
8
        // lli for execution and the latter needs a main entry point.
137
8
        if (Name != "main")
138
7
          F.setName(renamer.newName());
139
8
140
8
        runOnFunction(F);
141
8
      }
142
2
      return true;
143
2
    }
144
145
8
    bool runOnFunction(Function &F) {
146
15
      for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; 
++AI7
)
147
7
        if (!AI->getType()->isVoidTy())
148
7
          AI->setName("arg");
149
8
150
12
      for (auto &BB : F) {
151
12
        BB.setName("bb");
152
12
153
12
        for (auto &I : BB)
154
65
          if (!I.getType()->isVoidTy())
155
35
            I.setName("tmp");
156
12
      }
157
8
      return true;
158
8
    }
159
  };
160
161
} // end anonymous namespace
162
163
char MetaRenamer::ID = 0;
164
165
11.0k
INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
166
11.0k
                      "Assign new names to everything", false, false)
167
11.0k
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
168
11.0k
INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
169
                    "Assign new names to everything", false, false)
170
171
//===----------------------------------------------------------------------===//
172
//
173
// MetaRenamer - Rename everything with metasyntactic names.
174
//
175
0
ModulePass *llvm::createMetaRenamerPass() {
176
0
  return new MetaRenamer();
177
0
}