1e8d8bef9SDimitry Andric //===-- memprof_interceptors_memintrinsics.cpp ---------------------------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===---------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // This file is a part of MemProfiler, a memory profiler.
10e8d8bef9SDimitry Andric //
11e8d8bef9SDimitry Andric // MemProf versions of memcpy, memmove, and memset.
12e8d8bef9SDimitry Andric //===---------------------------------------------------------------------===//
13e8d8bef9SDimitry Andric
14*06c3fb27SDimitry Andric #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS
15*06c3fb27SDimitry Andric
16e8d8bef9SDimitry Andric #include "memprof_interceptors_memintrinsics.h"
17*06c3fb27SDimitry Andric
18*06c3fb27SDimitry Andric #include "memprof_interceptors.h"
19e8d8bef9SDimitry Andric #include "memprof_stack.h"
20e8d8bef9SDimitry Andric
21e8d8bef9SDimitry Andric using namespace __memprof;
22e8d8bef9SDimitry Andric
23*06c3fb27SDimitry Andric // memcpy is called during __memprof_init() from the internals of printf(...).
24*06c3fb27SDimitry Andric // We do not treat memcpy with to==from as a bug.
25*06c3fb27SDimitry Andric // See http://llvm.org/bugs/show_bug.cgi?id=11763.
26*06c3fb27SDimitry Andric #define MEMPROF_MEMCPY_IMPL(to, from, size) \
27*06c3fb27SDimitry Andric do { \
28*06c3fb27SDimitry Andric if (UNLIKELY(!memprof_inited)) \
29*06c3fb27SDimitry Andric return internal_memcpy(to, from, size); \
30*06c3fb27SDimitry Andric if (memprof_init_is_running) { \
31*06c3fb27SDimitry Andric return REAL(memcpy)(to, from, size); \
32*06c3fb27SDimitry Andric } \
33*06c3fb27SDimitry Andric ENSURE_MEMPROF_INITED(); \
34*06c3fb27SDimitry Andric MEMPROF_READ_RANGE(from, size); \
35*06c3fb27SDimitry Andric MEMPROF_WRITE_RANGE(to, size); \
36*06c3fb27SDimitry Andric return REAL(memcpy)(to, from, size); \
37*06c3fb27SDimitry Andric } while (0)
38*06c3fb27SDimitry Andric
39*06c3fb27SDimitry Andric // memset is called inside Printf.
40*06c3fb27SDimitry Andric #define MEMPROF_MEMSET_IMPL(block, c, size) \
41*06c3fb27SDimitry Andric do { \
42*06c3fb27SDimitry Andric if (UNLIKELY(!memprof_inited)) \
43*06c3fb27SDimitry Andric return internal_memset(block, c, size); \
44*06c3fb27SDimitry Andric if (memprof_init_is_running) { \
45*06c3fb27SDimitry Andric return REAL(memset)(block, c, size); \
46*06c3fb27SDimitry Andric } \
47*06c3fb27SDimitry Andric ENSURE_MEMPROF_INITED(); \
48*06c3fb27SDimitry Andric MEMPROF_WRITE_RANGE(block, size); \
49*06c3fb27SDimitry Andric return REAL(memset)(block, c, size); \
50*06c3fb27SDimitry Andric } while (0)
51*06c3fb27SDimitry Andric
52*06c3fb27SDimitry Andric #define MEMPROF_MEMMOVE_IMPL(to, from, size) \
53*06c3fb27SDimitry Andric do { \
54*06c3fb27SDimitry Andric if (UNLIKELY(!memprof_inited)) \
55*06c3fb27SDimitry Andric return internal_memmove(to, from, size); \
56*06c3fb27SDimitry Andric ENSURE_MEMPROF_INITED(); \
57*06c3fb27SDimitry Andric MEMPROF_READ_RANGE(from, size); \
58*06c3fb27SDimitry Andric MEMPROF_WRITE_RANGE(to, size); \
59*06c3fb27SDimitry Andric return internal_memmove(to, from, size); \
60*06c3fb27SDimitry Andric } while (0)
61*06c3fb27SDimitry Andric
62*06c3fb27SDimitry Andric #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
63*06c3fb27SDimitry Andric do { \
64*06c3fb27SDimitry Andric MEMPROF_INTERCEPTOR_ENTER(ctx, memmove); \
65*06c3fb27SDimitry Andric MEMPROF_MEMMOVE_IMPL(to, from, size); \
66*06c3fb27SDimitry Andric } while (false)
67*06c3fb27SDimitry Andric
68*06c3fb27SDimitry Andric #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
69*06c3fb27SDimitry Andric do { \
70*06c3fb27SDimitry Andric MEMPROF_INTERCEPTOR_ENTER(ctx, memcpy); \
71*06c3fb27SDimitry Andric MEMPROF_MEMCPY_IMPL(to, from, size); \
72*06c3fb27SDimitry Andric } while (false)
73*06c3fb27SDimitry Andric
74*06c3fb27SDimitry Andric #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
75*06c3fb27SDimitry Andric do { \
76*06c3fb27SDimitry Andric MEMPROF_INTERCEPTOR_ENTER(ctx, memset); \
77*06c3fb27SDimitry Andric MEMPROF_MEMSET_IMPL(block, c, size); \
78*06c3fb27SDimitry Andric } while (false)
79*06c3fb27SDimitry Andric
80*06c3fb27SDimitry Andric #include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"
81*06c3fb27SDimitry Andric
__memprof_memcpy(void * to,const void * from,uptr size)82e8d8bef9SDimitry Andric void *__memprof_memcpy(void *to, const void *from, uptr size) {
83e8d8bef9SDimitry Andric MEMPROF_MEMCPY_IMPL(to, from, size);
84e8d8bef9SDimitry Andric }
85e8d8bef9SDimitry Andric
__memprof_memset(void * block,int c,uptr size)86e8d8bef9SDimitry Andric void *__memprof_memset(void *block, int c, uptr size) {
87e8d8bef9SDimitry Andric MEMPROF_MEMSET_IMPL(block, c, size);
88e8d8bef9SDimitry Andric }
89e8d8bef9SDimitry Andric
__memprof_memmove(void * to,const void * from,uptr size)90e8d8bef9SDimitry Andric void *__memprof_memmove(void *to, const void *from, uptr size) {
91e8d8bef9SDimitry Andric MEMPROF_MEMMOVE_IMPL(to, from, size);
92e8d8bef9SDimitry Andric }
93