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