Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / tools / sanstats / sanstats.cpp
blob0b4b2b8196bbad155ee4d9f65caa432c127e2d5c
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.
12 //===----------------------------------------------------------------------===//
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>
23 using namespace llvm;
25 static cl::opt<std::string> ClInputFile(cl::Positional, cl::Required,
26 cl::desc("<filename>"));
28 static cl::opt<bool> ClDemangle("demangle", cl::init(false),
29 cl::desc("Print demangled function name."));
31 inline uint64_t KindFromData(uint64_t Data, char SizeofPtr) {
32 return Data >> (SizeofPtr * 8 - kSanitizerStatKindBits);
35 inline uint64_t CountFromData(uint64_t Data, char SizeofPtr) {
36 return Data & ((1ull << (SizeofPtr * 8 - kSanitizerStatKindBits)) - 1);
39 uint64_t ReadLE(char Size, const char *Begin, const char *End) {
40 uint64_t Result = 0;
41 char Pos = 0;
42 while (Begin < End && Pos != Size) {
43 Result |= uint64_t(uint8_t(*Begin)) << (Pos * 8);
44 ++Begin;
45 ++Pos;
47 return Result;
50 const char *ReadModule(char SizeofPtr, const char *Begin, const char *End) {
51 const char *FilenameBegin = Begin;
52 while (Begin != End && *Begin)
53 ++Begin;
54 if (Begin == End)
55 return nullptr;
56 std::string Filename(FilenameBegin, Begin - FilenameBegin);
58 if (!llvm::sys::fs::exists(Filename))
59 Filename = std::string(llvm::sys::path::parent_path(ClInputFile)) +
60 std::string(llvm::sys::path::filename(Filename));
62 ++Begin;
63 if (Begin == End)
64 return nullptr;
66 symbolize::LLVMSymbolizer::Options SymbolizerOptions;
67 SymbolizerOptions.Demangle = ClDemangle;
68 SymbolizerOptions.UseSymbolTable = true;
69 symbolize::LLVMSymbolizer Symbolizer(SymbolizerOptions);
71 while (1) {
72 uint64_t Addr = ReadLE(SizeofPtr, Begin, End);
73 Begin += SizeofPtr;
74 uint64_t Data = ReadLE(SizeofPtr, Begin, End);
75 Begin += SizeofPtr;
77 if (Begin > End)
78 return nullptr;
79 if (Addr == 0 && Data == 0)
80 return Begin;
81 if (Begin == End)
82 return nullptr;
84 // As the instrumentation tracks the return address and not
85 // the address of the call to `__sanitizer_stat_report` we
86 // remove one from the address to get the correct DI.
87 if (Expected<DILineInfo> LineInfo =
88 Symbolizer.symbolizeCode(Filename, Addr - 1)) {
89 llvm::outs() << format_hex(Addr - 1, 18) << ' ' << LineInfo->FileName
90 << ':' << LineInfo->Line << ' ' << LineInfo->FunctionName
91 << ' ';
92 } else {
93 logAllUnhandledErrors(LineInfo.takeError(), llvm::outs(), "<error> ");
96 switch (KindFromData(Data, SizeofPtr)) {
97 case SanStat_CFI_VCall:
98 llvm::outs() << "cfi-vcall";
99 break;
100 case SanStat_CFI_NVCall:
101 llvm::outs() << "cfi-nvcall";
102 break;
103 case SanStat_CFI_DerivedCast:
104 llvm::outs() << "cfi-derived-cast";
105 break;
106 case SanStat_CFI_UnrelatedCast:
107 llvm::outs() << "cfi-unrelated-cast";
108 break;
109 case SanStat_CFI_ICall:
110 llvm::outs() << "cfi-icall";
111 break;
112 default:
113 llvm::outs() << "<unknown>";
114 break;
117 llvm::outs() << " " << CountFromData(Data, SizeofPtr) << '\n';
121 int main(int argc, char **argv) {
122 cl::ParseCommandLineOptions(argc, argv,
123 "Sanitizer Statistics Processing Tool");
125 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
126 MemoryBuffer::getFile(ClInputFile, -1, false);
127 if (!MBOrErr) {
128 errs() << argv[0] << ": " << ClInputFile << ": "
129 << MBOrErr.getError().message() << '\n';
130 return 1;
132 std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
133 const char *Begin = MB->getBufferStart(), *End = MB->getBufferEnd();
134 if (Begin == End) {
135 errs() << argv[0] << ": " << ClInputFile << ": short read\n";
136 return 1;
138 char SizeofPtr = *Begin++;
139 while (Begin != End) {
140 Begin = ReadModule(SizeofPtr, Begin, End);
141 if (Begin == nullptr) {
142 errs() << argv[0] << ": " << ClInputFile << ": short read\n";
143 return 1;
145 assert(Begin <= End);