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