forked from gcc-mirror/gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mf-hooks2.c
2162 lines (1789 loc) · 53.7 KB
/
mf-hooks2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Mudflap: narrow-pointer bounds-checking by tree rewriting.
Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
Contributed by Frank Ch. Eigler <[email protected]>
and Graydon Hoare <[email protected]>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file into combinations with other programs,
and to distribute those combinations without any restriction coming
from the use of this file. (The General Public License restrictions
do apply in other respects; for example, they cover modification of
the file, and distribution when not linked into a combine
executable.)
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
#include "config.h"
#ifndef HAVE_SOCKLEN_T
#define socklen_t int
#endif
/* These attempt to coax various unix flavours to declare all our
needed tidbits in the system headers. */
#if !defined(__FreeBSD__) && !defined(__APPLE__)
#define _POSIX_SOURCE
#endif /* Some BSDs break <sys/socket.h> if this is defined. */
#define _GNU_SOURCE
#define _XOPEN_SOURCE
#define _BSD_TYPES
#define __EXTENSIONS__
#define _ALL_SOURCE
#define _LARGE_FILE_API
#define _LARGEFILE64_SOURCE
#define _XOPEN_SOURCE_EXTENDED 1
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#endif
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_SYS_IPC_H
#include <sys/ipc.h>
#endif
#ifdef HAVE_SYS_SEM_H
#include <sys/sem.h>
#endif
#ifdef HAVE_SYS_SHM_H
#include <sys/shm.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#ifdef HAVE_GRP_H
#include <grp.h>
#endif
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include "mf-runtime.h"
#include "mf-impl.h"
#ifdef _MUDFLAP
#error "Do not compile this file with -fmudflap!"
#endif
/* A bunch of independent stdlib/unistd hook functions, all
intercepted by mf-runtime.h macros. */
#ifndef HAVE_STRNLEN
static inline size_t (strnlen) (const char* str, size_t n)
{
const char *s;
for (s = str; n && *s; ++s, --n)
;
return (s - str);
}
#endif
/* str*,mem*,b* */
WRAPPER2(void *, memcpy, void *dest, const void *src, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(src, n, __MF_CHECK_READ, "memcpy source");
MF_VALIDATE_EXTENT(dest, n, __MF_CHECK_WRITE, "memcpy dest");
return memcpy (dest, src, n);
}
WRAPPER2(void *, memmove, void *dest, const void *src, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(src, n, __MF_CHECK_READ, "memmove src");
MF_VALIDATE_EXTENT(dest, n, __MF_CHECK_WRITE, "memmove dest");
return memmove (dest, src, n);
}
WRAPPER2(void *, memset, void *s, int c, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, n, __MF_CHECK_WRITE, "memset dest");
return memset (s, c, n);
}
WRAPPER2(int, memcmp, const void *s1, const void *s2, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s1, n, __MF_CHECK_READ, "memcmp 1st arg");
MF_VALIDATE_EXTENT(s2, n, __MF_CHECK_READ, "memcmp 2nd arg");
return memcmp (s1, s2, n);
}
WRAPPER2(void *, memchr, const void *s, int c, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, n, __MF_CHECK_READ, "memchr region");
return memchr (s, c, n);
}
#ifdef HAVE_MEMRCHR
WRAPPER2(void *, memrchr, const void *s, int c, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, n, __MF_CHECK_READ, "memrchr region");
return memrchr (s, c, n);
}
#endif
WRAPPER2(char *, strcpy, char *dest, const char *src)
{
/* nb: just because strlen(src) == n doesn't mean (src + n) or (src + n +
1) are valid pointers. the allocated object might have size < n.
check anyways. */
size_t n = strlen (src);
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(src, CLAMPADD(n, 1), __MF_CHECK_READ, "strcpy src");
MF_VALIDATE_EXTENT(dest, CLAMPADD(n, 1), __MF_CHECK_WRITE, "strcpy dest");
return strcpy (dest, src);
}
#ifdef HAVE_STRNCPY
WRAPPER2(char *, strncpy, char *dest, const char *src, size_t n)
{
size_t len = strnlen (src, n);
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(src, len, __MF_CHECK_READ, "strncpy src");
MF_VALIDATE_EXTENT(dest, len, __MF_CHECK_WRITE, "strncpy dest"); /* nb: strNcpy */
return strncpy (dest, src, n);
}
#endif
WRAPPER2(char *, strcat, char *dest, const char *src)
{
size_t dest_sz;
size_t src_sz;
TRACE ("%s\n", __PRETTY_FUNCTION__);
dest_sz = strlen (dest);
src_sz = strlen (src);
MF_VALIDATE_EXTENT(src, CLAMPADD(src_sz, 1), __MF_CHECK_READ, "strcat src");
MF_VALIDATE_EXTENT(dest, CLAMPADD(dest_sz, CLAMPADD(src_sz, 1)),
__MF_CHECK_WRITE, "strcat dest");
return strcat (dest, src);
}
WRAPPER2(char *, strncat, char *dest, const char *src, size_t n)
{
/* nb: validating the extents (s,n) might be a mistake for two reasons.
(1) the string s might be shorter than n chars, and n is just a
poor choice by the programmer. this is not a "true" error in the
sense that the call to strncat would still be ok.
(2) we could try to compensate for case (1) by calling strlen(s) and
using that as a bound for the extent to verify, but strlen might fall off
the end of a non-terminated string, leading to a false positive.
so we will call strnlen(s,n) and use that as a bound.
if strnlen returns a length beyond the end of the registered extent
associated with s, there is an error: the programmer's estimate for n is
too large _AND_ the string s is unterminated, in which case they'd be
about to touch memory they don't own while calling strncat.
this same logic applies to further uses of strnlen later down in this
file. */
size_t src_sz;
size_t dest_sz;
TRACE ("%s\n", __PRETTY_FUNCTION__);
src_sz = strnlen (src, n);
dest_sz = strnlen (dest, n);
MF_VALIDATE_EXTENT(src, src_sz, __MF_CHECK_READ, "strncat src");
MF_VALIDATE_EXTENT(dest, (CLAMPADD(dest_sz, CLAMPADD(src_sz, 1))),
__MF_CHECK_WRITE, "strncat dest");
return strncat (dest, src, n);
}
WRAPPER2(int, strcmp, const char *s1, const char *s2)
{
size_t s1_sz;
size_t s2_sz;
TRACE ("%s\n", __PRETTY_FUNCTION__);
s1_sz = strlen (s1);
s2_sz = strlen (s2);
MF_VALIDATE_EXTENT(s1, CLAMPADD(s1_sz, 1), __MF_CHECK_READ, "strcmp 1st arg");
MF_VALIDATE_EXTENT(s2, CLAMPADD(s2_sz, 1), __MF_CHECK_WRITE, "strcmp 2nd arg");
return strcmp (s1, s2);
}
WRAPPER2(int, strcasecmp, const char *s1, const char *s2)
{
size_t s1_sz;
size_t s2_sz;
TRACE ("%s\n", __PRETTY_FUNCTION__);
s1_sz = strlen (s1);
s2_sz = strlen (s2);
MF_VALIDATE_EXTENT(s1, CLAMPADD(s1_sz, 1), __MF_CHECK_READ, "strcasecmp 1st arg");
MF_VALIDATE_EXTENT(s2, CLAMPADD(s2_sz, 1), __MF_CHECK_READ, "strcasecmp 2nd arg");
return strcasecmp (s1, s2);
}
WRAPPER2(int, strncmp, const char *s1, const char *s2, size_t n)
{
size_t s1_sz;
size_t s2_sz;
TRACE ("%s\n", __PRETTY_FUNCTION__);
s1_sz = strnlen (s1, n);
s2_sz = strnlen (s2, n);
MF_VALIDATE_EXTENT(s1, s1_sz, __MF_CHECK_READ, "strncmp 1st arg");
MF_VALIDATE_EXTENT(s2, s2_sz, __MF_CHECK_READ, "strncmp 2nd arg");
return strncmp (s1, s2, n);
}
WRAPPER2(int, strncasecmp, const char *s1, const char *s2, size_t n)
{
size_t s1_sz;
size_t s2_sz;
TRACE ("%s\n", __PRETTY_FUNCTION__);
s1_sz = strnlen (s1, n);
s2_sz = strnlen (s2, n);
MF_VALIDATE_EXTENT(s1, s1_sz, __MF_CHECK_READ, "strncasecmp 1st arg");
MF_VALIDATE_EXTENT(s2, s2_sz, __MF_CHECK_READ, "strncasecmp 2nd arg");
return strncasecmp (s1, s2, n);
}
WRAPPER2(char *, strdup, const char *s)
{
DECLARE(void *, malloc, size_t sz);
char *result;
size_t n = strlen (s);
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, CLAMPADD(n,1), __MF_CHECK_READ, "strdup region");
result = (char *)CALL_REAL(malloc,
CLAMPADD(CLAMPADD(n,1),
CLAMPADD(__mf_opts.crumple_zone,
__mf_opts.crumple_zone)));
if (UNLIKELY(! result)) return result;
result += __mf_opts.crumple_zone;
memcpy (result, s, n);
result[n] = '\0';
__mf_register (result, CLAMPADD(n,1), __MF_TYPE_HEAP_I, "strdup region");
return result;
}
WRAPPER2(char *, strndup, const char *s, size_t n)
{
DECLARE(void *, malloc, size_t sz);
char *result;
size_t sz = strnlen (s, n);
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, sz, __MF_CHECK_READ, "strndup region"); /* nb: strNdup */
/* note: strndup still adds a \0, even with the N limit! */
result = (char *)CALL_REAL(malloc,
CLAMPADD(CLAMPADD(n,1),
CLAMPADD(__mf_opts.crumple_zone,
__mf_opts.crumple_zone)));
if (UNLIKELY(! result)) return result;
result += __mf_opts.crumple_zone;
memcpy (result, s, n);
result[n] = '\0';
__mf_register (result, CLAMPADD(n,1), __MF_TYPE_HEAP_I, "strndup region");
return result;
}
WRAPPER2(char *, strchr, const char *s, int c)
{
size_t n;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (s);
MF_VALIDATE_EXTENT(s, CLAMPADD(n,1), __MF_CHECK_READ, "strchr region");
return strchr (s, c);
}
WRAPPER2(char *, strrchr, const char *s, int c)
{
size_t n;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (s);
MF_VALIDATE_EXTENT(s, CLAMPADD(n,1), __MF_CHECK_READ, "strrchr region");
return strrchr (s, c);
}
WRAPPER2(char *, strstr, const char *haystack, const char *needle)
{
size_t haystack_sz;
size_t needle_sz;
TRACE ("%s\n", __PRETTY_FUNCTION__);
haystack_sz = strlen (haystack);
needle_sz = strlen (needle);
MF_VALIDATE_EXTENT(haystack, CLAMPADD(haystack_sz, 1), __MF_CHECK_READ, "strstr haystack");
MF_VALIDATE_EXTENT(needle, CLAMPADD(needle_sz, 1), __MF_CHECK_READ, "strstr needle");
return strstr (haystack, needle);
}
#ifdef HAVE_MEMMEM
WRAPPER2(void *, memmem,
const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(haystack, haystacklen, __MF_CHECK_READ, "memmem haystack");
MF_VALIDATE_EXTENT(needle, needlelen, __MF_CHECK_READ, "memmem needle");
return memmem (haystack, haystacklen, needle, needlelen);
}
#endif
WRAPPER2(size_t, strlen, const char *s)
{
size_t result = strlen (s);
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, CLAMPADD(result, 1), __MF_CHECK_READ, "strlen region");
return result;
}
WRAPPER2(size_t, strnlen, const char *s, size_t n)
{
size_t result = strnlen (s, n);
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, result, __MF_CHECK_READ, "strnlen region");
return result;
}
WRAPPER2(void, bzero, void *s, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, n, __MF_CHECK_WRITE, "bzero region");
bzero (s, n);
}
#undef bcopy
WRAPPER2(void, bcopy, const void *src, void *dest, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(src, n, __MF_CHECK_READ, "bcopy src");
MF_VALIDATE_EXTENT(dest, n, __MF_CHECK_WRITE, "bcopy dest");
bcopy (src, dest, n);
}
#undef bcmp
WRAPPER2(int, bcmp, const void *s1, const void *s2, size_t n)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s1, n, __MF_CHECK_READ, "bcmp 1st arg");
MF_VALIDATE_EXTENT(s2, n, __MF_CHECK_READ, "bcmp 2nd arg");
return bcmp (s1, s2, n);
}
WRAPPER2(char *, index, const char *s, int c)
{
size_t n = strlen (s);
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, CLAMPADD(n, 1), __MF_CHECK_READ, "index region");
return index (s, c);
}
WRAPPER2(char *, rindex, const char *s, int c)
{
size_t n = strlen (s);
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(s, CLAMPADD(n, 1), __MF_CHECK_READ, "rindex region");
return rindex (s, c);
}
/* XXX: stpcpy, memccpy */
/* XXX: *printf,*scanf */
/* XXX: setjmp, longjmp */
WRAPPER2(char *, asctime, struct tm *tm)
{
static char *reg_result = NULL;
char *result;
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(tm, sizeof (struct tm), __MF_CHECK_READ, "asctime tm");
result = asctime (tm);
if (reg_result == NULL)
{
__mf_register (result, strlen (result)+1, __MF_TYPE_STATIC, "asctime string");
reg_result = result;
}
return result;
}
WRAPPER2(char *, ctime, const time_t *timep)
{
static char *reg_result = NULL;
char *result;
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(timep, sizeof (time_t), __MF_CHECK_READ, "ctime time");
result = ctime (timep);
if (reg_result == NULL)
{
/* XXX: what if asctime and ctime return the same static ptr? */
__mf_register (result, strlen (result)+1, __MF_TYPE_STATIC, "ctime string");
reg_result = result;
}
return result;
}
WRAPPER2(struct tm*, localtime, const time_t *timep)
{
static struct tm *reg_result = NULL;
struct tm *result;
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(timep, sizeof (time_t), __MF_CHECK_READ, "localtime time");
result = localtime (timep);
if (reg_result == NULL)
{
__mf_register (result, sizeof (struct tm), __MF_TYPE_STATIC, "localtime tm");
reg_result = result;
}
return result;
}
WRAPPER2(struct tm*, gmtime, const time_t *timep)
{
static struct tm *reg_result = NULL;
struct tm *result;
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT(timep, sizeof (time_t), __MF_CHECK_READ, "gmtime time");
result = gmtime (timep);
if (reg_result == NULL)
{
__mf_register (result, sizeof (struct tm), __MF_TYPE_STATIC, "gmtime tm");
reg_result = result;
}
return result;
}
/* EL start */
/* The following indicate if the result of the corresponding function
* should be explicitly un/registered by the wrapper
*/
#ifdef __FreeBSD__
#define MF_REGISTER_fopen __MF_TYPE_STATIC
#else
#undef MF_REGISTER_fopen
#endif
#define MF_RESULT_SIZE_fopen (sizeof (FILE))
#undef MF_REGISTER_opendir
#define MF_RESULT_SIZE_opendir 0 /* (sizeof (DIR)) */
#undef MF_REGISTER_readdir
#define MF_REGISTER_gethostbyname __MF_TYPE_STATIC
#undef MF_REGISTER_gethostbyname_items
#undef MF_REGISTER_dlopen
#undef MF_REGISTER_dlerror
#undef MF_REGISTER_dlsym
#define MF_REGISTER_shmat __MF_TYPE_GUESS
#include <time.h>
WRAPPER2(time_t, time, time_t *timep)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
if (NULL != timep)
MF_VALIDATE_EXTENT (timep, sizeof (*timep), __MF_CHECK_WRITE,
"time timep");
return time (timep);
}
WRAPPER2(char *, strerror, int errnum)
{
char *p;
static char * last_strerror = NULL;
TRACE ("%s\n", __PRETTY_FUNCTION__);
p = strerror (errnum);
if (last_strerror != NULL)
__mf_unregister (last_strerror, 0, __MF_TYPE_STATIC);
if (NULL != p)
__mf_register (p, strlen (p) + 1, __MF_TYPE_STATIC, "strerror result");
last_strerror = p;
return p;
}
/* An auxiliary data structure for tracking the hand-made stdio
buffers we generate during the fopen/fopen64 hooks. In a civilized
language, this would be a simple dynamically sized FILE*->char*
lookup table, but this is C and we get to do it by hand. */
struct mf_filebuffer
{
FILE *file;
char *buffer;
struct mf_filebuffer *next;
};
static struct mf_filebuffer *mf_filebuffers = NULL;
static void
mkbuffer (FILE *f)
{
/* Reset any buffer automatically provided by libc, since this may
have been done via mechanisms that libmudflap couldn't
intercept. */
int rc;
size_t bufsize = BUFSIZ;
int bufmode;
char *buffer = malloc (bufsize);
struct mf_filebuffer *b = malloc (sizeof (struct mf_filebuffer));
assert ((buffer != NULL) && (b != NULL));
/* Link it into list. */
b->file = f;
b->buffer = buffer;
b->next = mf_filebuffers;
mf_filebuffers = b;
/* Determine how the file is supposed to be buffered at the moment. */
bufmode = fileno (f) == 2 ? _IONBF : (isatty (fileno (f)) ? _IOLBF : _IOFBF);
rc = setvbuf (f, buffer, bufmode, bufsize);
assert (rc == 0);
}
static void
unmkbuffer (FILE *f)
{
struct mf_filebuffer *b = mf_filebuffers;
struct mf_filebuffer **pb = & mf_filebuffers;
while (b != NULL)
{
if (b->file == f)
{
*pb = b->next;
free (b->buffer);
free (b);
return;
}
pb = & b->next;
b = b->next;
}
}
WRAPPER2(FILE *, fopen, const char *path, const char *mode)
{
size_t n;
FILE *p;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (path);
MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "fopen path");
n = strlen (mode);
MF_VALIDATE_EXTENT (mode, CLAMPADD(n, 1), __MF_CHECK_READ, "fopen mode");
p = fopen (path, mode);
if (NULL != p) {
#ifdef MF_REGISTER_fopen
__mf_register (p, sizeof (*p), MF_REGISTER_fopen, "fopen result");
#endif
MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_WRITE, "fopen result");
mkbuffer (p);
}
return p;
}
WRAPPER2(int, setvbuf, FILE *stream, char *buf, int mode, size_t size)
{
int rc = 0;
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE, "setvbuf stream");
unmkbuffer (stream);
if (buf != NULL)
MF_VALIDATE_EXTENT (buf, size, __MF_CHECK_WRITE, "setvbuf buffer");
/* Override the user only if it's an auto-allocated buffer request. Otherwise
assume that the supplied buffer is already known to libmudflap. */
if ((buf == NULL) && ((mode == _IOFBF) || (mode == _IOLBF)))
mkbuffer (stream);
else
rc = setvbuf (stream, buf, mode, size);
return rc;
}
#ifdef HAVE_SETBUF
WRAPPER2(int, setbuf, FILE* stream, char *buf)
{
return __mfwrap_setvbuf (stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);
}
#endif
#ifdef HAVE_SETBUFFER
WRAPPER2(int, setbuffer, FILE* stream, char *buf, size_t sz)
{
return __mfwrap_setvbuf (stream, buf, buf ? _IOFBF : _IONBF, sz);
}
#endif
#ifdef HAVE_SETLINEBUF
WRAPPER2(int, setlinebuf, FILE* stream)
{
return __mfwrap_setvbuf(stream, NULL, _IOLBF, 0);
}
#endif
WRAPPER2(FILE *, fdopen, int fd, const char *mode)
{
size_t n;
FILE *p;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (mode);
MF_VALIDATE_EXTENT (mode, CLAMPADD(n, 1), __MF_CHECK_READ, "fdopen mode");
p = fdopen (fd, mode);
if (NULL != p) {
#ifdef MF_REGISTER_fopen
__mf_register (p, sizeof (*p), MF_REGISTER_fopen, "fdopen result");
#endif
MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_WRITE, "fdopen result");
mkbuffer (p);
}
return p;
}
WRAPPER2(FILE *, freopen, const char *path, const char *mode, FILE *s)
{
size_t n;
FILE *p;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (path);
MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "freopen path");
MF_VALIDATE_EXTENT (s, (sizeof (*s)), __MF_CHECK_WRITE, "freopen stream");
unmkbuffer (s);
n = strlen (mode);
MF_VALIDATE_EXTENT (mode, CLAMPADD(n, 1), __MF_CHECK_READ, "freopen mode");
p = freopen (path, mode, s);
if (NULL != p) {
#ifdef MF_REGISTER_fopen
__mf_register (p, sizeof (*p), MF_REGISTER_fopen, "freopen result");
#endif
MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_WRITE, "freopen result");
mkbuffer (p);
}
return p;
}
#ifdef HAVE_FOPEN64
WRAPPER2(FILE *, fopen64, const char *path, const char *mode)
{
size_t n;
FILE *p;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (path);
MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "fopen64 path");
n = strlen (mode);
MF_VALIDATE_EXTENT (mode, CLAMPADD(n, 1), __MF_CHECK_READ, "fopen64 mode");
p = fopen64 (path, mode);
if (NULL != p) {
#ifdef MF_REGISTER_fopen
__mf_register (p, sizeof (*p), MF_REGISTER_fopen, "fopen64 result");
#endif
MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_WRITE, "fopen64 result");
mkbuffer (p);
}
return p;
}
#endif
#ifdef HAVE_FREOPEN64
WRAPPER2(FILE *, freopen64, const char *path, const char *mode, FILE *s)
{
size_t n;
FILE *p;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (path);
MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "freopen64 path");
MF_VALIDATE_EXTENT (s, (sizeof (*s)), __MF_CHECK_WRITE, "freopen64 stream");
unmkbuffer (s);
n = strlen (mode);
MF_VALIDATE_EXTENT (mode, CLAMPADD(n, 1), __MF_CHECK_READ, "freopen64 mode");
p = freopen (path, mode, s);
if (NULL != p) {
#ifdef MF_REGISTER_fopen
__mf_register (p, sizeof (*p), MF_REGISTER_fopen, "freopen64 result");
#endif
MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_WRITE, "freopen64 result");
mkbuffer (p);
}
return p;
}
#endif
WRAPPER2(int, fclose, FILE *stream)
{
int resp;
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"fclose stream");
resp = fclose (stream);
#ifdef MF_REGISTER_fopen
__mf_unregister (stream, sizeof (*stream), MF_REGISTER_fopen);
#endif
unmkbuffer (stream);
return resp;
}
WRAPPER2(size_t, fread, void *ptr, size_t size, size_t nmemb, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"fread stream");
MF_VALIDATE_EXTENT (ptr, size * nmemb, __MF_CHECK_WRITE, "fread buffer");
return fread (ptr, size, nmemb, stream);
}
WRAPPER2(size_t, fwrite, const void *ptr, size_t size, size_t nmemb,
FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"fwrite stream");
MF_VALIDATE_EXTENT (ptr, size * nmemb, __MF_CHECK_READ, "fwrite buffer");
return fwrite (ptr, size, nmemb, stream);
}
WRAPPER2(int, fgetc, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"fgetc stream");
return fgetc (stream);
}
WRAPPER2(char *, fgets, char *s, int size, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"fgets stream");
MF_VALIDATE_EXTENT (s, size, __MF_CHECK_WRITE, "fgets buffer");
return fgets (s, size, stream);
}
WRAPPER2(int, getc, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"getc stream");
return getc (stream);
}
WRAPPER2(char *, gets, char *s)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (s, 1, __MF_CHECK_WRITE, "gets buffer");
/* Avoid link-time warning... */
s = fgets (s, INT_MAX, stdin);
if (NULL != s) { /* better late than never */
size_t n = strlen (s);
MF_VALIDATE_EXTENT (s, CLAMPADD(n, 1), __MF_CHECK_WRITE, "gets buffer");
}
return s;
}
WRAPPER2(int, ungetc, int c, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"ungetc stream");
return ungetc (c, stream);
}
WRAPPER2(int, fputc, int c, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"fputc stream");
return fputc (c, stream);
}
WRAPPER2(int, fputs, const char *s, FILE *stream)
{
size_t n;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (s);
MF_VALIDATE_EXTENT (s, CLAMPADD(n, 1), __MF_CHECK_READ, "fputs buffer");
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"fputs stream");
return fputs (s, stream);
}
WRAPPER2(int, putc, int c, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"putc stream");
return putc (c, stream);
}
WRAPPER2(int, puts, const char *s)
{
size_t n;
TRACE ("%s\n", __PRETTY_FUNCTION__);
n = strlen (s);
MF_VALIDATE_EXTENT (s, CLAMPADD(n, 1), __MF_CHECK_READ, "puts buffer");
return puts (s);
}
WRAPPER2(void, clearerr, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"clearerr stream");
clearerr (stream);
}
WRAPPER2(int, feof, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"feof stream");
return feof (stream);
}
WRAPPER2(int, ferror, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"ferror stream");
return ferror (stream);
}
WRAPPER2(int, fileno, FILE *stream)
{
TRACE ("%s\n", __PRETTY_FUNCTION__);
MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
"fileno stream");
return fileno (stream);
}
WRAPPER2(int, printf, const char *format, ...)