SanitizerStats.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===- SanitizerStats.cpp - Sanitizer statistics gathering ----------------===//
  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. // Implements code generation for sanitizer statistics gathering.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Utils/SanitizerStats.h"
  13. #include "llvm/ADT/Triple.h"
  14. #include "llvm/IR/Constants.h"
  15. #include "llvm/IR/DerivedTypes.h"
  16. #include "llvm/IR/GlobalVariable.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/Transforms/Utils/ModuleUtils.h"
  20. using namespace llvm;
  21. SanitizerStatReport::SanitizerStatReport(Module *M) : M(M) {
  22. StatTy = ArrayType::get(Type::getInt8PtrTy(M->getContext()), 2);
  23. EmptyModuleStatsTy = makeModuleStatsTy();
  24. ModuleStatsGV = new GlobalVariable(*M, EmptyModuleStatsTy, false,
  25. GlobalValue::InternalLinkage, nullptr);
  26. }
  27. ArrayType *SanitizerStatReport::makeModuleStatsArrayTy() {
  28. return ArrayType::get(StatTy, Inits.size());
  29. }
  30. StructType *SanitizerStatReport::makeModuleStatsTy() {
  31. return StructType::get(M->getContext(), {Type::getInt8PtrTy(M->getContext()),
  32. Type::getInt32Ty(M->getContext()),
  33. makeModuleStatsArrayTy()});
  34. }
  35. void SanitizerStatReport::create(IRBuilder<> &B, SanitizerStatKind SK) {
  36. Function *F = B.GetInsertBlock()->getParent();
  37. Module *M = F->getParent();
  38. PointerType *Int8PtrTy = B.getInt8PtrTy();
  39. IntegerType *IntPtrTy = B.getIntPtrTy(M->getDataLayout());
  40. ArrayType *StatTy = ArrayType::get(Int8PtrTy, 2);
  41. Inits.push_back(ConstantArray::get(
  42. StatTy,
  43. {Constant::getNullValue(Int8PtrTy),
  44. ConstantExpr::getIntToPtr(
  45. ConstantInt::get(IntPtrTy, uint64_t(SK) << (IntPtrTy->getBitWidth() -
  46. kSanitizerStatKindBits)),
  47. Int8PtrTy)}));
  48. FunctionType *StatReportTy =
  49. FunctionType::get(B.getVoidTy(), Int8PtrTy, false);
  50. FunctionCallee StatReport =
  51. M->getOrInsertFunction("__sanitizer_stat_report", StatReportTy);
  52. auto InitAddr = ConstantExpr::getGetElementPtr(
  53. EmptyModuleStatsTy, ModuleStatsGV,
  54. ArrayRef<Constant *>{
  55. ConstantInt::get(IntPtrTy, 0), ConstantInt::get(B.getInt32Ty(), 2),
  56. ConstantInt::get(IntPtrTy, Inits.size() - 1),
  57. });
  58. B.CreateCall(StatReport, ConstantExpr::getBitCast(InitAddr, Int8PtrTy));
  59. }
  60. void SanitizerStatReport::finish() {
  61. if (Inits.empty()) {
  62. ModuleStatsGV->eraseFromParent();
  63. return;
  64. }
  65. PointerType *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
  66. IntegerType *Int32Ty = Type::getInt32Ty(M->getContext());
  67. Type *VoidTy = Type::getVoidTy(M->getContext());
  68. // Create a new ModuleStatsGV to replace the old one. We can't just set the
  69. // old one's initializer because its type is different.
  70. auto NewModuleStatsGV = new GlobalVariable(
  71. *M, makeModuleStatsTy(), false, GlobalValue::InternalLinkage,
  72. ConstantStruct::getAnon(
  73. {Constant::getNullValue(Int8PtrTy),
  74. ConstantInt::get(Int32Ty, Inits.size()),
  75. ConstantArray::get(makeModuleStatsArrayTy(), Inits)}));
  76. ModuleStatsGV->replaceAllUsesWith(
  77. ConstantExpr::getBitCast(NewModuleStatsGV, ModuleStatsGV->getType()));
  78. ModuleStatsGV->eraseFromParent();
  79. // Create a global constructor to register NewModuleStatsGV.
  80. auto F = Function::Create(FunctionType::get(VoidTy, false),
  81. GlobalValue::InternalLinkage, "", M);
  82. auto BB = BasicBlock::Create(M->getContext(), "", F);
  83. IRBuilder<> B(BB);
  84. FunctionType *StatInitTy = FunctionType::get(VoidTy, Int8PtrTy, false);
  85. FunctionCallee StatInit =
  86. M->getOrInsertFunction("__sanitizer_stat_init", StatInitTy);
  87. B.CreateCall(StatInit, ConstantExpr::getBitCast(NewModuleStatsGV, Int8PtrTy));
  88. B.CreateRetVoid();
  89. appendToGlobalCtors(*M, F, 0);
  90. }