1 /* Mudflap: narrow-pointer bounds-checking by tree rewriting.
2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Frank Ch. Eigler <fche@redhat.com>
4 and Graydon Hoare <graydon@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING. If not, write to the Free
29 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
35 #ifndef HAVE_SOCKLEN_T
39 /* These attempt to coax various unix flavours to declare all our
40 needed tidbits in the system headers. */
41 #if !defined(__FreeBSD__) && !defined(__APPLE__)
43 #endif /* Some BSDs break <sys/socket.h> if this is defined. */
47 #define __EXTENSIONS__
49 #define _LARGE_FILE_API
50 #define _LARGEFILE64_SOURCE
51 #define _XOPEN_SOURCE_EXTENDED 1
59 #include <sys/types.h>
72 #ifdef HAVE_SYS_SOCKET_H
73 #include <sys/socket.h>
78 #ifdef HAVE_SYS_WAIT_H
99 #ifdef HAVE_SYS_SOCKET_H
100 #include <sys/socket.h>
102 #ifdef HAVE_NETINET_IN_H
103 #include <netinet/in.h>
105 #ifdef HAVE_ARPA_INET_H
106 #include <arpa/inet.h>
109 #include "mf-runtime.h"
113 #error "Do not compile this file with -fmudflap!"
117 /* A bunch of independent stdlib/unistd hook functions, all
118 intercepted by mf-runtime.h macros. */
121 static inline size_t (strnlen
) (const char* str
, size_t n
)
125 for (s
= str
; n
&& *s
; ++s
, --n
)
134 WRAPPER2(void *, memcpy
, void *dest
, const void *src
, size_t n
)
136 TRACE ("%s\n", __PRETTY_FUNCTION__
);
137 MF_VALIDATE_EXTENT(src
, n
, __MF_CHECK_READ
, "memcpy source");
138 MF_VALIDATE_EXTENT(dest
, n
, __MF_CHECK_WRITE
, "memcpy dest");
139 return memcpy (dest
, src
, n
);
143 WRAPPER2(void *, memmove
, void *dest
, const void *src
, size_t n
)
145 TRACE ("%s\n", __PRETTY_FUNCTION__
);
146 MF_VALIDATE_EXTENT(src
, n
, __MF_CHECK_READ
, "memmove src");
147 MF_VALIDATE_EXTENT(dest
, n
, __MF_CHECK_WRITE
, "memmove dest");
148 return memmove (dest
, src
, n
);
152 WRAPPER2(void *, memset
, void *s
, int c
, size_t n
)
154 TRACE ("%s\n", __PRETTY_FUNCTION__
);
155 MF_VALIDATE_EXTENT(s
, n
, __MF_CHECK_WRITE
, "memset dest");
156 return memset (s
, c
, n
);
160 WRAPPER2(int, memcmp
, const void *s1
, const void *s2
, size_t n
)
162 TRACE ("%s\n", __PRETTY_FUNCTION__
);
163 MF_VALIDATE_EXTENT(s1
, n
, __MF_CHECK_READ
, "memcmp 1st arg");
164 MF_VALIDATE_EXTENT(s2
, n
, __MF_CHECK_READ
, "memcmp 2nd arg");
165 return memcmp (s1
, s2
, n
);
169 WRAPPER2(void *, memchr
, const void *s
, int c
, size_t n
)
171 TRACE ("%s\n", __PRETTY_FUNCTION__
);
172 MF_VALIDATE_EXTENT(s
, n
, __MF_CHECK_READ
, "memchr region");
173 return memchr (s
, c
, n
);
178 WRAPPER2(void *, memrchr
, const void *s
, int c
, size_t n
)
180 TRACE ("%s\n", __PRETTY_FUNCTION__
);
181 MF_VALIDATE_EXTENT(s
, n
, __MF_CHECK_READ
, "memrchr region");
182 return memrchr (s
, c
, n
);
187 WRAPPER2(char *, strcpy
, char *dest
, const char *src
)
189 /* nb: just because strlen(src) == n doesn't mean (src + n) or (src + n +
190 1) are valid pointers. the allocated object might have size < n.
193 size_t n
= strlen (src
);
194 TRACE ("%s\n", __PRETTY_FUNCTION__
);
195 MF_VALIDATE_EXTENT(src
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "strcpy src");
196 MF_VALIDATE_EXTENT(dest
, CLAMPADD(n
, 1), __MF_CHECK_WRITE
, "strcpy dest");
197 return strcpy (dest
, src
);
202 WRAPPER2(char *, strncpy
, char *dest
, const char *src
, size_t n
)
204 size_t len
= strnlen (src
, n
);
205 TRACE ("%s\n", __PRETTY_FUNCTION__
);
206 MF_VALIDATE_EXTENT(src
, len
, __MF_CHECK_READ
, "strncpy src");
207 MF_VALIDATE_EXTENT(dest
, len
, __MF_CHECK_WRITE
, "strncpy dest"); /* nb: strNcpy */
208 return strncpy (dest
, src
, n
);
213 WRAPPER2(char *, strcat
, char *dest
, const char *src
)
217 TRACE ("%s\n", __PRETTY_FUNCTION__
);
218 dest_sz
= strlen (dest
);
219 src_sz
= strlen (src
);
220 MF_VALIDATE_EXTENT(src
, CLAMPADD(src_sz
, 1), __MF_CHECK_READ
, "strcat src");
221 MF_VALIDATE_EXTENT(dest
, CLAMPADD(dest_sz
, CLAMPADD(src_sz
, 1)),
222 __MF_CHECK_WRITE
, "strcat dest");
223 return strcat (dest
, src
);
227 WRAPPER2(char *, strncat
, char *dest
, const char *src
, size_t n
)
230 /* nb: validating the extents (s,n) might be a mistake for two reasons.
232 (1) the string s might be shorter than n chars, and n is just a
233 poor choice by the programmer. this is not a "true" error in the
234 sense that the call to strncat would still be ok.
236 (2) we could try to compensate for case (1) by calling strlen(s) and
237 using that as a bound for the extent to verify, but strlen might fall off
238 the end of a non-terminated string, leading to a false positive.
240 so we will call strnlen(s,n) and use that as a bound.
242 if strnlen returns a length beyond the end of the registered extent
243 associated with s, there is an error: the programmer's estimate for n is
244 too large _AND_ the string s is unterminated, in which case they'd be
245 about to touch memory they don't own while calling strncat.
247 this same logic applies to further uses of strnlen later down in this
252 TRACE ("%s\n", __PRETTY_FUNCTION__
);
253 src_sz
= strnlen (src
, n
);
254 dest_sz
= strnlen (dest
, n
);
255 MF_VALIDATE_EXTENT(src
, src_sz
, __MF_CHECK_READ
, "strncat src");
256 MF_VALIDATE_EXTENT(dest
, (CLAMPADD(dest_sz
, CLAMPADD(src_sz
, 1))),
257 __MF_CHECK_WRITE
, "strncat dest");
258 return strncat (dest
, src
, n
);
262 WRAPPER2(int, strcmp
, const char *s1
, const char *s2
)
266 TRACE ("%s\n", __PRETTY_FUNCTION__
);
269 MF_VALIDATE_EXTENT(s1
, CLAMPADD(s1_sz
, 1), __MF_CHECK_READ
, "strcmp 1st arg");
270 MF_VALIDATE_EXTENT(s2
, CLAMPADD(s2_sz
, 1), __MF_CHECK_WRITE
, "strcmp 2nd arg");
271 return strcmp (s1
, s2
);
275 WRAPPER2(int, strcasecmp
, const char *s1
, const char *s2
)
279 TRACE ("%s\n", __PRETTY_FUNCTION__
);
282 MF_VALIDATE_EXTENT(s1
, CLAMPADD(s1_sz
, 1), __MF_CHECK_READ
, "strcasecmp 1st arg");
283 MF_VALIDATE_EXTENT(s2
, CLAMPADD(s2_sz
, 1), __MF_CHECK_READ
, "strcasecmp 2nd arg");
284 return strcasecmp (s1
, s2
);
288 WRAPPER2(int, strncmp
, const char *s1
, const char *s2
, size_t n
)
292 TRACE ("%s\n", __PRETTY_FUNCTION__
);
293 s1_sz
= strnlen (s1
, n
);
294 s2_sz
= strnlen (s2
, n
);
295 MF_VALIDATE_EXTENT(s1
, s1_sz
, __MF_CHECK_READ
, "strncmp 1st arg");
296 MF_VALIDATE_EXTENT(s2
, s2_sz
, __MF_CHECK_READ
, "strncmp 2nd arg");
297 return strncmp (s1
, s2
, n
);
301 WRAPPER2(int, strncasecmp
, const char *s1
, const char *s2
, size_t n
)
305 TRACE ("%s\n", __PRETTY_FUNCTION__
);
306 s1_sz
= strnlen (s1
, n
);
307 s2_sz
= strnlen (s2
, n
);
308 MF_VALIDATE_EXTENT(s1
, s1_sz
, __MF_CHECK_READ
, "strncasecmp 1st arg");
309 MF_VALIDATE_EXTENT(s2
, s2_sz
, __MF_CHECK_READ
, "strncasecmp 2nd arg");
310 return strncasecmp (s1
, s2
, n
);
314 WRAPPER2(char *, strdup
, const char *s
)
316 DECLARE(void *, malloc
, size_t sz
);
318 size_t n
= strlen (s
);
319 TRACE ("%s\n", __PRETTY_FUNCTION__
);
320 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
,1), __MF_CHECK_READ
, "strdup region");
321 result
= (char *)CALL_REAL(malloc
,
322 CLAMPADD(CLAMPADD(n
,1),
323 CLAMPADD(__mf_opts
.crumple_zone
,
324 __mf_opts
.crumple_zone
)));
326 if (UNLIKELY(! result
)) return result
;
328 result
+= __mf_opts
.crumple_zone
;
329 memcpy (result
, s
, n
);
332 __mf_register (result
, CLAMPADD(n
,1), __MF_TYPE_HEAP_I
, "strdup region");
337 WRAPPER2(char *, strndup
, const char *s
, size_t n
)
339 DECLARE(void *, malloc
, size_t sz
);
341 size_t sz
= strnlen (s
, n
);
342 TRACE ("%s\n", __PRETTY_FUNCTION__
);
343 MF_VALIDATE_EXTENT(s
, sz
, __MF_CHECK_READ
, "strndup region"); /* nb: strNdup */
345 /* note: strndup still adds a \0, even with the N limit! */
346 result
= (char *)CALL_REAL(malloc
,
347 CLAMPADD(CLAMPADD(n
,1),
348 CLAMPADD(__mf_opts
.crumple_zone
,
349 __mf_opts
.crumple_zone
)));
351 if (UNLIKELY(! result
)) return result
;
353 result
+= __mf_opts
.crumple_zone
;
354 memcpy (result
, s
, n
);
357 __mf_register (result
, CLAMPADD(n
,1), __MF_TYPE_HEAP_I
, "strndup region");
362 WRAPPER2(char *, strchr
, const char *s
, int c
)
365 TRACE ("%s\n", __PRETTY_FUNCTION__
);
367 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
,1), __MF_CHECK_READ
, "strchr region");
368 return strchr (s
, c
);
372 WRAPPER2(char *, strrchr
, const char *s
, int c
)
375 TRACE ("%s\n", __PRETTY_FUNCTION__
);
377 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
,1), __MF_CHECK_READ
, "strrchr region");
378 return strrchr (s
, c
);
382 WRAPPER2(char *, strstr
, const char *haystack
, const char *needle
)
386 TRACE ("%s\n", __PRETTY_FUNCTION__
);
387 haystack_sz
= strlen (haystack
);
388 needle_sz
= strlen (needle
);
389 MF_VALIDATE_EXTENT(haystack
, CLAMPADD(haystack_sz
, 1), __MF_CHECK_READ
, "strstr haystack");
390 MF_VALIDATE_EXTENT(needle
, CLAMPADD(needle_sz
, 1), __MF_CHECK_READ
, "strstr needle");
391 return strstr (haystack
, needle
);
396 WRAPPER2(void *, memmem
,
397 const void *haystack
, size_t haystacklen
,
398 const void *needle
, size_t needlelen
)
400 TRACE ("%s\n", __PRETTY_FUNCTION__
);
401 MF_VALIDATE_EXTENT(haystack
, haystacklen
, __MF_CHECK_READ
, "memmem haystack");
402 MF_VALIDATE_EXTENT(needle
, needlelen
, __MF_CHECK_READ
, "memmem needle");
403 return memmem (haystack
, haystacklen
, needle
, needlelen
);
408 WRAPPER2(size_t, strlen
, const char *s
)
410 size_t result
= strlen (s
);
411 TRACE ("%s\n", __PRETTY_FUNCTION__
);
412 MF_VALIDATE_EXTENT(s
, CLAMPADD(result
, 1), __MF_CHECK_READ
, "strlen region");
417 WRAPPER2(size_t, strnlen
, const char *s
, size_t n
)
419 size_t result
= strnlen (s
, n
);
420 TRACE ("%s\n", __PRETTY_FUNCTION__
);
421 MF_VALIDATE_EXTENT(s
, result
, __MF_CHECK_READ
, "strnlen region");
426 WRAPPER2(void, bzero
, void *s
, size_t n
)
428 TRACE ("%s\n", __PRETTY_FUNCTION__
);
429 MF_VALIDATE_EXTENT(s
, n
, __MF_CHECK_WRITE
, "bzero region");
435 WRAPPER2(void, bcopy
, const void *src
, void *dest
, size_t n
)
437 TRACE ("%s\n", __PRETTY_FUNCTION__
);
438 MF_VALIDATE_EXTENT(src
, n
, __MF_CHECK_READ
, "bcopy src");
439 MF_VALIDATE_EXTENT(dest
, n
, __MF_CHECK_WRITE
, "bcopy dest");
440 bcopy (src
, dest
, n
);
445 WRAPPER2(int, bcmp
, const void *s1
, const void *s2
, size_t n
)
447 TRACE ("%s\n", __PRETTY_FUNCTION__
);
448 MF_VALIDATE_EXTENT(s1
, n
, __MF_CHECK_READ
, "bcmp 1st arg");
449 MF_VALIDATE_EXTENT(s2
, n
, __MF_CHECK_READ
, "bcmp 2nd arg");
450 return bcmp (s1
, s2
, n
);
454 WRAPPER2(char *, index
, const char *s
, int c
)
456 size_t n
= strlen (s
);
457 TRACE ("%s\n", __PRETTY_FUNCTION__
);
458 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "index region");
463 WRAPPER2(char *, rindex
, const char *s
, int c
)
465 size_t n
= strlen (s
);
466 TRACE ("%s\n", __PRETTY_FUNCTION__
);
467 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "rindex region");
468 return rindex (s
, c
);
471 /* XXX: stpcpy, memccpy */
473 /* XXX: *printf,*scanf */
475 /* XXX: setjmp, longjmp */
477 WRAPPER2(char *, asctime
, struct tm
*tm
)
479 static char *reg_result
= NULL
;
481 TRACE ("%s\n", __PRETTY_FUNCTION__
);
482 MF_VALIDATE_EXTENT(tm
, sizeof (struct tm
), __MF_CHECK_READ
, "asctime tm");
483 result
= asctime (tm
);
484 if (reg_result
== NULL
)
486 __mf_register (result
, strlen (result
)+1, __MF_TYPE_STATIC
, "asctime string");
493 WRAPPER2(char *, ctime
, const time_t *timep
)
495 static char *reg_result
= NULL
;
497 TRACE ("%s\n", __PRETTY_FUNCTION__
);
498 MF_VALIDATE_EXTENT(timep
, sizeof (time_t), __MF_CHECK_READ
, "ctime time");
499 result
= ctime (timep
);
500 if (reg_result
== NULL
)
502 /* XXX: what if asctime and ctime return the same static ptr? */
503 __mf_register (result
, strlen (result
)+1, __MF_TYPE_STATIC
, "ctime string");
510 WRAPPER2(struct tm
*, localtime
, const time_t *timep
)
512 static struct tm
*reg_result
= NULL
;
514 TRACE ("%s\n", __PRETTY_FUNCTION__
);
515 MF_VALIDATE_EXTENT(timep
, sizeof (time_t), __MF_CHECK_READ
, "localtime time");
516 result
= localtime (timep
);
517 if (reg_result
== NULL
)
519 __mf_register (result
, sizeof (struct tm
), __MF_TYPE_STATIC
, "localtime tm");
526 WRAPPER2(struct tm
*, gmtime
, const time_t *timep
)
528 static struct tm
*reg_result
= NULL
;
530 TRACE ("%s\n", __PRETTY_FUNCTION__
);
531 MF_VALIDATE_EXTENT(timep
, sizeof (time_t), __MF_CHECK_READ
, "gmtime time");
532 result
= gmtime (timep
);
533 if (reg_result
== NULL
)
535 __mf_register (result
, sizeof (struct tm
), __MF_TYPE_STATIC
, "gmtime tm");
544 /* The following indicate if the result of the corresponding function
545 * should be explicitly un/registered by the wrapper
549 #define MF_REGISTER_fopen __MF_TYPE_STATIC
551 #undef MF_REGISTER_fopen
553 #define MF_RESULT_SIZE_fopen (sizeof (FILE))
555 #undef MF_REGISTER_opendir
556 #define MF_RESULT_SIZE_opendir 0 /* (sizeof (DIR)) */
557 #undef MF_REGISTER_readdir
558 #define MF_REGISTER_gethostbyname __MF_TYPE_STATIC
559 #undef MF_REGISTER_gethostbyname_items
560 #undef MF_REGISTER_dlopen
561 #undef MF_REGISTER_dlerror
562 #undef MF_REGISTER_dlsym
563 #define MF_REGISTER_shmat __MF_TYPE_GUESS
567 WRAPPER2(time_t, time
, time_t *timep
)
569 TRACE ("%s\n", __PRETTY_FUNCTION__
);
571 MF_VALIDATE_EXTENT (timep
, sizeof (*timep
), __MF_CHECK_WRITE
,
577 WRAPPER2(char *, strerror
, int errnum
)
580 static char * last_strerror
= NULL
;
582 TRACE ("%s\n", __PRETTY_FUNCTION__
);
583 p
= strerror (errnum
);
584 if (last_strerror
!= NULL
)
585 __mf_unregister (last_strerror
, 0, __MF_TYPE_STATIC
);
587 __mf_register (p
, strlen (p
) + 1, __MF_TYPE_STATIC
, "strerror result");
594 /* An auxiliary data structure for tracking the hand-made stdio
595 buffers we generate during the fopen/fopen64 hooks. In a civilized
596 language, this would be a simple dynamically sized FILE*->char*
597 lookup table, but this is C and we get to do it by hand. */
602 struct mf_filebuffer
*next
;
604 static struct mf_filebuffer
*mf_filebuffers
= NULL
;
609 /* Reset any buffer automatically provided by libc, since this may
610 have been done via mechanisms that libmudflap couldn't
613 size_t bufsize
= BUFSIZ
;
615 char *buffer
= malloc (bufsize
);
616 struct mf_filebuffer
*b
= malloc (sizeof (struct mf_filebuffer
));
617 assert ((buffer
!= NULL
) && (b
!= NULL
));
619 /* Link it into list. */
622 b
->next
= mf_filebuffers
;
625 /* Determine how the file is supposed to be buffered at the moment. */
626 bufmode
= fileno (f
) == 2 ? _IONBF
: (isatty (fileno (f
)) ? _IOLBF
: _IOFBF
);
628 rc
= setvbuf (f
, buffer
, bufmode
, bufsize
);
635 struct mf_filebuffer
*b
= mf_filebuffers
;
636 struct mf_filebuffer
**pb
= & mf_filebuffers
;
653 WRAPPER2(FILE *, fopen
, const char *path
, const char *mode
)
657 TRACE ("%s\n", __PRETTY_FUNCTION__
);
660 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fopen path");
663 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fopen mode");
665 p
= fopen (path
, mode
);
667 #ifdef MF_REGISTER_fopen
668 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "fopen result");
670 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "fopen result");
679 WRAPPER2(int, setvbuf
, FILE *stream
, char *buf
, int mode
, size_t size
)
682 TRACE ("%s\n", __PRETTY_FUNCTION__
);
684 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
, "setvbuf stream");
689 MF_VALIDATE_EXTENT (buf
, size
, __MF_CHECK_WRITE
, "setvbuf buffer");
691 /* Override the user only if it's an auto-allocated buffer request. Otherwise
692 assume that the supplied buffer is already known to libmudflap. */
693 if ((buf
== NULL
) && ((mode
== _IOFBF
) || (mode
== _IOLBF
)))
696 rc
= setvbuf (stream
, buf
, mode
, size
);
703 WRAPPER2(int, setbuf
, FILE* stream
, char *buf
)
705 return __mfwrap_setvbuf (stream
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
709 #ifdef HAVE_SETBUFFER
710 WRAPPER2(int, setbuffer
, FILE* stream
, char *buf
, size_t sz
)
712 return __mfwrap_setvbuf (stream
, buf
, buf
? _IOFBF
: _IONBF
, sz
);
716 #ifdef HAVE_SETLINEBUF
717 WRAPPER2(int, setlinebuf
, FILE* stream
)
719 return __mfwrap_setvbuf(stream
, NULL
, _IOLBF
, 0);
725 WRAPPER2(FILE *, fdopen
, int fd
, const char *mode
)
729 TRACE ("%s\n", __PRETTY_FUNCTION__
);
732 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fdopen mode");
734 p
= fdopen (fd
, mode
);
736 #ifdef MF_REGISTER_fopen
737 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "fdopen result");
739 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "fdopen result");
748 WRAPPER2(FILE *, freopen
, const char *path
, const char *mode
, FILE *s
)
752 TRACE ("%s\n", __PRETTY_FUNCTION__
);
755 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "freopen path");
757 MF_VALIDATE_EXTENT (s
, (sizeof (*s
)), __MF_CHECK_WRITE
, "freopen stream");
761 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "freopen mode");
763 p
= freopen (path
, mode
, s
);
765 #ifdef MF_REGISTER_fopen
766 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "freopen result");
768 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "freopen result");
778 WRAPPER2(FILE *, fopen64
, const char *path
, const char *mode
)
782 TRACE ("%s\n", __PRETTY_FUNCTION__
);
785 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fopen64 path");
788 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fopen64 mode");
790 p
= fopen64 (path
, mode
);
792 #ifdef MF_REGISTER_fopen
793 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "fopen64 result");
795 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "fopen64 result");
805 #ifdef HAVE_FREOPEN64
806 WRAPPER2(FILE *, freopen64
, const char *path
, const char *mode
, FILE *s
)
810 TRACE ("%s\n", __PRETTY_FUNCTION__
);
813 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "freopen64 path");
815 MF_VALIDATE_EXTENT (s
, (sizeof (*s
)), __MF_CHECK_WRITE
, "freopen64 stream");
819 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "freopen64 mode");
821 p
= freopen (path
, mode
, s
);
823 #ifdef MF_REGISTER_fopen
824 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "freopen64 result");
826 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "freopen64 result");
836 WRAPPER2(int, fclose
, FILE *stream
)
839 TRACE ("%s\n", __PRETTY_FUNCTION__
);
840 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
842 resp
= fclose (stream
);
843 #ifdef MF_REGISTER_fopen
844 __mf_unregister (stream
, sizeof (*stream
), MF_REGISTER_fopen
);
852 WRAPPER2(size_t, fread
, void *ptr
, size_t size
, size_t nmemb
, FILE *stream
)
854 TRACE ("%s\n", __PRETTY_FUNCTION__
);
855 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
857 MF_VALIDATE_EXTENT (ptr
, size
* nmemb
, __MF_CHECK_WRITE
, "fread buffer");
858 return fread (ptr
, size
, nmemb
, stream
);
862 WRAPPER2(size_t, fwrite
, const void *ptr
, size_t size
, size_t nmemb
,
865 TRACE ("%s\n", __PRETTY_FUNCTION__
);
866 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
868 MF_VALIDATE_EXTENT (ptr
, size
* nmemb
, __MF_CHECK_READ
, "fwrite buffer");
869 return fwrite (ptr
, size
, nmemb
, stream
);
873 WRAPPER2(int, fgetc
, FILE *stream
)
875 TRACE ("%s\n", __PRETTY_FUNCTION__
);
876 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
878 return fgetc (stream
);
882 WRAPPER2(char *, fgets
, char *s
, int size
, FILE *stream
)
884 TRACE ("%s\n", __PRETTY_FUNCTION__
);
885 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
887 MF_VALIDATE_EXTENT (s
, size
, __MF_CHECK_WRITE
, "fgets buffer");
888 return fgets (s
, size
, stream
);
892 WRAPPER2(int, getc
, FILE *stream
)
894 TRACE ("%s\n", __PRETTY_FUNCTION__
);
895 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
897 return getc (stream
);
901 WRAPPER2(char *, gets
, char *s
)
903 TRACE ("%s\n", __PRETTY_FUNCTION__
);
904 MF_VALIDATE_EXTENT (s
, 1, __MF_CHECK_WRITE
, "gets buffer");
905 /* Avoid link-time warning... */
906 s
= fgets (s
, INT_MAX
, stdin
);
907 if (NULL
!= s
) { /* better late than never */
908 size_t n
= strlen (s
);
909 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_WRITE
, "gets buffer");
915 WRAPPER2(int, ungetc
, int c
, FILE *stream
)
917 TRACE ("%s\n", __PRETTY_FUNCTION__
);
918 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
920 return ungetc (c
, stream
);
924 WRAPPER2(int, fputc
, int c
, FILE *stream
)
926 TRACE ("%s\n", __PRETTY_FUNCTION__
);
927 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
929 return fputc (c
, stream
);
933 WRAPPER2(int, fputs
, const char *s
, FILE *stream
)
936 TRACE ("%s\n", __PRETTY_FUNCTION__
);
938 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fputs buffer");
939 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
941 return fputs (s
, stream
);
945 WRAPPER2(int, putc
, int c
, FILE *stream
)
947 TRACE ("%s\n", __PRETTY_FUNCTION__
);
948 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
950 return putc (c
, stream
);
954 WRAPPER2(int, puts
, const char *s
)
957 TRACE ("%s\n", __PRETTY_FUNCTION__
);
959 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "puts buffer");
964 WRAPPER2(void, clearerr
, FILE *stream
)
966 TRACE ("%s\n", __PRETTY_FUNCTION__
);
967 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
973 WRAPPER2(int, feof
, FILE *stream
)
975 TRACE ("%s\n", __PRETTY_FUNCTION__
);
976 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
978 return feof (stream
);
982 WRAPPER2(int, ferror
, FILE *stream
)
984 TRACE ("%s\n", __PRETTY_FUNCTION__
);
985 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
987 return ferror (stream
);
991 WRAPPER2(int, fileno
, FILE *stream
)
993 TRACE ("%s\n", __PRETTY_FUNCTION__
);
994 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
996 return fileno (stream
);
1000 WRAPPER2(int, printf
, const char *format
, ...)
1005 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1006 n
= strlen (format
);
1007 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1009 va_start (ap
, format
);
1010 result
= vprintf (format
, ap
);
1016 WRAPPER2(int, fprintf
, FILE *stream
, const char *format
, ...)
1021 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1022 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1024 n
= strlen (format
);
1025 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1027 va_start (ap
, format
);
1028 result
= vfprintf (stream
, format
, ap
);
1034 WRAPPER2(int, sprintf
, char *str
, const char *format
, ...)
1039 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1040 MF_VALIDATE_EXTENT (str
, 1, __MF_CHECK_WRITE
, "sprintf str");
1041 n
= strlen (format
);
1042 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1044 va_start (ap
, format
);
1045 result
= vsprintf (str
, format
, ap
);
1048 MF_VALIDATE_EXTENT (str
, CLAMPADD(n
, 1), __MF_CHECK_WRITE
, "sprintf str");
1053 WRAPPER2(int, snprintf
, char *str
, size_t size
, const char *format
, ...)
1058 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1059 MF_VALIDATE_EXTENT (str
, size
, __MF_CHECK_WRITE
, "snprintf str");
1060 n
= strlen (format
);
1061 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1063 va_start (ap
, format
);
1064 result
= vsnprintf (str
, size
, format
, ap
);
1070 WRAPPER2(int, vprintf
, const char *format
, va_list ap
)
1073 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1074 n
= strlen (format
);
1075 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1077 return vprintf (format
, ap
);
1081 WRAPPER2(int, vfprintf
, FILE *stream
, const char *format
, va_list ap
)
1084 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1085 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1087 n
= strlen (format
);
1088 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1090 return vfprintf (stream
, format
, ap
);
1094 WRAPPER2(int, vsprintf
, char *str
, const char *format
, va_list ap
)
1098 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1099 MF_VALIDATE_EXTENT (str
, 1, __MF_CHECK_WRITE
, "vsprintf str");
1100 n
= strlen (format
);
1101 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1103 result
= vsprintf (str
, format
, ap
);
1105 MF_VALIDATE_EXTENT (str
, CLAMPADD(n
, 1), __MF_CHECK_WRITE
, "vsprintf str");
1110 WRAPPER2(int, vsnprintf
, char *str
, size_t size
, const char *format
,
1114 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1115 MF_VALIDATE_EXTENT (str
, size
, __MF_CHECK_WRITE
, "vsnprintf str");
1116 n
= strlen (format
);
1117 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1118 "vsnprintf format");
1119 return vsnprintf (str
, size
, format
, ap
);
1123 WRAPPER2(int , access
, const char *path
, int mode
)
1126 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1128 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "access path");
1129 return access (path
, mode
);
1133 WRAPPER2(int , remove
, const char *path
)
1136 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1138 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "remove path");
1139 return remove (path
);
1143 WRAPPER2(int, fflush
, FILE *stream
)
1145 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1147 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1149 return fflush (stream
);
1153 WRAPPER2(int, fseek
, FILE *stream
, long offset
, int whence
)
1155 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1156 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1158 return fseek (stream
, offset
, whence
);
1162 #ifdef HAVE_FSEEKO64
1163 WRAPPER2(int, fseeko64
, FILE *stream
, off64_t offset
, int whence
)
1165 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1166 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1168 return fseeko64 (stream
, offset
, whence
);
1173 WRAPPER2(long, ftell
, FILE *stream
)
1175 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1176 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1178 return ftell (stream
);
1182 #ifdef HAVE_FTELLO64
1183 WRAPPER2(off64_t
, ftello64
, FILE *stream
)
1185 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1186 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1188 return ftello64 (stream
);
1193 WRAPPER2(void, rewind
, FILE *stream
)
1195 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1196 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1202 WRAPPER2(int, fgetpos
, FILE *stream
, fpos_t *pos
)
1204 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1205 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1207 MF_VALIDATE_EXTENT (pos
, sizeof (*pos
), __MF_CHECK_WRITE
, "fgetpos pos");
1208 return fgetpos (stream
, pos
);
1212 WRAPPER2(int, fsetpos
, FILE *stream
, fpos_t *pos
)
1214 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1215 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1217 MF_VALIDATE_EXTENT (pos
, sizeof (*pos
), __MF_CHECK_READ
, "fsetpos pos");
1218 return fsetpos (stream
, pos
);
1222 WRAPPER2(int , stat
, const char *path
, struct stat
*buf
)
1225 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1227 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "stat path");
1228 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
, "stat buf");
1229 return stat (path
, buf
);
1234 WRAPPER2(int , stat64
, const char *path
, struct stat64
*buf
)
1237 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1239 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "stat64 path");
1240 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
, "stat64 buf");
1241 return stat64 (path
, buf
);
1246 WRAPPER2(int , fstat
, int filedes
, struct stat
*buf
)
1248 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1249 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
, "fstat buf");
1250 return fstat (filedes
, buf
);
1254 WRAPPER2(int , lstat
, const char *path
, struct stat
*buf
)
1257 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1259 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "lstat path");
1260 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
, "lstat buf");
1261 return lstat (path
, buf
);
1265 WRAPPER2(int , mkfifo
, const char *path
, mode_t mode
)
1268 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1270 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "mkfifo path");
1271 return mkfifo (path
, mode
);
1275 #ifdef HAVE_DIRENT_H
1276 WRAPPER2(DIR *, opendir
, const char *path
)
1280 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1282 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "opendir path");
1286 #ifdef MF_REGISTER_opendir
1287 __mf_register (p
, MF_RESULT_SIZE_opendir
, MF_REGISTER_opendir
,
1290 MF_VALIDATE_EXTENT (p
, MF_RESULT_SIZE_opendir
, __MF_CHECK_WRITE
,
1297 WRAPPER2(int, closedir
, DIR *dir
)
1299 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1300 MF_VALIDATE_EXTENT (dir
, 0, __MF_CHECK_WRITE
, "closedir dir");
1301 #ifdef MF_REGISTER_opendir
1302 __mf_unregister (dir
, MF_RESULT_SIZE_opendir
, MF_REGISTER_opendir
);
1304 return closedir (dir
);
1308 WRAPPER2(struct dirent
*, readdir
, DIR *dir
)
1311 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1312 MF_VALIDATE_EXTENT (dir
, 0, __MF_CHECK_READ
, "readdir dir");
1315 #ifdef MF_REGISTER_readdir
1316 __mf_register (p
, sizeof (*p
), MF_REGISTER_readdir
, "readdir result");
1318 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "readdir result");
1325 #ifdef HAVE_SYS_SOCKET_H
1327 WRAPPER2(int, recv
, int s
, void *buf
, size_t len
, int flags
)
1329 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1330 MF_VALIDATE_EXTENT (buf
, len
, __MF_CHECK_WRITE
, "recv buf");
1331 return recv (s
, buf
, len
, flags
);
1335 WRAPPER2(int, recvfrom
, int s
, void *buf
, size_t len
, int flags
,
1336 struct sockaddr
*from
, socklen_t
*fromlen
)
1338 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1339 MF_VALIDATE_EXTENT (buf
, len
, __MF_CHECK_WRITE
, "recvfrom buf");
1340 MF_VALIDATE_EXTENT (from
, (size_t)*fromlen
, __MF_CHECK_WRITE
,
1342 return recvfrom (s
, buf
, len
, flags
, from
, fromlen
);
1346 WRAPPER2(int, recvmsg
, int s
, struct msghdr
*msg
, int flags
)
1348 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1349 MF_VALIDATE_EXTENT (msg
, sizeof (*msg
), __MF_CHECK_WRITE
, "recvmsg msg");
1350 return recvmsg (s
, msg
, flags
);
1354 WRAPPER2(int, send
, int s
, const void *msg
, size_t len
, int flags
)
1356 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1357 MF_VALIDATE_EXTENT (msg
, len
, __MF_CHECK_READ
, "send msg");
1358 return send (s
, msg
, len
, flags
);
1362 WRAPPER2(int, sendto
, int s
, const void *msg
, size_t len
, int flags
,
1363 const struct sockaddr
*to
, socklen_t tolen
)
1365 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1366 MF_VALIDATE_EXTENT (msg
, len
, __MF_CHECK_READ
, "sendto msg");
1367 MF_VALIDATE_EXTENT (to
, (size_t)tolen
, __MF_CHECK_WRITE
, "sendto to");
1368 return sendto (s
, msg
, len
, flags
, to
, tolen
);
1372 WRAPPER2(int, sendmsg
, int s
, const void *msg
, int flags
)
1374 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1375 MF_VALIDATE_EXTENT (msg
, sizeof (*msg
), __MF_CHECK_READ
, "sendmsg msg");
1376 return sendmsg (s
, msg
, flags
);
1380 WRAPPER2(int, setsockopt
, int s
, int level
, int optname
, const void *optval
,
1383 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1384 MF_VALIDATE_EXTENT (optval
, (size_t)optlen
, __MF_CHECK_READ
,
1385 "setsockopt optval");
1386 return setsockopt (s
, level
, optname
, optval
, optlen
);
1390 WRAPPER2(int, getsockopt
, int s
, int level
, int optname
, void *optval
,
1393 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1394 MF_VALIDATE_EXTENT (optval
, (size_t)*optlen
, __MF_CHECK_WRITE
,
1395 "getsockopt optval");
1396 return getsockopt (s
, level
, optname
, optval
, optlen
);
1400 WRAPPER2(int, accept
, int s
, struct sockaddr
*addr
, socklen_t
*addrlen
)
1402 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1404 MF_VALIDATE_EXTENT (addr
, (size_t)*addrlen
, __MF_CHECK_WRITE
, "accept addr");
1405 return accept (s
, addr
, addrlen
);
1409 WRAPPER2(int, bind
, int sockfd
, struct sockaddr
*addr
, socklen_t addrlen
)
1411 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1412 MF_VALIDATE_EXTENT (addr
, (size_t)addrlen
, __MF_CHECK_WRITE
, "bind addr");
1413 return bind (sockfd
, addr
, addrlen
);
1417 WRAPPER2(int, connect
, int sockfd
, const struct sockaddr
*addr
,
1420 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1421 MF_VALIDATE_EXTENT (addr
, (size_t)addrlen
, __MF_CHECK_READ
,
1423 return connect (sockfd
, addr
, addrlen
);
1426 #endif /* HAVE_SYS_SOCKET_H */
1429 WRAPPER2(int, gethostname
, char *name
, size_t len
)
1431 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1432 MF_VALIDATE_EXTENT (name
, len
, __MF_CHECK_WRITE
, "gethostname name");
1433 return gethostname (name
, len
);
1437 #ifdef HAVE_SETHOSTNAME
1438 WRAPPER2(int, sethostname
, const char *name
, size_t len
)
1440 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1441 MF_VALIDATE_EXTENT (name
, len
, __MF_CHECK_READ
, "sethostname name");
1442 return sethostname (name
, len
);
1449 WRAPPER2(struct hostent
*, gethostbyname
, const char *name
)
1456 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1458 MF_VALIDATE_EXTENT (name
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1459 "gethostbyname name");
1460 p
= gethostbyname (name
);
1462 #ifdef MF_REGISTER_gethostbyname
1463 __mf_register (p
, sizeof (*p
), MF_REGISTER_gethostbyname
,
1464 "gethostbyname result");
1466 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
,
1467 "gethostbyname result");
1468 if (NULL
!= (s
= p
->h_name
)) {
1471 #ifdef MF_REGISTER_gethostbyname_items
1472 __mf_register (s
, n
, MF_REGISTER_gethostbyname_items
,
1473 "gethostbyname result->h_name");
1475 MF_VALIDATE_EXTENT (s
, n
, __MF_CHECK_WRITE
,
1476 "gethostbyname result->h_name");
1479 if (NULL
!= (ss
= p
->h_aliases
)) {
1480 for (nreg
= 1;; ++nreg
) {
1486 #ifdef MF_REGISTER_gethostbyname_items
1487 __mf_register (s
, n
, MF_REGISTER_gethostbyname_items
,
1488 "gethostbyname result->h_aliases[]");
1490 MF_VALIDATE_EXTENT (s
, n
, __MF_CHECK_WRITE
,
1491 "gethostbyname result->h_aliases[]");
1493 nreg
*= sizeof (*p
->h_aliases
);
1494 #ifdef MF_REGISTER_gethostbyname_items
1495 __mf_register (p
->h_aliases
, nreg
, MF_REGISTER_gethostbyname_items
,
1496 "gethostbyname result->h_aliases");
1498 MF_VALIDATE_EXTENT (p
->h_aliases
, nreg
, __MF_CHECK_WRITE
,
1499 "gethostbyname result->h_aliases");
1502 if (NULL
!= (ss
= p
->h_addr_list
)) {
1503 for (nreg
= 1;; ++nreg
) {
1507 #ifdef MF_REGISTER_gethostbyname_items
1508 __mf_register (s
, p
->h_length
, MF_REGISTER_gethostbyname_items
,
1509 "gethostbyname result->h_addr_list[]");
1511 MF_VALIDATE_EXTENT (s
, p
->h_length
, __MF_CHECK_WRITE
,
1512 "gethostbyname result->h_addr_list[]");
1514 nreg
*= sizeof (*p
->h_addr_list
);
1515 #ifdef MF_REGISTER_gethostbyname_items
1516 __mf_register (p
->h_addr_list
, nreg
, MF_REGISTER_gethostbyname_items
,
1517 "gethostbyname result->h_addr_list");
1519 MF_VALIDATE_EXTENT (p
->h_addr_list
, nreg
, __MF_CHECK_WRITE
,
1520 "gethostbyname result->h_addr_list");
1526 #endif /* HAVE_NETDB_H */
1529 #ifdef HAVE_SYS_WAIT_H
1531 WRAPPER2(pid_t
, wait
, int *status
)
1533 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1535 MF_VALIDATE_EXTENT (status
, sizeof (*status
), __MF_CHECK_WRITE
,
1537 return wait (status
);
1541 WRAPPER2(pid_t
, waitpid
, pid_t pid
, int *status
, int options
)
1543 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1545 MF_VALIDATE_EXTENT (status
, sizeof (*status
), __MF_CHECK_WRITE
,
1547 return waitpid (pid
, status
, options
);
1550 #endif /* HAVE_SYS_WAIT_H */
1553 WRAPPER2(FILE *, popen
, const char *command
, const char *mode
)
1557 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1559 n
= strlen (command
);
1560 MF_VALIDATE_EXTENT (command
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "popen path");
1563 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "popen mode");
1565 p
= popen (command
, mode
);
1567 #ifdef MF_REGISTER_fopen
1568 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "popen result");
1570 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "popen result");
1576 WRAPPER2(int, pclose
, FILE *stream
)
1579 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1580 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1582 resp
= pclose (stream
);
1583 #ifdef MF_REGISTER_fopen
1584 __mf_unregister (stream
, sizeof (*stream
), MF_REGISTER_fopen
);
1590 WRAPPER2(int, execve
, const char *path
, char *const argv
[],
1596 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1599 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execve path");
1602 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_READ
, "execve *argv");
1607 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execve **argv");
1611 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_READ
, "execve *envp");
1616 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execve **envp");
1618 return execve (path
, argv
, envp
);
1622 WRAPPER2(int, execv
, const char *path
, char *const argv
[])
1627 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1630 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execv path");
1633 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_READ
, "execv *argv");
1638 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execv **argv");
1640 return execv (path
, argv
);
1644 WRAPPER2(int, execvp
, const char *path
, char *const argv
[])
1649 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1652 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execvp path");
1655 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_READ
, "execvp *argv");
1660 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execvp **argv");
1662 return execvp (path
, argv
);
1666 WRAPPER2(int, system
, const char *string
)
1669 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1670 n
= strlen (string
);
1671 MF_VALIDATE_EXTENT (string
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1673 return system (string
);
1677 WRAPPER2(void *, dlopen
, const char *path
, int flags
)
1681 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1683 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "dlopen path");
1684 p
= dlopen (path
, flags
);
1686 #ifdef MF_REGISTER_dlopen
1687 __mf_register (p
, 0, MF_REGISTER_dlopen
, "dlopen result");
1689 MF_VALIDATE_EXTENT (p
, 0, __MF_CHECK_WRITE
, "dlopen result");
1695 WRAPPER2(int, dlclose
, void *handle
)
1698 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1699 MF_VALIDATE_EXTENT (handle
, 0, __MF_CHECK_READ
, "dlclose handle");
1700 resp
= dlclose (handle
);
1701 #ifdef MF_REGISTER_dlopen
1702 __mf_unregister (handle
, 0, MF_REGISTER_dlopen
);
1708 WRAPPER2(char *, dlerror
)
1711 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1717 #ifdef MF_REGISTER_dlerror
1718 __mf_register (p
, n
, MF_REGISTER_dlerror
, "dlerror result");
1720 MF_VALIDATE_EXTENT (p
, n
, __MF_CHECK_WRITE
, "dlerror result");
1726 WRAPPER2(void *, dlsym
, void *handle
, char *symbol
)
1730 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1731 MF_VALIDATE_EXTENT (handle
, 0, __MF_CHECK_READ
, "dlsym handle");
1732 n
= strlen (symbol
);
1733 MF_VALIDATE_EXTENT (symbol
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "dlsym symbol");
1734 p
= dlsym (handle
, symbol
);
1736 #ifdef MF_REGISTER_dlsym
1737 __mf_register (p
, 0, MF_REGISTER_dlsym
, "dlsym result");
1739 MF_VALIDATE_EXTENT (p
, 0, __MF_CHECK_WRITE
, "dlsym result");
1745 #if defined (HAVE_SYS_IPC_H) && defined (HAVE_SYS_SEM_H) && defined (HAVE_SYS_SHM_H)
1747 WRAPPER2(int, semop
, int semid
, struct sembuf
*sops
, unsigned nsops
)
1749 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1750 MF_VALIDATE_EXTENT (sops
, sizeof (*sops
) * nsops
, __MF_CHECK_READ
,
1752 return semop (semid
, sops
, nsops
);
1756 #ifndef HAVE_UNION_SEMUN
1758 int val
; /* value for SETVAL */
1759 struct semid_ds
*buf
; /* buffer for IPC_STAT, IPC_SET */
1760 unsigned short int *array
; /* array for GETALL, SETALL */
1761 struct seminfo
*__buf
; /* buffer for IPC_INFO */
1764 WRAPPER2(int, semctl
, int semid
, int semnum
, int cmd
, union semun arg
)
1766 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1769 MF_VALIDATE_EXTENT (arg
.buf
, sizeof (*arg
.buf
), __MF_CHECK_WRITE
,
1773 MF_VALIDATE_EXTENT (arg
.buf
, sizeof (*arg
.buf
), __MF_CHECK_READ
,
1777 MF_VALIDATE_EXTENT (arg
.array
, sizeof (*arg
.array
), __MF_CHECK_WRITE
,
1780 MF_VALIDATE_EXTENT (arg
.array
, sizeof (*arg
.array
), __MF_CHECK_READ
,
1784 /* FreeBSD 5.1 And Cygwin headers include IPC_INFO but not the __buf field. */
1785 #if !defined(__FreeBSD__) && !defined(__CYGWIN__)
1787 MF_VALIDATE_EXTENT (arg
.__buf
, sizeof (*arg
.__buf
), __MF_CHECK_WRITE
,
1795 return semctl (semid
, semnum
, cmd
, arg
);
1799 WRAPPER2(int, shmctl
, int shmid
, int cmd
, struct shmid_ds
*buf
)
1801 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1804 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_WRITE
,
1808 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
,
1814 return shmctl (shmid
, cmd
, buf
);
1818 WRAPPER2(void *, shmat
, int shmid
, const void *shmaddr
, int shmflg
)
1821 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1822 p
= shmat (shmid
, shmaddr
, shmflg
);
1823 #ifdef MF_REGISTER_shmat
1825 struct shmid_ds buf
;
1826 __mf_register (p
, shmctl (shmid
, IPC_STAT
, &buf
) ? 0 : buf
.shm_segsz
,
1827 MF_REGISTER_shmat
, "shmat result");
1834 WRAPPER2(int, shmdt
, const void *shmaddr
)
1837 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1838 resp
= shmdt (shmaddr
);
1839 #ifdef MF_REGISTER_shmat
1840 __mf_unregister ((void *)shmaddr
, 0, MF_REGISTER_shmat
);
1846 #endif /* HAVE_SYS_IPC/SEM/SHM_H */
1850 /* ctype stuff. This is host-specific by necessity, as the arrays
1851 that is used by most is*()/to*() macros are implementation-defined. */
1854 #ifdef HAVE___CTYPE_B_LOC
1855 WRAPPER2(unsigned short **, __ctype_b_loc
, void)
1857 static unsigned short * last_buf
= (void *) 0;
1858 static unsigned short ** last_ptr
= (void *) 0;
1859 unsigned short ** ptr
= (unsigned short **) __ctype_b_loc ();
1860 unsigned short * buf
= * ptr
;
1861 if (ptr
!= last_ptr
)
1863 /* XXX: unregister last_ptr? */
1865 __mf_register (last_ptr
, sizeof(last_ptr
), __MF_TYPE_STATIC
, "ctype_b_loc **");
1867 if (buf
!= last_buf
)
1870 __mf_register ((void *) (last_buf
- 128), 384 * sizeof(unsigned short), __MF_TYPE_STATIC
,
1877 #ifdef HAVE___CTYPE_TOUPPER_LOC
1878 WRAPPER2(int **, __ctype_toupper_loc
, void)
1880 static int * last_buf
= (void *) 0;
1881 static int ** last_ptr
= (void *) 0;
1882 int ** ptr
= (int **) __ctype_toupper_loc ();
1884 if (ptr
!= last_ptr
)
1886 /* XXX: unregister last_ptr? */
1888 __mf_register (last_ptr
, sizeof(last_ptr
), __MF_TYPE_STATIC
, "ctype_toupper_loc **");
1890 if (buf
!= last_buf
)
1893 __mf_register ((void *) (last_buf
- 128), 384 * sizeof(int), __MF_TYPE_STATIC
,
1894 "ctype_toupper_loc []");
1900 #ifdef HAVE___CTYPE_TOLOWER_LOC
1901 WRAPPER2(int **, __ctype_tolower_loc
, void)
1903 static int * last_buf
= (void *) 0;
1904 static int ** last_ptr
= (void *) 0;
1905 int ** ptr
= (int **) __ctype_tolower_loc ();
1907 if (ptr
!= last_ptr
)
1909 /* XXX: unregister last_ptr? */
1911 __mf_register (last_ptr
, sizeof(last_ptr
), __MF_TYPE_STATIC
, "ctype_tolower_loc **");
1913 if (buf
!= last_buf
)
1916 __mf_register ((void *) (last_buf
- 128), 384 * sizeof(int), __MF_TYPE_STATIC
,
1917 "ctype_tolower_loc []");
1924 /* passwd/group related functions. These register every (static) pointer value returned,
1925 and rely on libmudflap's quiet toleration of duplicate static registrations. */
1927 #ifdef HAVE_GETLOGIN
1928 WRAPPER2(char *, getlogin
, void)
1930 char *buf
= getlogin ();
1932 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1933 "getlogin() return");
1940 WRAPPER2(char *, cuserid
, char * buf
)
1944 MF_VALIDATE_EXTENT(buf
, L_cuserid
, __MF_CHECK_WRITE
,
1945 "cuserid destination");
1946 return cuserid (buf
);
1948 buf
= cuserid (NULL
);
1950 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1951 "getcuserid() return");
1957 #ifdef HAVE_GETPWNAM
1958 WRAPPER2(struct passwd
*, getpwnam
, const char *name
)
1961 MF_VALIDATE_EXTENT(name
, strlen(name
)+1, __MF_CHECK_READ
,
1963 buf
= getpwnam (name
);
1965 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1972 #ifdef HAVE_GETPWUID
1973 WRAPPER2(struct passwd
*, getpwuid
, uid_t uid
)
1976 buf
= getpwuid (uid
);
1978 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1985 #ifdef HAVE_GETGRNAM
1986 WRAPPER2(struct group
*, getgrnam
, const char *name
)
1989 MF_VALIDATE_EXTENT(name
, strlen(name
)+1, __MF_CHECK_READ
,
1991 buf
= getgrnam (name
);
1993 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2000 #ifdef HAVE_GETGRGID
2001 WRAPPER2(struct group
*, getgrgid
, uid_t uid
)
2004 buf
= getgrgid (uid
);
2006 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2013 #ifdef HAVE_GETSERVENT
2014 WRAPPER2(struct servent
*, getservent
, void)
2016 struct servent
*buf
;
2017 buf
= getservent ();
2019 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2020 "getserv*() return");
2026 #ifdef HAVE_GETSERVBYNAME
2027 WRAPPER2(struct servent
*, getservbyname
, const char *name
, const char *proto
)
2029 struct servent
*buf
;
2030 MF_VALIDATE_EXTENT(name
, strlen(name
)+1, __MF_CHECK_READ
,
2031 "getservbyname name");
2032 MF_VALIDATE_EXTENT(proto
, strlen(proto
)+1, __MF_CHECK_READ
,
2033 "getservbyname proto");
2034 buf
= getservbyname (name
, proto
);
2036 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2037 "getserv*() return");
2043 #ifdef HAVE_GETSERVBYPORT
2044 WRAPPER2(struct servent
*, getservbyport
, int port
, const char *proto
)
2046 struct servent
*buf
;
2047 MF_VALIDATE_EXTENT(proto
, strlen(proto
)+1, __MF_CHECK_READ
,
2048 "getservbyport proto");
2049 buf
= getservbyport (port
, proto
);
2051 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2052 "getserv*() return");
2058 #ifdef HAVE_GAI_STRERROR
2059 WRAPPER2(const char *, gai_strerror
, int errcode
)
2062 buf
= gai_strerror (errcode
);
2064 __mf_register ((void *) buf
, strlen(buf
)+1, __MF_TYPE_STATIC
,
2065 "gai_strerror() return");
2071 #ifdef HAVE_GETMNTENT
2072 WRAPPER2(struct mntent
*, getmntent
, FILE *filep
)
2075 static struct mntent
*last
= NULL
;
2077 MF_VALIDATE_EXTENT (filep
, sizeof (*filep
), __MF_CHECK_WRITE
,
2078 "getmntent stream");
2079 #define UR(field) __mf_unregister(last->field, strlen (last->field)+1, __MF_TYPE_STATIC)
2086 __mf_unregister (last
, sizeof (*last
), __MF_TYPE_STATIC
);
2090 m
= getmntent (filep
);
2093 #define R(field) __mf_register(last->field, strlen (last->field)+1, __MF_TYPE_STATIC, "mntent " #field)
2100 __mf_register (last
, sizeof (*last
), __MF_TYPE_STATIC
, "getmntent result");
2109 #ifdef HAVE_INET_NTOA
2110 WRAPPER2(char *, inet_ntoa
, struct in_addr in
)
2112 static char *last_buf
= NULL
;
2115 __mf_unregister (last_buf
, strlen (last_buf
)+1, __MF_TYPE_STATIC
);
2116 buf
= inet_ntoa (in
);
2119 __mf_register (last_buf
, strlen (last_buf
)+1, __MF_TYPE_STATIC
, "inet_ntoa result");
2125 #ifdef HAVE_GETPROTOENT
2126 WRAPPER2(struct protoent
*, getprotoent
, void)
2128 struct protoent
*buf
;
2129 buf
= getprotoent ();
2131 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
, "getproto*() return");
2137 #ifdef HAVE_GETPROTOBYNAME
2138 WRAPPER2(struct protoent
*, getprotobyname
, const char *name
)
2140 struct protoent
*buf
;
2141 MF_VALIDATE_EXTENT(name
, strlen(name
)+1, __MF_CHECK_READ
,
2142 "getprotobyname name");
2143 buf
= getprotobyname (name
);
2145 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2146 "getproto*() return");
2152 #ifdef HAVE_GETPROTOBYNUMBER
2153 WRAPPER2(struct protoent
*, getprotobynumber
, int port
)
2155 struct protoent
*buf
;
2156 buf
= getprotobynumber (port
);
2158 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2159 "getproto*() return");