1 //===-- memprof_interceptors_memintrinsics.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 is a part of MemProfiler, a memory profiler.
10 //
11 // MemProf-private header for memprof_interceptors_memintrinsics.cpp
12 //===---------------------------------------------------------------------===//
13 #ifndef MEMPROF_MEMINTRIN_H
14 #define MEMPROF_MEMINTRIN_H
15 
16 #include "interception/interception.h"
17 #include "memprof_interface_internal.h"
18 #include "memprof_internal.h"
19 #include "memprof_mapping.h"
20 
21 DECLARE_REAL(void *, memcpy, void *to, const void *from, uptr size)
22 DECLARE_REAL(void *, memset, void *block, int c, uptr size)
23 
24 namespace __memprof {
25 
26 // We implement ACCESS_MEMORY_RANGE, MEMPROF_READ_RANGE,
27 // and MEMPROF_WRITE_RANGE as macro instead of function so
28 // that no extra frames are created, and stack trace contains
29 // relevant information only.
30 #define ACCESS_MEMORY_RANGE(offset, size)                                      \
31   do {                                                                         \
32     __memprof_record_access_range(offset, size);                               \
33   } while (0)
34 
35 // memcpy is called during __memprof_init() from the internals of printf(...).
36 // We do not treat memcpy with to==from as a bug.
37 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
38 #define MEMPROF_MEMCPY_IMPL(to, from, size)                                    \
39   do {                                                                         \
40     if (UNLIKELY(!memprof_inited))                                             \
41       return internal_memcpy(to, from, size);                                  \
42     if (memprof_init_is_running) {                                             \
43       return REAL(memcpy)(to, from, size);                                     \
44     }                                                                          \
45     ENSURE_MEMPROF_INITED();                                                   \
46     MEMPROF_READ_RANGE(from, size);                                            \
47     MEMPROF_WRITE_RANGE(to, size);                                             \
48     return REAL(memcpy)(to, from, size);                                       \
49   } while (0)
50 
51 // memset is called inside Printf.
52 #define MEMPROF_MEMSET_IMPL(block, c, size)                                    \
53   do {                                                                         \
54     if (UNLIKELY(!memprof_inited))                                             \
55       return internal_memset(block, c, size);                                  \
56     if (memprof_init_is_running) {                                             \
57       return REAL(memset)(block, c, size);                                     \
58     }                                                                          \
59     ENSURE_MEMPROF_INITED();                                                   \
60     MEMPROF_WRITE_RANGE(block, size);                                          \
61     return REAL(memset)(block, c, size);                                       \
62   } while (0)
63 
64 #define MEMPROF_MEMMOVE_IMPL(to, from, size)                                   \
65   do {                                                                         \
66     if (UNLIKELY(!memprof_inited))                                             \
67       return internal_memmove(to, from, size);                                 \
68     ENSURE_MEMPROF_INITED();                                                   \
69     MEMPROF_READ_RANGE(from, size);                                            \
70     MEMPROF_WRITE_RANGE(to, size);                                             \
71     return internal_memmove(to, from, size);                                   \
72   } while (0)
73 
74 #define MEMPROF_READ_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size)
75 #define MEMPROF_WRITE_RANGE(offset, size) ACCESS_MEMORY_RANGE(offset, size)
76 
77 } // namespace __memprof
78 
79 #endif // MEMPROF_MEMINTRIN_H
80