Package: libc6-dev
Version: 2.24-9
Severity: grave

Dear Maintainer,

the program below shows the problem, happening on my Debian laptop.
When I create a child using clone(CLONE_VM), glibc's clone wrapper
does not update cached pid, and I receive parent's pid in getpid().
Here is output of the program:

$strace -o strace.log -f ./a.out
parent: pid=17530
parent: fork pid=17531
1)child: pid=17531
2)child: pid=17530

When CLONE_VM flag is not passed, the problem does not occur.
My case is not in the list of the cases, described in clone(2),
when wrong pid may be returned, so this is a BUG.

Regards,
Kirill

P.S. Strange, but reportbug-ng did not fill packages versions. So,
I do this manually:

$ dpkg-query -l 'libc6*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                          Version             Architecture        
Description
+++-=============================-===================-===================-================================================================
ii  libc6:amd64                   2.24-9              amd64               GNU C 
Library: Shared libraries
ii  libc6:i386                    2.24-9              i386                GNU C 
Library: Shared libraries
ii  libc6-amd64:i386              2.24-9              i386                GNU C 
Library: 64bit Shared libraries for AMD64
ii  libc6-arm64-cross             2.24-9cross3        all                 GNU C 
Library: Shared libraries (for cross-compiling)
un  libc6-arm64-dcv1              <none>              <none>              (no 
description available)
ii  libc6-dbg:amd64               2.24-9              amd64               GNU C 
Library: detached debugging symbols
ii  libc6-dev:amd64               2.24-9              amd64               GNU C 
Library: Development Libraries and Header Files
ii  libc6-dev-arm64-cross         2.24-9cross3        all                 GNU C 
Library: Development Libraries and Header Files (for cross
un  libc6-dev-arm64-dcv1          <none>              <none>              (no 
description available)
ii  libc6-dev-i386                2.24-9              amd64               GNU C 
Library: 32-bit development libraries for AMD64
ii  libc6-dev-x32                 2.24-9              amd64               GNU C 
Library: X32 ABI Development Libraries for AMD64
ii  libc6-i386                    2.24-9              amd64               GNU C 
Library: 32-bit shared libraries for AMD64
un  libc6-i686                    <none>              <none>              (no 
description available)
ii  libc6-x32                     2.24-9              amd64               GNU C 
Library: X32 ABI Shared libraries for AMD64
un  libc6.1                       <none>              <none>              (no 
description available)
un  libc6.1-dev                   <none>              <none>              (no 
description available)
un  libc6.1-dev-arm64-cross       <none>              <none>              (no 
description available)


#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/syscall.h>
#include <sys/mman.h>
#include <signal.h>

int child(void *a)
{
        printf("1)child: pid=%d\n", syscall(__NR_getpid));
        printf("2)child: pid=%d\n", getpid());
        return 0;
}

int main(void)
{
        int stack_size = 2 * 1024 * 1024;
        char *stack = mmap(NULL, stack_size, PROT_WRITE | PROT_READ, 
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
        pid_t pid;
        if (stack == MAP_FAILED) {
                perror("Can't allocate stack");
                exit(1);
        }

        setbuf(stdout, NULL);

        printf("parent: pid=%d\n", getpid());
        pid = clone(child, stack + stack_size, CLONE_VM | CLONE_FILES | 
SIGCHLD, NULL);
        printf("parent: fork pid=%d\n", pid);
}

--- System information. ---
Architecture: 
Kernel:       Linux 4.10.1+

Debian Release: 9.0
  500 unstable        ftp.ru.debian.org 
  500 testing         ftp.ru.debian.org 
  500 stable-updates  ftp.ru.debian.org 
  500 stable          security.debian.org 
  500 stable          ftp.ru.debian.org 
    1 experimental    ftp.ru.debian.org 

--- Package information. ---
Depends               (Version) | Installed
===============================-+-=============
libc6                (= 2.24-9) | 
libc-dev-bin         (= 2.24-9) | 
linux-libc-dev     (>= 4.9.2-2) | 


Package's Recommends field is empty.

Suggests          (Version) | Installed
===========================-+-===========
glibc-doc                   | 
manpages-dev                | 4.09-2
17530 execve("./a.out", ["./a.out"], [/* 48 vars */]) = 0
17530 brk(NULL)                         = 0x123e000
17530 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
17530 mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb686e47000
17530 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
17530 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
17530 fstat(3, {st_mode=S_IFREG|0644, st_size=190344, ...}) = 0
17530 mmap(NULL, 190344, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fb686e18000
17530 close(3)                          = 0
17530 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
17530 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
17530 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\3\2\0\0\0\0\0"..., 832) = 832
17530 fstat(3, {st_mode=S_IFREG|0755, st_size=1685264, ...}) = 0
17530 mmap(NULL, 3791264, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fb686889000
17530 mprotect(0x7fb686a1e000, 2093056, PROT_NONE) = 0
17530 mmap(0x7fb686c1d000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x194000) = 0x7fb686c1d000
17530 mmap(0x7fb686c23000, 14752, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fb686c23000
17530 close(3)                          = 0
17530 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb686e16000
17530 arch_prctl(ARCH_SET_FS, 0x7fb686e16700) = 0
17530 mprotect(0x7fb686c1d000, 16384, PROT_READ) = 0
17530 mprotect(0x600000, 4096, PROT_READ) = 0
17530 mprotect(0x7fb686e4a000, 4096, PROT_READ) = 0
17530 munmap(0x7fb686e18000, 190344)    = 0
17530 mmap(NULL, 2097152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb686689000
17530 getpid()                          = 17530
17530 write(1, "parent: pid=17530\n", 18) = 18
17530 clone(child_stack=0x7fb686888ff0, flags=CLONE_VM|CLONE_FILES|SIGCHLD) = 17531
17531 getpid( <unfinished ...>
17530 write(1, "parent: fork pid=17531\n", 23 <unfinished ...>
17531 <... getpid resumed> )            = 17531
17530 <... write resumed> )             = 23
17530 exit_group(0)                     = ?
17531 write(1, "1)child: pid=17531\n", 19 <unfinished ...>
17530 +++ exited with 0 +++
17531 <... write resumed> )             = 19
17531 write(1, "2)child: pid=17530\n", 19) = 19
17531 exit_group(0)                     = ?
17531 +++ exited with 0 +++

Reply via email to