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