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 | dda830a | 2003-07-20 22:28:42 +0000 | [diff] [blame] | 56 | /* Figure out if [dst .. dst+dstlen-1] overlaps with |
| 57 | [src .. src+srclen-1]. |
| 58 | We assume that the address ranges do not wrap around |
| 59 | (which is safe since on Linux addresses >= 0xC0000000 |
| 60 | are not accessible and the program will segfault in this |
| 61 | circumstance, presumably). |
| 62 | */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 63 | static __inline__ |
njn | c616819 | 2004-11-29 13:54:10 +0000 | [diff] [blame] | 64 | Bool is_overlap ( void* dst, const void* src, SizeT dstlen, SizeT srclen ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 65 | { |
sewardj | dda830a | 2003-07-20 22:28:42 +0000 | [diff] [blame] | 66 | Addr loS, hiS, loD, hiD; |
| 67 | |
| 68 | if (dstlen == 0 || srclen == 0) |
| 69 | return False; |
| 70 | |
| 71 | loS = (Addr)src; |
| 72 | loD = (Addr)dst; |
| 73 | hiS = loS + srclen - 1; |
| 74 | hiD = loD + dstlen - 1; |
| 75 | |
| 76 | /* So figure out if [loS .. hiS] overlaps with [loD .. hiD]. */ |
| 77 | if (loS < loD) { |
| 78 | return !(hiS < loD); |
| 79 | } |
| 80 | else if (loD < loS) { |
| 81 | return !(hiD < loS); |
| 82 | } |
| 83 | else { |
| 84 | /* They start at same place. Since we know neither of them has |
| 85 | zero length, they must overlap. */ |
| 86 | return True; |
| 87 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 88 | } |
| 89 | |
njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 90 | // This is a macro rather than a function because we don't want to have an |
| 91 | // extra function in the stack trace. |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 92 | #define RECORD_OVERLAP_ERROR(s, src, dst, len) \ |
njn | 1f8b3e7 | 2005-03-22 04:27:14 +0000 | [diff] [blame] | 93 | { \ |
| 94 | Word unused_res; \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 95 | VALGRIND_DO_CLIENT_REQUEST(unused_res, 0, \ |
| 96 | _VG_USERREQ__MEMCHECK_RECORD_OVERLAP_ERROR, \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 97 | s, src, dst, len, 0); \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 98 | } |
| 99 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 100 | |
| 101 | #define STRRCHR(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 102 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* s, int c ); \ |
| 103 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* s, int c ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 104 | { \ |
| 105 | UChar ch = (UChar)((UInt)c); \ |
| 106 | UChar* p = (UChar*)s; \ |
| 107 | UChar* last = NULL; \ |
| 108 | while (True) { \ |
| 109 | if (*p == ch) last = p; \ |
| 110 | if (*p == 0) return last; \ |
| 111 | p++; \ |
| 112 | } \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 113 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 114 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 115 | // Apparently rindex() is the same thing as strrchr() |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 116 | STRRCHR(VG_Z_LIBC_SONAME, strrchr) |
| 117 | STRRCHR(VG_Z_LIBC_SONAME, rindex) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 118 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 119 | STRRCHR(VG_Z_LIBC_SONAME, __GI_strrchr) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 120 | STRRCHR(VG_Z_LD_LINUX_SO_2, rindex) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 121 | #elif defined(VGO_darwin) |
| 122 | STRRCHR(VG_Z_DYLD, strrchr) |
| 123 | STRRCHR(VG_Z_DYLD, rindex) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 124 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | #define STRCHR(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 128 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* s, int c ); \ |
| 129 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* s, int c ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 130 | { \ |
| 131 | UChar ch = (UChar)((UInt)c); \ |
| 132 | UChar* p = (UChar*)s; \ |
| 133 | while (True) { \ |
| 134 | if (*p == ch) return p; \ |
| 135 | if (*p == 0) return NULL; \ |
| 136 | p++; \ |
| 137 | } \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 138 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 139 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 140 | // Apparently index() is the same thing as strchr() |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 141 | STRCHR(VG_Z_LIBC_SONAME, strchr) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 142 | STRCHR(VG_Z_LIBC_SONAME, index) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 143 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 144 | STRCHR(VG_Z_LIBC_SONAME, __GI_strchr) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 145 | STRCHR(VG_Z_LD_LINUX_SO_2, strchr) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 146 | STRCHR(VG_Z_LD_LINUX_SO_2, index) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 147 | STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, strchr) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 148 | STRCHR(VG_Z_LD_LINUX_X86_64_SO_2, index) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 149 | #elif defined(VGO_darwin) |
| 150 | STRCHR(VG_Z_DYLD, strchr) |
| 151 | STRCHR(VG_Z_DYLD, index) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 152 | #endif |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 153 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 154 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 155 | #define STRCAT(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 156 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ); \ |
| 157 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 158 | { \ |
| 159 | const Char* src_orig = src; \ |
| 160 | Char* dst_orig = dst; \ |
| 161 | while (*dst) dst++; \ |
| 162 | while (*src) *dst++ = *src++; \ |
| 163 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 164 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 165 | /* This is a bit redundant, I think; any overlap and the strcat will */ \ |
| 166 | /* go forever... or until a seg fault occurs. */ \ |
| 167 | if (is_overlap(dst_orig, \ |
| 168 | src_orig, \ |
| 169 | (Addr)dst-(Addr)dst_orig+1, \ |
| 170 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 171 | RECORD_OVERLAP_ERROR("strcat", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 172 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 173 | return dst_orig; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 174 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 175 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 176 | STRCAT(VG_Z_LIBC_SONAME, strcat) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 177 | #if defined(VGO_linux) |
| 178 | STRCAT(VG_Z_LIBC_SONAME, __GI_strcat) |
| 179 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 180 | |
| 181 | #define STRNCAT(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 182 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 183 | ( char* dst, const char* src, SizeT n ); \ |
| 184 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 185 | ( char* dst, const char* src, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 186 | { \ |
| 187 | const Char* src_orig = src; \ |
| 188 | Char* dst_orig = dst; \ |
| 189 | SizeT m = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 190 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 191 | while (*dst) dst++; \ |
| 192 | while (m < n && *src) { m++; *dst++ = *src++; } /* concat <= n chars */ \ |
| 193 | *dst = 0; /* always add null */ \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 194 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 195 | /* This checks for overlap after copying, unavoidable without */ \ |
| 196 | /* pre-counting lengths... should be ok */ \ |
| 197 | if (is_overlap(dst_orig, \ |
| 198 | src_orig, \ |
| 199 | (Addr)dst-(Addr)dst_orig+1, \ |
| 200 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 201 | RECORD_OVERLAP_ERROR("strncat", dst_orig, src_orig, n); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 202 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 203 | return dst_orig; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 204 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 205 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 206 | STRNCAT(VG_Z_LIBC_SONAME, strncat) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 207 | #if defined(VGO_darwin) |
| 208 | STRNCAT(VG_Z_DYLD, strncat) |
| 209 | #endif |
| 210 | |
| 211 | |
| 212 | /* Append src to dst. n is the size of dst's buffer. dst is guaranteed |
| 213 | to be nul-terminated after the copy, unless n <= strlen(dst_orig). |
| 214 | Returns min(n, strlen(dst_orig)) + strlen(src_orig). |
| 215 | Truncation occurred if retval >= n. |
| 216 | */ |
| 217 | #define STRLCAT(soname, fnname) \ |
| 218 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 219 | ( char* dst, const char* src, SizeT n ); \ |
| 220 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 221 | ( char* dst, const char* src, SizeT n ) \ |
| 222 | { \ |
| 223 | const Char* src_orig = src; \ |
| 224 | Char* dst_orig = dst; \ |
| 225 | SizeT m = 0; \ |
| 226 | \ |
| 227 | while (m < n && *dst) { m++; dst++; } \ |
| 228 | if (m < n) { \ |
| 229 | /* Fill as far as dst_orig[n-2], then nul-terminate. */ \ |
| 230 | while (m < n-1 && *src) { m++; *dst++ = *src++; } \ |
| 231 | *dst = 0; \ |
| 232 | } else { \ |
| 233 | /* No space to copy anything to dst. m == n */ \ |
| 234 | } \ |
| 235 | /* Finish counting min(n, strlen(dst_orig)) + strlen(src_orig) */ \ |
| 236 | while (*src) { m++; src++; } \ |
| 237 | /* This checks for overlap after copying, unavoidable without */ \ |
| 238 | /* pre-counting lengths... should be ok */ \ |
| 239 | if (is_overlap(dst_orig, \ |
| 240 | src_orig, \ |
| 241 | (Addr)dst-(Addr)dst_orig+1, \ |
| 242 | (Addr)src-(Addr)src_orig+1)) \ |
| 243 | RECORD_OVERLAP_ERROR("strlcat", dst_orig, src_orig, n); \ |
| 244 | \ |
| 245 | return m; \ |
| 246 | } |
| 247 | |
| 248 | #if defined(VGO_darwin) |
| 249 | STRLCAT(VG_Z_LIBC_SONAME, strlcat) |
| 250 | STRLCAT(VG_Z_DYLD, strlcat) |
| 251 | #endif |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 252 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 253 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 254 | #define STRNLEN(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 255 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* str, SizeT n ); \ |
| 256 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) ( const char* str, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 257 | { \ |
| 258 | SizeT i = 0; \ |
| 259 | while (i < n && str[i] != 0) i++; \ |
| 260 | return i; \ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 261 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 262 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 263 | STRNLEN(VG_Z_LIBC_SONAME, strnlen) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 264 | #if defined(VGO_linux) |
| 265 | STRNLEN(VG_Z_LIBC_SONAME, __GI_strnlen) |
| 266 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 267 | |
sewardj | 3ceec24 | 2003-07-30 21:24:25 +0000 | [diff] [blame] | 268 | |
njn | 5ec15ed | 2005-08-24 19:55:51 +0000 | [diff] [blame] | 269 | // Note that this replacement often doesn't get used because gcc inlines |
| 270 | // calls to strlen() with its own built-in version. This can be very |
| 271 | // confusing if you aren't expecting it. Other small functions in this file |
| 272 | // may also be inline by gcc. |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 273 | #define STRLEN(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 274 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* str ); \ |
| 275 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname)( const char* str ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 276 | { \ |
| 277 | SizeT i = 0; \ |
| 278 | while (str[i] != 0) i++; \ |
| 279 | return i; \ |
sewardj | 3ceec24 | 2003-07-30 21:24:25 +0000 | [diff] [blame] | 280 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 281 | |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 282 | STRLEN(VG_Z_LIBC_SONAME, strlen) |
| 283 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 284 | STRLEN(VG_Z_LIBC_SONAME, __GI_strlen) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 285 | STRLEN(VG_Z_LD_LINUX_SO_2, strlen) |
| 286 | STRLEN(VG_Z_LD_LINUX_X86_64_SO_2, strlen) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 287 | #endif |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 288 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 289 | |
| 290 | #define STRCPY(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 291 | char* VG_REPLACE_FUNCTION_ZU(soname, fnname) ( char* dst, const char* src ); \ |
| 292 | char* VG_REPLACE_FUNCTION_ZU(soname, fnname) ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 293 | { \ |
| 294 | const Char* src_orig = src; \ |
| 295 | Char* dst_orig = dst; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 296 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 297 | while (*src) *dst++ = *src++; \ |
| 298 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 299 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 300 | /* This checks for overlap after copying, unavoidable without */ \ |
| 301 | /* pre-counting length... should be ok */ \ |
| 302 | if (is_overlap(dst_orig, \ |
| 303 | src_orig, \ |
| 304 | (Addr)dst-(Addr)dst_orig+1, \ |
| 305 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 306 | RECORD_OVERLAP_ERROR("strcpy", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 307 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 308 | return dst_orig; \ |
| 309 | } |
| 310 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 311 | STRCPY(VG_Z_LIBC_SONAME, strcpy) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 312 | #if defined(VGO_linux) |
| 313 | STRCPY(VG_Z_LIBC_SONAME, __GI_strcpy) |
| 314 | #elif defined(VGO_darwin) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 315 | STRCPY(VG_Z_DYLD, strcpy) |
| 316 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | #define STRNCPY(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 320 | char* VG_REPLACE_FUNCTION_ZU(soname, fnname) \ |
| 321 | ( char* dst, const char* src, SizeT n ); \ |
| 322 | char* VG_REPLACE_FUNCTION_ZU(soname, fnname) \ |
| 323 | ( char* dst, const char* src, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 324 | { \ |
| 325 | const Char* src_orig = src; \ |
| 326 | Char* dst_orig = dst; \ |
| 327 | SizeT m = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 328 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 329 | while (m < n && *src) { m++; *dst++ = *src++; } \ |
| 330 | /* Check for overlap after copying; all n bytes of dst are relevant, */ \ |
| 331 | /* but only m+1 bytes of src if terminator was found */ \ |
| 332 | if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 333 | RECORD_OVERLAP_ERROR("strncpy", dst, src, n); \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 334 | while (m++ < n) *dst++ = 0; /* must pad remainder with nulls */ \ |
| 335 | \ |
| 336 | return dst_orig; \ |
| 337 | } |
| 338 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 339 | STRNCPY(VG_Z_LIBC_SONAME, strncpy) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 340 | #if defined(VGO_linux) |
| 341 | STRNCPY(VG_Z_LIBC_SONAME, __GI_strncpy) |
| 342 | #elif defined(VGO_darwin) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 343 | STRNCPY(VG_Z_DYLD, strncpy) |
| 344 | #endif |
| 345 | |
| 346 | |
| 347 | /* Copy up to n-1 bytes from src to dst. Then nul-terminate dst if n > 0. |
| 348 | Returns strlen(src). Does not zero-fill the remainder of dst. */ |
| 349 | #define STRLCPY(soname, fnname) \ |
| 350 | SizeT VG_REPLACE_FUNCTION_ZU(soname, fnname) \ |
| 351 | ( char* dst, const char* src, SizeT n ); \ |
| 352 | SizeT VG_REPLACE_FUNCTION_ZU(soname, fnname) \ |
| 353 | ( char* dst, const char* src, SizeT n ) \ |
| 354 | { \ |
| 355 | const char* src_orig = src; \ |
| 356 | char* dst_orig = dst; \ |
| 357 | SizeT m = 0; \ |
| 358 | \ |
| 359 | while (m < n-1 && *src) { m++; *dst++ = *src++; } \ |
| 360 | /* m non-nul bytes have now been copied, and m <= n-1. */ \ |
| 361 | /* Check for overlap after copying; all n bytes of dst are relevant, */ \ |
| 362 | /* but only m+1 bytes of src if terminator was found */ \ |
| 363 | if (is_overlap(dst_orig, src_orig, n, (m < n) ? m+1 : n)) \ |
| 364 | RECORD_OVERLAP_ERROR("strlcpy", dst, src, n); \ |
| 365 | /* Nul-terminate dst. */ \ |
| 366 | if (n > 0) *dst = 0; \ |
| 367 | /* Finish counting strlen(src). */ \ |
| 368 | while (*src) src++; \ |
| 369 | return src - src_orig; \ |
| 370 | } |
| 371 | |
| 372 | #if defined(VGO_darwin) |
| 373 | STRLCPY(VG_Z_LIBC_SONAME, strlcpy) |
| 374 | STRLCPY(VG_Z_DYLD, strlcpy) |
| 375 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 376 | |
| 377 | |
| 378 | #define STRNCMP(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 379 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 380 | ( const char* s1, const char* s2, SizeT nmax ); \ |
| 381 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 382 | ( const char* s1, const char* s2, SizeT nmax ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 383 | { \ |
| 384 | SizeT n = 0; \ |
| 385 | while (True) { \ |
| 386 | if (n >= nmax) return 0; \ |
| 387 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 388 | if (*s1 == 0) return -1; \ |
| 389 | if (*s2 == 0) return 1; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 390 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 391 | if (*(unsigned char*)s1 < *(unsigned char*)s2) return -1; \ |
| 392 | if (*(unsigned char*)s1 > *(unsigned char*)s2) return 1; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 393 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 394 | s1++; s2++; n++; \ |
| 395 | } \ |
| 396 | } |
| 397 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 398 | STRNCMP(VG_Z_LIBC_SONAME, strncmp) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 399 | #if defined(VGO_linux) |
| 400 | STRNCMP(VG_Z_LIBC_SONAME, __GI_strncmp) |
| 401 | #elif defined(VGO_darwin) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 402 | STRNCMP(VG_Z_DYLD, strncmp) |
| 403 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 404 | |
| 405 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 406 | #define STRCASECMP(soname, fnname) \ |
| 407 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 408 | ( const char* s1, const char* s2 ); \ |
| 409 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 410 | ( const char* s1, const char* s2 ) \ |
| 411 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 412 | extern int tolower(int); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 413 | register unsigned char c1; \ |
| 414 | register unsigned char c2; \ |
| 415 | while (True) { \ |
| 416 | c1 = tolower(*(unsigned char *)s1); \ |
| 417 | c2 = tolower(*(unsigned char *)s2); \ |
| 418 | if (c1 != c2) break; \ |
| 419 | if (c1 == 0) break; \ |
| 420 | s1++; s2++; \ |
| 421 | } \ |
| 422 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 423 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 424 | return 0; \ |
| 425 | } |
| 426 | |
| 427 | STRCASECMP(VG_Z_LIBC_SONAME, strcasecmp) |
| 428 | #if defined(VGO_linux) |
| 429 | STRCASECMP(VG_Z_LIBC_SONAME, __GI_strcasecmp) |
| 430 | #endif |
| 431 | |
| 432 | |
| 433 | #define STRNCASECMP(soname, fnname) \ |
| 434 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 435 | ( const char* s1, const char* s2, SizeT nmax ); \ |
| 436 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 437 | ( const char* s1, const char* s2, SizeT nmax ) \ |
| 438 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 439 | extern int tolower(int); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 440 | SizeT n = 0; \ |
| 441 | while (True) { \ |
| 442 | if (n >= nmax) return 0; \ |
| 443 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 444 | if (*s1 == 0) return -1; \ |
| 445 | if (*s2 == 0) return 1; \ |
| 446 | \ |
| 447 | if (tolower(*(unsigned char*)s1) < tolower(*(unsigned char*)s2)) return -1; \ |
| 448 | if (tolower(*(unsigned char*)s1) > tolower(*(unsigned char*)s2)) return 1; \ |
| 449 | \ |
| 450 | s1++; s2++; n++; \ |
| 451 | } \ |
| 452 | } |
| 453 | |
| 454 | STRNCASECMP(VG_Z_LIBC_SONAME, strncasecmp) |
| 455 | #if defined(VGO_linux) |
| 456 | STRNCASECMP(VG_Z_LIBC_SONAME, __GI_strncasecmp) |
| 457 | #elif defined(VGO_darwin) |
| 458 | STRNCASECMP(VG_Z_DYLD, strncasecmp) |
| 459 | #endif |
| 460 | |
| 461 | |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 462 | #define STRCASECMP_L(soname, fnname) \ |
| 463 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 464 | ( const char* s1, const char* s2, void* locale ); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 465 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 466 | ( const char* s1, const char* s2, void* locale ) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 467 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 468 | extern int tolower_l(int, void*) __attribute__((weak)); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 469 | register unsigned char c1; \ |
| 470 | register unsigned char c2; \ |
| 471 | while (True) { \ |
| 472 | c1 = tolower_l(*(unsigned char *)s1, locale); \ |
| 473 | c2 = tolower_l(*(unsigned char *)s2, locale); \ |
| 474 | if (c1 != c2) break; \ |
| 475 | if (c1 == 0) break; \ |
| 476 | s1++; s2++; \ |
| 477 | } \ |
| 478 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 479 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 480 | return 0; \ |
| 481 | } |
| 482 | |
| 483 | STRCASECMP_L(VG_Z_LIBC_SONAME, strcasecmp_l) |
| 484 | #if defined(VGO_linux) |
| 485 | STRCASECMP_L(VG_Z_LIBC_SONAME, __GI_strcasecmp_l) |
| 486 | #endif |
| 487 | |
| 488 | |
| 489 | #define STRNCASECMP_L(soname, fnname) \ |
| 490 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 491 | ( const char* s1, const char* s2, SizeT nmax, void* locale ); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 492 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 493 | ( const char* s1, const char* s2, SizeT nmax, void* locale ) \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 494 | { \ |
tom | e03c8c4 | 2010-11-12 10:40:20 +0000 | [diff] [blame] | 495 | extern int tolower_l(int, void*) __attribute__((weak)); \ |
tom | ce6d0ac | 2010-11-12 10:03:13 +0000 | [diff] [blame] | 496 | SizeT n = 0; \ |
| 497 | while (True) { \ |
| 498 | if (n >= nmax) return 0; \ |
| 499 | if (*s1 == 0 && *s2 == 0) return 0; \ |
| 500 | if (*s1 == 0) return -1; \ |
| 501 | if (*s2 == 0) return 1; \ |
| 502 | \ |
| 503 | if (tolower_l(*(unsigned char*)s1, locale) < tolower_l(*(unsigned char*)s2, locale)) return -1; \ |
| 504 | if (tolower_l(*(unsigned char*)s1, locale) > tolower_l(*(unsigned char*)s2, locale)) return 1; \ |
| 505 | \ |
| 506 | s1++; s2++; n++; \ |
| 507 | } \ |
| 508 | } |
| 509 | |
| 510 | STRNCASECMP_L(VG_Z_LIBC_SONAME, strncasecmp_l) |
| 511 | #if defined(VGO_linux) |
| 512 | STRNCASECMP_L(VG_Z_LIBC_SONAME, __GI_strncasecmp_l) |
| 513 | #elif defined(VGO_darwin) |
| 514 | STRNCASECMP_L(VG_Z_DYLD, strncasecmp_l) |
| 515 | #endif |
| 516 | |
| 517 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 518 | #define STRCMP(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 519 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 520 | ( const char* s1, const char* s2 ); \ |
| 521 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 522 | ( const char* s1, const char* s2 ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 523 | { \ |
| 524 | register unsigned char c1; \ |
| 525 | register unsigned char c2; \ |
| 526 | while (True) { \ |
| 527 | c1 = *(unsigned char *)s1; \ |
| 528 | c2 = *(unsigned char *)s2; \ |
| 529 | if (c1 != c2) break; \ |
| 530 | if (c1 == 0) break; \ |
| 531 | s1++; s2++; \ |
| 532 | } \ |
| 533 | if ((unsigned char)c1 < (unsigned char)c2) return -1; \ |
| 534 | if ((unsigned char)c1 > (unsigned char)c2) return 1; \ |
| 535 | return 0; \ |
| 536 | } |
| 537 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 538 | STRCMP(VG_Z_LIBC_SONAME, strcmp) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 539 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 540 | STRCMP(VG_Z_LIBC_SONAME, __GI_strcmp) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 541 | STRCMP(VG_Z_LD_LINUX_X86_64_SO_2, strcmp) |
| 542 | STRCMP(VG_Z_LD64_SO_1, strcmp) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 543 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 544 | |
| 545 | |
| 546 | #define MEMCHR(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 547 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const void *s, int c, SizeT n); \ |
| 548 | 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] | 549 | { \ |
| 550 | SizeT i; \ |
| 551 | UChar c0 = (UChar)c; \ |
| 552 | UChar* p = (UChar*)s; \ |
| 553 | for (i = 0; i < n; i++) \ |
| 554 | if (p[i] == c0) return (void*)(&p[i]); \ |
| 555 | return NULL; \ |
| 556 | } |
| 557 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 558 | MEMCHR(VG_Z_LIBC_SONAME, memchr) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 559 | #if defined(VGO_darwin) |
| 560 | MEMCHR(VG_Z_DYLD, memchr) |
| 561 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 562 | |
| 563 | |
| 564 | #define MEMCPY(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 565 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 566 | ( void *dst, const void *src, SizeT len ); \ |
| 567 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 568 | ( void *dst, const void *src, SizeT len ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 569 | { \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 570 | if (is_overlap(dst, src, len, len)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 571 | RECORD_OVERLAP_ERROR("memcpy", dst, src, len); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 572 | \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 573 | const Addr WS = sizeof(UWord); /* 8 or 4 */ \ |
| 574 | const Addr WM = WS - 1; /* 7 or 3 */ \ |
| 575 | \ |
| 576 | if (dst < src) { \ |
| 577 | \ |
| 578 | /* Copying backwards. */ \ |
| 579 | SizeT n = len; \ |
| 580 | Addr d = (Addr)dst; \ |
| 581 | Addr s = (Addr)src; \ |
| 582 | \ |
| 583 | if (((s^d) & WM) == 0) { \ |
| 584 | /* s and d have same UWord alignment. */ \ |
| 585 | /* Pull up to a UWord boundary. */ \ |
| 586 | while ((s & WM) != 0 && n >= 1) \ |
| 587 | { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \ |
| 588 | /* Copy UWords. */ \ |
| 589 | while (n >= WS) \ |
| 590 | { *(UWord*)d = *(UWord*)s; s += WS; d += WS; n -= WS; } \ |
| 591 | if (n == 0) \ |
| 592 | return dst; \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 593 | } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 594 | if (((s|d) & 1) == 0) { \ |
| 595 | /* Both are 16-aligned; copy what we can thusly. */ \ |
| 596 | while (n >= 2) \ |
| 597 | { *(UShort*)d = *(UShort*)s; s += 2; d += 2; n -= 2; } \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 598 | } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 599 | /* Copy leftovers, or everything if misaligned. */ \ |
| 600 | while (n >= 1) \ |
| 601 | { *(UChar*)d = *(UChar*)s; s += 1; d += 1; n -= 1; } \ |
| 602 | \ |
| 603 | } else if (dst > src) { \ |
| 604 | \ |
| 605 | SizeT n = len; \ |
| 606 | Addr d = ((Addr)dst) + n; \ |
| 607 | Addr s = ((Addr)src) + n; \ |
| 608 | \ |
| 609 | /* Copying forwards. */ \ |
| 610 | if (((s^d) & WM) == 0) { \ |
| 611 | /* s and d have same UWord alignment. */ \ |
| 612 | /* Back down to a UWord boundary. */ \ |
| 613 | while ((s & WM) != 0 && n >= 1) \ |
| 614 | { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \ |
| 615 | /* Copy UWords. */ \ |
| 616 | while (n >= WS) \ |
| 617 | { s -= WS; d -= WS; *(UWord*)d = *(UWord*)s; n -= WS; } \ |
| 618 | if (n == 0) \ |
| 619 | return dst; \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 620 | } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 621 | if (((s|d) & 1) == 0) { \ |
| 622 | /* Both are 16-aligned; copy what we can thusly. */ \ |
| 623 | while (n >= 2) \ |
| 624 | { s -= 2; d -= 2; *(UShort*)d = *(UShort*)s; n -= 2; } \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 625 | } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 626 | /* Copy leftovers, or everything if misaligned. */ \ |
| 627 | while (n >= 1) \ |
| 628 | { s -= 1; d -= 1; *(UChar*)d = *(UChar*)s; n -= 1; } \ |
| 629 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 630 | } \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 631 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 632 | return dst; \ |
| 633 | } |
| 634 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 635 | MEMCPY(VG_Z_LIBC_SONAME, memcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 636 | #if defined(VGO_linux) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 637 | MEMCPY(VG_Z_LD_SO_1, memcpy) /* ld.so.1 */ |
| 638 | MEMCPY(VG_Z_LD64_SO_1, memcpy) /* ld64.so.1 */ |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 639 | #elif defined(VGO_darwin) |
| 640 | MEMCPY(VG_Z_DYLD, memcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 641 | #endif |
sewardj | f0b3432 | 2007-01-16 21:42:28 +0000 | [diff] [blame] | 642 | /* icc9 blats these around all over the place. Not only in the main |
| 643 | executable but various .so's. They are highly tuned and read |
| 644 | memory beyond the source boundary (although work correctly and |
| 645 | never go across page boundaries), so give errors when run natively, |
| 646 | at least for misaligned source arg. Just intercepting in the exe |
| 647 | only until we understand more about the problem. See |
| 648 | http://bugs.kde.org/show_bug.cgi?id=139776 |
| 649 | */ |
| 650 | MEMCPY(NONE, _intel_fast_memcpy) |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 651 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 652 | |
| 653 | #define MEMCMP(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 654 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 655 | ( const void *s1V, const void *s2V, SizeT n ); \ |
| 656 | int VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 657 | ( const void *s1V, const void *s2V, SizeT n ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 658 | { \ |
| 659 | int res; \ |
| 660 | unsigned char a0; \ |
| 661 | unsigned char b0; \ |
| 662 | unsigned char* s1 = (unsigned char*)s1V; \ |
| 663 | unsigned char* s2 = (unsigned char*)s2V; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 664 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 665 | while (n != 0) { \ |
| 666 | a0 = s1[0]; \ |
| 667 | b0 = s2[0]; \ |
| 668 | s1 += 1; \ |
| 669 | s2 += 1; \ |
| 670 | res = ((int)a0) - ((int)b0); \ |
| 671 | if (res != 0) \ |
| 672 | return res; \ |
| 673 | n -= 1; \ |
| 674 | } \ |
| 675 | return 0; \ |
| 676 | } |
| 677 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 678 | MEMCMP(VG_Z_LIBC_SONAME, memcmp) |
| 679 | MEMCMP(VG_Z_LIBC_SONAME, bcmp) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 680 | #if defined(VGO_linux) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 681 | MEMCMP(VG_Z_LD_SO_1, bcmp) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 682 | #elif defined(VGO_darwin) |
| 683 | MEMCMP(VG_Z_DYLD, memcmp) |
| 684 | MEMCMP(VG_Z_DYLD, bcmp) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 685 | #endif |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 686 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 687 | |
| 688 | /* Copy SRC to DEST, returning the address of the terminating '\0' in |
| 689 | DEST. (minor variant of strcpy) */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 690 | #define STPCPY(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 691 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ); \ |
| 692 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) ( char* dst, const char* src ) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 693 | { \ |
| 694 | const Char* src_orig = src; \ |
| 695 | Char* dst_orig = dst; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 696 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 697 | while (*src) *dst++ = *src++; \ |
| 698 | *dst = 0; \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 699 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 700 | /* This checks for overlap after copying, unavoidable without */ \ |
| 701 | /* pre-counting length... should be ok */ \ |
| 702 | if (is_overlap(dst_orig, \ |
| 703 | src_orig, \ |
| 704 | (Addr)dst-(Addr)dst_orig+1, \ |
| 705 | (Addr)src-(Addr)src_orig+1)) \ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 706 | RECORD_OVERLAP_ERROR("stpcpy", dst_orig, src_orig, 0); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 707 | \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 708 | return dst; \ |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 709 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 710 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 711 | STPCPY(VG_Z_LIBC_SONAME, stpcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 712 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 713 | STPCPY(VG_Z_LIBC_SONAME, __GI_stpcpy) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 714 | STPCPY(VG_Z_LD_LINUX_SO_2, stpcpy) |
| 715 | STPCPY(VG_Z_LD_LINUX_X86_64_SO_2, stpcpy) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 716 | #elif defined(VGO_darwin) |
| 717 | STPCPY(VG_Z_DYLD, stpcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 718 | #endif |
| 719 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 720 | |
| 721 | #define MEMSET(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 722 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname)(void *s, Int c, SizeT n); \ |
| 723 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname)(void *s, Int c, SizeT n) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 724 | { \ |
sewardj | 7b4e00b | 2010-08-24 09:05:52 +0000 | [diff] [blame] | 725 | Addr a = (Addr)s; \ |
| 726 | UInt c4 = (c & 0xFF); \ |
| 727 | c4 = (c4 << 8) | c4; \ |
| 728 | c4 = (c4 << 16) | c4; \ |
| 729 | while ((a & 3) != 0 && n >= 1) \ |
| 730 | { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \ |
| 731 | while (n >= 4) \ |
| 732 | { *(UInt*)a = c4; a += 4; n -= 4; } \ |
| 733 | while (n >= 1) \ |
| 734 | { *(UChar*)a = (UChar)c; a += 1; n -= 1; } \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 735 | return s; \ |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 736 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 737 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 738 | MEMSET(VG_Z_LIBC_SONAME, memset) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 739 | #if defined(VGO_darwin) |
| 740 | MEMSET(VG_Z_DYLD, memset) |
| 741 | #endif |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 742 | |
| 743 | |
| 744 | #define MEMMOVE(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 745 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 746 | (void *dstV, const void *srcV, SizeT n); \ |
| 747 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 748 | (void *dstV, const void *srcV, SizeT n) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 749 | { \ |
| 750 | SizeT i; \ |
| 751 | Char* dst = (Char*)dstV; \ |
| 752 | Char* src = (Char*)srcV; \ |
| 753 | if (dst < src) { \ |
| 754 | for (i = 0; i < n; i++) \ |
| 755 | dst[i] = src[i]; \ |
| 756 | } \ |
| 757 | else \ |
| 758 | if (dst > src) { \ |
| 759 | for (i = 0; i < n; i++) \ |
| 760 | dst[n-i-1] = src[n-i-1]; \ |
| 761 | } \ |
| 762 | return dst; \ |
| 763 | } |
| 764 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 765 | MEMMOVE(VG_Z_LIBC_SONAME, memmove) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 766 | #if defined(VGO_darwin) |
| 767 | MEMMOVE(VG_Z_DYLD, memmove) |
| 768 | #endif |
| 769 | |
| 770 | |
| 771 | #define BCOPY(soname, fnname) \ |
| 772 | void VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 773 | (const void *srcV, void *dstV, SizeT n); \ |
| 774 | void VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 775 | (const void *srcV, void *dstV, SizeT n) \ |
| 776 | { \ |
| 777 | SizeT i; \ |
| 778 | Char* dst = (Char*)dstV; \ |
| 779 | Char* src = (Char*)srcV; \ |
| 780 | if (dst < src) { \ |
| 781 | for (i = 0; i < n; i++) \ |
| 782 | dst[i] = src[i]; \ |
| 783 | } \ |
| 784 | else \ |
| 785 | if (dst > src) { \ |
| 786 | for (i = 0; i < n; i++) \ |
| 787 | dst[n-i-1] = src[n-i-1]; \ |
| 788 | } \ |
| 789 | } |
| 790 | |
| 791 | #if defined(VGO_darwin) |
| 792 | BCOPY(VG_Z_LIBC_SONAME, bcopy) |
| 793 | BCOPY(VG_Z_DYLD, bcopy) |
| 794 | #endif |
sewardj | 44e495f | 2005-05-12 17:58:28 +0000 | [diff] [blame] | 795 | |
jseward | 0845ef8 | 2003-12-22 22:31:27 +0000 | [diff] [blame] | 796 | |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 797 | /* glibc 2.5 variant of memmove which checks the dest is big enough. |
| 798 | There is no specific part of glibc that this is copied from. */ |
| 799 | #define GLIBC25___MEMMOVE_CHK(soname, fnname) \ |
| 800 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 801 | (void *dstV, const void *srcV, SizeT n, SizeT destlen); \ |
| 802 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 803 | (void *dstV, const void *srcV, SizeT n, SizeT destlen) \ |
| 804 | { \ |
| 805 | extern void _exit(int status); \ |
| 806 | SizeT i; \ |
| 807 | Char* dst = (Char*)dstV; \ |
| 808 | Char* src = (Char*)srcV; \ |
| 809 | if (destlen < n) \ |
| 810 | goto badness; \ |
| 811 | if (dst < src) { \ |
| 812 | for (i = 0; i < n; i++) \ |
| 813 | dst[i] = src[i]; \ |
| 814 | } \ |
| 815 | else \ |
| 816 | if (dst > src) { \ |
| 817 | for (i = 0; i < n; i++) \ |
| 818 | dst[n-i-1] = src[n-i-1]; \ |
| 819 | } \ |
| 820 | return dst; \ |
| 821 | badness: \ |
| 822 | VALGRIND_PRINTF_BACKTRACE( \ |
| 823 | "*** memmove_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 824 | "program terminated\n"); \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 825 | _exit(127); \ |
sewardj | c271ec8 | 2007-02-27 22:36:14 +0000 | [diff] [blame] | 826 | /*NOTREACHED*/ \ |
| 827 | return NULL; \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 828 | } |
| 829 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 830 | GLIBC25___MEMMOVE_CHK(VG_Z_LIBC_SONAME, __memmove_chk) |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 831 | |
| 832 | |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 833 | /* Find the first occurrence of C in S or the final NUL byte. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 834 | #define GLIBC232_STRCHRNUL(soname, fnname) \ |
sewardj | 0ec07f3 | 2006-01-12 12:32:32 +0000 | [diff] [blame] | 835 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const char* s, int c_in); \ |
| 836 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) (const char* s, int c_in) \ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 837 | { \ |
| 838 | unsigned char c = (unsigned char) c_in; \ |
| 839 | unsigned char* char_ptr = (unsigned char *)s; \ |
| 840 | while (1) { \ |
| 841 | if (*char_ptr == 0) return char_ptr; \ |
| 842 | if (*char_ptr == c) return char_ptr; \ |
| 843 | char_ptr++; \ |
| 844 | } \ |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 845 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 846 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 847 | GLIBC232_STRCHRNUL(VG_Z_LIBC_SONAME, strchrnul) |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 848 | |
| 849 | |
| 850 | /* Find the first occurrence of C in S. */ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 851 | #define GLIBC232_RAWMEMCHR(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 == c) return char_ptr; \ |
| 859 | char_ptr++; \ |
| 860 | } \ |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 861 | } |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 862 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 863 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, rawmemchr) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 864 | #if defined (VGO_linux) |
| 865 | GLIBC232_RAWMEMCHR(VG_Z_LIBC_SONAME, __GI___rawmemchr) |
| 866 | #endif |
sewardj | 4e9a4b6 | 2004-11-23 00:20:17 +0000 | [diff] [blame] | 867 | |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 868 | /* glibc variant of strcpy that checks the dest is big enough. |
| 869 | Copied from glibc-2.5/debug/test-strcpy_chk.c. */ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 870 | #define GLIBC25___STRCPY_CHK(soname,fnname) \ |
| 871 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 872 | (char* dst, const char* src, SizeT len); \ |
| 873 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 874 | (char* dst, const char* src, SizeT len) \ |
| 875 | { \ |
| 876 | extern void _exit(int status); \ |
| 877 | char* ret = dst; \ |
| 878 | if (! len) \ |
| 879 | goto badness; \ |
| 880 | while ((*dst++ = *src++) != '\0') \ |
| 881 | if (--len == 0) \ |
| 882 | goto badness; \ |
| 883 | return ret; \ |
| 884 | badness: \ |
| 885 | VALGRIND_PRINTF_BACKTRACE( \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 886 | "*** strcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 887 | "program terminated\n"); \ |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 888 | _exit(127); \ |
| 889 | /*NOTREACHED*/ \ |
| 890 | return NULL; \ |
| 891 | } |
| 892 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 893 | GLIBC25___STRCPY_CHK(VG_Z_LIBC_SONAME, __strcpy_chk) |
sewardj | 620e526 | 2006-12-31 00:22:30 +0000 | [diff] [blame] | 894 | |
| 895 | |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 896 | /* glibc variant of stpcpy that checks the dest is big enough. |
| 897 | Copied from glibc-2.5/debug/test-stpcpy_chk.c. */ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 898 | #define GLIBC25___STPCPY_CHK(soname,fnname) \ |
| 899 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 900 | (char* dst, const char* src, SizeT len); \ |
| 901 | char* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 902 | (char* dst, const char* src, SizeT len) \ |
| 903 | { \ |
| 904 | extern void _exit(int status); \ |
sewardj | dc5d832 | 2007-01-28 06:32:01 +0000 | [diff] [blame] | 905 | if (! len) \ |
| 906 | goto badness; \ |
| 907 | while ((*dst++ = *src++) != '\0') \ |
| 908 | if (--len == 0) \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 909 | goto badness; \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 910 | return dst - 1; \ |
| 911 | badness: \ |
| 912 | VALGRIND_PRINTF_BACKTRACE( \ |
sewardj | 24cb217 | 2007-02-23 09:03:26 +0000 | [diff] [blame] | 913 | "*** stpcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 914 | "program terminated\n"); \ |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 915 | _exit(127); \ |
| 916 | /*NOTREACHED*/ \ |
| 917 | return NULL; \ |
| 918 | } |
| 919 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 920 | GLIBC25___STPCPY_CHK(VG_Z_LIBC_SONAME, __stpcpy_chk) |
sewardj | b8d0385 | 2007-01-27 00:49:44 +0000 | [diff] [blame] | 921 | |
| 922 | |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 923 | /* mempcpy */ |
| 924 | #define GLIBC25_MEMPCPY(soname, fnname) \ |
| 925 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 926 | ( void *dst, const void *src, SizeT len ); \ |
| 927 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 928 | ( void *dst, const void *src, SizeT len ) \ |
| 929 | { \ |
| 930 | register char *d; \ |
| 931 | register char *s; \ |
| 932 | SizeT len_saved = len; \ |
| 933 | \ |
| 934 | if (len == 0) \ |
| 935 | return dst; \ |
| 936 | \ |
| 937 | if (is_overlap(dst, src, len, len)) \ |
| 938 | RECORD_OVERLAP_ERROR("mempcpy", dst, src, len); \ |
| 939 | \ |
| 940 | if ( dst > src ) { \ |
| 941 | d = (char *)dst + len - 1; \ |
| 942 | s = (char *)src + len - 1; \ |
| 943 | while ( len-- ) { \ |
| 944 | *d-- = *s--; \ |
| 945 | } \ |
| 946 | } else if ( dst < src ) { \ |
| 947 | d = (char *)dst; \ |
| 948 | s = (char *)src; \ |
| 949 | while ( len-- ) { \ |
| 950 | *d++ = *s++; \ |
| 951 | } \ |
| 952 | } \ |
| 953 | return (void*)( ((char*)dst) + len_saved ); \ |
| 954 | } |
| 955 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 956 | GLIBC25_MEMPCPY(VG_Z_LIBC_SONAME, mempcpy) |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 957 | #if defined(VGO_linux) |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 958 | GLIBC25_MEMPCPY(VG_Z_LD_SO_1, mempcpy) /* ld.so.1 */ |
njn | b4cfbc4 | 2009-05-04 04:20:02 +0000 | [diff] [blame] | 959 | #endif |
sewardj | 841b72d | 2006-12-31 18:55:56 +0000 | [diff] [blame] | 960 | |
| 961 | |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 962 | #define GLIBC26___MEMCPY_CHK(soname, fnname) \ |
| 963 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 964 | (void* dst, const void* src, SizeT len, SizeT dstlen ); \ |
| 965 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 966 | (void* dst, const void* src, SizeT len, SizeT dstlen ) \ |
| 967 | { \ |
| 968 | extern void _exit(int status); \ |
| 969 | register char *d; \ |
| 970 | register char *s; \ |
| 971 | \ |
| 972 | if (dstlen < len) goto badness; \ |
| 973 | \ |
| 974 | if (len == 0) \ |
| 975 | return dst; \ |
| 976 | \ |
| 977 | if (is_overlap(dst, src, len, len)) \ |
| 978 | RECORD_OVERLAP_ERROR("memcpy_chk", dst, src, len); \ |
| 979 | \ |
| 980 | if ( dst > src ) { \ |
| 981 | d = (char *)dst + len - 1; \ |
| 982 | s = (char *)src + len - 1; \ |
| 983 | while ( len-- ) { \ |
| 984 | *d-- = *s--; \ |
| 985 | } \ |
| 986 | } else if ( dst < src ) { \ |
| 987 | d = (char *)dst; \ |
| 988 | s = (char *)src; \ |
| 989 | while ( len-- ) { \ |
| 990 | *d++ = *s++; \ |
| 991 | } \ |
| 992 | } \ |
| 993 | return dst; \ |
| 994 | badness: \ |
| 995 | VALGRIND_PRINTF_BACKTRACE( \ |
| 996 | "*** memcpy_chk: buffer overflow detected ***: " \ |
njn | d55f0d9 | 2009-08-03 01:38:56 +0000 | [diff] [blame] | 997 | "program terminated\n"); \ |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 998 | _exit(127); \ |
| 999 | /*NOTREACHED*/ \ |
| 1000 | return NULL; \ |
| 1001 | } |
| 1002 | |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1003 | GLIBC26___MEMCPY_CHK(VG_Z_LIBC_SONAME, __memcpy_chk) |
sewardj | b6c0403 | 2007-11-13 20:52:29 +0000 | [diff] [blame] | 1004 | |
| 1005 | |
sewardj | a77687c | 2010-08-19 13:22:34 +0000 | [diff] [blame] | 1006 | #define STRSTR(soname, fnname) \ |
| 1007 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 1008 | (void* haystack, void* needle); \ |
| 1009 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 1010 | (void* haystack, void* needle) \ |
| 1011 | { \ |
| 1012 | UChar* h = (UChar*)haystack; \ |
| 1013 | UChar* n = (UChar*)needle; \ |
| 1014 | \ |
| 1015 | /* find the length of n, not including terminating zero */ \ |
| 1016 | UWord nlen = 0; \ |
| 1017 | while (n[nlen]) nlen++; \ |
| 1018 | \ |
| 1019 | /* if n is the empty string, match immediately. */ \ |
| 1020 | if (nlen == 0) return h; \ |
| 1021 | \ |
| 1022 | /* assert(nlen >= 1); */ \ |
| 1023 | UChar n0 = n[0]; \ |
| 1024 | \ |
| 1025 | while (1) { \ |
| 1026 | UChar hh = *h; \ |
| 1027 | if (hh == 0) return NULL; \ |
| 1028 | if (hh != n0) { h++; continue; } \ |
| 1029 | \ |
| 1030 | UWord i; \ |
| 1031 | for (i = 0; i < nlen; i++) { \ |
| 1032 | if (n[i] != h[i]) \ |
| 1033 | break; \ |
| 1034 | } \ |
| 1035 | /* assert(i >= 0 && i <= nlen); */ \ |
| 1036 | if (i == nlen) \ |
| 1037 | return h; \ |
| 1038 | \ |
| 1039 | h++; \ |
| 1040 | } \ |
| 1041 | } |
| 1042 | |
| 1043 | #if defined(VGO_linux) |
| 1044 | STRSTR(VG_Z_LIBC_SONAME, strstr) |
| 1045 | #endif |
| 1046 | |
| 1047 | |
| 1048 | #define STRPBRK(soname, fnname) \ |
| 1049 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 1050 | (void* sV, void* acceptV); \ |
| 1051 | void* VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 1052 | (void* sV, void* acceptV) \ |
| 1053 | { \ |
| 1054 | UChar* s = (UChar*)sV; \ |
| 1055 | UChar* accept = (UChar*)acceptV; \ |
| 1056 | \ |
| 1057 | /* find the length of 'accept', not including terminating zero */ \ |
| 1058 | UWord nacc = 0; \ |
| 1059 | while (accept[nacc]) nacc++; \ |
| 1060 | \ |
| 1061 | /* if n is the empty string, fail immediately. */ \ |
| 1062 | if (nacc == 0) return NULL; \ |
| 1063 | \ |
| 1064 | /* assert(nacc >= 1); */ \ |
| 1065 | while (1) { \ |
| 1066 | UWord i; \ |
| 1067 | UChar sc = *s; \ |
| 1068 | if (sc == 0) \ |
| 1069 | break; \ |
| 1070 | for (i = 0; i < nacc; i++) { \ |
| 1071 | if (sc == accept[i]) \ |
| 1072 | return s; \ |
| 1073 | } \ |
| 1074 | s++; \ |
| 1075 | } \ |
| 1076 | \ |
| 1077 | return NULL; \ |
| 1078 | } |
| 1079 | |
| 1080 | #if defined(VGO_linux) |
| 1081 | STRPBRK(VG_Z_LIBC_SONAME, strpbrk) |
| 1082 | #endif |
| 1083 | |
| 1084 | |
| 1085 | #define STRCSPN(soname, fnname) \ |
| 1086 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 1087 | (void* sV, void* rejectV); \ |
| 1088 | SizeT VG_REPLACE_FUNCTION_ZU(soname,fnname) \ |
| 1089 | (void* sV, void* rejectV) \ |
| 1090 | { \ |
| 1091 | UChar* s = (UChar*)sV; \ |
| 1092 | UChar* reject = (UChar*)rejectV; \ |
| 1093 | \ |
| 1094 | /* find the length of 'reject', not including terminating zero */ \ |
| 1095 | UWord nrej = 0; \ |
| 1096 | while (reject[nrej]) nrej++; \ |
| 1097 | \ |
| 1098 | UWord len = 0; \ |
| 1099 | while (1) { \ |
| 1100 | UWord i; \ |
| 1101 | UChar sc = *s; \ |
| 1102 | if (sc == 0) \ |
| 1103 | break; \ |
| 1104 | for (i = 0; i < nrej; i++) { \ |
| 1105 | if (sc == reject[i]) \ |
| 1106 | break; \ |
| 1107 | } \ |
| 1108 | /* assert(i >= 0 && i <= nrej); */ \ |
| 1109 | if (i < nrej) \ |
| 1110 | break; \ |
| 1111 | s++; \ |
| 1112 | len++; \ |
| 1113 | } \ |
| 1114 | \ |
| 1115 | return len; \ |
| 1116 | } |
| 1117 | |
| 1118 | #if defined(VGO_linux) |
| 1119 | STRCSPN(VG_Z_LIBC_SONAME, strcspn) |
| 1120 | #endif |
| 1121 | |
| 1122 | |
sewardj | ba18935 | 2010-08-20 18:24:16 +0000 | [diff] [blame] | 1123 | // And here's a validated strspn replacement, should it |
| 1124 | // become necessary. |
| 1125 | //UWord mystrspn( UChar* s, UChar* accept ) |
| 1126 | //{ |
| 1127 | // /* find the length of 'accept', not including terminating zero */ |
| 1128 | // UWord nacc = 0; |
| 1129 | // while (accept[nacc]) nacc++; |
| 1130 | // if (nacc == 0) return 0; |
| 1131 | // |
| 1132 | // UWord len = 0; |
| 1133 | // while (1) { |
| 1134 | // UWord i; |
| 1135 | // UChar sc = *s; |
| 1136 | // if (sc == 0) |
| 1137 | // break; |
| 1138 | // for (i = 0; i < nacc; i++) { |
| 1139 | // if (sc == accept[i]) |
| 1140 | // break; |
| 1141 | // } |
| 1142 | // assert(i >= 0 && i <= nacc); |
| 1143 | // if (i == nacc) |
| 1144 | // break; |
| 1145 | // s++; |
| 1146 | // len++; |
| 1147 | // } |
| 1148 | // |
| 1149 | // return len; |
| 1150 | //} |
| 1151 | |
| 1152 | |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1153 | /*------------------------------------------------------------*/ |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1154 | /*--- Improve definedness checking of process environment ---*/ |
| 1155 | /*------------------------------------------------------------*/ |
| 1156 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1157 | #if defined(VGO_linux) |
| 1158 | |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1159 | /* putenv */ |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1160 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string); |
| 1161 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, putenv) (char* string) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1162 | { |
| 1163 | OrigFn fn; |
| 1164 | Word result; |
| 1165 | const char* p = string; |
| 1166 | VALGRIND_GET_ORIG_FN(fn); |
| 1167 | /* Now by walking over the string we magically produce |
| 1168 | traces when hitting undefined memory. */ |
| 1169 | if (p) |
| 1170 | while (*p++) |
| 1171 | ; |
| 1172 | CALL_FN_W_W(result, fn, string); |
| 1173 | return result; |
| 1174 | } |
| 1175 | |
| 1176 | /* unsetenv */ |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1177 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name); |
| 1178 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, unsetenv) (const char* name) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1179 | { |
| 1180 | OrigFn fn; |
| 1181 | Word result; |
| 1182 | const char* p = name; |
| 1183 | VALGRIND_GET_ORIG_FN(fn); |
| 1184 | /* Now by walking over the string we magically produce |
| 1185 | traces when hitting undefined memory. */ |
| 1186 | if (p) |
| 1187 | while (*p++) |
| 1188 | ; |
| 1189 | CALL_FN_W_W(result, fn, name); |
| 1190 | return result; |
| 1191 | } |
| 1192 | |
| 1193 | /* setenv */ |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1194 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1195 | (const char* name, const char* value, int overwrite); |
njn | e615466 | 2009-02-10 04:23:41 +0000 | [diff] [blame] | 1196 | int VG_WRAP_FUNCTION_ZU(VG_Z_LIBC_SONAME, setenv) |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1197 | (const char* name, const char* value, int overwrite) |
| 1198 | { |
| 1199 | OrigFn fn; |
| 1200 | Word result; |
| 1201 | const char* p; |
| 1202 | VALGRIND_GET_ORIG_FN(fn); |
| 1203 | /* Now by walking over the string we magically produce |
| 1204 | traces when hitting undefined memory. */ |
| 1205 | if (name) |
| 1206 | for (p = name; *p; p++) |
| 1207 | ; |
| 1208 | if (value) |
| 1209 | for (p = value; *p; p++) |
| 1210 | ; |
| 1211 | VALGRIND_CHECK_VALUE_IS_DEFINED (overwrite); |
| 1212 | CALL_FN_W_WWW(result, fn, name, value, overwrite); |
| 1213 | return result; |
| 1214 | } |
| 1215 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1216 | #endif /* defined(VGO_linux) */ |
| 1217 | |
| 1218 | |
dirk | 09beb9e | 2007-04-19 09:47:32 +0000 | [diff] [blame] | 1219 | /*------------------------------------------------------------*/ |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1220 | /*--- AIX stuff only after this point ---*/ |
| 1221 | /*------------------------------------------------------------*/ |
| 1222 | |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1223 | /* Generate replacements for strcat, strncat, strcpy, strncpy, strcmp |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1224 | in the given soname. */ |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1225 | #define Str5FNs(_soname) \ |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1226 | STRCAT(_soname, strcat) \ |
| 1227 | STRNCAT(_soname, strncat) \ |
| 1228 | STRCPY(_soname, strcpy) \ |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1229 | STRNCPY(_soname, strncpy) \ |
| 1230 | STRCMP(_soname, strcmp) |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1231 | |
| 1232 | #if defined(VGP_ppc32_aix5) |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1233 | Str5FNs(NONE) /* in main exe */ |
| 1234 | Str5FNs(libCZdaZLshrcoreZdoZR) /* libC.a(shrcore.o) */ |
| 1235 | Str5FNs(libX11ZdaZLshr4ZdoZR) /* libX11.a(shr4.o) */ |
| 1236 | Str5FNs(libXmZdaZLshrZaZdoZR) /* libXm.a(shr*.o) */ |
| 1237 | Str5FNs(libXtZdaZLshr4ZdoZR) /* libXt.a(shr4.o) */ |
| 1238 | Str5FNs(libppeZurZdaZLdynamicZdoZR) /* libppe_r.a(dynamic.o) */ |
| 1239 | Str5FNs(libodmZdaZLshrZdoZR) /* libodm.a(shr.o) */ |
| 1240 | Str5FNs(libmpiZurZdaZLmpicoreZurZdoZR) /* libmpi_r.a(mpicore_r.o) */ |
| 1241 | Str5FNs(libmpiZurZdaZLmpipoeZurZdoZR) /* libmpi_r.a(mpipoe_r.o) */ |
| 1242 | Str5FNs(libmpiZurZdaZLmpciZurZdoZR) /* libmpi_r.a(mpci_r.o) */ |
| 1243 | Str5FNs(libslurmZdso) /* libslurm.so */ |
| 1244 | Str5FNs(libglibZdso) /* libglib.so */ |
| 1245 | Str5FNs(libIMZdaZLshrZdoZR) /* libIM.a(shr.o) */ |
| 1246 | Str5FNs(libiconvZdaZLshr4ZdoZR) /* libiconv.a(shr4.o) */ |
| 1247 | Str5FNs(libGLZdaZLshrZdoZR) /* libGL.a(shr.o) */ |
| 1248 | Str5FNs(libgdkZdso) /* libgdk.so */ |
| 1249 | Str5FNs(libcursesZdaZLshr42ZdoZR) /* libcurses.a(shr42.o) */ |
| 1250 | Str5FNs(libqtZda) /* libqt.a */ |
sewardj | fd4b6f4 | 2007-11-29 03:08:32 +0000 | [diff] [blame] | 1251 | Str5FNs(ZaZLlibglibZhZaZdsoZaZR) /* *(libglib-*.so*) */ |
| 1252 | Str5FNs(ZaZLlibfontconfigZdsoZaZR) /* *(libfontconfig.so*) */ |
| 1253 | Str5FNs(libQtZaa) /* libQt*.a */ |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1254 | #endif |
| 1255 | #if defined(VGP_ppc64_aix5) |
sewardj | ddc00dd | 2007-11-27 11:42:47 +0000 | [diff] [blame] | 1256 | Str5FNs(NONE) /* in main exe */ |
| 1257 | Str5FNs(libX11ZdaZLshrZu64ZdoZR) /* libX11.a(shr_64.o) */ |
| 1258 | Str5FNs(libiconvZdaZLshr4Zu64ZdoZR) /* libiconv.a(shr4_64.o) */ |
| 1259 | Str5FNs(libGLZdaZLshrZu64ZdoZR) /* libGL.a(shr_64.o) */ |
| 1260 | Str5FNs(libppeZurZdaZLdynamic64ZdoZR) /* libppe_r.a(dynamic64.o) */ |
| 1261 | Str5FNs(libodmZdaZLshrZu64ZdoZR) /* libodm.a(shr_64.o) */ |
| 1262 | Str5FNs(libmpiZurZdaZLmpicore64ZurZdoZR) /* libmpi_r.a(mpicore64_r.o) */ |
| 1263 | Str5FNs(libmpiZurZdaZLmpipoe64ZurZdoZR) /* libmpi_r.a(mpipoe64_r.o) */ |
| 1264 | Str5FNs(libCZdaZLshrcoreZu64ZdoZR) /* libC.a(shrcore_64.o) */ |
| 1265 | Str5FNs(libmpiZurZdaZLmpci64ZurZdoZR) /* libmpi_r.a(mpci64_r.o) */ |
| 1266 | Str5FNs(libqtZda) /* libqt.a */ |
sewardj | fd4b6f4 | 2007-11-29 03:08:32 +0000 | [diff] [blame] | 1267 | Str5FNs(ZaZLlibglibZhZaZdsoZaZR) /* *(libglib-*.so*) */ |
| 1268 | Str5FNs(ZaZLlibfontconfigZdsoZaZR) /* *(libfontconfig.so*) */ |
| 1269 | Str5FNs(libQtZaa) /* libQt*.a */ |
sewardj | 31b9ce1 | 2006-10-17 01:27:13 +0000 | [diff] [blame] | 1270 | #endif |
| 1271 | |
| 1272 | |
| 1273 | /* AIX's libm contains a sqrt implementation which does a nasty thing: |
| 1274 | it loads the initial estimate of the root into a FP register, but |
| 1275 | only the upper half of the number is initialised data. Hence the |
| 1276 | least significant 32 mantissa bits are undefined, and it then uses |
| 1277 | Newton-Raphson iteration to compute the final, defined result. |
| 1278 | This fools memcheck completely; the only solution I can think of is |
| 1279 | provide our own substitute. The _FAST variant is almost right |
| 1280 | except the result is not correctly rounded. The _EXACT variant, |
| 1281 | which is selected by default, is always right; but it's also pretty |
| 1282 | darn slow. */ |
| 1283 | |
| 1284 | #if defined(VGP_ppc32_aix5) || defined(VGP_ppc64_aix5) |
| 1285 | #define SQRT_FAST(soname, fnname) \ |
| 1286 | double VG_REPLACE_FUNCTION_ZU(soname,fnname)( double x ); \ |
| 1287 | double VG_REPLACE_FUNCTION_ZU(soname,fnname)( double x ) \ |
| 1288 | { \ |
| 1289 | static UInt T1[32] = \ |
| 1290 | { 0, 1024, 3062, 5746, 9193, 13348, \ |
| 1291 | 18162, 23592, 29598, 36145, 43202, 50740, \ |
| 1292 | 58733, 67158, 75992, 85215, 83599, 71378, \ |
| 1293 | 60428, 50647, 41945, 34246, 27478, 21581, \ |
| 1294 | 16499, 12183, 8588, 5674, 3403, 1742, \ |
| 1295 | 661, 130 }; \ |
| 1296 | UInt x0, x1, sign, expo, mant0, bIGENDIAN = 1; \ |
| 1297 | union { UInt w[2]; double d; } u; \ |
| 1298 | u.d = x; \ |
| 1299 | x0 = u.w[1 - bIGENDIAN]; /* high half */ \ |
| 1300 | x1 = u.w[bIGENDIAN]; /* low half */ \ |
| 1301 | sign = x0 >> 31; \ |
| 1302 | expo = (x0 >> 20) & 0x7FF; \ |
| 1303 | mant0 = x0 & 0xFFFFF; \ |
| 1304 | if ( (sign == 0 && expo >= 1 && expo <= 0x7FE) /* +normal */ \ |
| 1305 | || (sign == 0 && expo == 0 \ |
| 1306 | && (mant0 | x1) > 0) /* +denorm */) { \ |
| 1307 | /* common case; do Newton-Raphson */ \ |
| 1308 | /* technically k should be signed int32, but since we're \ |
| 1309 | always entering here with x > 0, doesn't matter that it's \ |
| 1310 | unsigned. */ \ |
| 1311 | double y; \ |
| 1312 | UInt k = (x0>>1) + 0x1ff80000; \ |
| 1313 | u.w[1 - bIGENDIAN] = k - T1[31&(k>>15)]; \ |
| 1314 | u.w[bIGENDIAN] = 0; \ |
| 1315 | y = u.d; \ |
| 1316 | y = (y+x/y)/2.0 ; \ |
| 1317 | y = (y+x/y)/2.0 ; \ |
| 1318 | y = y-(y-x/y)/2.0 ; \ |
| 1319 | return y; \ |
| 1320 | } \ |
| 1321 | if ( (sign == 1 && expo >= 1 && expo <= 0x7FE) /* -normal */ \ |
| 1322 | || (sign == 1 && expo == 0 \ |
| 1323 | && (mant0 | x1) > 0) /* -denorm */) { \ |
| 1324 | u.w[1 - bIGENDIAN] = 0xFFF00000; \ |
| 1325 | u.w[bIGENDIAN] = 0x1; \ |
| 1326 | return u.d; /* -Inf -> NaN */ \ |
| 1327 | } \ |
| 1328 | if ((expo | mant0 | x1) == 0) \ |
| 1329 | return x; /* +/-zero -> self */ \ |
| 1330 | if (expo == 0x7FF && (mant0 | x1) == 0) { \ |
| 1331 | if (sign == 0) \ |
| 1332 | return x; /* +Inf -> self */ \ |
| 1333 | u.w[1 - bIGENDIAN] = 0xFFF00000; \ |
| 1334 | u.w[bIGENDIAN] = 0x1; \ |
| 1335 | return u.d; /* -Inf -> NaN */ \ |
| 1336 | } \ |
| 1337 | /* must be +/- NaN */ \ |
| 1338 | return x; /* +/-NaN -> self */ \ |
| 1339 | } |
| 1340 | |
| 1341 | #define SQRT_EXACT(soname, fnname) \ |
| 1342 | /* \ |
| 1343 | * ==================================================== \ |
| 1344 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. \ |
| 1345 | * \ |
| 1346 | * Developed at SunPro, a Sun Microsystems, Inc. business. \ |
| 1347 | * Permission to use, copy, modify, and distribute this \ |
| 1348 | * software is freely granted, provided that this notice \ |
| 1349 | * is preserved. \ |
| 1350 | * ==================================================== \ |
| 1351 | */ \ |
| 1352 | /* \ |
| 1353 | * Return correctly rounded sqrt. \ |
| 1354 | * ------------------------------------------ \ |
| 1355 | * | Use the hardware sqrt if you have one | \ |
| 1356 | * ------------------------------------------ \ |
| 1357 | * Method: \ |
| 1358 | * Bit by bit method using integer arithmetic. (Slow, but portable) \ |
| 1359 | * 1. Normalization \ |
| 1360 | * Scale x to y in [1,4) with even powers of 2: \ |
| 1361 | * find an integer k such that 1 <= (y=x*2^(2k)) < 4, then \ |
| 1362 | * sqrt(x) = 2^k * sqrt(y) \ |
| 1363 | * 2. Bit by bit computation \ |
| 1364 | * Let q = sqrt(y) truncated to i bit after binary point (q = 1), \ |
| 1365 | * i 0 \ |
| 1366 | * i+1 2 \ |
| 1367 | * s = 2*q , and y = 2 * ( y - q ). (1) \ |
| 1368 | * i i i i \ |
| 1369 | * \ |
| 1370 | * To compute q from q , one checks whether \ |
| 1371 | * i+1 i \ |
| 1372 | * \ |
| 1373 | * -(i+1) 2 \ |
| 1374 | * (q + 2 ) <= y. (2) \ |
| 1375 | * i \ |
| 1376 | * -(i+1) \ |
| 1377 | * If (2) is false, then q = q ; otherwise q = q + 2 . \ |
| 1378 | * i+1 i i+1 i \ |
| 1379 | * \ |
| 1380 | * With some algebric manipulation, it is not difficult to see \ |
| 1381 | * that (2) is equivalent to \ |
| 1382 | * -(i+1) \ |
| 1383 | * s + 2 <= y (3) \ |
| 1384 | * i i \ |
| 1385 | * \ |
| 1386 | * The advantage of (3) is that s and y can be computed by \ |
| 1387 | * i i \ |
| 1388 | * the following recurrence formula: \ |
| 1389 | * if (3) is false \ |
| 1390 | * \ |
| 1391 | * s = s , y = y ; (4) \ |
| 1392 | * i+1 i i+1 i \ |
| 1393 | * \ |
| 1394 | * otherwise, \ |
| 1395 | * -i -(i+1) \ |
| 1396 | * s = s + 2 , y = y - s - 2 (5) \ |
| 1397 | * i+1 i i+1 i i \ |
| 1398 | * \ |
| 1399 | * \ |
| 1400 | * One may easily use induction to prove (4) and (5). \ |
| 1401 | * Note. Since the left hand side of (3) contain only i+2 bits, \ |
| 1402 | * it does not necessary to do a full (53-bit) comparison \ |
| 1403 | * in (3). \ |
| 1404 | * 3. Final rounding \ |
| 1405 | * After generating the 53 bits result, we compute one more bit. \ |
| 1406 | * Together with the remainder, we can decide whether the \ |
| 1407 | * result is exact, bigger than 1/2ulp, or less than 1/2ulp \ |
| 1408 | * (it will never equal to 1/2ulp). \ |
| 1409 | * The rounding mode can be detected by checking whether \ |
| 1410 | * huge + tiny is equal to huge, and whether huge - tiny is \ |
| 1411 | * equal to huge for some floating point number "huge" and "tiny". \ |
| 1412 | * \ |
| 1413 | * Special cases: \ |
| 1414 | * sqrt(+-0) = +-0 ... exact \ |
| 1415 | * sqrt(inf) = inf \ |
| 1416 | * sqrt(-ve) = NaN ... with invalid signal \ |
| 1417 | * sqrt(NaN) = NaN ... with invalid signal for signaling NaN \ |
| 1418 | * \ |
| 1419 | */ \ |
| 1420 | double VG_REPLACE_FUNCTION_ZU(soname,fnname)( double x ); \ |
| 1421 | double VG_REPLACE_FUNCTION_ZU(soname,fnname)( double x ) \ |
| 1422 | { \ |
| 1423 | const Int bIGENDIAN = 1; \ |
| 1424 | const double one = 1.0, tiny=1.0e-300; \ |
| 1425 | double z; \ |
| 1426 | Int sign = (Int)0x80000000; \ |
| 1427 | Int ix0,s0,q,m,t,i; \ |
| 1428 | UInt r,t1,s1,ix1,q1; \ |
| 1429 | union { UInt w[2]; double d; } u; \ |
| 1430 | u.d = x; \ |
| 1431 | ix0 = u.w[1-bIGENDIAN]; \ |
| 1432 | ix1 = u.w[bIGENDIAN]; \ |
| 1433 | \ |
| 1434 | /* take care of Inf and NaN */ \ |
| 1435 | if((ix0&0x7ff00000)==0x7ff00000) { \ |
| 1436 | return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf \ |
| 1437 | sqrt(-inf)=sNaN */ \ |
| 1438 | } \ |
| 1439 | /* take care of zero */ \ |
| 1440 | if(ix0<=0) { \ |
| 1441 | if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */ \ |
| 1442 | else if(ix0<0) \ |
| 1443 | return (x-x)/(x-x); /* sqrt(-ve) = sNaN */ \ |
| 1444 | } \ |
| 1445 | /* normalize x */ \ |
| 1446 | m = (ix0>>20); \ |
| 1447 | if(m==0) { /* subnormal x */ \ |
| 1448 | while(ix0==0) { \ |
| 1449 | m -= 21; \ |
| 1450 | ix0 |= (ix1>>11); ix1 <<= 21; \ |
| 1451 | } \ |
| 1452 | for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1; \ |
| 1453 | m -= i-1; \ |
| 1454 | ix0 |= (ix1>>(32-i)); \ |
| 1455 | ix1 <<= i; \ |
| 1456 | } \ |
| 1457 | m -= 1023; /* unbias exponent */ \ |
| 1458 | ix0 = (ix0&0x000fffff)|0x00100000; \ |
| 1459 | if(m&1){ /* odd m, double x to make it even */ \ |
| 1460 | ix0 += ix0 + ((ix1&sign)>>31); \ |
| 1461 | ix1 += ix1; \ |
| 1462 | } \ |
| 1463 | m >>= 1; /* m = [m/2] */ \ |
| 1464 | /* generate sqrt(x) bit by bit */ \ |
| 1465 | ix0 += ix0 + ((ix1&sign)>>31); \ |
| 1466 | ix1 += ix1; \ |
| 1467 | q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ \ |
| 1468 | r = 0x00200000; /* r = moving bit from right to left */ \ |
| 1469 | while(r!=0) { \ |
| 1470 | t = s0+r; \ |
| 1471 | if(t<=ix0) { \ |
| 1472 | s0 = t+r; \ |
| 1473 | ix0 -= t; \ |
| 1474 | q += r; \ |
| 1475 | } \ |
| 1476 | ix0 += ix0 + ((ix1&sign)>>31); \ |
| 1477 | ix1 += ix1; \ |
| 1478 | r>>=1; \ |
| 1479 | } \ |
| 1480 | r = sign; \ |
| 1481 | while(r!=0) { \ |
| 1482 | t1 = s1+r; \ |
| 1483 | t = s0; \ |
| 1484 | if((t<ix0)||((t==ix0)&&(t1<=ix1))) { \ |
| 1485 | s1 = t1+r; \ |
| 1486 | if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1; \ |
| 1487 | ix0 -= t; \ |
| 1488 | if (ix1 < t1) ix0 -= 1; \ |
| 1489 | ix1 -= t1; \ |
| 1490 | q1 += r; \ |
| 1491 | } \ |
| 1492 | ix0 += ix0 + ((ix1&sign)>>31); \ |
| 1493 | ix1 += ix1; \ |
| 1494 | r>>=1; \ |
| 1495 | } \ |
| 1496 | /* use floating add to find out rounding direction */ \ |
| 1497 | if((ix0|ix1)!=0) { \ |
| 1498 | z = one-tiny; /* trigger inexact flag */ \ |
| 1499 | if (z>=one) { \ |
| 1500 | z = one+tiny; \ |
| 1501 | if (q1==(UInt)0xffffffff) { q1=0; q += 1;} \ |
| 1502 | else if (z>one) { \ |
| 1503 | if (q1==(UInt)0xfffffffe) q+=1; \ |
| 1504 | q1+=2; \ |
| 1505 | } else \ |
| 1506 | q1 += (q1&1); \ |
| 1507 | } \ |
| 1508 | } \ |
| 1509 | ix0 = (q>>1)+0x3fe00000; \ |
| 1510 | ix1 = q1>>1; \ |
| 1511 | if ((q&1)==1) ix1 |= sign; \ |
| 1512 | ix0 += (m <<20); \ |
| 1513 | ix0 = u.w[1-bIGENDIAN] = ix0; \ |
| 1514 | ix1 = u.w[bIGENDIAN] = ix1; \ |
| 1515 | z = u.d; \ |
| 1516 | return z; \ |
| 1517 | } |
| 1518 | |
| 1519 | #if 0 |
| 1520 | SQRT_FAST(NONE, sqrt) /* xlC generates these */ |
| 1521 | SQRT_FAST(NONE, _sqrt) /* xlf generates these */ |
| 1522 | #else |
| 1523 | SQRT_EXACT(NONE, sqrt) /* xlC generates these */ |
| 1524 | SQRT_EXACT(NONE, _sqrt) /* xlf generates these */ |
| 1525 | #endif |
| 1526 | |
| 1527 | #endif /* defined(VGP_ppc32_aix5) */ |
| 1528 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1529 | /*--------------------------------------------------------------------*/ |
njn | 4627586 | 2005-03-24 04:00:03 +0000 | [diff] [blame] | 1530 | /*--- end ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1531 | /*--------------------------------------------------------------------*/ |