1 //===-- memprof_interceptors_memintrinsics.cpp ---------------------------===//
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
7 //===---------------------------------------------------------------------===//
9 // This file is a part of MemProfiler, a memory profiler.
11 // MemProf versions of memcpy, memmove, and memset.
12 //===---------------------------------------------------------------------===//
14 #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS
16 #include "memprof_interceptors_memintrinsics.h"
18 #include "memprof_interceptors.h"
19 #include "memprof_stack.h"
21 using namespace __memprof
;
23 // memcpy is called during __memprof_init() from the internals of printf(...).
24 // We do not treat memcpy with to==from as a bug.
25 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
26 #define MEMPROF_MEMCPY_IMPL(to, from, size) \
28 if (UNLIKELY(!memprof_inited)) \
29 return internal_memcpy(to, from, size); \
30 if (memprof_init_is_running) { \
31 return REAL(memcpy)(to, from, size); \
33 ENSURE_MEMPROF_INITED(); \
34 MEMPROF_READ_RANGE(from, size); \
35 MEMPROF_WRITE_RANGE(to, size); \
36 return REAL(memcpy)(to, from, size); \
39 // memset is called inside Printf.
40 #define MEMPROF_MEMSET_IMPL(block, c, size) \
42 if (UNLIKELY(!memprof_inited)) \
43 return internal_memset(block, c, size); \
44 if (memprof_init_is_running) { \
45 return REAL(memset)(block, c, size); \
47 ENSURE_MEMPROF_INITED(); \
48 MEMPROF_WRITE_RANGE(block, size); \
49 return REAL(memset)(block, c, size); \
52 #define MEMPROF_MEMMOVE_IMPL(to, from, size) \
54 if (UNLIKELY(!memprof_inited)) \
55 return internal_memmove(to, from, size); \
56 ENSURE_MEMPROF_INITED(); \
57 MEMPROF_READ_RANGE(from, size); \
58 MEMPROF_WRITE_RANGE(to, size); \
59 return internal_memmove(to, from, size); \
62 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
64 MEMPROF_INTERCEPTOR_ENTER(ctx, memmove); \
65 MEMPROF_MEMMOVE_IMPL(to, from, size); \
68 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
70 MEMPROF_INTERCEPTOR_ENTER(ctx, memcpy); \
71 MEMPROF_MEMCPY_IMPL(to, from, size); \
74 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
76 MEMPROF_INTERCEPTOR_ENTER(ctx, memset); \
77 MEMPROF_MEMSET_IMPL(block, c, size); \
80 #include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"
82 void *__memprof_memcpy(void *to
, const void *from
, uptr size
) {
83 MEMPROF_MEMCPY_IMPL(to
, from
, size
);
86 void *__memprof_memset(void *block
, int c
, uptr size
) {
87 MEMPROF_MEMSET_IMPL(block
, c
, size
);
90 void *__memprof_memmove(void *to
, const void *from
, uptr size
) {
91 MEMPROF_MEMMOVE_IMPL(to
, from
, size
);