1*3cab2bb3Spatrick /*===- DataFlowCallbacks.cpp - a standalone DataFlow trace -------===//
2*3cab2bb3Spatrick //
3*3cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*3cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*3cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*3cab2bb3Spatrick //
7*3cab2bb3Spatrick //===----------------------------------------------------------------------===//
8*3cab2bb3Spatrick // Instrumentation callbacks for DataFlow.cpp.
9*3cab2bb3Spatrick // These functions should not be instrumented by DFSan, so we
10*3cab2bb3Spatrick // keep them in a separate file and compile it w/o DFSan.
11*3cab2bb3Spatrick //===----------------------------------------------------------------------===*/
12*3cab2bb3Spatrick #include "DataFlow.h"
13*3cab2bb3Spatrick
14*3cab2bb3Spatrick #include <cassert>
15*3cab2bb3Spatrick #include <cstdio>
16*3cab2bb3Spatrick #include <cstdlib>
17*3cab2bb3Spatrick
18*3cab2bb3Spatrick static __thread size_t CurrentFunc;
19*3cab2bb3Spatrick static uint32_t *GuardsBeg, *GuardsEnd;
BlockIsEntry(size_t BlockIdx)20*3cab2bb3Spatrick static inline bool BlockIsEntry(size_t BlockIdx) {
21*3cab2bb3Spatrick return __dft.PCsBeg[BlockIdx * 2 + 1] & PCFLAG_FUNC_ENTRY;
22*3cab2bb3Spatrick }
23*3cab2bb3Spatrick
24*3cab2bb3Spatrick extern "C" {
25*3cab2bb3Spatrick
__sanitizer_cov_trace_pc_guard_init(uint32_t * start,uint32_t * stop)26*3cab2bb3Spatrick void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
27*3cab2bb3Spatrick uint32_t *stop) {
28*3cab2bb3Spatrick assert(__dft.NumFuncs == 0 && "This tool does not support DSOs");
29*3cab2bb3Spatrick assert(start < stop && "The code is not instrumented for coverage");
30*3cab2bb3Spatrick if (start == stop || *start) return; // Initialize only once.
31*3cab2bb3Spatrick GuardsBeg = start;
32*3cab2bb3Spatrick GuardsEnd = stop;
33*3cab2bb3Spatrick }
34*3cab2bb3Spatrick
__sanitizer_cov_pcs_init(const uintptr_t * pcs_beg,const uintptr_t * pcs_end)35*3cab2bb3Spatrick void __sanitizer_cov_pcs_init(const uintptr_t *pcs_beg,
36*3cab2bb3Spatrick const uintptr_t *pcs_end) {
37*3cab2bb3Spatrick if (__dft.NumGuards) return; // Initialize only once.
38*3cab2bb3Spatrick __dft.NumGuards = GuardsEnd - GuardsBeg;
39*3cab2bb3Spatrick __dft.PCsBeg = pcs_beg;
40*3cab2bb3Spatrick __dft.PCsEnd = pcs_end;
41*3cab2bb3Spatrick assert(__dft.NumGuards == (__dft.PCsEnd - __dft.PCsBeg) / 2);
42*3cab2bb3Spatrick for (size_t i = 0; i < __dft.NumGuards; i++) {
43*3cab2bb3Spatrick if (BlockIsEntry(i)) {
44*3cab2bb3Spatrick __dft.NumFuncs++;
45*3cab2bb3Spatrick GuardsBeg[i] = __dft.NumFuncs;
46*3cab2bb3Spatrick }
47*3cab2bb3Spatrick }
48*3cab2bb3Spatrick __dft.BBExecuted = (bool*)calloc(__dft.NumGuards, sizeof(bool));
49*3cab2bb3Spatrick fprintf(stderr, "INFO: %zd instrumented function(s) observed "
50*3cab2bb3Spatrick "and %zd basic blocks\n", __dft.NumFuncs, __dft.NumGuards);
51*3cab2bb3Spatrick }
52*3cab2bb3Spatrick
__sanitizer_cov_trace_pc_indir(uint64_t x)53*3cab2bb3Spatrick void __sanitizer_cov_trace_pc_indir(uint64_t x){} // unused.
54*3cab2bb3Spatrick
__sanitizer_cov_trace_pc_guard(uint32_t * guard)55*3cab2bb3Spatrick void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
56*3cab2bb3Spatrick size_t GuardIdx = guard - GuardsBeg;
57*3cab2bb3Spatrick // assert(GuardIdx < __dft.NumGuards);
58*3cab2bb3Spatrick __dft.BBExecuted[GuardIdx] = true;
59*3cab2bb3Spatrick if (!*guard) return; // not a function entry.
60*3cab2bb3Spatrick uint32_t FuncNum = *guard - 1; // Guards start from 1.
61*3cab2bb3Spatrick // assert(FuncNum < __dft.NumFuncs);
62*3cab2bb3Spatrick CurrentFunc = FuncNum;
63*3cab2bb3Spatrick }
64*3cab2bb3Spatrick
__dfsw___sanitizer_cov_trace_switch(uint64_t Val,uint64_t * Cases,dfsan_label L1,dfsan_label UnusedL)65*3cab2bb3Spatrick void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases,
66*3cab2bb3Spatrick dfsan_label L1, dfsan_label UnusedL) {
67*3cab2bb3Spatrick assert(CurrentFunc < __dft.NumFuncs);
68*3cab2bb3Spatrick __dft.FuncLabels[CurrentFunc] |= L1;
69*3cab2bb3Spatrick }
70*3cab2bb3Spatrick
71*3cab2bb3Spatrick #define HOOK(Name, Type) \
72*3cab2bb3Spatrick void Name(Type Arg1, Type Arg2, dfsan_label L1, dfsan_label L2) { \
73*3cab2bb3Spatrick __dft.FuncLabels[CurrentFunc] |= L1 | L2; \
74*3cab2bb3Spatrick }
75*3cab2bb3Spatrick //assert(CurrentFunc < __dft.NumFuncs);
76*3cab2bb3Spatrick
77*3cab2bb3Spatrick HOOK(__dfsw___sanitizer_cov_trace_const_cmp1, uint8_t)
78*3cab2bb3Spatrick HOOK(__dfsw___sanitizer_cov_trace_const_cmp2, uint16_t)
79*3cab2bb3Spatrick HOOK(__dfsw___sanitizer_cov_trace_const_cmp4, uint32_t)
80*3cab2bb3Spatrick HOOK(__dfsw___sanitizer_cov_trace_const_cmp8, uint64_t)
81*3cab2bb3Spatrick HOOK(__dfsw___sanitizer_cov_trace_cmp1, uint8_t)
82*3cab2bb3Spatrick HOOK(__dfsw___sanitizer_cov_trace_cmp2, uint16_t)
83*3cab2bb3Spatrick HOOK(__dfsw___sanitizer_cov_trace_cmp4, uint32_t)
84*3cab2bb3Spatrick HOOK(__dfsw___sanitizer_cov_trace_cmp8, uint64_t)
85*3cab2bb3Spatrick
86*3cab2bb3Spatrick } // extern "C"
87