Back to index

glibc  2.9
bp-start.h
Go to the documentation of this file.
00001 /* Bounded-pointer checking macros for C.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Greg McGary <greg@mcgary.org>
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 
00022 #if __BOUNDED_POINTERS__
00023 
00024   /* The command-line arg vector and environment vector come to us from
00025      the OS as an unbounded pointer to an array of unbounded strings.
00026      The user's main expects argv and __environ to be bounded pointers
00027      to arrays of bounded strings.  */
00028 # define INIT_ARGV_and_ENVIRON \
00029   do {                                                               \
00030     int envc;                                                        \
00031     for (envc = 0; *ubp_ev; ubp_ev++, envc++)                               \
00032       ;                                                                     \
00033     ubp_ev -= envc;                                                  \
00034                                                                      \
00035     /* GKM FIXME: we could save some space by allocating only enough for      \
00036        the additional low & high words, and destructively rewriting         \
00037        argv in place.  */                                            \
00038     __ptrvalue (argv) = __ptrlow (argv)                                     \
00039       = alloca ((argc + envc + 2) * sizeof (*argv));                        \
00040     __ptrhigh (argv) = __ptrvalue (argv) + argc + 1;                        \
00041     __ptrvalue (__environ) = __ptrlow (__environ) = __ptrhigh (argv);       \
00042     __ptrhigh (__environ) = __ptrvalue (__environ) + envc + 1;              \
00043     boundify_vector (__environ, ubp_ev);                             \
00044     boundify_vector (argv, ubp_av);                                         \
00045   } while (0)
00046 
00047 
00048 /* Copy an unbounded vector of unbounded strings into a bounded
00049    counterpart.  */
00050 
00051 static void
00052 boundify_vector (char **dest, char *__unbounded *__unbounded src)
00053 {
00054   char *__unbounded s;
00055   for (; *src; src++, dest++)
00056     {
00057       __ptrvalue (*dest) = __ptrlow (*dest) = *src;
00058       __ptrhigh (*dest) = src[1];
00059     }
00060   *dest = 0;
00061   /* The OS lays out strings contiguously in vector order,
00062      so  */
00063   for (s = __ptrvalue (dest[-1]); *s; s++)
00064     ;
00065   __ptrhigh (dest[-1]) = ++s;
00066 }
00067 
00068 #else
00069 
00070 # define INIT_ARGV_and_ENVIRON __environ = ubp_ev
00071 
00072 #endif