OpenVPN
memdbg.h
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2025 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
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 for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef MEMDBG_H
24#define MEMDBG_H
25
26/*
27 * Valgrind debugging support.
28 *
29 * Valgrind is a great tool for debugging memory issues,
30 * though it seems to generate a lot of warnings in OpenSSL
31 * about uninitialized data. To silence these warnings,
32 * I've put together a suppressions file
33 * in debug/valgrind-suppress.
34 *
35 * Also, grep for VALGRIND_MAKE_READABLE in the OpenVPN source.
36 * Because valgrind thinks that some of the data passed from
37 * OpenSSL back to OpenVPN is tainted due to being sourced
38 * from uninitialized data, we need to untaint it before use --
39 * otherwise we will get a lot of useless warnings.
40 *
41 * valgrind --tool=memcheck --error-limit=no --suppressions=debug/valgrind-suppress
42 * --gen-suppressions=yes ./openvpn ...
43 */
44
45#ifdef USE_VALGRIND
46
47#include <valgrind/memcheck.h>
48
49#define VALGRIND_MAKE_READABLE(addr, len)
50
51#else /* ifdef USE_VALGRIND */
52
53#define VALGRIND_MAKE_READABLE(addr, len)
54
55#endif
56
57#ifdef DMALLOC /* see ./configure options to enable */
58
59/*
60 * See ./configure options to enable dmalloc
61 * support for memory leak checking.
62 *
63 * The dmalloc package can be downloaded from:
64 *
65 * http://dmalloc.com/
66 *
67 * When dmalloc is installed and enabled,
68 * use this command prior to running openvpn:
69 *
70 * dmalloc -l dlog -i 100 low -p log-unknown
71 *
72 * Also, put this in your .bashrc file:
73 *
74 * function dmalloc { eval `command dmalloc -b $*`; }
75 *
76 * Or take a more low-level approach:
77 *
78 * export DMALLOC_OPTIONS="debug=0x4e48503,inter=100,log=dlog"
79 *
80 * NOTE: When building dmalloc you need to add something
81 * like this to dmalloc's settings.h -- it will allocate a static
82 * buffer to be used as the malloc arena:
83 *
84 * #define INTERNAL_MEMORY_SPACE (1024 * 1024 * 50)
85 */
86
87#include <dmalloc.h>
88
89#define openvpn_dmalloc(file, line, size) \
90 dmalloc_malloc((file), (line), (size), DMALLOC_FUNC_MALLOC, 0, 0)
91
92/*
93 * This #define will put the line number of the log
94 * file position where leaked memory was allocated instead
95 * of the source code file and line number. Make sure
96 * to increase the size of dmalloc's info tables,
97 * (MEMORY_TABLE_SIZE in settings.h)
98 * otherwise it might get overwhelmed by the large
99 * number of unique file/line combinations.
100 */
101#if 0
102#undef malloc
103#define malloc(size) openvpn_dmalloc("logfile", x_msg_line_num, (size))
104#endif
105
106#endif /* DMALLOC */
107
108/*
109 * Force buffers to be zeroed after allocation.
110 * For debugging only.
111 */
112/*#define ZERO_BUFFER_ON_ALLOC*/
113
114#endif /* MEMDBG_H */