njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 1 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 3 | /*--- Client-space code for the core. vg_preloaded.c ---*/ |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | c0ae705 | 2005-08-25 22:55:19 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 9 | |
sewardj | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2013 Julian Seward |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | /* --------------------------------------------------------------------- |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 33 | ALL THE CODE IN THIS FILE RUNS ON THE SIMULATED CPU. |
| 34 | |
| 35 | These functions are not called directly - they're the targets of code |
| 36 | redirection or load notifications (see pub_core_redir.h for info). |
| 37 | They're named weirdly so that the intercept code can find them when the |
| 38 | shared object is initially loaded. |
| 39 | |
| 40 | Note that this filename has the "vg_" prefix because it can appear |
| 41 | in stack traces, and the "vg_" makes it a little clearer that it |
| 42 | originates from Valgrind. |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 43 | ------------------------------------------------------------------ */ |
| 44 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 45 | #include "pub_core_basics.h" |
njn | 93fe3b2 | 2005-12-21 20:22:52 +0000 | [diff] [blame] | 46 | #include "pub_core_clreq.h" |
njn | 24a6efb | 2005-06-20 03:36:51 +0000 | [diff] [blame] | 47 | #include "pub_core_debuginfo.h" // Needed for pub_core_redir.h |
| 48 | #include "pub_core_redir.h" // For VG_NOTIFY_ON_LOAD |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 49 | |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 50 | #if defined(VGO_linux) |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 51 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 52 | /* --------------------------------------------------------------------- |
| 53 | Hook for running __libc_freeres once the program exits. |
| 54 | ------------------------------------------------------------------ */ |
| 55 | |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 56 | void VG_NOTIFY_ON_LOAD(freeres)( void ); |
| 57 | void VG_NOTIFY_ON_LOAD(freeres)( void ) |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 58 | { |
philippe | 5d5dd8e | 2012-08-05 00:08:25 +0000 | [diff] [blame] | 59 | # if !defined(__UCLIBC__) \ |
dejanj | 9c6b05d | 2013-12-27 09:06:55 +0000 | [diff] [blame] | 60 | && !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android) \ |
| 61 | && !defined(VGPV_mips32_linux_android) |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 62 | extern void __libc_freeres(void); |
| 63 | __libc_freeres(); |
sewardj | 126e82d | 2011-07-12 13:33:00 +0000 | [diff] [blame] | 64 | # endif |
sewardj | 4b3a742 | 2011-10-24 13:21:57 +0000 | [diff] [blame] | 65 | VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__LIBC_FREERES_DONE, |
bart | 575ce8e | 2011-05-15 07:04:03 +0000 | [diff] [blame] | 66 | 0, 0, 0, 0, 0); |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 67 | /*NOTREACHED*/ |
sewardj | a6f76ee | 2010-10-11 19:15:33 +0000 | [diff] [blame] | 68 | *(volatile int *)0 = 'x'; |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 69 | } |
| 70 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 71 | /* --------------------------------------------------------------------- |
| 72 | Wrapper for indirect functions which need to be redirected. |
| 73 | ------------------------------------------------------------------ */ |
| 74 | |
| 75 | void * VG_NOTIFY_ON_LOAD(ifunc_wrapper) (void); |
| 76 | void * VG_NOTIFY_ON_LOAD(ifunc_wrapper) (void) |
| 77 | { |
| 78 | OrigFn fn; |
| 79 | Addr result = 0; |
mjw | a683826 | 2014-07-15 15:07:01 +0000 | [diff] [blame] | 80 | Addr fnentry; |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 81 | |
| 82 | /* Call the original indirect function and get it's result */ |
| 83 | VALGRIND_GET_ORIG_FN(fn); |
| 84 | CALL_FN_W_v(result, fn); |
| 85 | |
mjw | a683826 | 2014-07-15 15:07:01 +0000 | [diff] [blame] | 86 | #if defined(VGP_ppc64_linux) |
| 87 | /* ppc64 uses function descriptors, so get the actual function entry |
| 88 | address for the client request, but return the function descriptor |
| 89 | from this function. */ |
mjw | fa7b329 | 2014-07-17 10:56:26 +0000 | [diff] [blame^] | 90 | UWord *descr = (UWord*)(void*)result; |
| 91 | fnentry = (Addr)(void*)(descr[0]); |
mjw | a683826 | 2014-07-15 15:07:01 +0000 | [diff] [blame] | 92 | #else |
| 93 | fnentry = result; |
| 94 | #endif |
| 95 | |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 96 | /* Ask the valgrind core running on the real CPU (as opposed to this |
| 97 | code which runs on the emulated CPU) to update the redirection that |
| 98 | led to this function. This client request eventually gives control to |
| 99 | the function VG_(redir_add_ifunc_target) in m_redir.c */ |
sewardj | 4b3a742 | 2011-10-24 13:21:57 +0000 | [diff] [blame] | 100 | VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__ADD_IFUNC_TARGET, |
mjw | a683826 | 2014-07-15 15:07:01 +0000 | [diff] [blame] | 101 | fn.nraddr, fnentry, 0, 0, 0); |
bart | 431ad28 | 2009-11-10 15:11:30 +0000 | [diff] [blame] | 102 | return (void*)result; |
tom | d264514 | 2009-10-29 09:27:11 +0000 | [diff] [blame] | 103 | } |
| 104 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 105 | #elif defined(VGO_darwin) |
| 106 | |
bart | 62e0f3c | 2009-12-29 17:30:16 +0000 | [diff] [blame] | 107 | #include "config.h" /* VERSION */ |
| 108 | |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 109 | /* --------------------------------------------------------------------- |
| 110 | Darwin crash log hints |
| 111 | ------------------------------------------------------------------ */ |
| 112 | |
| 113 | /* This string will be inserted into crash logs, so crashes while |
| 114 | running under Valgrind can be distinguished from other crashes. */ |
florian | e07cbb3 | 2013-01-15 03:19:54 +0000 | [diff] [blame] | 115 | __private_extern__ const char *__crashreporter_info__ = "Instrumented by Valgrind " VERSION; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 116 | |
| 117 | /* --------------------------------------------------------------------- |
| 118 | Darwin environment cleanup |
| 119 | ------------------------------------------------------------------ */ |
| 120 | |
| 121 | /* Scrubbing DYLD_INSERT_LIBRARIES from envp during exec is insufficient, |
| 122 | as there are other ways to launch a process with environment that |
| 123 | valgrind can't catch easily (i.e. launchd). |
| 124 | Instead, scrub DYLD_INSERT_LIBRARIES from the parent process once |
| 125 | dyld is done loading vg_preload.so. |
| 126 | */ |
| 127 | #include <string.h> |
| 128 | #include <crt_externs.h> |
| 129 | |
| 130 | // GrP fixme copied from m_libcproc |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 131 | static void env_unsetenv ( HChar **env, const HChar *varname ) |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 132 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 133 | HChar **from; |
| 134 | HChar **to = NULL; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 135 | Int len = strlen(varname); |
| 136 | |
| 137 | for (from = to = env; from && *from; from++) { |
| 138 | if (!(strncmp(varname, *from, len) == 0 && (*from)[len] == '=')) { |
| 139 | *to = *from; |
| 140 | to++; |
| 141 | } |
| 142 | } |
| 143 | *(to++) = *(from++); |
| 144 | /* fix the 4th "char* apple" pointer (aka. executable path pointer) */ |
| 145 | *(to++) = *(from++); |
| 146 | *to = NULL; |
| 147 | } |
| 148 | |
| 149 | static void vg_cleanup_env(void) __attribute__((constructor)); |
| 150 | static void vg_cleanup_env(void) |
| 151 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 152 | HChar **envp = (HChar**)*_NSGetEnviron(); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 153 | env_unsetenv(envp, "VALGRIND_LAUNCHER"); |
| 154 | env_unsetenv(envp, "DYLD_SHARED_REGION"); |
| 155 | // GrP fixme should be more like mash_colon_env() |
| 156 | env_unsetenv(envp, "DYLD_INSERT_LIBRARIES"); |
| 157 | } |
| 158 | |
| 159 | /* --------------------------------------------------------------------- |
| 160 | Darwin arc4random (rdar://6166275) |
| 161 | ------------------------------------------------------------------ */ |
| 162 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 163 | #include <fcntl.h> |
| 164 | #include <unistd.h> |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 165 | |
| 166 | int VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random)(void); |
| 167 | int VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random)(void) |
| 168 | { |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 169 | static int rnd = -1; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 170 | int result; |
| 171 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 172 | if (rnd < 0) rnd = open("/dev/random", O_RDONLY); |
| 173 | |
| 174 | read(rnd, &result, sizeof(result)); |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 175 | return result; |
| 176 | } |
| 177 | |
| 178 | void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_stir)(void); |
| 179 | void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_stir)(void) |
| 180 | { |
| 181 | // do nothing |
| 182 | } |
| 183 | |
| 184 | void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_addrandom)(unsigned char *dat, int datlen); |
| 185 | void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_addrandom)(unsigned char *dat, int datlen) |
| 186 | { |
| 187 | // do nothing |
| 188 | // GrP fixme ought to check [dat..dat+datlen) is defined |
| 189 | // but don't care if it's initialized |
| 190 | } |
| 191 | |
| 192 | #else |
| 193 | |
| 194 | # error Unknown OS |
| 195 | #endif |
sewardj | eb3603e | 2006-01-24 01:01:17 +0000 | [diff] [blame] | 196 | |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 197 | /*--------------------------------------------------------------------*/ |
njn | 16eeb4e | 2005-06-16 03:56:58 +0000 | [diff] [blame] | 198 | /*--- end ---*/ |
sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 199 | /*--------------------------------------------------------------------*/ |