2012-09-11 10:46:18 +08:00
|
|
|
//===- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-09-15 03:19:57 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-03-24 07:21:07 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
#include "llvm/IR/Argument.h"
|
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
#include "llvm/IR/GlobalAlias.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2013-01-07 23:43:51 +08:00
|
|
|
#include "llvm/IR/TypeFinder.h"
|
2012-09-11 10:46:18 +08:00
|
|
|
#include "llvm/Pass.h"
|
2018-03-29 01:44:36 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
2017-10-27 09:09:08 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-10-27 09:09:08 +08:00
|
|
|
static const char *const 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"
|
|
|
|
};
|
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-16 00:51:49 +08:00
|
|
|
int rand() {
|
2012-09-11 10:46:18 +08:00
|
|
|
next = next * 1103515245 + 12345;
|
|
|
|
return (unsigned int)(next / 65536) % 32768;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-26 14:11:38 +08:00
|
|
|
struct Renamer {
|
|
|
|
Renamer(unsigned int seed) {
|
|
|
|
prng.srand(seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *newName() {
|
|
|
|
return metaNames[prng.rand() % array_lengthof(metaNames)];
|
|
|
|
}
|
|
|
|
|
|
|
|
PRNG prng;
|
|
|
|
};
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
struct MetaRenamer : public ModulePass {
|
2017-10-27 09:09:08 +08:00
|
|
|
// Pass identification, replacement for typeid
|
|
|
|
static char ID;
|
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
MetaRenamer() : ModulePass(ID) {
|
|
|
|
initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2017-03-24 07:21:07 +08:00
|
|
|
AU.addRequired<TargetLibraryInfoWrapperPass>();
|
2012-09-11 10:46:18 +08:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnModule(Module &M) override {
|
2012-09-11 10:46:18 +08:00
|
|
|
// 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;
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto C : M.getModuleIdentifier())
|
|
|
|
randSeed += C;
|
2012-09-11 10:46:18 +08:00
|
|
|
|
2015-08-26 14:11:38 +08:00
|
|
|
Renamer renamer(randSeed);
|
2015-08-27 13:37:12 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
// Rename all aliases
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
|
2013-01-23 23:03:08 +08:00
|
|
|
StringRef Name = AI->getName();
|
|
|
|
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
|
|
|
|
continue;
|
2012-09-11 10:46:18 +08:00
|
|
|
|
2013-01-23 23:03:08 +08:00
|
|
|
AI->setName("alias");
|
|
|
|
}
|
2015-08-26 14:11:36 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
// Rename all global variables
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
|
2013-01-23 23:03:08 +08:00
|
|
|
StringRef Name = GI->getName();
|
|
|
|
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
|
|
|
|
continue;
|
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
GI->setName("global");
|
2013-01-23 23:03:08 +08:00
|
|
|
}
|
2012-09-11 10:46:18 +08:00
|
|
|
|
|
|
|
// Rename all struct types
|
|
|
|
TypeFinder StructTypes;
|
|
|
|
StructTypes.run(M, true);
|
2015-08-26 14:11:41 +08:00
|
|
|
for (StructType *STy : StructTypes) {
|
2012-09-11 10:46:18 +08:00
|
|
|
if (STy->isLiteral() || STy->getName().empty()) continue;
|
|
|
|
|
|
|
|
SmallString<128> NameStorage;
|
2015-08-26 14:11:38 +08:00
|
|
|
STy->setName((Twine("struct.") +
|
|
|
|
renamer.newName()).toStringRef(NameStorage));
|
2012-09-11 10:46:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename all functions
|
2017-03-24 07:21:07 +08:00
|
|
|
const TargetLibraryInfo &TLI =
|
|
|
|
getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto &F : M) {
|
|
|
|
StringRef Name = F.getName();
|
2017-03-24 07:21:07 +08:00
|
|
|
LibFunc Tmp;
|
|
|
|
// Leave library functions alone because their presence or absence could
|
|
|
|
// affect the behavior of other passes.
|
|
|
|
if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
|
|
|
|
TLI.getLibFunc(F, Tmp))
|
2013-01-23 23:03:08 +08:00
|
|
|
continue;
|
|
|
|
|
2017-08-01 13:14:45 +08:00
|
|
|
// Leave @main alone. The output of -metarenamer might be passed to
|
|
|
|
// lli for execution and the latter needs a main entry point.
|
|
|
|
if (Name != "main")
|
|
|
|
F.setName(renamer.newName());
|
|
|
|
|
2015-08-26 14:11:41 +08:00
|
|
|
runOnFunction(F);
|
2012-09-11 10:46:18 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) {
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
|
2012-09-11 10:46:18 +08:00
|
|
|
if (!AI->getType()->isVoidTy())
|
|
|
|
AI->setName("arg");
|
|
|
|
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto &BB : F) {
|
|
|
|
BB.setName("bb");
|
2012-09-11 10:46:18 +08:00
|
|
|
|
2015-08-26 14:11:41 +08:00
|
|
|
for (auto &I : BB)
|
|
|
|
if (!I.getType()->isVoidTy())
|
|
|
|
I.setName("tmp");
|
2012-09-11 10:46:18 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2017-10-27 09:09:08 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2012-09-11 10:46:18 +08:00
|
|
|
|
|
|
|
char MetaRenamer::ID = 0;
|
2017-10-27 09:09:08 +08:00
|
|
|
|
2017-03-24 07:21:07 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
|
|
|
|
"Assign new names to everything", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
|
|
|
|
"Assign new names to everything", false, false)
|
2017-10-27 09:09:08 +08:00
|
|
|
|
2012-09-11 10:46:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// MetaRenamer - Rename everything with metasyntactic names.
|
|
|
|
//
|
|
|
|
ModulePass *llvm::createMetaRenamerPass() {
|
|
|
|
return new MetaRenamer();
|
|
|
|
}
|