Re: unshare dumps core on ppc64le due to use of vfork()

2025-10-18 Thread Denys Vlasenko
On Tue, Oct 7, 2025 at 7:20 PM Denys Vlasenko  wrote:
> On Sun, Sep 7, 2025 at 11:20 AM Ricardo Branco  wrote:
> >
> > To reproduce:
> >
> > $ unshare -mrpf sh
> > sh-5.3# Segmentation fault (core dumped)
>
> Works for me.

Scratch that. I reproduced it on ppc64le.

Please try current git.
___
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox


Re: unshare dumps core on ppc64le due to use of vfork()

2025-10-18 Thread Denys Vlasenko
On Sun, Sep 7, 2025 at 11:20 AM Ricardo Branco  wrote:
>
> To reproduce:
>
> $ unshare -mrpf sh
> sh-5.3# Segmentation fault (core dumped)

Works for me.

> It seems related to vfork(), which is called in lots of places.

vfork is VERY efficient. I had cases where programs were sped up 50x
by switching from fork+exec to vfork+exec.

But you do need to know exactly what you are doing when you use it.
You need to understand what is shared and what is not shared
between processes after vfork, and when exactly parent is unblocked.
(For example, with double vfork, you can inadvertently create two
concurrently running processes in one VM!)

> This simple program dumps core on ppc64le:
>
> #include 
> #include 
> #include 
> #include 
> int main(int argc, char *argv[])
> {
>  int child = vfork();
>  printf("Child: %d\n", child);
>
>  if (child) {
>  sleep(1);
>  int ret = waitpid(child,0, 0);
>  }
>  return EXIT_SUCCESS;
> }

Because this program is buggy. It is running libc cleanup code twice.
You must _not_ do that:
...
 if (child == 0) // we are child
 _exit(EXIT_SUCCESS);  //exit immediately, without any cleanup

 // we are parent
 waitpid(child, NULL, 0);
 return EXIT_SUCCESS;
}
___
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox