From patchwork Mon Apr 18 14:45:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 66029 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp1326667qge; Mon, 18 Apr 2016 07:46:13 -0700 (PDT) X-Received: by 10.98.11.78 with SMTP id t75mr49536858pfi.72.1460990773857; Mon, 18 Apr 2016 07:46:13 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id e3si3912396pap.82.2016.04.18.07.46.13 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 18 Apr 2016 07:46:13 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-68949-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-68949-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-68949-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=cQTT04BL+XuDwiwc3p97o6vkUIPfjavsUwf7mNB3NCNyfoSCvc4Gx UpRgtOjP1us2gKTzkiO+wqUU8aMCfVSMCNnEIcywcdz4LuIumejn17qd6sUPPf0S fY2/AfLJ5wE+aHuEG53jVBhIQ7GJpqh5rsOkRhWdqLfUMtb3SiFvgM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=RTSZ/erhhiIIQ8VLH1lufyPlwFE=; b=xCsZ14vAQ0tesnbwx8wnsEC8Tx02 PSzqJ4vS8hQYIWI3G+qxyeV/kBNfdn/qMkoY+qlXxIP8xIbKTBeqJW/Dj/C9Y+G0 5PJcBaOvk145MmcuQEZsZ+XI23kb+uak+AZewIzrSXCeTpmLjyjTxmPgVCewONHw z8ye//vpPlix0Ss= Received: (qmail 90479 invoked by alias); 18 Apr 2016 14:46:00 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 90160 invoked by uid 89); 18 Apr 2016 14:46:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=ear, 7910, 79, 10, overwrite X-HELO: mail-yw0-f182.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=uE0h4hmW3bJopp/2Q08pW3mwpgiG4LxPIuVRnElVLoY=; b=GQXChfnEuwAROjIuah3JvO6+4jsEudRB42LGgsgtZEgRPYSlT0G5RDs+oxo5py63GF iYTGyHNpkcr1Uyuey0SCR/p8g3tn4aQZlU0LQzNkfqYCjWvqjt+eLz2jvjceosi9FnIL vFrPdpfsFa2M3Zw/wzbAkcytTnaoky5Zu3jewfhkn/+280/Xp0sB78DRah4DvnRwRl28 BRxjiOf2OO1x9f8UlE+6uNaNA+4pxb72/G8B8x9gBgqvWHDrI6RM05rUKSTYSWsQwuVj uRRRyTE5mCVXuDgR+16srUFopgKQbVT9Ya0tUAD5MUiAZlRkbAin/QlKl8PF7pT5eHD3 5BYA== X-Gm-Message-State: AOPr4FVRfezcWX7Lrwj5ZppR24UHEolTj0EZNgAUrEikeyZ3AtptZPMhsGnvUSHoSGP4omzo X-Received: by 10.37.89.70 with SMTP id n67mr18759625ybb.16.1460990745041; Mon, 18 Apr 2016 07:45:45 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2] Fix clone (CLONE_VM) pid/tid reset (BZ#19957) Date: Mon, 18 Apr 2016 11:45:37 -0300 Message-Id: <1460990737-16469-1-git-send-email-adhemerval.zanella@linaro.org> Changes from previous version: * Changed the flags to check: now only CLONE_VM is tested, since CLONE_THREAD implies CLONE_VM; * Added mips, s390, and sparc fixes. * Added testcase. -- As discussed in libc-alpha [1] current clone with CLONE_VM (without CLONE_THREAD set) will reset the pthread pid/tid fields to -1. The issue is since memory is shared between the parent and child it will clobber parent's cached pid/tid leading to internal inconsistencies if the value is not restored. And even it is restored it may lead to racy conditions when between set/restore a thread might invoke pthread function that validate the pthread with INVALID_TD_P/INVALID_NOT_TERMINATED_TD_P and thus get wrong results. As stated in BZ19957, previously reports of this behaviour was close with EWONTFIX due the fact usage of clone outside glibc is tricky since glibc requires consistent internal pthread, while using clone directly may not provide it. However since now posix_spawn uses clone (CLONE_VM) to fixes various issues related to previous vfork usage this issue requires fixing. The vfork implementation also does something similar, but instead it negates and restores only the *pid* field and functions that might access its value know to handle such case (getpid, raise and pthread ones that uses INVALID_TD_P/INVALID_NOT_TERMINATED_TD_P macros that check only *tid* field). Also vfork does not call __clone directly, instead calling either __NR_vfork or __NR_clone directly. So this patch removes this clone behavior by avoiding setting the pthread pid/tid field for CLONE_VM. There is no need to check for CLONE_THREAD, since the minimum supported kernel in all architecture implies that CLONE_VM must be used with CLONE_THREAD, otherwise clone returns EINVAL. Instead of current approach of: int clone(int (*fn)(void *), void *child_stack, int flags, ...) [...] if (flags & CLONE_THREAD) goto do_syscall; pid_t new_value; if (flags & CLONE_VM) new_value = -1; else new_value = getpid (); THREAD_SETMEM (THREAD_SELF, pid, new_value); THREAD_SETMEM (THREAD_SELF, tid, new_value); do_syscall: [...] The new approach uses: int clone(int (*fn)(void *), void *child_stack, int flags, ...) [...] if (flags & CLONE_VM) goto do_syscall; pid_t new_value = getpid (); THREAD_SETMEM (THREAD_SELF, pid, new_value); THREAD_SETMEM (THREAD_SELF, tid, new_value); do_syscall: [...] It also removes the linux tst-getpid2.c test which expects the previous behavior and instead add another clone test. Tested on x86_64, i686, x32, powerpc64le, aarch64, armhf, s390, and s390x. I also did limited check on mips32 and sparc64 (using the new added test). If would like to ask for some help on fixing the remaning ones: alpha, microblaze, sh, ia64, tile, hppa, m68k, and nio2. [1] https://sourceware.org/ml/libc-alpha/2016-04/msg00307.html * sysdeps/unix/sysv/linux/Makefile [$(subdir) == nptl] (test): Remove tst-getpid2. (test): Add tst-clone2. * sysdeps/unix/sysv/linux/tst-clone2.c: New file. * sysdeps/unix/sysv/linux/aarch64/clone.S (__clone): Do not change pid/tid fields for CLONE_VM. * sysdeps/unix/sysv/linux/arm/clone.S: Likewise. * sysdeps/unix/sysv/linux/i386/clone.S: Likewise. * sysdeps/unix/sysv/linux/mips/clone.S: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S: Likewise. * sysdeps/unix/sysv/linux/s390/s390-32/clone.S: Likewise. * sysdeps/unix/sysv/linux/s390/s390-64/clone.S: Likewise. * sysdeps/unix/sysv/linux/sparc/sparc32/clone.S: Likewise. * sysdeps/unix/sysv/linux/sparc/sparc64/clone.S: Likewise. * sysdeps/unix/sysv/linux/x86_64/clone.S: Likewise. * sysdeps/unix/sysv/linux/tst-getpid2.c: Remove file. --- ChangeLog | 20 +++ sysdeps/unix/sysv/linux/Makefile | 4 +- sysdeps/unix/sysv/linux/aarch64/clone.S | 8 +- sysdeps/unix/sysv/linux/arm/clone.S | 11 +- sysdeps/unix/sysv/linux/i386/clone.S | 6 +- sysdeps/unix/sysv/linux/mips/clone.S | 9 +- sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S | 10 +- sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S | 8 +- sysdeps/unix/sysv/linux/s390/s390-32/clone.S | 7 +- sysdeps/unix/sysv/linux/s390/s390-64/clone.S | 7 +- sysdeps/unix/sysv/linux/sparc/sparc32/clone.S | 8 +- sysdeps/unix/sysv/linux/sparc/sparc64/clone.S | 9 +- sysdeps/unix/sysv/linux/tst-clone2.c | 178 ++++++++++++++++++++++ sysdeps/unix/sysv/linux/tst-getpid2.c | 2 - sysdeps/unix/sysv/linux/x86_64/clone.S | 8 +- 15 files changed, 226 insertions(+), 69 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/tst-clone2.c delete mode 100644 sysdeps/unix/sysv/linux/tst-getpid2.c -- 1.9.1 diff --git a/ChangeLog b/ChangeLog index 242e72c..9b29844 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2016-04-16 Adhemerval Zanella + + * sysdeps/unix/sysv/linux/Makefile [$(subdir) == nptl] (test): Remove + tst-getpid2. + (test): Add tst-clone2. + * sysdeps/unix/sysv/linux/tst-clone2.c: New file. + * sysdeps/unix/sysv/linux/aarch64/clone.S (__clone): Do not change + pid/tid fields for CLONE_VM. + * sysdeps/unix/sysv/linux/arm/clone.S: Likewise. + * sysdeps/unix/sysv/linux/i386/clone.S: Likewise. + * sysdeps/unix/sysv/linux/mips/clone.S: Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S: Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S: Likewise. + * sysdeps/unix/sysv/linux/s390/s390-32/clone.S: Likewise. + * sysdeps/unix/sysv/linux/s390/s390-64/clone.S: Likewise. + * sysdeps/unix/sysv/linux/sparc/sparc32/clone.S: Likewise. + * sysdeps/unix/sysv/linux/sparc/sparc64/clone.S: Likewise. + * sysdeps/unix/sysv/linux/x86_64/clone.S: Likewise. + * sysdeps/unix/sysv/linux/tst-getpid2.c: Remove file. + 2016-04-16 Robin van der Vliet [BZ #19400] diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 9999600..9b4e2e1 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -42,7 +42,7 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \ bits/socket_type.h bits/syscall.h bits/sysctl.h \ bits/mman-linux.h -tests += tst-clone tst-fanotify tst-personality +tests += tst-clone tst-clone2 tst-fanotify tst-personality # Generate the list of SYS_* macros for the system calls (__NR_* macros). @@ -194,7 +194,7 @@ CFLAGS-gai.c += -DNEED_NETLINK endif ifeq ($(subdir),nptl) -tests += tst-setgetname tst-align-clone tst-getpid1 tst-getpid2 \ +tests += tst-setgetname tst-align-clone tst-getpid1 \ tst-thread-affinity-pthread tst-thread-affinity-pthread2 \ tst-thread-affinity-sched endif diff --git a/sysdeps/unix/sysv/linux/aarch64/clone.S b/sysdeps/unix/sysv/linux/aarch64/clone.S index 8f3ab40..76baa7a 100644 --- a/sysdeps/unix/sysv/linux/aarch64/clone.S +++ b/sysdeps/unix/sysv/linux/aarch64/clone.S @@ -72,17 +72,15 @@ thread_start: cfi_undefined (x30) mov x29, 0 - tbnz x11, #CLONE_THREAD_BIT, 3f - mov x0, #-1 - tbnz x11, #CLONE_VM_BIT, 2f + tbnz x11, #CLONE_VM_BIT, 1f + mov x8, #SYS_ify(getpid) svc 0x0 -2: mrs x1, tpidr_el0 sub x1, x1, #PTHREAD_SIZEOF str w0, [x1, #PTHREAD_PID_OFFSET] str w0, [x1, #PTHREAD_TID_OFFSET] -3: +1: /* Pick the function arg and execute. */ mov x0, x12 diff --git a/sysdeps/unix/sysv/linux/arm/clone.S b/sysdeps/unix/sysv/linux/arm/clone.S index c103123..7ff6818 100644 --- a/sysdeps/unix/sysv/linux/arm/clone.S +++ b/sysdeps/unix/sysv/linux/arm/clone.S @@ -70,19 +70,16 @@ PSEUDO_END (__clone) 1: .fnstart .cantunwind - tst ip, #CLONE_THREAD - bne 3f + tst ip, #CLONE_VM + bne 2f GET_TLS (lr) mov r1, r0 - tst ip, #CLONE_VM ldr r7, =SYS_ify(getpid) - ite ne - movne r0, #-1 - swieq 0x0 + swi 0x0 NEGOFF_ADJ_BASE (r1, TID_OFFSET) str r0, NEGOFF_OFF1 (r1, TID_OFFSET) str r0, NEGOFF_OFF2 (r1, PID_OFFSET, TID_OFFSET) -3: +2: @ pick the function arg and call address off the stack and execute ldr r0, [sp, #4] ldr ip, [sp], #8 diff --git a/sysdeps/unix/sysv/linux/i386/clone.S b/sysdeps/unix/sysv/linux/i386/clone.S index ef447d1..25f2a9c 100644 --- a/sysdeps/unix/sysv/linux/i386/clone.S +++ b/sysdeps/unix/sysv/linux/i386/clone.S @@ -40,7 +40,6 @@ #define SYS_clone 120 #define CLONE_VM 0x00000100 -#define CLONE_THREAD 0x00010000 .text ENTRY (__clone) @@ -108,7 +107,7 @@ L(thread_start): cfi_undefined (eip); /* Note: %esi is zero. */ movl %esi,%ebp /* terminate the stack frame */ - testl $CLONE_THREAD, %edi + testl $CLONE_VM, %edi je L(newpid) L(haspid): call *%ebx @@ -124,9 +123,6 @@ L(here): .subsection 2 L(newpid): - testl $CLONE_VM, %edi - movl $-1, %eax - jne L(nomoregetpid) movl $SYS_ify(getpid), %eax ENTER_KERNEL L(nomoregetpid): diff --git a/sysdeps/unix/sysv/linux/mips/clone.S b/sysdeps/unix/sysv/linux/mips/clone.S index feb8250..39634c5 100644 --- a/sysdeps/unix/sysv/linux/mips/clone.S +++ b/sysdeps/unix/sysv/linux/mips/clone.S @@ -131,9 +131,8 @@ L(thread_start): /* The stackframe has been created on entry of clone(). */ /* Check and see if we need to reset the PID. */ - LONG_L a0,(PTRSIZE*2)(sp) - and a1,a0,CLONE_THREAD - beqz a1,L(restore_pid) + and a1,a0,CLONE_VM + beqz a1,L(restore_pid) L(donepid): /* Restore the arg for user's function. */ @@ -153,12 +152,8 @@ L(donepid): #endif L(restore_pid): - and a1,a0,CLONE_VM - li v0,-1 - bnez a1,L(gotpid) li v0,__NR_getpid syscall -L(gotpid): READ_THREAD_POINTER(v1) INT_S v0,PID_OFFSET(v1) INT_S v0,TID_OFFSET(v1) diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S b/sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S index eb973db..3761ded 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S @@ -76,13 +76,11 @@ ENTRY (__clone) crandc cr1*4+eq,cr1*4+eq,cr0*4+so bne- cr1,L(parent) /* The '-' is to minimise the race. */ - andis. r0,r28,CLONE_THREAD>>16 - bne+ r0,L(oldpid) - andi. r0,r28,CLONE_VM - li r3,-1 - bne- r0,L(nomoregetpid) + /* If CLONE_VM is set does not update the pid/tid field. */ + andi. r0,r29,CLONE_VM + bne+ cr0,L(oldpid) + DO_CALL(SYS_ify(getpid)) -L(nomoregetpid): stw r3,TID(r2) stw r3,PID(r2) L(oldpid): diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S b/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S index 959fdb7..7c59b9b 100644 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S +++ b/sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S @@ -78,13 +78,11 @@ ENTRY (__clone) crandc cr1*4+eq,cr1*4+eq,cr0*4+so bne- cr1,L(parent) /* The '-' is to minimise the race. */ - andis. r0,r29,CLONE_THREAD>>16 + /* If CLONE_VM is set do not update the pid/tid field. */ + rldicl. r0,r29,56,63 /* flags & CLONE_VM. */ bne+ cr0,L(oldpid) - andi. r0,r29,CLONE_VM - li r3,-1 - bne- cr0,L(nomoregetpid) + DO_CALL(SYS_ify(getpid)) -L(nomoregetpid): stw r3,TID(r13) stw r3,PID(r13) L(oldpid): diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/clone.S b/sysdeps/unix/sysv/linux/s390/s390-32/clone.S index f774e90..2f8fa0b 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-32/clone.S +++ b/sysdeps/unix/sysv/linux/s390/s390-32/clone.S @@ -54,13 +54,10 @@ error: PSEUDO_END (__clone) thread_start: - tmh %r3,1 /* CLONE_THREAD == 0x00010000 */ - jne 1f - lhi %r2,-1 tml %r3,256 /* CLONE_VM == 0x00000100 */ - jne 2f + jne 1f svc SYS_ify(getpid) -2: ear %r3,%a0 + ear %r3,%a0 st %r2,PID(%r3) st %r2,TID(%r3) 1: diff --git a/sysdeps/unix/sysv/linux/s390/s390-64/clone.S b/sysdeps/unix/sysv/linux/s390/s390-64/clone.S index 928a881..fb81692 100644 --- a/sysdeps/unix/sysv/linux/s390/s390-64/clone.S +++ b/sysdeps/unix/sysv/linux/s390/s390-64/clone.S @@ -55,13 +55,10 @@ error: PSEUDO_END (__clone) thread_start: - tmh %r3,1 /* CLONE_THREAD == 0x00010000 */ + tmll %r3,256 /* CLONE_VM == 0x00000100 */ jne 1f - lhi %r2,-1 - tml %r3,256 /* CLONE_VM == 0x00000100 */ - jne 2f svc SYS_ify(getpid) -2: ear %r3,%a0 + ear %r3,%a0 sllg %r3,%r3,32 ear %r3,%a1 st %r2,PID(%r3) diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/clone.S b/sysdeps/unix/sysv/linux/sparc/sparc32/clone.S index 68f8202..d6c92f6 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/clone.S +++ b/sysdeps/unix/sysv/linux/sparc/sparc32/clone.S @@ -25,7 +25,6 @@ #include #define CLONE_VM 0x00000100 -#define CLONE_THREAD 0x00010000 /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg, pid_t *ptid, void *tls, pid_t *ctid); */ @@ -80,15 +79,10 @@ END(__clone) .type __thread_start,@function __thread_start: - sethi %hi(CLONE_THREAD), %l0 - andcc %g4, %l0, %g0 + andcc %g4, CLONE_VM, %g0 bne 1f - andcc %g4, CLONE_VM, %g0 - bne,a 2f - mov -1,%o0 set __NR_getpid,%g1 ta 0x10 -2: st %o0,[%g7 + PID] st %o0,[%g7 + TID] 1: diff --git a/sysdeps/unix/sysv/linux/sparc/sparc64/clone.S b/sysdeps/unix/sysv/linux/sparc/sparc64/clone.S index cecffa7..b0f6266 100644 --- a/sysdeps/unix/sysv/linux/sparc/sparc64/clone.S +++ b/sysdeps/unix/sysv/linux/sparc/sparc64/clone.S @@ -25,7 +25,6 @@ #include #define CLONE_VM 0x00000100 -#define CLONE_THREAD 0x00010000 /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg, pid_t *ptid, void *tls, pid_t *ctid); */ @@ -77,15 +76,11 @@ END(__clone) .type __thread_start,@function __thread_start: - sethi %hi(CLONE_THREAD), %l0 - andcc %g4, %l0, %g0 + andcc %g4, CLONE_VM, %g0 bne,pt %icc, 1f - andcc %g4, CLONE_VM, %g0 - bne,a,pn %icc, 2f - mov -1,%o0 set __NR_getpid,%g1 ta 0x6d -2: st %o0,[%g7 + PID] + st %o0,[%g7 + PID] st %o0,[%g7 + TID] 1: mov %g0, %fp /* terminate backtrace */ diff --git a/sysdeps/unix/sysv/linux/tst-clone2.c b/sysdeps/unix/sysv/linux/tst-clone2.c new file mode 100644 index 0000000..7452a47 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-clone2.c @@ -0,0 +1,178 @@ +/* Test if CLONE_VM does not change pthread pid/tid field. + Copyright (C) 2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include /* for THREAD_* macros. */ + +static int sig; +static int pipefd[2]; + +static int +f (void *a) +{ + close (pipefd[0]); + + pid_t pid = THREAD_GETMEM (THREAD_SELF, pid); + pid_t tid = THREAD_GETMEM (THREAD_SELF, tid); + + while (write (pipefd[1], &pid, sizeof pid) < 0) + continue; + while (write (pipefd[1], &tid, sizeof tid) < 0) + continue; + + return 0; +} + + +static int +clone_test (int clone_flags) +{ + sig = SIGRTMIN; + sigset_t ss; + sigemptyset (&ss); + sigaddset (&ss, sig); + if (sigprocmask (SIG_BLOCK, &ss, NULL) != 0) + { + printf ("sigprocmask failed: %m\n"); + return 1; + } + + if (pipe2 (pipefd, O_CLOEXEC)) + { + printf ("sigprocmask failed: %m\n"); + return 1; + } + + pid_t ppid = getpid (); + +#ifdef __ia64__ + extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base, + size_t __child_stack_size, int __flags, + void *__arg, ...); + char st[256 * 1024] __attribute__ ((aligned)); + pid_t p = __clone2 (f, st, sizeof (st), clone_flags, 0); +#else + char st[128 * 1024] __attribute__ ((aligned)); +#if _STACK_GROWS_DOWN + pid_t p = clone (f, st + sizeof (st), clone_flags, 0); +#elif _STACK_GROWS_UP + pid_t p = clone (f, st, clone_flags, 0); +#else +#error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP" +#endif +#endif + close (pipefd[1]); + + if (p == -1) + { + printf ("clone failed: %m\n"); + return 1; + } + + pid_t pid, tid; + if (read (pipefd[0], &pid, sizeof pid) != sizeof pid) + { + printf ("read pid failed: %m\n"); + kill (p, SIGKILL); + return 1; + } + if (read (pipefd[0], &tid, sizeof tid) != sizeof tid) + { + printf ("read pid failed: %m\n"); + kill (p, SIGKILL); + return 1; + } + + close (pipefd[0]); + + int ret = 0; + + /* For CLONE_VM glibc clone implementation does not change the pthread + pid/tid field. */ + if ((clone_flags & CLONE_VM) == CLONE_VM) + { + if ((ppid != pid) || (ppid != tid)) + { + printf ("parent pid (%i) != received pid/tid (%i/%i)\n", + (int)ppid, (int)pid, (int)tid); + ret = 1; + } + } + /* For any other flag clone updates the new pthread pid and tid with + the clone return value. */ + else + { + if ((p != pid) || (p != tid)) + { + printf ("child pid (%i) != received pid/tid (%i/%i)\n", + (int)p, (int)pid, (int)tid); + ret = 1; + } + } + + int e; + if (waitpid (p, &e, __WCLONE) != p) + { + puts ("waitpid failed"); + kill (p, SIGKILL); + return 1; + } + if (!WIFEXITED (e)) + { + if (WIFSIGNALED (e)) + printf ("died from signal %s\n", strsignal (WTERMSIG (e))); + else + puts ("did not terminate correctly"); + return 1; + } + if (WEXITSTATUS (e) != 0) + { + printf ("exit code %d\n", WEXITSTATUS (e)); + return 1; + } + + return ret; +} + +int +do_test (void) +{ + /* It first checks the clone implementation without any flag, which will + make the child new pid to be cached on its pthread structure. */ + int ret = clone_test (0); + /* Then check if clone with CLONE_VM avoid caching it since memory is + shared between parent and it might overwrite parent's pthread + structure. */ + ret += clone_test (CLONE_VM); + return ret; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/unix/sysv/linux/tst-getpid2.c b/sysdeps/unix/sysv/linux/tst-getpid2.c deleted file mode 100644 index fc98cb6..0000000 --- a/sysdeps/unix/sysv/linux/tst-getpid2.c +++ /dev/null @@ -1,2 +0,0 @@ -#define TEST_CLONE_FLAGS CLONE_VM -#include "tst-getpid1.c" diff --git a/sysdeps/unix/sysv/linux/x86_64/clone.S b/sysdeps/unix/sysv/linux/x86_64/clone.S index 1a50957..66f4b11 100644 --- a/sysdeps/unix/sysv/linux/x86_64/clone.S +++ b/sysdeps/unix/sysv/linux/x86_64/clone.S @@ -24,7 +24,6 @@ #include #define CLONE_VM 0x00000100 -#define CLONE_THREAD 0x00010000 /* The userland implementation is: int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg), @@ -92,14 +91,11 @@ L(thread_start): the outermost frame obviously. */ xorl %ebp, %ebp - testq $CLONE_THREAD, %rdi + andq $CLONE_VM, %rdi jne 1f - testq $CLONE_VM, %rdi - movl $-1, %eax - jne 2f movl $SYS_ify(getpid), %eax syscall -2: movl %eax, %fs:PID + movl %eax, %fs:PID movl %eax, %fs:TID 1: