1 /* Mudflap: narrow-pointer bounds-checking by tree rewriting.
2 Copyright (C) 2002, 2003, 2004, 2009, 2011 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 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
29 #ifndef HAVE_SOCKLEN_T
33 /* These attempt to coax various unix flavours to declare all our
34 needed tidbits in the system headers. */
35 #if !defined(__FreeBSD__) && !defined(__APPLE__)
37 #endif /* Some BSDs break <sys/socket.h> if this is defined. */
41 #define __EXTENSIONS__
43 #define _LARGE_FILE_API
44 #define _LARGEFILE64_SOURCE
45 #define _XOPEN_SOURCE_EXTENDED 1
53 #include <sys/types.h>
66 #ifdef HAVE_SYS_SOCKET_H
67 #include <sys/socket.h>
72 #ifdef HAVE_SYS_WAIT_H
93 #ifdef HAVE_SYS_MNTTAB_H
94 #include <sys/mnttab.h>
96 #ifdef HAVE_SYS_SOCKET_H
97 #include <sys/socket.h>
99 #ifdef HAVE_NETINET_IN_H
100 #include <netinet/in.h>
102 #ifdef HAVE_ARPA_INET_H
103 #include <arpa/inet.h>
106 #include "mf-runtime.h"
110 #error "Do not compile this file with -fmudflap!"
114 /* A bunch of independent stdlib/unistd hook functions, all
115 intercepted by mf-runtime.h macros. */
118 static inline size_t (strnlen
) (const char* str
, size_t n
)
122 for (s
= str
; n
&& *s
; ++s
, --n
)
131 WRAPPER2(void *, memcpy
, void *dest
, const void *src
, size_t n
)
133 TRACE ("%s\n", __PRETTY_FUNCTION__
);
134 MF_VALIDATE_EXTENT(src
, n
, __MF_CHECK_READ
, "memcpy source");
135 MF_VALIDATE_EXTENT(dest
, n
, __MF_CHECK_WRITE
, "memcpy dest");
136 return memcpy (dest
, src
, n
);
140 WRAPPER2(void *, memmove
, void *dest
, const void *src
, size_t n
)
142 TRACE ("%s\n", __PRETTY_FUNCTION__
);
143 MF_VALIDATE_EXTENT(src
, n
, __MF_CHECK_READ
, "memmove src");
144 MF_VALIDATE_EXTENT(dest
, n
, __MF_CHECK_WRITE
, "memmove dest");
145 return memmove (dest
, src
, n
);
149 WRAPPER2(void *, memset
, void *s
, int c
, size_t n
)
151 TRACE ("%s\n", __PRETTY_FUNCTION__
);
152 MF_VALIDATE_EXTENT(s
, n
, __MF_CHECK_WRITE
, "memset dest");
153 return memset (s
, c
, n
);
157 WRAPPER2(int, memcmp
, const void *s1
, const void *s2
, size_t n
)
159 TRACE ("%s\n", __PRETTY_FUNCTION__
);
160 MF_VALIDATE_EXTENT(s1
, n
, __MF_CHECK_READ
, "memcmp 1st arg");
161 MF_VALIDATE_EXTENT(s2
, n
, __MF_CHECK_READ
, "memcmp 2nd arg");
162 return memcmp (s1
, s2
, n
);
166 WRAPPER2(void *, memchr
, const void *s
, int c
, size_t n
)
168 TRACE ("%s\n", __PRETTY_FUNCTION__
);
169 MF_VALIDATE_EXTENT(s
, n
, __MF_CHECK_READ
, "memchr region");
170 return memchr (s
, c
, n
);
175 WRAPPER2(void *, memrchr
, const void *s
, int c
, size_t n
)
177 TRACE ("%s\n", __PRETTY_FUNCTION__
);
178 MF_VALIDATE_EXTENT(s
, n
, __MF_CHECK_READ
, "memrchr region");
179 return memrchr (s
, c
, n
);
184 WRAPPER2(char *, strcpy
, char *dest
, const char *src
)
186 /* nb: just because strlen(src) == n doesn't mean (src + n) or (src + n +
187 1) are valid pointers. the allocated object might have size < n.
190 size_t n
= strlen (src
);
191 TRACE ("%s\n", __PRETTY_FUNCTION__
);
192 MF_VALIDATE_EXTENT(src
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "strcpy src");
193 MF_VALIDATE_EXTENT(dest
, CLAMPADD(n
, 1), __MF_CHECK_WRITE
, "strcpy dest");
194 return strcpy (dest
, src
);
199 WRAPPER2(char *, strncpy
, char *dest
, const char *src
, size_t n
)
201 size_t len
= strnlen (src
, n
);
202 TRACE ("%s\n", __PRETTY_FUNCTION__
);
203 MF_VALIDATE_EXTENT(src
, len
, __MF_CHECK_READ
, "strncpy src");
204 MF_VALIDATE_EXTENT(dest
, len
, __MF_CHECK_WRITE
, "strncpy dest"); /* nb: strNcpy */
205 return strncpy (dest
, src
, n
);
210 WRAPPER2(char *, strcat
, char *dest
, const char *src
)
214 TRACE ("%s\n", __PRETTY_FUNCTION__
);
215 dest_sz
= strlen (dest
);
216 src_sz
= strlen (src
);
217 MF_VALIDATE_EXTENT(src
, CLAMPADD(src_sz
, 1), __MF_CHECK_READ
, "strcat src");
218 MF_VALIDATE_EXTENT(dest
, CLAMPADD(dest_sz
, CLAMPADD(src_sz
, 1)),
219 __MF_CHECK_WRITE
, "strcat dest");
220 return strcat (dest
, src
);
224 WRAPPER2(char *, strncat
, char *dest
, const char *src
, size_t n
)
227 /* nb: validating the extents (s,n) might be a mistake for two reasons.
229 (1) the string s might be shorter than n chars, and n is just a
230 poor choice by the programmer. this is not a "true" error in the
231 sense that the call to strncat would still be ok.
233 (2) we could try to compensate for case (1) by calling strlen(s) and
234 using that as a bound for the extent to verify, but strlen might fall off
235 the end of a non-terminated string, leading to a false positive.
237 so we will call strnlen(s,n) and use that as a bound.
239 if strnlen returns a length beyond the end of the registered extent
240 associated with s, there is an error: the programmer's estimate for n is
241 too large _AND_ the string s is unterminated, in which case they'd be
242 about to touch memory they don't own while calling strncat.
244 this same logic applies to further uses of strnlen later down in this
249 TRACE ("%s\n", __PRETTY_FUNCTION__
);
250 src_sz
= strnlen (src
, n
);
251 dest_sz
= strnlen (dest
, n
);
252 MF_VALIDATE_EXTENT(src
, src_sz
, __MF_CHECK_READ
, "strncat src");
253 MF_VALIDATE_EXTENT(dest
, (CLAMPADD(dest_sz
, CLAMPADD(src_sz
, 1))),
254 __MF_CHECK_WRITE
, "strncat dest");
255 return strncat (dest
, src
, n
);
259 WRAPPER2(int, strcmp
, const char *s1
, const char *s2
)
263 TRACE ("%s\n", __PRETTY_FUNCTION__
);
266 MF_VALIDATE_EXTENT(s1
, CLAMPADD(s1_sz
, 1), __MF_CHECK_READ
, "strcmp 1st arg");
267 MF_VALIDATE_EXTENT(s2
, CLAMPADD(s2_sz
, 1), __MF_CHECK_WRITE
, "strcmp 2nd arg");
268 return strcmp (s1
, s2
);
272 WRAPPER2(int, strcasecmp
, const char *s1
, const char *s2
)
276 TRACE ("%s\n", __PRETTY_FUNCTION__
);
279 MF_VALIDATE_EXTENT(s1
, CLAMPADD(s1_sz
, 1), __MF_CHECK_READ
, "strcasecmp 1st arg");
280 MF_VALIDATE_EXTENT(s2
, CLAMPADD(s2_sz
, 1), __MF_CHECK_READ
, "strcasecmp 2nd arg");
281 return strcasecmp (s1
, s2
);
285 WRAPPER2(int, strncmp
, const char *s1
, const char *s2
, size_t n
)
289 TRACE ("%s\n", __PRETTY_FUNCTION__
);
290 s1_sz
= strnlen (s1
, n
);
291 s2_sz
= strnlen (s2
, n
);
292 MF_VALIDATE_EXTENT(s1
, s1_sz
, __MF_CHECK_READ
, "strncmp 1st arg");
293 MF_VALIDATE_EXTENT(s2
, s2_sz
, __MF_CHECK_READ
, "strncmp 2nd arg");
294 return strncmp (s1
, s2
, n
);
298 WRAPPER2(int, strncasecmp
, const char *s1
, const char *s2
, size_t n
)
302 TRACE ("%s\n", __PRETTY_FUNCTION__
);
303 s1_sz
= strnlen (s1
, n
);
304 s2_sz
= strnlen (s2
, n
);
305 MF_VALIDATE_EXTENT(s1
, s1_sz
, __MF_CHECK_READ
, "strncasecmp 1st arg");
306 MF_VALIDATE_EXTENT(s2
, s2_sz
, __MF_CHECK_READ
, "strncasecmp 2nd arg");
307 return strncasecmp (s1
, s2
, n
);
311 WRAPPER2(char *, strdup
, const char *s
)
313 DECLARE(void *, malloc
, size_t sz
);
315 size_t n
= strlen (s
);
316 TRACE ("%s\n", __PRETTY_FUNCTION__
);
317 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
,1), __MF_CHECK_READ
, "strdup region");
318 result
= (char *)CALL_REAL(malloc
,
319 CLAMPADD(CLAMPADD(n
,1),
320 CLAMPADD(__mf_opts
.crumple_zone
,
321 __mf_opts
.crumple_zone
)));
323 if (UNLIKELY(! result
)) return result
;
325 result
+= __mf_opts
.crumple_zone
;
326 memcpy (result
, s
, n
);
329 __mf_register (result
, CLAMPADD(n
,1), __MF_TYPE_HEAP_I
, "strdup region");
334 WRAPPER2(char *, strndup
, const char *s
, size_t n
)
336 DECLARE(void *, malloc
, size_t sz
);
338 size_t sz
= strnlen (s
, n
);
339 TRACE ("%s\n", __PRETTY_FUNCTION__
);
340 MF_VALIDATE_EXTENT(s
, sz
, __MF_CHECK_READ
, "strndup region"); /* nb: strNdup */
342 /* note: strndup still adds a \0, even with the N limit! */
343 result
= (char *)CALL_REAL(malloc
,
344 CLAMPADD(CLAMPADD(n
,1),
345 CLAMPADD(__mf_opts
.crumple_zone
,
346 __mf_opts
.crumple_zone
)));
348 if (UNLIKELY(! result
)) return result
;
350 result
+= __mf_opts
.crumple_zone
;
351 memcpy (result
, s
, n
);
354 __mf_register (result
, CLAMPADD(n
,1), __MF_TYPE_HEAP_I
, "strndup region");
359 WRAPPER2(char *, strchr
, const char *s
, int c
)
362 TRACE ("%s\n", __PRETTY_FUNCTION__
);
364 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
,1), __MF_CHECK_READ
, "strchr region");
365 return strchr (s
, c
);
369 WRAPPER2(char *, strrchr
, const char *s
, int c
)
372 TRACE ("%s\n", __PRETTY_FUNCTION__
);
374 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
,1), __MF_CHECK_READ
, "strrchr region");
375 return strrchr (s
, c
);
379 WRAPPER2(char *, strstr
, const char *haystack
, const char *needle
)
383 TRACE ("%s\n", __PRETTY_FUNCTION__
);
384 haystack_sz
= strlen (haystack
);
385 needle_sz
= strlen (needle
);
386 MF_VALIDATE_EXTENT(haystack
, CLAMPADD(haystack_sz
, 1), __MF_CHECK_READ
, "strstr haystack");
387 MF_VALIDATE_EXTENT(needle
, CLAMPADD(needle_sz
, 1), __MF_CHECK_READ
, "strstr needle");
388 return strstr (haystack
, needle
);
393 WRAPPER2(void *, memmem
,
394 const void *haystack
, size_t haystacklen
,
395 const void *needle
, size_t needlelen
)
397 TRACE ("%s\n", __PRETTY_FUNCTION__
);
398 MF_VALIDATE_EXTENT(haystack
, haystacklen
, __MF_CHECK_READ
, "memmem haystack");
399 MF_VALIDATE_EXTENT(needle
, needlelen
, __MF_CHECK_READ
, "memmem needle");
400 return memmem (haystack
, haystacklen
, needle
, needlelen
);
405 WRAPPER2(size_t, strlen
, const char *s
)
407 size_t result
= strlen (s
);
408 TRACE ("%s\n", __PRETTY_FUNCTION__
);
409 MF_VALIDATE_EXTENT(s
, CLAMPADD(result
, 1), __MF_CHECK_READ
, "strlen region");
414 WRAPPER2(size_t, strnlen
, const char *s
, size_t n
)
416 size_t result
= strnlen (s
, n
);
417 TRACE ("%s\n", __PRETTY_FUNCTION__
);
418 MF_VALIDATE_EXTENT(s
, result
, __MF_CHECK_READ
, "strnlen region");
423 WRAPPER2(void, bzero
, void *s
, size_t n
)
425 TRACE ("%s\n", __PRETTY_FUNCTION__
);
426 MF_VALIDATE_EXTENT(s
, n
, __MF_CHECK_WRITE
, "bzero region");
432 WRAPPER2(void, bcopy
, const void *src
, void *dest
, size_t n
)
434 TRACE ("%s\n", __PRETTY_FUNCTION__
);
435 MF_VALIDATE_EXTENT(src
, n
, __MF_CHECK_READ
, "bcopy src");
436 MF_VALIDATE_EXTENT(dest
, n
, __MF_CHECK_WRITE
, "bcopy dest");
437 bcopy (src
, dest
, n
);
442 WRAPPER2(int, bcmp
, const void *s1
, const void *s2
, size_t n
)
444 TRACE ("%s\n", __PRETTY_FUNCTION__
);
445 MF_VALIDATE_EXTENT(s1
, n
, __MF_CHECK_READ
, "bcmp 1st arg");
446 MF_VALIDATE_EXTENT(s2
, n
, __MF_CHECK_READ
, "bcmp 2nd arg");
447 return bcmp (s1
, s2
, n
);
451 WRAPPER2(char *, index
, const char *s
, int c
)
453 size_t n
= strlen (s
);
454 TRACE ("%s\n", __PRETTY_FUNCTION__
);
455 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "index region");
460 WRAPPER2(char *, rindex
, const char *s
, int c
)
462 size_t n
= strlen (s
);
463 TRACE ("%s\n", __PRETTY_FUNCTION__
);
464 MF_VALIDATE_EXTENT(s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "rindex region");
465 return rindex (s
, c
);
468 /* XXX: stpcpy, memccpy */
470 /* XXX: *printf,*scanf */
472 /* XXX: setjmp, longjmp */
474 WRAPPER2(char *, asctime
, struct tm
*tm
)
476 static char *reg_result
= NULL
;
478 TRACE ("%s\n", __PRETTY_FUNCTION__
);
479 MF_VALIDATE_EXTENT(tm
, sizeof (struct tm
), __MF_CHECK_READ
, "asctime tm");
480 result
= asctime (tm
);
481 if (reg_result
== NULL
)
483 __mf_register (result
, strlen (result
)+1, __MF_TYPE_STATIC
, "asctime string");
490 WRAPPER2(char *, ctime
, const time_t *timep
)
492 static char *reg_result
= NULL
;
494 TRACE ("%s\n", __PRETTY_FUNCTION__
);
495 MF_VALIDATE_EXTENT(timep
, sizeof (time_t), __MF_CHECK_READ
, "ctime time");
496 result
= ctime (timep
);
497 if (reg_result
== NULL
)
499 /* XXX: what if asctime and ctime return the same static ptr? */
500 __mf_register (result
, strlen (result
)+1, __MF_TYPE_STATIC
, "ctime string");
507 WRAPPER2(struct tm
*, localtime
, const time_t *timep
)
509 static struct tm
*reg_result
= NULL
;
511 TRACE ("%s\n", __PRETTY_FUNCTION__
);
512 MF_VALIDATE_EXTENT(timep
, sizeof (time_t), __MF_CHECK_READ
, "localtime time");
513 result
= localtime (timep
);
514 if (reg_result
== NULL
)
516 __mf_register (result
, sizeof (struct tm
), __MF_TYPE_STATIC
, "localtime tm");
523 WRAPPER2(struct tm
*, gmtime
, const time_t *timep
)
525 static struct tm
*reg_result
= NULL
;
527 TRACE ("%s\n", __PRETTY_FUNCTION__
);
528 MF_VALIDATE_EXTENT(timep
, sizeof (time_t), __MF_CHECK_READ
, "gmtime time");
529 result
= gmtime (timep
);
530 if (reg_result
== NULL
)
532 __mf_register (result
, sizeof (struct tm
), __MF_TYPE_STATIC
, "gmtime tm");
541 /* The following indicate if the result of the corresponding function
542 * should be explicitly un/registered by the wrapper
546 #define MF_REGISTER_fopen __MF_TYPE_STATIC
548 #undef MF_REGISTER_fopen
550 #define MF_RESULT_SIZE_fopen (sizeof (FILE))
552 #undef MF_REGISTER_opendir
553 #define MF_RESULT_SIZE_opendir 0 /* (sizeof (DIR)) */
554 #undef MF_REGISTER_readdir
555 #define MF_REGISTER_gethostbyname __MF_TYPE_STATIC
556 #undef MF_REGISTER_gethostbyname_items
557 #undef MF_REGISTER_dlopen
558 #undef MF_REGISTER_dlerror
559 #undef MF_REGISTER_dlsym
560 #define MF_REGISTER_shmat __MF_TYPE_GUESS
564 WRAPPER2(time_t, time
, time_t *timep
)
566 TRACE ("%s\n", __PRETTY_FUNCTION__
);
568 MF_VALIDATE_EXTENT (timep
, sizeof (*timep
), __MF_CHECK_WRITE
,
574 WRAPPER2(char *, strerror
, int errnum
)
577 static char * last_strerror
= NULL
;
579 TRACE ("%s\n", __PRETTY_FUNCTION__
);
580 p
= strerror (errnum
);
581 if (last_strerror
!= NULL
)
582 __mf_unregister (last_strerror
, 0, __MF_TYPE_STATIC
);
584 __mf_register (p
, strlen (p
) + 1, __MF_TYPE_STATIC
, "strerror result");
591 /* An auxiliary data structure for tracking the hand-made stdio
592 buffers we generate during the fopen/fopen64 hooks. In a civilized
593 language, this would be a simple dynamically sized FILE*->char*
594 lookup table, but this is C and we get to do it by hand. */
599 struct mf_filebuffer
*next
;
601 static struct mf_filebuffer
*mf_filebuffers
= NULL
;
606 /* Reset any buffer automatically provided by libc, since this may
607 have been done via mechanisms that libmudflap couldn't
610 size_t bufsize
= BUFSIZ
;
612 char *buffer
= malloc (bufsize
);
613 struct mf_filebuffer
*b
= malloc (sizeof (struct mf_filebuffer
));
614 assert ((buffer
!= NULL
) && (b
!= NULL
));
616 /* Link it into list. */
619 b
->next
= mf_filebuffers
;
622 /* Determine how the file is supposed to be buffered at the moment. */
623 bufmode
= fileno (f
) == 2 ? _IONBF
: (isatty (fileno (f
)) ? _IOLBF
: _IOFBF
);
625 rc
= setvbuf (f
, buffer
, bufmode
, bufsize
);
632 struct mf_filebuffer
*b
= mf_filebuffers
;
633 struct mf_filebuffer
**pb
= & mf_filebuffers
;
650 WRAPPER2(FILE *, fopen
, const char *path
, const char *mode
)
654 TRACE ("%s\n", __PRETTY_FUNCTION__
);
657 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fopen path");
660 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fopen mode");
662 p
= fopen (path
, mode
);
664 #ifdef MF_REGISTER_fopen
665 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "fopen result");
667 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "fopen result");
676 WRAPPER2(int, setvbuf
, FILE *stream
, char *buf
, int mode
, size_t size
)
679 TRACE ("%s\n", __PRETTY_FUNCTION__
);
681 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
, "setvbuf stream");
686 MF_VALIDATE_EXTENT (buf
, size
, __MF_CHECK_WRITE
, "setvbuf buffer");
688 /* Override the user only if it's an auto-allocated buffer request. Otherwise
689 assume that the supplied buffer is already known to libmudflap. */
690 if ((buf
== NULL
) && ((mode
== _IOFBF
) || (mode
== _IOLBF
)))
693 rc
= setvbuf (stream
, buf
, mode
, size
);
700 WRAPPER2(int, setbuf
, FILE* stream
, char *buf
)
702 return __mfwrap_setvbuf (stream
, buf
, buf
? _IOFBF
: _IONBF
, BUFSIZ
);
706 #ifdef HAVE_SETBUFFER
707 WRAPPER2(int, setbuffer
, FILE* stream
, char *buf
, size_t sz
)
709 return __mfwrap_setvbuf (stream
, buf
, buf
? _IOFBF
: _IONBF
, sz
);
713 #ifdef HAVE_SETLINEBUF
714 WRAPPER2(int, setlinebuf
, FILE* stream
)
716 return __mfwrap_setvbuf(stream
, NULL
, _IOLBF
, 0);
722 WRAPPER2(FILE *, fdopen
, int fd
, const char *mode
)
726 TRACE ("%s\n", __PRETTY_FUNCTION__
);
729 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fdopen mode");
731 p
= fdopen (fd
, mode
);
733 #ifdef MF_REGISTER_fopen
734 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "fdopen result");
736 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "fdopen result");
745 WRAPPER2(FILE *, freopen
, const char *path
, const char *mode
, FILE *s
)
749 TRACE ("%s\n", __PRETTY_FUNCTION__
);
752 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "freopen path");
754 MF_VALIDATE_EXTENT (s
, (sizeof (*s
)), __MF_CHECK_WRITE
, "freopen stream");
758 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "freopen mode");
760 p
= freopen (path
, mode
, s
);
762 #ifdef MF_REGISTER_fopen
763 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "freopen result");
765 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "freopen result");
775 WRAPPER2(FILE *, fopen64
, const char *path
, const char *mode
)
779 TRACE ("%s\n", __PRETTY_FUNCTION__
);
782 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fopen64 path");
785 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fopen64 mode");
787 p
= fopen64 (path
, mode
);
789 #ifdef MF_REGISTER_fopen
790 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "fopen64 result");
792 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "fopen64 result");
802 #ifdef HAVE_FREOPEN64
803 WRAPPER2(FILE *, freopen64
, const char *path
, const char *mode
, FILE *s
)
807 TRACE ("%s\n", __PRETTY_FUNCTION__
);
810 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "freopen64 path");
812 MF_VALIDATE_EXTENT (s
, (sizeof (*s
)), __MF_CHECK_WRITE
, "freopen64 stream");
816 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "freopen64 mode");
818 p
= freopen (path
, mode
, s
);
820 #ifdef MF_REGISTER_fopen
821 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "freopen64 result");
823 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "freopen64 result");
833 WRAPPER2(int, fclose
, FILE *stream
)
836 TRACE ("%s\n", __PRETTY_FUNCTION__
);
837 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
839 resp
= fclose (stream
);
840 #ifdef MF_REGISTER_fopen
841 __mf_unregister (stream
, sizeof (*stream
), MF_REGISTER_fopen
);
849 WRAPPER2(size_t, fread
, void *ptr
, size_t size
, size_t nmemb
, FILE *stream
)
851 TRACE ("%s\n", __PRETTY_FUNCTION__
);
852 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
854 MF_VALIDATE_EXTENT (ptr
, size
* nmemb
, __MF_CHECK_WRITE
, "fread buffer");
855 return fread (ptr
, size
, nmemb
, stream
);
859 WRAPPER2(size_t, fwrite
, const void *ptr
, size_t size
, size_t nmemb
,
862 TRACE ("%s\n", __PRETTY_FUNCTION__
);
863 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
865 MF_VALIDATE_EXTENT (ptr
, size
* nmemb
, __MF_CHECK_READ
, "fwrite buffer");
866 return fwrite (ptr
, size
, nmemb
, stream
);
870 WRAPPER2(int, fgetc
, FILE *stream
)
872 TRACE ("%s\n", __PRETTY_FUNCTION__
);
873 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
875 return fgetc (stream
);
879 WRAPPER2(char *, fgets
, char *s
, int size
, FILE *stream
)
881 TRACE ("%s\n", __PRETTY_FUNCTION__
);
882 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
884 MF_VALIDATE_EXTENT (s
, size
, __MF_CHECK_WRITE
, "fgets buffer");
885 return fgets (s
, size
, stream
);
889 WRAPPER2(int, getc
, FILE *stream
)
891 TRACE ("%s\n", __PRETTY_FUNCTION__
);
892 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
894 return getc (stream
);
898 WRAPPER2(char *, gets
, char *s
)
900 TRACE ("%s\n", __PRETTY_FUNCTION__
);
901 MF_VALIDATE_EXTENT (s
, 1, __MF_CHECK_WRITE
, "gets buffer");
902 /* Avoid link-time warning... */
903 s
= fgets (s
, INT_MAX
, stdin
);
904 if (NULL
!= s
) { /* better late than never */
905 size_t n
= strlen (s
);
906 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_WRITE
, "gets buffer");
912 WRAPPER2(int, ungetc
, int c
, FILE *stream
)
914 TRACE ("%s\n", __PRETTY_FUNCTION__
);
915 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
917 return ungetc (c
, stream
);
921 WRAPPER2(int, fputc
, int c
, FILE *stream
)
923 TRACE ("%s\n", __PRETTY_FUNCTION__
);
924 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
926 return fputc (c
, stream
);
930 WRAPPER2(int, fputs
, const char *s
, FILE *stream
)
933 TRACE ("%s\n", __PRETTY_FUNCTION__
);
935 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "fputs buffer");
936 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
938 return fputs (s
, stream
);
942 WRAPPER2(int, putc
, int c
, FILE *stream
)
944 TRACE ("%s\n", __PRETTY_FUNCTION__
);
945 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
947 return putc (c
, stream
);
951 WRAPPER2(int, puts
, const char *s
)
954 TRACE ("%s\n", __PRETTY_FUNCTION__
);
956 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "puts buffer");
961 WRAPPER2(void, clearerr
, FILE *stream
)
963 TRACE ("%s\n", __PRETTY_FUNCTION__
);
964 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
970 WRAPPER2(int, feof
, FILE *stream
)
972 TRACE ("%s\n", __PRETTY_FUNCTION__
);
973 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
975 return feof (stream
);
979 WRAPPER2(int, ferror
, FILE *stream
)
981 TRACE ("%s\n", __PRETTY_FUNCTION__
);
982 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
984 return ferror (stream
);
988 WRAPPER2(int, fileno
, FILE *stream
)
990 TRACE ("%s\n", __PRETTY_FUNCTION__
);
991 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
993 return fileno (stream
);
997 WRAPPER2(int, printf
, const char *format
, ...)
1002 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1003 n
= strlen (format
);
1004 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1006 va_start (ap
, format
);
1007 result
= vprintf (format
, ap
);
1013 WRAPPER2(int, fprintf
, FILE *stream
, const char *format
, ...)
1018 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1019 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1021 n
= strlen (format
);
1022 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1024 va_start (ap
, format
);
1025 result
= vfprintf (stream
, format
, ap
);
1031 WRAPPER2(int, sprintf
, char *str
, const char *format
, ...)
1036 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1037 MF_VALIDATE_EXTENT (str
, 1, __MF_CHECK_WRITE
, "sprintf str");
1038 n
= strlen (format
);
1039 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1041 va_start (ap
, format
);
1042 result
= vsprintf (str
, format
, ap
);
1045 MF_VALIDATE_EXTENT (str
, CLAMPADD(n
, 1), __MF_CHECK_WRITE
, "sprintf str");
1050 WRAPPER2(int, snprintf
, char *str
, size_t size
, const char *format
, ...)
1055 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1056 MF_VALIDATE_EXTENT (str
, size
, __MF_CHECK_WRITE
, "snprintf str");
1057 n
= strlen (format
);
1058 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1060 va_start (ap
, format
);
1061 result
= vsnprintf (str
, size
, format
, ap
);
1067 WRAPPER2(int, vprintf
, const char *format
, va_list ap
)
1070 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1071 n
= strlen (format
);
1072 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1074 return vprintf (format
, ap
);
1078 WRAPPER2(int, vfprintf
, FILE *stream
, const char *format
, va_list ap
)
1081 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1082 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1084 n
= strlen (format
);
1085 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1087 return vfprintf (stream
, format
, ap
);
1091 WRAPPER2(int, vsprintf
, char *str
, const char *format
, va_list ap
)
1095 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1096 MF_VALIDATE_EXTENT (str
, 1, __MF_CHECK_WRITE
, "vsprintf str");
1097 n
= strlen (format
);
1098 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1100 result
= vsprintf (str
, format
, ap
);
1102 MF_VALIDATE_EXTENT (str
, CLAMPADD(n
, 1), __MF_CHECK_WRITE
, "vsprintf str");
1107 WRAPPER2(int, vsnprintf
, char *str
, size_t size
, const char *format
,
1111 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1112 MF_VALIDATE_EXTENT (str
, size
, __MF_CHECK_WRITE
, "vsnprintf str");
1113 n
= strlen (format
);
1114 MF_VALIDATE_EXTENT (format
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1115 "vsnprintf format");
1116 return vsnprintf (str
, size
, format
, ap
);
1120 WRAPPER2(int , access
, const char *path
, int mode
)
1123 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1125 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "access path");
1126 return access (path
, mode
);
1130 WRAPPER2(int , remove
, const char *path
)
1133 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1135 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "remove path");
1136 return remove (path
);
1140 WRAPPER2(int, fflush
, FILE *stream
)
1142 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1144 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1146 return fflush (stream
);
1150 WRAPPER2(int, fseek
, FILE *stream
, long offset
, int whence
)
1152 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1153 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1155 return fseek (stream
, offset
, whence
);
1159 #ifdef HAVE_FSEEKO64
1160 WRAPPER2(int, fseeko64
, FILE *stream
, off64_t offset
, int whence
)
1162 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1163 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1165 return fseeko64 (stream
, offset
, whence
);
1170 WRAPPER2(long, ftell
, FILE *stream
)
1172 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1173 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1175 return ftell (stream
);
1179 #ifdef HAVE_FTELLO64
1180 WRAPPER2(off64_t
, ftello64
, FILE *stream
)
1182 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1183 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1185 return ftello64 (stream
);
1190 WRAPPER2(void, rewind
, FILE *stream
)
1192 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1193 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1199 WRAPPER2(int, fgetpos
, FILE *stream
, fpos_t *pos
)
1201 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1202 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1204 MF_VALIDATE_EXTENT (pos
, sizeof (*pos
), __MF_CHECK_WRITE
, "fgetpos pos");
1205 return fgetpos (stream
, pos
);
1209 WRAPPER2(int, fsetpos
, FILE *stream
, fpos_t *pos
)
1211 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1212 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1214 MF_VALIDATE_EXTENT (pos
, sizeof (*pos
), __MF_CHECK_READ
, "fsetpos pos");
1215 return fsetpos (stream
, pos
);
1219 WRAPPER2(int , stat
, const char *path
, struct stat
*buf
)
1222 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1224 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "stat path");
1225 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
, "stat buf");
1226 return stat (path
, buf
);
1231 WRAPPER2(int , stat64
, const char *path
, struct stat64
*buf
)
1234 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1236 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "stat64 path");
1237 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
, "stat64 buf");
1238 return stat64 (path
, buf
);
1243 WRAPPER2(int , fstat
, int filedes
, struct stat
*buf
)
1245 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1246 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
, "fstat buf");
1247 return fstat (filedes
, buf
);
1251 WRAPPER2(int , lstat
, const char *path
, struct stat
*buf
)
1254 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1256 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "lstat path");
1257 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
, "lstat buf");
1258 return lstat (path
, buf
);
1262 WRAPPER2(int , mkfifo
, const char *path
, mode_t mode
)
1265 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1267 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "mkfifo path");
1268 return mkfifo (path
, mode
);
1272 #ifdef HAVE_DIRENT_H
1273 WRAPPER2(DIR *, opendir
, const char *path
)
1277 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1279 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "opendir path");
1283 #ifdef MF_REGISTER_opendir
1284 __mf_register (p
, MF_RESULT_SIZE_opendir
, MF_REGISTER_opendir
,
1287 MF_VALIDATE_EXTENT (p
, MF_RESULT_SIZE_opendir
, __MF_CHECK_WRITE
,
1294 WRAPPER2(int, closedir
, DIR *dir
)
1296 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1297 MF_VALIDATE_EXTENT (dir
, 0, __MF_CHECK_WRITE
, "closedir dir");
1298 #ifdef MF_REGISTER_opendir
1299 __mf_unregister (dir
, MF_RESULT_SIZE_opendir
, MF_REGISTER_opendir
);
1301 return closedir (dir
);
1305 WRAPPER2(struct dirent
*, readdir
, DIR *dir
)
1308 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1309 MF_VALIDATE_EXTENT (dir
, 0, __MF_CHECK_READ
, "readdir dir");
1312 #ifdef MF_REGISTER_readdir
1313 __mf_register (p
, sizeof (*p
), MF_REGISTER_readdir
, "readdir result");
1315 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "readdir result");
1322 #ifdef HAVE_SYS_SOCKET_H
1324 WRAPPER2(int, recv
, int s
, void *buf
, size_t len
, int flags
)
1326 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1327 MF_VALIDATE_EXTENT (buf
, len
, __MF_CHECK_WRITE
, "recv buf");
1328 return recv (s
, buf
, len
, flags
);
1332 WRAPPER2(int, recvfrom
, int s
, void *buf
, size_t len
, int flags
,
1333 struct sockaddr
*from
, socklen_t
*fromlen
)
1335 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1336 MF_VALIDATE_EXTENT (buf
, len
, __MF_CHECK_WRITE
, "recvfrom buf");
1337 MF_VALIDATE_EXTENT (from
, (size_t)*fromlen
, __MF_CHECK_WRITE
,
1339 return recvfrom (s
, buf
, len
, flags
, from
, fromlen
);
1343 WRAPPER2(int, recvmsg
, int s
, struct msghdr
*msg
, int flags
)
1345 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1346 MF_VALIDATE_EXTENT (msg
, sizeof (*msg
), __MF_CHECK_WRITE
, "recvmsg msg");
1347 return recvmsg (s
, msg
, flags
);
1351 WRAPPER2(int, send
, int s
, const void *msg
, size_t len
, int flags
)
1353 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1354 MF_VALIDATE_EXTENT (msg
, len
, __MF_CHECK_READ
, "send msg");
1355 return send (s
, msg
, len
, flags
);
1359 WRAPPER2(int, sendto
, int s
, const void *msg
, size_t len
, int flags
,
1360 const struct sockaddr
*to
, socklen_t tolen
)
1362 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1363 MF_VALIDATE_EXTENT (msg
, len
, __MF_CHECK_READ
, "sendto msg");
1364 MF_VALIDATE_EXTENT (to
, (size_t)tolen
, __MF_CHECK_WRITE
, "sendto to");
1365 return sendto (s
, msg
, len
, flags
, to
, tolen
);
1369 WRAPPER2(int, sendmsg
, int s
, const void *msg
, int flags
)
1371 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1372 MF_VALIDATE_EXTENT (msg
, sizeof (*msg
), __MF_CHECK_READ
, "sendmsg msg");
1373 return sendmsg (s
, msg
, flags
);
1377 WRAPPER2(int, setsockopt
, int s
, int level
, int optname
, const void *optval
,
1380 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1381 MF_VALIDATE_EXTENT (optval
, (size_t)optlen
, __MF_CHECK_READ
,
1382 "setsockopt optval");
1383 return setsockopt (s
, level
, optname
, optval
, optlen
);
1387 WRAPPER2(int, getsockopt
, int s
, int level
, int optname
, void *optval
,
1390 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1391 MF_VALIDATE_EXTENT (optval
, (size_t)*optlen
, __MF_CHECK_WRITE
,
1392 "getsockopt optval");
1393 return getsockopt (s
, level
, optname
, optval
, optlen
);
1397 WRAPPER2(int, accept
, int s
, struct sockaddr
*addr
, socklen_t
*addrlen
)
1399 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1401 MF_VALIDATE_EXTENT (addr
, (size_t)*addrlen
, __MF_CHECK_WRITE
, "accept addr");
1402 return accept (s
, addr
, addrlen
);
1406 WRAPPER2(int, bind
, int sockfd
, struct sockaddr
*addr
, socklen_t addrlen
)
1408 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1409 MF_VALIDATE_EXTENT (addr
, (size_t)addrlen
, __MF_CHECK_WRITE
, "bind addr");
1410 return bind (sockfd
, addr
, addrlen
);
1414 WRAPPER2(int, connect
, int sockfd
, const struct sockaddr
*addr
,
1417 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1418 MF_VALIDATE_EXTENT (addr
, (size_t)addrlen
, __MF_CHECK_READ
,
1420 return connect (sockfd
, addr
, addrlen
);
1423 #endif /* HAVE_SYS_SOCKET_H */
1426 WRAPPER2(int, gethostname
, char *name
, size_t len
)
1428 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1429 MF_VALIDATE_EXTENT (name
, len
, __MF_CHECK_WRITE
, "gethostname name");
1430 return gethostname (name
, len
);
1434 #ifdef HAVE_SETHOSTNAME
1435 WRAPPER2(int, sethostname
, const char *name
, size_t len
)
1437 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1438 MF_VALIDATE_EXTENT (name
, len
, __MF_CHECK_READ
, "sethostname name");
1439 return sethostname (name
, len
);
1446 WRAPPER2(struct hostent
*, gethostbyname
, const char *name
)
1453 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1455 MF_VALIDATE_EXTENT (name
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1456 "gethostbyname name");
1457 p
= gethostbyname (name
);
1459 #ifdef MF_REGISTER_gethostbyname
1460 __mf_register (p
, sizeof (*p
), MF_REGISTER_gethostbyname
,
1461 "gethostbyname result");
1463 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
,
1464 "gethostbyname result");
1465 if (NULL
!= (s
= p
->h_name
)) {
1468 #ifdef MF_REGISTER_gethostbyname_items
1469 __mf_register (s
, n
, MF_REGISTER_gethostbyname_items
,
1470 "gethostbyname result->h_name");
1472 MF_VALIDATE_EXTENT (s
, n
, __MF_CHECK_WRITE
,
1473 "gethostbyname result->h_name");
1476 if (NULL
!= (ss
= p
->h_aliases
)) {
1477 for (nreg
= 1;; ++nreg
) {
1483 #ifdef MF_REGISTER_gethostbyname_items
1484 __mf_register (s
, n
, MF_REGISTER_gethostbyname_items
,
1485 "gethostbyname result->h_aliases[]");
1487 MF_VALIDATE_EXTENT (s
, n
, __MF_CHECK_WRITE
,
1488 "gethostbyname result->h_aliases[]");
1490 nreg
*= sizeof (*p
->h_aliases
);
1491 #ifdef MF_REGISTER_gethostbyname_items
1492 __mf_register (p
->h_aliases
, nreg
, MF_REGISTER_gethostbyname_items
,
1493 "gethostbyname result->h_aliases");
1495 MF_VALIDATE_EXTENT (p
->h_aliases
, nreg
, __MF_CHECK_WRITE
,
1496 "gethostbyname result->h_aliases");
1499 if (NULL
!= (ss
= p
->h_addr_list
)) {
1500 for (nreg
= 1;; ++nreg
) {
1504 #ifdef MF_REGISTER_gethostbyname_items
1505 __mf_register (s
, p
->h_length
, MF_REGISTER_gethostbyname_items
,
1506 "gethostbyname result->h_addr_list[]");
1508 MF_VALIDATE_EXTENT (s
, p
->h_length
, __MF_CHECK_WRITE
,
1509 "gethostbyname result->h_addr_list[]");
1511 nreg
*= sizeof (*p
->h_addr_list
);
1512 #ifdef MF_REGISTER_gethostbyname_items
1513 __mf_register (p
->h_addr_list
, nreg
, MF_REGISTER_gethostbyname_items
,
1514 "gethostbyname result->h_addr_list");
1516 MF_VALIDATE_EXTENT (p
->h_addr_list
, nreg
, __MF_CHECK_WRITE
,
1517 "gethostbyname result->h_addr_list");
1523 #endif /* HAVE_NETDB_H */
1526 #ifdef HAVE_SYS_WAIT_H
1528 WRAPPER2(pid_t
, wait
, int *status
)
1530 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1532 MF_VALIDATE_EXTENT (status
, sizeof (*status
), __MF_CHECK_WRITE
,
1534 return wait (status
);
1538 WRAPPER2(pid_t
, waitpid
, pid_t pid
, int *status
, int options
)
1540 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1542 MF_VALIDATE_EXTENT (status
, sizeof (*status
), __MF_CHECK_WRITE
,
1544 return waitpid (pid
, status
, options
);
1547 #endif /* HAVE_SYS_WAIT_H */
1550 WRAPPER2(FILE *, popen
, const char *command
, const char *mode
)
1554 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1556 n
= strlen (command
);
1557 MF_VALIDATE_EXTENT (command
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "popen path");
1560 MF_VALIDATE_EXTENT (mode
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "popen mode");
1562 p
= popen (command
, mode
);
1564 #ifdef MF_REGISTER_fopen
1565 __mf_register (p
, sizeof (*p
), MF_REGISTER_fopen
, "popen result");
1567 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_WRITE
, "popen result");
1573 WRAPPER2(int, pclose
, FILE *stream
)
1576 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1577 MF_VALIDATE_EXTENT (stream
, sizeof (*stream
), __MF_CHECK_WRITE
,
1579 resp
= pclose (stream
);
1580 #ifdef MF_REGISTER_fopen
1581 __mf_unregister (stream
, sizeof (*stream
), MF_REGISTER_fopen
);
1587 WRAPPER2(int, execve
, const char *path
, char *const argv
[],
1593 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1596 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execve path");
1599 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_READ
, "execve *argv");
1604 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execve **argv");
1608 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_READ
, "execve *envp");
1613 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execve **envp");
1615 return execve (path
, argv
, envp
);
1619 WRAPPER2(int, execv
, const char *path
, char *const argv
[])
1624 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1627 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execv path");
1630 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_READ
, "execv *argv");
1635 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execv **argv");
1637 return execv (path
, argv
);
1641 WRAPPER2(int, execvp
, const char *path
, char *const argv
[])
1646 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1649 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execvp path");
1652 MF_VALIDATE_EXTENT (p
, sizeof (*p
), __MF_CHECK_READ
, "execvp *argv");
1657 MF_VALIDATE_EXTENT (s
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "execvp **argv");
1659 return execvp (path
, argv
);
1663 WRAPPER2(int, system
, const char *string
)
1666 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1667 n
= strlen (string
);
1668 MF_VALIDATE_EXTENT (string
, CLAMPADD(n
, 1), __MF_CHECK_READ
,
1670 return system (string
);
1674 WRAPPER2(void *, dlopen
, const char *path
, int flags
)
1678 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1680 MF_VALIDATE_EXTENT (path
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "dlopen path");
1681 p
= dlopen (path
, flags
);
1683 #ifdef MF_REGISTER_dlopen
1684 __mf_register (p
, 0, MF_REGISTER_dlopen
, "dlopen result");
1686 MF_VALIDATE_EXTENT (p
, 0, __MF_CHECK_WRITE
, "dlopen result");
1692 WRAPPER2(int, dlclose
, void *handle
)
1695 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1696 MF_VALIDATE_EXTENT (handle
, 0, __MF_CHECK_READ
, "dlclose handle");
1697 resp
= dlclose (handle
);
1698 #ifdef MF_REGISTER_dlopen
1699 __mf_unregister (handle
, 0, MF_REGISTER_dlopen
);
1705 WRAPPER2(char *, dlerror
)
1708 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1714 #ifdef MF_REGISTER_dlerror
1715 __mf_register (p
, n
, MF_REGISTER_dlerror
, "dlerror result");
1717 MF_VALIDATE_EXTENT (p
, n
, __MF_CHECK_WRITE
, "dlerror result");
1723 WRAPPER2(void *, dlsym
, void *handle
, char *symbol
)
1727 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1728 MF_VALIDATE_EXTENT (handle
, 0, __MF_CHECK_READ
, "dlsym handle");
1729 n
= strlen (symbol
);
1730 MF_VALIDATE_EXTENT (symbol
, CLAMPADD(n
, 1), __MF_CHECK_READ
, "dlsym symbol");
1731 p
= dlsym (handle
, symbol
);
1733 #ifdef MF_REGISTER_dlsym
1734 __mf_register (p
, 0, MF_REGISTER_dlsym
, "dlsym result");
1736 MF_VALIDATE_EXTENT (p
, 0, __MF_CHECK_WRITE
, "dlsym result");
1742 #if defined (HAVE_SYS_IPC_H) && defined (HAVE_SYS_SEM_H) && defined (HAVE_SYS_SHM_H)
1744 WRAPPER2(int, semop
, int semid
, struct sembuf
*sops
, unsigned nsops
)
1746 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1747 MF_VALIDATE_EXTENT (sops
, sizeof (*sops
) * nsops
, __MF_CHECK_READ
,
1749 return semop (semid
, sops
, nsops
);
1753 #ifndef HAVE_UNION_SEMUN
1755 int val
; /* value for SETVAL */
1756 struct semid_ds
*buf
; /* buffer for IPC_STAT, IPC_SET */
1757 unsigned short int *array
; /* array for GETALL, SETALL */
1758 struct seminfo
*__buf
; /* buffer for IPC_INFO */
1761 WRAPPER2(int, semctl
, int semid
, int semnum
, int cmd
, union semun arg
)
1763 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1766 MF_VALIDATE_EXTENT (arg
.buf
, sizeof (*arg
.buf
), __MF_CHECK_WRITE
,
1770 MF_VALIDATE_EXTENT (arg
.buf
, sizeof (*arg
.buf
), __MF_CHECK_READ
,
1774 MF_VALIDATE_EXTENT (arg
.array
, sizeof (*arg
.array
), __MF_CHECK_WRITE
,
1777 MF_VALIDATE_EXTENT (arg
.array
, sizeof (*arg
.array
), __MF_CHECK_READ
,
1781 /* FreeBSD 5.1 And Cygwin headers include IPC_INFO but not the __buf field. */
1782 #if !defined(__FreeBSD__) && !defined(__CYGWIN__)
1784 MF_VALIDATE_EXTENT (arg
.__buf
, sizeof (*arg
.__buf
), __MF_CHECK_WRITE
,
1792 return semctl (semid
, semnum
, cmd
, arg
);
1796 WRAPPER2(int, shmctl
, int shmid
, int cmd
, struct shmid_ds
*buf
)
1798 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1801 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_WRITE
,
1805 MF_VALIDATE_EXTENT (buf
, sizeof (*buf
), __MF_CHECK_READ
,
1811 return shmctl (shmid
, cmd
, buf
);
1815 WRAPPER2(void *, shmat
, int shmid
, const void *shmaddr
, int shmflg
)
1818 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1819 p
= shmat (shmid
, shmaddr
, shmflg
);
1820 #ifdef MF_REGISTER_shmat
1822 struct shmid_ds buf
;
1823 __mf_register (p
, shmctl (shmid
, IPC_STAT
, &buf
) ? 0 : buf
.shm_segsz
,
1824 MF_REGISTER_shmat
, "shmat result");
1831 WRAPPER2(int, shmdt
, const void *shmaddr
)
1834 TRACE ("%s\n", __PRETTY_FUNCTION__
);
1835 resp
= shmdt (shmaddr
);
1836 #ifdef MF_REGISTER_shmat
1837 __mf_unregister ((void *)shmaddr
, 0, MF_REGISTER_shmat
);
1843 #endif /* HAVE_SYS_IPC/SEM/SHM_H */
1847 /* ctype stuff. This is host-specific by necessity, as the arrays
1848 that is used by most is*()/to*() macros are implementation-defined. */
1851 #ifdef HAVE___CTYPE_B_LOC
1852 WRAPPER2(unsigned short **, __ctype_b_loc
, void)
1854 static unsigned short * last_buf
= (void *) 0;
1855 static unsigned short ** last_ptr
= (void *) 0;
1856 unsigned short ** ptr
= (unsigned short **) __ctype_b_loc ();
1857 unsigned short * buf
= * ptr
;
1858 if (ptr
!= last_ptr
)
1860 /* XXX: unregister last_ptr? */
1862 __mf_register (last_ptr
, sizeof(last_ptr
), __MF_TYPE_STATIC
, "ctype_b_loc **");
1864 if (buf
!= last_buf
)
1867 __mf_register ((void *) (last_buf
- 128), 384 * sizeof(unsigned short), __MF_TYPE_STATIC
,
1874 #ifdef HAVE___CTYPE_TOUPPER_LOC
1875 WRAPPER2(int **, __ctype_toupper_loc
, void)
1877 static int * last_buf
= (void *) 0;
1878 static int ** last_ptr
= (void *) 0;
1879 int ** ptr
= (int **) __ctype_toupper_loc ();
1881 if (ptr
!= last_ptr
)
1883 /* XXX: unregister last_ptr? */
1885 __mf_register (last_ptr
, sizeof(last_ptr
), __MF_TYPE_STATIC
, "ctype_toupper_loc **");
1887 if (buf
!= last_buf
)
1890 __mf_register ((void *) (last_buf
- 128), 384 * sizeof(int), __MF_TYPE_STATIC
,
1891 "ctype_toupper_loc []");
1897 #ifdef HAVE___CTYPE_TOLOWER_LOC
1898 WRAPPER2(int **, __ctype_tolower_loc
, void)
1900 static int * last_buf
= (void *) 0;
1901 static int ** last_ptr
= (void *) 0;
1902 int ** ptr
= (int **) __ctype_tolower_loc ();
1904 if (ptr
!= last_ptr
)
1906 /* XXX: unregister last_ptr? */
1908 __mf_register (last_ptr
, sizeof(last_ptr
), __MF_TYPE_STATIC
, "ctype_tolower_loc **");
1910 if (buf
!= last_buf
)
1913 __mf_register ((void *) (last_buf
- 128), 384 * sizeof(int), __MF_TYPE_STATIC
,
1914 "ctype_tolower_loc []");
1921 /* passwd/group related functions. These register every (static) pointer value returned,
1922 and rely on libmudflap's quiet toleration of duplicate static registrations. */
1924 #ifdef HAVE_GETLOGIN
1925 WRAPPER2(char *, getlogin
, void)
1927 char *buf
= getlogin ();
1929 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1930 "getlogin() return");
1937 WRAPPER2(char *, cuserid
, char * buf
)
1941 MF_VALIDATE_EXTENT(buf
, L_cuserid
, __MF_CHECK_WRITE
,
1942 "cuserid destination");
1943 return cuserid (buf
);
1945 buf
= cuserid (NULL
);
1947 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1948 "getcuserid() return");
1954 #ifdef HAVE_GETPWNAM
1955 WRAPPER2(struct passwd
*, getpwnam
, const char *name
)
1958 MF_VALIDATE_EXTENT(name
, strlen(name
)+1, __MF_CHECK_READ
,
1960 buf
= getpwnam (name
);
1962 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1969 #ifdef HAVE_GETPWUID
1970 WRAPPER2(struct passwd
*, getpwuid
, uid_t uid
)
1973 buf
= getpwuid (uid
);
1975 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1982 #ifdef HAVE_GETGRNAM
1983 WRAPPER2(struct group
*, getgrnam
, const char *name
)
1986 MF_VALIDATE_EXTENT(name
, strlen(name
)+1, __MF_CHECK_READ
,
1988 buf
= getgrnam (name
);
1990 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
1997 #ifdef HAVE_GETGRGID
1998 WRAPPER2(struct group
*, getgrgid
, uid_t uid
)
2001 buf
= getgrgid (uid
);
2003 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2010 #ifdef HAVE_GETSERVENT
2011 WRAPPER2(struct servent
*, getservent
, void)
2013 struct servent
*buf
;
2014 buf
= getservent ();
2016 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2017 "getserv*() return");
2023 #ifdef HAVE_GETSERVBYNAME
2024 WRAPPER2(struct servent
*, getservbyname
, const char *name
, const char *proto
)
2026 struct servent
*buf
;
2027 MF_VALIDATE_EXTENT(name
, strlen(name
)+1, __MF_CHECK_READ
,
2028 "getservbyname name");
2029 MF_VALIDATE_EXTENT(proto
, strlen(proto
)+1, __MF_CHECK_READ
,
2030 "getservbyname proto");
2031 buf
= getservbyname (name
, proto
);
2033 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2034 "getserv*() return");
2040 #ifdef HAVE_GETSERVBYPORT
2041 WRAPPER2(struct servent
*, getservbyport
, int port
, const char *proto
)
2043 struct servent
*buf
;
2044 MF_VALIDATE_EXTENT(proto
, strlen(proto
)+1, __MF_CHECK_READ
,
2045 "getservbyport proto");
2046 buf
= getservbyport (port
, proto
);
2048 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2049 "getserv*() return");
2055 #ifdef HAVE_GAI_STRERROR
2056 WRAPPER2(const char *, gai_strerror
, int errcode
)
2059 buf
= gai_strerror (errcode
);
2061 __mf_register ((void *) buf
, strlen(buf
)+1, __MF_TYPE_STATIC
,
2062 "gai_strerror() return");
2068 #ifdef HAVE_GETMNTENT
2069 #ifdef HAVE_MNTENT_H
2070 WRAPPER2(struct mntent
*, getmntent
, FILE *filep
)
2073 static struct mntent
*last
= NULL
;
2075 MF_VALIDATE_EXTENT (filep
, sizeof (*filep
), __MF_CHECK_WRITE
,
2076 "getmntent stream");
2077 #define UR(field) __mf_unregister(last->field, strlen (last->field)+1, __MF_TYPE_STATIC)
2084 __mf_unregister (last
, sizeof (*last
), __MF_TYPE_STATIC
);
2088 m
= getmntent (filep
);
2091 #define R(field) __mf_register(last->field, strlen (last->field)+1, __MF_TYPE_STATIC, "mntent " #field)
2098 __mf_register (last
, sizeof (*last
), __MF_TYPE_STATIC
, "getmntent result");
2104 #elif defined HAVE_SYS_MNTTAB_H
2105 WRAPPER2(int, getmntent
, FILE *filep
, struct mnttab
*mp
)
2107 static struct mnttab
*last
= NULL
;
2110 MF_VALIDATE_EXTENT (filep
, sizeof (*filep
), __MF_CHECK_WRITE
,
2111 "getmntent stream");
2112 #define UR(field) __mf_unregister(last->field, strlen (last->field)+1, __MF_TYPE_STATIC)
2120 __mf_unregister (last
, sizeof (*last
), __MF_TYPE_STATIC
);
2124 res
= getmntent (filep
, mp
);
2127 #define R(field) __mf_register(last->field, strlen (last->field)+1, __MF_TYPE_STATIC, "mntent " #field)
2135 __mf_register (last
, sizeof (*last
), __MF_TYPE_STATIC
, "getmntent result");
2145 #ifdef HAVE_INET_NTOA
2146 WRAPPER2(char *, inet_ntoa
, struct in_addr in
)
2148 static char *last_buf
= NULL
;
2151 __mf_unregister (last_buf
, strlen (last_buf
)+1, __MF_TYPE_STATIC
);
2152 buf
= inet_ntoa (in
);
2155 __mf_register (last_buf
, strlen (last_buf
)+1, __MF_TYPE_STATIC
, "inet_ntoa result");
2161 #ifdef HAVE_GETPROTOENT
2162 WRAPPER2(struct protoent
*, getprotoent
, void)
2164 struct protoent
*buf
;
2165 buf
= getprotoent ();
2167 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
, "getproto*() return");
2173 #ifdef HAVE_GETPROTOBYNAME
2174 WRAPPER2(struct protoent
*, getprotobyname
, const char *name
)
2176 struct protoent
*buf
;
2177 MF_VALIDATE_EXTENT(name
, strlen(name
)+1, __MF_CHECK_READ
,
2178 "getprotobyname name");
2179 buf
= getprotobyname (name
);
2181 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2182 "getproto*() return");
2188 #ifdef HAVE_GETPROTOBYNUMBER
2189 WRAPPER2(struct protoent
*, getprotobynumber
, int port
)
2191 struct protoent
*buf
;
2192 buf
= getprotobynumber (port
);
2194 __mf_register (buf
, sizeof(*buf
), __MF_TYPE_STATIC
,
2195 "getproto*() return");