1 //===- sanstats.cpp - Sanitizer statistics dumper -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tool dumps statistics information from files in the format produced
11 // by clang's -fsanitize-stats feature.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/DebugInfo/Symbolize/Symbolize.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Transforms/Utils/SanitizerStats.h"
20 #include <stdint.h>
21
22 using namespace llvm;
23
24 static cl::opt<std::string> ClInputFile(cl::Positional, cl::Required,
25 cl::desc("<filename>"));
26
27 static cl::opt<bool> ClDemangle("demangle", cl::init(false),
28 cl::desc("Print demangled function name."));
29
KindFromData(uint64_t Data,char SizeofPtr)30 inline uint64_t KindFromData(uint64_t Data, char SizeofPtr) {
31 return Data >> (SizeofPtr * 8 - kSanitizerStatKindBits);
32 }
33
CountFromData(uint64_t Data,char SizeofPtr)34 inline uint64_t CountFromData(uint64_t Data, char SizeofPtr) {
35 return Data & ((1ull << (SizeofPtr * 8 - kSanitizerStatKindBits)) - 1);
36 }
37
ReadLE(char Size,const char * Begin,const char * End)38 uint64_t ReadLE(char Size, const char *Begin, const char *End) {
39 uint64_t Result = 0;
40 char Pos = 0;
41 while (Begin < End && Pos != Size) {
42 Result |= uint64_t(uint8_t(*Begin)) << (Pos * 8);
43 ++Begin;
44 ++Pos;
45 }
46 return Result;
47 }
48
ReadModule(char SizeofPtr,const char * Begin,const char * End)49 const char *ReadModule(char SizeofPtr, const char *Begin, const char *End) {
50 const char *FilenameBegin = Begin;
51 while (Begin != End && *Begin)
52 ++Begin;
53 if (Begin == End)
54 return nullptr;
55 StringRef Filename(FilenameBegin, Begin - FilenameBegin);
56
57 ++Begin;
58 if (Begin == End)
59 return nullptr;
60
61 symbolize::LLVMSymbolizer::Options SymbolizerOptions;
62 SymbolizerOptions.Demangle = ClDemangle;
63 SymbolizerOptions.UseSymbolTable = true;
64 symbolize::LLVMSymbolizer Symbolizer(SymbolizerOptions);
65
66 while (1) {
67 uint64_t Addr = ReadLE(SizeofPtr, Begin, End);
68 Begin += SizeofPtr;
69 uint64_t Data = ReadLE(SizeofPtr, Begin, End);
70 Begin += SizeofPtr;
71
72 if (Begin > End)
73 return nullptr;
74 if (Addr == 0 && Data == 0)
75 return Begin;
76 if (Begin == End)
77 return nullptr;
78
79 if (Expected<DILineInfo> LineInfo =
80 Symbolizer.symbolizeCode(Filename, Addr)) {
81 llvm::outs() << LineInfo->FileName << ':' << LineInfo->Line << ' '
82 << LineInfo->FunctionName << ' ';
83 } else {
84 logAllUnhandledErrors(LineInfo.takeError(), llvm::outs(), "<error> ");
85 }
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
108 llvm::outs() << " " << CountFromData(Data, SizeofPtr) << '\n';
109 }
110 }
111
main(int argc,char ** argv)112 int main(int argc, char **argv) {
113 cl::ParseCommandLineOptions(argc, argv,
114 "Sanitizer Statistics Processing Tool");
115
116 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
117 MemoryBuffer::getFile(ClInputFile, -1, false);
118 if (!MBOrErr) {
119 errs() << argv[0] << ": " << ClInputFile << ": "
120 << MBOrErr.getError().message() << '\n';
121 return 1;
122 }
123 std::unique_ptr<MemoryBuffer> MB = std::move(MBOrErr.get());
124 const char *Begin = MB->getBufferStart(), *End = MB->getBufferEnd();
125 if (Begin == End) {
126 errs() << argv[0] << ": " << ClInputFile << ": short read\n";
127 return 1;
128 }
129 char SizeofPtr = *Begin++;
130 while (Begin != End) {
131 Begin = ReadModule(SizeofPtr, Begin, End);
132 if (Begin == nullptr) {
133 errs() << argv[0] << ": " << ClInputFile << ": short read\n";
134 return 1;
135 }
136 assert(Begin <= End);
137 }
138 }
139