1/* Copyright (C) 2010, 2017, MariaDB Corporation.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
15
16/* clang -> gcc */
17#ifndef __has_feature
18# define __has_feature(x) 0
19#endif
20#if __has_feature(address_sanitizer)
21# define __SANITIZE_ADDRESS__ 1
22#endif
23
24#ifdef HAVE_valgrind
25#define IF_VALGRIND(A,B) A
26#else
27#define IF_VALGRIND(A,B) B
28#endif
29
30#if defined(HAVE_VALGRIND_MEMCHECK_H) && defined(HAVE_valgrind)
31# include <valgrind/memcheck.h>
32# define MEM_UNDEFINED(a,len) VALGRIND_MAKE_MEM_UNDEFINED(a,len)
33# define MEM_NOACCESS(a,len) VALGRIND_MAKE_MEM_NOACCESS(a,len)
34# define MEM_CHECK_ADDRESSABLE(a,len) VALGRIND_CHECK_MEM_IS_ADDRESSABLE(a,len)
35# define MEM_CHECK_DEFINED(a,len) VALGRIND_CHECK_MEM_IS_DEFINED(a,len)
36#elif defined(__SANITIZE_ADDRESS__)
37# include <sanitizer/asan_interface.h>
38/* How to do manual poisoning:
39https://github.com/google/sanitizers/wiki/AddressSanitizerManualPoisoning */
40# define MEM_UNDEFINED(a,len) ASAN_UNPOISON_MEMORY_REGION(a,len)
41# define MEM_NOACCESS(a,len) ASAN_POISON_MEMORY_REGION(a,len)
42# define MEM_CHECK_ADDRESSABLE(a,len) ((void) 0)
43# define MEM_CHECK_DEFINED(a,len) ((void) 0)
44#else
45# define MEM_UNDEFINED(a,len) ((void) 0)
46# define MEM_NOACCESS(a,len) ((void) 0)
47# define MEM_CHECK_ADDRESSABLE(a,len) ((void) 0)
48# define MEM_CHECK_DEFINED(a,len) ((void) 0)
49#endif /* HAVE_VALGRIND_MEMCHECK_H */
50
51#if !defined(DBUG_OFF) || defined(TRASH_FREED_MEMORY)
52#define TRASH_FILL(A,B,C) do { const size_t trash_tmp= (B); MEM_UNDEFINED(A, trash_tmp); memset(A, C, trash_tmp); } while (0)
53#else
54#define TRASH_FILL(A,B,C) do { const size_t trash_tmp __attribute__((unused))= (B); MEM_UNDEFINED(A,trash_tmp); } while (0)
55#endif
56
57#define TRASH_ALLOC(A,B) do { TRASH_FILL(A,B,0xA5); MEM_UNDEFINED(A,B); } while(0)
58#define TRASH_FREE(A,B) do { TRASH_FILL(A,B,0x8F); MEM_NOACCESS(A,B); } while(0)
59