| 1 | /*----------------------------------------------------------------------*
|
|---|
| 2 | * Bounds Checking for GCC. *
|
|---|
| 3 | * Copyright (C) 1995 Richard W.M. Jones <rwmj@doc.ic.ac.uk>. *
|
|---|
| 4 | *----------------------------------------------------------------------*
|
|---|
| 5 | * This program is free software; you can redistribute it and/or modify *
|
|---|
| 6 | * it under the terms of the GNU General Public License as published by *
|
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or *
|
|---|
| 8 | * (at your option) any later version. *
|
|---|
| 9 | * *
|
|---|
| 10 | * This program is distributed in the hope that it will be useful, *
|
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|---|
| 13 | * GNU General Public License for more details. *
|
|---|
| 14 | * *
|
|---|
| 15 | * You should have received a copy of the GNU General Public License *
|
|---|
| 16 | * along with this program; if not, write to the Free Software *
|
|---|
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
|
|---|
| 18 | *----------------------------------------------------------------------*
|
|---|
| 19 | * File:
|
|---|
| 20 | * run-includes/unchecked.h
|
|---|
| 21 | * Summary:
|
|---|
| 22 | * Macros to allow per-pointer and per-use unchecked pointers.
|
|---|
| 23 | * Other notes:
|
|---|
| 24 | *
|
|---|
| 25 | * Author Date Notes
|
|---|
| 26 | * RWMJ 23/1/95 Initial implementation.
|
|---|
| 27 | * RWMJ 17/6/95 Added miscellaneous features.
|
|---|
| 28 | *----------------------------------------------------------------------*/
|
|---|
| 29 |
|
|---|
| 30 | #ifndef _bounds_unchecked_h
|
|---|
| 31 | #define _bounds_unchecked_h
|
|---|
| 32 |
|
|---|
| 33 | #if defined(__GNUC__) && defined(__BOUNDS_CHECKING_ON)
|
|---|
| 34 |
|
|---|
| 35 | /* Make pointer operations unchecked on each use.
|
|---|
| 36 | */
|
|---|
| 37 | #define UNCHECKED_PTR_DIFF(p,q) ((ptrdiff_t)(((unsigned)(p))-((unsigned)(q))/sizeof (*p)))
|
|---|
| 38 | #define UNCHECKED_PTR_PLUS_INT(p,i) ((typeof(p))((unsigned)(p))+(i)*(sizeof *p))
|
|---|
| 39 | #define UNCHECKED_PTR_MINUS_INT(p,i) ((typeof(p))((unsigned)(p))-(i)*(sizeof *p))
|
|---|
| 40 |
|
|---|
| 41 | /* How to switch checking off over a region of the program. You will need
|
|---|
| 42 | * 'DEBUG_FEATURES' set in the library. You do:
|
|---|
| 43 | * ..
|
|---|
| 44 | * BOUNDS_CHECKING_OFF;
|
|---|
| 45 | * .. strange pointer operations here ..
|
|---|
| 46 | * BOUNDS_CHECKING_ON;
|
|---|
| 47 | */
|
|---|
| 48 | #define BOUNDS_CHECKING_OFF do {extern int __bounds_debug_no_checking; \
|
|---|
| 49 | __bounds_debug_no_checking = 1;} while(0)
|
|---|
| 50 | #define BOUNDS_CHECKING_ON do {extern int __bounds_debug_no_checking; \
|
|---|
| 51 | __bounds_debug_no_checking = 0;} while(0)
|
|---|
| 52 |
|
|---|
| 53 | /* Switch bounds checking off in a single statement. You do:
|
|---|
| 54 | * BOUNDS_CHECKING_OFF_DURING (stmt);
|
|---|
| 55 | * This works for most 'ordinary' statements, ie. not returns, or gotos, etc.
|
|---|
| 56 | * For expressions, use:
|
|---|
| 57 | * BOUNDS_CHECKING_OFF_IN_EXPR (expr)
|
|---|
| 58 | */
|
|---|
| 59 | #define BOUNDS_CHECKING_OFF_DURING(stmt) \
|
|---|
| 60 | do { extern int __bounds_debug_no_checking; \
|
|---|
| 61 | __bounds_debug_no_checking = 1; \
|
|---|
| 62 | stmt; \
|
|---|
| 63 | __bounds_debug_no_checking = 0; \
|
|---|
| 64 | } while (0)
|
|---|
| 65 | #define BOUNDS_CHECKING_OFF_IN_EXPR(expr) \
|
|---|
| 66 | ({ extern int __bounds_debug_no_checking; \
|
|---|
| 67 | typeof(expr) __value; \
|
|---|
| 68 | __bounds_debug_no_checking = 1; \
|
|---|
| 69 | __value = (expr); \
|
|---|
| 70 | __bounds_debug_no_checking = 0; \
|
|---|
| 71 | __value; })
|
|---|
| 72 |
|
|---|
| 73 | #else /* not __GNUC__ or not __BOUNDS_CHECKING_ON */
|
|---|
| 74 |
|
|---|
| 75 | #define UNCHECKED_PTR_DIFF(p,q) ((p)-(q))
|
|---|
| 76 | #define UNCHECKED_PTR_PLUS_INT(p,i) ((p)+(i))
|
|---|
| 77 | #define UNCHECKED_PTR_MINUS_INT(p,i) ((p)-(i))
|
|---|
| 78 |
|
|---|
| 79 | #define BOUNDS_CHECKING_OFF
|
|---|
| 80 | #define BOUNDS_CHECKING_ON
|
|---|
| 81 |
|
|---|
| 82 | #define BOUNDS_CHECKING_OFF_DURING(stmt) stmt
|
|---|
| 83 | #define BOUNDS_CHECKING_OFF_IN_EXPR(expr) expr
|
|---|
| 84 |
|
|---|
| 85 | #endif /* __GNUC__, __BOUNDS_CHECKING_ON */
|
|---|
| 86 | #endif /* _bounds_unchecked_h */
|
|---|