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 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
|
/*--------------------------------------------------------------------*/
/*--- Replacements for strcpy(), memcpy() et al, which run on the ---*/
/*--- simulated CPU. ---*/
/*--- vg_replace_strmem.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Valgrind.
Copyright (C) 2000-2015 Julian Seward
jseward@acm.org
This program 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 of the
License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include "pub_tool_basics.h"
#include "pub_tool_poolalloc.h"
#include "pub_tool_hashtable.h"
#include "pub_tool_redir.h"
#include "pub_tool_tooliface.h"
#include "pub_tool_clreq.h"
/* ---------------------------------------------------------------------
We have our own versions of these functions for two reasons:
(a) it allows us to do overlap checking
(b) some of the normal versions are hyper-optimised, which fools
Memcheck and cause spurious value warnings. Our versions are
simpler.
(c) the glibc SSE-variants can read past the end of the input data
ranges. This can cause false-positive Memcheck / Helgrind / DRD
reports.
Note that overenthusiastic use of PLT bypassing by the glibc people also
means that we need to patch multiple versions of some of the functions to
our own implementations.
THEY RUN ON THE SIMD CPU!
------------------------------------------------------------------ */
/* Assignment of behavioural equivalence class tags: 2NNNP is intended
to be reserved for str/mem intercepts. Current usage:
20010 STRRCHR
20020 STRCHR
20030 STRCAT
20040 STRNCAT
20050 STRLCAT
20060 STRNLEN
20070 STRLEN
20080 STRCPY
20090 STRNCPY
20100 STRLCPY
20110 STRNCMP
20120 STRCASECMP
20130 STRNCASECMP
20140 STRCASECMP_L
20150 STRNCASECMP_L
20160 STRCMP
20170 MEMCHR
20180 MEMCPY if there's a conflict between memcpy and
20181 MEMMOVE memmove, prefer memmove
20190 MEMCMP
20200 STPCPY
20210 MEMSET
2022P unused (was previously MEMMOVE)
20230 BCOPY
20240 GLIBC25___MEMMOVE_CHK
20250 GLIBC232_STRCHRNUL
20260 GLIBC232_RAWMEMCHR
20270 GLIBC25___STRCPY_CHK
20280 GLIBC25___STPCPY_CHK
20290 GLIBC25_MEMPCPY
20300 GLIBC26___MEMCPY_CHK
20310 STRSTR
20320 STRPBRK
20330 STRCSPN
20340 STRSPN
20350 STRCASESTR
20360 MEMRCHR
20370 WCSLEN
20380 WCSCMP
20390 WCSCPY
20400 WCSCHR
20410 WCSRCHR
20420 STPNCPY
*/
#if defined(VGO_solaris)
/*
Detour functions in the libc and the runtime linker. If a function isn't
much optimized (and no overlap checking is necessary) then redir the
function only in the libc. This way we can keep stacktraces in the tests
consistent.
*/
#endif
/* Figure out if [dst .. dst+dstlen-1] overlaps with
[src .. src+srclen-1].
We assume that the address ranges do not wrap around
(which is safe since on Linux addresses >= 0xC0000000
are not accessible and the program will segfault in this
circumstance, presumably).
*/
static inline
Bool is_overlap ( void* dst, const void* src, SizeT dstlen, SizeT srclen )
{
Addr loS, hiS, loD, hiD;
if (dstlen == 0 || srclen == 0)
return False;
loS = (Addr)src;
loD = (Addr)dst;
hiS = loS + srclen - 1;
hiD = loD + dstlen - 1;
/* So figure out if [loS .. hiS] overlaps with [loD .. hiD]. */
if (loS < loD) {
return !(hiS < loD);
}
else if (loD < loS) {
return !(hiD < loS);
}
else {
/* They start at same place. Since we know neither of them has
zero length, they must overlap. */
return True;
}
}
/* Call here to exit if we can't continue. On Android we can't call
_exit for some reason, so we have to blunt-instrument it. */
__attribute__ ((__noreturn__))
static inline void my_exit ( int x )
{
# if defined(VGPV_arm_linux_android) || defined(VGPV_mips32_linux_android) \
|| defined(VGPV_arm64_linux_android)
__asm__ __volatile__(".word 0xFFFFFFFF");
while (1) {}
# elif defined(VGPV_x86_linux_android)
__asm__ __volatile__("ud2");
while (1) {}
# else
extern __attribute__ ((__noreturn__)) void _exit(int status);
_exit(x);
# endif
}
// This is a macro rather than a function because we don't want to have an
// extra function in the stack trace.
#ifndef RECORD_OVERLAP_ERROR
#define RECORD_OVERLAP_ERROR(s, src, dst, len) do { } while (0)
#endif
#ifndef VALGRIND_CHECK_VALUE_IS_DEFINED
#define VALGRIND_CHECK_VALUE_IS_DEFINED(__lvalue) 1
#endif
/*---------------------- strrchr ----------------------*/
#define STRRCHR(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20010,soname,fnname)( const char* s, int c ); \
char* VG_REPLACE_FUNCTION_EZU(20010,soname,fnname)( const char* s, int c ) \
{ \
HChar ch = (HChar)c; \
const HChar* p = s; \
const HChar* last = NULL; \
while (True) { \
if (*p == ch) last = p; \
if (*p == 0) return CONST_CAST(HChar *,last); \
p++; \
} \
}
// Apparently rindex() is the same thing as strrchr()
#if defined(VGO_linux)
STRRCHR(VG_Z_LIBC_SONAME, strrchr)
STRRCHR(VG_Z_LIBC_SONAME, rindex)
STRRCHR(VG_Z_LIBC_SONAME, __GI_strrchr)
STRRCHR(VG_Z_LIBC_SONAME, __strrchr_sse2)
STRRCHR(VG_Z_LIBC_SONAME, __strrchr_sse2_no_bsf)
STRRCHR(VG_Z_LIBC_SONAME, __strrchr_sse42)
STRRCHR(VG_Z_LD_LINUX_SO_2, rindex)
#if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
|| defined(VGPV_mips32_linux_android)
STRRCHR(NONE, __dl_strrchr); /* in /system/bin/linker */
#endif
#elif defined(VGO_darwin)
//STRRCHR(VG_Z_LIBC_SONAME, strrchr)
//STRRCHR(VG_Z_LIBC_SONAME, rindex)
//STRRCHR(VG_Z_DYLD, strrchr)
//STRRCHR(VG_Z_DYLD, rindex)
STRRCHR(VG_Z_LIBC_SONAME, strrchr)
# if DARWIN_VERS >= DARWIN_10_9
STRRCHR(libsystemZucZddylib, strrchr)
# endif
#elif defined(VGO_solaris)
STRRCHR(VG_Z_LIBC_SONAME, strrchr)
STRRCHR(VG_Z_LIBC_SONAME, rindex)
STRRCHR(VG_Z_LD_SO_1, strrchr)
#endif
/*---------------------- strchr ----------------------*/
#define STRCHR(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20020,soname,fnname) ( const char* s, int c ); \
char* VG_REPLACE_FUNCTION_EZU(20020,soname,fnname) ( const char* s, int c ) \
{ \
HChar ch = (HChar)c ; \
const HChar* p = s; \
while (True) { \
if (*p == ch) return CONST_CAST(HChar *,p); \
if (*p == 0) return NULL; \
p++; \
} \
}
// Apparently index() is the same thing as strchr()
#if defined(VGO_linux)
STRCHR(VG_Z_LIBC_SONAME, strchr)
STRCHR(VG_Z_LIBC_SONAME, __GI_strchr)
STRCHR(VG_Z_LIBC_SONAME, __strchr_sse2)
STRCHR(VG_Z_LIBC_SONAME, __strchr_sse2_no_bsf)
STRCHR(VG_Z_LIBC_SONAME, index)
# if !defined(VGP_x86_linux) && !defined(VGP_amd64_linux)
STRCHR(VG_Z_LD_LINUX_SO_2, strchr)
STRCHR(VG_Z_LD_LINUX_SO_2, index)
STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, strchr)
STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, index)
# endif
#elif defined(VGO_darwin)
STRCHR(VG_Z_LIBC_SONAME, strchr)
# if DARWIN_VERS == DARWIN_10_9
STRCHR(libsystemZuplatformZddylib, _platform_strchr)
# endif
# if DARWIN_VERS >= DARWIN_10_10
/* _platform_strchr$VARIANT$Generic */
STRCHR(libsystemZuplatformZddylib, _platform_strchr$VARIANT$Generic)
/* _platform_strchr$VARIANT$Haswell */
STRCHR(libsystemZuplatformZddylib, _platform_strchr$VARIANT$Haswell)
# endif
#elif defined(VGO_solaris)
STRCHR(VG_Z_LIBC_SONAME, strchr)
STRCHR(VG_Z_LIBC_SONAME, index)
STRCHR(VG_Z_LD_SO_1, strchr)
#endif
/*---------------------- strcat ----------------------*/
#define STRCAT(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20030,soname,fnname) \
( char* dst, const char* src ); \
char* VG_REPLACE_FUNCTION_EZU(20030,soname,fnname) \
( char* dst, const char* src ) \
{ \
const HChar* src_orig = src; \
HChar* dst_orig = dst; \
while (*dst) dst++; \
while (*src) *dst++ = *src++; \
*dst = 0; \
\
/* This is a bit redundant, I think; any overlap and the strcat will */ \
/* go forever... or until a seg fault occurs. */ \
if (is_overlap(dst_orig, \
src_orig, \
(Addr)dst-(Addr)dst_orig+1, \
(Addr)src-(Addr)src_orig+1)) \
RECORD_OVERLAP_ERROR("strcat", dst_orig, src_orig, 0); \
\
return dst_orig; \
}
#if defined(VGO_linux)
STRCAT(VG_Z_LIBC_SONAME, strcat)
STRCAT(VG_Z_LIBC_SONAME, __GI_strcat)
#elif defined(VGO_darwin)
//STRCAT(VG_Z_LIBC_SONAME, strcat)
#elif defined(VGO_solaris)
STRCAT(VG_Z_LIBC_SONAME, strcat)
STRCAT(VG_Z_LD_SO_1, strcat)
#endif
/*---------------------- strncat ----------------------*/
#define STRNCAT(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20040,soname,fnname) \
( char* dst, const char* src, SizeT n ); \
char* VG_REPLACE_FUNCTION_EZU(20040,soname,fnname) \
( char* dst, const char* src, SizeT n ) \
{ \
const HChar* src_orig = src; \
HChar* dst_orig = dst; \
SizeT m = 0; \
\
while (*dst) dst++; \
while (m < n && *src) { m++; *dst++ = *src++; } /* concat <= n chars */ \
*dst = 0; /* always add null */ \
\
/* This checks for overlap after copying, unavoidable without */ \
/* pre-counting lengths... should be ok */ \
if (is_overlap(dst_orig, \
src_orig, \
(Addr)dst-(Addr)dst_orig+1, \
(Addr)src-(Addr)src_orig+1)) \
RECORD_OVERLAP_ERROR("strncat", dst_orig, src_orig, n); \
\
return dst_orig; \
}
#if defined(VGO_linux)
STRNCAT(VG_Z_LIBC_SONAME, strncat)
#elif defined(VGO_darwin)
//STRNCAT(VG_Z_LIBC_SONAME, strncat)
//STRNCAT(VG_Z_DYLD, strncat)
#elif defined(VGO_solaris)
STRNCAT(VG_Z_LIBC_SONAME, strncat)
#endif
/*---------------------- strlcat ----------------------*/
/* Append src to dst. n is the size of dst's buffer. dst is guaranteed
to be nul-terminated after the copy, unless n <= strlen(dst_orig).
Returns min(n, strlen(dst_orig)) + strlen(src_orig).
Truncation occurred if retval >= n.
*/
#define STRLCAT(soname, fnname) \
SizeT VG_REPLACE_FUNCTION_EZU(20050,soname,fnname) \
( char* dst, const char* src, SizeT n ); \
SizeT VG_REPLACE_FUNCTION_EZU(20050,soname,fnname) \
( char* dst, const char* src, SizeT n ) \
{ \
const HChar* src_orig = src; \
HChar* dst_orig = dst; \
SizeT m = 0; \
\
while (m < n && *dst) { m++; dst++; } \
if (m < n) { \
/* Fill as far as dst_orig[n-2], then nul-terminate. */ \
while (m < n-1 && *src) { m++; *dst++ = *src++; } \
*dst = 0; \
} else { \
/* No space to copy anything to dst. m == n */ \
} \
/* Finish counting min(n, strlen(dst_orig)) + strlen(src_orig) */ \
while (*src) { m++; src++; } \
/* This checks for overlap after copying, unavoidable without */ \
/* pre-counting lengths... should be ok */ \
if (is_overlap(dst_orig, \
src_orig, \
(Addr)dst-(Addr)dst_orig+1, \
(Addr)src-(Addr)src_orig+1)) \
RECORD_OVERLAP_ERROR("strlcat", dst_orig, src_orig, n); \
\
return m; \
}
#if defined(VGO_linux)
#elif defined(VGO_darwin)
//STRLCAT(VG_Z_LIBC_SONAME, strlcat)
//STRLCAT(VG_Z_DYLD, strlcat)
STRLCAT(VG_Z_LIBC_SONAME, strlcat)
#elif defined(VGO_solaris)
STRLCAT(VG_Z_LIBC_SONAME, strlcat)
#endif
/*---------------------- strnlen ----------------------*/
#define STRNLEN(soname, fnname) \
SizeT VG_REPLACE_FUNCTION_EZU(20060,soname,fnname) \
( const char* str, SizeT n ); \
SizeT VG_REPLACE_FUNCTION_EZU(20060,soname,fnname) \
( const char* str, SizeT n ) \
{ \
SizeT i = 0; \
while (i < n && str[i] != 0) i++; \
return i; \
}
#if defined(VGO_linux)
STRNLEN(VG_Z_LIBC_SONAME, strnlen)
STRNLEN(VG_Z_LIBC_SONAME, __GI_strnlen)
#elif defined(VGO_darwin)
# if DARWIN_VERS == DARWIN_10_9
STRNLEN(libsystemZucZddylib, strnlen)
# endif
#elif defined(VGO_solaris)
STRNLEN(VG_Z_LIBC_SONAME, strnlen)
#endif
/*---------------------- strlen ----------------------*/
// Note that this replacement often doesn't get used because gcc inlines
// calls to strlen() with its own built-in version. This can be very
// confusing if you aren't expecting it. Other small functions in
// this file may also be inline by gcc.
#define STRLEN(soname, fnname) \
SizeT VG_REPLACE_FUNCTION_EZU(20070,soname,fnname) \
( const char* str ); \
SizeT VG_REPLACE_FUNCTION_EZU(20070,soname,fnname) \
( const char* str ) \
{ \
SizeT i = 0; \
while (str[i] != 0) i++; \
return i; \
}
#if defined(VGO_linux)
STRLEN(VG_Z_LIBC_SONAME, strlen)
STRLEN(VG_Z_LIBC_SONAME, __GI_strlen)
STRLEN(VG_Z_LIBC_SONAME, __strlen_sse2)
STRLEN(VG_Z_LIBC_SONAME, __strlen_sse2_no_bsf)
STRLEN(VG_Z_LIBC_SONAME, __strlen_sse42)
STRLEN(VG_Z_LD_LINUX_SO_2, strlen)
STRLEN(VG_Z_LD_LINUX_X86_64_SO_2, strlen)
# if defined(VGPV_arm_linux_android) \
|| defined(VGPV_x86_linux_android) \
|| defined(VGPV_mips32_linux_android)
STRLEN(NONE, __dl_strlen); /* in /system/bin/linker */
# endif
#elif defined(VGO_darwin)
STRLEN(VG_Z_LIBC_SONAME, strlen)
# if DARWIN_VERS >= DARWIN_10_9
STRLEN(libsystemZucZddylib, strlen)
# endif
#elif defined(VGO_solaris)
STRLEN(VG_Z_LIBC_SONAME, strlen)
STRLEN(VG_Z_LD_SO_1, strlen)
#endif
/*---------------------- strcpy ----------------------*/
#define STRCPY(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20080,soname,fnname) \
( char* dst, const char* src ); \
char* VG_REPLACE_FUNCTION_EZU(20080,soname,fnname) \
( char* dst, const char* src ) \
{ \
const HChar* src_orig = src; \
HChar* dst_orig = dst; \
\
while (*src) *dst++ = *src++; \
*dst = 0; \
\
/* This checks for overlap after copying, unavoidable without */ \
/* pre-counting length... should be ok */ \
if (is_overlap(dst_orig, \
src_orig, \
(Addr)dst-(Addr)dst_orig+1, \
(Addr)src-(Addr)src_orig+1)) \
RECORD_OVERLAP_ERROR("strcpy", dst_orig, src_orig, 0); \
\
return dst_orig; \
}
#if defined(VGO_linux)
STRCPY(VG_Z_LIBC_SONAME, strcpy)
STRCPY(VG_Z_LIBC_SONAME, __GI_strcpy)
#elif defined(VGO_darwin)
STRCPY(VG_Z_LIBC_SONAME, strcpy)
# if DARWIN_VERS == DARWIN_10_9
STRCPY(libsystemZucZddylib, strcpy)
# endif
#elif defined(VGO_solaris)
STRCPY(VG_Z_LIBC_SONAME, strcpy)
STRCPY(VG_Z_LD_SO_1, strcpy)
#endif
/*---------------------- strncpy ----------------------*/
#define STRNCPY(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20090,soname,fnname) \
( char* dst, const char* src, SizeT n ); \
char* VG_REPLACE_FUNCTION_EZU(20090,soname,fnname) \
( char* dst, const char* src, SizeT n ) \
{ \
const HChar* src_orig = src; \
HChar* dst_orig = dst; \
SizeT m = 0; \
\
while (m < n && *src) { m++; *dst++ = *src++; } \
/* Check for overlap after copying; all n bytes of dst are relevant, */ \
/* but only m+1 bytes of src if terminator was found */ \
if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \
RECORD_OVERLAP_ERROR("strncpy", dst, src, n); \
while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \
\
return dst_orig; \
}
#if defined(VGO_linux)
STRNCPY(VG_Z_LIBC_SONAME, strncpy)
STRNCPY(VG_Z_LIBC_SONAME, __GI_strncpy)
STRNCPY(VG_Z_LIBC_SONAME, __strncpy_sse2)
STRNCPY(VG_Z_LIBC_SONAME, __strncpy_sse2_unaligned)
#elif defined(VGO_darwin)
STRNCPY(VG_Z_LIBC_SONAME, strncpy)
# if DARWIN_VERS >= DARWIN_10_9
STRNCPY(libsystemZucZddylib, strncpy)
# endif
#elif defined(VGO_solaris)
STRNCPY(VG_Z_LIBC_SONAME, strncpy)
STRNCPY(VG_Z_LD_SO_1, strncpy)
#endif
/*---------------------- strlcpy ----------------------*/
/* Copy up to n-1 bytes from src to dst. Then nul-terminate dst if n > 0.
Returns strlen(src). Does not zero-fill the remainder of dst. */
#define STRLCPY(soname, fnname) \
SizeT VG_REPLACE_FUNCTION_EZU(20100,soname,fnname) \
( char* dst, const char* src, SizeT n ); \
SizeT VG_REPLACE_FUNCTION_EZU(20100,soname,fnname) \
( char* dst, const char* src, SizeT n ) \
{ \
const HChar* src_orig = src; \
HChar* dst_orig = dst; \
SizeT m = 0; \
\
STRLCPY_CHECK_FOR_DSTSIZE_ZERO \
\
while (m < n-1 && *src) { m++; *dst++ = *src++; } \
/* m non-nul bytes have now been copied, and m <= n-1. */ \
/* Check for overlap after copying; all n bytes of dst are relevant, */ \
/* but only m+1 bytes of src if terminator was found */ \
if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \
RECORD_OVERLAP_ERROR("strlcpy", dst, src, n); \
/* Nul-terminate dst. */ \
if (n > 0) *dst = 0; \
/* Finish counting strlen(src). */ \
while (*src) src++; \
return src - src_orig; \
}
#if defined(VGO_linux)
#if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
|| defined(VGPV_mips32_linux_android)
#define STRLCPY_CHECK_FOR_DSTSIZE_ZERO
STRLCPY(VG_Z_LIBC_SONAME, strlcpy);
#endif
#elif defined(VGO_darwin)
#define STRLCPY_CHECK_FOR_DSTSIZE_ZERO
//STRLCPY(VG_Z_LIBC_SONAME, strlcpy)
//STRLCPY(VG_Z_DYLD, strlcpy)
STRLCPY(VG_Z_LIBC_SONAME, strlcpy)
#elif defined(VGO_solaris)
/* special case for n == 0 which is undocumented but heavily used */
#define STRLCPY_CHECK_FOR_DSTSIZE_ZERO \
if (n == 0) { \
while (*src) src++; \
return src - src_orig; \
}
STRLCPY(VG_Z_LIBC_SONAME, strlcpy)
#endif
/*---------------------- strncmp ----------------------*/
#define STRNCMP(soname, fnname) \
int VG_REPLACE_FUNCTION_EZU(20110,soname,fnname) \
( const char* s1, const char* s2, SizeT nmax ); \
int VG_REPLACE_FUNCTION_EZU(20110,soname,fnname) \
( const char* s1, const char* s2, SizeT nmax ) \
{ \
SizeT n = 0; \
while (True) { \
if (n >= nmax) return 0; \
if (*s1 == 0 && *s2 == 0) return 0; \
if (*s1 == 0) return -1; \
if (*s2 == 0) return 1; \
\
if (*(const UChar*)s1 < *(const UChar*)s2) return -1; \
if (*(const UChar*)s1 > *(const UChar*)s2) return 1; \
\
s1++; s2++; n++; \
} \
}
#if defined(VGO_linux)
STRNCMP(VG_Z_LIBC_SONAME, strncmp)
STRNCMP(VG_Z_LIBC_SONAME, __GI_strncmp)
STRNCMP(VG_Z_LIBC_SONAME, __strncmp_sse2)
STRNCMP(VG_Z_LIBC_SONAME, __strncmp_sse42)
#elif defined(VGO_darwin)
STRNCMP(VG_Z_LIBC_SONAME, strncmp)
# if DARWIN_VERS >= DARWIN_10_9
STRNCMP(libsystemZuplatformZddylib, _platform_strncmp)
# endif
#elif defined(VGO_solaris)
STRNCMP(VG_Z_LIBC_SONAME, strncmp)
#endif
/*---------------------- strcasecmp ----------------------*/
#define STRCASECMP(soname, fnname) \
int VG_REPLACE_FUNCTION_EZU(20120,soname,fnname) \
( const char* s1, const char* s2 ); \
int VG_REPLACE_FUNCTION_EZU(20120,soname,fnname) \
( const char* s1, const char* s2 ) \
{ \
extern int tolower(int); \
register UChar c1; \
register UChar c2; \
while (True) { \
c1 = tolower(*(const UChar *)s1); \
c2 = tolower(*(const UChar *)s2); \
if (c1 != c2) break; \
if (c1 == 0) break; \
s1++; s2++; \
} \
if ((UChar)c1 < (UChar)c2) return -1; \
if ((UChar)c1 > (UChar)c2) return 1; \
return 0; \
}
#if defined(VGO_linux)
# if !defined(VGPV_arm_linux_android) \
&& !defined(VGPV_x86_linux_android) \
&& !defined(VGPV_mips32_linux_android) \
&& !defined(VGPV_arm64_linux_android)
STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp)
STRCASECMP(VG_Z_LIBC_SONAME, __GI_strcasecmp)
# endif
#elif defined(VGO_darwin)
//STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp)
#elif defined(VGO_solaris)
STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp)
#endif
/*---------------------- strncasecmp ----------------------*/
#define STRNCASECMP(soname, fnname) \
int VG_REPLACE_FUNCTION_EZU(20130,soname,fnname) \
( const char* s1, const char* s2, SizeT nmax ); \
int VG_REPLACE_FUNCTION_EZU(20130,soname,fnname) \
( const char* s1, const char* s2, SizeT nmax ) \
{ \
extern int tolower(int); \
SizeT n = 0; \
while (True) { \
if (n >= nmax) return 0; \
if (*s1 == 0 && *s2 == 0) return 0; \
if (*s1 == 0) return -1; \
if (*s2 == 0) return 1; \
\
if (tolower(*(const UChar *)s1) \
< tolower(*(const UChar*)s2)) return -1; \
if (tolower(*(const UChar *)s1) \
> tolower(*(const UChar *)s2)) return 1; \
\
s1++; s2++; n++; \
} \
}
#if defined(VGO_linux)
# if !defined(VGPV_arm_linux_android) \
&& !defined(VGPV_x86_linux_android) \
&& !defined(VGPV_mips32_linux_android) \
&& !defined(VGPV_arm64_linux_android)
STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp)
STRNCASECMP(VG_Z_LIBC_SONAME, __GI_strncasecmp)
# endif
#elif defined(VGO_darwin)
//STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp)
//STRNCASECMP(VG_Z_DYLD, strncasecmp)
#elif defined(VGO_solaris)
STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp)
#endif
/*---------------------- strcasecmp_l ----------------------*/
#define STRCASECMP_L(soname, fnname) \
int VG_REPLACE_FUNCTION_EZU(20140,soname,fnname) \
( const char* s1, const char* s2, void* locale ); \
int VG_REPLACE_FUNCTION_EZU(20140,soname,fnname) \
( const char* s1, const char* s2, void* locale ) \
{ \
extern int tolower_l(int, void*) __attribute__((weak)); \
register UChar c1; \
register UChar c2; \
while (True) { \
c1 = tolower_l(*(const UChar *)s1, locale); \
c2 = tolower_l(*(const UChar *)s2, locale); \
if (c1 != c2) break; \
if (c1 == 0) break; \
s1++; s2++; \
} \
if ((UChar)c1 < (UChar)c2) return -1; \
if ((UChar)c1 > (UChar)c2) return 1; \
return 0; \
}
#if defined(VGO_linux)
STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l)
STRCASECMP_L(VG_Z_LIBC_SONAME, __GI_strcasecmp_l)
STRCASECMP_L(VG_Z_LIBC_SONAME, __GI___strcasecmp_l)
#elif defined(VGO_darwin)
//STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l)
#elif defined(VGO_solaris)
#endif
/*---------------------- strncasecmp_l ----------------------*/
#define STRNCASECMP_L(soname, fnname) \
int VG_REPLACE_FUNCTION_EZU(20150,soname,fnname) \
( const char* s1, const char* s2, SizeT nmax, void* locale ); \
int VG_REPLACE_FUNCTION_EZU(20150,soname,fnname) \
( const char* s1, const char* s2, SizeT nmax, void* locale ) \
{ \
extern int tolower_l(int, void*) __attribute__((weak)); \
SizeT n = 0; \
while (True) { \
if (n >= nmax) return 0; \
if (*s1 == 0 && *s2 == 0) return 0; \
if (*s1 == 0) return -1; \
if (*s2 == 0) return 1; \
\
if (tolower_l(*(const UChar *)s1, locale) \
< tolower_l(*(const UChar *)s2, locale)) return -1; \
if (tolower_l(*(const UChar *)s1, locale) \
> tolower_l(*(const UChar *)s2, locale)) return 1; \
\
s1++; s2++; n++; \
} \
}
#if defined(VGO_linux)
STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l)
STRNCASECMP_L(VG_Z_LIBC_SONAME, __GI_strncasecmp_l)
STRNCASECMP_L(VG_Z_LIBC_SONAME, __GI___strncasecmp_l)
#elif defined(VGO_darwin)
//STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l)
//STRNCASECMP_L(VG_Z_DYLD, strncasecmp_l)
#elif defined(VGO_solaris)
#endif
/*---------------------- strcmp ----------------------*/
#define STRCMP(soname, fnname) \
int VG_REPLACE_FUNCTION_EZU(20160,soname,fnname) \
( const char* s1, const char* s2 ); \
int VG_REPLACE_FUNCTION_EZU(20160,soname,fnname) \
( const char* s1, const char* s2 ) \
{ \
register UChar c1; \
register UChar c2; \
while (True) { \
c1 = *(const UChar *)s1; \
c2 = *(const UChar *)s2; \
if (c1 != c2) break; \
if (c1 == 0) break; \
s1++; s2++; \
} \
if ((UChar)c1 < (UChar)c2) return -1; \
if ((UChar)c1 > (UChar)c2) return 1; \
return 0; \
}
#if defined(VGO_linux)
STRCMP(VG_Z_LIBC_SONAME, strcmp)
STRCMP(VG_Z_LIBC_SONAME, __GI_strcmp)
STRCMP(VG_Z_LIBC_SONAME, __strcmp_sse2)
STRCMP(VG_Z_LIBC_SONAME, __strcmp_sse42)
STRCMP(VG_Z_LD_LINUX_X86_64_SO_2, strcmp)
STRCMP(VG_Z_LD64_SO_1, strcmp)
# if defined(VGPV_arm_linux_android) || defined(VGPV_x86_linux_android) \
|| defined(VGPV_mips32_linux_android)
STRCMP(NONE, __dl_strcmp); /* in /system/bin/linker */
# endif
#elif defined(VGO_darwin)
STRCMP(VG_Z_LIBC_SONAME, strcmp)
# if DARWIN_VERS >= DARWIN_10_9
STRCMP(libsystemZuplatformZddylib, _platform_strcmp)
# endif
#elif defined(VGO_solaris)
STRCMP(VG_Z_LIBC_SONAME, strcmp)
STRCMP(VG_Z_LD_SO_1, strcmp)
#endif
/*---------------------- memchr ----------------------*/
#define MEMCHR(soname, fnname) \
void* VG_REPLACE_FUNCTION_EZU(20170,soname,fnname) \
(const void *s, int c, SizeT n); \
void* VG_REPLACE_FUNCTION_EZU(20170,soname,fnname) \
(const void *s, int c, SizeT n) \
{ \
SizeT i; \
UChar c0 = (UChar)c; \
const UChar* p = s; \
for (i = 0; i < n; i++) \
if (p[i] == c0) return CONST_CAST(void *,&p[i]); \
return NULL; \
}
#if defined(VGO_linux)
MEMCHR(VG_Z_LIBC_SONAME, memchr)
MEMCHR(VG_Z_LIBC_SONAME, __GI_memchr)
#elif defined(VGO_darwin)
# if DARWIN_VERS == DARWIN_10_9
MEMCHR(VG_Z_DYLD, memchr)
MEMCHR(libsystemZuplatformZddylib, _platform_memchr)
# endif
# if DARWIN_VERS >= DARWIN_10_10
MEMCHR(VG_Z_DYLD, memchr)
/* _platform_memchr$VARIANT$Generic */
MEMCHR(libsystemZuplatformZddylib, _platform_memchr$VARIANT$Generic)
/* _platform_memchr$VARIANT$Haswell */
MEMCHR(libsystemZuplatformZddylib, _platform_memchr$VARIANT$Haswell)
# endif
#elif defined(VGO_solaris)
MEMCHR(VG_Z_LIBC_SONAME, memchr)
#endif
/*---------------------- memrchr ----------------------*/
#define MEMRCHR(soname, fnname) \
void* VG_REPLACE_FUNCTION_EZU(20360,soname,fnname) \
(const void *s, int c, SizeT n); \
void* VG_REPLACE_FUNCTION_EZU(20360,soname,fnname) \
(const void *s, int c, SizeT n) \
{ \
SizeT i; \
UChar c0 = (UChar)c; \
const UChar* p = s; \
for (i = 0; i < n; i++) \
if (p[n-1-i] == c0) return CONST_CAST(void *,&p[n-1-i]); \
return NULL; \
}
#if defined(VGO_linux)
MEMRCHR(VG_Z_LIBC_SONAME, memrchr)
#elif defined(VGO_darwin)
//MEMRCHR(VG_Z_LIBC_SONAME, memrchr)
//MEMRCHR(VG_Z_DYLD, memrchr)
#elif defined(VGO_solaris)
#endif
/*---------------------- memcpy ----------------------*/
#define MEMMOVE_OR_MEMCPY(becTag, soname, fnname, do_ol_check) \
void* VG_REPLACE_FUNCTION_EZZ(becTag,soname,fnname) \
( void *dst, const void *src, SizeT len ); \
void* VG_REPLACE_FUNCTION_EZZ(becTag,soname,fnname) \
( void *dst, const void *src, SizeT len ) \
{ \
if (do_ol_check && is_overlap(dst, src, len, len)) \
RECORD_OVERLAP_ERROR("memcpy", dst, src, len); \
\
const Addr WS = sizeof(UWord); /* 8 or 4 */ \
const Addr WM = WS - 1; /* 7 or 3 */ \
\
if (len > 0) { \
if (dst < src || !is_overlap(dst, src, len, len)) { \
\
/* Copying backwards. */ \
SizeT n = len; \
Addr d = (Addr)dst; \
Addr s = (Addr)src; \
\
if (((s^d) & WM) == 0) { \
/* s and d have same UWord alignment. */ \
/* Pull up to a UWord boundary. */ \
while ((s & WM) != 0 && n >= 1) \
{ *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \
/* Copy UWords. */ \
while (n >= WS) \
{ *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; } \
if (n == 0) \
return dst; \
} \
if (((s|d) & 1) == 0) { \
/* Both are 16-aligned; copy what we can thusly. */ \
while (n >= 2) \
{ *(UShort*)d = *(UShort*)s; s += 2; d += 2; n -= 2; } \
} \
/* Copy leftovers, or everything if misaligned. */ \
while (n >= 1) \
{ *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \
\
} else if (dst > src) { \
\
SizeT n = len; \
Addr d = ((Addr)dst) + n; \
Addr s = ((Addr)src) + n; \
\
/* Copying forwards. */ \
if (((s^d) & WM) == 0) { \
/* s and d have same UWord alignment. */ \
/* Back down to a UWord boundary. */ \
while ((s & WM) != 0 && n >= 1) \
{ s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \
/* Copy UWords. */ \
while (n >= WS) \
{ s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; } \
if (n == 0) \
return dst; \
} \
if (((s|d) & 1) == 0) { \
/* Both are 16-aligned; copy what we can thusly. */ \
while (n >= 2) \
{ s -= 2; d -= 2; *(UShort*)d = *(UShort*)s; n -= 2; } \
} \
/* Copy leftovers, or everything if misaligned. */ \
while (n >= 1) \
{ s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \
\
} \
} \
\
return dst; \
}
#define MEMMOVE(soname, fnname) \
MEMMOVE_OR_MEMCPY(20181, soname, fnname, 0)
#define MEMCPY(soname, fnname) \
MEMMOVE_OR_MEMCPY(20180, soname, fnname, 1)
#if defined(VGO_linux)
/* For older memcpy we have to use memmove-like semantics and skip
the overlap check; sigh; see #275284. */
MEMMOVE(VG_Z_LIBC_SONAME, memcpyZAGLIBCZu2Zd2Zd5) /* memcpy@GLIBC_2.2.5 */
MEMCPY(VG_Z_LIBC_SONAME, memcpyZAZAGLIBCZu2Zd14) /* memcpy@@GLIBC_2.14 */
MEMCPY(VG_Z_LIBC_SONAME, memcpy) /* fallback case */
MEMCPY(VG_Z_LIBC_SONAME, __GI_memcpy)
MEMCPY(VG_Z_LIBC_SONAME, __memcpy_sse2)
MEMCPY(VG_Z_LD_SO_1, memcpy) /* ld.so.1 */
MEMCPY(VG_Z_LD64_SO_1, memcpy) /* ld64.so.1 */
/* icc9 blats these around all over the place. Not only in the main
executable but various .so's. They are highly tuned and read
memory beyond the source boundary (although work correctly and
never go across page boundaries), so give errors when run
natively, at least for misaligned source arg. Just intercepting
in the exe only until we understand more about the problem. See
http://bugs.kde.org/show_bug.cgi?id=139776
*/
MEMCPY(NONE, ZuintelZufastZumemcpy)
#elif defined(VGO_darwin)
# if DARWIN_VERS <= DARWIN_10_6
MEMCPY(VG_Z_LIBC_SONAME, memcpy)
# endif
MEMCPY(VG_Z_LIBC_SONAME, memcpyZDVARIANTZDsse3x) /* memcpy$VARIANT$sse3x */
MEMCPY(VG_Z_LIBC_SONAME, memcpyZDVARIANTZDsse42) /* memcpy$VARIANT$sse42 */
#elif defined(VGO_solaris)
MEMCPY(VG_Z_LIBC_SONAME, memcpy)
MEMCPY(VG_Z_LD_SO_1, memcpy)
#endif
/*---------------------- memcmp ----------------------*/
#define MEMCMP(soname, fnname) \
int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \
( const void *s1V, const void *s2V, SizeT n ); \
int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \
( const void *s1V, const void *s2V, SizeT n ) \
{ \
const SizeT WS = sizeof(UWord); /* 8 or 4 */ \
const SizeT WM = WS - 1; /* 7 or 3 */ \
Addr s1A = (Addr)s1V; \
Addr s2A = (Addr)s2V; \
\
if (((s1A | s2A) & WM) == 0) { \
/* Both areas are word aligned. Skip over the */ \
/* equal prefix as fast as possible. */ \
while (n >= WS) { \
UWord w1 = *(UWord*)s1A; \
UWord w2 = *(UWord*)s2A; \
if (w1 != w2) break; \
s1A += WS; \
s2A += WS; \
n -= WS; \
} \
} \
\
const UChar* s1 = (const UChar*) s1A; \
const UChar* s2 = (const UChar*) s2A; \
\
while (n != 0) { \
UChar a0 = s1[0]; \
UChar b0 = s2[0]; \
s1 += 1; \
s2 += 1; \
int res = ((int)a0) - ((int)b0); \
if (res != 0) \
return res; \
n -= 1; \
} \
return 0; \
}
#if defined(VGO_linux)
MEMCMP(VG_Z_LIBC_SONAME, memcmp)
MEMCMP(VG_Z_LIBC_SONAME, __GI_memcmp)
MEMCMP(VG_Z_LIBC_SONAME, __memcmp_sse2)
MEMCMP(VG_Z_LIBC_SONAME, __memcmp_sse4_1)
MEMCMP(VG_Z_LIBC_SONAME, bcmp)
MEMCMP(VG_Z_LD_SO_1, bcmp)
#elif defined(VGO_darwin)
# if DARWIN_VERS >= DARWIN_10_9
MEMCMP(libsystemZuplatformZddylib, _platform_memcmp)
# endif
#elif defined(VGO_solaris)
MEMCMP(VG_Z_LIBC_SONAME, memcmp)
MEMCMP(VG_Z_LIBC_SONAME, bcmp)
MEMCMP(VG_Z_LD_SO_1, memcmp)
#endif
/*---------------------- stpcpy ----------------------*/
/* Copy SRC to DEST, returning the address of the terminating '\0' in
DEST. (minor variant of strcpy) */
#define STPCPY(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \
( char* dst, const char* src ); \
char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \
( char* dst, const char* src ) \
{ \
const HChar* src_orig = src; \
HChar* dst_orig = dst; \
\
while (*src) *dst++ = *src++; \
*dst = 0; \
\
/* This checks for overlap after copying, unavoidable without */ \
/* pre-counting length... should be ok */ \
if (is_overlap(dst_orig, \
src_orig, \
(Addr)dst-(Addr)dst_orig+1, \
(Addr)src-(Addr)src_orig+1)) \
RECORD_OVERLAP_ERROR("stpcpy", dst_orig, src_orig, 0); \
\
return dst; \
}
#if defined(VGO_linux)
STPCPY(VG_Z_LIBC_SONAME, stpcpy)
STPCPY(VG_Z_LIBC_SONAME, __GI_stpcpy)
STPCPY(VG_Z_LIBC_SONAME, __stpcpy_sse2)
STPCPY(VG_Z_LIBC_SONAME, __stpcpy_sse2_unaligned)
STPCPY(VG_Z_LD_LINUX_SO_2, stpcpy)
STPCPY(VG_Z_LD_LINUX_X86_64_SO_2, stpcpy)
#elif defined(VGO_darwin)
//STPCPY(VG_Z_LIBC_SONAME, stpcpy)
//STPCPY(VG_Z_DYLD, stpcpy)
#elif defined(VGO_solaris)
STPCPY(VG_Z_LIBC_SONAME, stpcpy)
#endif
/*---------------------- stpncpy ----------------------*/
#define STPNCPY(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20420,soname,fnname) \
( char* dst, const char* src, SizeT n ); \
char* VG_REPLACE_FUNCTION_EZU(20420,soname,fnname) \
( char* dst, const char* src, SizeT n ) \
{ \
const HChar* src_orig = src; \
HChar* dst_str = dst; \
SizeT m = 0; \
\
while (m < n && *src) { m++; *dst++ = *src++; } \
/* Check for overlap after copying; all n bytes of dst are relevant, */ \
/* but only m+1 bytes of src if terminator was found */ \
if (is_overlap(dst_str, src_orig, n, (m < n) ? m+1 : n)) \
RECORD_OVERLAP_ERROR("stpncpy", dst, src, n); \
dst_str = dst; \
while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \
\
return dst_str; \
}
#if defined(VGO_linux)
STPNCPY(VG_Z_LIBC_SONAME, stpncpy)
#endif
/*---------------------- memset ----------------------*/
/* Why are we bothering to intercept this? It seems entirely
pointless. */
#define MEMSET(soname, fnname) \
void* VG_REPLACE_FUNCTION_EZU(20210,soname,fnname) \
(void *s, Int c, SizeT n); \
void* VG_REPLACE_FUNCTION_EZU(20210,soname,fnname) \
(void *s, Int c, SizeT n) \
{ \
if (sizeof(void*) == 8) { \
Addr a = (Addr)s; \
ULong c8 = (c & 0xFF); \
c8 = (c8 << 8) | c8; \
c8 = (c8 << 16) | c8; \
c8 = (c8 << 32) | c8; \
while ((a & 7) != 0 && n >= 1) \
{ *(UChar*)a = (UChar)c; a += 1; n -= 1; } \
while (n >= 32) \
{ *(ULong*)a = c8; a += 8; n -= 8; \
*(ULong*)a = c8; a += 8; n -= 8; \
*(ULong*)a = c8; a += 8; n -= 8; \
*(ULong*)a = c8; a += 8; n -= 8; } \
while (n >= 8) \
{ *(ULong*)a = c8; a += 8; n -= 8; } \
while (n >= 1) \
{ *(UChar*)a = (UChar)c; a += 1; n -= 1; } \
return s; \
} else { \
Addr a = (Addr)s; \
UInt c4 = (c & 0xFF); \
c4 = (c4 << 8) | c4; \
c4 = (c4 << 16) | c4; \
while ((a & 3) != 0 && n >= 1) \
{ *(UChar*)a = (UChar)c; a += 1; n -= 1; } \
while (n >= 16) \
{ *(UInt*)a = c4; a += 4; n -= 4; \
*(UInt*)a = c4; a += 4; n -= 4; \
*(UInt*)a = c4; a += 4; n -= 4; \
*(UInt*)a = c4; a += 4; n -= 4; } \
while (n >= 4) \
{ *(UInt*)a = c4; a += 4; n -= 4; } \
while (n >= 1) \
{ *(UChar*)a = (UChar)c; a += 1; n -= 1; } \
return s; \
} \
}
#if defined(VGO_linux)
MEMSET(VG_Z_LIBC_SONAME, memset)
#elif defined(VGO_darwin)
//MEMSET(VG_Z_LIBC_SONAME, memset)
//MEMSET(VG_Z_DYLD, memset)
MEMSET(VG_Z_LIBC_SONAME, memset)
#elif defined(VGO_solaris)
MEMSET(VG_Z_LIBC_SONAME, memset)
#endif
/*---------------------- memmove ----------------------*/
/* memmove -- use the MEMMOVE defn above. */
#if defined(VGO_linux)
MEMMOVE(VG_Z_LIBC_SONAME, memmove)
MEMMOVE(VG_Z_LIBC_SONAME, __GI_memmove)
/* See bug #349828 Override for ld64.so.1 like memcpy, because for some
arches MEMCPY_OK_FOR_FORWARD_MEMMOVE is set, which might cause memmove
to call memcpy. */
MEMMOVE(VG_Z_LD64_SO_1, memmove)
#elif defined(VGO_darwin)
# if DARWIN_VERS <= DARWIN_10_6
MEMMOVE(VG_Z_LIBC_SONAME, memmove)
# endif
MEMMOVE(VG_Z_LIBC_SONAME, memmoveZDVARIANTZDsse3x) /* memmove$VARIANT$sse3x */
MEMMOVE(VG_Z_LIBC_SONAME, memmoveZDVARIANTZDsse42) /* memmove$VARIANT$sse42 */
# if DARWIN_VERS >= DARWIN_10_9
/* _platform_memmove$VARIANT$Ivybridge */
MEMMOVE(libsystemZuplatformZddylib, ZuplatformZumemmoveZDVARIANTZDIvybridge)
# endif
#elif defined(VGO_solaris)
MEMMOVE(VG_Z_LIBC_SONAME, memmove)
MEMMOVE(VG_Z_LD_SO_1, memmove)
#endif
/*---------------------- bcopy ----------------------*/
#define BCOPY(soname, fnname) \
void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \
(const void *srcV, void *dstV, SizeT n); \
void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \
(const void *srcV, void *dstV, SizeT n) \
{ \
SizeT i; \
HChar* dst = dstV; \
const HChar* src = srcV; \
if (dst < src) { \
for (i = 0; i < n; i++) \
dst[i] = src[i]; \
} \
else \
if (dst > src) { \
for (i = 0; i < n; i++) \
dst[n-i-1] = src[n-i-1]; \
} \
}
#if defined(VGO_linux)
BCOPY(VG_Z_LIBC_SONAME, bcopy)
#elif defined(VGO_darwin)
//BCOPY(VG_Z_LIBC_SONAME, bcopy)
//BCOPY(VG_Z_DYLD, bcopy)
#elif defined(VGO_darwin)
BCOPY(VG_Z_LIBC_SONAME, bcopy)
#endif
/*-------------------- memmove_chk --------------------*/
/* glibc 2.5 variant of memmove which checks the dest is big enough.
There is no specific part of glibc that this is copied from. */
#define GLIBC25___MEMMOVE_CHK(soname, fnname) \
void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \
(void *dstV, const void *srcV, SizeT n, SizeT destlen); \
void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \
(void *dstV, const void *srcV, SizeT n, SizeT destlen) \
{ \
SizeT i; \
HChar* dst = dstV; \
const HChar* src = srcV; \
if (destlen < n) \
goto badness; \
if (dst < src) { \
for (i = 0; i < n; i++) \
dst[i] = src[i]; \
} \
else \
if (dst > src) { \
for (i = 0; i < n; i++) \
dst[n-i-1] = src[n-i-1]; \
} \
return dst; \
badness: \
VALGRIND_PRINTF_BACKTRACE( \
"*** memmove_chk: buffer overflow detected ***: " \
"program terminated\n"); \
my_exit(1); \
/*NOTREACHED*/ \
return NULL; \
}
#if defined(VGO_linux)
GLIBC25___MEMMOVE_CHK(VG_Z_LIBC_SONAME, __memmove_chk)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
#endif
/*-------------------- strchrnul --------------------*/
/* Find the first occurrence of C in S or the final NUL byte. */
#define GLIBC232_STRCHRNUL(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \
(const char* s, int c_in); \
char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \
(const char* s, int c_in) \
{ \
HChar c = (HChar) c_in; \
const HChar* char_ptr = s; \
while (1) { \
if (*char_ptr == 0) return CONST_CAST(HChar *,char_ptr); \
if (*char_ptr == c) return CONST_CAST(HChar *,char_ptr); \
char_ptr++; \
} \
}
#if defined(VGO_linux)
GLIBC232_STRCHRNUL(VG_Z_LIBC_SONAME, strchrnul)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
#endif
/*---------------------- rawmemchr ----------------------*/
/* Find the first occurrence of C in S. */
#define GLIBC232_RAWMEMCHR(soname, fnname) \
void* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \
(const void* s, int c_in); \
void* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \
(const void* s, int c_in) \
{ \
UChar c = (UChar) c_in; \
const UChar* char_ptr = s; \
while (1) { \
if (*char_ptr == c) return CONST_CAST(void *,char_ptr); \
char_ptr++; \
} \
}
#if defined (VGO_linux)
GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, rawmemchr)
GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, __GI___rawmemchr)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
#endif
/*---------------------- strcpy_chk ----------------------*/
/* glibc variant of strcpy that checks the dest is big enough.
Copied from glibc-2.5/debug/test-strcpy_chk.c. */
#define GLIBC25___STRCPY_CHK(soname,fnname) \
char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \
(char* dst, const char* src, SizeT len); \
char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \
(char* dst, const char* src, SizeT len) \
{ \
HChar* ret = dst; \
if (! len) \
goto badness; \
while ((*dst++ = *src++) != '\0') \
if (--len == 0) \
goto badness; \
return ret; \
badness: \
VALGRIND_PRINTF_BACKTRACE( \
"*** strcpy_chk: buffer overflow detected ***: " \
"program terminated\n"); \
my_exit(1); \
/*NOTREACHED*/ \
return NULL; \
}
#if defined(VGO_linux)
GLIBC25___STRCPY_CHK(VG_Z_LIBC_SONAME, __strcpy_chk)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
#endif
/*---------------------- stpcpy_chk ----------------------*/
/* glibc variant of stpcpy that checks the dest is big enough.
Copied from glibc-2.5/debug/test-stpcpy_chk.c. */
#define GLIBC25___STPCPY_CHK(soname,fnname) \
char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \
(char* dst, const char* src, SizeT len); \
char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \
(char* dst, const char* src, SizeT len) \
{ \
if (! len) \
goto badness; \
while ((*dst++ = *src++) != '\0') \
if (--len == 0) \
goto badness; \
return dst - 1; \
badness: \
VALGRIND_PRINTF_BACKTRACE( \
"*** stpcpy_chk: buffer overflow detected ***: " \
"program terminated\n"); \
my_exit(1); \
/*NOTREACHED*/ \
return NULL; \
}
#if defined(VGO_linux)
GLIBC25___STPCPY_CHK(VG_Z_LIBC_SONAME, __stpcpy_chk)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
#endif
/*---------------------- mempcpy ----------------------*/
/* mempcpy */
#define GLIBC25_MEMPCPY(soname, fnname) \
void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \
( void *dst, const void *src, SizeT len ); \
void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \
( void *dst, const void *src, SizeT len ) \
{ \
SizeT len_saved = len; \
\
if (len == 0) \
return dst; \
\
if (is_overlap(dst, src, len, len)) \
RECORD_OVERLAP_ERROR("mempcpy", dst, src, len); \
\
if ( dst > src ) { \
register HChar *d = (char *)dst + len - 1; \
register const HChar *s = (const char *)src + len - 1; \
while ( len-- ) { \
*d-- = *s--; \
} \
} else if ( dst < src ) { \
register HChar *d = dst; \
register const HChar *s = src; \
while ( len-- ) { \
*d++ = *s++; \
} \
} \
return (void*)( ((char*)dst) + len_saved ); \
}
#if defined(VGO_linux)
GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy)
GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, __GI_mempcpy)
GLIBC25_MEMPCPY(VG_Z_LD_SO_1, mempcpy) /* ld.so.1 */
GLIBC25_MEMPCPY(VG_Z_LD_LINUX_SO_3, mempcpy) /* ld-linux.so.3 */
GLIBC25_MEMPCPY(VG_Z_LD_LINUX_X86_64_SO_2, mempcpy) /* ld-linux-x86-64.so.2 */
#elif defined(VGO_darwin)
//GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy)
#elif defined(VGO_solaris)
#endif
/*-------------------- memcpy_chk --------------------*/
#define GLIBC26___MEMCPY_CHK(soname, fnname) \
void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \
(void* dst, const void* src, SizeT len, SizeT dstlen ); \
void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \
(void* dst, const void* src, SizeT len, SizeT dstlen ) \
{ \
register HChar *d; \
register const HChar *s; \
\
if (dstlen < len) goto badness; \
\
if (len == 0) \
return dst; \
\
if (is_overlap(dst, src, len, len)) \
RECORD_OVERLAP_ERROR("memcpy_chk", dst, src, len); \
\
if ( dst > src ) { \
d = (HChar *)dst + len - 1; \
s = (const HChar *)src + len - 1; \
while ( len-- ) { \
*d-- = *s--; \
} \
} else if ( dst < src ) { \
d = (HChar *)dst; \
s = (const HChar *)src; \
while ( len-- ) { \
*d++ = *s++; \
} \
} \
return dst; \
badness: \
VALGRIND_PRINTF_BACKTRACE( \
"*** memcpy_chk: buffer overflow detected ***: " \
"program terminated\n"); \
my_exit(1); \
/*NOTREACHED*/ \
return NULL; \
}
#if defined(VGO_linux)
GLIBC26___MEMCPY_CHK(VG_Z_LIBC_SONAME, __memcpy_chk)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
#endif
/*---------------------- strstr ----------------------*/
#define STRSTR(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \
(const char* haystack, const char* needle); \
char* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \
(const char* haystack, const char* needle) \
{ \
const HChar* h = haystack; \
const HChar* n = needle; \
\
/* find the length of n, not including terminating zero */ \
UWord nlen = 0; \
while (n[nlen]) nlen++; \
\
/* if n is the empty string, match immediately. */ \
if (nlen == 0) return CONST_CAST(HChar *,h); \
\
/* assert(nlen >= 1); */ \
HChar n0 = n[0]; \
\
while (1) { \
const HChar hh = *h; \
if (hh == 0) return NULL; \
if (hh != n0) { h++; continue; } \
\
UWord i; \
for (i = 0; i < nlen; i++) { \
if (n[i] != h[i]) \
break; \
} \
/* assert(i >= 0 && i <= nlen); */ \
if (i == nlen) \
return CONST_CAST(HChar *,h); \
\
h++; \
} \
}
#if defined(VGO_linux)
STRSTR(VG_Z_LIBC_SONAME, strstr)
STRSTR(VG_Z_LIBC_SONAME, __strstr_sse2)
STRSTR(VG_Z_LIBC_SONAME, __strstr_sse42)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
STRSTR(VG_Z_LIBC_SONAME, strstr)
#endif
/*---------------------- strpbrk ----------------------*/
#define STRPBRK(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \
(const char* sV, const char* acceptV); \
char* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \
(const char* sV, const char* acceptV) \
{ \
const HChar* s = sV; \
const HChar* accept = acceptV; \
\
/* find the length of 'accept', not including terminating zero */ \
UWord nacc = 0; \
while (accept[nacc]) nacc++; \
\
/* if n is the empty string, fail immediately. */ \
if (nacc == 0) return NULL; \
\
/* assert(nacc >= 1); */ \
while (1) { \
UWord i; \
HChar sc = *s; \
if (sc == 0) \
break; \
for (i = 0; i < nacc; i++) { \
if (sc == accept[i]) \
return CONST_CAST(HChar *,s); \
} \
s++; \
} \
\
return NULL; \
}
#if defined(VGO_linux)
STRPBRK(VG_Z_LIBC_SONAME, strpbrk)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
STRPBRK(VG_Z_LIBC_SONAME, strpbrk)
#endif
/*---------------------- strcspn ----------------------*/
#define STRCSPN(soname, fnname) \
SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \
(const char* sV, const char* rejectV); \
SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \
(const char* sV, const char* rejectV) \
{ \
const HChar* s = sV; \
const HChar* reject = rejectV; \
\
/* find the length of 'reject', not including terminating zero */ \
UWord nrej = 0; \
while (reject[nrej]) nrej++; \
\
UWord len = 0; \
while (1) { \
UWord i; \
HChar sc = *s; \
if (sc == 0) \
break; \
for (i = 0; i < nrej; i++) { \
if (sc == reject[i]) \
break; \
} \
/* assert(i >= 0 && i <= nrej); */ \
if (i < nrej) \
break; \
s++; \
len++; \
} \
\
return len; \
}
#if defined(VGO_linux)
STRCSPN(VG_Z_LIBC_SONAME, strcspn)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
STRCSPN(VG_Z_LIBC_SONAME, strcspn)
#endif
/*---------------------- strspn ----------------------*/
#define STRSPN(soname, fnname) \
SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \
(const char* sV, const char* acceptV); \
SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \
(const char* sV, const char* acceptV) \
{ \
const UChar* s = (const UChar *)sV; \
const UChar* accept = (const UChar *)acceptV; \
\
/* find the length of 'accept', not including terminating zero */ \
UWord nacc = 0; \
while (accept[nacc]) nacc++; \
if (nacc == 0) return 0; \
\
UWord len = 0; \
while (1) { \
UWord i; \
HChar sc = *s; \
if (sc == 0) \
break; \
for (i = 0; i < nacc; i++) { \
if (sc == accept[i]) \
break; \
} \
/* assert(i >= 0 && i <= nacc); */ \
if (i == nacc) \
break; \
s++; \
len++; \
} \
\
return len; \
}
#if defined(VGO_linux)
STRSPN(VG_Z_LIBC_SONAME, strspn)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
STRSPN(VG_Z_LIBC_SONAME, strspn)
#endif
/*---------------------- strcasestr ----------------------*/
#define STRCASESTR(soname, fnname) \
char* VG_REPLACE_FUNCTION_EZU(20350,soname,fnname) \
(const char* haystack, const char* needle); \
char* VG_REPLACE_FUNCTION_EZU(20350,soname,fnname) \
(const char* haystack, const char* needle) \
{ \
extern int tolower(int); \
const HChar* h = haystack; \
const HChar* n = needle; \
\
/* find the length of n, not including terminating zero */ \
UWord nlen = 0; \
while (n[nlen]) nlen++; \
\
/* if n is the empty string, match immediately. */ \
if (nlen == 0) return CONST_CAST(HChar *,h); \
\
/* assert(nlen >= 1); */ \
UChar n0 = tolower(n[0]); \
\
while (1) { \
UChar hh = tolower(*h); \
if (hh == 0) return NULL; \
if (hh != n0) { h++; continue; } \
\
UWord i; \
for (i = 0; i < nlen; i++) { \
if (tolower(n[i]) != tolower(h[i])) \
break; \
} \
/* assert(i >= 0 && i <= nlen); */ \
if (i == nlen) \
return CONST_CAST(HChar *,h); \
\
h++; \
} \
}
#if defined(VGO_linux)
# if !defined(VGPV_arm_linux_android) \
&& !defined(VGPV_x86_linux_android) \
&& !defined(VGPV_mips32_linux_android) \
&& !defined(VGPV_arm64_linux_android)
STRCASESTR(VG_Z_LIBC_SONAME, strcasestr)
# endif
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
STRCASESTR(VG_Z_LIBC_SONAME, strcasestr)
#endif
/*---------------------- wcslen ----------------------*/
// This is a wchar_t equivalent to strlen. Unfortunately
// we don't have wchar_t available here, but it looks like
// a 32 bit int on Linux. I don't know if that is also
// valid on MacOSX.
#define WCSLEN(soname, fnname) \
SizeT VG_REPLACE_FUNCTION_EZU(20370,soname,fnname) \
( const UInt* str ); \
SizeT VG_REPLACE_FUNCTION_EZU(20370,soname,fnname) \
( const UInt* str ) \
{ \
SizeT i = 0; \
while (str[i] != 0) i++; \
return i; \
}
#if defined(VGO_linux)
WCSLEN(VG_Z_LIBC_SONAME, wcslen)
#elif defined(VGO_darwin)
#elif defined(VGO_solaris)
WCSLEN(VG_Z_LIBC_SONAME, wcslen)
#endif
/*---------------------- wcscmp ----------------------*/
// This is a wchar_t equivalent to strcmp. We don't
// have wchar_t available here, but in the GNU C Library
// wchar_t is always 32 bits wide and wcscmp uses signed
// comparison, not unsigned as in strcmp function.
#define WCSCMP(soname, fnname) \
int VG_REPLACE_FUNCTION_EZU(20380,soname,fnname) \
( const Int* s1, const Int* s2 ); \
int VG_REPLACE_FUNCTION_EZU(20380,soname,fnname) \
( const Int* s1, const Int* s2 ) \
{ \
register Int c1; \
register Int c2; \
while (True) { \
c1 = *s1; \
c2 = *s2; \
if (c1 != c2) break; \
if (c1 == 0) break; \
s1++; s2++; \
} \
if (c1 < c2) return -1; \
if (c1 > c2) return 1; \
return 0; \
}
#if defined(VGO_linux)
WCSCMP(VG_Z_LIBC_SONAME, wcscmp)
#endif
/*---------------------- wcscpy ----------------------*/
// This is a wchar_t equivalent to strcpy. We don't
// have wchar_t available here, but in the GNU C Library
// wchar_t is always 32 bits wide.
#define WCSCPY(soname, fnname) \
Int* VG_REPLACE_FUNCTION_EZU(20390,soname,fnname) \
( Int* dst, const Int* src ); \
Int* VG_REPLACE_FUNCTION_EZU(20390,soname,fnname) \
( Int* dst, const Int* src ) \
{ \
const Int* src_orig = src; \
Int* dst_orig = dst; \
\
while (*src) *dst++ = *src++; \
*dst = 0; \
\
/* This checks for overlap after copying, unavoidable without */ \
/* pre-counting length... should be ok */ \
if (is_overlap(dst_orig, \
src_orig, \
(Addr)dst-(Addr)dst_orig+1, \
(Addr)src-(Addr)src_orig+1)) \
RECORD_OVERLAP_ERROR("wcscpy", dst_orig, src_orig, 0); \
\
return dst_orig; \
}
#if defined(VGO_linux)
WCSCPY(VG_Z_LIBC_SONAME, wcscpy)
#endif
/*---------------------- wcschr ----------------------*/
// This is a wchar_t equivalent to strchr. We don't
// have wchar_t available here, but in the GNU C Library
// wchar_t is always 32 bits wide.
#define WCSCHR(soname, fnname) \
Int* VG_REPLACE_FUNCTION_EZU(20400,soname,fnname) ( const Int* s, Int c ); \
Int* VG_REPLACE_FUNCTION_EZU(20400,soname,fnname) ( const Int* s, Int c ) \
{ \
const Int* p = s; \
while (True) { \
if (*p == c) return CONST_CAST(Int *,p); \
if (*p == 0) return NULL; \
p++; \
} \
}
#if defined(VGO_linux)
WCSCHR(VG_Z_LIBC_SONAME, wcschr)
#endif
/*---------------------- wcsrchr ----------------------*/
// This is a wchar_t equivalent to strrchr. We don't
// have wchar_t available here, but in the GNU C Library
// wchar_t is always 32 bits wide.
#define WCSRCHR(soname, fnname) \
Int* VG_REPLACE_FUNCTION_EZU(20410,soname,fnname)( const Int* s, Int c ); \
Int* VG_REPLACE_FUNCTION_EZU(20410,soname,fnname)( const Int* s, Int c ) \
{ \
const Int* p = s; \
const Int* last = NULL; \
while (True) { \
if (*p == c) last = p; \
if (*p == 0) return CONST_CAST(Int *,last); \
p++; \
} \
}
#if defined(VGO_linux)
WCSRCHR(VG_Z_LIBC_SONAME, wcsrchr)
#endif
/*------------------------------------------------------------*/
/*--- Improve definedness checking of process environment ---*/
/*------------------------------------------------------------*/
#if defined(VGO_linux)
/* If these wind up getting generated via a macro, so that multiple
versions of each function exist (as above), use the _EZU variants
to assign equivalance class tags. */
/*---------------------- putenv ----------------------*/
int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string);
int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string)
{
OrigFn fn;
Word result;
const HChar* p = string;
VALGRIND_GET_ORIG_FN(fn);
/* Now by walking over the string we magically produce
traces when hitting undefined memory. */
if (p)
while (*p++)
__asm__ __volatile__("" ::: "memory");
CALL_FN_W_W(result, fn, string);
return result;
}
/*---------------------- unsetenv ----------------------*/
int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name);
int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name)
{
OrigFn fn;
Word result;
const HChar* p = name;
VALGRIND_GET_ORIG_FN(fn);
/* Now by walking over the string we magically produce
traces when hitting undefined memory. */
if (p)
while (*p++)
__asm__ __volatile__("" ::: "memory");
CALL_FN_W_W(result, fn, name);
return result;
}
/*---------------------- setenv ----------------------*/
/* setenv */
int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv)
(const char* name, const char* value, int overwrite);
int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv)
(const char* name, const char* value, int overwrite)
{
OrigFn fn;
Word result;
const HChar* p;
VALGRIND_GET_ORIG_FN(fn);
/* Now by walking over the string we magically produce
traces when hitting undefined memory. */
if (name)
for (p = name; *p; p++)
__asm__ __volatile__("" ::: "memory");
if (value)
for (p = value; *p; p++)
__asm__ __volatile__("" ::: "memory");
(void) VALGRIND_CHECK_VALUE_IS_DEFINED (overwrite);
CALL_FN_W_WWW(result, fn, name, value, overwrite);
return result;
}
#endif /* defined(VGO_linux) */
/*--------------------------------------------------------------------*/
/*--- end ---*/
/*--------------------------------------------------------------------*/
|