• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*--------------------------------------------------------------------*/
3 /*--- Client-space code for the core.               vg_preloaded.c ---*/
4 /*--------------------------------------------------------------------*/
5 
6 /*
7    This file is part of Valgrind, a dynamic binary instrumentation
8    framework.
9 
10    Copyright (C) 2000-2013 Julian Seward
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 /* ---------------------------------------------------------------------
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.
43    ------------------------------------------------------------------ */
44 
45 #include "pub_core_basics.h"
46 #include "pub_core_clreq.h"
47 #include "pub_core_debuginfo.h"  // Needed for pub_core_redir.h
48 #include "pub_core_redir.h"      // For VG_NOTIFY_ON_LOAD
49 
50 #if defined(VGO_linux)
51 
52 /* ---------------------------------------------------------------------
53    Hook for running __libc_freeres once the program exits.
54    ------------------------------------------------------------------ */
55 
56 void VG_NOTIFY_ON_LOAD(freeres)( void );
VG_NOTIFY_ON_LOAD(freeres)57 void VG_NOTIFY_ON_LOAD(freeres)( void )
58 {
59 #  if !defined(__UCLIBC__) \
60    && !defined(VGPV_arm_linux_android) && !defined(VGPV_x86_linux_android) \
61    && !defined(VGPV_mips32_linux_android) && !defined(VGPV_arm64_linux_android)
62    extern void __libc_freeres(void);
63    __libc_freeres();
64 #  endif
65    VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__LIBC_FREERES_DONE,
66                                    0, 0, 0, 0, 0);
67    /*NOTREACHED*/
68    *(volatile int *)0 = 'x';
69 }
70 
71 /* ---------------------------------------------------------------------
72    Wrapper for indirect functions which need to be redirected.
73    ------------------------------------------------------------------ */
74 
75 void * VG_NOTIFY_ON_LOAD(ifunc_wrapper) (void);
VG_NOTIFY_ON_LOAD(ifunc_wrapper)76 void * VG_NOTIFY_ON_LOAD(ifunc_wrapper) (void)
77 {
78     OrigFn fn;
79     Addr result = 0;
80 
81     /* Call the original indirect function and get it's result */
82     VALGRIND_GET_ORIG_FN(fn);
83     CALL_FN_W_v(result, fn);
84 
85     /* Ask the valgrind core running on the real CPU (as opposed to this
86        code which runs on the emulated CPU) to update the redirection that
87        led to this function. This client request eventually gives control to
88        the function VG_(redir_add_ifunc_target) in m_redir.c  */
89     VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__ADD_IFUNC_TARGET,
90                                     fn.nraddr, result, 0, 0, 0);
91     return (void*)result;
92 }
93 
94 #elif defined(VGO_darwin)
95 
96 #include "config.h" /* VERSION */
97 
98 /* ---------------------------------------------------------------------
99    Darwin crash log hints
100    ------------------------------------------------------------------ */
101 
102 /* This string will be inserted into crash logs, so crashes while
103    running under Valgrind can be distinguished from other crashes. */
104 __private_extern__ const char *__crashreporter_info__ = "Instrumented by Valgrind " VERSION;
105 
106 /* ---------------------------------------------------------------------
107    Darwin environment cleanup
108    ------------------------------------------------------------------ */
109 
110 /* Scrubbing DYLD_INSERT_LIBRARIES from envp during exec is insufficient,
111    as there are other ways to launch a process with environment that
112    valgrind can't catch easily (i.e. launchd).
113    Instead, scrub DYLD_INSERT_LIBRARIES from the parent process once
114    dyld is done loading vg_preload.so.
115 */
116 #include <string.h>
117 #include <crt_externs.h>
118 
119 // GrP fixme copied from m_libcproc
env_unsetenv(HChar ** env,const HChar * varname)120 static void env_unsetenv ( HChar **env, const HChar *varname )
121 {
122    HChar **from;
123    HChar **to = NULL;
124    Int len = strlen(varname);
125 
126    for (from = to = env; from && *from; from++) {
127       if (!(strncmp(varname, *from, len) == 0 && (*from)[len] == '=')) {
128 	 *to = *from;
129 	 to++;
130       }
131    }
132    *(to++) = *(from++);
133    /* fix the 4th "char* apple" pointer (aka. executable path pointer) */
134    *(to++) = *(from++);
135    *to = NULL;
136 }
137 
138 static void vg_cleanup_env(void)  __attribute__((constructor));
vg_cleanup_env(void)139 static void vg_cleanup_env(void)
140 {
141     HChar **envp = (HChar**)*_NSGetEnviron();
142     env_unsetenv(envp, "VALGRIND_LAUNCHER");
143     env_unsetenv(envp, "DYLD_SHARED_REGION");
144     // GrP fixme should be more like mash_colon_env()
145     env_unsetenv(envp, "DYLD_INSERT_LIBRARIES");
146 }
147 
148 /* ---------------------------------------------------------------------
149    Darwin arc4random (rdar://6166275)
150    ------------------------------------------------------------------ */
151 
152 #include <fcntl.h>
153 #include <unistd.h>
154 
155 int VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random)(void);
VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib,arc4random)156 int VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random)(void)
157 {
158     static int rnd = -1;
159     int result;
160 
161     if (rnd < 0) rnd = open("/dev/random", O_RDONLY);
162 
163     read(rnd, &result, sizeof(result));
164     return result;
165 }
166 
167 void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_stir)(void);
VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib,arc4random_stir)168 void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_stir)(void)
169 {
170     // do nothing
171 }
172 
173 void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_addrandom)(unsigned char *dat, int datlen);
VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib,arc4random_addrandom)174 void VG_REPLACE_FUNCTION_ZU(libSystemZdZaZddylib, arc4random_addrandom)(unsigned char *dat, int datlen)
175 {
176     // do nothing
177     // GrP fixme ought to check [dat..dat+datlen) is defined
178     // but don't care if it's initialized
179 }
180 
181 #else
182 
183 #  error Unknown OS
184 #endif
185 
186 /*--------------------------------------------------------------------*/
187 /*--- end                                                          ---*/
188 /*--------------------------------------------------------------------*/
189