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