llvm.org GIT mirror llvm / 95f1ef4
Add a pass that renames everything with metasyntatic names. This works well after using bugpoint to reduce the confusion presented by the original names, which no longer mean what they used to. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@163592 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Rosenberg 7 years ago
7 changed file(s) with 237 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
177177 void initializeMemCpyOptPass(PassRegistry&);
178178 void initializeMemDepPrinterPass(PassRegistry&);
179179 void initializeMemoryDependenceAnalysisPass(PassRegistry&);
180 void initializeMetaRenamerPass(PassRegistry&);
180181 void initializeMergeFunctionsPass(PassRegistry&);
181182 void initializeModuleDebugInfoPrinterPass(PassRegistry&);
182183 void initializeNoAAPass(PassRegistry&);
140140 (void) llvm::createLoopDeletionPass();
141141 (void) llvm::createPostDomTree();
142142 (void) llvm::createInstructionNamerPass();
143 (void) llvm::createMetaRenamerPass();
143144 (void) llvm::createFunctionAttrsPass();
144145 (void) llvm::createMergeFunctionsPass();
145146 (void) llvm::createPrintModulePass(0);
191191 /// createPartialInliningPass - This pass inlines parts of functions.
192192 ///
193193 ModulePass *createPartialInliningPass();
194
195 //===----------------------------------------------------------------------===//
196 // createMetaRenamerPass - Rename everything with metasyntatic names.
197 //
198 ModulePass *createMetaRenamerPass();
194199
195200 } // End llvm namespace
196201
0 //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
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/Transforms/IPO.h"
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Type.h"
20 #include "llvm/TypeFinder.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallString.h"
24
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(void) {
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) {
48 initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
49 }
50
51 void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesAll();
53 }
54
55 bool runOnModule(Module &M) {
56 static const char *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 AI->setName("alias");
77
78 // Rename all global variables
79 for (Module::global_iterator GI = M.global_begin(), GE = M.global_end();
80 GI != GE; ++GI)
81 GI->setName("global");
82
83 // Rename all struct types
84 TypeFinder StructTypes;
85 StructTypes.run(M, true);
86 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
87 StructType *STy = StructTypes[i];
88 if (STy->isLiteral() || STy->getName().empty()) continue;
89
90 SmallString<128> NameStorage;
91 STy->setName((Twine("struct.") + metaNames[prng.rand() %
92 array_lengthof(metaNames)]).toStringRef(NameStorage));
93 }
94
95 // Rename all functions
96 for (Module::iterator FI = M.begin(), FE = M.end();
97 FI != FE; ++FI) {
98 FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]);
99 runOnFunction(*FI);
100 }
101 return true;
102 }
103
104 bool runOnFunction(Function &F) {
105 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
106 AI != AE; ++AI)
107 if (!AI->getType()->isVoidTy())
108 AI->setName("arg");
109
110 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
111 BB->setName("bb");
112
113 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
114 if (!I->getType()->isVoidTy())
115 I->setName("tmp");
116 }
117 return true;
118 }
119 };
120 }
121
122 char MetaRenamer::ID = 0;
123 INITIALIZE_PASS(MetaRenamer, "metarenamer",
124 "Assign new names to everything", false, false)
125 //===----------------------------------------------------------------------===//
126 //
127 // MetaRenamer - Rename everything with metasyntactic names.
128 //
129 ModulePass *llvm::createMetaRenamerPass() {
130 return new MetaRenamer();
131 }
2828 initializePromotePassPass(Registry);
2929 initializeUnifyFunctionExitNodesPass(Registry);
3030 initializeInstSimplifierPass(Registry);
31 initializeMetaRenamerPass(Registry);
3132 }
3233
3334 /// LLVMInitializeTransformUtils - C binding for initializeTransformUtilsPasses.
0 config.suffixes = ['.ll', '.c', '.cpp']
0 ; RUN: opt %s -metarenamer -S | FileCheck %s
1
2 ; CHECK: target triple {{.*}}
3 ; CHECK-NOT: {{^x*}}xxx{{^x*}}
4 ; CHECK: ret i32 6
5
6 target triple = "x86_64-pc-linux-gnu"
7
8 %struct.bar_xxx = type { i32, double }
9 %struct.foo_xxx = type { i32, float, %struct.bar_xxx }
10
11 @func_5_xxx.static_local_3_xxx = internal global i32 3, align 4
12 @global_3_xxx = common global i32 0, align 4
13
14 @func_7_xxx = alias weak i32 (...)* @aliased_func_7_xxx
15
16 declare i32 @aliased_func_7_xxx(...)
17
18 define i32 @func_3_xxx() nounwind uwtable ssp {
19 ret i32 3
20 }
21
22 define void @func_4_xxx(%struct.foo_xxx* sret %agg.result) nounwind uwtable ssp {
23 %1 = alloca %struct.foo_xxx, align 8
24 %2 = getelementptr inbounds %struct.foo_xxx* %1, i32 0, i32 0
25 store i32 1, i32* %2, align 4
26 %3 = getelementptr inbounds %struct.foo_xxx* %1, i32 0, i32 1
27 store float 2.000000e+00, float* %3, align 4
28 %4 = getelementptr inbounds %struct.foo_xxx* %1, i32 0, i32 2
29 %5 = getelementptr inbounds %struct.bar_xxx* %4, i32 0, i32 0
30 store i32 3, i32* %5, align 4
31 %6 = getelementptr inbounds %struct.bar_xxx* %4, i32 0, i32 1
32 store double 4.000000e+00, double* %6, align 8
33 %7 = bitcast %struct.foo_xxx* %agg.result to i8*
34 %8 = bitcast %struct.foo_xxx* %1 to i8*
35 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %7, i8* %8, i64 24, i32 8, i1 false)
36 ret void
37 }
38
39 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
40
41 define i32 @func_5_xxx(i32 %arg_1_xxx, i32 %arg_2_xxx, i32 %arg_3_xxx, i32 %arg_4_xxx) nounwind uwtable ssp {
42 %1 = alloca i32, align 4
43 %2 = alloca i32, align 4
44 %3 = alloca i32, align 4
45 %4 = alloca i32, align 4
46 %local_1_xxx = alloca i32, align 4
47 %local_2_xxx = alloca i32, align 4
48 %i = alloca i32, align 4
49 store i32 %arg_1_xxx, i32* %1, align 4
50 store i32 %arg_2_xxx, i32* %2, align 4
51 store i32 %arg_3_xxx, i32* %3, align 4
52 store i32 %arg_4_xxx, i32* %4, align 4
53 store i32 1, i32* %local_1_xxx, align 4
54 store i32 2, i32* %local_2_xxx, align 4
55 store i32 0, i32* %i, align 4
56 br label %5
57
58 ;
59 %6 = load i32* %i, align 4
60 %7 = icmp slt i32 %6, 10
61 br i1 %7, label %8, label %12
62
63 ;
64 br label %9
65
66 ;
67 %10 = load i32* %i, align 4
68 %11 = add nsw i32 %10, 1
69 store i32 %11, i32* %i, align 4
70 br label %5
71
72 ;
73 %13 = load i32* %local_1_xxx, align 4
74 %14 = load i32* %1, align 4
75 %15 = add nsw i32 %13, %14
76 %16 = load i32* %local_2_xxx, align 4
77 %17 = add nsw i32 %15, %16
78 %18 = load i32* %2, align 4
79 %19 = add nsw i32 %17, %18
80 %20 = load i32* @func_5_xxx.static_local_3_xxx, align 4
81 %21 = add nsw i32 %19, %20
82 %22 = load i32* %3, align 4
83 %23 = add nsw i32 %21, %22
84 %24 = load i32* %4, align 4
85 %25 = add nsw i32 %23, %24
86 ret i32 %25
87 }
88
89 define i32 @varargs_func_6_xxx(i32 %arg_1_xxx, i32 %arg_2_xxx, ...) nounwind uwtable ssp {
90 %1 = alloca i32, align 4
91 %2 = alloca i32, align 4
92 store i32 %arg_1_xxx, i32* %1, align 4
93 store i32 %arg_2_xxx, i32* %2, align 4
94 ret i32 6
95 }