• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- FuzzerTracePC.cpp - PC tracing--------------------------------------===//
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 // Trace PCs.
10 // This module implements __sanitizer_cov_trace_pc_guard[_init],
11 // the callback required for -fsanitize-coverage=trace-pc-guard instrumentation.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "FuzzerCorpus.h"
16 #include "FuzzerDefs.h"
17 #include "FuzzerDictionary.h"
18 #include "FuzzerExtFunctions.h"
19 #include "FuzzerIO.h"
20 #include "FuzzerTracePC.h"
21 #include "FuzzerValueBitMap.h"
22 #include <map>
23 #include <sanitizer/coverage_interface.h>
24 #include <set>
25 #include <sstream>
26 
27 namespace fuzzer {
28 
29 TracePC TPC;
30 
HandleTrace(uint32_t * Guard,uintptr_t PC)31 void TracePC::HandleTrace(uint32_t *Guard, uintptr_t PC) {
32   uint32_t Idx = *Guard;
33   if (!Idx) return;
34   PCs[Idx % kNumPCs] = PC;
35   Counters[Idx % kNumCounters]++;
36 }
37 
GetTotalPCCoverage()38 size_t TracePC::GetTotalPCCoverage() {
39   size_t Res = 0;
40   for (size_t i = 1; i < GetNumPCs(); i++)
41     if (PCs[i])
42       Res++;
43   return Res;
44 }
45 
HandleInit(uint32_t * Start,uint32_t * Stop)46 void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
47   if (Start == Stop || *Start) return;
48   assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
49   for (uint32_t *P = Start; P < Stop; P++)
50     *P = ++NumGuards;
51   Modules[NumModules].Start = Start;
52   Modules[NumModules].Stop = Stop;
53   NumModules++;
54 }
55 
PrintModuleInfo()56 void TracePC::PrintModuleInfo() {
57   Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
58   for (size_t i = 0; i < NumModules; i++)
59     Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
60   Printf("\n");
61 }
62 
HandleCallerCallee(uintptr_t Caller,uintptr_t Callee)63 void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
64   const uintptr_t kBits = 12;
65   const uintptr_t kMask = (1 << kBits) - 1;
66   uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
67   HandleValueProfile(Idx);
68 }
69 
IsInterestingCoverageFile(std::string & File)70 static bool IsInterestingCoverageFile(std::string &File) {
71   if (File.find("compiler-rt/lib/") != std::string::npos)
72     return false; // sanitizer internal.
73   if (File.find("/usr/lib/") != std::string::npos)
74     return false;
75   if (File.find("/usr/include/") != std::string::npos)
76     return false;
77   if (File == "<null>")
78     return false;
79   return true;
80 }
81 
PrintNewPCs()82 void TracePC::PrintNewPCs() {
83   if (DoPrintNewPCs) {
84     if (!PrintedPCs)
85       PrintedPCs = new std::set<uintptr_t>;
86     for (size_t i = 1; i < GetNumPCs(); i++)
87       if (PCs[i] && PrintedPCs->insert(PCs[i]).second)
88         PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs[i]);
89   }
90 }
91 
PrintCoverage()92 void TracePC::PrintCoverage() {
93   if (!EF->__sanitizer_symbolize_pc ||
94       !EF->__sanitizer_get_module_and_offset_for_pc) {
95     Printf("INFO: __sanitizer_symbolize_pc or "
96            "__sanitizer_get_module_and_offset_for_pc is not available,"
97            " not printing coverage\n");
98     return;
99   }
100   std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule;
101   std::map<std::string, uintptr_t> ModuleOffsets;
102   std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions,
103       CoveredLines;
104   Printf("COVERAGE:\n");
105   for (size_t i = 1; i < GetNumPCs(); i++) {
106     if (!PCs[i]) continue;
107     std::string FileStr = DescribePC("%s", PCs[i]);
108     if (!IsInterestingCoverageFile(FileStr)) continue;
109     std::string FixedPCStr = DescribePC("%p", PCs[i]);
110     std::string FunctionStr = DescribePC("%F", PCs[i]);
111     std::string LineStr = DescribePC("%l", PCs[i]);
112     char ModulePathRaw[4096] = "";  // What's PATH_MAX in portable C++?
113     void *OffsetRaw = nullptr;
114     if (!EF->__sanitizer_get_module_and_offset_for_pc(
115             reinterpret_cast<void *>(PCs[i]), ModulePathRaw,
116             sizeof(ModulePathRaw), &OffsetRaw))
117       continue;
118     std::string Module = ModulePathRaw;
119     uintptr_t FixedPC = std::stol(FixedPCStr, 0, 16);
120     uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw);
121     ModuleOffsets[Module] = FixedPC - PcOffset;
122     CoveredPCsPerModule[Module].push_back(PcOffset);
123     CoveredFunctions.insert(FunctionStr);
124     CoveredFiles.insert(FileStr);
125     CoveredDirs.insert(DirName(FileStr));
126     if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
127       continue;
128     Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
129            FileStr.c_str(), LineStr.c_str());
130   }
131 
132   std::string CoveredDirsStr;
133   for (auto &Dir : CoveredDirs) {
134     if (!CoveredDirsStr.empty())
135       CoveredDirsStr += ",";
136     CoveredDirsStr += Dir;
137   }
138   Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str());
139 
140   for (auto &M : CoveredPCsPerModule) {
141     std::set<std::string> UncoveredFiles, UncoveredFunctions;
142     std::map<std::string, std::set<int> > UncoveredLines;  // Func+File => lines
143     auto &ModuleName = M.first;
144     auto &CoveredOffsets = M.second;
145     uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
146     std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
147     Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
148     // sancov does not yet fully support DSOs.
149     // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
150     std::string Cmd = "objdump -d " + ModuleName +
151         " | grep 'call.*__sanitizer_cov_trace_pc_guard' | awk -F: '{print $1}'";
152     std::string SanCovOutput;
153     if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
154       Printf("INFO: Command failed: %s\n", Cmd.c_str());
155       continue;
156     }
157     std::istringstream ISS(SanCovOutput);
158     std::string S;
159     while (std::getline(ISS, S, '\n')) {
160       uintptr_t PcOffset = std::stol(S, 0, 16);
161       if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
162                               PcOffset)) {
163         uintptr_t PC = ModuleOffset + PcOffset;
164         auto FileStr = DescribePC("%s", PC);
165         if (!IsInterestingCoverageFile(FileStr)) continue;
166         if (CoveredFiles.count(FileStr) == 0) {
167           UncoveredFiles.insert(FileStr);
168           continue;
169         }
170         auto FunctionStr = DescribePC("%F", PC);
171         if (CoveredFunctions.count(FunctionStr) == 0) {
172           UncoveredFunctions.insert(FunctionStr);
173           continue;
174         }
175         std::string LineStr = DescribePC("%l", PC);
176         uintptr_t Line = std::stoi(LineStr);
177         std::string FileLineStr = FileStr + ":" + LineStr;
178         if (CoveredLines.count(FileLineStr) == 0)
179           UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
180       }
181     }
182     for (auto &FileLine: UncoveredLines)
183       for (int Line : FileLine.second)
184         Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
185     for (auto &Func : UncoveredFunctions)
186       Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
187     for (auto &File : UncoveredFiles)
188       Printf("UNCOVERED_FILE: %s\n", File.c_str());
189   }
190 }
191 
DumpCoverage()192 void TracePC::DumpCoverage() {
193   __sanitizer_dump_coverage(PCs, GetNumPCs());
194 }
195 
196 // Value profile.
197 // We keep track of various values that affect control flow.
198 // These values are inserted into a bit-set-based hash map.
199 // Every new bit in the map is treated as a new coverage.
200 //
201 // For memcmp/strcmp/etc the interesting value is the length of the common
202 // prefix of the parameters.
203 // For cmp instructions the interesting value is a XOR of the parameters.
204 // The interesting value is mixed up with the PC and is then added to the map.
205 
206 ATTRIBUTE_NO_SANITIZE_MEMORY
AddValueForMemcmp(void * caller_pc,const void * s1,const void * s2,size_t n)207 void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
208                               size_t n) {
209   if (!n) return;
210   size_t Len = std::min(n, (size_t)32);
211   const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
212   const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
213   size_t I = 0;
214   for (; I < Len; I++)
215     if (A1[I] != A2[I])
216       break;
217   size_t PC = reinterpret_cast<size_t>(caller_pc);
218   size_t Idx = I;
219   // if (I < Len)
220   //  Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
221   TPC.HandleValueProfile((PC & 4095) | (Idx << 12));
222 }
223 
224 ATTRIBUTE_NO_SANITIZE_MEMORY
AddValueForStrcmp(void * caller_pc,const char * s1,const char * s2,size_t n)225 void TracePC::AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
226                               size_t n) {
227   if (!n) return;
228   size_t Len = std::min(n, (size_t)32);
229   const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
230   const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
231   size_t I = 0;
232   for (; I < Len; I++)
233     if (A1[I] != A2[I] || A1[I] == 0)
234       break;
235   size_t PC = reinterpret_cast<size_t>(caller_pc);
236   size_t Idx = I;
237   // if (I < Len && A1[I])
238   //  Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
239   TPC.HandleValueProfile((PC & 4095) | (Idx << 12));
240 }
241 
242 template <class T>
243 ATTRIBUTE_TARGET_POPCNT
244 #ifdef __clang__  // g++ can't handle this __attribute__ here :(
245 __attribute__((always_inline))
246 #endif  // __clang__
HandleCmp(void * PC,T Arg1,T Arg2)247 void TracePC::HandleCmp(void *PC, T Arg1, T Arg2) {
248   uintptr_t PCuint = reinterpret_cast<uintptr_t>(PC);
249   uint64_t ArgXor = Arg1 ^ Arg2;
250   uint64_t ArgDistance = __builtin_popcountl(ArgXor) + 1; // [1,65]
251   uintptr_t Idx = ((PCuint & 4095) + 1) * ArgDistance;
252   if (sizeof(T) == 4)
253       TORC4.Insert(ArgXor, Arg1, Arg2);
254   else if (sizeof(T) == 8)
255       TORC8.Insert(ArgXor, Arg1, Arg2);
256   HandleValueProfile(Idx);
257 }
258 
259 } // namespace fuzzer
260 
261 extern "C" {
262 __attribute__((visibility("default")))
__sanitizer_cov_trace_pc_guard(uint32_t * Guard)263 void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
264   uintptr_t PC = (uintptr_t)__builtin_return_address(0);
265   fuzzer::TPC.HandleTrace(Guard, PC);
266 }
267 
268 __attribute__((visibility("default")))
__sanitizer_cov_trace_pc_guard_init(uint32_t * Start,uint32_t * Stop)269 void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
270   fuzzer::TPC.HandleInit(Start, Stop);
271 }
272 
273 __attribute__((visibility("default")))
__sanitizer_cov_trace_pc_indir(uintptr_t Callee)274 void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
275   uintptr_t PC = (uintptr_t)__builtin_return_address(0);
276   fuzzer::TPC.HandleCallerCallee(PC, Callee);
277 }
278 
279 __attribute__((visibility("default")))
__sanitizer_cov_trace_cmp8(uint64_t Arg1,uint64_t Arg2)280 void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
281   fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
282 }
283 __attribute__((visibility("default")))
__sanitizer_cov_trace_cmp4(uint32_t Arg1,uint32_t Arg2)284 void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
285   fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
286 }
287 __attribute__((visibility("default")))
__sanitizer_cov_trace_cmp2(uint16_t Arg1,uint16_t Arg2)288 void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
289   fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
290 }
291 __attribute__((visibility("default")))
__sanitizer_cov_trace_cmp1(uint8_t Arg1,uint8_t Arg2)292 void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
293   fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
294 }
295 
296 __attribute__((visibility("default")))
__sanitizer_cov_trace_switch(uint64_t Val,uint64_t * Cases)297 void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
298   // Updates the value profile based on the relative position of Val and Cases.
299   // We want to handle one random case at every call (handling all is slow).
300   // Since none of the arguments contain any random bits we use a thread-local
301   // counter to choose the random case to handle.
302   static thread_local size_t Counter;
303   Counter++;
304   uint64_t N = Cases[0];
305   uint64_t *Vals = Cases + 2;
306   char *PC = (char*)__builtin_return_address(0);
307   // We need a random number < N using Counter as a seed. But w/o DIV.
308   // * find a power of two >= N
309   // * mask Counter with this power of two.
310   // * maybe subtract N.
311   size_t Nlog = sizeof(long) * 8 - __builtin_clzl((long)N);
312   size_t PowerOfTwoGeN = 1U << Nlog;
313   assert(PowerOfTwoGeN >= N);
314   size_t Idx = Counter & (PowerOfTwoGeN - 1);
315   if (Idx >= N)
316     Idx -= N;
317   assert(Idx < N);
318   uint64_t TwoIn32 = 1ULL << 32;
319   if ((Val | Vals[Idx]) < TwoIn32)
320     fuzzer::TPC.HandleCmp(PC + Idx, static_cast<uint32_t>(Val),
321                           static_cast<uint32_t>(Vals[Idx]));
322   else
323     fuzzer::TPC.HandleCmp(PC + Idx, Val, Vals[Idx]);
324 }
325 
326 __attribute__((visibility("default")))
__sanitizer_cov_trace_div4(uint32_t Val)327 void __sanitizer_cov_trace_div4(uint32_t Val) {
328   fuzzer::TPC.HandleCmp(__builtin_return_address(0), Val, (uint32_t)0);
329 }
330 __attribute__((visibility("default")))
__sanitizer_cov_trace_div8(uint64_t Val)331 void __sanitizer_cov_trace_div8(uint64_t Val) {
332   fuzzer::TPC.HandleCmp(__builtin_return_address(0), Val, (uint64_t)0);
333 }
334 __attribute__((visibility("default")))
__sanitizer_cov_trace_gep(uintptr_t Idx)335 void __sanitizer_cov_trace_gep(uintptr_t Idx) {
336   fuzzer::TPC.HandleCmp(__builtin_return_address(0), Idx, (uintptr_t)0);
337 }
338 
339 }  // extern "C"
340