MetaRenamer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "llvm/Transforms/Utils/MetaRenamer.h"
  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/PassManager.h"
  29. #include "llvm/IR/Type.h"
  30. #include "llvm/IR/TypeFinder.h"
  31. #include "llvm/InitializePasses.h"
  32. #include "llvm/Pass.h"
  33. #include "llvm/Transforms/Utils.h"
  34. using namespace llvm;
  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. namespace {
  41. // This PRNG is from the ISO C spec. It is intentionally simple and
  42. // unsuitable for cryptographic use. We're just looking for enough
  43. // variety to surprise and delight users.
  44. struct PRNG {
  45. unsigned long next;
  46. void srand(unsigned int seed) { next = seed; }
  47. int rand() {
  48. next = next * 1103515245 + 12345;
  49. return (unsigned int)(next / 65536) % 32768;
  50. }
  51. };
  52. struct Renamer {
  53. Renamer(unsigned int seed) { prng.srand(seed); }
  54. const char *newName() {
  55. return metaNames[prng.rand() % array_lengthof(metaNames)];
  56. }
  57. PRNG prng;
  58. };
  59. void MetaRename(Function &F) {
  60. for (auto AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI)
  61. if (!AI->getType()->isVoidTy())
  62. AI->setName("arg");
  63. for (auto &BB : F) {
  64. BB.setName("bb");
  65. for (auto &I : BB)
  66. if (!I.getType()->isVoidTy())
  67. I.setName("tmp");
  68. }
  69. }
  70. void MetaRename(Module &M,
  71. function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
  72. // Seed our PRNG with simple additive sum of ModuleID. We're looking to
  73. // simply avoid always having the same function names, and we need to
  74. // remain deterministic.
  75. unsigned int randSeed = 0;
  76. for (auto C : M.getModuleIdentifier())
  77. randSeed += C;
  78. Renamer renamer(randSeed);
  79. // Rename all aliases
  80. for (auto AI = M.alias_begin(), AE = M.alias_end(); AI != AE; ++AI) {
  81. StringRef Name = AI->getName();
  82. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
  83. continue;
  84. AI->setName("alias");
  85. }
  86. // Rename all global variables
  87. for (auto GI = M.global_begin(), GE = M.global_end(); GI != GE; ++GI) {
  88. StringRef Name = GI->getName();
  89. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
  90. continue;
  91. GI->setName("global");
  92. }
  93. // Rename all struct types
  94. TypeFinder StructTypes;
  95. StructTypes.run(M, true);
  96. for (StructType *STy : StructTypes) {
  97. if (STy->isLiteral() || STy->getName().empty())
  98. continue;
  99. SmallString<128> NameStorage;
  100. STy->setName(
  101. (Twine("struct.") + renamer.newName()).toStringRef(NameStorage));
  102. }
  103. // Rename all functions
  104. for (auto &F : M) {
  105. StringRef Name = F.getName();
  106. LibFunc Tmp;
  107. // Leave library functions alone because their presence or absence could
  108. // affect the behavior of other passes.
  109. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
  110. GetTLI(F).getLibFunc(F, Tmp))
  111. continue;
  112. // Leave @main alone. The output of -metarenamer might be passed to
  113. // lli for execution and the latter needs a main entry point.
  114. if (Name != "main")
  115. F.setName(renamer.newName());
  116. MetaRename(F);
  117. }
  118. }
  119. struct MetaRenamer : public ModulePass {
  120. // Pass identification, replacement for typeid
  121. static char ID;
  122. MetaRenamer() : ModulePass(ID) {
  123. initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
  124. }
  125. void getAnalysisUsage(AnalysisUsage &AU) const override {
  126. AU.addRequired<TargetLibraryInfoWrapperPass>();
  127. AU.setPreservesAll();
  128. }
  129. bool runOnModule(Module &M) override {
  130. auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
  131. return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  132. };
  133. MetaRename(M, GetTLI);
  134. return true;
  135. }
  136. };
  137. } // end anonymous namespace
  138. char MetaRenamer::ID = 0;
  139. INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
  140. "Assign new names to everything", false, false)
  141. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  142. INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
  143. "Assign new names to everything", false, false)
  144. //===----------------------------------------------------------------------===//
  145. //
  146. // MetaRenamer - Rename everything with metasyntactic names.
  147. //
  148. ModulePass *llvm::createMetaRenamerPass() {
  149. return new MetaRenamer();
  150. }
  151. PreservedAnalyses MetaRenamerPass::run(Module &M, ModuleAnalysisManager &AM) {
  152. FunctionAnalysisManager &FAM =
  153. AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  154. auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
  155. return FAM.getResult<TargetLibraryAnalysis>(F);
  156. };
  157. MetaRename(M, GetTLI);
  158. return PreservedAnalyses::all();
  159. }