blob: eaf6272778ae6b10b328f2728526ddb25bcc89bb [file] [log] [blame]
njn16eeb4e2005-06-16 03:56:58 +00001
sewardjcbdddcf2005-03-10 23:23:45 +00002/*--------------------------------------------------------------------*/
njn16eeb4e2005-06-16 03:56:58 +00003/*--- Client-space code for the core. vg_preloaded.c ---*/
sewardjcbdddcf2005-03-10 23:23:45 +00004/*--------------------------------------------------------------------*/
5
6/*
njnc0ae7052005-08-25 22:55:19 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
sewardjcbdddcf2005-03-10 23:23:45 +00009
sewardj0f157dd2013-10-18 14:27:36 +000010 Copyright (C) 2000-2013 Julian Seward
sewardjcbdddcf2005-03-10 23:23:45 +000011 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/* ---------------------------------------------------------------------
njn16eeb4e2005-06-16 03:56:58 +000033 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.
sewardjcbdddcf2005-03-10 23:23:45 +000043 ------------------------------------------------------------------ */
44
njnc7561b92005-06-19 01:24:32 +000045#include "pub_core_basics.h"
njn93fe3b22005-12-21 20:22:52 +000046#include "pub_core_clreq.h"
njn24a6efb2005-06-20 03:36:51 +000047#include "pub_core_debuginfo.h" // Needed for pub_core_redir.h
48#include "pub_core_redir.h" // For VG_NOTIFY_ON_LOAD
sewardjcbdddcf2005-03-10 23:23:45 +000049
sewardj6e9de462011-06-28 07:25:29 +000050#if defined(VGO_linux)
tomd2645142009-10-29 09:27:11 +000051
sewardjcbdddcf2005-03-10 23:23:45 +000052/* ---------------------------------------------------------------------
53 Hook for running __libc_freeres once the program exits.
54 ------------------------------------------------------------------ */
55
njn16eeb4e2005-06-16 03:56:58 +000056void VG_NOTIFY_ON_LOAD(freeres)( void );
57void VG_NOTIFY_ON_LOAD(freeres)( void )
sewardjcbdddcf2005-03-10 23:23:45 +000058{
philippe5d5dd8e2012-08-05 00:08:25 +000059# if !defined(__UCLIBC__) \
dejanj9c6b05d2013-12-27 09:06:55 +000060 && !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android) \
61 && !defined(VGPV_mips32_linux_android)
sewardjcbdddcf2005-03-10 23:23:45 +000062 extern void __libc_freeres(void);
63 __libc_freeres();
sewardj126e82d2011-07-12 13:33:00 +000064# endif
sewardj4b3a7422011-10-24 13:21:57 +000065 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__LIBC_FREERES_DONE,
bart575ce8e2011-05-15 07:04:03 +000066 0, 0, 0, 0, 0);
sewardjcbdddcf2005-03-10 23:23:45 +000067 /*NOTREACHED*/
sewardja6f76ee2010-10-11 19:15:33 +000068 *(volatile int *)0 = 'x';
sewardjcbdddcf2005-03-10 23:23:45 +000069}
70
tomd2645142009-10-29 09:27:11 +000071/* ---------------------------------------------------------------------
72 Wrapper for indirect functions which need to be redirected.
73 ------------------------------------------------------------------ */
74
75void * VG_NOTIFY_ON_LOAD(ifunc_wrapper) (void);
76void * VG_NOTIFY_ON_LOAD(ifunc_wrapper) (void)
77{
78 OrigFn fn;
79 Addr result = 0;
mjwa6838262014-07-15 15:07:01 +000080 Addr fnentry;
tomd2645142009-10-29 09:27:11 +000081
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
mjwa6838262014-07-15 15:07:01 +000086#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. */
mjwfa7b3292014-07-17 10:56:26 +000090 UWord *descr = (UWord*)(void*)result;
91 fnentry = (Addr)(void*)(descr[0]);
mjwa6838262014-07-15 15:07:01 +000092#else
93 fnentry = result;
94#endif
95
tomd2645142009-10-29 09:27:11 +000096 /* 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 */
sewardj4b3a7422011-10-24 13:21:57 +0000100 VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__ADD_IFUNC_TARGET,
mjwa6838262014-07-15 15:07:01 +0000101 fn.nraddr, fnentry, 0, 0, 0);
bart431ad282009-11-10 15:11:30 +0000102 return (void*)result;
tomd2645142009-10-29 09:27:11 +0000103}
104
njnf76d27a2009-05-28 01:53:07 +0000105#elif defined(VGO_darwin)
106
bart62e0f3c2009-12-29 17:30:16 +0000107#include "config.h" /* VERSION */
108
njnf76d27a2009-05-28 01:53:07 +0000109/* ---------------------------------------------------------------------
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. */
floriane07cbb32013-01-15 03:19:54 +0000115__private_extern__ const char *__crashreporter_info__ = "Instrumented by Valgrind " VERSION;
njnf76d27a2009-05-28 01:53:07 +0000116
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
florian19f91bb2012-11-10 22:29:54 +0000131static void env_unsetenv ( HChar **env, const HChar *varname )
njnf76d27a2009-05-28 01:53:07 +0000132{
florian19f91bb2012-11-10 22:29:54 +0000133 HChar **from;
134 HChar **to = NULL;
njnf76d27a2009-05-28 01:53:07 +0000135 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
149static void vg_cleanup_env(void) __attribute__((constructor));
150static void vg_cleanup_env(void)
151{
florian19f91bb2012-11-10 22:29:54 +0000152 HChar **envp = (HChar**)*_NSGetEnviron();
njnf76d27a2009-05-28 01:53:07 +0000153 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
njnea2d6fd2010-07-01 00:20:20 +0000163#include <fcntl.h>
164#include <unistd.h>
njnf76d27a2009-05-28 01:53:07 +0000165
166int VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random)(void);
167int VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random)(void)
168{
njnea2d6fd2010-07-01 00:20:20 +0000169 static int rnd = -1;
njnf76d27a2009-05-28 01:53:07 +0000170 int result;
171
njnea2d6fd2010-07-01 00:20:20 +0000172 if (rnd < 0) rnd = open("/dev/random", O_RDONLY);
173
174 read(rnd, &result, sizeof(result));
njnf76d27a2009-05-28 01:53:07 +0000175 return result;
176}
177
178void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_stir)(void);
179void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_stir)(void)
180{
181 // do nothing
182}
183
184void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_addrandom)(unsigned char *dat, int datlen);
185void 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
sewardjeb3603e2006-01-24 01:01:17 +0000196
sewardjcbdddcf2005-03-10 23:23:45 +0000197/*--------------------------------------------------------------------*/
njn16eeb4e2005-06-16 03:56:58 +0000198/*--- end ---*/
sewardjcbdddcf2005-03-10 23:23:45 +0000199/*--------------------------------------------------------------------*/