MemoryChecking.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * Provides a common interface to the ASan (AddressSanitizer) and Valgrind
  7. * functions used to mark memory in certain ways. In detail, the following
  8. * three macros are provided:
  9. *
  10. * MOZ_MAKE_MEM_NOACCESS - Mark memory as unsafe to access (e.g. freed)
  11. * MOZ_MAKE_MEM_UNDEFINED - Mark memory as accessible, with content undefined
  12. * MOZ_MAKE_MEM_DEFINED - Mark memory as accessible, with content defined
  13. *
  14. * With Valgrind in use, these directly map to the three respective Valgrind
  15. * macros. With ASan in use, the NOACCESS macro maps to poisoning the memory,
  16. * while the UNDEFINED/DEFINED macros unpoison memory.
  17. *
  18. * With no memory checker available, all macros expand to the empty statement.
  19. */
  20. #ifndef mozilla_MemoryChecking_h
  21. #define mozilla_MemoryChecking_h
  22. #if defined(MOZ_VALGRIND)
  23. #include "valgrind/memcheck.h"
  24. #endif
  25. #if defined(MOZ_ASAN) || defined(MOZ_VALGRIND)
  26. #define MOZ_HAVE_MEM_CHECKS 1
  27. #endif
  28. #if defined(MOZ_ASAN)
  29. #include <stddef.h>
  30. #include "mozilla/Attributes.h"
  31. #include "mozilla/Types.h"
  32. #ifdef _MSC_VER
  33. // In clang-cl based ASAN, we link against the memory poisoning functions
  34. // statically.
  35. #define MOZ_ASAN_VISIBILITY
  36. #else
  37. #define MOZ_ASAN_VISIBILITY MOZ_EXPORT
  38. #endif
  39. extern "C" {
  40. /* These definitions are usually provided through the
  41. * sanitizer/asan_interface.h header installed by ASan.
  42. */
  43. void MOZ_ASAN_VISIBILITY
  44. __asan_poison_memory_region(void const volatile *addr, size_t size);
  45. void MOZ_ASAN_VISIBILITY
  46. __asan_unpoison_memory_region(void const volatile *addr, size_t size);
  47. #define MOZ_MAKE_MEM_NOACCESS(addr, size) \
  48. __asan_poison_memory_region((addr), (size))
  49. #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \
  50. __asan_unpoison_memory_region((addr), (size))
  51. #define MOZ_MAKE_MEM_DEFINED(addr, size) \
  52. __asan_unpoison_memory_region((addr), (size))
  53. /*
  54. * These definitions are usually provided through the
  55. * sanitizer/lsan_interface.h header installed by LSan.
  56. */
  57. void MOZ_EXPORT
  58. __lsan_ignore_object(const void *p);
  59. }
  60. #elif defined(MOZ_MSAN)
  61. #include <stddef.h>
  62. #include "mozilla/Types.h"
  63. extern "C" {
  64. /* These definitions are usually provided through the
  65. * sanitizer/msan_interface.h header installed by MSan.
  66. */
  67. void MOZ_EXPORT
  68. __msan_poison(void const volatile *addr, size_t size);
  69. void MOZ_EXPORT
  70. __msan_unpoison(void const volatile *addr, size_t size);
  71. #define MOZ_MAKE_MEM_NOACCESS(addr, size) \
  72. __msan_poison((addr), (size))
  73. #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \
  74. __msan_poison((addr), (size))
  75. #define MOZ_MAKE_MEM_DEFINED(addr, size) \
  76. __msan_unpoison((addr), (size))
  77. }
  78. #elif defined(MOZ_VALGRIND)
  79. #define MOZ_MAKE_MEM_NOACCESS(addr, size) \
  80. VALGRIND_MAKE_MEM_NOACCESS((addr), (size))
  81. #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \
  82. VALGRIND_MAKE_MEM_UNDEFINED((addr), (size))
  83. #define MOZ_MAKE_MEM_DEFINED(addr, size) \
  84. VALGRIND_MAKE_MEM_DEFINED((addr), (size))
  85. #else
  86. #define MOZ_MAKE_MEM_NOACCESS(addr, size) do {} while (0)
  87. #define MOZ_MAKE_MEM_UNDEFINED(addr, size) do {} while (0)
  88. #define MOZ_MAKE_MEM_DEFINED(addr, size) do {} while (0)
  89. #endif
  90. /*
  91. * MOZ_LSAN_INTENTIONAL_LEAK(X) is a macro to tell LeakSanitizer that X
  92. * points to a value that will intentionally never be deallocated during
  93. * the execution of the process.
  94. *
  95. * Additional uses of this macro should be reviewed by people
  96. * conversant in leak-checking and/or MFBT peers.
  97. */
  98. #if defined(MOZ_ASAN)
  99. # define MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(X) __lsan_ignore_object(X)
  100. #else
  101. # define MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(X) /* nothing */
  102. #endif // defined(MOZ_ASAN)
  103. #endif /* mozilla_MemoryChecking_h */