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