blob: 5a32ab5656ddb2d095c0307829c5d3b2a4ac9414 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001
2/*
3 This file is part of Valgrind, an x86 protected-mode emulator
4 designed for debugging and profiling binaries on x86-Unixes.
5
6 Copyright (C) 2000-2002 Julian Seward
7 jseward@acm.org
8 Julian_Seward@muraroa.demon.co.uk
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 02111-1307, USA.
24
25 The GNU General Public License is contained in the file LICENSE.
26*/
27
28
29#ifndef __VALGRIND_H
30#define __VALGRIND_H
31
32
33/* This file is for inclusion into client (your!) code.
34
35 You can use these macros to manipulate and query memory permissions
36 inside your own programs.
37
38 The resulting executables will still run without Valgrind, just a
39 little bit more slowly than they otherwise would, but otherwise
40 unchanged.
41
42 When run on Valgrind with --client-perms=yes, Valgrind observes
43 these macro calls and takes appropriate action. When run on
44 Valgrind with --client-perms=no (the default), Valgrind observes
45 these macro calls but does not take any action as a result. */
46
47
48
49/* This defines the magic code sequence which the JITter spots and
50 handles magically. Don't look too closely at this; it will rot
51 your brain.
52*/
53#define VALGRIND_MAGIC_SEQUENCE(_zzq_res,_zzq_code,_zzq_addr,_zzq_len) \
54 asm volatile("movl %1, %%eax\n\t" \
55 "movl %2, %%ebx\n\t" \
56 "movl %3, %%ecx\n\t" \
57 "roll $29, %%eax ; roll $3, %%eax\n\t" \
58 "roll $27, %%eax ; roll $5, %%eax\n\t" \
59 "movl %%eax, %0\t" \
60 : "=r" (_zzq_res) \
61 : "r" (_zzq_code), "r" (_zzq_addr), "r" (_zzq_len) \
62 : "eax", "ebx", "ecx", "cc", "memory" \
63 );
64
65
66
67/* Client-code macros to manipulate the state of memory. */
68
69/* Mark memory at _qzz_addr as unaddressible and undefined for
70 _qzz_len bytes. Returns an int handle pertaining to the block
71 descriptions Valgrind will use in subsequent error messages. */
72#define VALGRIND_MAKE_NOACCESS(_qzz_addr,_qzz_len) \
73 ({unsigned int _qzz_res; \
74 VALGRIND_MAGIC_SEQUENCE(_qzz_res,1001,_qzz_addr,_qzz_len); \
75 _qzz_res; \
76 })
77
78/* Similarly, mark memory at _qzz_addr as addressible but undefined
79 for _qzz_len bytes. */
80#define VALGRIND_MAKE_WRITABLE(_qzz_addr,_qzz_len) \
81 ({unsigned int _qzz_res; \
82 VALGRIND_MAGIC_SEQUENCE(_qzz_res,1002,_qzz_addr,_qzz_len); \
83 _qzz_res; \
84 })
85
86/* Similarly, mark memory at _qzz_addr as addressible and defined
87 for _qzz_len bytes. */
88#define VALGRIND_MAKE_READABLE(_qzz_addr,_qzz_len) \
89 ({unsigned int _qzz_res; \
90 VALGRIND_MAGIC_SEQUENCE(_qzz_res,1003,_qzz_addr,_qzz_len); \
91 _qzz_res; \
92 })
93
94/* Discard a block-description-handle obtained from the above three
95 macros. After this, Valgrind will no longer be able to relate
96 addressing errors to the user-defined block associated with the
97 handle. The permissions settings associated with the handle remain
98 in place. Returns 1 for an invalid handle, 0 for a valid
99 handle. */
100#define VALGRIND_DISCARD(_qzz_blkindex) \
101 ({unsigned int _qzz_res; \
102 VALGRIND_MAGIC_SEQUENCE(_qzz_res,2004,0,_qzz_blkindex); \
103 _qzz_res; \
104 })
105
106
107
108/* Client-code macros to check the state of memory. */
109
110/* Check that memory at _qzz_addr is addressible for _qzz_len bytes.
111 If suitable addressibility is not established, Valgrind prints an
112 error message and returns the address of the first offending byte.
113 Otherwise it returns zero. */
114#define VALGRIND_CHECK_WRITABLE(_qzz_addr,_qzz_len) \
115 ({unsigned int _qzz_res; \
116 VALGRIND_MAGIC_SEQUENCE(_qzz_res,2002,_qzz_addr,_qzz_len); \
117 _qzz_res; \
118 })
119
120/* Check that memory at _qzz_addr is addressible and defined for
121 _qzz_len bytes. If suitable addressibility and definedness are not
122 established, Valgrind prints an error message and returns the
123 address of the first offending byte. Otherwise it returns zero. */
124#define VALGRIND_CHECK_READABLE(_qzz_addr,_qzz_len) \
125 ({unsigned int _qzz_res; \
126 VALGRIND_MAGIC_SEQUENCE(_qzz_res,2003,_qzz_addr,_qzz_len); \
127 _qzz_res; \
128 })
129
130
131/* Use this macro to force the definedness and addressibility of a
132 value to be checked. If suitable addressibility and definedness
133 are not established, Valgrind prints an error message and returns
134 the address of the first offending byte. Otherwise it returns
135 zero. */
136#define VALGRIND_CHECK_DEFINED(__lvalue) \
137 (void) \
138 VALGRIND_CHECK_READABLE( \
139 (volatile unsigned char *)&(__lvalue), \
140 (unsigned int)(sizeof (__lvalue)))
141
142
143
144/* Mark memory, intended to be on the client's stack, at _qzz_addr as
145 unaddressible and undefined for _qzz_len bytes. Does not return a
146 value. The record associated with this setting will be
147 automatically removed by Valgrind when the containing routine
148 exits. */
149#define VALGRIND_MAKE_NOACCESS_STACK(_qzz_addr,_qzz_len) \
150 ({unsigned int _qzz_res; \
151 VALGRIND_MAGIC_SEQUENCE(_qzz_res,3001,_qzz_addr,_qzz_len); \
152 _qzz_res; \
153 })
154
155
156#endif