Home | History | Annotate | Download | only in html
      1 <html>
      2 <head>
      3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      4 <title>11.Ptrcheck: an experimental heap, stack and global array overrun detector</title>
      5 <link rel="stylesheet" href="vg_basic.css" type="text/css">
      6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
      7 <link rel="home" href="index.html" title="Valgrind Documentation">
      8 <link rel="up" href="manual.html" title="Valgrind User Manual">
      9 <link rel="prev" href="dh-manual.html" title="10.DHAT: a dynamic heap analysis tool">
     10 <link rel="next" href="bbv-manual.html" title="12.BBV: an experimental basic block vector generation tool">
     11 </head>
     12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
     13 <div><table class="nav" width="100%" cellspacing="3" cellpadding="3" border="0" summary="Navigation header"><tr>
     14 <td width="22px" align="center" valign="middle"><a accesskey="p" href="dh-manual.html"><img src="images/prev.png" width="18" height="21" border="0" alt="Prev"></a></td>
     15 <td width="25px" align="center" valign="middle"><a accesskey="u" href="manual.html"><img src="images/up.png" width="21" height="18" border="0" alt="Up"></a></td>
     16 <td width="31px" align="center" valign="middle"><a accesskey="h" href="index.html"><img src="images/home.png" width="27" height="20" border="0" alt="Up"></a></td>
     17 <th align="center" valign="middle">Valgrind User Manual</th>
     18 <td width="22px" align="center" valign="middle"><a accesskey="n" href="bbv-manual.html"><img src="images/next.png" width="18" height="21" border="0" alt="Next"></a></td>
     19 </tr></table></div>
     20 <div class="chapter" title="11.Ptrcheck: an experimental heap, stack and global array overrun detector">
     21 <div class="titlepage"><div><div><h2 class="title">
     22 <a name="pc-manual"></a>11.Ptrcheck: an experimental heap, stack and global array overrun detector</h2></div></div></div>
     23 <div class="toc">
     24 <p><b>Table of Contents</b></p>
     25 <dl>
     26 <dt><span class="sect1"><a href="pc-manual.html#pc-manual.overview">11.1. Overview</a></span></dt>
     27 <dt><span class="sect1"><a href="pc-manual.html#pc-manual.options">11.2. Ptrcheck Command-line Options</a></span></dt>
     28 <dt><span class="sect1"><a href="pc-manual.html#pc-manual.how-works.heap-checks">11.3. How Ptrcheck Works: Heap Checks</a></span></dt>
     29 <dt><span class="sect1"><a href="pc-manual.html#pc-manual.how-works.sg-checks">11.4. How Ptrcheck Works: Stack and Global Checks</a></span></dt>
     30 <dt><span class="sect1"><a href="pc-manual.html#pc-manual.cmp-w-memcheck">11.5. Comparison with Memcheck</a></span></dt>
     31 <dt><span class="sect1"><a href="pc-manual.html#pc-manual.limitations">11.6. Limitations</a></span></dt>
     32 <dt><span class="sect1"><a href="pc-manual.html#pc-manual.todo-user-visible">11.7. Still To Do: User-visible Functionality</a></span></dt>
     33 <dt><span class="sect1"><a href="pc-manual.html#pc-manual.todo-implementation">11.8. Still To Do: Implementation Tidying</a></span></dt>
     34 </dl>
     35 </div>
     36 <p>To use this tool, you must specify
     37 <code class="option">--tool=exp-ptrcheck</code> on the Valgrind
     38 command line.</p>
     39 <div class="sect1" title="11.1.Overview">
     40 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
     41 <a name="pc-manual.overview"></a>11.1.Overview</h2></div></div></div>
     42 <p>Ptrcheck is a tool for finding overruns of heap, stack
     43 and global arrays.  Its functionality overlaps somewhat with
     44 Memcheck's, but it is able to catch invalid accesses in a number of
     45 cases that Memcheck would miss.  A detailed comparison against
     46 Memcheck is presented below.</p>
     47 <p>Ptrcheck is composed of two almost completely independent tools
     48 that have been glued together.  One part,
     49 in <code class="computeroutput">h_main.[ch]</code>, checks accesses
     50 through heap-derived pointers.  The other part, in
     51 <code class="computeroutput">sg_main.[ch]</code>, checks accesses to
     52 stack and global arrays.  The remaining
     53 files <code class="computeroutput">pc_{common,main}.[ch]</code>, provide
     54 common error-management and coordination functions, so as to make it
     55 appear as a single tool.</p>
     56 <p>The heap-check part is an extensively-hacked (largely rewritten)
     57 version of the experimental "Annelid" tool developed and described by
     58 Nicholas Nethercote and Jeremy Fitzhardinge.  The stack- and global-
     59 check part uses a heuristic approach derived from an observation about
     60 the likely forms of stack and global array accesses, and, as far as is
     61 known, is entirely novel.</p>
     62 </div>
     63 <div class="sect1" title="11.2.Ptrcheck Command-line Options">
     64 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
     65 <a name="pc-manual.options"></a>11.2.Ptrcheck Command-line Options</h2></div></div></div>
     66 <p>Ptrcheck-specific command-line options are:</p>
     67 <div class="variablelist">
     68 <a name="pc.opts.list"></a><dl>
     69 <dt>
     70 <a name="opt.enable-sg-checks"></a><span class="term">
     71       <code class="option">--enable-sg-checks=no|yes
     72       [default: yes] </code>
     73     </span>
     74 </dt>
     75 <dd><p>By default, Ptrcheck checks for overruns of stack, global
     76        and heap arrays.
     77        With <code class="varname">--enable-sg-checks=no</code>, the stack and
     78        global array checks are omitted, and only heap checking is
     79        performed.  This can be useful because the stack and global
     80        checks are quite expensive, so omitting them speeds Ptrcheck up
     81        a lot.
     82       </p></dd>
     83 <dt>
     84 <a name="opt.partial-loads-ok"></a><span class="term">
     85       <code class="option">--partial-loads-ok=&lt;yes|no&gt; [default: no] </code>
     86     </span>
     87 </dt>
     88 <dd>
     89 <p>This option has the same meaning as it does for
     90       Memcheck.</p>
     91 <p>Controls how Ptrcheck handles word-sized, word-aligned
     92       loads which partially overlap the end of heap blocks -- that is,
     93       some of the bytes in the word are validly addressable, but
     94       others are not.  When <code class="varname">yes</code>, such loads do not
     95       produce an address error.  When <code class="varname">no</code> (the
     96       default), loads from partially invalid addresses are treated the
     97       same as loads from completely invalid addresses: an illegal heap
     98       access error is issued.
     99       </p>
    100 <p>Note that code that behaves in this way is in violation of
    101       the the ISO C/C++ standards, and should be considered broken.  If
    102       at all possible, such code should be fixed.  This option should be
    103       used only as a last resort.</p>
    104 </dd>
    105 </dl>
    106 </div>
    107 </div>
    108 <div class="sect1" title="11.3.How Ptrcheck Works: Heap Checks">
    109 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
    110 <a name="pc-manual.how-works.heap-checks"></a>11.3.How Ptrcheck Works: Heap Checks</h2></div></div></div>
    111 <p>Ptrcheck can check for invalid uses of heap pointers, including
    112 out of range accesses and accesses to freed memory.  The mechanism is
    113 however completely different from Memcheck's, and the checking is more
    114 powerful.</p>
    115 <p>For each pointer in the program, Ptrcheck keeps track of which
    116 heap block (if any) it was derived from.  Then, when an access is made
    117 through that pointer, Ptrcheck compares the access address with the
    118 bounds of the associated block, and reports an error if the address is
    119 out of bounds, or if the block has been freed.</p>
    120 <p>Of course it is rarely the case that one wants to access a block
    121 only at the exact address returned by <code class="function">malloc</code> et al.
    122 Ptrcheck understands that adding or subtracting offsets from a pointer to a
    123 block results in a pointer to the same block.</p>
    124 <p>At a fundamental level, this scheme works because a correct
    125 program cannot make assumptions about the addresses returned by
    126 <code class="function">malloc</code> et al.  In particular it cannot make any
    127 assumptions about the differences in addresses returned by subsequent calls
    128 to <code class="function">malloc</code> et al.  Hence there are very few ways to take
    129 an address returned by <code class="function">malloc</code>, modify it, and still
    130 have a valid address.  In short, the only allowable operations are adding
    131 and subtracting other non-pointer values.  Almost all other operations
    132 produce a value which cannot possibly be a valid pointer.</p>
    133 </div>
    134 <div class="sect1" title="11.4.How Ptrcheck Works: Stack and Global Checks">
    135 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
    136 <a name="pc-manual.how-works.sg-checks"></a>11.4.How Ptrcheck Works: Stack and Global Checks</h2></div></div></div>
    137 <p>When a source file is compiled
    138 with <code class="option">-g</code>, the compiler attaches DWARF3
    139 debugging information which describes the location of all stack and
    140 global arrays in the file.</p>
    141 <p>Checking of accesses to such arrays would then be relatively
    142 simple, if the compiler could also tell us which array (if any) each
    143 memory referencing instruction was supposed to access.  Unfortunately
    144 the DWARF3 debugging format does not provide a way to represent such
    145 information, so we have to resort to a heuristic technique to
    146 approximate the same information.  The key observation is that
    147    <span class="emphasis"><em>
    148    if a memory referencing instruction accesses inside a stack or
    149    global array once, then it is highly likely to always access that
    150    same array</em></span>.</p>
    151 <p>To see how this might be useful, consider the following buggy
    152 fragment:</p>
    153 <pre class="programlisting">
    154    { int i, a[10];  // both are auto vars
    155      for (i = 0; i &lt;= 10; i++)
    156         a[i] = 42;
    157    }
    158 </pre>
    159 <p>At run time we will know the precise address
    160 of <code class="computeroutput">a[]</code> on the stack, and so we can
    161 observe that the first store resulting from <code class="computeroutput">a[i] =
    162 42</code> writes <code class="computeroutput">a[]</code>, and
    163 we will (correctly) assume that that instruction is intended always to
    164 access <code class="computeroutput">a[]</code>.  Then, on the 11th
    165 iteration, it accesses somewhere else, possibly a different local,
    166 possibly an un-accounted for area of the stack (eg, spill slot), so
    167 Ptrcheck reports an error.</p>
    168 <p>There is an important caveat.</p>
    169 <p>Imagine a function such as <code class="function">memcpy</code>, which is used
    170 to read and write many different areas of memory over the lifetime of the
    171 program.  If we insist that the read and write instructions in its memory
    172 copying loop only ever access one particular stack or global variable, we
    173 will be flooded with errors resulting from calls to
    174 <code class="function">memcpy</code>.</p>
    175 <p>To avoid this problem, Ptrcheck instantiates fresh likely-target
    176 records for each entry to a function, and discards them on exit.  This
    177 allows detection of cases where (e.g.) <code class="function">memcpy</code> overflows
    178 its source or destination buffers for any specific call, but does not carry
    179 any restriction from one call to the next.  Indeed, multiple threads may be
    180 multiple simultaneous calls to (e.g.) <code class="function">memcpy</code> without
    181 mutual interference.</p>
    182 </div>
    183 <div class="sect1" title="11.5.Comparison with Memcheck">
    184 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
    185 <a name="pc-manual.cmp-w-memcheck"></a>11.5.Comparison with Memcheck</h2></div></div></div>
    186 <p>Memcheck does not do any access checks for stack or global arrays, so
    187 the presence of those in Ptrcheck is a straight win.  (But see
    188 "Limitations" below).</p>
    189 <p>Memcheck and Ptrcheck use different approaches for checking heap
    190 accesses.  Memcheck maintains bitmaps telling it which areas of memory
    191 are accessible and which are not.  If a memory access falls in an
    192 unaccessible area, it reports an error.  By marking the 16 bytes
    193 before and after an allocated block unaccessible, Memcheck is able to
    194 detect small over- and underruns of the block.  Similarly, by marking
    195 freed memory as unaccessible, Memcheck can detect all accesses to
    196 freed memory.</p>
    197 <p>Memcheck's approach is simple.  But it's also weak.  It can't
    198 catch block overruns beyond 16 bytes.  And, more generally, because it
    199 focusses only on the question "is the target address accessible", it
    200 fails to detect invalid accesses which just happen to fall within some
    201 other valid area.  This is not improbable, especially in crowded areas
    202 of the process' address space.</p>
    203 <p>Ptrcheck's approach is to keep track of pointers derived from
    204 heap blocks.  It tracks pointers which are derived directly from calls
    205 to <code class="function">malloc</code> et al, but also ones derived indirectly, by
    206 adding or subtracting offsets from the directly-derived pointers.  When a
    207 pointer is finally used to access memory, Ptrcheck compares the access
    208 address with that of the block it was originally derived from, and
    209 reports an error if the access address is not within the block
    210 bounds.</p>
    211 <p>Consequently Ptrcheck can detect any out of bounds access
    212 through a heap-derived pointer, no matter how far from the original
    213 block it is.</p>
    214 <p>A second advantage is that Ptrcheck is better at detecting
    215 accesses to blocks freed very far in the past.  Memcheck can detect
    216 these too, but only for blocks freed relatively recently.  To detect
    217 accesses to a freed block, Memcheck must make it inaccessible, hence
    218 requiring a space overhead proportional to the size of the block.  If
    219 the blocks are large, Memcheck will have to make them available for
    220 re-allocation relatively quickly, thereby losing the ability to detect
    221 invalid accesses to them.</p>
    222 <p>By contrast, Ptrcheck has a constant per-block space requirement
    223 of four machine words, for detection of accesses to freed blocks.  A
    224 freed block can be reallocated immediately, yet Ptrcheck can still
    225 detect all invalid accesses through any pointers derived from the old
    226 allocation, providing only that the four-word descriptor for the old
    227 allocation is stored.  For example, on a 64-bit machine, to detect
    228 accesses in any of the most recently freed 10 million blocks, Ptrcheck
    229 will require only 320MB of extra storage.  Achieving the same level of
    230 detection with Memcheck is close to impossible and would likely
    231 involve several gigabytes of extra storage.</p>
    232 <p>Having said all that, remember that Memcheck performs uninitialised
    233 value checking, invalid and mismatched free checking, overlap checking, and
    234 leak checking, none of which Ptrcheck do.  Memcheck has also benefitted from
    235 years of refinement, tuning, and experience with production-level usage, and
    236 so is much faster than Ptrcheck as it currently stands.
    237 </p>
    238 <p>Consequently we recommend you first make your programs run Memcheck
    239 clean.  Once that's done, try Ptrcheck to see if you can shake out any
    240 further heap, global or stack errors.</p>
    241 </div>
    242 <div class="sect1" title="11.6.Limitations">
    243 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
    244 <a name="pc-manual.limitations"></a>11.6.Limitations</h2></div></div></div>
    245 <p>This is an experimental tool, which relies rather too heavily on some
    246 not-as-robust-as-I-would-like assumptions on the behaviour of correct
    247 programs.  There are a number of limitations which you should be aware
    248 of.</p>
    249 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
    250 <li class="listitem"><p>Heap checks: Ptrcheck can occasionally lose track of, or
    251    become confused about, which heap block a given pointer has been
    252    derived from.  This can cause it to falsely report errors, or to
    253    miss some errors.  This is not believed to be a serious
    254    problem.</p></li>
    255 <li class="listitem"><p>Heap checks: Ptrcheck only tracks pointers that are stored
    256    properly aligned in memory.  If a pointer is stored at a misaligned
    257    address, and then later read again, Ptrcheck will lose track of
    258    what it points at.  Similar problem if a pointer is split into
    259    pieces and later reconsitituted.</p></li>
    260 <li class="listitem"><p>Heap checks: Ptrcheck needs to "understand" which system
    261    calls return pointers and which don't.  Many, but not all system
    262    calls are handled.  If an unhandled one is encountered, Ptrcheck will
    263    abort.  Fortunately, adding support for a new syscall is very
    264    easy.</p></li>
    265 <li class="listitem"><p>Stack checks: It follows from the description above (<a class="xref" href="pc-manual.html#pc-manual.how-works.sg-checks" title="11.4.How Ptrcheck Works: Stack and Global Checks">How Ptrcheck Works: Stack and Global Checks</a>) that the first access by a
    266    memory referencing instruction to a stack or global array creates an
    267    association between that instruction and the array, which is checked on
    268    subsequent accesses by that instruction, until the containing function
    269    exits.  Hence, the first access by an instruction to an array (in any
    270    given function instantiation) is not checked for overrun, since Ptrcheck
    271    uses that as the "example" of how subsequent accesses should
    272    behave.</p></li>
    273 <li class="listitem">
    274 <p>Stack checks: Similarly, and more serious, it is clearly
    275    possible to write legitimate pieces of code which break the basic
    276    assumption upon which the stack/global checking rests.  For
    277    example:</p>
    278 <pre class="programlisting">
    279   { int a[10], b[10], *p, i;
    280     for (i = 0; i &lt; 10; i++) {
    281        p = /* arbitrary condition */  ? &amp;a[i]  : &amp;b[i];
    282        *p = 42;
    283     }
    284   }
    285 </pre>
    286 <p>In this case the store sometimes
    287    accesses <code class="computeroutput">a[]</code> and
    288    sometimes <code class="computeroutput">b[]</code>, but in no cases is
    289    the addressed array overrun.  Nevertheless the change in target
    290    will cause an error to be reported.</p>
    291 <p>It is hard to see how to get around this problem.  The only
    292    mitigating factor is that such constructions appear very rare, at
    293    least judging from the results using the tool so far.  Such a
    294    construction appears only once in the Valgrind sources (running
    295    Valgrind on Valgrind) and perhaps two or three times for a start
    296    and exit of Firefox.  The best that can be done is to suppress the
    297    errors.</p>
    298 </li>
    299 <li class="listitem"><p>Performance: the stack/global checks require reading all of
    300    the DWARF3 type and variable information on the executable and its
    301    shared objects.  This is computationally expensive and makes
    302    startup quite slow.  You can expect debuginfo reading time to be in
    303    the region of a minute for an OpenOffice sized application, on a
    304    2.4 GHz Core 2 machine.  Reading this information also requires a
    305    lot of memory.  To make it viable, Ptrcheck goes to considerable
    306    trouble to compress the in-memory representation of the DWARF3
    307    data, which is why the process of reading it appears slow.</p></li>
    308 <li class="listitem"><p>Performance: Ptrcheck runs slower than Memcheck.  This is
    309    partly due to a lack of tuning, but partly due to algorithmic
    310    difficulties.  The heap-check side is potentially quite fast.  The
    311    stack and global checks can sometimes require a number of range
    312    checks per memory access, and these are difficult to short-circuit
    313    (despite considerable efforts having been made).
    314    </p></li>
    315 <li class="listitem">
    316 <p>Coverage: the heap checking is relatively robust, requiring
    317    only that Ptrcheck can see calls to <code class="function">malloc</code> et al.
    318    In that sense it has debug-info requirements comparable with Memcheck,
    319    and is able to heap-check programs even with no debugging information
    320    attached.</p>
    321 <p>Stack/global checking is much more fragile.  If a shared
    322    object does not have debug information attached, then Ptrcheck will
    323    not be able to determine the bounds of any stack or global arrays
    324    defined within that shared object, and so will not be able to check
    325    accesses to them.  This is true even when those arrays are accessed
    326    from some other shared object which was compiled with debug
    327    info.</p>
    328 <p>At the moment Ptrcheck accepts objects lacking debuginfo
    329    without comment.  This is dangerous as it causes Ptrcheck to
    330    silently skip stack and global checking for such objects.  It would
    331    be better to print a warning in such circumstances.</p>
    332 </li>
    333 <li class="listitem"><p>Coverage: Ptrcheck checks that the areas read or written by
    334    system calls do not overrun heap blocks.  But it doesn't currently
    335    check them for overruns stack and global arrays.  This would be
    336    easy to add.</p></li>
    337 <li class="listitem"><p>Platforms: the stack/global checks won't work properly on any
    338    PowerPC platforms, only on x86 and amd64 targets.  That's because
    339    the stack and global checking requires tracking function calls and
    340    exits reliably, and there's no obvious way to do it with the PPC
    341    ABIs.  (In comparison, with the x86 and amd64 ABIs this is relatively
    342    straightforward.)</p></li>
    343 <li class="listitem"><p>Robustness: related to the previous point.  Function
    344    call/exit tracking for x86/amd64 is believed to work properly even
    345    in the presence of longjmps within the same stack (although this
    346    has not been tested).  However, code which switches stacks is
    347    likely to cause breakage/chaos.</p></li>
    348 </ul></div>
    349 </div>
    350 <div class="sect1" title="11.7.Still To Do: User-visible Functionality">
    351 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
    352 <a name="pc-manual.todo-user-visible"></a>11.7.Still To Do: User-visible Functionality</h2></div></div></div>
    353 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
    354 <li class="listitem"><p>Extend system call checking to work on stack and global arrays.</p></li>
    355 <li class="listitem"><p>Print a warning if a shared object does not have debug info
    356    attached, or if, for whatever reason, debug info could not be
    357    found, or read.</p></li>
    358 </ul></div>
    359 </div>
    360 <div class="sect1" title="11.8.Still To Do: Implementation Tidying">
    361 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
    362 <a name="pc-manual.todo-implementation"></a>11.8.Still To Do: Implementation Tidying</h2></div></div></div>
    363 <p>Items marked CRITICAL are considered important for correctness:
    364 non-fixage of them is liable to lead to crashes or assertion failures
    365 in real use.</p>
    366 <div class="itemizedlist"><ul class="itemizedlist" type="disc">
    367 <li class="listitem"><p>h_main.c: make N_FREED_SEGS command-line configurable.</p></li>
    368 <li class="listitem"><p> sg_main.c: Improve the performance of the stack / global
    369    checks by doing some up-front filtering to ignore references in
    370    areas which "obviously" can't be stack or globals.  This will
    371    require using information that m_aspacemgr knows about the address
    372    space layout.</p></li>
    373 <li class="listitem"><p>h_main.c: get rid of the last_seg_added hack; add suitable
    374    plumbing to the core/tool interface to do this cleanly.</p></li>
    375 <li class="listitem"><p>h_main.c: move vast amounts of arch-dependent ugliness
    376    (get_IntRegInfo et al) to its own source file, a la
    377    mc_machine.c.</p></li>
    378 <li class="listitem"><p>h_main.c: make the lossage-check stuff work again, as a way
    379    of doing quality assurance on the implementation.</p></li>
    380 <li class="listitem"><p>h_main.c: schemeEw_Atom: don't generate a call to
    381    nonptr_or_unknown, this is really stupid, since it could be done at
    382    translation time instead.</p></li>
    383 <li class="listitem"><p>CRITICAL: h_main.c: h_instrument (main instrumentation fn):
    384    generate shadows for word-sized temps defined in the block's
    385    preamble.  (Why does this work at all, as it stands?)</p></li>
    386 <li class="listitem"><p>sg_main.c: fix compute_II_hash to make it a bit more sensible
    387    for ppc32/64 targets (except that sg_ doesn't work on ppc32/64
    388    targets, so this is a bit academic at the moment).</p></li>
    389 </ul></div>
    390 </div>
    391 </div>
    392 <div>
    393 <br><table class="nav" width="100%" cellspacing="3" cellpadding="2" border="0" summary="Navigation footer">
    394 <tr>
    395 <td rowspan="2" width="40%" align="left">
    396 <a accesskey="p" href="dh-manual.html">&lt;&lt;10.DHAT: a dynamic heap analysis tool</a></td>
    397 <td width="20%" align="center"><a accesskey="u" href="manual.html">Up</a></td>
    398 <td rowspan="2" width="40%" align="right"><a accesskey="n" href="bbv-manual.html">12.BBV: an experimental basic block vector generation tool&gt;&gt;</a>
    399 </td>
    400 </tr>
    401 <tr><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td></tr>
    402 </table>
    403 </div>
    404 </body>
    405 </html>
    406