Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:30

0001 
0002 /*
0003    ----------------------------------------------------------------
0004 
0005    Notice that the following BSD-style license applies to this one
0006    file (memcheck.h) only.  The rest of Valgrind is licensed under the
0007    terms of the GNU General Public License, version 2, unless
0008    otherwise indicated.  See the COPYING file in the source
0009    distribution for details.
0010 
0011    ----------------------------------------------------------------
0012 
0013    This file is part of MemCheck, a heavyweight Valgrind tool for
0014    detecting memory errors.
0015 
0016    Copyright (C) 2000-2017 Julian Seward.  All rights reserved.
0017 
0018    Redistribution and use in source and binary forms, with or without
0019    modification, are permitted provided that the following conditions
0020    are met:
0021 
0022    1. Redistributions of source code must retain the above copyright
0023       notice, this list of conditions and the following disclaimer.
0024 
0025    2. The origin of this software must not be misrepresented; you must 
0026       not claim that you wrote the original software.  If you use this 
0027       software in a product, an acknowledgment in the product 
0028       documentation would be appreciated but is not required.
0029 
0030    3. Altered source versions must be plainly marked as such, and must
0031       not be misrepresented as being the original software.
0032 
0033    4. The name of the author may not be used to endorse or promote 
0034       products derived from this software without specific prior written 
0035       permission.
0036 
0037    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
0038    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
0039    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0040    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
0041    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0042    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
0043    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0044    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
0045    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0046    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0047    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0048 
0049    ----------------------------------------------------------------
0050 
0051    Notice that the above BSD-style license applies to this one file
0052    (memcheck.h) only.  The entire rest of Valgrind is licensed under
0053    the terms of the GNU General Public License, version 2.  See the
0054    COPYING file in the source distribution for details.
0055 
0056    ---------------------------------------------------------------- 
0057 */
0058 
0059 
0060 #ifndef __MEMCHECK_H
0061 #define __MEMCHECK_H
0062 
0063 
0064 /* This file is for inclusion into client (your!) code.
0065 
0066    You can use these macros to manipulate and query memory permissions
0067    inside your own programs.
0068 
0069    See comment near the top of valgrind.h on how to use them.
0070 */
0071 
0072 #include "valgrind.h"
0073 
0074 /* !! ABIWARNING !! ABIWARNING !! ABIWARNING !! ABIWARNING !! 
0075    This enum comprises an ABI exported by Valgrind to programs
0076    which use client requests.  DO NOT CHANGE THE ORDER OF THESE
0077    ENTRIES, NOR DELETE ANY -- add new ones at the end. */
0078 typedef
0079    enum { 
0080       VG_USERREQ__MAKE_MEM_NOACCESS = VG_USERREQ_TOOL_BASE('M','C'),
0081       VG_USERREQ__MAKE_MEM_UNDEFINED,
0082       VG_USERREQ__MAKE_MEM_DEFINED,
0083       VG_USERREQ__DISCARD,
0084       VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,
0085       VG_USERREQ__CHECK_MEM_IS_DEFINED,
0086       VG_USERREQ__DO_LEAK_CHECK,
0087       VG_USERREQ__COUNT_LEAKS,
0088 
0089       VG_USERREQ__GET_VBITS,
0090       VG_USERREQ__SET_VBITS,
0091 
0092       VG_USERREQ__CREATE_BLOCK,
0093 
0094       VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE,
0095 
0096       /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */
0097       VG_USERREQ__COUNT_LEAK_BLOCKS,
0098 
0099       VG_USERREQ__ENABLE_ADDR_ERROR_REPORTING_IN_RANGE,
0100       VG_USERREQ__DISABLE_ADDR_ERROR_REPORTING_IN_RANGE,
0101 
0102       /* This is just for memcheck's internal use - don't use it */
0103       _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR 
0104          = VG_USERREQ_TOOL_BASE('M','C') + 256
0105    } Vg_MemCheckClientRequest;
0106 
0107 
0108 
0109 /* Client-code macros to manipulate the state of memory. */
0110 
0111 /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
0112 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len)           \
0113     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
0114                             VG_USERREQ__MAKE_MEM_NOACCESS,       \
0115                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0116       
0117 /* Similarly, mark memory at _qzz_addr as addressable but undefined
0118    for _qzz_len bytes. */
0119 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len)          \
0120     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
0121                             VG_USERREQ__MAKE_MEM_UNDEFINED,      \
0122                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0123 
0124 /* Similarly, mark memory at _qzz_addr as addressable and defined
0125    for _qzz_len bytes. */
0126 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len)            \
0127     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
0128                             VG_USERREQ__MAKE_MEM_DEFINED,        \
0129                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0130 
0131 /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
0132    not altered: bytes which are addressable are marked as defined,
0133    but those which are not addressable are left unchanged. */
0134 #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len)     \
0135     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,              \
0136                             VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
0137                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0138 
0139 /* Create a block-description handle.  The description is an ascii
0140    string which is included in any messages pertaining to addresses
0141    within the specified memory range.  Has no other effect on the
0142    properties of the memory range. */
0143 #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc)       \
0144     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,        \
0145                             VG_USERREQ__CREATE_BLOCK,              \
0146                             (_qzz_addr), (_qzz_len), (_qzz_desc),  \
0147                             0, 0)
0148 
0149 /* Discard a block-description-handle. Returns 1 for an
0150    invalid handle, 0 for a valid handle. */
0151 #define VALGRIND_DISCARD(_qzz_blkindex)                          \
0152     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
0153                             VG_USERREQ__DISCARD,                 \
0154                             0, (_qzz_blkindex), 0, 0, 0)
0155 
0156 
0157 /* Client-code macros to check the state of memory. */
0158 
0159 /* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
0160    If suitable addressibility is not established, Valgrind prints an
0161    error message and returns the address of the first offending byte.
0162    Otherwise it returns zero. */
0163 #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len)      \
0164     VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                             \
0165                             VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,  \
0166                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0167 
0168 /* Check that memory at _qzz_addr is addressable and defined for
0169    _qzz_len bytes.  If suitable addressibility and definedness are not
0170    established, Valgrind prints an error message and returns the
0171    address of the first offending byte.  Otherwise it returns zero. */
0172 #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len)        \
0173     VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                           \
0174                             VG_USERREQ__CHECK_MEM_IS_DEFINED,    \
0175                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0176 
0177 /* Use this macro to force the definedness and addressibility of an
0178    lvalue to be checked.  If suitable addressibility and definedness
0179    are not established, Valgrind prints an error message and returns
0180    the address of the first offending byte.  Otherwise it returns
0181    zero. */
0182 #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue)                \
0183    VALGRIND_CHECK_MEM_IS_DEFINED(                                \
0184       (volatile unsigned char *)&(__lvalue),                     \
0185                       (unsigned long)(sizeof (__lvalue)))
0186 
0187 
0188 /* Do a full memory leak check (like --leak-check=full) mid-execution. */
0189 #define VALGRIND_DO_LEAK_CHECK                                   \
0190     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,   \
0191                                     0, 0, 0, 0, 0)
0192 
0193 /* Same as VALGRIND_DO_LEAK_CHECK but only showing the entries for
0194    which there was an increase in leaked bytes or leaked nr of blocks
0195    since the previous leak search. */
0196 #define VALGRIND_DO_ADDED_LEAK_CHECK                            \
0197     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,  \
0198                                     0, 1, 0, 0, 0)
0199 
0200 /* Same as VALGRIND_DO_ADDED_LEAK_CHECK but showing entries with
0201    increased or decreased leaked bytes/blocks since previous leak
0202    search. */
0203 #define VALGRIND_DO_CHANGED_LEAK_CHECK                          \
0204     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,  \
0205                                     0, 2, 0, 0, 0)
0206 
0207 /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */
0208 #define VALGRIND_DO_QUICK_LEAK_CHECK                             \
0209     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,   \
0210                                     1, 0, 0, 0, 0)
0211 
0212 /* Return number of leaked, dubious, reachable and suppressed bytes found by
0213    all previous leak checks.  They must be lvalues.  */
0214 #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed)     \
0215    /* For safety on 64-bit platforms we assign the results to private
0216       unsigned long variables, then assign these to the lvalues the user
0217       specified, which works no matter what type 'leaked', 'dubious', etc
0218       are.  We also initialise '_qzz_leaked', etc because
0219       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
0220       defined. */                                                        \
0221    {                                                                     \
0222     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
0223     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
0224     VALGRIND_DO_CLIENT_REQUEST_STMT(                                     \
0225                                VG_USERREQ__COUNT_LEAKS,                  \
0226                                &_qzz_leaked, &_qzz_dubious,              \
0227                                &_qzz_reachable, &_qzz_suppressed, 0);    \
0228     leaked     = _qzz_leaked;                                            \
0229     dubious    = _qzz_dubious;                                           \
0230     reachable  = _qzz_reachable;                                         \
0231     suppressed = _qzz_suppressed;                                        \
0232    }
0233 
0234 /* Return number of leaked, dubious, reachable and suppressed bytes found by
0235    all previous leak checks.  They must be lvalues.  */
0236 #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
0237    /* For safety on 64-bit platforms we assign the results to private
0238       unsigned long variables, then assign these to the lvalues the user
0239       specified, which works no matter what type 'leaked', 'dubious', etc
0240       are.  We also initialise '_qzz_leaked', etc because
0241       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
0242       defined. */                                                        \
0243    {                                                                     \
0244     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
0245     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
0246     VALGRIND_DO_CLIENT_REQUEST_STMT(                                     \
0247                                VG_USERREQ__COUNT_LEAK_BLOCKS,            \
0248                                &_qzz_leaked, &_qzz_dubious,              \
0249                                &_qzz_reachable, &_qzz_suppressed, 0);    \
0250     leaked     = _qzz_leaked;                                            \
0251     dubious    = _qzz_dubious;                                           \
0252     reachable  = _qzz_reachable;                                         \
0253     suppressed = _qzz_suppressed;                                        \
0254    }
0255 
0256 
0257 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
0258    into the provided zzvbits array.  Return values:
0259       0   if not running on valgrind
0260       1   success
0261       2   [previously indicated unaligned arrays;  these are now allowed]
0262       3   if any parts of zzsrc/zzvbits are not addressable.
0263    The metadata is not copied in cases 0, 2 or 3 so it should be
0264    impossible to segfault your system by using this call.
0265 */
0266 #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes)                \
0267     (unsigned)VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                \
0268                                     VG_USERREQ__GET_VBITS,      \
0269                                     (const char*)(zza),         \
0270                                     (char*)(zzvbits),           \
0271                                     (zznbytes), 0, 0)
0272 
0273 /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
0274    from the provided zzvbits array.  Return values:
0275       0   if not running on valgrind
0276       1   success
0277       2   [previously indicated unaligned arrays;  these are now allowed]
0278       3   if any parts of zza/zzvbits are not addressable.
0279    The metadata is not copied in cases 0, 2 or 3 so it should be
0280    impossible to segfault your system by using this call.
0281 */
0282 #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes)                \
0283     (unsigned)VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                \
0284                                     VG_USERREQ__SET_VBITS,      \
0285                                     (const char*)(zza),         \
0286                                     (const char*)(zzvbits),     \
0287                                     (zznbytes), 0, 0 )
0288 
0289 /* Disable and re-enable reporting of addressing errors in the
0290    specified address range. */
0291 #define VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(_qzz_addr,_qzz_len) \
0292     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,    \
0293        VG_USERREQ__DISABLE_ADDR_ERROR_REPORTING_IN_RANGE,      \
0294        (_qzz_addr), (_qzz_len), 0, 0, 0)
0295 
0296 #define VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(_qzz_addr,_qzz_len) \
0297     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,    \
0298        VG_USERREQ__ENABLE_ADDR_ERROR_REPORTING_IN_RANGE,       \
0299        (_qzz_addr), (_qzz_len), 0, 0, 0)
0300 
0301 #endif
0302