memprof_interceptors_memintrinsics.cpp 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===-- memprof_interceptors_memintrinsics.cpp ---------------------------===//
  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 versions of memcpy, memmove, and memset.
  12. //===---------------------------------------------------------------------===//
  13. #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS
  14. #include "memprof_interceptors_memintrinsics.h"
  15. #include "memprof_interceptors.h"
  16. #include "memprof_stack.h"
  17. using namespace __memprof;
  18. // memcpy is called during __memprof_init() from the internals of printf(...).
  19. // We do not treat memcpy with to==from as a bug.
  20. // See http://llvm.org/bugs/show_bug.cgi?id=11763.
  21. #define MEMPROF_MEMCPY_IMPL(to, from, size) \
  22. do { \
  23. if (UNLIKELY(!memprof_inited)) \
  24. return internal_memcpy(to, from, size); \
  25. if (memprof_init_is_running) { \
  26. return REAL(memcpy)(to, from, size); \
  27. } \
  28. ENSURE_MEMPROF_INITED(); \
  29. MEMPROF_READ_RANGE(from, size); \
  30. MEMPROF_WRITE_RANGE(to, size); \
  31. return REAL(memcpy)(to, from, size); \
  32. } while (0)
  33. // memset is called inside Printf.
  34. #define MEMPROF_MEMSET_IMPL(block, c, size) \
  35. do { \
  36. if (UNLIKELY(!memprof_inited)) \
  37. return internal_memset(block, c, size); \
  38. if (memprof_init_is_running) { \
  39. return REAL(memset)(block, c, size); \
  40. } \
  41. ENSURE_MEMPROF_INITED(); \
  42. MEMPROF_WRITE_RANGE(block, size); \
  43. return REAL(memset)(block, c, size); \
  44. } while (0)
  45. #define MEMPROF_MEMMOVE_IMPL(to, from, size) \
  46. do { \
  47. if (UNLIKELY(!memprof_inited)) \
  48. return internal_memmove(to, from, size); \
  49. ENSURE_MEMPROF_INITED(); \
  50. MEMPROF_READ_RANGE(from, size); \
  51. MEMPROF_WRITE_RANGE(to, size); \
  52. return internal_memmove(to, from, size); \
  53. } while (0)
  54. #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
  55. do { \
  56. MEMPROF_INTERCEPTOR_ENTER(ctx, memmove); \
  57. MEMPROF_MEMMOVE_IMPL(to, from, size); \
  58. } while (false)
  59. #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
  60. do { \
  61. MEMPROF_INTERCEPTOR_ENTER(ctx, memcpy); \
  62. MEMPROF_MEMCPY_IMPL(to, from, size); \
  63. } while (false)
  64. #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
  65. do { \
  66. MEMPROF_INTERCEPTOR_ENTER(ctx, memset); \
  67. MEMPROF_MEMSET_IMPL(block, c, size); \
  68. } while (false)
  69. #include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"
  70. void *__memprof_memcpy(void *to, const void *from, uptr size) {
  71. MEMPROF_MEMCPY_IMPL(to, from, size);
  72. }
  73. void *__memprof_memset(void *block, int c, uptr size) {
  74. MEMPROF_MEMSET_IMPL(block, c, size);
  75. }
  76. void *__memprof_memmove(void *to, const void *from, uptr size) {
  77. MEMPROF_MEMMOVE_IMPL(to, from, size);
  78. }