Bug 886842 - Add clang trunk builds for ASan. r=froydnj
[gecko.git] / mfbt / MemoryChecking.h
blob3287e57ba165e02a2830e00dca6f54542b7b1e4a
1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 /*
7 * Provides a common interface to the ASan (AddressSanitizer) and Valgrind
8 * functions used to mark memory in certain ways. In detail, the following
9 * three macros are provided:
11 * MOZ_MAKE_MEM_NOACCESS - Mark memory as unsafe to access (e.g. freed)
12 * MOZ_MAKE_MEM_UNDEFINED - Mark memory as accessible, with content undefined
13 * MOZ_MAKE_MEM_DEFINED - Mark memory as accessible, with content defined
15 * With Valgrind in use, these directly map to the three respective Valgrind
16 * macros. With ASan in use, the NOACCESS macro maps to poisoning the memory,
17 * while the UNDEFINED/DEFINED macros unpoison memory.
19 * With no memory checker available, all macros expand to the empty statement.
22 #ifndef mozilla_MemoryChecking_h_
23 #define mozilla_MemoryChecking_h_
25 #if defined(MOZ_VALGRIND)
26 #include "valgrind/memcheck.h"
27 #endif
29 #if defined(MOZ_ASAN) || defined(MOZ_VALGRIND)
30 #define MOZ_HAVE_MEM_CHECKS 1
31 #endif
33 #if defined(MOZ_ASAN)
34 #include <stddef.h>
36 extern "C" {
37 /* These definitions are usually provided through the
38 * sanitizer/asan_interface.h header installed by ASan.
40 void __asan_poison_memory_region(void const volatile *addr, size_t size)
41 __attribute__((visibility("default")));
42 void __asan_unpoison_memory_region(void const volatile *addr, size_t size)
43 __attribute__((visibility("default")));
45 #define MOZ_MAKE_MEM_NOACCESS(addr, size) \
46 __asan_poison_memory_region((addr), (size))
48 #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \
49 __asan_unpoison_memory_region((addr), (size))
51 #define MOZ_MAKE_MEM_DEFINED(addr, size) \
52 __asan_unpoison_memory_region((addr), (size))
54 #elif defined(MOZ_VALGRIND)
55 #define MOZ_MAKE_MEM_NOACCESS(addr, size) \
56 VALGRIND_MAKE_MEM_NOACCESS((addr), (size))
58 #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \
59 VALGRIND_MAKE_MEM_UNDEFINED((addr), (size))
61 #define MOZ_MAKE_MEM_DEFINED(addr, size) \
62 VALGRIND_MAKE_MEM_DEFINED((addr), (size))
63 #else
65 #define MOZ_MAKE_MEM_NOACCESS(addr, size) do {} while(0)
66 #define MOZ_MAKE_MEM_UNDEFINED(addr, size) do {} while(0)
67 #define MOZ_MAKE_MEM_DEFINED(addr, size) do {} while(0)
69 #endif
71 #endif /* mozilla_MemoryChecking_h_ */