2 /*--------------------------------------------------------------------*/
3 /*--- Process-related libc stuff. m_libcproc.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.
29 #include "pub_core_basics.h"
30 #include "pub_core_machine.h" // For VG_(machine_get_VexArchInfo)
31 #include "pub_core_vki.h"
32 #include "pub_core_vkiscnums.h"
33 #include "pub_core_libcbase.h"
34 #include "pub_core_libcassert.h"
35 #include "pub_core_libcfile.h"
36 #include "pub_core_libcprint.h"
37 #include "pub_core_libcproc.h"
38 #include "pub_core_libcsignal.h"
39 #include "pub_core_seqmatch.h"
40 #include "pub_core_mallocfree.h"
41 #include "pub_core_signals.h"
42 #include "pub_core_syscall.h"
43 #include "pub_core_xarray.h"
44 #include "pub_core_clientstate.h"
45 #include "pub_core_debuglog.h" // VG_(debugLog)
47 #if defined(VGO_darwin)
48 /* --- !!! --- EXTERNAL HEADERS start --- !!! --- */
49 #include <mach/mach.h> /* mach_thread_self */
50 /* --- !!! --- EXTERNAL HEADERS end --- !!! --- */
53 /* IMPORTANT: on Darwin it is essential to use the _nocancel versions
54 of syscalls rather than the vanilla version, if a _nocancel version
55 is available. See docs/internals/Darwin-notes.txt for the reason
58 /* ---------------------------------------------------------------------
59 Command line and environment stuff
60 ------------------------------------------------------------------ */
62 /* As deduced from sp_at_startup, the client's argc, argv[] and
63 envp[] as extracted from the client's stack at startup-time. */
64 HChar
** VG_(client_envp
) = NULL
;
66 /* Path to library directory */
67 const HChar
*VG_(libdir
) = VG_LIBDIR
;
69 const HChar
*VG_(LD_PRELOAD_var_name
) =
70 #if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
72 #elif defined(VGO_darwin)
73 "DYLD_INSERT_LIBRARIES";
78 /* We do getenv without libc's help by snooping around in
79 VG_(client_envp) as determined at startup time. */
80 HChar
*VG_(getenv
)(const HChar
*varname
)
83 vg_assert( VG_(client_envp
) );
84 n
= VG_(strlen
)(varname
);
85 for (i
= 0; VG_(client_envp
)[i
] != NULL
; i
++) {
86 HChar
* s
= VG_(client_envp
)[i
];
87 if (VG_(strncmp
)(varname
, s
, n
) == 0 && s
[n
] == '=') {
94 /* If free_fn is not NULL, it is called on "unset" environment variable. */
95 void VG_(env_unsetenv
) ( HChar
**env
, const HChar
*varname
,
96 void (*free_fn
) (void *) )
102 Int len
= VG_(strlen
)(varname
);
104 for (from
= to
= env
; from
&& *from
; from
++) {
105 if (!(VG_(strncmp
)(varname
, *from
, len
) == 0 && (*from
)[len
] == '=')) {
108 } else if (free_fn
!= NULL
) {
115 /* set the environment; returns the old env if a new one was allocated */
116 HChar
**VG_(env_setenv
) ( HChar
***envp
, const HChar
* varname
,
119 HChar
**env
= (*envp
);
121 Int len
= VG_(strlen
)(varname
);
122 HChar
*valstr
= VG_(malloc
)("libcproc.es.1", len
+ VG_(strlen
)(val
) + 2);
123 HChar
**oldenv
= NULL
;
125 VG_(sprintf
)(valstr
, "%s=%s", varname
, val
);
127 for (cpp
= env
; cpp
&& *cpp
; cpp
++) {
128 if (VG_(strncmp
)(varname
, *cpp
, len
) == 0 && (*cpp
)[len
] == '=') {
135 env
= VG_(malloc
)("libcproc.es.2", sizeof(HChar
*) * 2);
142 Int envlen
= (cpp
-env
) + 2;
143 HChar
**newenv
= VG_(malloc
)("libcproc.es.3", envlen
* sizeof(HChar
*));
145 for (cpp
= newenv
; *env
; )
159 /* Walk through a colon-separated environment variable, and remove the
160 entries which match remove_pattern. It slides everything down over
161 the removed entries, and pads the remaining space with '\0'. It
162 modifies the entries in place (in the client address space), but it
163 shouldn't matter too much, since we only do this just before an
166 This is also careful to mop up any excess ':'s, since empty strings
167 delimited by ':' are considered to be '.' in a path.
169 static void mash_colon_env(HChar
*varp
, const HChar
*remove_pattern
)
171 HChar
*const start
= varp
;
172 HChar
*entry_start
= varp
;
173 HChar
*output
= varp
;
183 /* This is a bit subtle: we want to match against the entry
184 we just copied, because it may have overlapped with
185 itself, junking the original. */
190 match
= VG_(string_match
)(remove_pattern
, entry_start
);
195 output
= entry_start
;
196 varp
++; /* skip ':' after removed entry */
198 entry_start
= output
+1; /* entry starts after ':' */
205 /* make sure last entry is nul terminated */
208 /* match against the last entry */
209 if (VG_(string_match
)(remove_pattern
, entry_start
)) {
210 output
= entry_start
;
211 if (output
> start
) {
212 /* remove trailing ':' */
214 vg_assert(*output
== ':');
218 /* pad out the left-overs with '\0' */
224 /* Removes all the Valgrind-added stuff from the passed environment. Used
225 when starting child processes, so they don't see that added stuff.
226 If the ro_strings option is set to True then all strings referenced by envp
227 are considered read-only, which means they will be duplicated before they
229 If free_fn is not NULL, it is called on "unset" environment variables. */
230 void VG_(env_remove_valgrind_env_stuff
)(HChar
** envp
, Bool ro_strings
,
231 void (*free_fn
) (void *) )
234 HChar
* ld_preload_str
= NULL
;
235 HChar
* ld_library_path_str
= NULL
;
236 HChar
* dyld_insert_libraries_str
= NULL
;
239 // Find LD_* variables
240 // DDD: should probably conditionally compiled some of this:
241 // - LD_LIBRARY_PATH is universal?
242 // - LD_PRELOAD is on Linux, not on Darwin, not sure about AIX
243 // - DYLD_INSERT_LIBRARIES and DYLD_SHARED_REGION are Darwin-only
244 for (i
= 0; envp
[i
] != NULL
; i
++) {
245 if (VG_(strncmp
)(envp
[i
], "LD_PRELOAD=", 11) == 0) {
247 envp
[i
] = VG_(strdup
)("libcproc.erves.1", envp
[i
]);
248 ld_preload_str
= &envp
[i
][11];
250 if (VG_(strncmp
)(envp
[i
], "LD_LIBRARY_PATH=", 16) == 0) {
252 envp
[i
] = VG_(strdup
)("libcproc.erves.2", envp
[i
]);
253 ld_library_path_str
= &envp
[i
][16];
255 if (VG_(strncmp
)(envp
[i
], "DYLD_INSERT_LIBRARIES=", 22) == 0) {
257 envp
[i
] = VG_(strdup
)("libcproc.erves.3", envp
[i
]);
258 dyld_insert_libraries_str
= &envp
[i
][22];
262 buf
= VG_(malloc
)("libcproc.erves.4", VG_(strlen
)(VG_(libdir
)) + 20);
264 // Remove Valgrind-specific entries from LD_*.
265 VG_(sprintf
)(buf
, "%s*/vgpreload_*.so", VG_(libdir
));
266 mash_colon_env(ld_preload_str
, buf
);
267 mash_colon_env(dyld_insert_libraries_str
, buf
);
268 VG_(sprintf
)(buf
, "%s*", VG_(libdir
));
269 mash_colon_env(ld_library_path_str
, buf
);
271 // Remove VALGRIND_LAUNCHER variable.
272 VG_(env_unsetenv
)(envp
, VALGRIND_LAUNCHER
, free_fn
);
274 // Remove DYLD_SHARED_REGION variable.
275 VG_(env_unsetenv
)(envp
, "DYLD_SHARED_REGION", free_fn
);
277 // XXX if variable becomes empty, remove it completely?
282 /* Resolves filename of VG_(cl_exec_fd) and copies it to the buffer.
283 Buffer must not be NULL and buf_size must be at least 1.
284 If buffer is not large enough it is terminated with '\0' only
285 when 'terminate_with_NUL == True'. */
286 void VG_(client_fname
)(HChar
*buffer
, SizeT buf_size
, Bool terminate_with_NUL
)
288 vg_assert(buffer
!= NULL
);
289 vg_assert(buf_size
>= 1);
292 if (VG_(resolve_filename
)(VG_(cl_exec_fd
), &name
)) {
293 const HChar
*n
= name
+ VG_(strlen
)(name
) - 1;
295 while (n
> name
&& *n
!= '/')
300 VG_(strncpy
)(buffer
, n
, buf_size
);
301 if (terminate_with_NUL
)
302 buffer
[buf_size
- 1] = '\0';
308 static Bool
add_string(HChar
*buffer
, SizeT
*buf_size
, const HChar
*string
)
310 SizeT len
= VG_(strlen
)(string
);
311 VG_(strncat
)(buffer
, string
, *buf_size
);
312 if (len
>= *buf_size
- 1) {
321 /* Concatenates client exename and command line arguments into
322 the buffer. Buffer must not be NULL and buf_size must be
323 at least 1. Buffer is always terminated with '\0'. */
324 void VG_(client_cmd_and_args
)(HChar
*buffer
, SizeT buf_size
)
326 vg_assert(buffer
!= NULL
);
327 vg_assert(buf_size
>= 1);
331 if (add_string(buffer
, &buf_size
, VG_(args_the_exename
)) == False
)
335 for (i
= 0; i
< VG_(sizeXA
)(VG_(args_for_client
)); i
++) {
336 if (add_string(buffer
, &buf_size
, " ") == False
)
339 HChar
*arg
= *(HChar
**) VG_(indexXA
)(VG_(args_for_client
), i
);
340 if (add_string(buffer
, &buf_size
, arg
) == False
)
345 /* ---------------------------------------------------------------------
346 Various important syscall wrappers
347 ------------------------------------------------------------------ */
349 Int
VG_(waitpid
)(Int pid
, Int
*status
, Int options
)
351 # if defined(VGO_linux) || defined(VGO_freebsd)
352 SysRes res
= VG_(do_syscall4
)(__NR_wait4
,
353 pid
, (UWord
)status
, options
, 0);
354 return sr_isError(res
) ? -1 : sr_Res(res
);
355 # elif defined(VGO_darwin)
356 SysRes res
= VG_(do_syscall4
)(__NR_wait4_nocancel
,
357 pid
, (UWord
)status
, options
, 0);
358 return sr_isError(res
) ? -1 : sr_Res(res
);
359 # elif defined(VGO_solaris)
365 /* We need to do a lot of work here. */
375 else if (pid
== -1) {
381 res
= VG_(do_syscall0
)(__NR_getpid
);
385 options
|= VKI_WEXITED
| VKI_WTRAPPED
;
387 res
= VG_(do_syscall4
)(__NR_waitsys
, idtype
, id
, (UWord
)&info
, options
);
392 Int s
= info
.si_status
& 0xff;
394 switch (info
.si_code
) {
403 case VKI_CLD_TRAPPED
:
404 case VKI_CLD_STOPPED
:
408 case VKI_CLD_CONTINUED
:
421 /* clone the environment */
422 HChar
**VG_(env_clone
) ( HChar
**oldenv
)
430 for (oldenvp
= oldenv
; oldenvp
&& *oldenvp
; oldenvp
++);
432 envlen
= oldenvp
- oldenv
+ 1;
434 newenv
= VG_(malloc
)("libcproc.ec.1", envlen
* sizeof(HChar
*));
439 while (oldenvp
&& *oldenvp
) {
440 *newenvp
++ = *oldenvp
++;
448 void VG_(execv
) ( const HChar
* filename
, const HChar
** argv
)
453 envp
= VG_(env_clone
)(VG_(client_envp
));
454 VG_(env_remove_valgrind_env_stuff
)( envp
, True
/*ro_strings*/, NULL
);
456 res
= VG_(do_syscall3
)(__NR_execve
,
457 (UWord
)filename
, (UWord
)argv
, (UWord
)envp
);
459 VG_(printf
)("EXEC failed, errno = %lld\n", (Long
)sr_Err(res
));
462 /* Spawns a new child. Uses either spawn syscall or fork+execv combo. */
463 Int
VG_(spawn
) ( const HChar
*filename
, const HChar
**argv
)
465 vg_assert(filename
!= NULL
);
466 vg_assert(argv
!= NULL
);
468 # if defined(VGO_solaris) && defined(SOLARIS_SPAWN_SYSCALL)
469 HChar
**envp
= VG_(env_clone
)(VG_(client_envp
));
470 for (HChar
**p
= envp
; *p
!= NULL
; p
++) {
471 *p
= VG_(strdup
)("libcproc.s.1", *p
);
473 VG_(env_remove_valgrind_env_stuff
)(envp
, /* ro_strings */ False
, VG_(free
));
475 /* Now combine argv and argp into argenv. */
476 SizeT argenv_size
= 1 + 1;
477 for (const HChar
**p
= argv
; *p
!= NULL
; p
++) {
478 argenv_size
+= VG_(strlen
)(*p
) + 2;
480 for (HChar
**p
= envp
; *p
!= NULL
; p
++) {
481 argenv_size
+= VG_(strlen
)(*p
) + 2;
484 HChar
*argenv
= VG_(malloc
)("libcproc.s.2", argenv_size
);
485 HChar
*current
= argenv
;
486 # define COPY_CHAR_TO_ARGENV(dst, character) \
488 *(dst) = character; \
491 # define COPY_STRING_TO_ARGENV(dst, src) \
493 COPY_CHAR_TO_ARGENV(dst, '\1'); \
494 SizeT src_len = VG_(strlen)((src)) + 1; \
495 VG_(memcpy)((dst), (src), src_len); \
499 for (const HChar
**p
= argv
; *p
!= NULL
; p
++) {
500 COPY_STRING_TO_ARGENV(current
, *p
);
502 COPY_CHAR_TO_ARGENV(current
, '\0');
503 for (HChar
**p
= envp
; *p
!= NULL
; p
++) {
504 COPY_STRING_TO_ARGENV(current
, *p
);
506 COPY_CHAR_TO_ARGENV(current
, '\0');
507 vg_assert(current
== argenv
+ argenv_size
);
508 # undef COPY_CHAR_TO_ARGENV
509 # undef COPY_STRING_TOARGENV
511 SysRes res
= VG_(do_syscall5
)(__NR_spawn
, (UWord
) filename
, (UWord
) NULL
, 0,
512 (UWord
) argenv
, argenv_size
);
515 for (HChar
**p
= envp
; *p
!= NULL
; p
++) {
526 Int pid
= VG_(fork
)();
531 VG_(execv
)(argv
[0], argv
);
533 /* If we're still alive here, execv failed. */
538 # endif /* VGO_solaris && SOLARIS_SPAWN_SYSCALL */
541 /* Return -1 if error, else 0. NOTE does not indicate return code of
543 Int
VG_(system
) ( const HChar
* cmd
)
549 const HChar
*argv
[4] = { "/bin/sh", "-c", cmd
, 0 };
550 pid
= VG_(spawn
)(argv
[0], argv
);
556 /* We have to set SIGCHLD to its default behaviour in order that
557 VG_(waitpid) works (at least on AIX). According to the Linux
558 man page for waitpid:
560 POSIX.1-2001 specifies that if the disposition of SIGCHLD is
561 set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD
562 (see sigaction(2)), then children that terminate do not
563 become zombies and a call to wait() or waitpid() will block
564 until all children have terminated, and then fail with errno
565 set to ECHILD. (The original POSIX standard left the
566 behaviour of setting SIGCHLD to SIG_IGN unspecified.)
569 vki_sigaction_toK_t sa
, sa2
;
570 vki_sigaction_fromK_t saved_sa
;
571 VG_(memset
)( &sa
, 0, sizeof(sa
) );
572 VG_(sigemptyset
)(&sa
.sa_mask
);
573 sa
.ksa_handler
= VKI_SIG_DFL
;
575 ir
= VG_(sigaction
)(VKI_SIGCHLD
, &sa
, &saved_sa
);
578 zzz
= VG_(waitpid
)(pid
, NULL
, 0);
580 VG_(convert_sigaction_fromK_to_toK
)( &saved_sa
, &sa2
);
581 ir
= VG_(sigaction
)(VKI_SIGCHLD
, &sa2
, NULL
);
583 return zzz
== -1 ? -1 : 0;
586 Int
VG_(sysctl
)(Int
*name
, UInt namelen
, void *oldp
, SizeT
*oldlenp
, const void *newp
, SizeT newlen
)
589 # if defined(VGO_darwin) || defined(VGO_freebsd)
590 res
= VG_(do_syscall6
)(__NR___sysctl
,
591 (UWord
)name
, namelen
, (UWord
)oldp
, (UWord
)oldlenp
, (UWord
)newp
, newlen
);
593 res
= VG_(mk_SysRes_Error
)(VKI_ENOSYS
);
595 return sr_isError(res
) ? -1 : sr_Res(res
);
598 /* ---------------------------------------------------------------------
600 ------------------------------------------------------------------ */
602 /* Support for getrlimit. */
603 Int
VG_(getrlimit
) (Int resource
, struct vki_rlimit
*rlim
)
606 /* res = getrlimit( resource, rlim ); */
608 # if defined(__NR_prlimit64) && defined(VKI_RLIM_INFINITY) && defined(VKI_RLIM64_INFINITY)
609 struct vki_rlimit64 new_rlimit
;
610 res
= VG_(do_syscall4
)(__NR_prlimit64
, 0, resource
, 0, (UWord
)&new_rlimit
);
611 if (!sr_isError(res
)) {
612 if (new_rlimit
.rlim_cur
== VKI_RLIM_INFINITY
)
613 new_rlimit
.rlim_cur
= VKI_RLIM64_INFINITY
;
614 if (new_rlimit
.rlim_max
== VKI_RLIM_INFINITY
)
615 new_rlimit
.rlim_max
= VKI_RLIM64_INFINITY
;
616 rlim
->rlim_cur
= new_rlimit
.rlim_cur
;
617 rlim
->rlim_max
= new_rlimit
.rlim_max
;
620 if (sr_Err(res
) != VKI_ENOSYS
) return -1;
623 # ifdef __NR_ugetrlimit
624 res
= VG_(do_syscall2
)(__NR_ugetrlimit
, resource
, (UWord
)rlim
);
625 if (!sr_isError(res
)) return sr_Res(res
);
626 if (sr_Err(res
) != VKI_ENOSYS
) return -1;
629 # ifdef __NR_getrlimit
630 res
= VG_(do_syscall2
)(__NR_getrlimit
, resource
, (UWord
)rlim
);
631 if (!sr_isError(res
)) return sr_Res(res
);
637 /* Support for setrlimit. */
638 Int
VG_(setrlimit
) (Int resource
, const struct vki_rlimit
*rlim
)
641 /* res = setrlimit( resource, rlim ); */
643 # ifdef __NR_prlimit64
644 struct vki_rlimit64 new_rlimit
;
645 new_rlimit
.rlim_cur
= rlim
->rlim_cur
;
646 new_rlimit
.rlim_max
= rlim
->rlim_max
;
647 res
= VG_(do_syscall4
)(__NR_prlimit64
, 0, resource
, (UWord
)&new_rlimit
, 0);
648 if (!sr_isError(res
)) return sr_Res(res
);
649 if (sr_Err(res
) != VKI_ENOSYS
) return -1;
652 # ifdef __NR_setrlimit
653 res
= VG_(do_syscall2
)(__NR_setrlimit
, resource
, (UWord
)rlim
);
654 if (!sr_isError(res
)) return sr_Res(res
);
655 if (sr_Err(res
) != VKI_ENOSYS
) return -1;
661 /* Support for prctl. */
662 Int
VG_(prctl
) (Int option
,
663 ULong arg2
, ULong arg3
, ULong arg4
, ULong arg5
)
665 SysRes res
= VG_(mk_SysRes_Error
)(VKI_ENOSYS
);
666 # if defined(VGO_linux)
667 /* res = prctl( option, arg2, arg3, arg4, arg5 ); */
668 res
= VG_(do_syscall5
)(__NR_prctl
, (UWord
) option
,
669 (UWord
) arg2
, (UWord
) arg3
, (UWord
) arg4
,
673 return sr_isError(res
) ? -1 : sr_Res(res
);
676 /* ---------------------------------------------------------------------
678 ------------------------------------------------------------------ */
680 Int
VG_(gettid
)(void)
682 # if defined(VGO_linux)
683 SysRes res
= VG_(do_syscall0
)(__NR_gettid
);
685 if (sr_isError(res
) && sr_Res(res
) == VKI_ENOSYS
) {
688 * The gettid system call does not exist. The obvious assumption
689 * to make at this point would be that we are running on an older
690 * system where the getpid system call actually returns the ID of
691 * the current thread.
693 * Unfortunately it seems that there are some systems with a kernel
694 * where getpid has been changed to return the ID of the thread group
695 * leader but where the gettid system call has not yet been added.
697 * So instead of calling getpid here we use readlink to see where
698 * the /proc/self link is pointing...
701 # if defined(VGP_arm64_linux) || defined(VGP_nanomips_linux)
702 res
= VG_(do_syscall4
)(__NR_readlinkat
, VKI_AT_FDCWD
,
704 (UWord
)pid
, sizeof(pid
));
706 res
= VG_(do_syscall3
)(__NR_readlink
, (UWord
)"/proc/self",
707 (UWord
)pid
, sizeof(pid
));
709 if (!sr_isError(res
) && sr_Res(res
) > 0) {
711 pid
[sr_Res(res
)] = '\0';
712 res
= VG_(mk_SysRes_Success
)( VG_(strtoll10
)(pid
, &s
) );
714 VG_(message
)(Vg_DebugMsg
,
715 "Warning: invalid file name linked to by /proc/self: %s\n",
723 # elif defined(VGO_freebsd)
727 res
= VG_(do_syscall1
)(__NR_thr_self
, (UWord
)&tid
);
729 tid
= sr_Res(VG_(do_syscall0
)(__NR_getpid
));
732 # elif defined(VGO_darwin)
733 // Darwin's gettid syscall is something else.
734 // Use Mach thread ports for lwpid instead.
735 return mach_thread_self();
737 # elif defined(VGO_solaris)
738 SysRes res
= VG_(do_syscall0
)(__NR_lwp_self
);
746 /* You'd be amazed how many places need to know the current pid. */
747 Int
VG_(getpid
) ( void )
749 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
750 return sr_Res( VG_(do_syscall0
)(__NR_getpid
) );
753 Int
VG_(getpgrp
) ( void )
755 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
756 # if defined(VGP_arm64_linux) || defined(VGP_nanomips_linux)
757 return sr_Res( VG_(do_syscall1
)(__NR_getpgid
, 0) );
758 # elif defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_freebsd)
759 return sr_Res( VG_(do_syscall0
)(__NR_getpgrp
) );
760 # elif defined(VGO_solaris)
761 /* Uses the shared pgrpsys syscall, 0 for the getpgrp variant. */
762 return sr_Res( VG_(do_syscall1
)(__NR_pgrpsys
, 0) );
768 Int
VG_(getppid
) ( void )
770 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
771 # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_freebsd)
772 return sr_Res( VG_(do_syscall0
)(__NR_getppid
) );
773 # elif defined(VGO_solaris)
774 /* Uses the shared getpid/getppid syscall, val2 contains a parent pid. */
775 return sr_ResHI( VG_(do_syscall0
)(__NR_getpid
) );
781 Int
VG_(geteuid
) ( void )
783 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
784 # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_freebsd)
786 # if defined(__NR_geteuid32)
787 // We use the 32-bit version if it's supported. Otherwise, IDs greater
788 // than 65536 cause problems, as bug #151209 showed.
789 return sr_Res( VG_(do_syscall0
)(__NR_geteuid32
) );
791 return sr_Res( VG_(do_syscall0
)(__NR_geteuid
) );
794 # elif defined(VGO_solaris)
795 /* Uses the shared getuid/geteuid syscall, val2 contains the effective
797 return sr_ResHI( VG_(do_syscall0
)(__NR_getuid
) );
803 Int
VG_(getegid
) ( void )
805 # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_freebsd)
806 /* ASSUMES SYSCALL ALWAYS SUCCEEDS */
807 # if defined(__NR_getegid32)
808 // We use the 32-bit version if it's supported. Otherwise, IDs greater
809 // than 65536 cause problems, as bug #151209 showed.
810 return sr_Res( VG_(do_syscall0
)(__NR_getegid32
) );
812 return sr_Res( VG_(do_syscall0
)(__NR_getegid
) );
815 # elif defined(VGO_solaris)
816 /* Uses the shared getgid/getegid syscall, val2 contains the effective
818 return sr_ResHI( VG_(do_syscall0
)(__NR_getgid
) );
824 /* Get supplementary groups into list[0 .. size-1]. Returns the
825 number of groups written, or -1 if error. Note that in order to be
826 portable, the groups are 32-bit unsigned ints regardless of the
828 As a special case, if size == 0 the function returns the number of
829 groups leaving list untouched. */
830 Int
VG_(getgroups
)( Int size
, UInt
* list
)
832 if (size
< 0) return -1;
834 # if defined(VGP_x86_linux) || defined(VGP_ppc32_linux) \
835 || defined(VGP_mips64_linux)
839 sres
= VG_(do_syscall2
)(__NR_getgroups
, size
, (Addr
)list16
);
840 if (sr_isError(sres
))
843 for (i
= 0; i
< sr_Res(sres
); i
++)
844 list
[i
] = (UInt
)list16
[i
];
848 # elif defined(VGP_amd64_linux) || defined(VGP_arm_linux) \
849 || defined(VGP_ppc64be_linux) || defined(VGP_ppc64le_linux) \
850 || defined(VGO_darwin) || defined(VGP_s390x_linux) \
851 || defined(VGP_mips32_linux) || defined(VGP_arm64_linux) \
852 || defined(VGO_solaris) || defined(VGP_nanomips_linux) \
853 || defined(VGO_freebsd)
855 sres
= VG_(do_syscall2
)(__NR_getgroups
, size
, (Addr
)list
);
856 if (sr_isError(sres
))
861 # error "VG_(getgroups): needs implementation on this platform"
865 /* ---------------------------------------------------------------------
867 ------------------------------------------------------------------ */
869 Int
VG_(ptrace
) ( Int request
, Int pid
, void *addr
, void *data
)
872 # if defined(VGO_linux) || defined(VGO_darwin) || defined(VGO_freebsd)
873 res
= VG_(do_syscall4
)(__NR_ptrace
, request
, pid
, (UWord
)addr
, (UWord
)data
);
874 # elif defined(VGO_solaris)
875 /* There is no ptrace syscall on Solaris. Such requests has to be
876 implemented using the /proc interface. Callers of VG_(ptrace) should
877 ensure that this function is not reached on Solaris, i.e. they must
878 provide a special code for Solaris for whatever feature they provide. */
888 /* ---------------------------------------------------------------------
890 ------------------------------------------------------------------ */
892 /* Record PID of a child process in order to avoid sending any SIGCHLD from
893 it to the client. If PID is 0 then this is the child process and it
894 should synch with the parent to ensure it can't send any SIGCHLD before
895 the parent has registered its PID.
897 FDS should be initialized with VG_(pipe). This function closes both
899 static void register_sigchld_ignore ( Int pid
, Int fds
[2])
904 if (fds
[0] < 0 || fds
[1] < 0)
908 /* We are the child, close writing fd that we don't use. */
910 /* Before proceeding, ensure parent has recorded child PID in map
911 of SIGCHLD to ignore */
912 while (child_wait
== 1)
914 if (VG_(read
)(fds
[0], &child_wait
, sizeof(Int
)) <= 0) {
915 VG_(message
)(Vg_DebugMsg
,
916 "warning: Unable to record PID of internal process (read)\n");
921 /* Now close reading fd. */
926 n
= VG_(malloc
)("ht.ignore.node", sizeof(ht_ignore_node
));
928 if (ht_sigchld_ignore
== NULL
)
929 ht_sigchld_ignore
= VG_(HT_construct
)("ht.sigchld.ignore");
930 VG_(HT_add_node
)(ht_sigchld_ignore
, n
);
932 /* We are the parent process, close read fd that we don't use. */
936 if (VG_(write
)(fds
[1], &child_wait
, sizeof(Int
)) <= 0)
937 VG_(message
)(Vg_DebugMsg
,
938 "warning: Unable to record PID of internal process (write)\n");
940 /* Now close writing fd. */
944 Int
VG_(fork
) ( void )
948 if (VG_(pipe
)(fds
) != 0) {
949 VG_(message
)(Vg_DebugMsg
,
950 "warning: Unable to record PID of internal process (pipe)\n");
951 fds
[0] = fds
[1] = -1;
954 # if defined(VGP_arm64_linux) || defined(VGP_nanomips_linux)
956 res
= VG_(do_syscall5
)(__NR_clone
, VKI_SIGCHLD
,
957 (UWord
)NULL
, (UWord
)NULL
, (UWord
)NULL
, (UWord
)NULL
);
960 register_sigchld_ignore(sr_Res(res
), fds
);
963 # elif defined(VGO_linux) || defined(VGO_freebsd)
965 res
= VG_(do_syscall0
)(__NR_fork
);
968 register_sigchld_ignore(sr_Res(res
), fds
);
971 # elif defined(VGO_darwin)
973 res
= VG_(do_syscall0
)(__NR_fork
); /* __NR_fork is UX64 */
976 /* on success: wLO = child pid; wHI = 1 for child, 0 for parent */
977 if (sr_ResHI(res
) != 0) {
978 register_sigchld_ignore(0, fds
);
979 return 0; /* this is child: return 0 instead of child pid */
981 register_sigchld_ignore(sr_Res(res
), fds
);
984 # elif defined(VGO_solaris)
985 /* Using fork() on Solaris is not really the best thing to do. Solaris
986 does not do memory overcommitment so fork() can fail if there is not
987 enough memory to copy the current process into a new one.
988 Prefer to use VG_(spawn)() over VG_(fork)() + VG_(execv)(). */
990 res
= VG_(do_syscall2
)(__NR_forksys
, 0 /*subcode (fork)*/, 0 /*flags*/);
994 val = a pid of the child in the parent, a pid of the parent in the
996 val2 = 0 in the parent process, 1 in the child process. */
997 if (sr_ResHI(res
) != 0) {
998 register_sigchld_ignore(0, fds
);
1001 register_sigchld_ignore(sr_Res(res
), fds
);
1005 # error "Unknown OS"
1009 /* ---------------------------------------------------------------------
1011 ------------------------------------------------------------------ */
1013 UInt
VG_(read_millisecond_timer
) ( void )
1015 /* 'now' and 'base' are in microseconds */
1016 static ULong base
= 0;
1019 # if defined(VGO_linux) || defined(VGO_solaris)
1021 struct vki_timespec ts_now
;
1022 res
= VG_(do_syscall2
)(__NR_clock_gettime
, VKI_CLOCK_MONOTONIC
,
1024 if (sr_isError(res
) == 0) {
1025 now
= ts_now
.tv_sec
* 1000000ULL + ts_now
.tv_nsec
/ 1000;
1027 struct vki_timeval tv_now
;
1028 /* Note: On Solaris, this syscall takes only one parameter but the
1029 extra dummy one does not cause any harm. */
1030 res
= VG_(do_syscall2
)(__NR_gettimeofday
, (UWord
)&tv_now
, (UWord
)NULL
);
1031 vg_assert(! sr_isError(res
));
1032 now
= tv_now
.tv_sec
* 1000000ULL + tv_now
.tv_usec
;
1036 # elif defined(VGO_freebsd)
1038 struct vki_timeval tv_now
;
1039 res
= VG_(do_syscall2
)(__NR_gettimeofday
, (UWord
)&tv_now
, (UWord
)NULL
);
1040 vg_assert(! sr_isError(res
));
1041 now
= tv_now
.tv_sec
* 1000000ULL + tv_now
.tv_usec
;
1043 # elif defined(VGO_darwin)
1044 // Weird: it seems that gettimeofday() doesn't fill in the timeval, but
1045 // rather returns the tv_sec as the low 32 bits of the result and the
1046 // tv_usec as the high 32 bits of the result. (But the timeval cannot be
1047 // NULL!) See bug 200990.
1049 struct vki_timeval tv_now
= { 0, 0 };
1050 res
= VG_(do_syscall2
)(__NR_gettimeofday
, (UWord
)&tv_now
, (UWord
)NULL
);
1051 vg_assert(! sr_isError(res
));
1052 now
= sr_Res(res
) * 1000000ULL + sr_ResHI(res
);
1056 # error "Unknown OS"
1063 return (now
- base
) / 1000;
1066 # if defined(VGO_linux) || defined(VGO_solaris) || defined(VGO_freebsd)
1067 void VG_(clock_gettime
) ( struct vki_timespec
*ts
, vki_clockid_t clk_id
)
1070 res
= VG_(do_syscall2
)(__NR_clock_gettime
, clk_id
,
1072 vg_assert (sr_isError(res
) == 0);
1074 # elif defined(VGO_darwin)
1075 /* See pub_tool_libcproc.h */
1077 # error "Unknown OS"
1080 Int
VG_(gettimeofday
)(struct vki_timeval
*tv
, struct vki_timezone
*tz
)
1083 res
= VG_(do_syscall2
)(__NR_gettimeofday
, (UWord
)tv
, (UWord
)tz
);
1085 if (! sr_isError(res
)) return 0;
1087 /* Make sure, argument values are deterministic upon failure */
1088 if (tv
) *tv
= (struct vki_timeval
){ .tv_sec
= 0, .tv_usec
= 0 };
1089 if (tz
) *tz
= (struct vki_timezone
){ .tz_minuteswest
= 0, .tz_dsttime
= 0 };
1094 UInt
VG_(get_user_milliseconds
)(void)
1097 # if defined(VGO_linux)
1099 struct vki_rusage ru
;
1100 VG_(memset
)(&ru
, 0, sizeof(ru
));
1101 SysRes sr
= VG_(do_syscall2
)(__NR_getrusage
, VKI_RUSAGE_SELF
, (UWord
)&ru
);
1102 if (!sr_isError(sr
)) {
1103 res
= ru
.ru_utime
.tv_sec
* 1000 + ru
.ru_utime
.tv_usec
/ 1000;
1107 # elif defined(VGO_solaris)
1109 struct vki_rusage ru
;
1110 VG_(memset
)(&ru
, 0, sizeof(ru
));
1111 SysRes sr
= VG_(do_syscall2
)(__NR_rusagesys
, VKI__RUSAGESYS_GETRUSAGE
,
1113 if (!sr_isError(sr
)) {
1114 res
= ru
.ru_utime
.tv_sec
* 1000 + ru
.ru_utime
.tv_usec
/ 1000;
1118 # elif defined(VGO_freebsd)
1120 struct vki_rusage ru
;
1121 VG_(memset
)(&ru
, 0, sizeof(ru
));
1122 SysRes sr
= VG_(do_syscall2
)(__NR_getrusage
, VKI_RUSAGE_SELF
, (UWord
)&ru
);
1123 if (!sr_isError(sr
)) {
1124 res
= ru
.ru_utime
.tv_sec
* 1000 + ru
.ru_utime
.tv_usec
/ 1000;
1128 # elif defined(VGO_darwin)
1132 # error "Unknown OS"
1139 /* ---------------------------------------------------------------------
1141 ------------------------------------------------------------------ */
1149 #define VG_MAX_ATFORK 10
1151 static struct atfork atforks
[VG_MAX_ATFORK
];
1152 static Int n_atfork
= 0;
1154 void VG_(atfork
)(vg_atfork_t pre
, vg_atfork_t parent
, vg_atfork_t child
)
1158 for (i
= 0; i
< n_atfork
; i
++) {
1159 if (atforks
[i
].pre
== pre
&&
1160 atforks
[i
].parent
== parent
&&
1161 atforks
[i
].child
== child
)
1165 if (n_atfork
>= VG_MAX_ATFORK
)
1167 "Too many VG_(atfork) handlers requested: raise VG_MAX_ATFORK");
1169 atforks
[n_atfork
].pre
= pre
;
1170 atforks
[n_atfork
].parent
= parent
;
1171 atforks
[n_atfork
].child
= child
;
1176 void VG_(do_atfork_pre
)(ThreadId tid
)
1180 for (i
= 0; i
< n_atfork
; i
++)
1181 if (atforks
[i
].pre
!= NULL
)
1182 (*atforks
[i
].pre
)(tid
);
1185 void VG_(do_atfork_parent
)(ThreadId tid
)
1189 for (i
= 0; i
< n_atfork
; i
++)
1190 if (atforks
[i
].parent
!= NULL
)
1191 (*atforks
[i
].parent
)(tid
);
1194 void VG_(do_atfork_child
)(ThreadId tid
)
1198 for (i
= 0; i
< n_atfork
; i
++)
1199 if (atforks
[i
].child
!= NULL
)
1200 (*atforks
[i
].child
)(tid
);
1203 /* ---------------------------------------------------------------------
1204 FreeBSD sysctlbyname, getosreldate, is32on64
1205 ------------------------------------------------------------------ */
1207 #if defined(VGO_freebsd)
1208 Int
VG_(sysctlbyname
)(const HChar
*name
, void *oldp
, SizeT
*oldlenp
, const void *newp
, SizeT newlen
)
1211 #if ((__FreeBSD_version >= 1201522 && __FreeBSD_version <= 1300000) || __FreeBSD_version >= 1300045)
1212 SysRes res
= VG_(do_syscall6
)(__NR___sysctlbyname
, (RegWord
)name
, VG_(strlen
)(name
), (RegWord
)oldp
, (RegWord
)oldlenp
, (RegWord
)newp
, (RegWord
)newlen
);
1213 return sr_isError(res
) ? -1 : sr_Res(res
);
1220 oid
[0] = 0; /* magic */
1221 oid
[1] = 3; /* undocumented */
1222 oidlen
= sizeof(real_oid
);
1223 error
= VG_(sysctl
)(oid
, 2, real_oid
, &oidlen
, name
, VG_(strlen
)(name
));
1226 oidlen
/= sizeof(int);
1227 error
= VG_(sysctl
)(real_oid
, oidlen
, oldp
, oldlenp
, newp
, newlen
);
1232 Int
VG_(getosreldate
)(void)
1234 static Int osreldate
= 0;
1237 if (osreldate
== 0) {
1238 osreldatel
= sizeof(osreldate
);
1239 VG_(sysctlbyname
)("kern.osreldate", &osreldate
, &osreldatel
, 0, 0);
1244 Bool
VG_(is32on64
)(void)
1246 #if defined(VGP_amd64_freebsd) || defined(VGP_arm64_freebsd)
1248 #elif defined(VGP_x86_freebsd)
1250 struct vg_stat stat_buf
;
1251 res
= VG_(stat
)("/libexec/ld-elf32.so.1", &stat_buf
);
1252 if (!sr_isError(res
)) {
1253 // file exists, we're running on amd64
1254 VG_(debugLog
)(1, "check-os-bitness", "i386 executable on amd64 kernel\n");
1257 VG_(debugLog
)(1, "check-os-bitness", "i386 executable on i386 kernel\n");
1261 # error Unknown platform
1266 /* ---------------------------------------------------------------------
1268 ------------------------------------------------------------------ */
1270 void VG_(invalidate_icache
) ( void *ptr
, SizeT nbytes
)
1272 if (nbytes
== 0) return; // nothing to do
1276 VG_(machine_get_VexArchInfo
)(NULL
, &vai
);
1278 // If I-caches are coherent, nothing needs to be done here
1279 if (vai
.hwcache_info
.icaches_maintain_coherence
) return;
1281 # if defined(VGA_ppc32) || defined(VGA_ppc64be) || defined(VGA_ppc64le)
1282 Addr startaddr
= (Addr
) ptr
;
1283 Addr endaddr
= startaddr
+ nbytes
;
1287 cls
= vai
.ppc_icache_line_szB
;
1290 vg_assert(cls
== 16 || cls
== 32 || cls
== 64 || cls
== 128);
1292 startaddr
&= ~(cls
- 1);
1293 for (addr
= startaddr
; addr
< endaddr
; addr
+= cls
) {
1294 __asm__
__volatile__("dcbst 0,%0" : : "r" (addr
));
1296 __asm__
__volatile__("sync");
1297 for (addr
= startaddr
; addr
< endaddr
; addr
+= cls
) {
1298 __asm__
__volatile__("icbi 0,%0" : : "r" (addr
));
1300 __asm__
__volatile__("sync; isync");
1302 # elif defined(VGP_arm_linux)
1303 /* ARM cache flushes are privileged, so we must defer to the kernel. */
1304 Addr startaddr
= (Addr
) ptr
;
1305 Addr endaddr
= startaddr
+ nbytes
;
1306 VG_(do_syscall2
)(__NR_ARM_cacheflush
, startaddr
, endaddr
);
1308 # elif defined(VGP_arm64_linux) || defined(VGP_arm64_freebsd)
1309 // This arm64_linux section of this function VG_(invalidate_icache)
1311 // https://github.com/armvixl/vixl/blob/master/src/a64/cpu-a64.cc
1312 // which has the following copyright notice:
1314 Copyright 2013, ARM Limited
1315 All rights reserved.
1317 Redistribution and use in source and binary forms, with or without
1318 modification, are permitted provided that the following conditions are met:
1320 * Redistributions of source code must retain the above copyright notice,
1321 this list of conditions and the following disclaimer.
1322 * Redistributions in binary form must reproduce the above copyright notice,
1323 this list of conditions and the following disclaimer in the documentation
1324 and/or other materials provided with the distribution.
1325 * Neither the name of ARM Limited nor the names of its contributors may be
1326 used to endorse or promote products derived from this software without
1327 specific prior written permission.
1329 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
1330 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1331 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1332 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
1333 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1334 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1335 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
1336 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
1337 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1338 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1341 // Ask what the I and D line sizes are
1343 // Copy the content of the cache type register to a core register.
1344 __asm__
__volatile__ ("mrs %[ctr], ctr_el0" // NOLINT
1345 : [ctr
] "=r" (read_mrs
));
1347 UInt cache_type_register
= read_mrs
;
1348 const Int kDCacheLineSizeShift
= 16;
1349 const Int kICacheLineSizeShift
= 0;
1350 const UInt kDCacheLineSizeMask
= 0xf << kDCacheLineSizeShift
;
1351 const UInt kICacheLineSizeMask
= 0xf << kICacheLineSizeShift
;
1353 // The cache type register holds the size of the I and D caches as a power of
1355 const UInt dcache_line_size_power_of_two
=
1356 (cache_type_register
& kDCacheLineSizeMask
) >> kDCacheLineSizeShift
;
1357 const UInt icache_line_size_power_of_two
=
1358 (cache_type_register
& kICacheLineSizeMask
) >> kICacheLineSizeShift
;
1360 const UInt dcache_line_size_
= 4 * (1 << dcache_line_size_power_of_two
);
1361 const UInt icache_line_size_
= 4 * (1 << icache_line_size_power_of_two
);
1363 Addr start
= (Addr
)ptr
;
1364 // Sizes will be used to generate a mask big enough to cover a pointer.
1365 Addr dsize
= (Addr
)dcache_line_size_
;
1366 Addr isize
= (Addr
)icache_line_size_
;
1368 // Cache line sizes are always a power of 2.
1369 Addr dstart
= start
& ~(dsize
- 1);
1370 Addr istart
= start
& ~(isize
- 1);
1371 Addr end
= start
+ nbytes
;
1373 __asm__
__volatile__ (
1374 // Clean every line of the D cache containing the target data.
1376 // dc : Data Cache maintenance
1378 // va : by (Virtual) Address
1379 // u : to the point of Unification
1380 // The point of unification for a processor is the point by which the
1381 // instruction and data caches are guaranteed to see the same copy of a
1382 // memory location. See ARM DDI 0406B page B2-12 for more information.
1383 "dc cvau, %[dline] \n\t"
1384 "add %[dline], %[dline], %[dsize] \n\t"
1385 "cmp %[dline], %[end] \n\t"
1387 // Barrier to make sure the effect of the code above is visible to the rest
1389 // dsb : Data Synchronisation Barrier
1390 // ish : Inner SHareable domain
1391 // The point of unification for an Inner Shareable shareability domain is
1392 // the point by which the instruction and data caches of all the processors
1393 // in that Inner Shareable shareability domain are guaranteed to see the
1394 // same copy of a memory location. See ARM DDI 0406B page B2-12 for more
1397 // Invalidate every line of the I cache containing the target data.
1399 // ic : instruction cache maintenance
1402 // u : to the point of unification
1403 "ic ivau, %[iline] \n\t"
1404 "add %[iline], %[iline], %[isize] \n\t"
1405 "cmp %[iline], %[end] \n\t"
1407 // Barrier to make sure the effect of the code above is visible to the rest
1410 // Barrier to ensure any prefetching which happened before this code is
1412 // isb : Instruction Synchronisation Barrier
1414 : [dline
] "+r" (dstart
),
1415 [iline
] "+r" (istart
)
1416 : [dsize
] "r" (dsize
),
1417 [isize
] "r" (isize
),
1419 // This code does not write to memory but without the dependency gcc might
1420 // move this code before the code is generated.
1424 # elif defined(VGA_mips32) || defined(VGA_mips64)
1425 SysRes sres
= VG_(do_syscall3
)(__NR_cacheflush
, (UWord
) ptr
,
1426 (UWord
) nbytes
, (UWord
) 3);
1427 vg_assert( !sr_isError(sres
) );
1429 # elif defined(VGA_nanomips)
1431 __builtin___clear_cache(ptr
, (char*)ptr
+ nbytes
);
1437 /* ---------------------------------------------------------------------
1439 ------------------------------------------------------------------ */
1441 void VG_(flush_dcache
) ( void *ptr
, SizeT nbytes
)
1443 /* Currently this is only required on ARM64. */
1444 # if defined(VGA_arm64)
1445 Addr startaddr
= (Addr
) ptr
;
1446 Addr endaddr
= startaddr
+ nbytes
;
1451 __asm__
__volatile__ ("mrs %0, ctr_el0" : "=r"(ctr_el0
));
1452 cls
= 4 * (1ULL << (0xF & (ctr_el0
>> 16)));
1455 vg_assert(cls
== 64 || cls
== 128);
1457 startaddr
&= ~(cls
- 1);
1458 for (addr
= startaddr
; addr
< endaddr
; addr
+= cls
) {
1459 __asm__
__volatile__("dc cvau, %0" : : "r" (addr
));
1461 __asm__
__volatile__("dsb ish");
1465 /*--------------------------------------------------------------------*/
1467 /*--------------------------------------------------------------------*/