Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 09:14:27

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_USERREQ__MEMCHECK_VERIFY_ALIGNMENT
0106    } Vg_MemCheckClientRequest;
0107 
0108 
0109 
0110 /* Client-code macros to manipulate the state of memory. */
0111 
0112 /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
0113 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len)           \
0114     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
0115                             VG_USERREQ__MAKE_MEM_NOACCESS,       \
0116                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0117       
0118 /* Similarly, mark memory at _qzz_addr as addressable but undefined
0119    for _qzz_len bytes. */
0120 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len)          \
0121     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
0122                             VG_USERREQ__MAKE_MEM_UNDEFINED,      \
0123                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0124 
0125 /* Similarly, mark memory at _qzz_addr as addressable and defined
0126    for _qzz_len bytes. */
0127 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len)            \
0128     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
0129                             VG_USERREQ__MAKE_MEM_DEFINED,        \
0130                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0131 
0132 /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
0133    not altered: bytes which are addressable are marked as defined,
0134    but those which are not addressable are left unchanged. */
0135 #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len)     \
0136     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,              \
0137                             VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
0138                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0139 
0140 /* Create a block-description handle.  The description is an ascii
0141    string which is included in any messages pertaining to addresses
0142    within the specified memory range.  Has no other effect on the
0143    properties of the memory range. */
0144 #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc)       \
0145     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,        \
0146                             VG_USERREQ__CREATE_BLOCK,              \
0147                             (_qzz_addr), (_qzz_len), (_qzz_desc),  \
0148                             0, 0)
0149 
0150 /* Discard a block-description-handle. Returns 1 for an
0151    invalid handle, 0 for a valid handle. */
0152 #define VALGRIND_DISCARD(_qzz_blkindex)                          \
0153     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
0154                             VG_USERREQ__DISCARD,                 \
0155                             0, (_qzz_blkindex), 0, 0, 0)
0156 
0157 
0158 /* Client-code macros to check the state of memory. */
0159 
0160 /* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
0161    If suitable addressibility is not established, Valgrind prints an
0162    error message and returns the address of the first offending byte.
0163    Otherwise it returns zero. */
0164 #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len)      \
0165     VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                             \
0166                             VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,  \
0167                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0168 
0169 /* Check that memory at _qzz_addr is addressable and defined for
0170    _qzz_len bytes.  If suitable addressibility and definedness are not
0171    established, Valgrind prints an error message and returns the
0172    address of the first offending byte.  Otherwise it returns zero. */
0173 #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len)        \
0174     VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                           \
0175                             VG_USERREQ__CHECK_MEM_IS_DEFINED,    \
0176                             (_qzz_addr), (_qzz_len), 0, 0, 0)
0177 
0178 /* Use this macro to force the definedness and addressibility of an
0179    lvalue to be checked.  If suitable addressibility and definedness
0180    are not established, Valgrind prints an error message and returns
0181    the address of the first offending byte.  Otherwise it returns
0182    zero. */
0183 #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue)                \
0184    VALGRIND_CHECK_MEM_IS_DEFINED(                                \
0185       (volatile unsigned char *)&(__lvalue),                     \
0186                       (unsigned long)(sizeof (__lvalue)))
0187 
0188 
0189 /* Do a full memory leak check (like --leak-check=full) mid-execution. */
0190 #define VALGRIND_DO_LEAK_CHECK                                   \
0191     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,   \
0192                                     0, 0, 0, 0, 0)
0193 
0194 /* Same as VALGRIND_DO_LEAK_CHECK but only showing the entries for
0195    which there was an increase in leaked bytes or leaked nr of blocks
0196    since the previous leak search. */
0197 #define VALGRIND_DO_ADDED_LEAK_CHECK                            \
0198     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,  \
0199                                     0, 1, 0, 0, 0)
0200 
0201 /* Same as VALGRIND_DO_ADDED_LEAK_CHECK but showing entries with
0202    increased or decreased leaked bytes/blocks since previous leak
0203    search. */
0204 #define VALGRIND_DO_CHANGED_LEAK_CHECK                          \
0205     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,  \
0206                                     0, 2, 0, 0, 0)
0207 
0208 /* Same as VALGRIND_DO_LEAK_CHECK but only showing new entries
0209    i.e. loss records that were not there in the previous leak
0210    search. */
0211 #define VALGRIND_DO_NEW_LEAK_CHECK                              \
0212     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,  \
0213                                     0, 3, 0, 0, 0)
0214 
0215 /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */
0216 #define VALGRIND_DO_QUICK_LEAK_CHECK                             \
0217     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DO_LEAK_CHECK,   \
0218                                     1, 0, 0, 0, 0)
0219 
0220 /* Return number of leaked, dubious, reachable and suppressed bytes found by
0221    all previous leak checks.  They must be lvalues.  */
0222 #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed)     \
0223    /* For safety on 64-bit platforms we assign the results to private
0224       unsigned long variables, then assign these to the lvalues the user
0225       specified, which works no matter what type 'leaked', 'dubious', etc
0226       are.  We also initialise '_qzz_leaked', etc because
0227       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
0228       defined. */                                                        \
0229    {                                                                     \
0230     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
0231     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
0232     VALGRIND_DO_CLIENT_REQUEST_STMT(                                     \
0233                                VG_USERREQ__COUNT_LEAKS,                  \
0234                                &_qzz_leaked, &_qzz_dubious,              \
0235                                &_qzz_reachable, &_qzz_suppressed, 0);    \
0236     leaked     = _qzz_leaked;                                            \
0237     dubious    = _qzz_dubious;                                           \
0238     reachable  = _qzz_reachable;                                         \
0239     suppressed = _qzz_suppressed;                                        \
0240    }
0241 
0242 /* Return number of leaked, dubious, reachable and suppressed bytes found by
0243    all previous leak checks.  They must be lvalues.  */
0244 #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
0245    /* For safety on 64-bit platforms we assign the results to private
0246       unsigned long variables, then assign these to the lvalues the user
0247       specified, which works no matter what type 'leaked', 'dubious', etc
0248       are.  We also initialise '_qzz_leaked', etc because
0249       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
0250       defined. */                                                        \
0251    {                                                                     \
0252     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
0253     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
0254     VALGRIND_DO_CLIENT_REQUEST_STMT(                                     \
0255                                VG_USERREQ__COUNT_LEAK_BLOCKS,            \
0256                                &_qzz_leaked, &_qzz_dubious,              \
0257                                &_qzz_reachable, &_qzz_suppressed, 0);    \
0258     leaked     = _qzz_leaked;                                            \
0259     dubious    = _qzz_dubious;                                           \
0260     reachable  = _qzz_reachable;                                         \
0261     suppressed = _qzz_suppressed;                                        \
0262    }
0263 
0264 
0265 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
0266    into the provided zzvbits array.  Return values:
0267       0   if not running on valgrind
0268       1   success
0269       2   [previously indicated unaligned arrays;  these are now allowed]
0270       3   if any parts of zzsrc/zzvbits are not addressable.
0271    The metadata is not copied in cases 0, 2 or 3 so it should be
0272    impossible to segfault your system by using this call.
0273 */
0274 #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes)                \
0275     (unsigned)VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                \
0276                                     VG_USERREQ__GET_VBITS,      \
0277                                     (const char*)(zza),         \
0278                                     (char*)(zzvbits),           \
0279                                     (zznbytes), 0, 0)
0280 
0281 /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
0282    from the provided zzvbits array.  Return values:
0283       0   if not running on valgrind
0284       1   success
0285       2   [previously indicated unaligned arrays;  these are now allowed]
0286       3   if any parts of zza/zzvbits are not addressable.
0287    The metadata is not copied in cases 0, 2 or 3 so it should be
0288    impossible to segfault your system by using this call.
0289 */
0290 #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes)                \
0291     (unsigned)VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                \
0292                                     VG_USERREQ__SET_VBITS,      \
0293                                     (const char*)(zza),         \
0294                                     (const char*)(zzvbits),     \
0295                                     (zznbytes), 0, 0 )
0296 
0297 /* Disable and re-enable reporting of addressing errors in the
0298    specified address range. */
0299 #define VALGRIND_DISABLE_ADDR_ERROR_REPORTING_IN_RANGE(_qzz_addr,_qzz_len) \
0300     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,    \
0301        VG_USERREQ__DISABLE_ADDR_ERROR_REPORTING_IN_RANGE,      \
0302        (_qzz_addr), (_qzz_len), 0, 0, 0)
0303 
0304 #define VALGRIND_ENABLE_ADDR_ERROR_REPORTING_IN_RANGE(_qzz_addr,_qzz_len) \
0305     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,    \
0306        VG_USERREQ__ENABLE_ADDR_ERROR_REPORTING_IN_RANGE,       \
0307        (_qzz_addr), (_qzz_len), 0, 0, 0)
0308 
0309 #endif
0310