1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2<html> 3 4<head> 5<title>GCC Bounds Checking</title> 6</head> 7 8<body> 9 10<h2><a name=Example>GCC Bounds Checking Example</a></h2> 11 12<p><code><pre> 13$ cat example.c 14main() { 15char *malloc(); char *a = malloc(2); 16a[2] = 'x'; 17} 18$ ./bgcc -fbounds-checking example.c 19$ ./a.out 20Bounds Checking GCC v gcc-4.0.4-3.2 Copyright (C) 1995 Richard W.M. Jones 21Bounds Checking comes with ABSOLUTELY NO WARRANTY. For details see file 22`COPYING' that should have come with the source to this program. 23Bounds Checking is free software, and you are welcome to redistribute it 24under certain conditions. See the file `COPYING' for details. 25For more information, set GCC_BOUNDS_OPTS to `-help' 26example.c:3:Bounds error: attempt to reference memory overrunning the end of an object. 27example.c:3: Pointer value: 0x8431002, Size: 1 28example.c:3: Object `malloc': 29example.c:3: Address in memory: 0x8431000 .. 0x8431001 30example.c:3: Size: 2 bytes 31example.c:3: Element size: 1 bytes 32example.c:3: Number of elements: 2 33example.c:3: Created at: example.c, line 2 34example.c:3: Storage class: heap 35</pre></code> 36 37<h2><a name=Example2>A Longer GCC Bounds Checking Example</a></h2> 38 39<p> 40 41This example shows some additional features: 42<ul> 43<li>Modules compiled with and without bounds checking may be linked together without special preparation. 44 <ul> 45 <li>Checked pointers may be passed to modules compiled without bounds checking. 46 <li>Structures containing pointers may be passed between modules compiled with and without bounds checking. 47 </ul> 48<li>Modules compiled without bounds checking still check some pointer accesses. 49 <ul> 50 <li>Checked pointers (pointers created by <code>malloc</code> or by modules compiled with 51 bounds checking) are checked in calls to the <code>str*</code> and <code>mem*</code> 52 library functions and in calls to modules compiled with bounds checking. 53 <li>The checks are similar to a malloc debug library, except more powerful because they 54 also check local and static variables created in modules compiled with checking. 55 </ul> 56<li>The <code>GCC_BOUNDS_OPTS</code> environment variable passes options to the bounds checker. 57 <ul> 58 <li>The <code>-never-fatal</code> option continues after errors. 59 <li>The <code>-print-heap</code> option shows memory leaks. 60 </ul> 61</ul> 62 63<p><code><pre> 64$ cat example2.c 65void test(char *p) { p[1] = 'x'; } /* access a pointer out of range */ 66 67static char static_var[1]; /* example static variable */ 68 69main() { 70char local_var[1]; /* example stack variable */ 71char *heap_var, *malloc(); 72struct { char *a; char *b; } s; /* example structure with pointers */ 73heap_var = malloc(1); /* example heap variable */ 74test(local_var); /* access a local variable out of range */ 75test(static_var); /* access a static variable out of range */ 76test(heap_var); /* access a heap variable out of range */ 77s.a = local_var; 78s.b = static_var; 79unchecked(&s); /* pass to a routine in unchecked module */ 80} 81$ cat example2u.c 82/* example unchecked module mixed with checked module */ 83/* No special preparation is required, even for */ 84/* structures containing pointers. */ 85 86struct s_tag { char *a; char *b; }; 87 88void unchecked(struct s_tag *s) { 89memset(s->a, 0, 2); /* example of catching checked pointers in */ 90memcpy(s->b, s->a, 2); /* library functions even from unchecked code */ 91} 92$ gcc -c example2u.c 93$ ./bgcc -fbounds-checking example2.c example2u.o 94$ GCC_BOUNDS_OPTS="-never-fatal -print-heap" ./a.out 95Bounds Checking GCC v gcc-4.0.4-3.2 Copyright (C) 1995 Richard W.M. Jones 96Bounds Checking comes with ABSOLUTELY NO WARRANTY. For details see file 97`COPYING' that should have come with the source to this program. 98Bounds Checking is free software, and you are welcome to redistribute it 99under certain conditions. See the file `COPYING' for details. 100For more information, set GCC_BOUNDS_OPTS to `-help' 101example2.c:1:Bounds error: attempt to reference memory overrunning the end of an object. 102example2.c:1: Pointer value: 0xbff63f6b, Size: 1 103example2.c:1: Object `local_var': 104example2.c:1: Address in memory: 0xbff63f6a .. 0xbff63f6a 105example2.c:1: Size: 1 bytes 106example2.c:1: Element size: 1 bytes 107example2.c:1: Number of elements: 1 108example2.c:1: Created at: example2.c, line 6 109example2.c:1: Storage class: stack 110example2.c:1:Bounds error: attempt to reference memory overrunning the end of an object. 111example2.c:1: Pointer value: 0x80691e9, Size: 1 112example2.c:1: Object `static_var': 113example2.c:1: Address in memory: 0x80691e8 .. 0x80691e8 114example2.c:1: Size: 1 bytes 115example2.c:1: Element size: 1 bytes 116example2.c:1: Number of elements: 1 117example2.c:1: Created at: example2.c, line 3 118example2.c:1: Storage class: static 119example2.c:1:Bounds error: attempt to reference memory overrunning the end of an object. 120example2.c:1: Pointer value: 0x8f46001, Size: 1 121example2.c:1: Object `malloc': 122example2.c:1: Address in memory: 0x8f46000 .. 0x8f46000 123example2.c:1: Size: 1 bytes 124example2.c:1: Element size: 1 bytes 125example2.c:1: Number of elements: 1 126example2.c:1: Created at: example2.c, line 9 127example2.c:1: Storage class: heap 128<unknown>:0:Bounds error: memset with this destination pointer and size 2 would overrun the end of the object's allocated memory. 129<unknown>:0: Pointer value: 0xbff63f6a 130<unknown>:0: Object `local_var': 131<unknown>:0: Address in memory: 0xbff63f6a .. 0xbff63f6a 132<unknown>:0: Size: 1 bytes 133<unknown>:0: Element size: 1 bytes 134<unknown>:0: Number of elements: 1 135<unknown>:0: Created at: example2.c, line 6 136<unknown>:0: Storage class: stack 137<unknown>:0:Bounds error: memcpy with this destination pointer and size 2 would overrun the end of the object's allocated memory. 138<unknown>:0: Pointer value: 0x80691e8 139<unknown>:0: Object `static_var': 140<unknown>:0: Address in memory: 0x80691e8 .. 0x80691e8 141<unknown>:0: Size: 1 bytes 142<unknown>:0: Element size: 1 bytes 143<unknown>:0: Number of elements: 1 144<unknown>:0: Created at: example2.c, line 3 145<unknown>:0: Storage class: static 146<unknown>:0:Bounds error: memcpy with this source pointer and size 2 would overrun the end of the object's allocated memory. 147<unknown>:0: Pointer value: 0xbff63f6a 148<unknown>:0: Object `local_var': 149<unknown>:0: Address in memory: 0xbff63f6a .. 0xbff63f6a 150<unknown>:0: Size: 1 bytes 151<unknown>:0: Element size: 1 bytes 152<unknown>:0: Number of elements: 1 153<unknown>:0: Created at: example2.c, line 6 154<unknown>:0: Storage class: stack 155Bounds library call frequency statistics: 156 Calls to push, pop, param function: 1, 1, 0 157 Calls to add, delete stack: 2, 2 158 Calls to add, delete heap: 1, 0 159 Calls to check pointer +/- integer: 3 160 Calls to check array references: 0 161 Calls to check pointer differences: 0 162 Calls to check object references: 3 163 Calls to check component references: 0 164 Calls to check truth, falsity of pointers: 0, 0 165 Calls to check <, >, <=, >= of pointers: 0 166 Calls to check ==, != of pointers: 0 167 Calls to check p++, ++p, p--, --p: 0, 0, 0, 0 168 Calls to add, find, delete oob pointers: 0, 0, 0 169 References to unchecked static, stack: 0, 0 170Filename = example2.c, Line = 9, Function = malloc, Count = 1 Avg Size = 1, Total = 1 171</pre></code> 172<h1><a name="download">GCC</a> Bounds Checking Patches</h1> 173 174<h2> 175Download the official bounds checking patches for GCC releases from Haj Ten Brugge on 176<A HREF="http://sourceforge.net/projects/boundschecking/">sourceforge</A>. 177Download bounds checking patches for <A HREF="https://williambader.com/bounds/gcc-4.0.4-bgcc-4.0.4.pat.bz2">gcc-4.0.4</A>. 178<br> 179Download older patches: 180<A HREF="https://williambader.com/bounds/gcc-3.4.6-bgcc-3.4.6.pat.bz2">gcc-3.4.6</A> 181<A HREF="https://williambader.com/bounds/gcc-4.0.2-bgcc-4.0.2.pat.bz2">gcc-4.0.2</A> 182<!--
183<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-2.95.3-bgcc-2.95.3.pat.bz2">gcc-2.95.3</A> 184<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.0-bgcc-3.0.pat.bz2">gcc-3.0</A> 185<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.0.1-bgcc-3.0.1.pat.bz2">gcc-3.0.1</A> 186<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.0.2-bgcc-3.0.2.pat.bz2">gcc-3.0.2</A> 187<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.0.3-bgcc-3.0.3.pat.bz2">gcc-3.0.3</A> 188<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.0.4-bgcc-3.0.4.pat.bz2">gcc-3.0.4</A> 189<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.1-bgcc-3.1.pat.bz2">gcc-3.1</A> 190<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.1.1-bgcc-3.1.1.pat.bz2">gcc-3.1.1</A> 191<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.2-bgcc-3.2.pat.bz2">gcc-3.2</A> 192<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.2.1-bgcc-3.2.1.pat.bz2">gcc-3.2.1</A> 193<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.2.2-bgcc-3.2.2.pat.bz2">gcc-3.2.2</A> 194<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.2.3-bgcc-3.2.3.pat.bz2">gcc-3.2.3</A> 195<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.3-bgcc-3.3.pat.bz2">gcc-3.3</A> 196<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.3.1-bgcc-3.3.1.pat.bz2">gcc-3.3.1</A> 197<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.3.2-bgcc-3.3.2.pat.bz2">gcc-3.3.2</A> 198<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.3.3-bgcc-3.3.3.pat.bz2">gcc-3.3.3</A> 199<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.4.1-bgcc-3.4.1.pat.bz2">gcc-3.4.1</A> 200<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.4.2-bgcc-3.4.2.pat.bz2">gcc-3.4.2</A> 201<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.4.3-bgcc-3.4.3.pat.bz2">gcc-3.4.3</A> 202<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-3.4.6-bgcc-3.4.6.pat.bz2">gcc-3.4.6</A> 203<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-4.0.1-bgcc-4.0.1.pat.bz2">gcc-4.0.1</A> 204<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-4.0.2-bgcc-4.0.2.pat.bz2">gcc-4.0.2</A> 205<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/gcc-4.0.4-bgcc-4.0.4.pat.bz2">gcc-4.0.4</A> 206--> 207</h2> 208 209<p> 210To build a bounds checking gcc: 211<pre> 212<code> 213 cd /u/gnu # go to a work area 214 tar xzf gcc-x.y.tar.gz # unpack gcc 215 mv gcc-x.y bgcc-x.y # rename the gcc directory 216 cd bgcc-x.y # go to the bgcc directory 217 patch -p1 -T < gcc-x.y-bgcc-x.y.pat # apply the patches 218 touch gcc/c-parse.in # force a rebuild of .y and .c 219 mkdir objdir # make an object file area 220 cd objdir # enter the area 221 /u/gnu/bgcc-x.y/configure # initialize the build 222 make bootstrap # do the build 223</code> 224</pre> 225You can run the bounds checking gcc from its build area using the script 226<code>/u/gnu/bgcc-x.y/gcc/bounds/bgcc</code><br> 227For more build information, see the <a href="https://williambader.com/bounds/README.103">README file</A>. 228<!-- 229<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/">ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/</A> 230//--> 231or the files created by the patches in <code>/u/gnu/bgcc-x.y/gcc/bounds/</code>. 232 233<p>These patches add a <code>-fbounds-checking</code> flag that 234adds bounds checking tests to pointer and array accesses. 235Richard Jones developed the 236<A HREF="http://www-ala.doc.ic.ac.uk/~phjk/BoundsChecking.html">patches against gcc-2.7</A> 237in 1995. 238Herman ten Brugge is the current maintainer and updates patches to the 239<A HREF="http://sourceforge.net/projects/boundschecking/">boundschecking project at sourceforge</A>. 240<a href="https://williambader.com">William Bader</a> has 241<a href="https://williambader.com/bounds/example.html#download">unofficial updates</a>. 242 243<!-- 244<p>A copy of Richard Jones' manual is at
245<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/bounds-checking-reports.tar.bz2"> 246ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/bounds-checking-reports.tar.bz2 247</A>. 248 249<p>A copy of the 2002 paper by Olatunji Ruwase and Monica Lam 250on their C Range Error Detector bounds checking enhancements is at 251<A HREF="ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/cred.pdf"> 252ftp://ftp.newspapersystems.com/pub/binaries/boundschecking/cred.pdf 253</A>. 254//--> 255 256<p>You may freely mix object modules compiled with and without bounds checking. 257The bounds checker also includes replacements for 258<code>mem*</code> and <code>str*</code> routines 259and can detect invalid calls against checked memory objects, even from modules 260compiled without bounds checking. 261 262<i><small> 263<p>These patches are unrelated to the fat pointer bounds checking patches by 264Greg McGary <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35525e5875505b521b544656505b511b565a58">[email protected]</a> which change the size of pointers and require 265building modified versions of libc and most other libraries that your program 266calls. Greg's patches will eventually be incorporated into GCC. 267If you can use Greg's fat pointer bounds checker, it has the advantage of 268better run-time performance and support for languages other than C. 269</small></i> 270 271<i><small> 272<p><b>Valgrind</b> by Julian Seward is another open-source memory debugger for Intel x86-based Linux systems. 273Valgrind runs unmodified ELF x86 Linux executables within a Pentium emulator and detects 274accesses to uninitialized variables, accesses to unallocated memory, and memory leaks. 275In comparison to bounds checking gcc, valgrind has the advantages of detecting accesses to 276uninitialized variables, of not requiring recompilation or relinking, and of supporting C++. 277Valgrind has the disadvantages of working only on x86-based Linux systems and of not detecting 278out-of-bounds array accesses as long as the accesses still produce valid addresses (so it often 279misses off-by-one accesses to local arrays). 280Valgrind has more memory overhead than bounds checking gcc but about the same amount of cpu overhead. 281I regularly use both bounds checking gcc and valgrind. 282<br> 283Valgrind Home 284<a href="http://valgrind.kde.org/">http://valgrind.kde.org/</a> 285<br> 286Valgrind freshmeat project 287<a href="http://freshmeat.net/projects/valgrind/">http://freshmeat.net/projects/valgrind/</a> 288<br> 289Valgrind HowTo 290<a href="http://www.tldp.org/HOWTO/Valgrind-HOWTO/">http://www.tldp.org/HOWTO/Valgrind-HOWTO/</a> 291</small></i> 292 293<hr> 294<address> 295Return to <A HREF="http://gcc.gnu.org/extensions.html">GCC extensions</A> 296<br> 297<small>Last modified 2982007-05-12 299</small> 300</address>
vendor: 110 bytes, lines 300-302
300 301 302<script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script>
302<script type="module" src="https://static.cloudflareinsights.com/beacon.min.js/v31edd6df95cf4e85bb4c19e7a9bdbcba1788362987495" integrity="sha512-iIg7k2xntmwu6/uSb5tpc/hySgZc4eoL31yB29W6tJFo2akwjPWcEqnCEdJvGexCL0KEQwVYv5BlowfhVz26hg==" data-cf-beacon='{"version":"2024.11.0","token":"994506ef87924eeba79654a40d48c911","r":1,"spa":2}' crossorigin="anonymous"></script>
302 303</body> 304</html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.