2 /*--------------------------------------------------------------------*/
3 /*--- Client-space code for the core. vg_preloaded.c ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2000-2017 Julian Seward
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.
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.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, see <http://www.gnu.org/licenses/>.
26 The GNU General Public License is contained in the file COPYING.
30 /* ---------------------------------------------------------------------
31 ALL THE CODE IN THIS FILE RUNS ON THE SIMULATED CPU.
33 These functions are not called directly - they're the targets of code
34 redirection or load notifications (see pub_core_redir.h for info).
35 They're named weirdly so that the intercept code can find them when the
36 shared object is initially loaded.
38 Note that this filename has the "vg_" prefix because it can appear
39 in stack traces, and the "vg_" makes it a little clearer that it
40 originates from Valgrind.
41 ------------------------------------------------------------------ */
43 #include "pub_core_basics.h"
44 #include "pub_core_clreq.h"
45 #include "pub_core_debuginfo.h" // Needed for pub_core_redir.h
46 #include "pub_core_redir.h" // For VG_NOTIFY_ON_LOAD
48 #if defined(VGO_linux) || defined(VGO_solaris)
50 /* ---------------------------------------------------------------------
51 Hook for running __gnu_cxx::__freeres() and __libc_freeres() once
53 ------------------------------------------------------------------ */
55 void VG_NOTIFY_ON_LOAD(freeres
)(Vg_FreeresToRun to_run
);
56 void VG_NOTIFY_ON_LOAD(freeres
)(Vg_FreeresToRun to_run
)
58 # if !defined(__UCLIBC__) && !defined(MUSL_LIBC) \
59 && !defined(VGPV_arm_linux_android) \
60 && !defined(VGPV_x86_linux_android) \
61 && !defined(VGPV_mips32_linux_android) \
62 && !defined(VGPV_arm64_linux_android)
64 /* g++ mangled __gnu_cxx::__freeres yields -> _ZN9__gnu_cxx9__freeresEv */
65 extern void _ZN9__gnu_cxx9__freeresEv(void) __attribute__((weak
));
66 if (((to_run
& VG_RUN__GNU_CXX__FREERES
) != 0) &&
67 (_ZN9__gnu_cxx9__freeresEv
!= NULL
)) {
68 _ZN9__gnu_cxx9__freeresEv();
71 extern void __libc_freeres(void) __attribute__((weak
));
72 if (((to_run
& VG_RUN__LIBC_FREERES
) != 0) &&
73 (__libc_freeres
!= NULL
)) {
79 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__FREERES_DONE
, 0, 0, 0, 0, 0);
81 *(volatile int *)0 = 'x';
84 #endif // VGO_linux || VGO_solaris
86 #if defined(VGO_linux)
88 /* ---------------------------------------------------------------------
89 Wrapper for indirect functions which need to be redirected.
90 ------------------------------------------------------------------ */
92 void * VG_NOTIFY_ON_LOAD(ifunc_wrapper
) (void);
93 void * VG_NOTIFY_ON_LOAD(ifunc_wrapper
) (void)
99 /* Call the original indirect function and get it's result */
100 VALGRIND_GET_ORIG_FN(fn
);
101 CALL_FN_W_v(result
, fn
);
103 #if defined(VGP_ppc64be_linux)
104 /* ppc64be uses function descriptors, so get the actual function entry
105 address for the client request, but return the function descriptor
107 result points to the function descriptor, which starts with the
109 fnentry
= *(Addr
*)result
;
114 /* Ask the valgrind core running on the real CPU (as opposed to this
115 code which runs on the emulated CPU) to update the redirection that
116 led to this function. This client request eventually gives control to
117 the function VG_(redir_add_ifunc_target) in m_redir.c */
118 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__ADD_IFUNC_TARGET
,
119 fn
.nraddr
, fnentry
, 0, 0, 0);
120 return (void*)result
;
123 #elif defined(VGO_darwin)
125 #include "config.h" /* VERSION */
127 /* ---------------------------------------------------------------------
128 Darwin crash log hints
129 ------------------------------------------------------------------ */
131 /* This string will be inserted into crash logs, so crashes while
132 running under Valgrind can be distinguished from other crashes. */
133 __private_extern__
const char *__crashreporter_info__
= "Instrumented by Valgrind " VERSION
;
135 /* ---------------------------------------------------------------------
136 Darwin environment cleanup
137 ------------------------------------------------------------------ */
139 /* Scrubbing DYLD_INSERT_LIBRARIES from envp during exec is insufficient,
140 as there are other ways to launch a process with environment that
141 valgrind can't catch easily (i.e. launchd).
142 Instead, scrub DYLD_INSERT_LIBRARIES from the parent process once
143 dyld is done loading vg_preload.so.
146 #include <crt_externs.h>
148 // GrP fixme copied from m_libcproc
149 static void env_unsetenv ( HChar
**env
, const HChar
*varname
)
153 Int len
= strlen(varname
);
155 for (from
= to
= env
; from
&& *from
; from
++) {
156 if (!(strncmp(varname
, *from
, len
) == 0 && (*from
)[len
] == '=')) {
162 /* fix the 4th "char* apple" pointer (aka. executable path pointer) */
167 static void vg_cleanup_env(void) __attribute__((constructor
));
168 static void vg_cleanup_env(void)
170 HChar
**envp
= (HChar
**)*_NSGetEnviron();
171 env_unsetenv(envp
, "VALGRIND_LAUNCHER");
172 env_unsetenv(envp
, "DYLD_SHARED_REGION");
173 // GrP fixme should be more like mash_colon_env()
174 env_unsetenv(envp
, "DYLD_INSERT_LIBRARIES");
177 /* ---------------------------------------------------------------------
178 Darwin arc4random (rdar://6166275)
179 ------------------------------------------------------------------ */
184 int VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib
, arc4random
)(void);
185 int VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib
, arc4random
)(void)
190 if (rnd
< 0) rnd
= open("/dev/random", O_RDONLY
);
192 read(rnd
, &result
, sizeof(result
));
196 void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib
, arc4random_stir
)(void);
197 void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib
, arc4random_stir
)(void)
202 void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib
, arc4random_addrandom
)(unsigned char *dat
, int datlen
);
203 void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib
, arc4random_addrandom
)(unsigned char *dat
, int datlen
)
206 // GrP fixme ought to check [dat..dat+datlen) is defined
207 // but don't care if it's initialized
210 #elif defined(VGO_solaris)
212 /* Declare the errno and environ symbols weakly in case the client is not
213 linked against libc. In such a case it also cannot run replacement
214 functions for set_error() and spawnveg() where these two variables are
215 needed so this is ok. */
216 __attribute__((weak
)) extern int errno
;
217 __attribute__((weak
)) extern char **environ
;
222 #include <sys/syscall.h>
223 #include <sys/signal.h>
226 /* Replace function block_all_signals() from libc. When the client program is
227 not running under valgrind, the function blocks all signals by setting
228 sc_sigblock flag in the schedctl control block. When run under Valgrind
229 this would bypass Valgrind's syscall and signal machinery.
230 Valgrind's signal machinery needs to retain control over which signals are
231 blocked and which not (see m_signals.c and m_scheduler/scheduler.c for more
232 information - typically synchronous signals should not be blocked).
233 Therefore this function replacement emulates lwp_sigmask syscall.
235 void VG_REPLACE_FUNCTION_ZU(VG_Z_LIBC_SONAME
, block_all_signals
)(/*ulwp_t*/ void *self
);
236 void VG_REPLACE_FUNCTION_ZU(VG_Z_LIBC_SONAME
, block_all_signals
)(/*ulwp_t*/ void *self
)
238 syscall(SYS_lwp_sigmask
, SIG_SETMASK
, ~0U, ~0U, ~0U, ~0U);
241 /* Replace functions get_error() and set_error() in libc. These functions are
242 internal to the library and are used to work with an error value returned
243 by posix_spawn() (when it is implemented using vfork()). A child calls
244 set_error() to set an error code and the parent then calls get_error() to
245 read it. Accessor functions are used so these trivial store+load operations
246 are not changed by the compiler in any way.
248 Since Valgrind translates vfork() to a normal fork(), calling set_error()
249 by the child would have no effect on the error value in the parent so
250 something must be done to fix this problem.
252 A pipe is created between a child and its parent in the forksys pre-wrapper
253 when a vfork() is encountered. The child's end of the pipe is closed when
254 the child exits or execs (because close-on-exec is set on the file
255 descriptor). Valgrind (the parent) waits on the child's end of the pipe to
256 be closed which preserves the vfork() behaviour that the parent process is
257 suspended while the child is using its resources.
259 The pipe is then used to send an eventual error code set by the child in
260 posix_spawn() to the parent. If there is any error Valgrind returns it as
261 an error from the vfork() syscall. This means the syscall can return errors
262 that it would normally never return but this is not a problem in practice
263 because any error is directly propagated as a return code from
266 Address of vg_vfork_fildes is found by Valgrind when debug information for
267 vgpreload_core.so is being processed. A value of this variable is set in
268 the forksys pre-wrapper before a fork() call is made and set back to -1
269 before returning from the wrapper by the parent.
271 Newer Solaris versions introduce the spawn syscall and posix_spawn() is
272 implemented using it. The redirect is not needed for these versions.
274 int vg_vfork_fildes
= -1;
276 int VG_REPLACE_FUNCTION_ZU(VG_Z_LIBC_SONAME
, get_error
)(int *errp
);
277 int VG_REPLACE_FUNCTION_ZU(VG_Z_LIBC_SONAME
, get_error
)(int *errp
)
279 /* Always return 0 when the parent tries to call get_error(). Any error
280 from the child is returned directly as an error from the vfork child.
281 Value pointed by errp is initialized only by the child so not
282 redirecting this function would mean that the parent gets an
283 uninitialized/garbage value when it calls this function. */
287 int VG_REPLACE_FUNCTION_ZU(VG_Z_LIBC_SONAME
, set_error
)(int *errp
, int err
);
288 int VG_REPLACE_FUNCTION_ZU(VG_Z_LIBC_SONAME
, set_error
)(int *errp
, int err
)
292 /* Libc should always call set_error() only after doing a vfork() syscall
293 in posix_spawn(). The forksys pre-wrapper saves a descriptor of the
294 child's end of the pipe in vg_vfork_fildes so it is an error if it is
295 not a valid file descriptor at this point. */
296 assert(vg_vfork_fildes
>= 0);
297 /* Current protocol between this function and the forksys pre-wrapper
298 allows to send only errors in range [0, 255] (one byte values). */
299 assert(err
>= 0 && err
<= 0xff);
302 unsigned char w
= (unsigned char)(err
& 0xff);
305 res
= write(vg_vfork_fildes
, &w
, 1);
306 assert(res
== 1 || (errno
== EINTR
|| errno
== ERESTART
));
313 /* Replace spawnveg() in libast.so.1. This function is used by ksh to spawn
314 new processes. The library has a build time option to select between
315 several variants of this function based on behaviour of vfork() and
316 posix_spawn() on the system for which the library is being compiled.
317 Unfortunately, Solaris and illumos use the real vfork() variant which does
318 not work correctly with the vfork() -> fork() translation done by Valgrind
319 (see the forksys pre-wrapper for details). Therefore the function is
320 replaced here with an implementation that uses posix_spawn(). This
321 replacement can be removed when a configuration of libast in Solaris and
322 illumos is changed to use the posix_spawn() implementation.
324 pid_t
VG_REPLACE_FUNCTION_ZU(libastZdsoZd1
, spawnveg
)(const char *command
,
328 pid_t
VG_REPLACE_FUNCTION_ZU(libastZdsoZd1
, spawnveg
)(const char *command
,
335 posix_spawnattr_t attr
;
336 int attr_init_done
= 0;
338 err
= posix_spawnattr_init(&attr
);
343 err
= posix_spawnattr_init(&attr
);
350 err
= posix_spawnattr_setpgroup(&attr
, pgid
);
353 err
= posix_spawnattr_setflags(&attr
, POSIX_SPAWN_SETPGROUP
);
358 err
= posix_spawn(&pid
, command
, NULL
, &attr
, argv
, envp
? envp
: environ
);
362 posix_spawnattr_destroy(&attr
);
374 /*--------------------------------------------------------------------*/
376 /*--------------------------------------------------------------------*/