michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Provides a common interface to the ASan (AddressSanitizer) and Valgrind michael@0: * functions used to mark memory in certain ways. In detail, the following michael@0: * three macros are provided: michael@0: * michael@0: * MOZ_MAKE_MEM_NOACCESS - Mark memory as unsafe to access (e.g. freed) michael@0: * MOZ_MAKE_MEM_UNDEFINED - Mark memory as accessible, with content undefined michael@0: * MOZ_MAKE_MEM_DEFINED - Mark memory as accessible, with content defined michael@0: * michael@0: * With Valgrind in use, these directly map to the three respective Valgrind michael@0: * macros. With ASan in use, the NOACCESS macro maps to poisoning the memory, michael@0: * while the UNDEFINED/DEFINED macros unpoison memory. michael@0: * michael@0: * With no memory checker available, all macros expand to the empty statement. michael@0: */ michael@0: michael@0: #ifndef mozilla_MemoryChecking_h michael@0: #define mozilla_MemoryChecking_h michael@0: michael@0: #if defined(MOZ_VALGRIND) michael@0: #include "valgrind/memcheck.h" michael@0: #endif michael@0: michael@0: #if defined(MOZ_ASAN) || defined(MOZ_VALGRIND) michael@0: #define MOZ_HAVE_MEM_CHECKS 1 michael@0: #endif michael@0: michael@0: #if defined(MOZ_ASAN) michael@0: #include michael@0: michael@0: extern "C" { michael@0: /* These definitions are usually provided through the michael@0: * sanitizer/asan_interface.h header installed by ASan. michael@0: */ michael@0: void __asan_poison_memory_region(void const volatile *addr, size_t size) michael@0: __attribute__((visibility("default"))); michael@0: void __asan_unpoison_memory_region(void const volatile *addr, size_t size) michael@0: __attribute__((visibility("default"))); michael@0: michael@0: #define MOZ_MAKE_MEM_NOACCESS(addr, size) \ michael@0: __asan_poison_memory_region((addr), (size)) michael@0: michael@0: #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \ michael@0: __asan_unpoison_memory_region((addr), (size)) michael@0: michael@0: #define MOZ_MAKE_MEM_DEFINED(addr, size) \ michael@0: __asan_unpoison_memory_region((addr), (size)) michael@0: } michael@0: #elif defined(MOZ_VALGRIND) michael@0: #define MOZ_MAKE_MEM_NOACCESS(addr, size) \ michael@0: VALGRIND_MAKE_MEM_NOACCESS((addr), (size)) michael@0: michael@0: #define MOZ_MAKE_MEM_UNDEFINED(addr, size) \ michael@0: VALGRIND_MAKE_MEM_UNDEFINED((addr), (size)) michael@0: michael@0: #define MOZ_MAKE_MEM_DEFINED(addr, size) \ michael@0: VALGRIND_MAKE_MEM_DEFINED((addr), (size)) michael@0: #else michael@0: michael@0: #define MOZ_MAKE_MEM_NOACCESS(addr, size) do {} while(0) michael@0: #define MOZ_MAKE_MEM_UNDEFINED(addr, size) do {} while(0) michael@0: #define MOZ_MAKE_MEM_DEFINED(addr, size) do {} while(0) michael@0: michael@0: #endif michael@0: michael@0: #endif /* mozilla_MemoryChecking_h */