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 | a65f80c | 2012-03-07 15:26:50 +0000 | [diff] [blame] | 853 | # if DARWIN_VERS <= DARWIN_10_6 |
| 854 | MEMCPY(VG_Z_LIBC_SONAME, memcpy) |
| 855 | # endif |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 856 | MEMCPY(VG_Z_LIBC_SONAME, memcpyZDVARIANTZDsse3x) /* memcpy$VARIANT$sse3x */ |
| 857 | MEMCPY(VG_Z_LIBC_SONAME, memcpyZDVARIANTZDsse42) /* memcpy$VARIANT$sse42 */ |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 858 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 859 | #endif |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 860 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 861 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 862 | /*---------------------- memcmp ----------------------*/ |
| 863 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 864 | #define MEMCMP(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 865 | int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 866 | ( const void *s1V, const void *s2V, SizeT n ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 867 | int VG_REPLACE_FUNCTION_EZU(20190,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 868 | ( const void *s1V, const void *s2V, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 869 | { \ |
| 870 | int res; \ |
| 871 | unsigned char a0; \ |
| 872 | unsigned char b0; \ |
| 873 | unsigned char* s1 = (unsigned char*)s1V; \ |
| 874 | unsigned char* s2 = (unsigned char*)s2V; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 875 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 876 | while (n != 0) { \ |
| 877 | a0 = s1[0]; \ |
| 878 | b0 = s2[0]; \ |
| 879 | s1 += 1; \ |
| 880 | s2 += 1; \ |
| 881 | res = ((int)a0) - ((int)b0); \ |
| 882 | if (res != 0) \ |
| 883 | return res; \ |
| 884 | n -= 1; \ |
| 885 | } \ |
| 886 | return 0; \ |
| 887 | } |
| 888 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 889 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 890 | MEMCMP(VG_Z_LIBC_SONAME, memcmp) |
| 891 | MEMCMP(VG_Z_LIBC_SONAME, bcmp) |
| 892 | MEMCMP(VG_Z_LD_SO_1, bcmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 893 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 894 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 895 | //MEMCMP(VG_Z_LIBC_SONAME, memcmp) |
| 896 | //MEMCMP(VG_Z_LIBC_SONAME, bcmp) |
| 897 | //MEMCMP(VG_Z_DYLD, memcmp) |
| 898 | //MEMCMP(VG_Z_DYLD, bcmp) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 899 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 900 | #endif |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 901 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 902 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 903 | /*---------------------- stpcpy ----------------------*/ |
| 904 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 905 | /* Copy SRC to DEST, returning the address of the terminating '\0' in |
| 906 | DEST. (minor variant of strcpy) */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 907 | #define STPCPY(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 908 | char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 909 | ( char* dst, const char* src ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 910 | char* VG_REPLACE_FUNCTION_EZU(20200,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 911 | ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 912 | { \ |
| 913 | const Char* src_orig = src; \ |
| 914 | Char* dst_orig = dst; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 915 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 916 | while (*src) *dst++ = *src++; \ |
| 917 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 918 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 919 | /* This checks for overlap after copying, unavoidable without */ \ |
| 920 | /* pre-counting length... should be ok */ \ |
| 921 | if (is_overlap(dst_orig, \ |
| 922 | src_orig, \ |
| 923 | (Addr)dst-(Addr)dst_orig+1, \ |
| 924 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 925 | RECORD_OVERLAP_ERROR("stpcpy", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 926 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 927 | return dst; \ |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 928 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 929 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 930 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 931 | STPCPY(VG_Z_LIBC_SONAME, stpcpy) |
| 932 | STPCPY(VG_Z_LIBC_SONAME, __GI_stpcpy) |
| 933 | STPCPY(VG_Z_LD_LINUX_SO_2, stpcpy) |
| 934 | STPCPY(VG_Z_LD_LINUX_X86_64_SO_2, stpcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 935 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 936 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 937 | //STPCPY(VG_Z_LIBC_SONAME, stpcpy) |
| 938 | //STPCPY(VG_Z_DYLD, stpcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 939 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 940 | #endif |
| 941 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 942 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 943 | /*---------------------- memset ----------------------*/ |
| 944 | |
| 945 | /* Why are we bothering to intercept this? It seems entirely |
| 946 | pointless. */ |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 947 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 948 | #define MEMSET(soname, fnname) \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 949 | void* VG_REPLACE_FUNCTION_EZU(20210,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 950 | (void *s, Int c, SizeT n); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 951 | void* VG_REPLACE_FUNCTION_EZU(20210,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 952 | (void *s, Int c, SizeT n) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 953 | { \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 954 | Addr a = (Addr)s; \ |
| 955 | UInt c4 = (c & 0xFF); \ |
| 956 | c4 = (c4 << 8) | c4; \ |
| 957 | c4 = (c4 << 16) | c4; \ |
| 958 | while ((a & 3) != 0 && n >= 1) \ |
| 959 | { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \ |
| 960 | while (n >= 4) \ |
| 961 | { *(UInt*)a = c4; a += 4; n -= 4; } \ |
| 962 | while (n >= 1) \ |
| 963 | { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 964 | return s; \ |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 965 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 966 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 967 | #if defined(VGO_linux) |
| 968 | MEMSET(VG_Z_LIBC_SONAME, memset) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 969 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 970 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 971 | //MEMSET(VG_Z_LIBC_SONAME, memset) |
| 972 | //MEMSET(VG_Z_DYLD, memset) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 973 | MEMSET(VG_Z_LIBC_SONAME, memset) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 974 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 975 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 976 | |
| 977 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 978 | /*---------------------- memmove ----------------------*/ |
| 979 | |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 980 | /* memmove -- use the MEMMOVE defn above. */ |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 981 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 982 | #if defined(VGO_linux) |
| 983 | MEMMOVE(VG_Z_LIBC_SONAME, memmove) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 984 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 985 | #elif defined(VGO_darwin) |
sewardj | a65f80c | 2012-03-07 15:26:50 +0000 | [diff] [blame] | 986 | # if DARWIN_VERS <= DARWIN_10_6 |
| 987 | MEMMOVE(VG_Z_LIBC_SONAME, memmove) |
| 988 | # endif |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 989 | MEMMOVE(VG_Z_LIBC_SONAME, memmoveZDVARIANTZDsse3x) /* memmove$VARIANT$sse3x */ |
| 990 | MEMMOVE(VG_Z_LIBC_SONAME, memmoveZDVARIANTZDsse42) /* memmove$VARIANT$sse42 */ |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 991 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 992 | #endif |
| 993 | |
| 994 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 995 | /*---------------------- bcopy ----------------------*/ |
| 996 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 997 | #define BCOPY(soname, fnname) \ |
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); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1000 | void VG_REPLACE_FUNCTION_EZU(20230,soname,fnname) \ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1001 | (const void *srcV, void *dstV, SizeT n) \ |
| 1002 | { \ |
| 1003 | SizeT i; \ |
| 1004 | Char* dst = (Char*)dstV; \ |
| 1005 | Char* src = (Char*)srcV; \ |
| 1006 | if (dst < src) { \ |
| 1007 | for (i = 0; i < n; i++) \ |
| 1008 | dst[i] = src[i]; \ |
| 1009 | } \ |
| 1010 | else \ |
| 1011 | if (dst > src) { \ |
| 1012 | for (i = 0; i < n; i++) \ |
| 1013 | dst[n-i-1] = src[n-i-1]; \ |
| 1014 | } \ |
| 1015 | } |
| 1016 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1017 | #if defined(VGO_linux) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1018 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1019 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 1020 | //BCOPY(VG_Z_LIBC_SONAME, bcopy) |
| 1021 | //BCOPY(VG_Z_DYLD, bcopy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1022 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1023 | #endif |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 1024 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 1025 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1026 | /*-------------------- memmove_chk --------------------*/ |
| 1027 | |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1028 | /* glibc 2.5 variant of memmove which checks the dest is big enough. |
| 1029 | There is no specific part of glibc that this is copied from. */ |
| 1030 | #define GLIBC25___MEMMOVE_CHK(soname, fnname) \ |
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); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1033 | void* VG_REPLACE_FUNCTION_EZU(20240,soname,fnname) \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1034 | (void *dstV, const void *srcV, SizeT n, SizeT destlen) \ |
| 1035 | { \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1036 | SizeT i; \ |
| 1037 | Char* dst = (Char*)dstV; \ |
| 1038 | Char* src = (Char*)srcV; \ |
| 1039 | if (destlen < n) \ |
| 1040 | goto badness; \ |
| 1041 | if (dst < src) { \ |
| 1042 | for (i = 0; i < n; i++) \ |
| 1043 | dst[i] = src[i]; \ |
| 1044 | } \ |
| 1045 | else \ |
| 1046 | if (dst > src) { \ |
| 1047 | for (i = 0; i < n; i++) \ |
| 1048 | dst[n-i-1] = src[n-i-1]; \ |
| 1049 | } \ |
| 1050 | return dst; \ |
| 1051 | badness: \ |
| 1052 | VALGRIND_PRINTF_BACKTRACE( \ |
| 1053 | "*** memmove_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1054 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1055 | my_exit(127); \ |
sewardj | c271ec8 | 2007-02-27 22:36:14 +0000 | [diff] [blame] | 1056 | /*NOTREACHED*/ \ |
| 1057 | return NULL; \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1060 | #if defined(VGO_linux) |
| 1061 | GLIBC25___MEMMOVE_CHK(VG_Z_LIBC_SONAME, __memmove_chk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1062 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1063 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1064 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1065 | #endif |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1066 | |
| 1067 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1068 | /*-------------------- strchrnul --------------------*/ |
| 1069 | |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1070 | /* Find the first occurrence of C in S or the final NUL byte. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1071 | #define GLIBC232_STRCHRNUL(soname, fnname) \ |
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); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1074 | char* VG_REPLACE_FUNCTION_EZU(20250,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1075 | (const char* s, int c_in) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1076 | { \ |
| 1077 | unsigned char c = (unsigned char) c_in; \ |
| 1078 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 1079 | while (1) { \ |
| 1080 | if (*char_ptr == 0) return char_ptr; \ |
| 1081 | if (*char_ptr == c) return char_ptr; \ |
| 1082 | char_ptr++; \ |
| 1083 | } \ |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1084 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1085 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1086 | #if defined(VGO_linux) |
| 1087 | GLIBC232_STRCHRNUL(VG_Z_LIBC_SONAME, strchrnul) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1088 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1089 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1090 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1091 | #endif |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1092 | |
| 1093 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1094 | /*---------------------- rawmemchr ----------------------*/ |
| 1095 | |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1096 | /* Find the first occurrence of C in S. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1097 | #define GLIBC232_RAWMEMCHR(soname, fnname) \ |
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); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1100 | char* VG_REPLACE_FUNCTION_EZU(20260,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1101 | (const char* s, int c_in) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1102 | { \ |
| 1103 | unsigned char c = (unsigned char) c_in; \ |
| 1104 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 1105 | while (1) { \ |
| 1106 | if (*char_ptr == c) return char_ptr; \ |
| 1107 | char_ptr++; \ |
| 1108 | } \ |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1109 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1110 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 1111 | #if defined (VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1112 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, rawmemchr) |
| 1113 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, __GI___rawmemchr) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1114 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1115 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1116 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 1117 | #endif |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 1118 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1119 | |
| 1120 | /*---------------------- strcpy_chk ----------------------*/ |
| 1121 | |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 1122 | /* glibc variant of strcpy that checks the dest is big enough. |
| 1123 | Copied from glibc-2.5/debug/test-strcpy_chk.c. */ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1124 | #define GLIBC25___STRCPY_CHK(soname,fnname) \ |
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 | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1127 | char* VG_REPLACE_FUNCTION_EZU(20270,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1128 | (char* dst, const char* src, SizeT len) \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1129 | { \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1130 | char* ret = dst; \ |
| 1131 | if (! len) \ |
| 1132 | goto badness; \ |
| 1133 | while ((*dst++ = *src++) != '\0') \ |
| 1134 | if (--len == 0) \ |
| 1135 | goto badness; \ |
| 1136 | return ret; \ |
| 1137 | badness: \ |
| 1138 | VALGRIND_PRINTF_BACKTRACE( \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1139 | "*** strcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1140 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1141 | my_exit(127); \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1142 | /*NOTREACHED*/ \ |
| 1143 | return NULL; \ |
| 1144 | } |
| 1145 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1146 | #if defined(VGO_linux) |
| 1147 | GLIBC25___STRCPY_CHK(VG_Z_LIBC_SONAME, __strcpy_chk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1148 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1149 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1150 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1151 | #endif |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 1152 | |
| 1153 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1154 | /*---------------------- stpcpy_chk ----------------------*/ |
| 1155 | |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 1156 | /* glibc variant of stpcpy that checks the dest is big enough. |
| 1157 | Copied from glibc-2.5/debug/test-stpcpy_chk.c. */ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1158 | #define GLIBC25___STPCPY_CHK(soname,fnname) \ |
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 | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1161 | char* VG_REPLACE_FUNCTION_EZU(20280,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1162 | (char* dst, const char* src, SizeT len) \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1163 | { \ |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 1164 | if (! len) \ |
| 1165 | goto badness; \ |
| 1166 | while ((*dst++ = *src++) != '\0') \ |
| 1167 | if (--len == 0) \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1168 | goto badness; \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1169 | return dst - 1; \ |
| 1170 | badness: \ |
| 1171 | VALGRIND_PRINTF_BACKTRACE( \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 1172 | "*** stpcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1173 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1174 | my_exit(127); \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1175 | /*NOTREACHED*/ \ |
| 1176 | return NULL; \ |
| 1177 | } |
| 1178 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1179 | #if defined(VGO_linux) |
| 1180 | GLIBC25___STPCPY_CHK(VG_Z_LIBC_SONAME, __stpcpy_chk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1181 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1182 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1183 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1184 | #endif |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 1185 | |
| 1186 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1187 | /*---------------------- mempcpy ----------------------*/ |
| 1188 | |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 1189 | /* mempcpy */ |
| 1190 | #define GLIBC25_MEMPCPY(soname, fnname) \ |
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 ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1193 | void* VG_REPLACE_FUNCTION_EZU(20290,soname,fnname) \ |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 1194 | ( void *dst, const void *src, SizeT len ) \ |
| 1195 | { \ |
| 1196 | register char *d; \ |
| 1197 | register char *s; \ |
| 1198 | SizeT len_saved = len; \ |
| 1199 | \ |
| 1200 | if (len == 0) \ |
| 1201 | return dst; \ |
| 1202 | \ |
| 1203 | if (is_overlap(dst, src, len, len)) \ |
| 1204 | RECORD_OVERLAP_ERROR("mempcpy", dst, src, len); \ |
| 1205 | \ |
| 1206 | if ( dst > src ) { \ |
| 1207 | d = (char *)dst + len - 1; \ |
| 1208 | s = (char *)src + len - 1; \ |
| 1209 | while ( len-- ) { \ |
| 1210 | *d-- = *s--; \ |
| 1211 | } \ |
| 1212 | } else if ( dst < src ) { \ |
| 1213 | d = (char *)dst; \ |
| 1214 | s = (char *)src; \ |
| 1215 | while ( len-- ) { \ |
| 1216 | *d++ = *s++; \ |
| 1217 | } \ |
| 1218 | } \ |
| 1219 | return (void*)( ((char*)dst) + len_saved ); \ |
| 1220 | } |
| 1221 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 1222 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1223 | GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy) |
| 1224 | GLIBC25_MEMPCPY(VG_Z_LD_SO_1, mempcpy) /* ld.so.1 */ |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1225 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1226 | #elif defined(VGO_darwin) |
sewardj | 731f9cf | 2011-09-21 08:43:08 +0000 | [diff] [blame] | 1227 | //GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1228 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 1229 | #endif |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 1230 | |
| 1231 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1232 | /*-------------------- memcpy_chk --------------------*/ |
| 1233 | |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1234 | #define GLIBC26___MEMCPY_CHK(soname, fnname) \ |
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 ); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1237 | void* VG_REPLACE_FUNCTION_EZU(20300,soname,fnname) \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1238 | (void* dst, const void* src, SizeT len, SizeT dstlen ) \ |
| 1239 | { \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1240 | register char *d; \ |
| 1241 | register char *s; \ |
| 1242 | \ |
| 1243 | if (dstlen < len) goto badness; \ |
| 1244 | \ |
| 1245 | if (len == 0) \ |
| 1246 | return dst; \ |
| 1247 | \ |
| 1248 | if (is_overlap(dst, src, len, len)) \ |
| 1249 | RECORD_OVERLAP_ERROR("memcpy_chk", dst, src, len); \ |
| 1250 | \ |
| 1251 | if ( dst > src ) { \ |
| 1252 | d = (char *)dst + len - 1; \ |
| 1253 | s = (char *)src + len - 1; \ |
| 1254 | while ( len-- ) { \ |
| 1255 | *d-- = *s--; \ |
| 1256 | } \ |
| 1257 | } else if ( dst < src ) { \ |
| 1258 | d = (char *)dst; \ |
| 1259 | s = (char *)src; \ |
| 1260 | while ( len-- ) { \ |
| 1261 | *d++ = *s++; \ |
| 1262 | } \ |
| 1263 | } \ |
| 1264 | return dst; \ |
| 1265 | badness: \ |
| 1266 | VALGRIND_PRINTF_BACKTRACE( \ |
| 1267 | "*** memcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 1268 | "program terminated\n"); \ |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 1269 | my_exit(127); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1270 | /*NOTREACHED*/ \ |
| 1271 | return NULL; \ |
| 1272 | } |
| 1273 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1274 | #if defined(VGO_linux) |
| 1275 | GLIBC26___MEMCPY_CHK(VG_Z_LIBC_SONAME, __memcpy_chk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1276 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1277 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1278 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1279 | #endif |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1280 | |
| 1281 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1282 | /*---------------------- strstr ----------------------*/ |
| 1283 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1284 | #define STRSTR(soname, fnname) \ |
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); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1287 | void* VG_REPLACE_FUNCTION_EZU(20310,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1288 | (void* haystack, void* needle) \ |
| 1289 | { \ |
| 1290 | UChar* h = (UChar*)haystack; \ |
| 1291 | UChar* n = (UChar*)needle; \ |
| 1292 | \ |
| 1293 | /* find the length of n, not including terminating zero */ \ |
| 1294 | UWord nlen = 0; \ |
| 1295 | while (n[nlen]) nlen++; \ |
| 1296 | \ |
| 1297 | /* if n is the empty string, match immediately. */ \ |
| 1298 | if (nlen == 0) return h; \ |
| 1299 | \ |
| 1300 | /* assert(nlen >= 1); */ \ |
| 1301 | UChar n0 = n[0]; \ |
| 1302 | \ |
| 1303 | while (1) { \ |
| 1304 | UChar hh = *h; \ |
| 1305 | if (hh == 0) return NULL; \ |
| 1306 | if (hh != n0) { h++; continue; } \ |
| 1307 | \ |
| 1308 | UWord i; \ |
| 1309 | for (i = 0; i < nlen; i++) { \ |
| 1310 | if (n[i] != h[i]) \ |
| 1311 | break; \ |
| 1312 | } \ |
| 1313 | /* assert(i >= 0 && i <= nlen); */ \ |
| 1314 | if (i == nlen) \ |
| 1315 | return h; \ |
| 1316 | \ |
| 1317 | h++; \ |
| 1318 | } \ |
| 1319 | } |
| 1320 | |
| 1321 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1322 | STRSTR(VG_Z_LIBC_SONAME, strstr) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1323 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1324 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1325 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | |
| 1328 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1329 | /*---------------------- strpbrk ----------------------*/ |
| 1330 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1331 | #define STRPBRK(soname, fnname) \ |
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); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1334 | void* VG_REPLACE_FUNCTION_EZU(20320,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1335 | (void* sV, void* acceptV) \ |
| 1336 | { \ |
| 1337 | UChar* s = (UChar*)sV; \ |
| 1338 | UChar* accept = (UChar*)acceptV; \ |
| 1339 | \ |
| 1340 | /* find the length of 'accept', not including terminating zero */ \ |
| 1341 | UWord nacc = 0; \ |
| 1342 | while (accept[nacc]) nacc++; \ |
| 1343 | \ |
| 1344 | /* if n is the empty string, fail immediately. */ \ |
| 1345 | if (nacc == 0) return NULL; \ |
| 1346 | \ |
| 1347 | /* assert(nacc >= 1); */ \ |
| 1348 | while (1) { \ |
| 1349 | UWord i; \ |
| 1350 | UChar sc = *s; \ |
| 1351 | if (sc == 0) \ |
| 1352 | break; \ |
| 1353 | for (i = 0; i < nacc; i++) { \ |
| 1354 | if (sc == accept[i]) \ |
| 1355 | return s; \ |
| 1356 | } \ |
| 1357 | s++; \ |
| 1358 | } \ |
| 1359 | \ |
| 1360 | return NULL; \ |
| 1361 | } |
| 1362 | |
| 1363 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1364 | STRPBRK(VG_Z_LIBC_SONAME, strpbrk) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1365 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1366 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1367 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1368 | #endif |
| 1369 | |
| 1370 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1371 | /*---------------------- strcspn ----------------------*/ |
| 1372 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1373 | #define STRCSPN(soname, fnname) \ |
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); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1376 | SizeT VG_REPLACE_FUNCTION_EZU(20330,soname,fnname) \ |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1377 | (void* sV, void* rejectV) \ |
| 1378 | { \ |
| 1379 | UChar* s = (UChar*)sV; \ |
| 1380 | UChar* reject = (UChar*)rejectV; \ |
| 1381 | \ |
| 1382 | /* find the length of 'reject', not including terminating zero */ \ |
| 1383 | UWord nrej = 0; \ |
| 1384 | while (reject[nrej]) nrej++; \ |
| 1385 | \ |
| 1386 | UWord len = 0; \ |
| 1387 | while (1) { \ |
| 1388 | UWord i; \ |
| 1389 | UChar sc = *s; \ |
| 1390 | if (sc == 0) \ |
| 1391 | break; \ |
| 1392 | for (i = 0; i < nrej; i++) { \ |
| 1393 | if (sc == reject[i]) \ |
| 1394 | break; \ |
| 1395 | } \ |
| 1396 | /* assert(i >= 0 && i <= nrej); */ \ |
| 1397 | if (i < nrej) \ |
| 1398 | break; \ |
| 1399 | s++; \ |
| 1400 | len++; \ |
| 1401 | } \ |
| 1402 | \ |
| 1403 | return len; \ |
| 1404 | } |
| 1405 | |
| 1406 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1407 | STRCSPN(VG_Z_LIBC_SONAME, strcspn) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1408 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1409 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1410 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1411 | #endif |
| 1412 | |
| 1413 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1414 | /*---------------------- strspn ----------------------*/ |
| 1415 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1416 | #define STRSPN(soname, fnname) \ |
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); \ |
sewardj | 9604484 | 2011-08-18 13:09:55 +0000 | [diff] [blame] | 1419 | SizeT VG_REPLACE_FUNCTION_EZU(20340,soname,fnname) \ |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1420 | (void* sV, void* acceptV) \ |
| 1421 | { \ |
| 1422 | UChar* s = (UChar*)sV; \ |
| 1423 | UChar* accept = (UChar*)acceptV; \ |
| 1424 | \ |
| 1425 | /* find the length of 'accept', not including terminating zero */ \ |
| 1426 | UWord nacc = 0; \ |
| 1427 | while (accept[nacc]) nacc++; \ |
| 1428 | if (nacc == 0) return 0; \ |
| 1429 | \ |
| 1430 | UWord len = 0; \ |
| 1431 | while (1) { \ |
| 1432 | UWord i; \ |
| 1433 | UChar sc = *s; \ |
| 1434 | if (sc == 0) \ |
| 1435 | break; \ |
| 1436 | for (i = 0; i < nacc; i++) { \ |
| 1437 | if (sc == accept[i]) \ |
| 1438 | break; \ |
| 1439 | } \ |
| 1440 | /* assert(i >= 0 && i <= nacc); */ \ |
| 1441 | if (i == nacc) \ |
| 1442 | break; \ |
| 1443 | s++; \ |
| 1444 | len++; \ |
| 1445 | } \ |
| 1446 | \ |
| 1447 | return len; \ |
| 1448 | } |
| 1449 | |
| 1450 | #if defined(VGO_linux) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1451 | STRSPN(VG_Z_LIBC_SONAME, strspn) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1452 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1453 | #elif defined(VGO_darwin) |
sewardj | 4157d4f | 2011-09-05 22:18:13 +0000 | [diff] [blame] | 1454 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1455 | #endif |
sewardj | ba18935 | 2010-08-20 18:24:16 +0000 | [diff] [blame] | 1456 | |
| 1457 | |
sewardj | 284f2a3 | 2011-10-21 08:07:13 +0000 | [diff] [blame] | 1458 | /*---------------------- strcasestr ----------------------*/ |
| 1459 | |
| 1460 | #define STRCASESTR(soname, fnname) \ |
| 1461 | void* VG_REPLACE_FUNCTION_EZU(20350,soname,fnname) \ |
| 1462 | (void* haystack, void* needle); \ |
| 1463 | void* VG_REPLACE_FUNCTION_EZU(20350,soname,fnname) \ |
| 1464 | (void* haystack, void* needle) \ |
| 1465 | { \ |
| 1466 | extern int tolower(int); \ |
| 1467 | UChar* h = (UChar*)haystack; \ |
| 1468 | UChar* n = (UChar*)needle; \ |
| 1469 | \ |
| 1470 | /* find the length of n, not including terminating zero */ \ |
| 1471 | UWord nlen = 0; \ |
| 1472 | while (n[nlen]) nlen++; \ |
| 1473 | \ |
| 1474 | /* if n is the empty string, match immediately. */ \ |
| 1475 | if (nlen == 0) return h; \ |
| 1476 | \ |
| 1477 | /* assert(nlen >= 1); */ \ |
| 1478 | UChar n0 = tolower(n[0]); \ |
| 1479 | \ |
| 1480 | while (1) { \ |
| 1481 | UChar hh = tolower(*h); \ |
| 1482 | if (hh == 0) return NULL; \ |
| 1483 | if (hh != n0) { h++; continue; } \ |
| 1484 | \ |
| 1485 | UWord i; \ |
| 1486 | for (i = 0; i < nlen; i++) { \ |
| 1487 | if (tolower(n[i]) != tolower(h[i])) \ |
| 1488 | break; \ |
| 1489 | } \ |
| 1490 | /* assert(i >= 0 && i <= nlen); */ \ |
| 1491 | if (i == nlen) \ |
| 1492 | return h; \ |
| 1493 | \ |
| 1494 | h++; \ |
| 1495 | } \ |
| 1496 | } |
| 1497 | |
| 1498 | #if defined(VGO_linux) |
sewardj | 1b3a7a4 | 2011-10-26 15:10:49 +0000 | [diff] [blame] | 1499 | # if !defined(VGPV_arm_linux_android) |
| 1500 | STRCASESTR(VG_Z_LIBC_SONAME, strcasestr) |
| 1501 | # endif |
sewardj | 284f2a3 | 2011-10-21 08:07:13 +0000 | [diff] [blame] | 1502 | |
| 1503 | #elif defined(VGO_darwin) |
| 1504 | |
| 1505 | #endif |
| 1506 | |
| 1507 | |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1508 | /*------------------------------------------------------------*/ |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1509 | /*--- Improve definedness checking of process environment ---*/ |
| 1510 | /*------------------------------------------------------------*/ |
| 1511 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1512 | #if defined(VGO_linux) |
| 1513 | |
sewardj | bd2cff2 | 2011-08-16 21:45:28 +0000 | [diff] [blame] | 1514 | /* If these wind up getting generated via a macro, so that multiple |
| 1515 | versions of each function exist (as above), use the _EZU variants |
| 1516 | to assign equivalance class tags. */ |
| 1517 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1518 | /*---------------------- putenv ----------------------*/ |
| 1519 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1520 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string); |
| 1521 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1522 | { |
| 1523 | OrigFn fn; |
| 1524 | Word result; |
| 1525 | const char* p = string; |
| 1526 | VALGRIND_GET_ORIG_FN(fn); |
| 1527 | /* Now by walking over the string we magically produce |
| 1528 | traces when hitting undefined memory. */ |
| 1529 | if (p) |
| 1530 | while (*p++) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1531 | __asm__ __volatile__("" ::: "memory"); |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1532 | CALL_FN_W_W(result, fn, string); |
| 1533 | return result; |
| 1534 | } |
| 1535 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1536 | |
| 1537 | /*---------------------- unsetenv ----------------------*/ |
| 1538 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1539 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name); |
| 1540 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1541 | { |
| 1542 | OrigFn fn; |
| 1543 | Word result; |
| 1544 | const char* p = name; |
| 1545 | VALGRIND_GET_ORIG_FN(fn); |
| 1546 | /* Now by walking over the string we magically produce |
| 1547 | traces when hitting undefined memory. */ |
| 1548 | if (p) |
| 1549 | while (*p++) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1550 | __asm__ __volatile__("" ::: "memory"); |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1551 | CALL_FN_W_W(result, fn, name); |
| 1552 | return result; |
| 1553 | } |
| 1554 | |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1555 | |
| 1556 | /*---------------------- setenv ----------------------*/ |
| 1557 | |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1558 | /* setenv */ |
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); |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1561 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1562 | (const char* name, const char* value, int overwrite) |
| 1563 | { |
| 1564 | OrigFn fn; |
| 1565 | Word result; |
| 1566 | const char* p; |
| 1567 | VALGRIND_GET_ORIG_FN(fn); |
| 1568 | /* Now by walking over the string we magically produce |
| 1569 | traces when hitting undefined memory. */ |
| 1570 | if (name) |
| 1571 | for (p = name; *p; p++) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1572 | __asm__ __volatile__("" ::: "memory"); |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1573 | if (value) |
| 1574 | for (p = value; *p; p++) |
sewardj | 3c94445 | 2011-09-05 20:39:57 +0000 | [diff] [blame] | 1575 | __asm__ __volatile__("" ::: "memory"); |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1576 | VALGRIND_CHECK_VALUE_IS_DEFINED (overwrite); |
| 1577 | CALL_FN_W_WWW(result, fn, name, value, overwrite); |
| 1578 | return result; |
| 1579 | } |
| 1580 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1581 | #endif /* defined(VGO_linux) */ |
| 1582 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1583 | /*--------------------------------------------------------------------*/ |
njn | 4627586 | 2005-03-24 04:00:03 +0000 | [diff] [blame] | 1584 | /*--- end ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1585 | /*--------------------------------------------------------------------*/ |