Home | History | Annotate | Download | only in valgrind
      1 #ifdef ANDROID
      2   #include "memcheck/memcheck.h"
      3 #else
      4 /*
      5    ----------------------------------------------------------------
      6 
      7    Notice that the following BSD-style license applies to this one
      8    file (memcheck.h) only.  The rest of Valgrind is licensed under the
      9    terms of the GNU General Public License, version 2, unless
     10    otherwise indicated.  See the COPYING file in the source
     11    distribution for details.
     12 
     13    ----------------------------------------------------------------
     14 
     15    This file is part of MemCheck, a heavyweight Valgrind tool for
     16    detecting memory errors.
     17 
     18    Copyright (C) 2000-2010 Julian Seward.  All rights reserved.
     19 
     20    Redistribution and use in source and binary forms, with or without
     21    modification, are permitted provided that the following conditions
     22    are met:
     23 
     24    1. Redistributions of source code must retain the above copyright
     25       notice, this list of conditions and the following disclaimer.
     26 
     27    2. The origin of this software must not be misrepresented; you must
     28       not claim that you wrote the original software.  If you use this
     29       software in a product, an acknowledgment in the product
     30       documentation would be appreciated but is not required.
     31 
     32    3. Altered source versions must be plainly marked as such, and must
     33       not be misrepresented as being the original software.
     34 
     35    4. The name of the author may not be used to endorse or promote
     36       products derived from this software without specific prior written
     37       permission.
     38 
     39    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     40    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     41    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     42    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     43    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     44    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     45    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     46    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     47    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     48    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     49    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     50 
     51    ----------------------------------------------------------------
     52 
     53    Notice that the above BSD-style license applies to this one file
     54    (memcheck.h) only.  The entire rest of Valgrind is licensed under
     55    the terms of the GNU General Public License, version 2.  See the
     56    COPYING file in the source distribution for details.
     57 
     58    ----------------------------------------------------------------
     59 */
     60 
     61 
     62 #ifndef __MEMCHECK_H
     63 #define __MEMCHECK_H
     64 
     65 
     66 /* This file is for inclusion into client (your!) code.
     67 
     68    You can use these macros to manipulate and query memory permissions
     69    inside your own programs.
     70 
     71    See comment near the top of valgrind.h on how to use them.
     72 */
     73 
     74 #include "valgrind.h"
     75 
     76 /* !! ABIWARNING !! ABIWARNING !! ABIWARNING !! ABIWARNING !!
     77    This enum comprises an ABI exported by Valgrind to programs
     78    which use client requests.  DO NOT CHANGE THE ORDER OF THESE
     79    ENTRIES, NOR DELETE ANY -- add new ones at the end. */
     80 typedef
     81    enum {
     82       VG_USERREQ__MAKE_MEM_NOACCESS = VG_USERREQ_TOOL_BASE('M','C'),
     83       VG_USERREQ__MAKE_MEM_UNDEFINED,
     84       VG_USERREQ__MAKE_MEM_DEFINED,
     85       VG_USERREQ__DISCARD,
     86       VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,
     87       VG_USERREQ__CHECK_MEM_IS_DEFINED,
     88       VG_USERREQ__DO_LEAK_CHECK,
     89       VG_USERREQ__COUNT_LEAKS,
     90 
     91       VG_USERREQ__GET_VBITS,
     92       VG_USERREQ__SET_VBITS,
     93 
     94       VG_USERREQ__CREATE_BLOCK,
     95 
     96       VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE,
     97 
     98       /* Not next to VG_USERREQ__COUNT_LEAKS because it was added later. */
     99       VG_USERREQ__COUNT_LEAK_BLOCKS,
    100 
    101       /* This is just for memcheck's internal use - don't use it */
    102       _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR
    103          = VG_USERREQ_TOOL_BASE('M','C') + 256
    104    } Vg_MemCheckClientRequest;
    105 
    106 
    107 
    108 /* Client-code macros to manipulate the state of memory. */
    109 
    110 /* Mark memory at _qzz_addr as unaddressable for _qzz_len bytes. */
    111 #define VALGRIND_MAKE_MEM_NOACCESS(_qzz_addr,_qzz_len)           \
    112     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
    113                             VG_USERREQ__MAKE_MEM_NOACCESS,       \
    114                             (_qzz_addr), (_qzz_len), 0, 0, 0)
    115 
    116 /* Similarly, mark memory at _qzz_addr as addressable but undefined
    117    for _qzz_len bytes. */
    118 #define VALGRIND_MAKE_MEM_UNDEFINED(_qzz_addr,_qzz_len)          \
    119     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
    120                             VG_USERREQ__MAKE_MEM_UNDEFINED,      \
    121                             (_qzz_addr), (_qzz_len), 0, 0, 0)
    122 
    123 /* Similarly, mark memory at _qzz_addr as addressable and defined
    124    for _qzz_len bytes. */
    125 #define VALGRIND_MAKE_MEM_DEFINED(_qzz_addr,_qzz_len)            \
    126     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
    127                             VG_USERREQ__MAKE_MEM_DEFINED,        \
    128                             (_qzz_addr), (_qzz_len), 0, 0, 0)
    129 
    130 /* Similar to VALGRIND_MAKE_MEM_DEFINED except that addressability is
    131    not altered: bytes which are addressable are marked as defined,
    132    but those which are not addressable are left unchanged. */
    133 #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(_qzz_addr,_qzz_len)     \
    134     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,              \
    135                             VG_USERREQ__MAKE_MEM_DEFINED_IF_ADDRESSABLE, \
    136                             (_qzz_addr), (_qzz_len), 0, 0, 0)
    137 
    138 /* Create a block-description handle.  The description is an ascii
    139    string which is included in any messages pertaining to addresses
    140    within the specified memory range.  Has no other effect on the
    141    properties of the memory range. */
    142 #define VALGRIND_CREATE_BLOCK(_qzz_addr,_qzz_len, _qzz_desc)	   \
    143     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,        \
    144                             VG_USERREQ__CREATE_BLOCK,              \
    145                             (_qzz_addr), (_qzz_len), (_qzz_desc),  \
    146                             0, 0)
    147 
    148 /* Discard a block-description-handle. Returns 1 for an
    149    invalid handle, 0 for a valid handle. */
    150 #define VALGRIND_DISCARD(_qzz_blkindex)                          \
    151     VALGRIND_DO_CLIENT_REQUEST_EXPR(0 /* default return */,      \
    152                             VG_USERREQ__DISCARD,                 \
    153                             0, (_qzz_blkindex), 0, 0, 0)
    154 
    155 
    156 /* Client-code macros to check the state of memory. */
    157 
    158 /* Check that memory at _qzz_addr is addressable for _qzz_len bytes.
    159    If suitable addressibility is not established, Valgrind prints an
    160    error message and returns the address of the first offending byte.
    161    Otherwise it returns zero. */
    162 #define VALGRIND_CHECK_MEM_IS_ADDRESSABLE(_qzz_addr,_qzz_len)      \
    163     VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                             \
    164                             VG_USERREQ__CHECK_MEM_IS_ADDRESSABLE,  \
    165                             (_qzz_addr), (_qzz_len), 0, 0, 0)
    166 
    167 /* Check that memory at _qzz_addr is addressable and defined for
    168    _qzz_len bytes.  If suitable addressibility and definedness are not
    169    established, Valgrind prints an error message and returns the
    170    address of the first offending byte.  Otherwise it returns zero. */
    171 #define VALGRIND_CHECK_MEM_IS_DEFINED(_qzz_addr,_qzz_len)        \
    172     VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                           \
    173                             VG_USERREQ__CHECK_MEM_IS_DEFINED,    \
    174                             (_qzz_addr), (_qzz_len), 0, 0, 0)
    175 
    176 /* Use this macro to force the definedness and addressibility of an
    177    lvalue to be checked.  If suitable addressibility and definedness
    178    are not established, Valgrind prints an error message and returns
    179    the address of the first offending byte.  Otherwise it returns
    180    zero. */
    181 #define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue)                \
    182    VALGRIND_CHECK_MEM_IS_DEFINED(                                \
    183       (volatile unsigned char *)&(__lvalue),                     \
    184                       (unsigned long)(sizeof (__lvalue)))
    185 
    186 
    187 /* Do a full memory leak check (like --leak-check=full) mid-execution. */
    188 #define VALGRIND_DO_LEAK_CHECK                                   \
    189    {unsigned long _qzz_res;                                      \
    190     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
    191                             VG_USERREQ__DO_LEAK_CHECK,           \
    192                             0, 0, 0, 0, 0);                      \
    193    }
    194 
    195 /* Do a summary memory leak check (like --leak-check=summary) mid-execution. */
    196 #define VALGRIND_DO_QUICK_LEAK_CHECK				 \
    197    {unsigned long _qzz_res;                                      \
    198     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                      \
    199                             VG_USERREQ__DO_LEAK_CHECK,           \
    200                             1, 0, 0, 0, 0);                      \
    201    }
    202 
    203 /* Return number of leaked, dubious, reachable and suppressed bytes found by
    204    all previous leak checks.  They must be lvalues.  */
    205 #define VALGRIND_COUNT_LEAKS(leaked, dubious, reachable, suppressed)     \
    206    /* For safety on 64-bit platforms we assign the results to private
    207       unsigned long variables, then assign these to the lvalues the user
    208       specified, which works no matter what type 'leaked', 'dubious', etc
    209       are.  We also initialise '_qzz_leaked', etc because
    210       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
    211       defined. */                                                        \
    212    {unsigned long _qzz_res;                                              \
    213     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
    214     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
    215     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                              \
    216                                VG_USERREQ__COUNT_LEAKS,                  \
    217                                &_qzz_leaked, &_qzz_dubious,              \
    218                                &_qzz_reachable, &_qzz_suppressed, 0);    \
    219     leaked     = _qzz_leaked;                                            \
    220     dubious    = _qzz_dubious;                                           \
    221     reachable  = _qzz_reachable;                                         \
    222     suppressed = _qzz_suppressed;                                        \
    223    }
    224 
    225 /* Return number of leaked, dubious, reachable and suppressed bytes found by
    226    all previous leak checks.  They must be lvalues.  */
    227 #define VALGRIND_COUNT_LEAK_BLOCKS(leaked, dubious, reachable, suppressed) \
    228    /* For safety on 64-bit platforms we assign the results to private
    229       unsigned long variables, then assign these to the lvalues the user
    230       specified, which works no matter what type 'leaked', 'dubious', etc
    231       are.  We also initialise '_qzz_leaked', etc because
    232       VG_USERREQ__COUNT_LEAKS doesn't mark the values returned as
    233       defined. */                                                        \
    234    {unsigned long _qzz_res;                                              \
    235     unsigned long _qzz_leaked    = 0, _qzz_dubious    = 0;               \
    236     unsigned long _qzz_reachable = 0, _qzz_suppressed = 0;               \
    237     VALGRIND_DO_CLIENT_REQUEST(_qzz_res, 0,                              \
    238                                VG_USERREQ__COUNT_LEAK_BLOCKS,            \
    239                                &_qzz_leaked, &_qzz_dubious,              \
    240                                &_qzz_reachable, &_qzz_suppressed, 0);    \
    241     leaked     = _qzz_leaked;                                            \
    242     dubious    = _qzz_dubious;                                           \
    243     reachable  = _qzz_reachable;                                         \
    244     suppressed = _qzz_suppressed;                                        \
    245    }
    246 
    247 
    248 /* Get the validity data for addresses [zza..zza+zznbytes-1] and copy it
    249    into the provided zzvbits array.  Return values:
    250       0   if not running on valgrind
    251       1   success
    252       2   [previously indicated unaligned arrays;  these are now allowed]
    253       3   if any parts of zzsrc/zzvbits are not addressable.
    254    The metadata is not copied in cases 0, 2 or 3 so it should be
    255    impossible to segfault your system by using this call.
    256 */
    257 #define VALGRIND_GET_VBITS(zza,zzvbits,zznbytes)                \
    258     VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                          \
    259                                     VG_USERREQ__GET_VBITS,      \
    260                                     (const char*)(zza),         \
    261                                     (char*)(zzvbits),           \
    262                                     (zznbytes), 0, 0)
    263 
    264 /* Set the validity data for addresses [zza..zza+zznbytes-1], copying it
    265    from the provided zzvbits array.  Return values:
    266       0   if not running on valgrind
    267       1   success
    268       2   [previously indicated unaligned arrays;  these are now allowed]
    269       3   if any parts of zza/zzvbits are not addressable.
    270    The metadata is not copied in cases 0, 2 or 3 so it should be
    271    impossible to segfault your system by using this call.
    272 */
    273 #define VALGRIND_SET_VBITS(zza,zzvbits,zznbytes)                \
    274     VALGRIND_DO_CLIENT_REQUEST_EXPR(0,                          \
    275                                     VG_USERREQ__SET_VBITS,      \
    276                                     (const char*)(zza),         \
    277                                     (const char*)(zzvbits),     \
    278                                     (zznbytes), 0, 0 )
    279 
    280 #endif
    281 
    282 #endif
    283