17330f729Sjoerg //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This pass renames everything with metasyntatic names. The intent is to use
107330f729Sjoerg // this pass after bugpoint reduction to conceal the nature of the original
117330f729Sjoerg // program.
127330f729Sjoerg //
137330f729Sjoerg //===----------------------------------------------------------------------===//
147330f729Sjoerg
15*82d56013Sjoerg #include "llvm/Transforms/Utils/MetaRenamer.h"
167330f729Sjoerg #include "llvm/ADT/STLExtras.h"
177330f729Sjoerg #include "llvm/ADT/SmallString.h"
187330f729Sjoerg #include "llvm/ADT/StringRef.h"
197330f729Sjoerg #include "llvm/ADT/Twine.h"
207330f729Sjoerg #include "llvm/Analysis/TargetLibraryInfo.h"
217330f729Sjoerg #include "llvm/IR/Argument.h"
227330f729Sjoerg #include "llvm/IR/BasicBlock.h"
237330f729Sjoerg #include "llvm/IR/DerivedTypes.h"
247330f729Sjoerg #include "llvm/IR/Function.h"
257330f729Sjoerg #include "llvm/IR/GlobalAlias.h"
267330f729Sjoerg #include "llvm/IR/GlobalVariable.h"
277330f729Sjoerg #include "llvm/IR/Instruction.h"
287330f729Sjoerg #include "llvm/IR/Module.h"
29*82d56013Sjoerg #include "llvm/IR/PassManager.h"
307330f729Sjoerg #include "llvm/IR/Type.h"
317330f729Sjoerg #include "llvm/IR/TypeFinder.h"
32*82d56013Sjoerg #include "llvm/InitializePasses.h"
337330f729Sjoerg #include "llvm/Pass.h"
347330f729Sjoerg #include "llvm/Transforms/Utils.h"
357330f729Sjoerg
367330f729Sjoerg using namespace llvm;
377330f729Sjoerg
387330f729Sjoerg static const char *const metaNames[] = {
397330f729Sjoerg // See http://en.wikipedia.org/wiki/Metasyntactic_variable
407330f729Sjoerg "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
417330f729Sjoerg "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
427330f729Sjoerg };
437330f729Sjoerg
447330f729Sjoerg namespace {
457330f729Sjoerg // This PRNG is from the ISO C spec. It is intentionally simple and
467330f729Sjoerg // unsuitable for cryptographic use. We're just looking for enough
477330f729Sjoerg // variety to surprise and delight users.
487330f729Sjoerg struct PRNG {
497330f729Sjoerg unsigned long next;
507330f729Sjoerg
srand__anonf5f4de4a0111::PRNG51*82d56013Sjoerg void srand(unsigned int seed) { next = seed; }
527330f729Sjoerg
rand__anonf5f4de4a0111::PRNG537330f729Sjoerg int rand() {
547330f729Sjoerg next = next * 1103515245 + 12345;
557330f729Sjoerg return (unsigned int)(next / 65536) % 32768;
567330f729Sjoerg }
577330f729Sjoerg };
587330f729Sjoerg
597330f729Sjoerg struct Renamer {
Renamer__anonf5f4de4a0111::Renamer60*82d56013Sjoerg Renamer(unsigned int seed) { prng.srand(seed); }
617330f729Sjoerg
newName__anonf5f4de4a0111::Renamer627330f729Sjoerg const char *newName() {
637330f729Sjoerg return metaNames[prng.rand() % array_lengthof(metaNames)];
647330f729Sjoerg }
657330f729Sjoerg
667330f729Sjoerg PRNG prng;
677330f729Sjoerg };
687330f729Sjoerg
MetaRename(Function & F)69*82d56013Sjoerg void MetaRename(Function &F) {
70*82d56013Sjoerg for (Argument &Arg : F.args())
71*82d56013Sjoerg if (!Arg.getType()->isVoidTy())
72*82d56013Sjoerg Arg.setName("arg");
737330f729Sjoerg
74*82d56013Sjoerg for (auto &BB : F) {
75*82d56013Sjoerg BB.setName("bb");
76*82d56013Sjoerg
77*82d56013Sjoerg for (auto &I : BB)
78*82d56013Sjoerg if (!I.getType()->isVoidTy())
79*82d56013Sjoerg I.setName("tmp");
80*82d56013Sjoerg }
817330f729Sjoerg }
827330f729Sjoerg
MetaRename(Module & M,function_ref<TargetLibraryInfo & (Function &)> GetTLI)83*82d56013Sjoerg void MetaRename(Module &M,
84*82d56013Sjoerg function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
857330f729Sjoerg // Seed our PRNG with simple additive sum of ModuleID. We're looking to
867330f729Sjoerg // simply avoid always having the same function names, and we need to
877330f729Sjoerg // remain deterministic.
887330f729Sjoerg unsigned int randSeed = 0;
897330f729Sjoerg for (auto C : M.getModuleIdentifier())
907330f729Sjoerg randSeed += C;
917330f729Sjoerg
927330f729Sjoerg Renamer renamer(randSeed);
937330f729Sjoerg
947330f729Sjoerg // Rename all aliases
957330f729Sjoerg for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
967330f729Sjoerg StringRef Name = AI->getName();
977330f729Sjoerg if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
987330f729Sjoerg continue;
997330f729Sjoerg
1007330f729Sjoerg AI->setName("alias");
1017330f729Sjoerg }
1027330f729Sjoerg
1037330f729Sjoerg // Rename all global variables
104*82d56013Sjoerg for (GlobalVariable &GV : M.globals()) {
105*82d56013Sjoerg StringRef Name = GV.getName();
1067330f729Sjoerg if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
1077330f729Sjoerg continue;
1087330f729Sjoerg
109*82d56013Sjoerg GV.setName("global");
1107330f729Sjoerg }
1117330f729Sjoerg
1127330f729Sjoerg // Rename all struct types
1137330f729Sjoerg TypeFinder StructTypes;
1147330f729Sjoerg StructTypes.run(M, true);
1157330f729Sjoerg for (StructType *STy : StructTypes) {
116*82d56013Sjoerg if (STy->isLiteral() || STy->getName().empty())
117*82d56013Sjoerg continue;
1187330f729Sjoerg
1197330f729Sjoerg SmallString<128> NameStorage;
120*82d56013Sjoerg STy->setName(
121*82d56013Sjoerg (Twine("struct.") + renamer.newName()).toStringRef(NameStorage));
1227330f729Sjoerg }
1237330f729Sjoerg
1247330f729Sjoerg // Rename all functions
1257330f729Sjoerg for (auto &F : M) {
1267330f729Sjoerg StringRef Name = F.getName();
1277330f729Sjoerg LibFunc Tmp;
1287330f729Sjoerg // Leave library functions alone because their presence or absence could
1297330f729Sjoerg // affect the behavior of other passes.
1307330f729Sjoerg if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
131*82d56013Sjoerg GetTLI(F).getLibFunc(F, Tmp))
1327330f729Sjoerg continue;
1337330f729Sjoerg
1347330f729Sjoerg // Leave @main alone. The output of -metarenamer might be passed to
1357330f729Sjoerg // lli for execution and the latter needs a main entry point.
1367330f729Sjoerg if (Name != "main")
1377330f729Sjoerg F.setName(renamer.newName());
1387330f729Sjoerg
139*82d56013Sjoerg MetaRename(F);
1407330f729Sjoerg }
1417330f729Sjoerg }
1427330f729Sjoerg
143*82d56013Sjoerg struct MetaRenamer : public ModulePass {
144*82d56013Sjoerg // Pass identification, replacement for typeid
145*82d56013Sjoerg static char ID;
1467330f729Sjoerg
MetaRenamer__anonf5f4de4a0111::MetaRenamer147*82d56013Sjoerg MetaRenamer() : ModulePass(ID) {
148*82d56013Sjoerg initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
1497330f729Sjoerg }
150*82d56013Sjoerg
getAnalysisUsage__anonf5f4de4a0111::MetaRenamer151*82d56013Sjoerg void getAnalysisUsage(AnalysisUsage &AU) const override {
152*82d56013Sjoerg AU.addRequired<TargetLibraryInfoWrapperPass>();
153*82d56013Sjoerg AU.setPreservesAll();
154*82d56013Sjoerg }
155*82d56013Sjoerg
runOnModule__anonf5f4de4a0111::MetaRenamer156*82d56013Sjoerg bool runOnModule(Module &M) override {
157*82d56013Sjoerg auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
158*82d56013Sjoerg return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
159*82d56013Sjoerg };
160*82d56013Sjoerg MetaRename(M, GetTLI);
1617330f729Sjoerg return true;
1627330f729Sjoerg }
1637330f729Sjoerg };
1647330f729Sjoerg
1657330f729Sjoerg } // end anonymous namespace
1667330f729Sjoerg
1677330f729Sjoerg char MetaRenamer::ID = 0;
1687330f729Sjoerg
1697330f729Sjoerg INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
1707330f729Sjoerg "Assign new names to everything", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)1717330f729Sjoerg INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1727330f729Sjoerg INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
1737330f729Sjoerg "Assign new names to everything", false, false)
1747330f729Sjoerg
1757330f729Sjoerg //===----------------------------------------------------------------------===//
1767330f729Sjoerg //
1777330f729Sjoerg // MetaRenamer - Rename everything with metasyntactic names.
1787330f729Sjoerg //
1797330f729Sjoerg ModulePass *llvm::createMetaRenamerPass() {
1807330f729Sjoerg return new MetaRenamer();
1817330f729Sjoerg }
182*82d56013Sjoerg
run(Module & M,ModuleAnalysisManager & AM)183*82d56013Sjoerg PreservedAnalyses MetaRenamerPass::run(Module &M, ModuleAnalysisManager &AM) {
184*82d56013Sjoerg FunctionAnalysisManager &FAM =
185*82d56013Sjoerg AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
186*82d56013Sjoerg auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
187*82d56013Sjoerg return FAM.getResult<TargetLibraryAnalysis>(F);
188*82d56013Sjoerg };
189*82d56013Sjoerg MetaRename(M, GetTLI);
190*82d56013Sjoerg
191*82d56013Sjoerg return PreservedAnalyses::all();
192*82d56013Sjoerg }
193