MySQL  8.0.18
Source Code Documentation
memory_debugging.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, 2018, 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, version 2.0,
5  as published by the Free Software Foundation.
6 
7  This program is also distributed with certain software (including
8  but not limited to OpenSSL) that is licensed under separate terms,
9  as designated in a particular file or component or in included license
10  documentation. The authors of MySQL hereby grant you an additional
11  permission to link the program and your derivative works with the
12  separately licensed software that they have included with MySQL.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License, version 2.0, for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 #ifndef MEMORY_DEBUGGING_INCLUDED
24 #define MEMORY_DEBUGGING_INCLUDED
25 
26 /**
27  @file memory_debugging.h
28 
29  Various macros useful for communicating with memory debuggers,
30  such as Valgrind.
31 */
32 
33 #ifdef HAVE_VALGRIND
34 #include <valgrind/valgrind.h>
35 
36 #define MEM_MALLOCLIKE_BLOCK(p1, p2, p3, p4) \
37  VALGRIND_MALLOCLIKE_BLOCK(p1, p2, p3, p4)
38 #define MEM_FREELIKE_BLOCK(p1, p2) VALGRIND_FREELIKE_BLOCK(p1, p2)
39 #include <valgrind/memcheck.h>
40 
41 #define MEM_UNDEFINED(a, len) VALGRIND_MAKE_MEM_UNDEFINED(a, len)
42 #define MEM_DEFINED_IF_ADDRESSABLE(a, len) \
43  VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(a, len)
44 #define MEM_NOACCESS(a, len) VALGRIND_MAKE_MEM_NOACCESS(a, len)
45 #define MEM_CHECK_ADDRESSABLE(a, len) VALGRIND_CHECK_MEM_IS_ADDRESSABLE(a, len)
46 
47 #else /* HAVE_VALGRIND */
48 
49 #define MEM_MALLOCLIKE_BLOCK(p1, p2, p3, p4) \
50  do { \
51  } while (0)
52 #define MEM_FREELIKE_BLOCK(p1, p2) \
53  do { \
54  } while (0)
55 #define MEM_UNDEFINED(a, len) ((void)0)
56 #define MEM_DEFINED_IF_ADDRESSABLE(a, len) ((void)0)
57 #define MEM_NOACCESS(a, len) ((void)0)
58 #define MEM_CHECK_ADDRESSABLE(a, len) ((void)0)
59 
60 #endif
61 
62 #if !defined(DBUG_OFF) || defined(HAVE_VALGRIND)
63 
64 /**
65  Put bad content in memory to be sure it will segfault if dereferenced.
66  With Valgrind, verify that memory is addressable, and mark it undefined.
67  We cache value of B because if B is expression which depends on A, memset()
68  trashes value of B.
69 */
70 #define TRASH(A, B) \
71  do { \
72  void *p = (A); \
73  const size_t l = (B); \
74  MEM_CHECK_ADDRESSABLE(A, l); \
75  memset(p, 0x8F, l); \
76  MEM_UNDEFINED(A, l); \
77  } while (0)
78 
79 #else
80 
81 #define TRASH(A, B) \
82  do { \
83  } while (0)
84 
85 #endif
86 
87 #endif // MEMORY_DEBUGGING_INCLUDED