sanstats.cpp 5.1 KB

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