|
From: Steve V.
|
Valgrind currently ignores the child_sp parameter to clone when the flags
make it a fork like clone. This patch passes the child_sp down and uses
it to update the stack pointer in the new child.
Index: valgrind/coregrind/m_syswrap/priv_syswrap-linux.h
===================================================================
--- valgrind.orig/coregrind/m_syswrap/priv_syswrap-linux.h 2008-08-21 16:05:14.000000000 -0700
+++ valgrind/coregrind/m_syswrap/priv_syswrap-linux.h 2008-08-21 16:06:25.000000000 -0700
@@ -38,7 +38,7 @@
extern Addr ML_(allocstack) ( ThreadId tid );
extern void ML_(call_on_new_stack_0_1) ( Addr stack, Addr retaddr,
void (*f)(Word), Word arg1 );
-extern SysRes ML_(do_fork_clone) ( ThreadId tid, UInt flags,
+extern SysRes ML_(do_fork_clone) ( ThreadId tid, UInt flags, Addr child_esp,
Int* parent_tidptr, Int* child_tidptr );
Index: valgrind/coregrind/m_syswrap/syswrap-amd64-linux.c
===================================================================
--- valgrind.orig/coregrind/m_syswrap/syswrap-amd64-linux.c 2008-08-21 16:05:14.000000000 -0700
+++ valgrind/coregrind/m_syswrap/syswrap-amd64-linux.c 2008-08-21 16:06:25.000000000 -0700
@@ -432,6 +432,7 @@
SET_STATUS_from_SysRes(
ML_(do_fork_clone)(tid,
cloneflags, /* flags */
+ (Addr)ARG2, /* child ESP */
(Int *)ARG3, /* parent_tidptr */
(Int *)ARG4)); /* child_tidptr */
break;
Index: valgrind/coregrind/m_syswrap/syswrap-linux.c
===================================================================
--- valgrind.orig/coregrind/m_syswrap/syswrap-linux.c 2008-08-21 16:05:14.000000000 -0700
+++ valgrind/coregrind/m_syswrap/syswrap-linux.c 2008-08-21 16:13:08.000000000 -0700
@@ -51,6 +51,8 @@
#include "pub_core_signals.h"
#include "pub_core_syscall.h"
#include "pub_core_syswrap.h"
+#include "pub_core_machine.h" // VG_STACK_PTR
+
#include "priv_types_n_macros.h"
#include "priv_syswrap-generic.h"
@@ -297,7 +299,7 @@
/* Do a clone which is really a fork() */
-SysRes ML_(do_fork_clone) ( ThreadId tid, UInt flags,
+SysRes ML_(do_fork_clone) ( ThreadId tid, UInt flags, Addr child_esp,
Int* parent_tidptr, Int* child_tidptr )
{
vki_sigset_t fork_saved_mask;
@@ -331,6 +333,10 @@
if (!res.isError && res.res == 0) {
/* child */
+ ThreadState *const ctst = VG_(get_ThreadState)(tid);
+ if (child_esp) /* Set the stack pointer */
+ ctst->arch.vex.VG_STACK_PTR = child_esp;
+
VG_(do_atfork_child)(tid);
/* restore signal mask */
Index: valgrind/coregrind/m_syswrap/syswrap-ppc32-linux.c
===================================================================
--- valgrind.orig/coregrind/m_syswrap/syswrap-ppc32-linux.c 2008-08-21 16:05:14.000000000 -0700
+++ valgrind/coregrind/m_syswrap/syswrap-ppc32-linux.c 2008-08-21 16:06:25.000000000 -0700
@@ -1005,6 +1005,7 @@
SET_STATUS_from_SysRes(
ML_(do_fork_clone)(tid,
cloneflags, /* flags */
+ (Addr)ARG2, /* child SP */
(Int *)ARG3, /* parent_tidptr */
(Int *)ARG5)); /* child_tidptr */
break;
Index: valgrind/coregrind/m_syswrap/syswrap-ppc64-linux.c
===================================================================
--- valgrind.orig/coregrind/m_syswrap/syswrap-ppc64-linux.c 2008-08-21 16:05:14.000000000 -0700
+++ valgrind/coregrind/m_syswrap/syswrap-ppc64-linux.c 2008-08-21 16:06:25.000000000 -0700
@@ -984,6 +984,7 @@
SET_STATUS_from_SysRes(
ML_(do_fork_clone)(tid,
cloneflags, /* flags */
+ (Addr)ARG2, /* child SP */
(Int *)ARG3, /* parent_tidptr */
(Int *)ARG5)); /* child_tidptr */
break;
Index: valgrind/coregrind/m_syswrap/syswrap-x86-linux.c
===================================================================
--- valgrind.orig/coregrind/m_syswrap/syswrap-x86-linux.c 2008-08-21 16:05:14.000000000 -0700
+++ valgrind/coregrind/m_syswrap/syswrap-x86-linux.c 2008-08-21 16:06:25.000000000 -0700
@@ -923,6 +923,7 @@
SET_STATUS_from_SysRes(
ML_(do_fork_clone)(tid,
cloneflags, /* flags */
+ (Addr)ARG2, /* child ESP */
(Int *)ARG3, /* parent_tidptr */
(Int *)ARG5)); /* child_tidptr */
break;
Index: valgrind/none/tests/x86/clone-fork-child-sp.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ valgrind/none/tests/x86/clone-fork-child-sp.c 2008-08-21 16:06:25.000000000 -0700
@@ -0,0 +1,37 @@
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+int fn_child(void *argp)
+{
+ int *const p = (int *)argp;
+ printf("child stack=%p arg=%d\n", &argp, *p);
+ fflush(stdout);
+ return 0;
+}
+
+typedef void (*sighandler_t)(int);
+
+void handler(int signo)
+{
+ printf("handler signo=%d\n", signo);
+}
+
+int x = 39;
+
+int main(int argc, char *argv[])
+{
+ int status = 0;
+ sighandler_t rv = signal(SIGCHLD, handler);
+ void *stack = mmap((void *)0x300000, 4096, PROT_READ|PROT_WRITE,
+ MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+ int pid = clone(fn_child, 4096+ stack, 0 | SIGCHLD, &x, 0,0,0);
+ if (pid < 0)
+ perror("clone failed");
+ waitpid(pid, &status, 0);
+ printf("status= %x\n", status);
+ return 0;
+}
Index: valgrind/none/tests/x86/clone-fork-child-sp.stderr.exp
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ valgrind/none/tests/x86/clone-fork-child-sp.stderr.exp 2008-08-21 16:06:25.000000000 -0700
@@ -0,0 +1,3 @@
+
+
+
Index: valgrind/none/tests/x86/clone-fork-child-sp.stdout.exp
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ valgrind/none/tests/x86/clone-fork-child-sp.stdout.exp 2008-08-21 16:06:25.000000000 -0700
@@ -0,0 +1,3 @@
+child stack=0x300ff0 arg=39
+handler signo=17
+status= 0
Index: valgrind/none/tests/x86/clone-fork-child-sp.vgtest
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ valgrind/none/tests/x86/clone-fork-child-sp.vgtest 2008-08-21 16:06:25.000000000 -0700
@@ -0,0 +1 @@
+prog: clone-fork-child-sp
Index: valgrind/none/tests/x86/Makefile.am
===================================================================
--- valgrind.orig/none/tests/x86/Makefile.am 2008-08-21 16:05:14.000000000 -0700
+++ valgrind/none/tests/x86/Makefile.am 2008-08-21 16:06:25.000000000 -0700
@@ -47,6 +47,8 @@
movx.stderr.exp movx.stdout.exp movx.vgtest \
pushpopseg.stderr.exp pushpopseg.stdout.exp pushpopseg.vgtest \
sbbmisc.stderr.exp sbbmisc.stdout.exp sbbmisc.vgtest \
+ clone-fork-child-sp.stderr.exp clone-fork-child-sp.stdout.exp \
+ clone-fork-child-sp.vgtest \
seg_override.stderr.exp seg_override.stdout.exp seg_override.vgtest \
sigcontext.stdout.exp sigcontext.stderr.exp sigcontext.vgtest \
smc1.stderr.exp smc1.stdout.exp smc1.vgtest \
@@ -68,6 +70,7 @@
getseg incdec_alt $(INSN_TESTS) \
jcxz \
lahf looper movx int pushpopseg sbbmisc \
+ clone-fork-child-sp \
seg_override sigcontext smc1 yield
if BUILD_SSSE3_TESTS
check_PROGRAMS += ssse3_misaligned
|