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