SanitizerStats.cpp 4.0 KB

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