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