summaryrefslogtreecommitdiff
path: root/libmudflap/mf-hooks2.c
blob: e031b43f4c90c7c427867e2d787f73407d80a087 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
/* Mudflap: narrow-pointer bounds-checking by tree rewriting.
   Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
   Contributed by Frank Ch. Eigler <fche@redhat.com>
   and Graydon Hoare <graydon@redhat.com>

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, 59 Temple Place - Suite 330, Boston, MA
02111-1307, 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_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


#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
*/
#define MF_REGISTER_strerror		__MF_TYPE_STATIC
#undef  MF_REGISTER_fopen
#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;
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  p = strerror (errnum);
  if (NULL != p) {
    n = strlen (p);
    n = CLAMPADD(n, 1);
#ifdef MF_REGISTER_strerror
    __mf_register (p, n, MF_REGISTER_strerror, "strerror result");
#endif
    MF_VALIDATE_EXTENT (p, n, __MF_CHECK_WRITE, "strerror result");
  }
  return p;
}


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");
  }

  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");
  }

  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

  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, ...)
{
  size_t n;
  va_list ap;
  int result;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (format);
  MF_VALIDATE_EXTENT (format, CLAMPADD(n, 1), __MF_CHECK_READ,
    "printf format");
  va_start (ap, format);
  result = vprintf (format, ap);
  va_end (ap);
  return result;
}


WRAPPER2(int, fprintf, FILE *stream, const char *format, ...)
{
  size_t n;
  va_list ap;
  int result;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "fprintf stream");
  n = strlen (format);
  MF_VALIDATE_EXTENT (format, CLAMPADD(n, 1), __MF_CHECK_READ,
    "fprintf format");
  va_start (ap, format);
  result = vfprintf (stream, format, ap);
  va_end (ap);
  return result;
}


WRAPPER2(int, sprintf, char *str, const char *format, ...)
{
  size_t n;
  va_list ap;
  int result;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (str, 1, __MF_CHECK_WRITE, "sprintf str");
  n = strlen (format);
  MF_VALIDATE_EXTENT (format, CLAMPADD(n, 1), __MF_CHECK_READ,
    "sprintf format");
  va_start (ap, format);
  result = vsprintf (str, format, ap);
  va_end (ap);
  n = strlen (str);
  MF_VALIDATE_EXTENT (str, CLAMPADD(n, 1), __MF_CHECK_WRITE, "sprintf str");
  return result;
}


WRAPPER2(int, snprintf, char *str, size_t size, const char *format, ...)
{
  size_t n;
  va_list ap;
  int result;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (str, size, __MF_CHECK_WRITE, "snprintf str");
  n = strlen (format);
  MF_VALIDATE_EXTENT (format, CLAMPADD(n, 1), __MF_CHECK_READ,
    "snprintf format");
  va_start (ap, format);
  result = vsnprintf (str, size, format, ap);
  va_end (ap);
  return result;
}


WRAPPER2(int, vprintf,  const char *format, va_list ap)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (format);
  MF_VALIDATE_EXTENT (format, CLAMPADD(n, 1), __MF_CHECK_READ,
    "vprintf format");
  return vprintf (format, ap);
}


WRAPPER2(int, vfprintf, FILE *stream, const char *format, va_list ap)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "vfprintf stream");
  n = strlen (format);
  MF_VALIDATE_EXTENT (format, CLAMPADD(n, 1), __MF_CHECK_READ,
    "vfprintf format");
  return vfprintf (stream, format, ap);
}


WRAPPER2(int, vsprintf, char *str, const char *format, va_list ap)
{
  size_t n;
  int result;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (str, 1, __MF_CHECK_WRITE, "vsprintf str");
  n = strlen (format);
  MF_VALIDATE_EXTENT (format, CLAMPADD(n, 1), __MF_CHECK_READ,
    "vsprintf format");
  result = vsprintf (str, format, ap);
  n = strlen (str);
  MF_VALIDATE_EXTENT (str, CLAMPADD(n, 1), __MF_CHECK_WRITE, "vsprintf str");
  return result;
}


WRAPPER2(int, vsnprintf, char *str, size_t size, const char *format,
	va_list ap)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (str, size, __MF_CHECK_WRITE, "vsnprintf str");
  n = strlen (format);
  MF_VALIDATE_EXTENT (format, CLAMPADD(n, 1), __MF_CHECK_READ,
    "vsnprintf format");
  return vsnprintf (str, size, format, ap);
}


WRAPPER2(int , access, const char *path, int mode)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "access path");
  return access (path, mode);
}


WRAPPER2(int , remove, const char *path)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "remove path");
  return remove (path);
}


WRAPPER2(int, fflush, FILE *stream)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "fflush stream");
  return fflush (stream);
}


WRAPPER2(int, fseek, FILE *stream, long offset, int whence)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "fseek stream");
  return fseek (stream, offset, whence);
}


#ifdef HAVE_FSEEKO64
WRAPPER2(int, fseeko64, FILE *stream, off64_t offset, int whence)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "fseeko64 stream");
  return fseeko64 (stream, offset, whence);
}
#endif


WRAPPER2(long, ftell, FILE *stream)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "ftell stream");
  return ftell (stream);
}


#ifdef HAVE_FTELLO64
WRAPPER2(off64_t, ftello64, FILE *stream)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "ftello64 stream");
  return ftello64 (stream);
}
#endif


WRAPPER2(void, rewind, FILE *stream)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "rewind stream");
  rewind (stream);
}


WRAPPER2(int, fgetpos, FILE *stream, fpos_t *pos)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "fgetpos stream");
  MF_VALIDATE_EXTENT (pos, sizeof (*pos), __MF_CHECK_WRITE, "fgetpos pos");
  return fgetpos (stream, pos);
}


WRAPPER2(int, fsetpos, FILE *stream, fpos_t *pos)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "fsetpos stream");
  MF_VALIDATE_EXTENT (pos, sizeof (*pos), __MF_CHECK_READ, "fsetpos pos");
  return fsetpos (stream, pos);
}


WRAPPER2(int , stat, const char *path, struct stat *buf)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "stat path");
  MF_VALIDATE_EXTENT (buf, sizeof (*buf), __MF_CHECK_READ, "stat buf");
  return stat (path, buf);
}


#ifdef HAVE_STAT64
WRAPPER2(int , stat64, const char *path, struct stat64 *buf)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "stat64 path");
  MF_VALIDATE_EXTENT (buf, sizeof (*buf), __MF_CHECK_READ, "stat64 buf");
  return stat64 (path, buf);
}
#endif


WRAPPER2(int , fstat, int filedes, struct stat *buf)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (buf, sizeof (*buf), __MF_CHECK_READ, "fstat buf");
  return fstat (filedes, buf);
}


WRAPPER2(int , lstat, const char *path, struct stat *buf)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "lstat path");
  MF_VALIDATE_EXTENT (buf, sizeof (*buf), __MF_CHECK_READ, "lstat buf");
  return lstat (path, buf);
}


WRAPPER2(int , mkfifo, const char *path, mode_t mode)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "mkfifo path");
  return mkfifo (path, mode);
}


WRAPPER2(int, setvbuf, FILE *stream, char *buf, int mode , size_t size)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "setvbuf stream");
  if (NULL != buf)
    MF_VALIDATE_EXTENT (buf, size, __MF_CHECK_READ, "setvbuf buf");
  return setvbuf (stream, buf, mode, size);
}


WRAPPER2(void, setbuf, FILE *stream, char *buf)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "setbuf stream");
  if (NULL != buf)
    MF_VALIDATE_EXTENT (buf, BUFSIZ, __MF_CHECK_READ, "setbuf buf");
  setbuf (stream, buf);
}


#ifdef HAVE_DIRENT_H
WRAPPER2(DIR *, opendir, const char *path)
{
  DIR *p;
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "opendir path");

  p = opendir (path);
  if (NULL != p) {
#ifdef MF_REGISTER_opendir
    __mf_register (p, MF_RESULT_SIZE_opendir, MF_REGISTER_opendir,
      "opendir result");
#endif
    MF_VALIDATE_EXTENT (p, MF_RESULT_SIZE_opendir, __MF_CHECK_WRITE,
      "opendir result");
  }
  return p;
}


WRAPPER2(int, closedir, DIR *dir)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (dir, 0, __MF_CHECK_WRITE, "closedir dir");
#ifdef MF_REGISTER_opendir
  __mf_unregister (dir, MF_RESULT_SIZE_opendir, MF_REGISTER_opendir);
#endif
  return closedir (dir);
}


WRAPPER2(struct dirent *, readdir, DIR *dir)
{
  struct dirent *p;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (dir, 0, __MF_CHECK_READ, "readdir dir");
  p = readdir (dir);
  if (NULL != p) {
#ifdef MF_REGISTER_readdir
    __mf_register (p, sizeof (*p), MF_REGISTER_readdir, "readdir result");
#endif
    MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_WRITE, "readdir result");
  }
  return p;
}
#endif


#ifdef HAVE_SYS_SOCKET_H

WRAPPER2(int, recv, int s, void *buf, size_t len, int flags)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (buf, len, __MF_CHECK_WRITE, "recv buf");
  return recv (s, buf, len, flags);
}


WRAPPER2(int, recvfrom, int s, void *buf, size_t len, int flags,
		struct sockaddr *from, socklen_t *fromlen)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (buf, len, __MF_CHECK_WRITE, "recvfrom buf");
  MF_VALIDATE_EXTENT (from, (size_t)*fromlen, __MF_CHECK_WRITE,
    "recvfrom from");
  return recvfrom (s, buf, len, flags, from, fromlen);
}


WRAPPER2(int, recvmsg, int s, struct msghdr *msg, int flags)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (msg, sizeof (*msg), __MF_CHECK_WRITE, "recvmsg msg");
  return recvmsg (s, msg, flags);
}


WRAPPER2(int, send, int s, const void *msg, size_t len, int flags)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (msg, len, __MF_CHECK_READ, "send msg");
  return send (s, msg, len, flags);
}


WRAPPER2(int, sendto, int s, const void *msg, size_t len, int flags,
		const struct sockaddr *to, socklen_t tolen)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (msg, len, __MF_CHECK_READ, "sendto msg");
  MF_VALIDATE_EXTENT (to, (size_t)tolen, __MF_CHECK_WRITE, "sendto to");
  return sendto (s, msg, len, flags, to, tolen);
}


WRAPPER2(int, sendmsg, int s, const void *msg, int flags)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (msg, sizeof (*msg), __MF_CHECK_READ, "sendmsg msg");
  return sendmsg (s, msg, flags);
}


WRAPPER2(int, setsockopt, int s, int level, int optname, const void *optval,
	socklen_t optlen)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (optval, (size_t)optlen, __MF_CHECK_READ,
    "setsockopt optval");
  return setsockopt (s, level, optname, optval, optlen);
}


WRAPPER2(int, getsockopt, int s, int level, int optname, void *optval,
		socklen_t *optlen)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (optval, (size_t)*optlen, __MF_CHECK_WRITE,
    "getsockopt optval");
  return getsockopt (s, level, optname, optval, optlen);
}


WRAPPER2(int, accept, int s, struct  sockaddr *addr, socklen_t *addrlen)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (addr, (size_t)*addrlen, __MF_CHECK_WRITE, "accept addr");
  return accept (s, addr, addrlen);
}


WRAPPER2(int, bind, int sockfd, struct  sockaddr *addr, socklen_t addrlen)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (addr, (size_t)addrlen, __MF_CHECK_WRITE, "bind addr");
  return bind (sockfd, addr, addrlen);
}


WRAPPER2(int, connect, int sockfd, const struct sockaddr  *addr,
	socklen_t addrlen)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (addr, (size_t)addrlen, __MF_CHECK_READ,
    "connect addr");
  return connect (sockfd, addr, addrlen);
}

#endif /* HAVE_SYS_SOCKET_H */


WRAPPER2(int, gethostname, char *name, size_t len)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (name, len, __MF_CHECK_WRITE, "gethostname name");
  return gethostname (name, len);
}


#ifdef HAVE_SETHOSTNAME
WRAPPER2(int, sethostname, const char *name, size_t len)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (name, len, __MF_CHECK_READ, "sethostname name");
  return sethostname (name, len);
}
#endif


#ifdef HAVE_NETDB_H

WRAPPER2(struct hostent *, gethostbyname, const char *name)
{
  struct hostent *p;
  char **ss;
  char *s;
  size_t n;
  int nreg;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (name);
  MF_VALIDATE_EXTENT (name, CLAMPADD(n, 1), __MF_CHECK_READ,
    "gethostbyname name");
  p = gethostbyname (name);
  if (NULL != p) {
#ifdef MF_REGISTER_gethostbyname
    __mf_register (p, sizeof (*p), MF_REGISTER_gethostbyname,
      "gethostbyname result");
#endif
    MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_WRITE,
      "gethostbyname result");
    if (NULL != (s = p->h_name)) {
      n = strlen (s);
      n = CLAMPADD(n, 1);
#ifdef MF_REGISTER_gethostbyname_items
      __mf_register (s, n, MF_REGISTER_gethostbyname_items,
        "gethostbyname result->h_name");
#endif
      MF_VALIDATE_EXTENT (s, n, __MF_CHECK_WRITE,
        "gethostbyname result->h_name");
    }

    if (NULL != (ss = p->h_aliases)) {
      for (nreg = 1;; ++nreg) {
        s = *ss++;
        if (NULL == s)
          break;
        n = strlen (s);
        n = CLAMPADD(n, 1);
#ifdef MF_REGISTER_gethostbyname_items
        __mf_register (s, n, MF_REGISTER_gethostbyname_items,
          "gethostbyname result->h_aliases[]");
#endif
        MF_VALIDATE_EXTENT (s, n, __MF_CHECK_WRITE,
          "gethostbyname result->h_aliases[]");
      }
      nreg *= sizeof (*p->h_aliases);
#ifdef MF_REGISTER_gethostbyname_items
      __mf_register (p->h_aliases, nreg, MF_REGISTER_gethostbyname_items,
        "gethostbyname result->h_aliases");
#endif
      MF_VALIDATE_EXTENT (p->h_aliases, nreg, __MF_CHECK_WRITE,
        "gethostbyname result->h_aliases");
    }

    if (NULL != (ss = p->h_addr_list)) {
      for (nreg = 1;; ++nreg) {
        s = *ss++;
        if (NULL == s)
          break;
#ifdef MF_REGISTER_gethostbyname_items
        __mf_register (s, p->h_length, MF_REGISTER_gethostbyname_items,
          "gethostbyname result->h_addr_list[]");
#endif
        MF_VALIDATE_EXTENT (s, p->h_length, __MF_CHECK_WRITE,
          "gethostbyname result->h_addr_list[]");
      }
      nreg *= sizeof (*p->h_addr_list);
#ifdef MF_REGISTER_gethostbyname_items
      __mf_register (p->h_addr_list, nreg, MF_REGISTER_gethostbyname_items,
        "gethostbyname result->h_addr_list");
#endif
      MF_VALIDATE_EXTENT (p->h_addr_list, nreg, __MF_CHECK_WRITE,
        "gethostbyname result->h_addr_list");
    }
  }
  return p;
}

#endif /* HAVE_NETDB_H */


#ifdef HAVE_SYS_WAIT_H

WRAPPER2(pid_t, wait, int *status)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  if (NULL != status)
    MF_VALIDATE_EXTENT (status, sizeof (*status), __MF_CHECK_WRITE,
      "wait status");
  return wait (status);
}


WRAPPER2(pid_t, waitpid, pid_t pid, int *status, int options)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  if (NULL != status)
    MF_VALIDATE_EXTENT (status, sizeof (*status), __MF_CHECK_WRITE,
      "waitpid status");
  return waitpid (pid, status, options);
}

#endif /* HAVE_SYS_WAIT_H */


WRAPPER2(FILE *, popen, const char *command, const char *mode)
{
  size_t n;
  FILE *p;
  TRACE ("%s\n", __PRETTY_FUNCTION__);

  n = strlen (command);
  MF_VALIDATE_EXTENT (command, CLAMPADD(n, 1), __MF_CHECK_READ, "popen path");

  n = strlen (mode);
  MF_VALIDATE_EXTENT (mode, CLAMPADD(n, 1), __MF_CHECK_READ, "popen mode");

  p = popen (command, mode);
  if (NULL != p) {
#ifdef MF_REGISTER_fopen
    __mf_register (p, sizeof (*p), MF_REGISTER_fopen, "popen result");
#endif
    MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_WRITE, "popen result");
  }
  return p;
}


WRAPPER2(int, pclose, FILE *stream)
{
  int resp;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (stream, sizeof (*stream), __MF_CHECK_WRITE,
    "pclose stream");
  resp = pclose (stream);
#ifdef MF_REGISTER_fopen
  __mf_unregister (stream, sizeof (*stream), MF_REGISTER_fopen);
#endif
  return resp;
}


WRAPPER2(int, execve, const char *path, char *const argv [],
	char *const envp[])
{
  size_t n;
  char *const *p;
  const char *s;
  TRACE ("%s\n", __PRETTY_FUNCTION__);

  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "execve path");

  for (p = argv;;) {
    MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_READ, "execve *argv");
    s = *p++;
    if (NULL == s)
      break;
    n = strlen (s);
    MF_VALIDATE_EXTENT (s, CLAMPADD(n, 1), __MF_CHECK_READ, "execve **argv");
  }

  for (p = envp;;) {
    MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_READ, "execve *envp");
    s = *p++;
    if (NULL == s)
      break;
    n = strlen (s);
    MF_VALIDATE_EXTENT (s, CLAMPADD(n, 1), __MF_CHECK_READ, "execve **envp");
  }
  return execve (path, argv, envp);
}


WRAPPER2(int, execv, const char *path, char *const argv [])
{
  size_t n;
  char *const *p;
  const char *s;
  TRACE ("%s\n", __PRETTY_FUNCTION__);

  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "execv path");

  for (p = argv;;) {
    MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_READ, "execv *argv");
    s = *p++;
    if (NULL == s)
      break;
    n = strlen (s);
    MF_VALIDATE_EXTENT (s, CLAMPADD(n, 1), __MF_CHECK_READ, "execv **argv");
  }
  return execv (path, argv);
}


WRAPPER2(int, execvp, const char *path, char *const argv [])
{
  size_t n;
  char *const *p;
  const char *s;
  TRACE ("%s\n", __PRETTY_FUNCTION__);

  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "execvp path");

  for (p = argv;;) {
    MF_VALIDATE_EXTENT (p, sizeof (*p), __MF_CHECK_READ, "execvp *argv");
    s = *p++;
    if (NULL == s)
      break;
    n = strlen (s);
    MF_VALIDATE_EXTENT (s, CLAMPADD(n, 1), __MF_CHECK_READ, "execvp **argv");
  }
  return execvp (path, argv);
}


WRAPPER2(int, system, const char *string)
{
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (string);
  MF_VALIDATE_EXTENT (string, CLAMPADD(n, 1), __MF_CHECK_READ,
    "system string");
  return system (string);
}


WRAPPER2(void *, dlopen, const char *path, int flags)
{
  void *p;
  size_t n;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  n = strlen (path);
  MF_VALIDATE_EXTENT (path, CLAMPADD(n, 1), __MF_CHECK_READ, "dlopen path");
  p = dlopen (path, flags);
  if (NULL != p) {
#ifdef MF_REGISTER_dlopen
    __mf_register (p, 0, MF_REGISTER_dlopen, "dlopen result");
#endif
    MF_VALIDATE_EXTENT (p, 0, __MF_CHECK_WRITE, "dlopen result");
  }
  return p;
}


WRAPPER2(int, dlclose, void *handle)
{
  int resp;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (handle, 0, __MF_CHECK_READ, "dlclose handle");
  resp = dlclose (handle);
#ifdef MF_REGISTER_dlopen
  __mf_unregister (handle, 0, MF_REGISTER_dlopen);
#endif
  return resp;
}


WRAPPER2(char *, dlerror)
{
  char *p;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  p = dlerror ();
  if (NULL != p) {
    size_t n;
    n = strlen (p);
    n = CLAMPADD(n, 1);
#ifdef MF_REGISTER_dlerror
    __mf_register (p, n, MF_REGISTER_dlerror, "dlerror result");
#endif
    MF_VALIDATE_EXTENT (p, n, __MF_CHECK_WRITE, "dlerror result");
  }
  return p;
}


WRAPPER2(void *, dlsym, void *handle, char *symbol)
{
  size_t n;
  void *p;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (handle, 0, __MF_CHECK_READ, "dlsym handle");
  n = strlen (symbol);
  MF_VALIDATE_EXTENT (symbol, CLAMPADD(n, 1), __MF_CHECK_READ, "dlsym symbol");
  p = dlsym (handle, symbol);
  if (NULL != p) {
#ifdef MF_REGISTER_dlsym
    __mf_register (p, 0, MF_REGISTER_dlsym, "dlsym result");
#endif
    MF_VALIDATE_EXTENT (p, 0, __MF_CHECK_WRITE, "dlsym result");
  }
  return p;
}


#if defined (HAVE_SYS_IPC_H) && defined (HAVE_SYS_SEM_H) && defined (HAVE_SYS_SHM_H)

WRAPPER2(int, semop, int semid, struct sembuf *sops, unsigned nsops)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  MF_VALIDATE_EXTENT (sops, sizeof (*sops) * nsops, __MF_CHECK_READ,
    "semop sops");
  return semop (semid, sops, nsops);
}


#ifndef HAVE_UNION_SEMUN
union semun {
	int val;			/* value for SETVAL */
	struct semid_ds *buf;		/* buffer for IPC_STAT, IPC_SET */
	unsigned short int *array;	/* array for GETALL, SETALL */
	struct seminfo *__buf;		/* buffer for IPC_INFO */
};
#endif
WRAPPER2(int, semctl, int semid, int semnum, int cmd, union semun arg)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  switch (cmd) {
  case IPC_STAT:
    MF_VALIDATE_EXTENT (arg.buf, sizeof (*arg.buf), __MF_CHECK_WRITE,
      "semctl buf");
    break;
  case IPC_SET:
    MF_VALIDATE_EXTENT (arg.buf, sizeof (*arg.buf), __MF_CHECK_READ,
      "semctl buf");
    break;
  case GETALL:
    MF_VALIDATE_EXTENT (arg.array, sizeof (*arg.array), __MF_CHECK_WRITE,
      "semctl array");
  case SETALL:
    MF_VALIDATE_EXTENT (arg.array, sizeof (*arg.array), __MF_CHECK_READ,
      "semctl array");
    break;
#ifdef IPC_INFO
  /* FreeBSD 5.1 And Cygwin headers include IPC_INFO but not the __buf field.  */
#if !defined(__FreeBSD__) && !defined(__CYGWIN__)
  case IPC_INFO:
    MF_VALIDATE_EXTENT (arg.__buf, sizeof (*arg.__buf), __MF_CHECK_WRITE,
      "semctl __buf");
    break;
#endif
#endif
  default:
    break;
  }
  return semctl (semid, semnum, cmd, arg);
}


WRAPPER2(int, shmctl, int shmid, int cmd, struct shmid_ds *buf)
{
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  switch (cmd) {
  case IPC_STAT:
    MF_VALIDATE_EXTENT (buf, sizeof (*buf), __MF_CHECK_WRITE,
      "shmctl buf");
    break;
  case IPC_SET:
    MF_VALIDATE_EXTENT (buf, sizeof (*buf), __MF_CHECK_READ,
      "shmctl buf");
    break;
  default:
    break;
  }
  return shmctl (shmid, cmd, buf);
}


WRAPPER2(void *, shmat, int shmid, const void *shmaddr, int shmflg)
{
  void *p;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  p = shmat (shmid, shmaddr, shmflg);
#ifdef MF_REGISTER_shmat
  if (NULL != p) {
    struct shmid_ds buf;
    __mf_register (p, shmctl (shmid, IPC_STAT, &buf) ? 0 : buf.shm_segsz,
      MF_REGISTER_shmat, "shmat result");
  }
#endif
  return p;
}


WRAPPER2(int, shmdt, const void *shmaddr)
{
  int resp;
  TRACE ("%s\n", __PRETTY_FUNCTION__);
  resp = shmdt (shmaddr);
#ifdef MF_REGISTER_shmat
  __mf_unregister ((void *)shmaddr, 0, MF_REGISTER_shmat);
#endif
  return resp;
}


#endif /* HAVE_SYS_IPC/SEM/SHM_H */



/* ctype stuff.  This is host-specific by necessity, as the arrays
   that is used by most is*()/to*() macros are implementation-defined.  */

/* GLIBC 2.3 */
#ifdef HAVE___CTYPE_B_LOC
WRAPPER2(unsigned short **, __ctype_b_loc, void)
{
  static unsigned short * last_buf = (void *) 0;
  static unsigned short ** last_ptr = (void *) 0;
  unsigned short ** ptr = (unsigned short **) __ctype_b_loc ();
  unsigned short * buf = * ptr;
  if (ptr != last_ptr)
    {
      /* XXX: unregister last_ptr? */
      last_ptr = ptr;
      __mf_register (last_ptr, sizeof(last_ptr), __MF_TYPE_STATIC, "ctype_b_loc **");
    }
  if (buf != last_buf)
    {
      last_buf = buf;
      __mf_register ((void *) (last_buf - 128), 384 * sizeof(unsigned short), __MF_TYPE_STATIC,
                     "ctype_b_loc []");
    }
  return ptr;
}
#endif

#ifdef HAVE___CTYPE_TOUPPER_LOC
WRAPPER2(int **, __ctype_toupper_loc, void)
{
  static int * last_buf = (void *) 0;
  static int ** last_ptr = (void *) 0;
  int ** ptr = (int **) __ctype_toupper_loc ();
  int * buf = * ptr;
  if (ptr != last_ptr)
    {
      /* XXX: unregister last_ptr? */
      last_ptr = ptr;
      __mf_register (last_ptr, sizeof(last_ptr), __MF_TYPE_STATIC, "ctype_toupper_loc **");
    }
  if (buf != last_buf)
    {
      last_buf = buf;
      __mf_register ((void *) (last_buf - 128), 384 * sizeof(int), __MF_TYPE_STATIC,
                     "ctype_toupper_loc []");
    }
  return ptr;
}
#endif

#ifdef HAVE___CTYPE_TOLOWER_LOC
WRAPPER2(int **, __ctype_tolower_loc, void)
{
  static int * last_buf = (void *) 0;
  static int ** last_ptr = (void *) 0;
  int ** ptr = (int **) __ctype_tolower_loc ();
  int * buf = * ptr;
  if (ptr != last_ptr)
    {
      /* XXX: unregister last_ptr? */
      last_ptr = ptr;
      __mf_register (last_ptr, sizeof(last_ptr), __MF_TYPE_STATIC, "ctype_tolower_loc **");
    }
  if (buf != last_buf)
    {
      last_buf = buf;
      __mf_register ((void *) (last_buf - 128), 384 * sizeof(int), __MF_TYPE_STATIC,
                     "ctype_tolower_loc []");
    }
  return ptr;
}
#endif