sanstats.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===- sanstats.cpp - Sanitizer statistics dumper -------------------------===//
  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 tool dumps statistics information from files in the format produced
  10. // by clang's -fsanitize-stats feature.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/DebugInfo/Symbolize/Symbolize.h"
  14. #include "llvm/Support/CommandLine.h"
  15. #include "llvm/Support/ErrorOr.h"
  16. #include "llvm/Support/FileSystem.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. #include "llvm/Support/Path.h"
  19. #include "llvm/Transforms/Utils/SanitizerStats.h"
  20. #include <stdint.h>
  21. using namespace llvm;
  22. static cl::opt<std::string> ClInputFile(cl::Positional, cl::Required,
  23. cl::desc("<filename>"));
  24. static cl::opt<bool> ClDemangle("demangle", cl::init(false),
  25. cl::desc("Print demangled function name."));
  26. inline uint64_t KindFromData(uint64_t Data, char SizeofPtr) {
  27. return Data >> (SizeofPtr * 8 - kSanitizerStatKindBits);
  28. }
  29. inline uint64_t CountFromData(uint64_t Data, char SizeofPtr) {
  30. return Data & ((1ull << (SizeofPtr * 8 - kSanitizerStatKindBits)) - 1);
  31. }
  32. static uint64_t ReadLE(char Size, const char *Begin, const char *End) {
  33. uint64_t Result = 0;
  34. char Pos = 0;
  35. while (Begin < End && Pos != Size) {
  36. Result |= uint64_t(uint8_t(*Begin)) << (Pos * 8);
  37. ++Begin;
  38. ++Pos;
  39. }
  40. return Result;
  41. }
  42. static const char *ReadModule(char SizeofPtr, const char *Begin,
  43. const char *End) {
  44. const char *FilenameBegin = Begin;
  45. while (Begin != End && *Begin)
  46. ++Begin;
  47. if (Begin == End)
  48. return nullptr;
  49. std::string Filename(FilenameBegin, Begin - FilenameBegin);
  50. if (!llvm::sys::fs::exists(Filename))
  51. Filename = std::string(llvm::sys::path::parent_path(ClInputFile)) +
  52. std::string(llvm::sys::path::filename(Filename));
  53. ++Begin;
  54. if (Begin == End)
  55. return nullptr;
  56. symbolize::LLVMSymbolizer::Options SymbolizerOptions;
  57. SymbolizerOptions.Demangle = ClDemangle;
  58. SymbolizerOptions.UseSymbolTable = true;
  59. symbolize::LLVMSymbolizer Symbolizer(SymbolizerOptions);
  60. while (true) {
  61. uint64_t Addr = ReadLE(SizeofPtr, Begin, End);
  62. Begin += SizeofPtr;
  63. uint64_t Data = ReadLE(SizeofPtr, Begin, End);
  64. Begin += SizeofPtr;
  65. if (Begin > End)
  66. return nullptr;
  67. if (Addr == 0 && Data == 0)
  68. return Begin;
  69. if (Begin == End)
  70. return nullptr;
  71. // As the instrumentation tracks the return address and not
  72. // the address of the call to `__sanitizer_stat_report` we
  73. // remove one from the address to get the correct DI.
  74. // TODO: it would be neccessary to set proper section index here.
  75. // object::SectionedAddress::UndefSection works for only absolute addresses.
  76. if (Expected<DILineInfo> LineInfo = Symbolizer.symbolizeCode(
  77. Filename, {Addr - 1, object::SectionedAddress::UndefSection})) {
  78. llvm::outs() << format_hex(Addr - 1, 18) << ' ' << LineInfo->FileName
  79. << ':' << LineInfo->Line << ' ' << LineInfo->FunctionName
  80. << ' ';
  81. } else {
  82. logAllUnhandledErrors(LineInfo.takeError(), llvm::outs(), "<error> ");
  83. }
  84. switch (KindFromData(Data, SizeofPtr)) {
  85. case SanStat_CFI_VCall:
  86. llvm::outs() << "cfi-vcall";
  87. break;
  88. case SanStat_CFI_NVCall:
  89. llvm::outs() << "cfi-nvcall";
  90. break;
  91. case SanStat_CFI_DerivedCast:
  92. llvm::outs() << "cfi-derived-cast";
  93. break;
  94. case SanStat_CFI_UnrelatedCast:
  95. llvm::outs() << "cfi-unrelated-cast";
  96. break;
  97. case SanStat_CFI_ICall:
  98. llvm::outs() << "cfi-icall";
  99. break;
  100. default:
  101. llvm::outs() << "<unknown>";
  102. break;
  103. }
  104. llvm::outs() << " " << CountFromData(Data, SizeofPtr) << '\n';
  105. }
  106. }
  107. int main(int argc, char **argv) {
  108. cl::ParseCommandLineOptions(argc, argv,
  109. "Sanitizer Statistics Processing Tool");
  110. ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
  111. ClInputFile, /*IsText=*/false, /*RequiresNullTerminator=*/false);
  112. if (!MBOrErr) {
  113. errs() << argv[0] << ": " << ClInputFile << ": "
  114. << MBOrErr.getError().message() << '\n';
  115. return 1;
  116. }
  117. std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
  118. const char *Begin = MB->getBufferStart(), *End = MB->getBufferEnd();
  119. if (Begin == End) {
  120. errs() << argv[0] << ": " << ClInputFile << ": short read\n";
  121. return 1;
  122. }
  123. char SizeofPtr = *Begin++;
  124. while (Begin != End) {
  125. Begin = ReadModule(SizeofPtr, Begin, End);
  126. if (Begin == nullptr) {
  127. errs() << argv[0] << ": " << ClInputFile << ": short read\n";
  128. return 1;
  129. }
  130. assert(Begin <= End);
  131. }
  132. }