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 | |
| 157 | #define STRRCHR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 158 | char* VG_REPLACE_FUNCTION_EZU(20010,soname,fnname)( const char* s, int c ); \ |
| 159 | char* VG_REPLACE_FUNCTION_EZU(20010,soname,fnname)( const char* s, int c ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 160 | { \ |
| 161 | UChar ch = (UChar)((UInt)c); \ |
| 162 | UChar* p = (UChar*)s; \ |
| 163 | UChar* last = NULL; \ |
| 164 | while (True) { \ |
| 165 | if (*p == ch) last = p; \ |
| 166 | if (*p == 0) return last; \ |
| 167 | p++; \ |
| 168 | } \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 169 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 170 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 171 | // Apparently rindex() is the same thing as strrchr() |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 172 | STRRCHR(VG_Z_LIBC_SONAME, strrchr) |
| 173 | STRRCHR(VG_Z_LIBC_SONAME, rindex) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 174 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 175 | STRRCHR(VG_Z_LIBC_SONAME, __GI_strrchr) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 176 | STRRCHR(VG_Z_LD_LINUX_SO_2, rindex) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 177 | #elif defined(VGO_darwin) |
| 178 | STRRCHR(VG_Z_DYLD, strrchr) |
| 179 | STRRCHR(VG_Z_DYLD, rindex) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 180 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | #define STRCHR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 184 | char* VG_REPLACE_FUNCTION_EZU(20020,soname,fnname) ( const char* s, int c ); \ |
| 185 | char* VG_REPLACE_FUNCTION_EZU(20020,soname,fnname) ( const char* s, int c ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 186 | { \ |
| 187 | UChar ch = (UChar)((UInt)c); \ |
| 188 | UChar* p = (UChar*)s; \ |
| 189 | while (True) { \ |
| 190 | if (*p == ch) return p; \ |
| 191 | if (*p == 0) return NULL; \ |
| 192 | p++; \ |
| 193 | } \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 194 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 195 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 196 | // Apparently index() is the same thing as strchr() |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 197 | STRCHR(VG_Z_LIBC_SONAME, strchr) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 198 | STRCHR(VG_Z_LIBC_SONAME, index) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 199 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 200 | STRCHR(VG_Z_LIBC_SONAME, __GI_strchr) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 201 | STRCHR(VG_Z_LD_LINUX_SO_2, strchr) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 202 | STRCHR(VG_Z_LD_LINUX_SO_2, index) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 203 | STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, strchr) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 204 | STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, index) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 205 | #elif defined(VGO_darwin) |
| 206 | STRCHR(VG_Z_DYLD, strchr) |
| 207 | STRCHR(VG_Z_DYLD, index) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 208 | #endif |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 209 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 210 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 211 | #define STRCAT(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 212 | char* VG_REPLACE_FUNCTION_EZU(20030,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 213 | ( char* dst, const char* src ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 214 | char* VG_REPLACE_FUNCTION_EZU(20030,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 215 | ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 216 | { \ |
| 217 | const Char* src_orig = src; \ |
| 218 | Char* dst_orig = dst; \ |
| 219 | while (*dst) dst++; \ |
| 220 | while (*src) *dst++ = *src++; \ |
| 221 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 222 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 223 | /* This is a bit redundant, I think; any overlap and the strcat will */ \ |
| 224 | /* go forever... or until a seg fault occurs. */ \ |
| 225 | if (is_overlap(dst_orig, \ |
| 226 | src_orig, \ |
| 227 | (Addr)dst-(Addr)dst_orig+1, \ |
| 228 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 229 | RECORD_OVERLAP_ERROR("strcat", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 230 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 231 | return dst_orig; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 232 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 233 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 234 | STRCAT(VG_Z_LIBC_SONAME, strcat) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 235 | #if defined(VGO_linux) |
| 236 | STRCAT(VG_Z_LIBC_SONAME, __GI_strcat) |
| 237 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 238 | |
| 239 | #define STRNCAT(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 240 | char* VG_REPLACE_FUNCTION_EZU(20040,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 241 | ( char* dst, const char* src, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 242 | char* VG_REPLACE_FUNCTION_EZU(20040,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 243 | ( char* dst, const char* src, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 244 | { \ |
| 245 | const Char* src_orig = src; \ |
| 246 | Char* dst_orig = dst; \ |
| 247 | SizeT m = 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 | while (*dst) dst++; \ |
| 250 | while (m < n && *src) { m++; *dst++ = *src++; } /* concat <= n chars */ \ |
| 251 | *dst = 0; /* always add null */ \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 252 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 253 | /* This checks for overlap after copying, unavoidable without */ \ |
| 254 | /* pre-counting lengths... should be ok */ \ |
| 255 | if (is_overlap(dst_orig, \ |
| 256 | src_orig, \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 257 | (Addr)dst-(Addr)dst_orig+1, \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 258 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 259 | RECORD_OVERLAP_ERROR("strncat", dst_orig, src_orig, n); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 260 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 261 | return dst_orig; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 262 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 263 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 264 | STRNCAT(VG_Z_LIBC_SONAME, strncat) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 265 | #if defined(VGO_darwin) |
| 266 | STRNCAT(VG_Z_DYLD, strncat) |
| 267 | #endif |
| 268 | |
| 269 | |
| 270 | /* Append src to dst. n is the size of dst's buffer. dst is guaranteed |
| 271 | to be nul-terminated after the copy, unless n <= strlen(dst_orig). |
| 272 | Returns min(n, strlen(dst_orig)) + strlen(src_orig). |
| 273 | Truncation occurred if retval >= n. |
| 274 | */ |
| 275 | #define STRLCAT(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 276 | SizeT VG_REPLACE_FUNCTION_EZU(20050,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 277 | ( char* dst, const char* src, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 278 | SizeT VG_REPLACE_FUNCTION_EZU(20050,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 279 | ( char* dst, const char* src, SizeT n ) \ |
| 280 | { \ |
| 281 | const Char* src_orig = src; \ |
| 282 | Char* dst_orig = dst; \ |
| 283 | SizeT m = 0; \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 284 | \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 285 | while (m < n && *dst) { m++; dst++; } \ |
| 286 | if (m < n) { \ |
| 287 | /* Fill as far as dst_orig[n-2], then nul-terminate. */ \ |
| 288 | while (m < n-1 && *src) { m++; *dst++ = *src++; } \ |
| 289 | *dst = 0; \ |
| 290 | } else { \ |
| 291 | /* No space to copy anything to dst. m == n */ \ |
| 292 | } \ |
| 293 | /* Finish counting min(n, strlen(dst_orig)) + strlen(src_orig) */ \ |
| 294 | while (*src) { m++; src++; } \ |
| 295 | /* This checks for overlap after copying, unavoidable without */ \ |
| 296 | /* pre-counting lengths... should be ok */ \ |
| 297 | if (is_overlap(dst_orig, \ |
| 298 | src_orig, \ |
| 299 | (Addr)dst-(Addr)dst_orig+1, \ |
| 300 | (Addr)src-(Addr)src_orig+1)) \ |
| 301 | RECORD_OVERLAP_ERROR("strlcat", dst_orig, src_orig, n); \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 302 | \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 303 | return m; \ |
| 304 | } |
| 305 | |
| 306 | #if defined(VGO_darwin) |
| 307 | STRLCAT(VG_Z_LIBC_SONAME, strlcat) |
| 308 | STRLCAT(VG_Z_DYLD, strlcat) |
| 309 | #endif |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 310 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 311 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 312 | #define STRNLEN(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 313 | SizeT VG_REPLACE_FUNCTION_EZU(20060,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 314 | ( const char* str, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 315 | SizeT VG_REPLACE_FUNCTION_EZU(20060,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 316 | ( const char* str, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 317 | { \ |
| 318 | SizeT i = 0; \ |
| 319 | while (i < n && str[i] != 0) i++; \ |
| 320 | return i; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 321 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 322 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 323 | STRNLEN(VG_Z_LIBC_SONAME, strnlen) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 324 | #if defined(VGO_linux) |
| 325 | STRNLEN(VG_Z_LIBC_SONAME, __GI_strnlen) |
| 326 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 327 | |
sewardj | 3ceec24 | 2003-07-30 21:24:25 +0000 | [diff] [blame] | 328 | |
njn | 5ec15ed | 2005-08-24 19:55:51 +0000 | [diff] [blame] | 329 | // Note that this replacement often doesn't get used because gcc inlines |
| 330 | // calls to strlen() with its own built-in version. This can be very |
| 331 | // confusing if you aren't expecting it. Other small functions in this file |
| 332 | // may also be inline by gcc. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 333 | #define STRLEN(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 334 | SizeT VG_REPLACE_FUNCTION_EZU(20070,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 335 | ( const char* str ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 336 | SizeT VG_REPLACE_FUNCTION_EZU(20070,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 337 | ( const char* str ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 338 | { \ |
| 339 | SizeT i = 0; \ |
| 340 | while (str[i] != 0) i++; \ |
| 341 | return i; \ |
sewardj | 3ceec24 | 2003-07-30 21:24:25 +0000 | [diff] [blame] | 342 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 343 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 344 | STRLEN(VG_Z_LIBC_SONAME, strlen) |
| 345 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 346 | STRLEN(VG_Z_LIBC_SONAME, __GI_strlen) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 347 | #endif |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 348 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 349 | |
| 350 | #define STRCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 351 | char* VG_REPLACE_FUNCTION_EZU(20080,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 352 | ( char* dst, const char* src ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 353 | char* VG_REPLACE_FUNCTION_EZU(20080,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 354 | ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 355 | { \ |
| 356 | const Char* src_orig = src; \ |
| 357 | Char* dst_orig = dst; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 358 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 359 | while (*src) *dst++ = *src++; \ |
| 360 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 361 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 362 | /* This checks for overlap after copying, unavoidable without */ \ |
| 363 | /* pre-counting length... should be ok */ \ |
| 364 | if (is_overlap(dst_orig, \ |
| 365 | src_orig, \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 366 | (Addr)dst-(Addr)dst_orig+1, \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 367 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 368 | RECORD_OVERLAP_ERROR("strcpy", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 369 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 370 | return dst_orig; \ |
| 371 | } |
| 372 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 373 | STRCPY(VG_Z_LIBC_SONAME, strcpy) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 374 | #if defined(VGO_linux) |
| 375 | STRCPY(VG_Z_LIBC_SONAME, __GI_strcpy) |
| 376 | #elif defined(VGO_darwin) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 377 | STRCPY(VG_Z_DYLD, strcpy) |
| 378 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | #define STRNCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 382 | char* VG_REPLACE_FUNCTION_EZU(20090,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 383 | ( char* dst, const char* src, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 384 | char* VG_REPLACE_FUNCTION_EZU(20090,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 385 | ( char* dst, const char* src, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 386 | { \ |
| 387 | const Char* src_orig = src; \ |
| 388 | Char* dst_orig = dst; \ |
| 389 | SizeT m = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 390 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 391 | while (m < n && *src) { m++; *dst++ = *src++; } \ |
| 392 | /* Check for overlap after copying; all n bytes of dst are relevant, */ \ |
| 393 | /* but only m+1 bytes of src if terminator was found */ \ |
| 394 | if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 395 | RECORD_OVERLAP_ERROR("strncpy", dst, src, n); \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 396 | while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \ |
| 397 | \ |
| 398 | return dst_orig; \ |
| 399 | } |
| 400 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 401 | STRNCPY(VG_Z_LIBC_SONAME, strncpy) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 402 | #if defined(VGO_linux) |
| 403 | STRNCPY(VG_Z_LIBC_SONAME, __GI_strncpy) |
| 404 | #elif defined(VGO_darwin) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 405 | STRNCPY(VG_Z_DYLD, strncpy) |
| 406 | #endif |
| 407 | |
| 408 | |
| 409 | /* Copy up to n-1 bytes from src to dst. Then nul-terminate dst if n > 0. |
| 410 | Returns strlen(src). Does not zero-fill the remainder of dst. */ |
| 411 | #define STRLCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 412 | SizeT VG_REPLACE_FUNCTION_EZU(20100,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 413 | ( char* dst, const char* src, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 414 | SizeT VG_REPLACE_FUNCTION_EZU(20100,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 415 | ( char* dst, const char* src, SizeT n ) \ |
| 416 | { \ |
| 417 | const char* src_orig = src; \ |
| 418 | char* dst_orig = dst; \ |
| 419 | SizeT m = 0; \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 420 | \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 421 | while (m < n-1 && *src) { m++; *dst++ = *src++; } \ |
| 422 | /* m non-nul bytes have now been copied, and m <= n-1. */ \ |
| 423 | /* Check for overlap after copying; all n bytes of dst are relevant, */ \ |
| 424 | /* but only m+1 bytes of src if terminator was found */ \ |
| 425 | if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \ |
| 426 | RECORD_OVERLAP_ERROR("strlcpy", dst, src, n); \ |
| 427 | /* Nul-terminate dst. */ \ |
| 428 | if (n > 0) *dst = 0; \ |
| 429 | /* Finish counting strlen(src). */ \ |
| 430 | while (*src) src++; \ |
| 431 | return src - src_orig; \ |
| 432 | } |
| 433 | |
| 434 | #if defined(VGO_darwin) |
| 435 | STRLCPY(VG_Z_LIBC_SONAME, strlcpy) |
| 436 | STRLCPY(VG_Z_DYLD, strlcpy) |
| 437 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 438 | |
| 439 | |
| 440 | #define STRNCMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 441 | int VG_REPLACE_FUNCTION_EZU(20110,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 442 | ( const char* s1, const char* s2, SizeT nmax ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 443 | int VG_REPLACE_FUNCTION_EZU(20110,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 444 | ( const char* s1, const char* s2, SizeT nmax ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 445 | { \ |
| 446 | SizeT n = 0; \ |
| 447 | while (True) { \ |
| 448 | if (n >= nmax) return 0; \ |
| 449 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 450 | if (*s1 == 0) return -1; \ |
| 451 | if (*s2 == 0) return 1; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 452 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 453 | if (*(unsigned char*)s1 < *(unsigned char*)s2) return -1; \ |
| 454 | if (*(unsigned char*)s1 > *(unsigned char*)s2) return 1; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 455 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 456 | s1++; s2++; n++; \ |
| 457 | } \ |
| 458 | } |
| 459 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 460 | STRNCMP(VG_Z_LIBC_SONAME, strncmp) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 461 | #if defined(VGO_linux) |
| 462 | STRNCMP(VG_Z_LIBC_SONAME, __GI_strncmp) |
| 463 | #elif defined(VGO_darwin) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 464 | STRNCMP(VG_Z_DYLD, strncmp) |
| 465 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 466 | |
| 467 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 468 | #define STRCASECMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 469 | int VG_REPLACE_FUNCTION_EZU(20120,soname,fnname) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 470 | ( const char* s1, const char* s2 ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 471 | int VG_REPLACE_FUNCTION_EZU(20120,soname,fnname) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 472 | ( const char* s1, const char* s2 ) \ |
| 473 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 474 | extern int tolower(int); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 475 | register unsigned char c1; \ |
| 476 | register unsigned char c2; \ |
| 477 | while (True) { \ |
| 478 | c1 = tolower(*(unsigned char *)s1); \ |
| 479 | c2 = tolower(*(unsigned char *)s2); \ |
| 480 | if (c1 != c2) break; \ |
| 481 | if (c1 == 0) break; \ |
| 482 | s1++; s2++; \ |
| 483 | } \ |
| 484 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 485 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 486 | return 0; \ |
| 487 | } |
| 488 | |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 489 | #if !defined(VGPV_arm_linux_android) |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 490 | STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp) |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 491 | #endif |
| 492 | #if defined(VGO_linux) && !defined(VGPV_arm_linux_android) |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 493 | STRCASECMP(VG_Z_LIBC_SONAME, __GI_strcasecmp) |
| 494 | #endif |
| 495 | |
| 496 | |
| 497 | #define STRNCASECMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 498 | int VG_REPLACE_FUNCTION_EZU(20130,soname,fnname) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 499 | ( const char* s1, const char* s2, SizeT nmax ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 500 | int VG_REPLACE_FUNCTION_EZU(20130,soname,fnname) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 501 | ( const char* s1, const char* s2, SizeT nmax ) \ |
| 502 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 503 | extern int tolower(int); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 504 | SizeT n = 0; \ |
| 505 | while (True) { \ |
| 506 | if (n >= nmax) return 0; \ |
| 507 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 508 | if (*s1 == 0) return -1; \ |
| 509 | if (*s2 == 0) return 1; \ |
| 510 | \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 511 | if (tolower(*(unsigned char*)s1) \ |
| 512 | < tolower(*(unsigned char*)s2)) return -1; \ |
| 513 | if (tolower(*(unsigned char*)s1) \ |
| 514 | > tolower(*(unsigned char*)s2)) return 1; \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 515 | \ |
| 516 | s1++; s2++; n++; \ |
| 517 | } \ |
| 518 | } |
| 519 | |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 520 | #if !defined(VGPV_arm_linux_android) |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 521 | STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp) |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 522 | #endif |
| 523 | #if defined(VGO_linux) && !defined(VGPV_arm_linux_android) |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 524 | STRNCASECMP(VG_Z_LIBC_SONAME, __GI_strncasecmp) |
| 525 | #elif defined(VGO_darwin) |
| 526 | STRNCASECMP(VG_Z_DYLD, strncasecmp) |
| 527 | #endif |
| 528 | |
| 529 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 530 | #define STRCASECMP_L(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 531 | int VG_REPLACE_FUNCTION_EZU(20140,soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 532 | ( const char* s1, const char* s2, void* locale ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 533 | int VG_REPLACE_FUNCTION_EZU(20140,soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 534 | ( const char* s1, const char* s2, void* locale ) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 535 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 536 | extern int tolower_l(int, void*) __attribute__((weak)); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 537 | register unsigned char c1; \ |
| 538 | register unsigned char c2; \ |
| 539 | while (True) { \ |
| 540 | c1 = tolower_l(*(unsigned char *)s1, locale); \ |
| 541 | c2 = tolower_l(*(unsigned char *)s2, locale); \ |
| 542 | if (c1 != c2) break; \ |
| 543 | if (c1 == 0) break; \ |
| 544 | s1++; s2++; \ |
| 545 | } \ |
| 546 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 547 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 548 | return 0; \ |
| 549 | } |
| 550 | |
| 551 | STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l) |
| 552 | #if defined(VGO_linux) |
| 553 | STRCASECMP_L(VG_Z_LIBC_SONAME, __GI_strcasecmp_l) |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 554 | STRCASECMP_L(VG_Z_LIBC_SONAME, __GI___strcasecmp_l) |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 555 | #endif |
| 556 | |
| 557 | |
| 558 | #define STRNCASECMP_L(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 559 | int VG_REPLACE_FUNCTION_EZU(20150,soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 560 | ( const char* s1, const char* s2, SizeT nmax, void* locale ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 561 | int VG_REPLACE_FUNCTION_EZU(20150,soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 562 | ( const char* s1, const char* s2, SizeT nmax, void* locale ) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 563 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 564 | extern int tolower_l(int, void*) __attribute__((weak)); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 565 | SizeT n = 0; \ |
| 566 | while (True) { \ |
| 567 | if (n >= nmax) return 0; \ |
| 568 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 569 | if (*s1 == 0) return -1; \ |
| 570 | if (*s2 == 0) return 1; \ |
| 571 | \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 572 | if (tolower_l(*(unsigned char*)s1, locale) \ |
| 573 | < tolower_l(*(unsigned char*)s2, locale)) return -1; \ |
| 574 | if (tolower_l(*(unsigned char*)s1, locale) \ |
| 575 | > tolower_l(*(unsigned char*)s2, locale)) return 1; \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 576 | \ |
| 577 | s1++; s2++; n++; \ |
| 578 | } \ |
| 579 | } |
| 580 | |
| 581 | STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l) |
| 582 | #if defined(VGO_linux) |
| 583 | STRNCASECMP_L(VG_Z_LIBC_SONAME, __GI_strncasecmp_l) |
| 584 | #elif defined(VGO_darwin) |
| 585 | STRNCASECMP_L(VG_Z_DYLD, strncasecmp_l) |
| 586 | #endif |
| 587 | |
| 588 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 589 | #define STRCMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 590 | int VG_REPLACE_FUNCTION_EZU(20160,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 591 | ( const char* s1, const char* s2 ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 592 | int VG_REPLACE_FUNCTION_EZU(20160,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 593 | ( const char* s1, const char* s2 ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 594 | { \ |
| 595 | register unsigned char c1; \ |
| 596 | register unsigned char c2; \ |
| 597 | while (True) { \ |
| 598 | c1 = *(unsigned char *)s1; \ |
| 599 | c2 = *(unsigned char *)s2; \ |
| 600 | if (c1 != c2) break; \ |
| 601 | if (c1 == 0) break; \ |
| 602 | s1++; s2++; \ |
| 603 | } \ |
| 604 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 605 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 606 | return 0; \ |
| 607 | } |
| 608 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 609 | STRCMP(VG_Z_LIBC_SONAME, strcmp) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 610 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 611 | STRCMP(VG_Z_LIBC_SONAME, __GI_strcmp) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 612 | STRCMP(VG_Z_LD_LINUX_X86_64_SO_2, strcmp) |
| 613 | STRCMP(VG_Z_LD64_SO_1, strcmp) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 614 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 615 | |
| 616 | |
| 617 | #define MEMCHR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 618 | void* VG_REPLACE_FUNCTION_EZU(20170,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 619 | (const void *s, int c, SizeT n); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 620 | void* VG_REPLACE_FUNCTION_EZU(20170,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 621 | (const void *s, int c, SizeT n) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 622 | { \ |
| 623 | SizeT i; \ |
| 624 | UChar c0 = (UChar)c; \ |
| 625 | UChar* p = (UChar*)s; \ |
| 626 | for (i = 0; i < n; i++) \ |
| 627 | if (p[i] == c0) return (void*)(&p[i]); \ |
| 628 | return NULL; \ |
| 629 | } |
| 630 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 631 | MEMCHR(VG_Z_LIBC_SONAME, memchr) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 632 | #if defined(VGO_darwin) |
| 633 | MEMCHR(VG_Z_DYLD, memchr) |
| 634 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 635 | |
| 636 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 637 | #define MEMMOVE_OR_MEMCPY(becTag, soname, fnname, do_ol_check) \ |
sewardj | d88797f | 2011-08-17 21:25:50 +0000 | [diff] [blame] | 638 | void* VG_REPLACE_FUNCTION_EZZ(becTag,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 639 | ( void *dst, const void *src, SizeT len ); \ |
sewardj | d88797f | 2011-08-17 21:25:50 +0000 | [diff] [blame] | 640 | void* VG_REPLACE_FUNCTION_EZZ(becTag,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 641 | ( void *dst, const void *src, SizeT len ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 642 | { \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 643 | if (do_ol_check && is_overlap(dst, src, len, len)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 644 | RECORD_OVERLAP_ERROR("memcpy", dst, src, len); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 645 | \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 646 | const Addr WS = sizeof(UWord); /* 8 or 4 */ \ |
| 647 | const Addr WM = WS - 1; /* 7 or 3 */ \ |
| 648 | \ |
tom | 863ab7c | 2011-08-18 08:10:20 +0000 | [diff] [blame] | 649 | if (len > 0) { \ |
| 650 | if (dst < src) { \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 651 | \ |
tom | 863ab7c | 2011-08-18 08:10:20 +0000 | [diff] [blame] | 652 | /* Copying backwards. */ \ |
| 653 | SizeT n = len; \ |
| 654 | Addr d = (Addr)dst; \ |
| 655 | Addr s = (Addr)src; \ |
| 656 | \ |
| 657 | if (((s^d) & WM) == 0) { \ |
| 658 | /* s and d have same UWord alignment. */ \ |
| 659 | /* Pull up to a UWord boundary. */ \ |
| 660 | while ((s & WM) != 0 && n >= 1) \ |
| 661 | { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \ |
| 662 | /* Copy UWords. */ \ |
| 663 | while (n >= WS) \ |
| 664 | { *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; } \ |
| 665 | if (n == 0) \ |
| 666 | return dst; \ |
| 667 | } \ |
| 668 | if (((s|d) & 1) == 0) { \ |
| 669 | /* Both are 16-aligned; copy what we can thusly. */ \ |
| 670 | while (n >= 2) \ |
| 671 | { *(UShort*)d = *(UShort*)s; s += 2; d += 2; n -= 2; } \ |
| 672 | } \ |
| 673 | /* Copy leftovers, or everything if misaligned. */ \ |
| 674 | while (n >= 1) \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 675 | { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 676 | \ |
tom | 863ab7c | 2011-08-18 08:10:20 +0000 | [diff] [blame] | 677 | } else if (dst > src) { \ |
| 678 | \ |
| 679 | SizeT n = len; \ |
| 680 | Addr d = ((Addr)dst) + n; \ |
| 681 | Addr s = ((Addr)src) + n; \ |
| 682 | \ |
| 683 | /* Copying forwards. */ \ |
| 684 | if (((s^d) & WM) == 0) { \ |
| 685 | /* s and d have same UWord alignment. */ \ |
| 686 | /* Back down to a UWord boundary. */ \ |
| 687 | while ((s & WM) != 0 && n >= 1) \ |
| 688 | { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \ |
| 689 | /* Copy UWords. */ \ |
| 690 | while (n >= WS) \ |
| 691 | { s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; } \ |
| 692 | if (n == 0) \ |
| 693 | return dst; \ |
| 694 | } \ |
| 695 | if (((s|d) & 1) == 0) { \ |
| 696 | /* Both are 16-aligned; copy what we can thusly. */ \ |
| 697 | while (n >= 2) \ |
| 698 | { s -= 2; d -= 2; *(UShort*)d = *(UShort*)s; n -= 2; } \ |
| 699 | } \ |
| 700 | /* Copy leftovers, or everything if misaligned. */ \ |
| 701 | while (n >= 1) \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 702 | { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \ |
tom | 863ab7c | 2011-08-18 08:10:20 +0000 | [diff] [blame] | 703 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 704 | } \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 705 | } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 706 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 707 | return dst; \ |
| 708 | } |
| 709 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 710 | #define MEMMOVE(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 711 | MEMMOVE_OR_MEMCPY(20181, soname, fnname, 0) |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 712 | |
| 713 | #define MEMCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 714 | MEMMOVE_OR_MEMCPY(20180, soname, fnname, 1) |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 715 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 716 | #if defined(VGO_linux) |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 717 | /* For older memcpy we have to use memmove-like semantics and skip the |
| 718 | overlap check; sigh; see #275284. */ |
sewardj | d88797f | 2011-08-17 21:25:50 +0000 | [diff] [blame] | 719 | MEMMOVE(VG_Z_LIBC_SONAME, memcpyZAGLIBCZu2Zd2Zd5) /* memcpy@GLIBC_2.2.5 */ |
| 720 | MEMCPY(VG_Z_LIBC_SONAME, memcpyZAZAGLIBCZu2Zd14) /* memcpy@@GLIBC_2.14 */ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 721 | MEMCPY(VG_Z_LIBC_SONAME, memcpy) /* fallback case */ |
sewardj | d88797f | 2011-08-17 21:25:50 +0000 | [diff] [blame] | 722 | MEMCPY(VG_Z_LD_SO_1, memcpy) /* ld.so.1 */ |
| 723 | MEMCPY(VG_Z_LD64_SO_1, memcpy) /* ld64.so.1 */ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 724 | #elif defined(VGO_darwin) |
sewardj | d88797f | 2011-08-17 21:25:50 +0000 | [diff] [blame] | 725 | MEMCPY(VG_Z_LIBC_SONAME, memcpy) |
| 726 | MEMCPY(VG_Z_DYLD, memcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 727 | #endif |
sewardj | f0b3432 | 2007-01-16 21:42:28 +0000 | [diff] [blame] | 728 | /* icc9 blats these around all over the place. Not only in the main |
| 729 | executable but various .so's. They are highly tuned and read |
| 730 | memory beyond the source boundary (although work correctly and |
| 731 | never go across page boundaries), so give errors when run natively, |
| 732 | at least for misaligned source arg. Just intercepting in the exe |
| 733 | only until we understand more about the problem. See |
| 734 | http://bugs.kde.org/show_bug.cgi?id=139776 |
| 735 | */ |
sewardj | d88797f | 2011-08-17 21:25:50 +0000 | [diff] [blame] | 736 | MEMCPY(NONE, ZuintelZufastZumemcpy) |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 737 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 738 | |
| 739 | #define MEMCMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 740 | int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 741 | ( const void *s1V, const void *s2V, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 742 | int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 743 | ( const void *s1V, const void *s2V, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 744 | { \ |
| 745 | int res; \ |
| 746 | unsigned char a0; \ |
| 747 | unsigned char b0; \ |
| 748 | unsigned char* s1 = (unsigned char*)s1V; \ |
| 749 | unsigned char* s2 = (unsigned char*)s2V; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 750 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 751 | while (n != 0) { \ |
| 752 | a0 = s1[0]; \ |
| 753 | b0 = s2[0]; \ |
| 754 | s1 += 1; \ |
| 755 | s2 += 1; \ |
| 756 | res = ((int)a0) - ((int)b0); \ |
| 757 | if (res != 0) \ |
| 758 | return res; \ |
| 759 | n -= 1; \ |
| 760 | } \ |
| 761 | return 0; \ |
| 762 | } |
| 763 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 764 | MEMCMP(VG_Z_LIBC_SONAME, memcmp) |
| 765 | MEMCMP(VG_Z_LIBC_SONAME, bcmp) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 766 | #if defined(VGO_linux) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 767 | MEMCMP(VG_Z_LD_SO_1, bcmp) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 768 | #elif defined(VGO_darwin) |
| 769 | MEMCMP(VG_Z_DYLD, memcmp) |
| 770 | MEMCMP(VG_Z_DYLD, bcmp) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 771 | #endif |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 772 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 773 | |
| 774 | /* Copy SRC to DEST, returning the address of the terminating '\0' in |
| 775 | DEST. (minor variant of strcpy) */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 776 | #define STPCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 777 | char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 778 | ( char* dst, const char* src ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 779 | char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 780 | ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 781 | { \ |
| 782 | const Char* src_orig = src; \ |
| 783 | Char* dst_orig = dst; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 784 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 785 | while (*src) *dst++ = *src++; \ |
| 786 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 787 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 788 | /* This checks for overlap after copying, unavoidable without */ \ |
| 789 | /* pre-counting length... should be ok */ \ |
| 790 | if (is_overlap(dst_orig, \ |
| 791 | src_orig, \ |
| 792 | (Addr)dst-(Addr)dst_orig+1, \ |
| 793 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 794 | RECORD_OVERLAP_ERROR("stpcpy", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 795 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 796 | return dst; \ |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 797 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 798 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 799 | STPCPY(VG_Z_LIBC_SONAME, stpcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 800 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 801 | STPCPY(VG_Z_LIBC_SONAME, __GI_stpcpy) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 802 | STPCPY(VG_Z_LD_LINUX_SO_2, stpcpy) |
| 803 | STPCPY(VG_Z_LD_LINUX_X86_64_SO_2, stpcpy) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 804 | #elif defined(VGO_darwin) |
| 805 | STPCPY(VG_Z_DYLD, stpcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 806 | #endif |
| 807 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 808 | |
| 809 | #define MEMSET(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 810 | void* VG_REPLACE_FUNCTION_EZU(20210,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 811 | (void *s, Int c, SizeT n); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 812 | void* VG_REPLACE_FUNCTION_EZU(20210,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 813 | (void *s, Int c, SizeT n) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 814 | { \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 815 | Addr a = (Addr)s; \ |
| 816 | UInt c4 = (c & 0xFF); \ |
| 817 | c4 = (c4 << 8) | c4; \ |
| 818 | c4 = (c4 << 16) | c4; \ |
| 819 | while ((a & 3) != 0 && n >= 1) \ |
| 820 | { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \ |
| 821 | while (n >= 4) \ |
| 822 | { *(UInt*)a = c4; a += 4; n -= 4; } \ |
| 823 | while (n >= 1) \ |
| 824 | { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 825 | return s; \ |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 826 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 827 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 828 | MEMSET(VG_Z_LIBC_SONAME, memset) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 829 | #if defined(VGO_darwin) |
| 830 | MEMSET(VG_Z_DYLD, memset) |
| 831 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 832 | |
| 833 | |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 834 | /* memmove -- use the MEMMOVE defn above. */ |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 835 | MEMMOVE(VG_Z_LIBC_SONAME, memmove) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 836 | #if defined(VGO_darwin) |
| 837 | MEMMOVE(VG_Z_DYLD, memmove) |
| 838 | #endif |
| 839 | |
| 840 | |
| 841 | #define BCOPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 842 | void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 843 | (const void *srcV, void *dstV, SizeT n); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 844 | void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 845 | (const void *srcV, void *dstV, SizeT n) \ |
| 846 | { \ |
| 847 | SizeT i; \ |
| 848 | Char* dst = (Char*)dstV; \ |
| 849 | Char* src = (Char*)srcV; \ |
| 850 | if (dst < src) { \ |
| 851 | for (i = 0; i < n; i++) \ |
| 852 | dst[i] = src[i]; \ |
| 853 | } \ |
| 854 | else \ |
| 855 | if (dst > src) { \ |
| 856 | for (i = 0; i < n; i++) \ |
| 857 | dst[n-i-1] = src[n-i-1]; \ |
| 858 | } \ |
| 859 | } |
| 860 | |
| 861 | #if defined(VGO_darwin) |
| 862 | BCOPY(VG_Z_LIBC_SONAME, bcopy) |
| 863 | BCOPY(VG_Z_DYLD, bcopy) |
| 864 | #endif |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 865 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 866 | |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 867 | /* glibc 2.5 variant of memmove which checks the dest is big enough. |
| 868 | There is no specific part of glibc that this is copied from. */ |
| 869 | #define GLIBC25___MEMMOVE_CHK(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 870 | void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 871 | (void *dstV, const void *srcV, SizeT n, SizeT destlen); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 872 | void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 873 | (void *dstV, const void *srcV, SizeT n, SizeT destlen) \ |
| 874 | { \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 875 | SizeT i; \ |
| 876 | Char* dst = (Char*)dstV; \ |
| 877 | Char* src = (Char*)srcV; \ |
| 878 | if (destlen < n) \ |
| 879 | goto badness; \ |
| 880 | if (dst < src) { \ |
| 881 | for (i = 0; i < n; i++) \ |
| 882 | dst[i] = src[i]; \ |
| 883 | } \ |
| 884 | else \ |
| 885 | if (dst > src) { \ |
| 886 | for (i = 0; i < n; i++) \ |
| 887 | dst[n-i-1] = src[n-i-1]; \ |
| 888 | } \ |
| 889 | return dst; \ |
| 890 | badness: \ |
| 891 | VALGRIND_PRINTF_BACKTRACE( \ |
| 892 | "*** memmove_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 893 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 894 | my_exit(127); \ |
sewardj | c271ec8 | 2007-02-27 22:36:14 +0000 | [diff] [blame] | 895 | /*NOTREACHED*/ \ |
| 896 | return NULL; \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 897 | } |
| 898 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 899 | GLIBC25___MEMMOVE_CHK(VG_Z_LIBC_SONAME, __memmove_chk) |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 900 | |
| 901 | |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 902 | /* Find the first occurrence of C in S or the final NUL byte. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 903 | #define GLIBC232_STRCHRNUL(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 904 | char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 905 | (const char* s, int c_in); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 906 | char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 907 | (const char* s, int c_in) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 908 | { \ |
| 909 | unsigned char c = (unsigned char) c_in; \ |
| 910 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 911 | while (1) { \ |
| 912 | if (*char_ptr == 0) return char_ptr; \ |
| 913 | if (*char_ptr == c) return char_ptr; \ |
| 914 | char_ptr++; \ |
| 915 | } \ |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 916 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 917 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 918 | GLIBC232_STRCHRNUL(VG_Z_LIBC_SONAME, strchrnul) |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 919 | |
| 920 | |
| 921 | /* Find the first occurrence of C in S. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 922 | #define GLIBC232_RAWMEMCHR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 923 | char* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 924 | (const char* s, int c_in); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 925 | char* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 926 | (const char* s, int c_in) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 927 | { \ |
| 928 | unsigned char c = (unsigned char) c_in; \ |
| 929 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 930 | while (1) { \ |
| 931 | if (*char_ptr == c) return char_ptr; \ |
| 932 | char_ptr++; \ |
| 933 | } \ |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 934 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 935 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 936 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, rawmemchr) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 937 | #if defined (VGO_linux) |
| 938 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, __GI___rawmemchr) |
| 939 | #endif |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 940 | |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 941 | /* glibc variant of strcpy that checks the dest is big enough. |
| 942 | Copied from glibc-2.5/debug/test-strcpy_chk.c. */ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 943 | #define GLIBC25___STRCPY_CHK(soname,fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 944 | char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 945 | (char* dst, const char* src, SizeT len); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 946 | char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 947 | (char* dst, const char* src, SizeT len) \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 948 | { \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 949 | char* ret = dst; \ |
| 950 | if (! len) \ |
| 951 | goto badness; \ |
| 952 | while ((*dst++ = *src++) != '\0') \ |
| 953 | if (--len == 0) \ |
| 954 | goto badness; \ |
| 955 | return ret; \ |
| 956 | badness: \ |
| 957 | VALGRIND_PRINTF_BACKTRACE( \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 958 | "*** strcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 959 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 960 | my_exit(127); \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 961 | /*NOTREACHED*/ \ |
| 962 | return NULL; \ |
| 963 | } |
| 964 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 965 | GLIBC25___STRCPY_CHK(VG_Z_LIBC_SONAME, __strcpy_chk) |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 966 | |
| 967 | |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 968 | /* glibc variant of stpcpy that checks the dest is big enough. |
| 969 | Copied from glibc-2.5/debug/test-stpcpy_chk.c. */ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 970 | #define GLIBC25___STPCPY_CHK(soname,fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 971 | char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 972 | (char* dst, const char* src, SizeT len); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 973 | char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 974 | (char* dst, const char* src, SizeT len) \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 975 | { \ |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 976 | if (! len) \ |
| 977 | goto badness; \ |
| 978 | while ((*dst++ = *src++) != '\0') \ |
| 979 | if (--len == 0) \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 980 | goto badness; \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 981 | return dst - 1; \ |
| 982 | badness: \ |
| 983 | VALGRIND_PRINTF_BACKTRACE( \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 984 | "*** stpcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 985 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 986 | my_exit(127); \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 987 | /*NOTREACHED*/ \ |
| 988 | return NULL; \ |
| 989 | } |
| 990 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 991 | GLIBC25___STPCPY_CHK(VG_Z_LIBC_SONAME, __stpcpy_chk) |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 992 | |
| 993 | |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 994 | /* mempcpy */ |
| 995 | #define GLIBC25_MEMPCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 996 | void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \ |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 997 | ( void *dst, const void *src, SizeT len ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 998 | void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \ |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 999 | ( void *dst, const void *src, SizeT len ) \ |
| 1000 | { \ |
| 1001 | register char *d; \ |
| 1002 | register char *s; \ |
| 1003 | SizeT len_saved = len; \ |
| 1004 | \ |
| 1005 | if (len == 0) \ |
| 1006 | return dst; \ |
| 1007 | \ |
| 1008 | if (is_overlap(dst, src, len, len)) \ |
| 1009 | RECORD_OVERLAP_ERROR("mempcpy", dst, src, len); \ |
| 1010 | \ |
| 1011 | if ( dst > src ) { \ |
| 1012 | d = (char *)dst + len - 1; \ |
| 1013 | s = (char *)src + len - 1; \ |
| 1014 | while ( len-- ) { \ |
| 1015 | *d-- = *s--; \ |
| 1016 | } \ |
| 1017 | } else if ( dst < src ) { \ |
| 1018 | d = (char *)dst; \ |
| 1019 | s = (char *)src; \ |
| 1020 | while ( len-- ) { \ |
| 1021 | *d++ = *s++; \ |
| 1022 | } \ |
| 1023 | } \ |
| 1024 | return (void*)( ((char*)dst) + len_saved ); \ |
| 1025 | } |
| 1026 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1027 | GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 1028 | #if defined(VGO_linux) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1029 | GLIBC25_MEMPCPY(VG_Z_LD_SO_1, mempcpy) /* ld.so.1 */ |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 1030 | #endif |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 1031 | |
| 1032 | |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1033 | #define GLIBC26___MEMCPY_CHK(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1034 | void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1035 | (void* dst, const void* src, SizeT len, SizeT dstlen ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1036 | void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1037 | (void* dst, const void* src, SizeT len, SizeT dstlen ) \ |
| 1038 | { \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1039 | register char *d; \ |
| 1040 | register char *s; \ |
| 1041 | \ |
| 1042 | if (dstlen < len) goto badness; \ |
| 1043 | \ |
| 1044 | if (len == 0) \ |
| 1045 | return dst; \ |
| 1046 | \ |
| 1047 | if (is_overlap(dst, src, len, len)) \ |
| 1048 | RECORD_OVERLAP_ERROR("memcpy_chk", dst, src, len); \ |
| 1049 | \ |
| 1050 | if ( dst > src ) { \ |
| 1051 | d = (char *)dst + len - 1; \ |
| 1052 | s = (char *)src + len - 1; \ |
| 1053 | while ( len-- ) { \ |
| 1054 | *d-- = *s--; \ |
| 1055 | } \ |
| 1056 | } else if ( dst < src ) { \ |
| 1057 | d = (char *)dst; \ |
| 1058 | s = (char *)src; \ |
| 1059 | while ( len-- ) { \ |
| 1060 | *d++ = *s++; \ |
| 1061 | } \ |
| 1062 | } \ |
| 1063 | return dst; \ |
| 1064 | badness: \ |
| 1065 | VALGRIND_PRINTF_BACKTRACE( \ |
| 1066 | "*** memcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1067 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1068 | my_exit(127); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1069 | /*NOTREACHED*/ \ |
| 1070 | return NULL; \ |
| 1071 | } |
| 1072 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1073 | GLIBC26___MEMCPY_CHK(VG_Z_LIBC_SONAME, __memcpy_chk) |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1074 | |
| 1075 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1076 | #define STRSTR(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1077 | void* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1078 | (void* haystack, void* needle); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1079 | void* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1080 | (void* haystack, void* needle) \ |
| 1081 | { \ |
| 1082 | UChar* h = (UChar*)haystack; \ |
| 1083 | UChar* n = (UChar*)needle; \ |
| 1084 | \ |
| 1085 | /* find the length of n, not including terminating zero */ \ |
| 1086 | UWord nlen = 0; \ |
| 1087 | while (n[nlen]) nlen++; \ |
| 1088 | \ |
| 1089 | /* if n is the empty string, match immediately. */ \ |
| 1090 | if (nlen == 0) return h; \ |
| 1091 | \ |
| 1092 | /* assert(nlen >= 1); */ \ |
| 1093 | UChar n0 = n[0]; \ |
| 1094 | \ |
| 1095 | while (1) { \ |
| 1096 | UChar hh = *h; \ |
| 1097 | if (hh == 0) return NULL; \ |
| 1098 | if (hh != n0) { h++; continue; } \ |
| 1099 | \ |
| 1100 | UWord i; \ |
| 1101 | for (i = 0; i < nlen; i++) { \ |
| 1102 | if (n[i] != h[i]) \ |
| 1103 | break; \ |
| 1104 | } \ |
| 1105 | /* assert(i >= 0 && i <= nlen); */ \ |
| 1106 | if (i == nlen) \ |
| 1107 | return h; \ |
| 1108 | \ |
| 1109 | h++; \ |
| 1110 | } \ |
| 1111 | } |
| 1112 | |
| 1113 | #if defined(VGO_linux) |
| 1114 | STRSTR(VG_Z_LIBC_SONAME, strstr) |
| 1115 | #endif |
| 1116 | |
| 1117 | |
| 1118 | #define STRPBRK(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1119 | void* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1120 | (void* sV, void* acceptV); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1121 | void* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1122 | (void* sV, void* acceptV) \ |
| 1123 | { \ |
| 1124 | UChar* s = (UChar*)sV; \ |
| 1125 | UChar* accept = (UChar*)acceptV; \ |
| 1126 | \ |
| 1127 | /* find the length of 'accept', not including terminating zero */ \ |
| 1128 | UWord nacc = 0; \ |
| 1129 | while (accept[nacc]) nacc++; \ |
| 1130 | \ |
| 1131 | /* if n is the empty string, fail immediately. */ \ |
| 1132 | if (nacc == 0) return NULL; \ |
| 1133 | \ |
| 1134 | /* assert(nacc >= 1); */ \ |
| 1135 | while (1) { \ |
| 1136 | UWord i; \ |
| 1137 | UChar sc = *s; \ |
| 1138 | if (sc == 0) \ |
| 1139 | break; \ |
| 1140 | for (i = 0; i < nacc; i++) { \ |
| 1141 | if (sc == accept[i]) \ |
| 1142 | return s; \ |
| 1143 | } \ |
| 1144 | s++; \ |
| 1145 | } \ |
| 1146 | \ |
| 1147 | return NULL; \ |
| 1148 | } |
| 1149 | |
| 1150 | #if defined(VGO_linux) |
| 1151 | STRPBRK(VG_Z_LIBC_SONAME, strpbrk) |
| 1152 | #endif |
| 1153 | |
| 1154 | |
| 1155 | #define STRCSPN(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1156 | SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1157 | (void* sV, void* rejectV); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1158 | SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1159 | (void* sV, void* rejectV) \ |
| 1160 | { \ |
| 1161 | UChar* s = (UChar*)sV; \ |
| 1162 | UChar* reject = (UChar*)rejectV; \ |
| 1163 | \ |
| 1164 | /* find the length of 'reject', not including terminating zero */ \ |
| 1165 | UWord nrej = 0; \ |
| 1166 | while (reject[nrej]) nrej++; \ |
| 1167 | \ |
| 1168 | UWord len = 0; \ |
| 1169 | while (1) { \ |
| 1170 | UWord i; \ |
| 1171 | UChar sc = *s; \ |
| 1172 | if (sc == 0) \ |
| 1173 | break; \ |
| 1174 | for (i = 0; i < nrej; i++) { \ |
| 1175 | if (sc == reject[i]) \ |
| 1176 | break; \ |
| 1177 | } \ |
| 1178 | /* assert(i >= 0 && i <= nrej); */ \ |
| 1179 | if (i < nrej) \ |
| 1180 | break; \ |
| 1181 | s++; \ |
| 1182 | len++; \ |
| 1183 | } \ |
| 1184 | \ |
| 1185 | return len; \ |
| 1186 | } |
| 1187 | |
| 1188 | #if defined(VGO_linux) |
| 1189 | STRCSPN(VG_Z_LIBC_SONAME, strcspn) |
| 1190 | #endif |
| 1191 | |
| 1192 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1193 | #define STRSPN(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1194 | SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1195 | (void* sV, void* acceptV); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame^] | 1196 | SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1197 | (void* sV, void* acceptV) \ |
| 1198 | { \ |
| 1199 | UChar* s = (UChar*)sV; \ |
| 1200 | UChar* accept = (UChar*)acceptV; \ |
| 1201 | \ |
| 1202 | /* find the length of 'accept', not including terminating zero */ \ |
| 1203 | UWord nacc = 0; \ |
| 1204 | while (accept[nacc]) nacc++; \ |
| 1205 | if (nacc == 0) return 0; \ |
| 1206 | \ |
| 1207 | UWord len = 0; \ |
| 1208 | while (1) { \ |
| 1209 | UWord i; \ |
| 1210 | UChar sc = *s; \ |
| 1211 | if (sc == 0) \ |
| 1212 | break; \ |
| 1213 | for (i = 0; i < nacc; i++) { \ |
| 1214 | if (sc == accept[i]) \ |
| 1215 | break; \ |
| 1216 | } \ |
| 1217 | /* assert(i >= 0 && i <= nacc); */ \ |
| 1218 | if (i == nacc) \ |
| 1219 | break; \ |
| 1220 | s++; \ |
| 1221 | len++; \ |
| 1222 | } \ |
| 1223 | \ |
| 1224 | return len; \ |
| 1225 | } |
| 1226 | |
| 1227 | #if defined(VGO_linux) |
| 1228 | STRSPN(VG_Z_LIBC_SONAME, strspn) |
| 1229 | #endif |
sewardj | ba18935 | 2010-08-20 18:24:16 +0000 | [diff] [blame] | 1230 | |
| 1231 | |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1232 | /*------------------------------------------------------------*/ |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1233 | /*--- Improve definedness checking of process environment ---*/ |
| 1234 | /*------------------------------------------------------------*/ |
| 1235 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1236 | #if defined(VGO_linux) |
| 1237 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1238 | /* If these wind up getting generated via a macro, so that multiple |
| 1239 | versions of each function exist (as above), use the _EZU variants |
| 1240 | to assign equivalance class tags. */ |
| 1241 | |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1242 | /* putenv */ |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1243 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string); |
| 1244 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1245 | { |
| 1246 | OrigFn fn; |
| 1247 | Word result; |
| 1248 | const char* p = string; |
| 1249 | VALGRIND_GET_ORIG_FN(fn); |
| 1250 | /* Now by walking over the string we magically produce |
| 1251 | traces when hitting undefined memory. */ |
| 1252 | if (p) |
| 1253 | while (*p++) |
| 1254 | ; |
| 1255 | CALL_FN_W_W(result, fn, string); |
| 1256 | return result; |
| 1257 | } |
| 1258 | |
| 1259 | /* unsetenv */ |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1260 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name); |
| 1261 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1262 | { |
| 1263 | OrigFn fn; |
| 1264 | Word result; |
| 1265 | const char* p = name; |
| 1266 | VALGRIND_GET_ORIG_FN(fn); |
| 1267 | /* Now by walking over the string we magically produce |
| 1268 | traces when hitting undefined memory. */ |
| 1269 | if (p) |
| 1270 | while (*p++) |
| 1271 | ; |
| 1272 | CALL_FN_W_W(result, fn, name); |
| 1273 | return result; |
| 1274 | } |
| 1275 | |
| 1276 | /* setenv */ |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1277 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1278 | (const char* name, const char* value, int overwrite); |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1279 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1280 | (const char* name, const char* value, int overwrite) |
| 1281 | { |
| 1282 | OrigFn fn; |
| 1283 | Word result; |
| 1284 | const char* p; |
| 1285 | VALGRIND_GET_ORIG_FN(fn); |
| 1286 | /* Now by walking over the string we magically produce |
| 1287 | traces when hitting undefined memory. */ |
| 1288 | if (name) |
| 1289 | for (p = name; *p; p++) |
| 1290 | ; |
| 1291 | if (value) |
| 1292 | for (p = value; *p; p++) |
| 1293 | ; |
| 1294 | VALGRIND_CHECK_VALUE_IS_DEFINED (overwrite); |
| 1295 | CALL_FN_W_WWW(result, fn, name, value, overwrite); |
| 1296 | return result; |
| 1297 | } |
| 1298 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1299 | #endif /* defined(VGO_linux) */ |
| 1300 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1301 | /*--------------------------------------------------------------------*/ |
njn | 4627586 | 2005-03-24 04:00:03 +0000 | [diff] [blame] | 1302 | /*--- end ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1303 | /*--------------------------------------------------------------------*/ |