|
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 extern "C" { |
|
38 /* These definitions are usually provided through the |
|
39 * sanitizer/asan_interface.h header installed by ASan. |
|
40 */ |
|
41 void __asan_poison_memory_region(void const volatile *addr, size_t size) |
|
42 __attribute__((visibility("default"))); |
|
43 void __asan_unpoison_memory_region(void const volatile *addr, size_t size) |
|
44 __attribute__((visibility("default"))); |
|
45 |
|
46 #define MOZ_MAKE_MEM_NOACCESS(addr, size) \ |
|
47 __asan_poison_memory_region((addr), (size)) |
|
48 |
|
49 #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \ |
|
50 __asan_unpoison_memory_region((addr), (size)) |
|
51 |
|
52 #define MOZ_MAKE_MEM_DEFINED(addr, size) \ |
|
53 __asan_unpoison_memory_region((addr), (size)) |
|
54 } |
|
55 #elif defined(MOZ_VALGRIND) |
|
56 #define MOZ_MAKE_MEM_NOACCESS(addr, size) \ |
|
57 VALGRIND_MAKE_MEM_NOACCESS((addr), (size)) |
|
58 |
|
59 #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \ |
|
60 VALGRIND_MAKE_MEM_UNDEFINED((addr), (size)) |
|
61 |
|
62 #define MOZ_MAKE_MEM_DEFINED(addr, size) \ |
|
63 VALGRIND_MAKE_MEM_DEFINED((addr), (size)) |
|
64 #else |
|
65 |
|
66 #define MOZ_MAKE_MEM_NOACCESS(addr, size) do {} while(0) |
|
67 #define MOZ_MAKE_MEM_UNDEFINED(addr, size) do {} while(0) |
|
68 #define MOZ_MAKE_MEM_DEFINED(addr, size) do {} while(0) |
|
69 |
|
70 #endif |
|
71 |
|
72 #endif /* mozilla_MemoryChecking_h */ |