njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- Replacements for strcpy(), memcpy() et al, which run on the ---*/ |
| 4 | /*--- simulated CPU. ---*/ |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 5 | /*--- mc_replace_strmem.c ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 9 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | 0e1b514 | 2003-04-15 14:58:06 +0000 | [diff] [blame] | 10 | detecting memory errors. |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 11 | |
sewardj | 9eecbbb | 2010-05-03 21:37:12 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2010 Julian Seward |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 13 | jseward@acm.org |
| 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
| 30 | The GNU General Public License is contained in the file COPYING. |
| 31 | */ |
| 32 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_tool_basics.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 34 | #include "pub_tool_hashtable.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 35 | #include "pub_tool_redir.h" |
| 36 | #include "pub_tool_tooliface.h" |
| 37 | #include "valgrind.h" |
| 38 | |
njn | 34419c1 | 2003-05-02 17:24:29 +0000 | [diff] [blame] | 39 | #include "mc_include.h" |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 40 | #include "memcheck.h" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 41 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 42 | /* --------------------------------------------------------------------- |
njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 43 | We have our own versions of these functions for two reasons: |
| 44 | (a) it allows us to do overlap checking |
| 45 | (b) some of the normal versions are hyper-optimised, which fools |
| 46 | Memcheck and cause spurious value warnings. Our versions are |
| 47 | simpler. |
| 48 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 49 | Note that overenthusiastic use of PLT bypassing by the glibc people also |
| 50 | means that we need to patch multiple versions of some of the functions to |
| 51 | our own implementations. |
| 52 | |
njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 53 | THEY RUN ON THE SIMD CPU! |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 54 | ------------------------------------------------------------------ */ |
| 55 | |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 56 | /* Assignment of behavioural equivalence class tags: 2NNNP is intended |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 57 | to be reserved for Memcheck. Current usage: |
| 58 | |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 59 | 20010 STRRCHR |
| 60 | 20020 STRCHR |
| 61 | 20030 STRCAT |
| 62 | 20040 STRNCAT |
| 63 | 20050 STRLCAT |
| 64 | 20060 STRNLEN |
| 65 | 20070 STRLEN |
| 66 | 20080 STRCPY |
| 67 | 20090 STRNCPY |
| 68 | 20100 STRLCPY |
| 69 | 20110 STRNCMP |
| 70 | 20120 STRCASECMP |
| 71 | 20130 STRNCASECMP |
| 72 | 20140 STRCASECMP_L |
| 73 | 20150 STRNCASECMP_L |
| 74 | 20160 STRCMP |
| 75 | 20170 MEMCHR |
| 76 | |
| 77 | 20180 MEMCPY if there's a conflict between memcpy and |
| 78 | 20181 MEMMOVE memmove, prefer memmove |
| 79 | |
| 80 | 20190 MEMCMP |
| 81 | 20200 STPCPY |
| 82 | 20210 MEMSET |
| 83 | 2022P unused (was previously MEMMOVE) |
| 84 | 20230 BCOPY |
| 85 | 20240 GLIBC25___MEMMOVE_CHK |
| 86 | 20250 GLIBC232_STRCHRNUL |
| 87 | 20260 GLIBC232_RAWMEMCHR |
| 88 | 20270 GLIBC25___STRCPY_CHK |
| 89 | 20280 GLIBC25___STPCPY_CHK |
| 90 | 20290 GLIBC25_MEMPCPY |
| 91 | 20300 GLIBC26___MEMCPY_CHK |
| 92 | 20310 STRSTR |
| 93 | 20320 STRPBRK |
| 94 | 20330 STRCSPN |
| 95 | 20340 STRSPN |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 96 | */ |
| 97 | |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 98 | |
sewardj | dda830a | 2003-07-20 22:28:42 +0000 | [diff] [blame] | 99 | /* Figure out if [dst .. dst+dstlen-1] overlaps with |
| 100 | [src .. src+srclen-1]. |
| 101 | We assume that the address ranges do not wrap around |
| 102 | (which is safe since on Linux addresses >= 0xC0000000 |
| 103 | are not accessible and the program will segfault in this |
| 104 | circumstance, presumably). |
| 105 | */ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 106 | static inline |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 107 | Bool is_overlap ( void* dst, const void* src, SizeT dstlen, SizeT srclen ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 108 | { |
sewardj | dda830a | 2003-07-20 22:28:42 +0000 | [diff] [blame] | 109 | Addr loS, hiS, loD, hiD; |
| 110 | |
| 111 | if (dstlen == 0 || srclen == 0) |
| 112 | return False; |
| 113 | |
| 114 | loS = (Addr)src; |
| 115 | loD = (Addr)dst; |
| 116 | hiS = loS + srclen - 1; |
| 117 | hiD = loD + dstlen - 1; |
| 118 | |
| 119 | /* So figure out if [loS .. hiS] overlaps with [loD .. hiD]. */ |
| 120 | if (loS < loD) { |
| 121 | return !(hiS < loD); |
| 122 | } |
| 123 | else if (loD < loS) { |
| 124 | return !(hiD < loS); |
| 125 | } |
| 126 | else { |
| 127 | /* They start at same place. Since we know neither of them has |
| 128 | zero length, they must overlap. */ |
| 129 | return True; |
| 130 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 131 | } |
| 132 | |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 133 | |
| 134 | /* Call here to exit if we can't continue. On Android we can't call |
| 135 | _exit for some reason, so we have to blunt-instrument it. */ |
| 136 | __attribute__ ((__noreturn__)) |
| 137 | static inline void my_exit ( int x ) |
| 138 | { |
| 139 | # if defined(VGPV_arm_linux_android) |
| 140 | __asm__ __volatile__(".word 0xFFFFFFFF"); |
| 141 | while (1) {} |
| 142 | # else |
| 143 | extern void _exit(int status); |
sewardj | 4966542 | 2011-07-12 13:50:59 +0000 | [diff] [blame] | 144 | _exit(x); |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 145 | # endif |
| 146 | } |
| 147 | |
| 148 | |
njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 149 | // This is a macro rather than a function because we don't want to have an |
| 150 | // extra function in the stack trace. |
bart | 575ce8e | 2011-05-15 07:04:03 +0000 | [diff] [blame] | 151 | #define RECORD_OVERLAP_ERROR(s, src, dst, len) \ |
| 152 | VALGRIND_DO_CLIENT_REQUEST_EXPR(0, \ |
| 153 | _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR, \ |
| 154 | s, src, dst, len, 0) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 155 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 156 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 157 | /*---------------------- strrchr ----------------------*/ |
| 158 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 159 | #define STRRCHR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 160 | char* VG_REPLACE_FUNCTION_EZU(20010,soname,fnname)( const char* s, int c ); \ |
| 161 | char* VG_REPLACE_FUNCTION_EZU(20010,soname,fnname)( const char* s, int c ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 162 | { \ |
| 163 | UChar ch = (UChar)((UInt)c); \ |
| 164 | UChar* p = (UChar*)s; \ |
| 165 | UChar* last = NULL; \ |
| 166 | while (True) { \ |
| 167 | if (*p == ch) last = p; \ |
| 168 | if (*p == 0) return last; \ |
| 169 | p++; \ |
| 170 | } \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 171 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 172 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 173 | // Apparently rindex() is the same thing as strrchr() |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 174 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 175 | STRRCHR(VG_Z_LIBC_SONAME, strrchr) |
| 176 | STRRCHR(VG_Z_LIBC_SONAME, rindex) |
| 177 | STRRCHR(VG_Z_LIBC_SONAME, __GI_strrchr) |
| 178 | STRRCHR(VG_Z_LD_LINUX_SO_2, rindex) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 179 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 180 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 181 | //STRRCHR(VG_Z_LIBC_SONAME, strrchr) |
| 182 | //STRRCHR(VG_Z_LIBC_SONAME, rindex) |
| 183 | //STRRCHR(VG_Z_DYLD, strrchr) |
| 184 | //STRRCHR(VG_Z_DYLD, rindex) |
| 185 | STRRCHR(VG_Z_LIBC_SONAME, strrchr) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 186 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 187 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 188 | |
| 189 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 190 | /*---------------------- strchr ----------------------*/ |
| 191 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 192 | #define STRCHR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 193 | char* VG_REPLACE_FUNCTION_EZU(20020,soname,fnname) ( const char* s, int c ); \ |
| 194 | char* VG_REPLACE_FUNCTION_EZU(20020,soname,fnname) ( const char* s, int c ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 195 | { \ |
| 196 | UChar ch = (UChar)((UInt)c); \ |
| 197 | UChar* p = (UChar*)s; \ |
| 198 | while (True) { \ |
| 199 | if (*p == ch) return p; \ |
| 200 | if (*p == 0) return NULL; \ |
| 201 | p++; \ |
| 202 | } \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 203 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 204 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 205 | // Apparently index() is the same thing as strchr() |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 206 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 207 | STRCHR(VG_Z_LIBC_SONAME, strchr) |
| 208 | STRCHR(VG_Z_LIBC_SONAME, index) |
| 209 | STRCHR(VG_Z_LIBC_SONAME, __GI_strchr) |
| 210 | # if !defined(VGP_x86_linux) |
| 211 | STRCHR(VG_Z_LD_LINUX_SO_2, strchr) |
| 212 | STRCHR(VG_Z_LD_LINUX_SO_2, index) |
| 213 | STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, strchr) |
| 214 | STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, index) |
| 215 | # endif |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 216 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 217 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 218 | //STRCHR(VG_Z_LIBC_SONAME, strchr) |
| 219 | //STRCHR(VG_Z_LIBC_SONAME, index) |
| 220 | //STRCHR(VG_Z_DYLD, strchr) |
| 221 | //STRCHR(VG_Z_DYLD, index) |
| 222 | STRCHR(VG_Z_LIBC_SONAME, strchr) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 223 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 224 | #endif |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 225 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 226 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 227 | /*---------------------- strcat ----------------------*/ |
| 228 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 229 | #define STRCAT(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 230 | char* VG_REPLACE_FUNCTION_EZU(20030,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 231 | ( char* dst, const char* src ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 232 | char* VG_REPLACE_FUNCTION_EZU(20030,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 233 | ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 234 | { \ |
| 235 | const Char* src_orig = src; \ |
| 236 | Char* dst_orig = dst; \ |
| 237 | while (*dst) dst++; \ |
| 238 | while (*src) *dst++ = *src++; \ |
| 239 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 240 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 241 | /* This is a bit redundant, I think; any overlap and the strcat will */ \ |
| 242 | /* go forever... or until a seg fault occurs. */ \ |
| 243 | if (is_overlap(dst_orig, \ |
| 244 | src_orig, \ |
| 245 | (Addr)dst-(Addr)dst_orig+1, \ |
| 246 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 247 | RECORD_OVERLAP_ERROR("strcat", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 248 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 249 | return dst_orig; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 250 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 251 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 252 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 253 | STRCAT(VG_Z_LIBC_SONAME, strcat) |
| 254 | STRCAT(VG_Z_LIBC_SONAME, __GI_strcat) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 255 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 256 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 257 | //STRCAT(VG_Z_LIBC_SONAME, strcat) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 258 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 259 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 260 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 261 | |
| 262 | /*---------------------- strncat ----------------------*/ |
| 263 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 264 | #define STRNCAT(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 265 | char* VG_REPLACE_FUNCTION_EZU(20040,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 266 | ( char* dst, const char* src, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 267 | char* VG_REPLACE_FUNCTION_EZU(20040,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 268 | ( char* dst, const char* src, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 269 | { \ |
| 270 | const Char* src_orig = src; \ |
| 271 | Char* dst_orig = dst; \ |
| 272 | SizeT m = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 273 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 274 | while (*dst) dst++; \ |
| 275 | while (m < n && *src) { m++; *dst++ = *src++; } /* concat <= n chars */ \ |
| 276 | *dst = 0; /* always add null */ \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 277 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 278 | /* This checks for overlap after copying, unavoidable without */ \ |
| 279 | /* pre-counting lengths... should be ok */ \ |
| 280 | if (is_overlap(dst_orig, \ |
| 281 | src_orig, \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 282 | (Addr)dst-(Addr)dst_orig+1, \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 283 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 284 | RECORD_OVERLAP_ERROR("strncat", dst_orig, src_orig, n); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 285 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 286 | return dst_orig; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 287 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 288 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 289 | #if defined(VGO_linux) |
| 290 | STRNCAT(VG_Z_LIBC_SONAME, strncat) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 291 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 292 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 293 | //STRNCAT(VG_Z_LIBC_SONAME, strncat) |
| 294 | //STRNCAT(VG_Z_DYLD, strncat) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 295 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 296 | #endif |
| 297 | |
| 298 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 299 | /*---------------------- strlcat ----------------------*/ |
| 300 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 301 | /* Append src to dst. n is the size of dst's buffer. dst is guaranteed |
| 302 | to be nul-terminated after the copy, unless n <= strlen(dst_orig). |
| 303 | Returns min(n, strlen(dst_orig)) + strlen(src_orig). |
| 304 | Truncation occurred if retval >= n. |
| 305 | */ |
| 306 | #define STRLCAT(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 307 | SizeT VG_REPLACE_FUNCTION_EZU(20050,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 308 | ( char* dst, const char* src, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 309 | SizeT VG_REPLACE_FUNCTION_EZU(20050,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 310 | ( char* dst, const char* src, SizeT n ) \ |
| 311 | { \ |
| 312 | const Char* src_orig = src; \ |
| 313 | Char* dst_orig = dst; \ |
| 314 | SizeT m = 0; \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 315 | \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 316 | while (m < n && *dst) { m++; dst++; } \ |
| 317 | if (m < n) { \ |
| 318 | /* Fill as far as dst_orig[n-2], then nul-terminate. */ \ |
| 319 | while (m < n-1 && *src) { m++; *dst++ = *src++; } \ |
| 320 | *dst = 0; \ |
| 321 | } else { \ |
| 322 | /* No space to copy anything to dst. m == n */ \ |
| 323 | } \ |
| 324 | /* Finish counting min(n, strlen(dst_orig)) + strlen(src_orig) */ \ |
| 325 | while (*src) { m++; src++; } \ |
| 326 | /* This checks for overlap after copying, unavoidable without */ \ |
| 327 | /* pre-counting lengths... should be ok */ \ |
| 328 | if (is_overlap(dst_orig, \ |
| 329 | src_orig, \ |
| 330 | (Addr)dst-(Addr)dst_orig+1, \ |
| 331 | (Addr)src-(Addr)src_orig+1)) \ |
| 332 | RECORD_OVERLAP_ERROR("strlcat", dst_orig, src_orig, n); \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 333 | \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 334 | return m; \ |
| 335 | } |
| 336 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 337 | #if defined(VGO_linux) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 338 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 339 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 340 | //STRLCAT(VG_Z_LIBC_SONAME, strlcat) |
| 341 | //STRLCAT(VG_Z_DYLD, strlcat) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 342 | STRLCAT(VG_Z_LIBC_SONAME, strlcat) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 343 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 344 | #endif |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 345 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 346 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 347 | /*---------------------- strnlen ----------------------*/ |
| 348 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 349 | #define STRNLEN(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 350 | SizeT VG_REPLACE_FUNCTION_EZU(20060,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 351 | ( const char* str, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 352 | SizeT VG_REPLACE_FUNCTION_EZU(20060,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 353 | ( const char* str, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 354 | { \ |
| 355 | SizeT i = 0; \ |
| 356 | while (i < n && str[i] != 0) i++; \ |
| 357 | return i; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 358 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 359 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 360 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 361 | STRNLEN(VG_Z_LIBC_SONAME, strnlen) |
| 362 | STRNLEN(VG_Z_LIBC_SONAME, __GI_strnlen) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 363 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 364 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 365 | //STRNLEN(VG_Z_LIBC_SONAME, strnlen) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 366 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 367 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 368 | |
sewardj | 3ceec24 | 2003-07-30 21:24:25 +0000 | [diff] [blame] | 369 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 370 | /*---------------------- strlen ----------------------*/ |
| 371 | |
njn | 5ec15ed | 2005-08-24 19:55:51 +0000 | [diff] [blame] | 372 | // Note that this replacement often doesn't get used because gcc inlines |
| 373 | // calls to strlen() with its own built-in version. This can be very |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 374 | // confusing if you aren't expecting it. Other small functions in |
| 375 | // this file may also be inline by gcc. |
| 376 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 377 | #define STRLEN(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 378 | SizeT VG_REPLACE_FUNCTION_EZU(20070,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 379 | ( const char* str ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 380 | SizeT VG_REPLACE_FUNCTION_EZU(20070,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 381 | ( const char* str ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 382 | { \ |
| 383 | SizeT i = 0; \ |
| 384 | while (str[i] != 0) i++; \ |
| 385 | return i; \ |
sewardj | 3ceec24 | 2003-07-30 21:24:25 +0000 | [diff] [blame] | 386 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 387 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 388 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 389 | STRLEN(VG_Z_LIBC_SONAME, strlen) |
| 390 | STRLEN(VG_Z_LIBC_SONAME, __GI_strlen) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 391 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 392 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 393 | //STRLEN(VG_Z_LIBC_SONAME, strlen) |
| 394 | STRLEN(VG_Z_LIBC_SONAME, strlen) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 395 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 396 | #endif |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 397 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 398 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 399 | /*---------------------- strcpy ----------------------*/ |
| 400 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 401 | #define STRCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 402 | char* VG_REPLACE_FUNCTION_EZU(20080,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 403 | ( char* dst, const char* src ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 404 | char* VG_REPLACE_FUNCTION_EZU(20080,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 405 | ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 406 | { \ |
| 407 | const Char* src_orig = src; \ |
| 408 | Char* dst_orig = dst; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 409 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 410 | while (*src) *dst++ = *src++; \ |
| 411 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 412 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 413 | /* This checks for overlap after copying, unavoidable without */ \ |
| 414 | /* pre-counting length... should be ok */ \ |
| 415 | if (is_overlap(dst_orig, \ |
| 416 | src_orig, \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 417 | (Addr)dst-(Addr)dst_orig+1, \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 418 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 419 | RECORD_OVERLAP_ERROR("strcpy", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 420 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 421 | return dst_orig; \ |
| 422 | } |
| 423 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 424 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 425 | STRCPY(VG_Z_LIBC_SONAME, strcpy) |
| 426 | STRCPY(VG_Z_LIBC_SONAME, __GI_strcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 427 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 428 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 429 | //STRCPY(VG_Z_LIBC_SONAME, strcpy) |
| 430 | //STRCPY(VG_Z_DYLD, strcpy) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 431 | STRCPY(VG_Z_LIBC_SONAME, strcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 432 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 433 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 434 | |
| 435 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 436 | /*---------------------- strncpy ----------------------*/ |
| 437 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 438 | #define STRNCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 439 | char* VG_REPLACE_FUNCTION_EZU(20090,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 440 | ( char* dst, const char* src, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 441 | char* VG_REPLACE_FUNCTION_EZU(20090,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 442 | ( char* dst, const char* src, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 443 | { \ |
| 444 | const Char* src_orig = src; \ |
| 445 | Char* dst_orig = dst; \ |
| 446 | SizeT m = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 447 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 448 | while (m < n && *src) { m++; *dst++ = *src++; } \ |
| 449 | /* Check for overlap after copying; all n bytes of dst are relevant, */ \ |
| 450 | /* but only m+1 bytes of src if terminator was found */ \ |
| 451 | if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 452 | RECORD_OVERLAP_ERROR("strncpy", dst, src, n); \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 453 | while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \ |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 454 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 455 | return dst_orig; \ |
| 456 | } |
| 457 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 458 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 459 | STRNCPY(VG_Z_LIBC_SONAME, strncpy) |
| 460 | STRNCPY(VG_Z_LIBC_SONAME, __GI_strncpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 461 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 462 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 463 | //STRNCPY(VG_Z_LIBC_SONAME, strncpy) |
| 464 | //STRNCPY(VG_Z_DYLD, strncpy) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 465 | STRNCPY(VG_Z_LIBC_SONAME, strncpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 466 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 467 | #endif |
| 468 | |
| 469 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 470 | /*---------------------- strlcpy ----------------------*/ |
| 471 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 472 | /* Copy up to n-1 bytes from src to dst. Then nul-terminate dst if n > 0. |
| 473 | Returns strlen(src). Does not zero-fill the remainder of dst. */ |
| 474 | #define STRLCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 475 | SizeT VG_REPLACE_FUNCTION_EZU(20100,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 476 | ( char* dst, const char* src, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 477 | SizeT VG_REPLACE_FUNCTION_EZU(20100,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 478 | ( char* dst, const char* src, SizeT n ) \ |
| 479 | { \ |
| 480 | const char* src_orig = src; \ |
| 481 | char* dst_orig = dst; \ |
| 482 | SizeT m = 0; \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 483 | \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 484 | while (m < n-1 && *src) { m++; *dst++ = *src++; } \ |
| 485 | /* m non-nul bytes have now been copied, and m <= n-1. */ \ |
| 486 | /* Check for overlap after copying; all n bytes of dst are relevant, */ \ |
| 487 | /* but only m+1 bytes of src if terminator was found */ \ |
| 488 | if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \ |
| 489 | RECORD_OVERLAP_ERROR("strlcpy", dst, src, n); \ |
| 490 | /* Nul-terminate dst. */ \ |
| 491 | if (n > 0) *dst = 0; \ |
| 492 | /* Finish counting strlen(src). */ \ |
| 493 | while (*src) src++; \ |
| 494 | return src - src_orig; \ |
| 495 | } |
| 496 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 497 | #if defined(VGO_linux) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 498 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 499 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 500 | //STRLCPY(VG_Z_LIBC_SONAME, strlcpy) |
| 501 | //STRLCPY(VG_Z_DYLD, strlcpy) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 502 | STRLCPY(VG_Z_LIBC_SONAME, strlcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 503 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 504 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 505 | |
| 506 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 507 | /*---------------------- strncmp ----------------------*/ |
| 508 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 509 | #define STRNCMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 510 | int VG_REPLACE_FUNCTION_EZU(20110,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 511 | ( const char* s1, const char* s2, SizeT nmax ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 512 | int VG_REPLACE_FUNCTION_EZU(20110,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 513 | ( const char* s1, const char* s2, SizeT nmax ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 514 | { \ |
| 515 | SizeT n = 0; \ |
| 516 | while (True) { \ |
| 517 | if (n >= nmax) return 0; \ |
| 518 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 519 | if (*s1 == 0) return -1; \ |
| 520 | if (*s2 == 0) return 1; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 521 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 522 | if (*(unsigned char*)s1 < *(unsigned char*)s2) return -1; \ |
| 523 | if (*(unsigned char*)s1 > *(unsigned char*)s2) return 1; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 524 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 525 | s1++; s2++; n++; \ |
| 526 | } \ |
| 527 | } |
| 528 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 529 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 530 | STRNCMP(VG_Z_LIBC_SONAME, strncmp) |
| 531 | STRNCMP(VG_Z_LIBC_SONAME, __GI_strncmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 532 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 533 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 534 | //STRNCMP(VG_Z_LIBC_SONAME, strncmp) |
| 535 | //STRNCMP(VG_Z_DYLD, strncmp) |
| 536 | STRNCMP(VG_Z_LIBC_SONAME, strncmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 537 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 538 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 539 | |
| 540 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 541 | /*---------------------- strcasecmp ----------------------*/ |
| 542 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 543 | #define STRCASECMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 544 | int VG_REPLACE_FUNCTION_EZU(20120,soname,fnname) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 545 | ( const char* s1, const char* s2 ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 546 | int VG_REPLACE_FUNCTION_EZU(20120,soname,fnname) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 547 | ( const char* s1, const char* s2 ) \ |
| 548 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 549 | extern int tolower(int); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 550 | register unsigned char c1; \ |
| 551 | register unsigned char c2; \ |
| 552 | while (True) { \ |
| 553 | c1 = tolower(*(unsigned char *)s1); \ |
| 554 | c2 = tolower(*(unsigned char *)s2); \ |
| 555 | if (c1 != c2) break; \ |
| 556 | if (c1 == 0) break; \ |
| 557 | s1++; s2++; \ |
| 558 | } \ |
| 559 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 560 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 561 | return 0; \ |
| 562 | } |
| 563 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 564 | #if defined(VGO_linux) |
| 565 | # if !defined(VGPV_arm_linux_android) |
| 566 | STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp) |
| 567 | STRCASECMP(VG_Z_LIBC_SONAME, __GI_strcasecmp) |
| 568 | # endif |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 569 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 570 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 571 | //STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 572 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 573 | #endif |
| 574 | |
| 575 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 576 | /*---------------------- strncasecmp ----------------------*/ |
| 577 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 578 | #define STRNCASECMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 579 | int VG_REPLACE_FUNCTION_EZU(20130,soname,fnname) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 580 | ( const char* s1, const char* s2, SizeT nmax ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 581 | int VG_REPLACE_FUNCTION_EZU(20130,soname,fnname) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 582 | ( const char* s1, const char* s2, SizeT nmax ) \ |
| 583 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 584 | extern int tolower(int); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 585 | SizeT n = 0; \ |
| 586 | while (True) { \ |
| 587 | if (n >= nmax) return 0; \ |
| 588 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 589 | if (*s1 == 0) return -1; \ |
| 590 | if (*s2 == 0) return 1; \ |
| 591 | \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 592 | if (tolower(*(unsigned char*)s1) \ |
| 593 | < tolower(*(unsigned char*)s2)) return -1; \ |
| 594 | if (tolower(*(unsigned char*)s1) \ |
| 595 | > tolower(*(unsigned char*)s2)) return 1; \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 596 | \ |
| 597 | s1++; s2++; n++; \ |
| 598 | } \ |
| 599 | } |
| 600 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 601 | #if defined(VGO_linux) |
| 602 | # if !defined(VGPV_arm_linux_android) |
| 603 | STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp) |
| 604 | STRNCASECMP(VG_Z_LIBC_SONAME, __GI_strncasecmp) |
| 605 | # endif |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 606 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 607 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 608 | //STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp) |
| 609 | //STRNCASECMP(VG_Z_DYLD, strncasecmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 610 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 611 | #endif |
| 612 | |
| 613 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 614 | /*---------------------- strcasecmp_l ----------------------*/ |
| 615 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 616 | #define STRCASECMP_L(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 617 | int VG_REPLACE_FUNCTION_EZU(20140,soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 618 | ( const char* s1, const char* s2, void* locale ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 619 | int VG_REPLACE_FUNCTION_EZU(20140,soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 620 | ( const char* s1, const char* s2, void* locale ) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 621 | { \ |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 622 | extern int tolower_l(int, void*) __attribute__((weak)); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 623 | register unsigned char c1; \ |
| 624 | register unsigned char c2; \ |
| 625 | while (True) { \ |
| 626 | c1 = tolower_l(*(unsigned char *)s1, locale); \ |
| 627 | c2 = tolower_l(*(unsigned char *)s2, locale); \ |
| 628 | if (c1 != c2) break; \ |
| 629 | if (c1 == 0) break; \ |
| 630 | s1++; s2++; \ |
| 631 | } \ |
| 632 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 633 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 634 | return 0; \ |
| 635 | } |
| 636 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 637 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 638 | STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l) |
| 639 | STRCASECMP_L(VG_Z_LIBC_SONAME, __GI_strcasecmp_l) |
| 640 | STRCASECMP_L(VG_Z_LIBC_SONAME, __GI___strcasecmp_l) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 641 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 642 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 643 | //STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 644 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 645 | #endif |
| 646 | |
| 647 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 648 | /*---------------------- strncasecmp_l ----------------------*/ |
| 649 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 650 | #define STRNCASECMP_L(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 651 | int VG_REPLACE_FUNCTION_EZU(20150,soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 652 | ( const char* s1, const char* s2, SizeT nmax, void* locale ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 653 | int VG_REPLACE_FUNCTION_EZU(20150,soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 654 | ( const char* s1, const char* s2, SizeT nmax, void* locale ) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 655 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 656 | extern int tolower_l(int, void*) __attribute__((weak)); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 657 | SizeT n = 0; \ |
| 658 | while (True) { \ |
| 659 | if (n >= nmax) return 0; \ |
| 660 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 661 | if (*s1 == 0) return -1; \ |
| 662 | if (*s2 == 0) return 1; \ |
| 663 | \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 664 | if (tolower_l(*(unsigned char*)s1, locale) \ |
| 665 | < tolower_l(*(unsigned char*)s2, locale)) return -1; \ |
| 666 | if (tolower_l(*(unsigned char*)s1, locale) \ |
| 667 | > tolower_l(*(unsigned char*)s2, locale)) return 1; \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 668 | \ |
| 669 | s1++; s2++; n++; \ |
| 670 | } \ |
| 671 | } |
| 672 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 673 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 674 | STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l) |
| 675 | STRNCASECMP_L(VG_Z_LIBC_SONAME, __GI_strncasecmp_l) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 676 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 677 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 678 | //STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l) |
| 679 | //STRNCASECMP_L(VG_Z_DYLD, strncasecmp_l) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 680 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 681 | #endif |
| 682 | |
| 683 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 684 | /*---------------------- strcmp ----------------------*/ |
| 685 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 686 | #define STRCMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 687 | int VG_REPLACE_FUNCTION_EZU(20160,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 688 | ( const char* s1, const char* s2 ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 689 | int VG_REPLACE_FUNCTION_EZU(20160,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 690 | ( const char* s1, const char* s2 ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 691 | { \ |
| 692 | register unsigned char c1; \ |
| 693 | register unsigned char c2; \ |
| 694 | while (True) { \ |
| 695 | c1 = *(unsigned char *)s1; \ |
| 696 | c2 = *(unsigned char *)s2; \ |
| 697 | if (c1 != c2) break; \ |
| 698 | if (c1 == 0) break; \ |
| 699 | s1++; s2++; \ |
| 700 | } \ |
| 701 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 702 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 703 | return 0; \ |
| 704 | } |
| 705 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 706 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 707 | STRCMP(VG_Z_LIBC_SONAME, strcmp) |
| 708 | STRCMP(VG_Z_LIBC_SONAME, __GI_strcmp) |
| 709 | STRCMP(VG_Z_LD_LINUX_X86_64_SO_2, strcmp) |
| 710 | STRCMP(VG_Z_LD64_SO_1, strcmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 711 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 712 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 713 | //STRCMP(VG_Z_LIBC_SONAME, strcmp) |
| 714 | STRCMP(VG_Z_LIBC_SONAME, strcmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 715 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 716 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 717 | |
| 718 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 719 | /*---------------------- memchr ----------------------*/ |
| 720 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 721 | #define MEMCHR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 722 | void* VG_REPLACE_FUNCTION_EZU(20170,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 723 | (const void *s, int c, SizeT n); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 724 | void* VG_REPLACE_FUNCTION_EZU(20170,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 725 | (const void *s, int c, SizeT n) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 726 | { \ |
| 727 | SizeT i; \ |
| 728 | UChar c0 = (UChar)c; \ |
| 729 | UChar* p = (UChar*)s; \ |
| 730 | for (i = 0; i < n; i++) \ |
| 731 | if (p[i] == c0) return (void*)(&p[i]); \ |
| 732 | return NULL; \ |
| 733 | } |
| 734 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 735 | #if defined(VGO_linux) |
| 736 | MEMCHR(VG_Z_LIBC_SONAME, memchr) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 737 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 738 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 739 | //MEMCHR(VG_Z_LIBC_SONAME, memchr) |
| 740 | //MEMCHR(VG_Z_DYLD, memchr) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 741 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 742 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 743 | |
| 744 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 745 | /*---------------------- memcpy ----------------------*/ |
| 746 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 747 | #define MEMMOVE_OR_MEMCPY(becTag, soname, fnname, do_ol_check) \ |
sewardj | d88797f | 2011-08-17 21:25:50 +0000 | [diff] [blame] | 748 | void* VG_REPLACE_FUNCTION_EZZ(becTag,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 749 | ( void *dst, const void *src, SizeT len ); \ |
sewardj | d88797f | 2011-08-17 21:25:50 +0000 | [diff] [blame] | 750 | void* VG_REPLACE_FUNCTION_EZZ(becTag,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 751 | ( void *dst, const void *src, SizeT len ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 752 | { \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 753 | if (do_ol_check && is_overlap(dst, src, len, len)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 754 | RECORD_OVERLAP_ERROR("memcpy", dst, src, len); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 755 | \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 756 | const Addr WS = sizeof(UWord); /* 8 or 4 */ \ |
| 757 | const Addr WM = WS - 1; /* 7 or 3 */ \ |
| 758 | \ |
tom | 863ab7c | 2011-08-18 08:10:20 +0000 | [diff] [blame] | 759 | if (len > 0) { \ |
| 760 | if (dst < src) { \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 761 | \ |
tom | 863ab7c | 2011-08-18 08:10:20 +0000 | [diff] [blame] | 762 | /* Copying backwards. */ \ |
| 763 | SizeT n = len; \ |
| 764 | Addr d = (Addr)dst; \ |
| 765 | Addr s = (Addr)src; \ |
| 766 | \ |
| 767 | if (((s^d) & WM) == 0) { \ |
| 768 | /* s and d have same UWord alignment. */ \ |
| 769 | /* Pull up to a UWord boundary. */ \ |
| 770 | while ((s & WM) != 0 && n >= 1) \ |
| 771 | { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \ |
| 772 | /* Copy UWords. */ \ |
| 773 | while (n >= WS) \ |
| 774 | { *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; } \ |
| 775 | if (n == 0) \ |
| 776 | return dst; \ |
| 777 | } \ |
| 778 | if (((s|d) & 1) == 0) { \ |
| 779 | /* Both are 16-aligned; copy what we can thusly. */ \ |
| 780 | while (n >= 2) \ |
| 781 | { *(UShort*)d = *(UShort*)s; s += 2; d += 2; n -= 2; } \ |
| 782 | } \ |
| 783 | /* Copy leftovers, or everything if misaligned. */ \ |
| 784 | while (n >= 1) \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 785 | { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 786 | \ |
tom | 863ab7c | 2011-08-18 08:10:20 +0000 | [diff] [blame] | 787 | } else if (dst > src) { \ |
| 788 | \ |
| 789 | SizeT n = len; \ |
| 790 | Addr d = ((Addr)dst) + n; \ |
| 791 | Addr s = ((Addr)src) + n; \ |
| 792 | \ |
| 793 | /* Copying forwards. */ \ |
| 794 | if (((s^d) & WM) == 0) { \ |
| 795 | /* s and d have same UWord alignment. */ \ |
| 796 | /* Back down to a UWord boundary. */ \ |
| 797 | while ((s & WM) != 0 && n >= 1) \ |
| 798 | { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \ |
| 799 | /* Copy UWords. */ \ |
| 800 | while (n >= WS) \ |
| 801 | { s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; } \ |
| 802 | if (n == 0) \ |
| 803 | return dst; \ |
| 804 | } \ |
| 805 | if (((s|d) & 1) == 0) { \ |
| 806 | /* Both are 16-aligned; copy what we can thusly. */ \ |
| 807 | while (n >= 2) \ |
| 808 | { s -= 2; d -= 2; *(UShort*)d = *(UShort*)s; n -= 2; } \ |
| 809 | } \ |
| 810 | /* Copy leftovers, or everything if misaligned. */ \ |
| 811 | while (n >= 1) \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 812 | { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \ |
tom | 863ab7c | 2011-08-18 08:10:20 +0000 | [diff] [blame] | 813 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 814 | } \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 815 | } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 816 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 817 | return dst; \ |
| 818 | } |
| 819 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 820 | #define MEMMOVE(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 821 | MEMMOVE_OR_MEMCPY(20181, soname, fnname, 0) |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 822 | |
| 823 | #define MEMCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 824 | MEMMOVE_OR_MEMCPY(20180, soname, fnname, 1) |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 825 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 826 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 827 | /* For older memcpy we have to use memmove-like semantics and skip |
| 828 | the overlap check; sigh; see #275284. */ |
| 829 | MEMMOVE(VG_Z_LIBC_SONAME, memcpyZAGLIBCZu2Zd2Zd5) /* memcpy@GLIBC_2.2.5 */ |
| 830 | MEMCPY(VG_Z_LIBC_SONAME, memcpyZAZAGLIBCZu2Zd14) /* memcpy@@GLIBC_2.14 */ |
| 831 | MEMCPY(VG_Z_LIBC_SONAME, memcpy) /* fallback case */ |
| 832 | MEMCPY(VG_Z_LD_SO_1, memcpy) /* ld.so.1 */ |
| 833 | MEMCPY(VG_Z_LD64_SO_1, memcpy) /* ld64.so.1 */ |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 834 | /* icc9 blats these around all over the place. Not only in the main |
| 835 | executable but various .so's. They are highly tuned and read |
| 836 | memory beyond the source boundary (although work correctly and |
| 837 | never go across page boundaries), so give errors when run |
| 838 | natively, at least for misaligned source arg. Just intercepting |
| 839 | in the exe only until we understand more about the problem. See |
| 840 | http://bugs.kde.org/show_bug.cgi?id=139776 |
sewardj | f0b3432 | 2007-01-16 21:42:28 +0000 | [diff] [blame] | 841 | */ |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 842 | MEMCPY(NONE, ZuintelZufastZumemcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 843 | |
| 844 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 845 | //MEMCPY(VG_Z_LIBC_SONAME, memcpy) |
| 846 | //MEMCPY(VG_Z_DYLD, memcpy) |
| 847 | MEMCPY(VG_Z_LIBC_SONAME, memcpyZDVARIANTZDsse3x) /* memcpy$VARIANT$sse3x */ |
| 848 | MEMCPY(VG_Z_LIBC_SONAME, memcpyZDVARIANTZDsse42) /* memcpy$VARIANT$sse42 */ |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 849 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 850 | #endif |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 851 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 852 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 853 | /*---------------------- memcmp ----------------------*/ |
| 854 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 855 | #define MEMCMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 856 | int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 857 | ( const void *s1V, const void *s2V, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 858 | int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 859 | ( const void *s1V, const void *s2V, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 860 | { \ |
| 861 | int res; \ |
| 862 | unsigned char a0; \ |
| 863 | unsigned char b0; \ |
| 864 | unsigned char* s1 = (unsigned char*)s1V; \ |
| 865 | unsigned char* s2 = (unsigned char*)s2V; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 866 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 867 | while (n != 0) { \ |
| 868 | a0 = s1[0]; \ |
| 869 | b0 = s2[0]; \ |
| 870 | s1 += 1; \ |
| 871 | s2 += 1; \ |
| 872 | res = ((int)a0) - ((int)b0); \ |
| 873 | if (res != 0) \ |
| 874 | return res; \ |
| 875 | n -= 1; \ |
| 876 | } \ |
| 877 | return 0; \ |
| 878 | } |
| 879 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 880 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 881 | MEMCMP(VG_Z_LIBC_SONAME, memcmp) |
| 882 | MEMCMP(VG_Z_LIBC_SONAME, bcmp) |
| 883 | MEMCMP(VG_Z_LD_SO_1, bcmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 884 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 885 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 886 | //MEMCMP(VG_Z_LIBC_SONAME, memcmp) |
| 887 | //MEMCMP(VG_Z_LIBC_SONAME, bcmp) |
| 888 | //MEMCMP(VG_Z_DYLD, memcmp) |
| 889 | //MEMCMP(VG_Z_DYLD, bcmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 890 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 891 | #endif |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 892 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 893 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 894 | /*---------------------- stpcpy ----------------------*/ |
| 895 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 896 | /* Copy SRC to DEST, returning the address of the terminating '\0' in |
| 897 | DEST. (minor variant of strcpy) */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 898 | #define STPCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 899 | char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 900 | ( char* dst, const char* src ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 901 | char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 902 | ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 903 | { \ |
| 904 | const Char* src_orig = src; \ |
| 905 | Char* dst_orig = dst; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 906 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 907 | while (*src) *dst++ = *src++; \ |
| 908 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 909 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 910 | /* This checks for overlap after copying, unavoidable without */ \ |
| 911 | /* pre-counting length... should be ok */ \ |
| 912 | if (is_overlap(dst_orig, \ |
| 913 | src_orig, \ |
| 914 | (Addr)dst-(Addr)dst_orig+1, \ |
| 915 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 916 | RECORD_OVERLAP_ERROR("stpcpy", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 917 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 918 | return dst; \ |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 919 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 920 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 921 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 922 | STPCPY(VG_Z_LIBC_SONAME, stpcpy) |
| 923 | STPCPY(VG_Z_LIBC_SONAME, __GI_stpcpy) |
| 924 | STPCPY(VG_Z_LD_LINUX_SO_2, stpcpy) |
| 925 | STPCPY(VG_Z_LD_LINUX_X86_64_SO_2, stpcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 926 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 927 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 928 | //STPCPY(VG_Z_LIBC_SONAME, stpcpy) |
| 929 | //STPCPY(VG_Z_DYLD, stpcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 930 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 931 | #endif |
| 932 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 933 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 934 | /*---------------------- memset ----------------------*/ |
| 935 | |
| 936 | /* Why are we bothering to intercept this? It seems entirely |
| 937 | pointless. */ |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 938 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 939 | #define MEMSET(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 940 | void* VG_REPLACE_FUNCTION_EZU(20210,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 941 | (void *s, Int c, SizeT n); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 942 | void* VG_REPLACE_FUNCTION_EZU(20210,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 943 | (void *s, Int c, SizeT n) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 944 | { \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 945 | Addr a = (Addr)s; \ |
| 946 | UInt c4 = (c & 0xFF); \ |
| 947 | c4 = (c4 << 8) | c4; \ |
| 948 | c4 = (c4 << 16) | c4; \ |
| 949 | while ((a & 3) != 0 && n >= 1) \ |
| 950 | { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \ |
| 951 | while (n >= 4) \ |
| 952 | { *(UInt*)a = c4; a += 4; n -= 4; } \ |
| 953 | while (n >= 1) \ |
| 954 | { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 955 | return s; \ |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 956 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 957 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 958 | #if defined(VGO_linux) |
| 959 | MEMSET(VG_Z_LIBC_SONAME, memset) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 960 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 961 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 962 | //MEMSET(VG_Z_LIBC_SONAME, memset) |
| 963 | //MEMSET(VG_Z_DYLD, memset) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 964 | MEMSET(VG_Z_LIBC_SONAME, memset) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 965 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 966 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 967 | |
| 968 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 969 | /*---------------------- memmove ----------------------*/ |
| 970 | |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 971 | /* memmove -- use the MEMMOVE defn above. */ |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 972 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 973 | #if defined(VGO_linux) |
| 974 | MEMMOVE(VG_Z_LIBC_SONAME, memmove) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 975 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 976 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 977 | //MEMMOVE(VG_Z_LIBC_SONAME, memmove) |
| 978 | //MEMMOVE(VG_Z_DYLD, memmove)# |
| 979 | MEMMOVE(VG_Z_LIBC_SONAME, memmoveZDVARIANTZDsse3x) /* memmove$VARIANT$sse3x */ |
| 980 | MEMMOVE(VG_Z_LIBC_SONAME, memmoveZDVARIANTZDsse42) /* memmove$VARIANT$sse42 */ |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 981 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 982 | #endif |
| 983 | |
| 984 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 985 | /*---------------------- bcopy ----------------------*/ |
| 986 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 987 | #define BCOPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 988 | void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 989 | (const void *srcV, void *dstV, SizeT n); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 990 | void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 991 | (const void *srcV, void *dstV, SizeT n) \ |
| 992 | { \ |
| 993 | SizeT i; \ |
| 994 | Char* dst = (Char*)dstV; \ |
| 995 | Char* src = (Char*)srcV; \ |
| 996 | if (dst < src) { \ |
| 997 | for (i = 0; i < n; i++) \ |
| 998 | dst[i] = src[i]; \ |
| 999 | } \ |
| 1000 | else \ |
| 1001 | if (dst > src) { \ |
| 1002 | for (i = 0; i < n; i++) \ |
| 1003 | dst[n-i-1] = src[n-i-1]; \ |
| 1004 | } \ |
| 1005 | } |
| 1006 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1007 | #if defined(VGO_linux) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1008 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1009 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 1010 | //BCOPY(VG_Z_LIBC_SONAME, bcopy) |
| 1011 | //BCOPY(VG_Z_DYLD, bcopy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1012 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1013 | #endif |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 1014 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 1015 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1016 | /*-------------------- memmove_chk --------------------*/ |
| 1017 | |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1018 | /* glibc 2.5 variant of memmove which checks the dest is big enough. |
| 1019 | There is no specific part of glibc that this is copied from. */ |
| 1020 | #define GLIBC25___MEMMOVE_CHK(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1021 | void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1022 | (void *dstV, const void *srcV, SizeT n, SizeT destlen); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1023 | void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1024 | (void *dstV, const void *srcV, SizeT n, SizeT destlen) \ |
| 1025 | { \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1026 | SizeT i; \ |
| 1027 | Char* dst = (Char*)dstV; \ |
| 1028 | Char* src = (Char*)srcV; \ |
| 1029 | if (destlen < n) \ |
| 1030 | goto badness; \ |
| 1031 | if (dst < src) { \ |
| 1032 | for (i = 0; i < n; i++) \ |
| 1033 | dst[i] = src[i]; \ |
| 1034 | } \ |
| 1035 | else \ |
| 1036 | if (dst > src) { \ |
| 1037 | for (i = 0; i < n; i++) \ |
| 1038 | dst[n-i-1] = src[n-i-1]; \ |
| 1039 | } \ |
| 1040 | return dst; \ |
| 1041 | badness: \ |
| 1042 | VALGRIND_PRINTF_BACKTRACE( \ |
| 1043 | "*** memmove_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1044 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1045 | my_exit(127); \ |
sewardj | c271ec8 | 2007-02-27 22:36:14 +0000 | [diff] [blame] | 1046 | /*NOTREACHED*/ \ |
| 1047 | return NULL; \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1050 | #if defined(VGO_linux) |
| 1051 | GLIBC25___MEMMOVE_CHK(VG_Z_LIBC_SONAME, __memmove_chk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1052 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1053 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1054 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1055 | #endif |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1056 | |
| 1057 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1058 | /*-------------------- strchrnul --------------------*/ |
| 1059 | |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1060 | /* Find the first occurrence of C in S or the final NUL byte. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1061 | #define GLIBC232_STRCHRNUL(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1062 | char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1063 | (const char* s, int c_in); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1064 | char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1065 | (const char* s, int c_in) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1066 | { \ |
| 1067 | unsigned char c = (unsigned char) c_in; \ |
| 1068 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 1069 | while (1) { \ |
| 1070 | if (*char_ptr == 0) return char_ptr; \ |
| 1071 | if (*char_ptr == c) return char_ptr; \ |
| 1072 | char_ptr++; \ |
| 1073 | } \ |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1074 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1075 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1076 | #if defined(VGO_linux) |
| 1077 | GLIBC232_STRCHRNUL(VG_Z_LIBC_SONAME, strchrnul) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1078 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1079 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1080 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1081 | #endif |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1082 | |
| 1083 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1084 | /*---------------------- rawmemchr ----------------------*/ |
| 1085 | |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1086 | /* Find the first occurrence of C in S. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1087 | #define GLIBC232_RAWMEMCHR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1088 | char* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1089 | (const char* s, int c_in); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1090 | char* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1091 | (const char* s, int c_in) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1092 | { \ |
| 1093 | unsigned char c = (unsigned char) c_in; \ |
| 1094 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 1095 | while (1) { \ |
| 1096 | if (*char_ptr == c) return char_ptr; \ |
| 1097 | char_ptr++; \ |
| 1098 | } \ |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1099 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1100 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 1101 | #if defined (VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1102 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, rawmemchr) |
| 1103 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, __GI___rawmemchr) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1104 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1105 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1106 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 1107 | #endif |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1108 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1109 | |
| 1110 | /*---------------------- strcpy_chk ----------------------*/ |
| 1111 | |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 1112 | /* glibc variant of strcpy that checks the dest is big enough. |
| 1113 | Copied from glibc-2.5/debug/test-strcpy_chk.c. */ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1114 | #define GLIBC25___STRCPY_CHK(soname,fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1115 | char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1116 | (char* dst, const char* src, SizeT len); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1117 | char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1118 | (char* dst, const char* src, SizeT len) \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1119 | { \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1120 | char* ret = dst; \ |
| 1121 | if (! len) \ |
| 1122 | goto badness; \ |
| 1123 | while ((*dst++ = *src++) != '\0') \ |
| 1124 | if (--len == 0) \ |
| 1125 | goto badness; \ |
| 1126 | return ret; \ |
| 1127 | badness: \ |
| 1128 | VALGRIND_PRINTF_BACKTRACE( \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1129 | "*** strcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1130 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1131 | my_exit(127); \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1132 | /*NOTREACHED*/ \ |
| 1133 | return NULL; \ |
| 1134 | } |
| 1135 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1136 | #if defined(VGO_linux) |
| 1137 | GLIBC25___STRCPY_CHK(VG_Z_LIBC_SONAME, __strcpy_chk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1138 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1139 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1140 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1141 | #endif |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1142 | |
| 1143 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1144 | /*---------------------- stpcpy_chk ----------------------*/ |
| 1145 | |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 1146 | /* glibc variant of stpcpy that checks the dest is big enough. |
| 1147 | Copied from glibc-2.5/debug/test-stpcpy_chk.c. */ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1148 | #define GLIBC25___STPCPY_CHK(soname,fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1149 | char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1150 | (char* dst, const char* src, SizeT len); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1151 | char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1152 | (char* dst, const char* src, SizeT len) \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1153 | { \ |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 1154 | if (! len) \ |
| 1155 | goto badness; \ |
| 1156 | while ((*dst++ = *src++) != '\0') \ |
| 1157 | if (--len == 0) \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1158 | goto badness; \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1159 | return dst - 1; \ |
| 1160 | badness: \ |
| 1161 | VALGRIND_PRINTF_BACKTRACE( \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1162 | "*** stpcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1163 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1164 | my_exit(127); \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1165 | /*NOTREACHED*/ \ |
| 1166 | return NULL; \ |
| 1167 | } |
| 1168 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1169 | #if defined(VGO_linux) |
| 1170 | GLIBC25___STPCPY_CHK(VG_Z_LIBC_SONAME, __stpcpy_chk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1171 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1172 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1173 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1174 | #endif |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1175 | |
| 1176 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1177 | /*---------------------- mempcpy ----------------------*/ |
| 1178 | |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 1179 | /* mempcpy */ |
| 1180 | #define GLIBC25_MEMPCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1181 | void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \ |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 1182 | ( void *dst, const void *src, SizeT len ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1183 | void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \ |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 1184 | ( void *dst, const void *src, SizeT len ) \ |
| 1185 | { \ |
| 1186 | register char *d; \ |
| 1187 | register char *s; \ |
| 1188 | SizeT len_saved = len; \ |
| 1189 | \ |
| 1190 | if (len == 0) \ |
| 1191 | return dst; \ |
| 1192 | \ |
| 1193 | if (is_overlap(dst, src, len, len)) \ |
| 1194 | RECORD_OVERLAP_ERROR("mempcpy", dst, src, len); \ |
| 1195 | \ |
| 1196 | if ( dst > src ) { \ |
| 1197 | d = (char *)dst + len - 1; \ |
| 1198 | s = (char *)src + len - 1; \ |
| 1199 | while ( len-- ) { \ |
| 1200 | *d-- = *s--; \ |
| 1201 | } \ |
| 1202 | } else if ( dst < src ) { \ |
| 1203 | d = (char *)dst; \ |
| 1204 | s = (char *)src; \ |
| 1205 | while ( len-- ) { \ |
| 1206 | *d++ = *s++; \ |
| 1207 | } \ |
| 1208 | } \ |
| 1209 | return (void*)( ((char*)dst) + len_saved ); \ |
| 1210 | } |
| 1211 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 1212 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1213 | GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy) |
| 1214 | GLIBC25_MEMPCPY(VG_Z_LD_SO_1, mempcpy) /* ld.so.1 */ |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1215 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1216 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame^] | 1217 | //GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1218 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 1219 | #endif |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 1220 | |
| 1221 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1222 | /*-------------------- memcpy_chk --------------------*/ |
| 1223 | |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1224 | #define GLIBC26___MEMCPY_CHK(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1225 | void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1226 | (void* dst, const void* src, SizeT len, SizeT dstlen ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1227 | void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1228 | (void* dst, const void* src, SizeT len, SizeT dstlen ) \ |
| 1229 | { \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1230 | register char *d; \ |
| 1231 | register char *s; \ |
| 1232 | \ |
| 1233 | if (dstlen < len) goto badness; \ |
| 1234 | \ |
| 1235 | if (len == 0) \ |
| 1236 | return dst; \ |
| 1237 | \ |
| 1238 | if (is_overlap(dst, src, len, len)) \ |
| 1239 | RECORD_OVERLAP_ERROR("memcpy_chk", dst, src, len); \ |
| 1240 | \ |
| 1241 | if ( dst > src ) { \ |
| 1242 | d = (char *)dst + len - 1; \ |
| 1243 | s = (char *)src + len - 1; \ |
| 1244 | while ( len-- ) { \ |
| 1245 | *d-- = *s--; \ |
| 1246 | } \ |
| 1247 | } else if ( dst < src ) { \ |
| 1248 | d = (char *)dst; \ |
| 1249 | s = (char *)src; \ |
| 1250 | while ( len-- ) { \ |
| 1251 | *d++ = *s++; \ |
| 1252 | } \ |
| 1253 | } \ |
| 1254 | return dst; \ |
| 1255 | badness: \ |
| 1256 | VALGRIND_PRINTF_BACKTRACE( \ |
| 1257 | "*** memcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1258 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1259 | my_exit(127); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1260 | /*NOTREACHED*/ \ |
| 1261 | return NULL; \ |
| 1262 | } |
| 1263 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1264 | #if defined(VGO_linux) |
| 1265 | GLIBC26___MEMCPY_CHK(VG_Z_LIBC_SONAME, __memcpy_chk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1266 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1267 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1268 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1269 | #endif |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1270 | |
| 1271 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1272 | /*---------------------- strstr ----------------------*/ |
| 1273 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1274 | #define STRSTR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1275 | void* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1276 | (void* haystack, void* needle); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1277 | void* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1278 | (void* haystack, void* needle) \ |
| 1279 | { \ |
| 1280 | UChar* h = (UChar*)haystack; \ |
| 1281 | UChar* n = (UChar*)needle; \ |
| 1282 | \ |
| 1283 | /* find the length of n, not including terminating zero */ \ |
| 1284 | UWord nlen = 0; \ |
| 1285 | while (n[nlen]) nlen++; \ |
| 1286 | \ |
| 1287 | /* if n is the empty string, match immediately. */ \ |
| 1288 | if (nlen == 0) return h; \ |
| 1289 | \ |
| 1290 | /* assert(nlen >= 1); */ \ |
| 1291 | UChar n0 = n[0]; \ |
| 1292 | \ |
| 1293 | while (1) { \ |
| 1294 | UChar hh = *h; \ |
| 1295 | if (hh == 0) return NULL; \ |
| 1296 | if (hh != n0) { h++; continue; } \ |
| 1297 | \ |
| 1298 | UWord i; \ |
| 1299 | for (i = 0; i < nlen; i++) { \ |
| 1300 | if (n[i] != h[i]) \ |
| 1301 | break; \ |
| 1302 | } \ |
| 1303 | /* assert(i >= 0 && i <= nlen); */ \ |
| 1304 | if (i == nlen) \ |
| 1305 | return h; \ |
| 1306 | \ |
| 1307 | h++; \ |
| 1308 | } \ |
| 1309 | } |
| 1310 | |
| 1311 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1312 | STRSTR(VG_Z_LIBC_SONAME, strstr) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1313 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1314 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1315 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1316 | #endif |
| 1317 | |
| 1318 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1319 | /*---------------------- strpbrk ----------------------*/ |
| 1320 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1321 | #define STRPBRK(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1322 | void* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1323 | (void* sV, void* acceptV); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1324 | void* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1325 | (void* sV, void* acceptV) \ |
| 1326 | { \ |
| 1327 | UChar* s = (UChar*)sV; \ |
| 1328 | UChar* accept = (UChar*)acceptV; \ |
| 1329 | \ |
| 1330 | /* find the length of 'accept', not including terminating zero */ \ |
| 1331 | UWord nacc = 0; \ |
| 1332 | while (accept[nacc]) nacc++; \ |
| 1333 | \ |
| 1334 | /* if n is the empty string, fail immediately. */ \ |
| 1335 | if (nacc == 0) return NULL; \ |
| 1336 | \ |
| 1337 | /* assert(nacc >= 1); */ \ |
| 1338 | while (1) { \ |
| 1339 | UWord i; \ |
| 1340 | UChar sc = *s; \ |
| 1341 | if (sc == 0) \ |
| 1342 | break; \ |
| 1343 | for (i = 0; i < nacc; i++) { \ |
| 1344 | if (sc == accept[i]) \ |
| 1345 | return s; \ |
| 1346 | } \ |
| 1347 | s++; \ |
| 1348 | } \ |
| 1349 | \ |
| 1350 | return NULL; \ |
| 1351 | } |
| 1352 | |
| 1353 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1354 | STRPBRK(VG_Z_LIBC_SONAME, strpbrk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1355 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1356 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1357 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1358 | #endif |
| 1359 | |
| 1360 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1361 | /*---------------------- strcspn ----------------------*/ |
| 1362 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1363 | #define STRCSPN(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1364 | SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1365 | (void* sV, void* rejectV); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1366 | SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1367 | (void* sV, void* rejectV) \ |
| 1368 | { \ |
| 1369 | UChar* s = (UChar*)sV; \ |
| 1370 | UChar* reject = (UChar*)rejectV; \ |
| 1371 | \ |
| 1372 | /* find the length of 'reject', not including terminating zero */ \ |
| 1373 | UWord nrej = 0; \ |
| 1374 | while (reject[nrej]) nrej++; \ |
| 1375 | \ |
| 1376 | UWord len = 0; \ |
| 1377 | while (1) { \ |
| 1378 | UWord i; \ |
| 1379 | UChar sc = *s; \ |
| 1380 | if (sc == 0) \ |
| 1381 | break; \ |
| 1382 | for (i = 0; i < nrej; i++) { \ |
| 1383 | if (sc == reject[i]) \ |
| 1384 | break; \ |
| 1385 | } \ |
| 1386 | /* assert(i >= 0 && i <= nrej); */ \ |
| 1387 | if (i < nrej) \ |
| 1388 | break; \ |
| 1389 | s++; \ |
| 1390 | len++; \ |
| 1391 | } \ |
| 1392 | \ |
| 1393 | return len; \ |
| 1394 | } |
| 1395 | |
| 1396 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1397 | STRCSPN(VG_Z_LIBC_SONAME, strcspn) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1398 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1399 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1400 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1401 | #endif |
| 1402 | |
| 1403 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1404 | /*---------------------- strspn ----------------------*/ |
| 1405 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1406 | #define STRSPN(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1407 | SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1408 | (void* sV, void* acceptV); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1409 | SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1410 | (void* sV, void* acceptV) \ |
| 1411 | { \ |
| 1412 | UChar* s = (UChar*)sV; \ |
| 1413 | UChar* accept = (UChar*)acceptV; \ |
| 1414 | \ |
| 1415 | /* find the length of 'accept', not including terminating zero */ \ |
| 1416 | UWord nacc = 0; \ |
| 1417 | while (accept[nacc]) nacc++; \ |
| 1418 | if (nacc == 0) return 0; \ |
| 1419 | \ |
| 1420 | UWord len = 0; \ |
| 1421 | while (1) { \ |
| 1422 | UWord i; \ |
| 1423 | UChar sc = *s; \ |
| 1424 | if (sc == 0) \ |
| 1425 | break; \ |
| 1426 | for (i = 0; i < nacc; i++) { \ |
| 1427 | if (sc == accept[i]) \ |
| 1428 | break; \ |
| 1429 | } \ |
| 1430 | /* assert(i >= 0 && i <= nacc); */ \ |
| 1431 | if (i == nacc) \ |
| 1432 | break; \ |
| 1433 | s++; \ |
| 1434 | len++; \ |
| 1435 | } \ |
| 1436 | \ |
| 1437 | return len; \ |
| 1438 | } |
| 1439 | |
| 1440 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1441 | STRSPN(VG_Z_LIBC_SONAME, strspn) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1442 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1443 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1444 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1445 | #endif |
sewardj | ba18935 | 2010-08-20 18:24:16 +0000 | [diff] [blame] | 1446 | |
| 1447 | |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1448 | /*------------------------------------------------------------*/ |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1449 | /*--- Improve definedness checking of process environment ---*/ |
| 1450 | /*------------------------------------------------------------*/ |
| 1451 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1452 | #if defined(VGO_linux) |
| 1453 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1454 | /* If these wind up getting generated via a macro, so that multiple |
| 1455 | versions of each function exist (as above), use the _EZU variants |
| 1456 | to assign equivalance class tags. */ |
| 1457 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1458 | /*---------------------- putenv ----------------------*/ |
| 1459 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1460 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string); |
| 1461 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1462 | { |
| 1463 | OrigFn fn; |
| 1464 | Word result; |
| 1465 | const char* p = string; |
| 1466 | VALGRIND_GET_ORIG_FN(fn); |
| 1467 | /* Now by walking over the string we magically produce |
| 1468 | traces when hitting undefined memory. */ |
| 1469 | if (p) |
| 1470 | while (*p++) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1471 | __asm__ __volatile__("" ::: "memory"); |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1472 | CALL_FN_W_W(result, fn, string); |
| 1473 | return result; |
| 1474 | } |
| 1475 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1476 | |
| 1477 | /*---------------------- unsetenv ----------------------*/ |
| 1478 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1479 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name); |
| 1480 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1481 | { |
| 1482 | OrigFn fn; |
| 1483 | Word result; |
| 1484 | const char* p = name; |
| 1485 | VALGRIND_GET_ORIG_FN(fn); |
| 1486 | /* Now by walking over the string we magically produce |
| 1487 | traces when hitting undefined memory. */ |
| 1488 | if (p) |
| 1489 | while (*p++) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1490 | __asm__ __volatile__("" ::: "memory"); |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1491 | CALL_FN_W_W(result, fn, name); |
| 1492 | return result; |
| 1493 | } |
| 1494 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1495 | |
| 1496 | /*---------------------- setenv ----------------------*/ |
| 1497 | |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1498 | /* setenv */ |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1499 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1500 | (const char* name, const char* value, int overwrite); |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1501 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1502 | (const char* name, const char* value, int overwrite) |
| 1503 | { |
| 1504 | OrigFn fn; |
| 1505 | Word result; |
| 1506 | const char* p; |
| 1507 | VALGRIND_GET_ORIG_FN(fn); |
| 1508 | /* Now by walking over the string we magically produce |
| 1509 | traces when hitting undefined memory. */ |
| 1510 | if (name) |
| 1511 | for (p = name; *p; p++) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1512 | __asm__ __volatile__("" ::: "memory"); |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1513 | if (value) |
| 1514 | for (p = value; *p; p++) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1515 | __asm__ __volatile__("" ::: "memory"); |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1516 | VALGRIND_CHECK_VALUE_IS_DEFINED (overwrite); |
| 1517 | CALL_FN_W_WWW(result, fn, name, value, overwrite); |
| 1518 | return result; |
| 1519 | } |
| 1520 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1521 | #endif /* defined(VGO_linux) */ |
| 1522 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1523 | /*--------------------------------------------------------------------*/ |
njn | 4627586 | 2005-03-24 04:00:03 +0000 | [diff] [blame] | 1524 | /*--- end ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1525 | /*--------------------------------------------------------------------*/ |