blob: 53341d3632ee287201f6f7632bbfc036be2a4137 [file] [log] [blame]
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +00001//===- 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, a callback required
11// for -fsanitize-coverage=trace-pc instrumentation.
12//
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000013//===----------------------------------------------------------------------===//
14
15#include "FuzzerInternal.h"
16
17namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000018
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000019static size_t PreviouslyComputedPCHash;
20static ValueBitMap CurrentPCMap;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000021
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000022// Merges CurrentPCMap into M, returns the number of new bits.
23size_t PCMapMergeFromCurrent(ValueBitMap &M) {
24 if (!PreviouslyComputedPCHash)
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000025 return 0;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000026 PreviouslyComputedPCHash = 0;
27 return M.MergeFrom(CurrentPCMap);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000028}
29
Kostya Serebryany2d4f8f12016-02-27 01:50:16 +000030static void HandlePC(uint32_t PC) {
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000031 // We take 12 bits of PC and mix it with the previous PCs.
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000032 uintptr_t Next = (PreviouslyComputedPCHash << 5) ^ (PC & 4095);
33 CurrentPCMap.AddValue(Next);
34 PreviouslyComputedPCHash = Next;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000035}
36
37} // namespace fuzzer
38
Dan Liew59144072016-06-06 20:27:09 +000039extern "C" {
40void __sanitizer_cov_trace_pc() {
Kostya Serebryany2d4f8f12016-02-27 01:50:16 +000041 fuzzer::HandlePC(static_cast<uint32_t>(
42 reinterpret_cast<uintptr_t>(__builtin_return_address(0))));
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000043}
Dan Liew59144072016-06-06 20:27:09 +000044
45void __sanitizer_cov_trace_pc_indir(int *) {
46 // Stub to allow linking with code built with
47 // -fsanitize=indirect-calls,trace-pc.
48 // This isn't used currently.
49}
50}