MetaRenamer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //===- MetaRenamer.cpp - Rename everything with metasyntatic names --------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This pass renames everything with metasyntatic names. The intent is to use
  11. // this pass after bugpoint reduction to conceal the nature of the original
  12. // program.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/Transforms/IPO.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/IR/DerivedTypes.h"
  19. #include "llvm/IR/Function.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/IR/Type.h"
  22. #include "llvm/IR/TypeFinder.h"
  23. #include "llvm/Pass.h"
  24. using namespace llvm;
  25. namespace {
  26. // This PRNG is from the ISO C spec. It is intentionally simple and
  27. // unsuitable for cryptographic use. We're just looking for enough
  28. // variety to surprise and delight users.
  29. struct PRNG {
  30. unsigned long next;
  31. void srand(unsigned int seed) {
  32. next = seed;
  33. }
  34. int rand() {
  35. next = next * 1103515245 + 12345;
  36. return (unsigned int)(next / 65536) % 32768;
  37. }
  38. };
  39. struct MetaRenamer : public ModulePass {
  40. static char ID; // Pass identification, replacement for typeid
  41. MetaRenamer() : ModulePass(ID) {
  42. initializeMetaRenamerPass(*PassRegistry::getPassRegistry());
  43. }
  44. void getAnalysisUsage(AnalysisUsage &AU) const override {
  45. AU.setPreservesAll();
  46. }
  47. bool runOnModule(Module &M) override {
  48. static const char *const metaNames[] = {
  49. // See http://en.wikipedia.org/wiki/Metasyntactic_variable
  50. "foo", "bar", "baz", "quux", "barney", "snork", "zot", "blam", "hoge",
  51. "wibble", "wobble", "widget", "wombat", "ham", "eggs", "pluto", "spam"
  52. };
  53. // Seed our PRNG with simple additive sum of ModuleID. We're looking to
  54. // simply avoid always having the same function names, and we need to
  55. // remain deterministic.
  56. unsigned int randSeed = 0;
  57. for (std::string::const_iterator I = M.getModuleIdentifier().begin(),
  58. E = M.getModuleIdentifier().end(); I != E; ++I)
  59. randSeed += *I;
  60. PRNG prng;
  61. prng.srand(randSeed);
  62. // Rename all aliases
  63. for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
  64. AI != AE; ++AI) {
  65. StringRef Name = AI->getName();
  66. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
  67. continue;
  68. AI->setName("alias");
  69. }
  70. // Rename all global variables
  71. for (Module::global_iterator GI = M.global_begin(), GE = M.global_end();
  72. GI != GE; ++GI) {
  73. StringRef Name = GI->getName();
  74. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
  75. continue;
  76. GI->setName("global");
  77. }
  78. // Rename all struct types
  79. TypeFinder StructTypes;
  80. StructTypes.run(M, true);
  81. for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
  82. StructType *STy = StructTypes[i];
  83. if (STy->isLiteral() || STy->getName().empty()) continue;
  84. SmallString<128> NameStorage;
  85. STy->setName((Twine("struct.") + metaNames[prng.rand() %
  86. array_lengthof(metaNames)]).toStringRef(NameStorage));
  87. }
  88. // Rename all functions
  89. for (Module::iterator FI = M.begin(), FE = M.end();
  90. FI != FE; ++FI) {
  91. StringRef Name = FI->getName();
  92. if (Name.startswith("llvm.") || (!Name.empty() && Name[0] == 1))
  93. continue;
  94. FI->setName(metaNames[prng.rand() % array_lengthof(metaNames)]);
  95. runOnFunction(*FI);
  96. }
  97. return true;
  98. }
  99. bool runOnFunction(Function &F) {
  100. for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
  101. AI != AE; ++AI)
  102. if (!AI->getType()->isVoidTy())
  103. AI->setName("arg");
  104. for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
  105. BB->setName("bb");
  106. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  107. if (!I->getType()->isVoidTy())
  108. I->setName("tmp");
  109. }
  110. return true;
  111. }
  112. };
  113. }
  114. char MetaRenamer::ID = 0;
  115. INITIALIZE_PASS(MetaRenamer, "metarenamer",
  116. "Assign new names to everything", false, false)
  117. //===----------------------------------------------------------------------===//
  118. //
  119. // MetaRenamer - Rename everything with metasyntactic names.
  120. //
  121. ModulePass *llvm::createMetaRenamerPass() {
  122. return new MetaRenamer();
  123. }