1//===-- copyprof_interceptors.h -------------------------------*- C++ -*-===//
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 file declares the standard library interceptor interface for CopyProf,
10/// defining hooks for intercepted memory and string functions.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef COPYPROF_INTERCEPTORS_H
15#define COPYPROF_INTERCEPTORS_H
16
17#include "copyprof_interface_internal.h"
18#include "copyprof_internal.h"
19#include "interception/interception.h"
20
21// Functions whose interceptor is defined in another TU.
22DECLARE_REAL(void*, memset, void* block, int c, SIZE_T size)
23DECLARE_REAL(void*, memcpy, void* to, const void* from, SIZE_T size)
24
25// Functions whose interception and interceptor are defined in another TU.
26DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, SIZE_T)
27DECLARE_REAL_AND_INTERCEPTOR(void, free, void*)
28
29// The macros below must be defined when using the interceptor infrastructure
30// (i.e. sanitizer_common_interceptors*.inc files).
31#define COPYPROF_INTERCEPT_FUNC(name) \
32 do { \
33 if (!INTERCEPT_FUNCTION(name)) \
34 VReport(1, "CopyProf: failed to intercept '%s'\n", #name); \
35 } while (0)
36#define COMMON_INTERCEPT_FUNCTION(name) COPYPROF_INTERCEPT_FUNC(name)
37
38#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
39 do { \
40 ctx = 0; \
41 (void)ctx; \
42 if (copyprof_init_is_running) \
43 return REAL(func)(__VA_ARGS__); \
44 if (UNLIKELY(!copyprof_is_initialized)) \
45 __copyprof_init_once(); \
46 } while (false)
47
48#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
49 do { \
50 (void)(ctx); \
51 (void)(ptr); \
52 (void)(size); \
53 } while (false)
54
55#define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
56 __copyprof_store_callback(ptr, size)
57
58#define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!copyprof_is_initialized)
59
60namespace __copyprof {
61
62// Initializes the interception infrastructure, s.t. CopyProf can hook into
63// library functions or syscalls. Must be called once during startup.
64void InitializeInterceptors();
65
66} // namespace __copyprof
67
68#endif // COPYPROF_INTERCEPTORS_H
69