blob: 4271cd8e5238439720ae4599c79bdbfc36f22dba [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
sewardj2e93c502002-04-12 11:12:52 +000051 your brain. Valgrind dumps the result value in %EDX, so we first
52 copy the default value there, so that it is returned when not
53 running on Valgrind. Since %EAX points to a block of mem
54 containing the args, you can pass as many args as you want like
55 this. Currently this is set up to deal with 4 args since that's
56 the max that we appear to need (pthread_create).
sewardjde4a1d02002-03-22 01:27:54 +000057*/
sewardj2e93c502002-04-12 11:12:52 +000058#define VALGRIND_MAGIC_SEQUENCE( \
59 _zzq_rlval, /* result lvalue */ \
60 _zzq_default, /* result returned when running on real CPU */ \
61 _zzq_request, /* request code */ \
62 _zzq_arg1, /* request first param */ \
63 _zzq_arg2, /* request second param */ \
64 _zzq_arg3, /* request third param */ \
65 _zzq_arg4 /* request fourth param */ ) \
66 \
67 { volatile unsigned int _zzq_args[5]; \
68 _zzq_args[0] = (volatile unsigned int)_zzq_request; \
69 _zzq_args[1] = (volatile unsigned int)_zzq_arg1; \
70 _zzq_args[2] = (volatile unsigned int)_zzq_arg2; \
71 _zzq_args[3] = (volatile unsigned int)_zzq_arg3; \
72 _zzq_args[4] = (volatile unsigned int)_zzq_arg4; \
73 asm volatile("movl %1, %%eax\n\t" \
74 "movl %2, %%edx\n\t" \
75 "roll $29, %%eax ; roll $3, %%eax\n\t" \
76 "rorl $27, %%eax ; rorl $5, %%eax\n\t" \
77 "roll $13, %%eax ; roll $19, %%eax\n\t" \
78 "movl %%edx, %0\t" \
79 : "=r" (_zzq_rlval) \
80 : "r" (&_zzq_args[0]), "r" (_zzq_default) \
81 : "eax", "edx", "cc", "memory" \
82 ); \
83 }
84
85
86/* Some request codes. There are many more of these, but most are not
87 exposed to end-user view. These are the public ones, all of the
88 form 0x1000 + small_number.
89*/
90
91#define VG_USERREQ__MAKE_NOACCESS 0x1001
92#define VG_USERREQ__MAKE_WRITABLE 0x1002
93#define VG_USERREQ__MAKE_READABLE 0x1003
94#define VG_USERREQ__DISCARD 0x1004
95#define VG_USERREQ__CHECK_WRITABLE 0x1005
96#define VG_USERREQ__CHECK_READABLE 0x1006
97#define VG_USERREQ__MAKE_NOACCESS_STACK 0x1007
98#define VG_USERREQ__RUNNING_ON_VALGRIND 0x1008
99#define VG_USERREQ__DO_LEAK_CHECK 0x1009 /* unimplemented */
sewardjde4a1d02002-03-22 01:27:54 +0000100
101
102
103/* Client-code macros to manipulate the state of memory. */
104
105/* Mark memory at _qzz_addr as unaddressible and undefined for
106 _qzz_len bytes. Returns an int handle pertaining to the block
107 descriptions Valgrind will use in subsequent error messages. */
108#define VALGRIND_MAKE_NOACCESS(_qzz_addr,_qzz_len) \
109 ({unsigned int _qzz_res; \
sewardj2e93c502002-04-12 11:12:52 +0000110 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
111 VG_USERREQ__MAKE_NOACCESS, \
112 _qzz_addr, _qzz_len, 0, 0); \
sewardjde4a1d02002-03-22 01:27:54 +0000113 _qzz_res; \
114 })
115
116/* Similarly, mark memory at _qzz_addr as addressible but undefined
117 for _qzz_len bytes. */
118#define VALGRIND_MAKE_WRITABLE(_qzz_addr,_qzz_len) \
119 ({unsigned int _qzz_res; \
sewardj2e93c502002-04-12 11:12:52 +0000120 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
121 VG_USERREQ__MAKE_WRITABLE, \
122 _qzz_addr,_ qzz_len, 0, 0); \
sewardjde4a1d02002-03-22 01:27:54 +0000123 _qzz_res; \
124 })
125
126/* Similarly, mark memory at _qzz_addr as addressible and defined
127 for _qzz_len bytes. */
128#define VALGRIND_MAKE_READABLE(_qzz_addr,_qzz_len) \
129 ({unsigned int _qzz_res; \
sewardj2e93c502002-04-12 11:12:52 +0000130 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
131 VG_USERREQ__MAKE_READABLE, \
132 _qzz_addr, _qzz_len, 0, 0); \
sewardjde4a1d02002-03-22 01:27:54 +0000133 _qzz_res; \
134 })
135
136/* Discard a block-description-handle obtained from the above three
137 macros. After this, Valgrind will no longer be able to relate
138 addressing errors to the user-defined block associated with the
139 handle. The permissions settings associated with the handle remain
140 in place. Returns 1 for an invalid handle, 0 for a valid
141 handle. */
142#define VALGRIND_DISCARD(_qzz_blkindex) \
143 ({unsigned int _qzz_res; \
sewardj2e93c502002-04-12 11:12:52 +0000144 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* default return */, \
145 VG_USERREQ__DISCARD, \
146 0, _qzz_blkindex, 0, 0); \
sewardjde4a1d02002-03-22 01:27:54 +0000147 _qzz_res; \
148 })
149
150
151
152/* Client-code macros to check the state of memory. */
153
154/* Check that memory at _qzz_addr is addressible for _qzz_len bytes.
155 If suitable addressibility is not established, Valgrind prints an
156 error message and returns the address of the first offending byte.
157 Otherwise it returns zero. */
sewardj2e93c502002-04-12 11:12:52 +0000158#define VALGRIND_CHECK_WRITABLE(_qzz_addr,_qzz_len) \
159 ({unsigned int _qzz_res; \
160 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
161 VG_USERREQ__CHECK_WRITABLE, \
162 _qzz_addr, _qzz_len, 0, 0); \
163 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000164 })
165
166/* Check that memory at _qzz_addr is addressible and defined for
167 _qzz_len bytes. If suitable addressibility and definedness are not
168 established, Valgrind prints an error message and returns the
169 address of the first offending byte. Otherwise it returns zero. */
sewardj2e93c502002-04-12 11:12:52 +0000170#define VALGRIND_CHECK_READABLE(_qzz_addr,_qzz_len) \
171 ({unsigned int _qzz_res; \
172 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
173 VG_USERREQ__CHECK_READABLE, \
174 _qzz_addr, _qzz_len, 0, 0); \
175 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000176 })
177
178
179/* Use this macro to force the definedness and addressibility of a
180 value to be checked. If suitable addressibility and definedness
181 are not established, Valgrind prints an error message and returns
182 the address of the first offending byte. Otherwise it returns
183 zero. */
sewardj2e93c502002-04-12 11:12:52 +0000184#define VALGRIND_CHECK_DEFINED(__lvalue) \
185 (void) \
186 VALGRIND_CHECK_READABLE( \
187 (volatile unsigned char *)&(__lvalue), \
sewardjde4a1d02002-03-22 01:27:54 +0000188 (unsigned int)(sizeof (__lvalue)))
189
190
191
192/* Mark memory, intended to be on the client's stack, at _qzz_addr as
193 unaddressible and undefined for _qzz_len bytes. Does not return a
194 value. The record associated with this setting will be
195 automatically removed by Valgrind when the containing routine
196 exits. */
sewardj2e93c502002-04-12 11:12:52 +0000197#define VALGRIND_MAKE_NOACCESS_STACK(_qzz_addr,_qzz_len) \
198 {unsigned int _qzz_res; \
199 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
200 VG_USERREQ__MAKE_NOACCESS_STACK, \
201 _qzz_addr, _qzz_len, 0, 0); \
202 }
203
204
205/* Returns 1 if running on Valgrind, 0 if running on the real CPU.
206 Currently implemented but untested. */
207#define RUNNING_ON_VALGRIND \
208 ({unsigned int _qzz_res; \
209 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0 /* returned if not */, \
210 VG_USERREQ__RUNNING_ON_VALGRIND, \
211 0, 0, 0, 0); \
212 _qzz_res; \
sewardjde4a1d02002-03-22 01:27:54 +0000213 })
214
215
sewardj2e93c502002-04-12 11:12:52 +0000216/* Mark memory, intended to be on the client's stack, at _qzz_addr as
217 unaddressible and undefined for _qzz_len bytes. Does not return a
218 value. The record associated with this setting will be
219 automatically removed by Valgrind when the containing routine
220 exits.
221
222 Currently implemented but untested.
223*/
224#define VALGRIND_DO_LEAK_CHECK \
225 {unsigned int _qzz_res; \
226 VALGRIND_MAGIC_SEQUENCE(_qzz_res, 0, \
227 VG_USERREQ__DO_LEAK_CHECK, \
228 0, 0, 0, 0); \
229 }
230
sewardjde4a1d02002-03-22 01:27:54 +0000231#endif