1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/MemoryChecking.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,72 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* 1.11 + * Provides a common interface to the ASan (AddressSanitizer) and Valgrind 1.12 + * functions used to mark memory in certain ways. In detail, the following 1.13 + * three macros are provided: 1.14 + * 1.15 + * MOZ_MAKE_MEM_NOACCESS - Mark memory as unsafe to access (e.g. freed) 1.16 + * MOZ_MAKE_MEM_UNDEFINED - Mark memory as accessible, with content undefined 1.17 + * MOZ_MAKE_MEM_DEFINED - Mark memory as accessible, with content defined 1.18 + * 1.19 + * With Valgrind in use, these directly map to the three respective Valgrind 1.20 + * macros. With ASan in use, the NOACCESS macro maps to poisoning the memory, 1.21 + * while the UNDEFINED/DEFINED macros unpoison memory. 1.22 + * 1.23 + * With no memory checker available, all macros expand to the empty statement. 1.24 + */ 1.25 + 1.26 +#ifndef mozilla_MemoryChecking_h 1.27 +#define mozilla_MemoryChecking_h 1.28 + 1.29 +#if defined(MOZ_VALGRIND) 1.30 +#include "valgrind/memcheck.h" 1.31 +#endif 1.32 + 1.33 +#if defined(MOZ_ASAN) || defined(MOZ_VALGRIND) 1.34 +#define MOZ_HAVE_MEM_CHECKS 1 1.35 +#endif 1.36 + 1.37 +#if defined(MOZ_ASAN) 1.38 +#include <stddef.h> 1.39 + 1.40 +extern "C" { 1.41 + /* These definitions are usually provided through the 1.42 + * sanitizer/asan_interface.h header installed by ASan. 1.43 + */ 1.44 + void __asan_poison_memory_region(void const volatile *addr, size_t size) 1.45 + __attribute__((visibility("default"))); 1.46 + void __asan_unpoison_memory_region(void const volatile *addr, size_t size) 1.47 + __attribute__((visibility("default"))); 1.48 + 1.49 +#define MOZ_MAKE_MEM_NOACCESS(addr, size) \ 1.50 + __asan_poison_memory_region((addr), (size)) 1.51 + 1.52 +#define MOZ_MAKE_MEM_UNDEFINED(addr, size) \ 1.53 + __asan_unpoison_memory_region((addr), (size)) 1.54 + 1.55 +#define MOZ_MAKE_MEM_DEFINED(addr, size) \ 1.56 + __asan_unpoison_memory_region((addr), (size)) 1.57 +} 1.58 +#elif defined(MOZ_VALGRIND) 1.59 +#define MOZ_MAKE_MEM_NOACCESS(addr, size) \ 1.60 + VALGRIND_MAKE_MEM_NOACCESS((addr), (size)) 1.61 + 1.62 +#define MOZ_MAKE_MEM_UNDEFINED(addr, size) \ 1.63 + VALGRIND_MAKE_MEM_UNDEFINED((addr), (size)) 1.64 + 1.65 +#define MOZ_MAKE_MEM_DEFINED(addr, size) \ 1.66 + VALGRIND_MAKE_MEM_DEFINED((addr), (size)) 1.67 +#else 1.68 + 1.69 +#define MOZ_MAKE_MEM_NOACCESS(addr, size) do {} while(0) 1.70 +#define MOZ_MAKE_MEM_UNDEFINED(addr, size) do {} while(0) 1.71 +#define MOZ_MAKE_MEM_DEFINED(addr, size) do {} while(0) 1.72 + 1.73 +#endif 1.74 + 1.75 +#endif /* mozilla_MemoryChecking_h */