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