|
| 1 | +/* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 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 St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 15 | + |
| 16 | +#ifndef MEMORY_DEBUGGING_INCLUDED |
| 17 | +#define MEMORY_DEBUGGING_INCLUDED |
| 18 | + |
| 19 | +/** |
| 20 | + @file memory_debugging.h |
| 21 | +
|
| 22 | + Various macros useful for communicating with memory debuggers, |
| 23 | + such as Valgrind. |
| 24 | +*/ |
| 25 | + |
| 26 | +#ifdef HAVE_VALGRIND |
| 27 | +# include <valgrind/valgrind.h> |
| 28 | + |
| 29 | +# define MEM_MALLOCLIKE_BLOCK(p1, p2, p3, p4) VALGRIND_MALLOCLIKE_BLOCK(p1, p2, p3, p4) |
| 30 | +# define MEM_FREELIKE_BLOCK(p1, p2) VALGRIND_FREELIKE_BLOCK(p1, p2) |
| 31 | +# include <valgrind/memcheck.h> |
| 32 | + |
| 33 | +# define MEM_UNDEFINED(a,len) VALGRIND_MAKE_MEM_UNDEFINED(a,len) |
| 34 | +# define MEM_DEFINED_IF_ADDRESSABLE(a,len) \ |
| 35 | + VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(a,len) |
| 36 | +# define MEM_NOACCESS(a,len) VALGRIND_MAKE_MEM_NOACCESS(a,len) |
| 37 | +# define MEM_CHECK_ADDRESSABLE(a,len) VALGRIND_CHECK_MEM_IS_ADDRESSABLE(a,len) |
| 38 | + |
| 39 | +#else /* HAVE_VALGRIND */ |
| 40 | + |
| 41 | +# define MEM_MALLOCLIKE_BLOCK(p1, p2, p3, p4) do {} while (0) |
| 42 | +# define MEM_FREELIKE_BLOCK(p1, p2) do {} while (0) |
| 43 | +# define MEM_UNDEFINED(a,len) ((void) 0) |
| 44 | +# define MEM_DEFINED_IF_ADDRESSABLE(a,len) ((void) 0) |
| 45 | +# define MEM_NOACCESS(a,len) ((void) 0) |
| 46 | +# define MEM_CHECK_ADDRESSABLE(a,len) ((void) 0) |
| 47 | + |
| 48 | +#endif |
| 49 | + |
| 50 | + |
| 51 | +#if !defined(DBUG_OFF) || defined(HAVE_VALGRIND) |
| 52 | + |
| 53 | +/** |
| 54 | + Put bad content in memory to be sure it will segfault if dereferenced. |
| 55 | + With Valgrind, verify that memory is addressable, and mark it undefined. |
| 56 | + We cache value of B because if B is expression which depends on A, memset() |
| 57 | + trashes value of B. |
| 58 | +*/ |
| 59 | +#define TRASH(A,B) do { \ |
| 60 | + const size_t l= (B); \ |
| 61 | + MEM_CHECK_ADDRESSABLE(A, l); \ |
| 62 | + memset(A, 0x8F, l); \ |
| 63 | + MEM_UNDEFINED(A, l); \ |
| 64 | + } while (0) |
| 65 | + |
| 66 | +#else |
| 67 | + |
| 68 | +#define TRASH(A,B) do {} while(0) |
| 69 | + |
| 70 | +#endif |
| 71 | + |
| 72 | +#endif // MEMORY_DEBUGGING_INCLUDED |
0 commit comments