MetaRenamer.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/SmallVector.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/Twine.h"
  20. #include "llvm/Analysis/TargetLibraryInfo.h"
  21. #include "llvm/IR/Argument.h"
  22. #include "llvm/IR/BasicBlock.h"
  23. #include "llvm/IR/DerivedTypes.h"
  24. #include "llvm/IR/Function.h"
  25. #include "llvm/IR/GlobalAlias.h"
  26. #include "llvm/IR/GlobalVariable.h"
  27. #include "llvm/IR/Instruction.h"
  28. #include "llvm/IR/Module.h"
  29. #include "llvm/IR/PassManager.h"
  30. #include "llvm/IR/Type.h"
  31. #include "llvm/IR/TypeFinder.h"
  32. #include "llvm/InitializePasses.h"
  33. #include "llvm/Pass.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Transforms/Utils.h"
  36. using namespace llvm;
  37. static cl::opt<std::string> RenameExcludeFunctionPrefixes(
  38. "rename-exclude-function-prefixes",
  39. cl::desc("Prefixes for functions that don't need to be renamed, separated "
  40. "by a comma"),
  41. cl::Hidden);
  42. static cl::opt<std::string> RenameExcludeAliasPrefixes(
  43. "rename-exclude-alias-prefixes",
  44. cl::desc("Prefixes for aliases that don't need to be renamed, separated "
  45. "by a comma"),
  46. cl::Hidden);
  47. static cl::opt<std::string> RenameExcludeGlobalPrefixes(
  48. "rename-exclude-global-prefixes",
  49. cl::desc(
  50. "Prefixes for global values that don't need to be renamed, separated "
  51. "by a comma"),
  52. cl::Hidden);
  53. static cl::opt<std::string> RenameExcludeStructPrefixes(
  54. "rename-exclude-struct-prefixes",
  55. cl::desc("Prefixes for structs that don't need to be renamed, separated "
  56. "by a comma"),
  57. cl::Hidden);
  58. static const char *const metaNames[] = {
  59. // See http://en.wikipedia.org/wiki/Metasyntactic_variable
  60. "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
  61. "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
  62. };
  63. namespace {
  64. // This PRNG is from the ISO C spec. It is intentionally simple and
  65. // unsuitable for cryptographic use. We're just looking for enough
  66. // variety to surprise and delight users.
  67. struct PRNG {
  68. unsigned long next;
  69. void srand(unsigned int seed) { next = seed; }
  70. int rand() {
  71. next = next * 1103515245 + 12345;
  72. return (unsigned int)(next / 65536) % 32768;
  73. }
  74. };
  75. struct Renamer {
  76. Renamer(unsigned int seed) { prng.srand(seed); }
  77. const char *newName() {
  78. return metaNames[prng.rand() % std::size(metaNames)];
  79. }
  80. PRNG prng;
  81. };
  82. static void
  83. parseExcludedPrefixes(StringRef PrefixesStr,
  84. SmallVectorImpl<StringRef> &ExcludedPrefixes) {
  85. for (;;) {
  86. auto PrefixesSplit = PrefixesStr.split(',');
  87. if (PrefixesSplit.first.empty())
  88. break;
  89. ExcludedPrefixes.push_back(PrefixesSplit.first);
  90. PrefixesStr = PrefixesSplit.second;
  91. }
  92. }
  93. void MetaRename(Function &F) {
  94. for (Argument &Arg : F.args())
  95. if (!Arg.getType()->isVoidTy())
  96. Arg.setName("arg");
  97. for (auto &BB : F) {
  98. BB.setName("bb");
  99. for (auto &I : BB)
  100. if (!I.getType()->isVoidTy())
  101. I.setName("tmp");
  102. }
  103. }
  104. void MetaRename(Module &M,
  105. function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
  106. // Seed our PRNG with simple additive sum of ModuleID. We're looking to
  107. // simply avoid always having the same function names, and we need to
  108. // remain deterministic.
  109. unsigned int randSeed = 0;
  110. for (auto C : M.getModuleIdentifier())
  111. randSeed += C;
  112. Renamer renamer(randSeed);
  113. SmallVector<StringRef, 8> ExcludedAliasesPrefixes;
  114. SmallVector<StringRef, 8> ExcludedGlobalsPrefixes;
  115. SmallVector<StringRef, 8> ExcludedStructsPrefixes;
  116. SmallVector<StringRef, 8> ExcludedFuncPrefixes;
  117. parseExcludedPrefixes(RenameExcludeAliasPrefixes, ExcludedAliasesPrefixes);
  118. parseExcludedPrefixes(RenameExcludeGlobalPrefixes, ExcludedGlobalsPrefixes);
  119. parseExcludedPrefixes(RenameExcludeStructPrefixes, ExcludedStructsPrefixes);
  120. parseExcludedPrefixes(RenameExcludeFunctionPrefixes, ExcludedFuncPrefixes);
  121. auto IsNameExcluded = [](StringRef &Name,
  122. SmallVectorImpl<StringRef> &ExcludedPrefixes) {
  123. return any_of(ExcludedPrefixes,
  124. [&Name](auto &Prefix) { return Name.startswith(Prefix); });
  125. };
  126. // Rename all aliases
  127. for (GlobalAlias &GA : M.aliases()) {
  128. StringRef Name = GA.getName();
  129. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
  130. IsNameExcluded(Name, ExcludedAliasesPrefixes))
  131. continue;
  132. GA.setName("alias");
  133. }
  134. // Rename all global variables
  135. for (GlobalVariable &GV : M.globals()) {
  136. StringRef Name = GV.getName();
  137. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
  138. IsNameExcluded(Name, ExcludedGlobalsPrefixes))
  139. continue;
  140. GV.setName("global");
  141. }
  142. // Rename all struct types
  143. TypeFinder StructTypes;
  144. StructTypes.run(M, true);
  145. for (StructType *STy : StructTypes) {
  146. StringRef Name = STy->getName();
  147. if (STy->isLiteral() || Name.empty() ||
  148. IsNameExcluded(Name, ExcludedStructsPrefixes))
  149. continue;
  150. SmallString<128> NameStorage;
  151. STy->setName(
  152. (Twine("struct.") + renamer.newName()).toStringRef(NameStorage));
  153. }
  154. // Rename all functions
  155. for (auto &F : M) {
  156. StringRef Name = F.getName();
  157. LibFunc Tmp;
  158. // Leave library functions alone because their presence or absence could
  159. // affect the behavior of other passes.
  160. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1) ||
  161. GetTLI(F).getLibFunc(F, Tmp) ||
  162. IsNameExcluded(Name, ExcludedFuncPrefixes))
  163. continue;
  164. // Leave @main alone. The output of -metarenamer might be passed to
  165. // lli for execution and the latter needs a main entry point.
  166. if (Name != "main")
  167. F.setName(renamer.newName());
  168. MetaRename(F);
  169. }
  170. }
  171. struct MetaRenamer : public ModulePass {
  172. // Pass identification, replacement for typeid
  173. static char ID;
  174. MetaRenamer() : ModulePass(ID) {
  175. initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
  176. }
  177. void getAnalysisUsage(AnalysisUsage &AU) const override {
  178. AU.addRequired<TargetLibraryInfoWrapperPass>();
  179. AU.setPreservesAll();
  180. }
  181. bool runOnModule(Module &M) override {
  182. auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
  183. return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  184. };
  185. MetaRename(M, GetTLI);
  186. return true;
  187. }
  188. };
  189. } // end anonymous namespace
  190. char MetaRenamer::ID = 0;
  191. INITIALIZE_PASS_BEGIN(MetaRenamer, "metarenamer",
  192. "Assign new names to everything", false, false)
  193. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  194. INITIALIZE_PASS_END(MetaRenamer, "metarenamer",
  195. "Assign new names to everything", false, false)
  196. //===----------------------------------------------------------------------===//
  197. //
  198. // MetaRenamer - Rename everything with metasyntactic names.
  199. //
  200. ModulePass *llvm::createMetaRenamerPass() {
  201. return new MetaRenamer();
  202. }
  203. PreservedAnalyses MetaRenamerPass::run(Module &M, ModuleAnalysisManager &AM) {
  204. FunctionAnalysisManager &FAM =
  205. AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  206. auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
  207. return FAM.getResult<TargetLibraryAnalysis>(F);
  208. };
  209. MetaRename(M, GetTLI);
  210. return PreservedAnalyses::all();
  211. }