qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Bug 1907969] [NEW] linux-user/i386: Segfault when mixing threads and signals
@ 2020-12-13 16:23 The Lemon Man
  2020-12-16  8:59 ` [Bug 1907969] " The Lemon Man
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: The Lemon Man @ 2020-12-13 16:23 UTC (permalink / raw)
  To: qemu-devel

Public bug reported:

Given the following C program, qemu-i386 will surely and certainly segfault when executing it.
The problem is only noticeable if the program is statically linked to musl's libc and, as written
in the title, it only manifests when targeting i386.

Removing the pthread calls or the second raise() makes it not segfault.

The crash is in some part of the TCG-generated code, right when it tries to perform a
%gs-relative access.

If you want a quick way of cross-compiling this binary:

* Download a copy of the Zig compiler from https://ziglang.org/download/
* Compile it with
  `zig cc -target i386-linux-musl <C-FILE> -o <OUT>`

```
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <asm/prctl.h>
#include <sys/syscall.h>

void sig_func(int sig)
{
    write(1, "hi!\n", strlen("hi!\n"));
}

void func(void *p) { }

typedef void *(*F)(void *);

int main()
{
    pthread_t tid;

    struct sigaction action;
    action.sa_flags = 0;
    action.sa_handler = sig_func;

    if (sigaction(SIGUSR1, &action, NULL) == -1) {
        return 1;
    }

    // This works.
    raise(SIGUSR1);

    pthread_create(&tid, NULL, (F)func, NULL);
    pthread_join(tid, NULL);

    // This makes qemu segfault.
    raise(SIGUSR1);
}
```

** Affects: qemu
     Importance: Undecided
         Status: New

** Attachment added: "Compiled static binary"
   https://bugs.launchpad.net/bugs/1907969/+attachment/5443145/+files/muslt

-- 
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1907969

Title:
  linux-user/i386: Segfault when mixing threads and signals

Status in QEMU:
  New

Bug description:
  Given the following C program, qemu-i386 will surely and certainly segfault when executing it.
  The problem is only noticeable if the program is statically linked to musl's libc and, as written
  in the title, it only manifests when targeting i386.

  Removing the pthread calls or the second raise() makes it not
  segfault.

  The crash is in some part of the TCG-generated code, right when it tries to perform a
  %gs-relative access.

  If you want a quick way of cross-compiling this binary:

  * Download a copy of the Zig compiler from https://ziglang.org/download/
  * Compile it with
    `zig cc -target i386-linux-musl <C-FILE> -o <OUT>`

  ```
  #include <pthread.h>
  #include <signal.h>
  #include <stdio.h>
  #include <string.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <asm/prctl.h>
  #include <sys/syscall.h>

  void sig_func(int sig)
  {
      write(1, "hi!\n", strlen("hi!\n"));
  }

  void func(void *p) { }

  typedef void *(*F)(void *);

  int main()
  {
      pthread_t tid;

      struct sigaction action;
      action.sa_flags = 0;
      action.sa_handler = sig_func;

      if (sigaction(SIGUSR1, &action, NULL) == -1) {
          return 1;
      }

      // This works.
      raise(SIGUSR1);

      pthread_create(&tid, NULL, (F)func, NULL);
      pthread_join(tid, NULL);

      // This makes qemu segfault.
      raise(SIGUSR1);
  }
  ```

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1907969/+subscriptions


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug 1907969] Re: linux-user/i386: Segfault when mixing threads and signals
  2020-12-13 16:23 [Bug 1907969] [NEW] linux-user/i386: Segfault when mixing threads and signals The Lemon Man
@ 2020-12-16  8:59 ` The Lemon Man
  2020-12-17 12:46   ` Laurent Vivier
  2021-05-12 15:16 ` Thomas Huth
  2021-07-12  4:17 ` Launchpad Bug Tracker
  2 siblings, 1 reply; 5+ messages in thread
From: The Lemon Man @ 2020-12-16  8:59 UTC (permalink / raw)
  To: qemu-devel

I finally understand where the problem is.

Qemu's user-mode emulation maps guest threads to native ones by spawning a new native one
and running a forked copy of the CPUX86State in parallel with the main thread.

This works fine for pretty much every architecture but i386 where the GDT/LDT comes into
play: the two descriptor tables are shared among all the threads, mimicking the real hw
behaviour, but since no host task-switching is being performed the TLS entry in the GDT
become stale.

Raising a signal makes Qemu reload the GS segment from the GDT, that's why removing that
line makes the problem disappear.

The problem is also confined to musl libc because of an interesting implementation choice.
Once a thread dies Glibc adds the now unused stack to a queue in order to reuse it later,
while musl frees it right away when it's not needed anymore and, as a consequence, makes
Qemu segfault.

As luck has it, after spending too much time debugging this, I found somebody else already
stumbled across this problem and wrote a patch. 

https://patchew.org/QEMU/74c9f1d9-4497-31c2-34a7-e21bef7932bd@scieneer.com/mbox

Too bad the patch flew under the radar...

-- 
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1907969

Title:
  linux-user/i386: Segfault when mixing threads and signals

Status in QEMU:
  New

Bug description:
  Given the following C program, qemu-i386 will surely and certainly segfault when executing it.
  The problem is only noticeable if the program is statically linked to musl's libc and, as written
  in the title, it only manifests when targeting i386.

  Removing the pthread calls or the second raise() makes it not
  segfault.

  The crash is in some part of the TCG-generated code, right when it tries to perform a
  %gs-relative access.

  If you want a quick way of cross-compiling this binary:

  * Download a copy of the Zig compiler from https://ziglang.org/download/
  * Compile it with
    `zig cc -target i386-linux-musl <C-FILE> -o <OUT>`

  ```
  #include <pthread.h>
  #include <signal.h>
  #include <stdio.h>
  #include <string.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <asm/prctl.h>
  #include <sys/syscall.h>

  void sig_func(int sig)
  {
      write(1, "hi!\n", strlen("hi!\n"));
  }

  void func(void *p) { }

  typedef void *(*F)(void *);

  int main()
  {
      pthread_t tid;

      struct sigaction action;
      action.sa_flags = 0;
      action.sa_handler = sig_func;

      if (sigaction(SIGUSR1, &action, NULL) == -1) {
          return 1;
      }

      // This works.
      raise(SIGUSR1);

      pthread_create(&tid, NULL, (F)func, NULL);
      pthread_join(tid, NULL);

      // This makes qemu segfault.
      raise(SIGUSR1);
  }
  ```

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1907969/+subscriptions


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Bug 1907969] Re: linux-user/i386: Segfault when mixing threads and signals
  2020-12-16  8:59 ` [Bug 1907969] " The Lemon Man
@ 2020-12-17 12:46   ` Laurent Vivier
  0 siblings, 0 replies; 5+ messages in thread
From: Laurent Vivier @ 2020-12-17 12:46 UTC (permalink / raw)
  To: qemu-devel

Le 16/12/2020 à 09:59, The Lemon Man a écrit :
> I finally understand where the problem is.
> 
> Qemu's user-mode emulation maps guest threads to native ones by spawning a new native one
> and running a forked copy of the CPUX86State in parallel with the main thread.
> 
> This works fine for pretty much every architecture but i386 where the GDT/LDT comes into
> play: the two descriptor tables are shared among all the threads, mimicking the real hw
> behaviour, but since no host task-switching is being performed the TLS entry in the GDT
> become stale.
> 
> Raising a signal makes Qemu reload the GS segment from the GDT, that's why removing that
> line makes the problem disappear.
> 
> The problem is also confined to musl libc because of an interesting implementation choice.
> Once a thread dies Glibc adds the now unused stack to a queue in order to reuse it later,
> while musl frees it right away when it's not needed anymore and, as a consequence, makes
> Qemu segfault.
> 
> As luck has it, after spending too much time debugging this, I found somebody else already
> stumbled across this problem and wrote a patch. 
> 
> https://patchew.org/QEMU/74c9f1d9-4497-31c2-34a7-e21bef7932bd@scieneer.com/mbox
> 
> Too bad the patch flew under the radar...
> 

Could you add a Reviewed-by and/or a tested by to the patch on the ML?

Thanks,
Laurent

-- 
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1907969

Title:
  linux-user/i386: Segfault when mixing threads and signals

Status in QEMU:
  New

Bug description:
  Given the following C program, qemu-i386 will surely and certainly segfault when executing it.
  The problem is only noticeable if the program is statically linked to musl's libc and, as written
  in the title, it only manifests when targeting i386.

  Removing the pthread calls or the second raise() makes it not
  segfault.

  The crash is in some part of the TCG-generated code, right when it tries to perform a
  %gs-relative access.

  If you want a quick way of cross-compiling this binary:

  * Download a copy of the Zig compiler from https://ziglang.org/download/
  * Compile it with
    `zig cc -target i386-linux-musl <C-FILE> -o <OUT>`

  ```
  #include <pthread.h>
  #include <signal.h>
  #include <stdio.h>
  #include <string.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <asm/prctl.h>
  #include <sys/syscall.h>

  void sig_func(int sig)
  {
      write(1, "hi!\n", strlen("hi!\n"));
  }

  void func(void *p) { }

  typedef void *(*F)(void *);

  int main()
  {
      pthread_t tid;

      struct sigaction action;
      action.sa_flags = 0;
      action.sa_handler = sig_func;

      if (sigaction(SIGUSR1, &action, NULL) == -1) {
          return 1;
      }

      // This works.
      raise(SIGUSR1);

      pthread_create(&tid, NULL, (F)func, NULL);
      pthread_join(tid, NULL);

      // This makes qemu segfault.
      raise(SIGUSR1);
  }
  ```

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1907969/+subscriptions


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug 1907969] Re: linux-user/i386: Segfault when mixing threads and signals
  2020-12-13 16:23 [Bug 1907969] [NEW] linux-user/i386: Segfault when mixing threads and signals The Lemon Man
  2020-12-16  8:59 ` [Bug 1907969] " The Lemon Man
@ 2021-05-12 15:16 ` Thomas Huth
  2021-07-12  4:17 ` Launchpad Bug Tracker
  2 siblings, 0 replies; 5+ messages in thread
From: Thomas Huth @ 2021-05-12 15:16 UTC (permalink / raw)
  To: qemu-devel

The QEMU project is currently moving its bug tracking to another system.
For this we need to know which bugs are still valid and which could be
closed already. Thus we are setting the bug state to "Incomplete" now.

If the bug has already been fixed in the latest upstream version of QEMU,
then please close this ticket as "Fix released".

If it is not fixed yet and you think that this bug report here is still
valid, then you have two options:

1) If you already have an account on gitlab.com, please open a new ticket
for this problem in our new tracker here:

    https://gitlab.com/qemu-project/qemu/-/issues

and then close this ticket here on Launchpad (or let it expire auto-
matically after 60 days). Please mention the URL of this bug ticket on
Launchpad in the new ticket on GitLab.

2) If you don't have an account on gitlab.com and don't intend to get
one, but still would like to keep this ticket opened, then please switch
the state back to "New" or "Confirmed" within the next 60 days (other-
wise it will get closed as "Expired"). We will then eventually migrate
the ticket automatically to the new system (but you won't be the reporter
of the bug in the new system and thus you won't get notified on changes
anymore).

Thank you and sorry for the inconvenience.


** Tags added: i386 linux-user

** Changed in: qemu
       Status: New => Incomplete

-- 
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1907969

Title:
  linux-user/i386: Segfault when mixing threads and signals

Status in QEMU:
  Incomplete

Bug description:
  Given the following C program, qemu-i386 will surely and certainly segfault when executing it.
  The problem is only noticeable if the program is statically linked to musl's libc and, as written
  in the title, it only manifests when targeting i386.

  Removing the pthread calls or the second raise() makes it not
  segfault.

  The crash is in some part of the TCG-generated code, right when it tries to perform a
  %gs-relative access.

  If you want a quick way of cross-compiling this binary:

  * Download a copy of the Zig compiler from https://ziglang.org/download/
  * Compile it with
    `zig cc -target i386-linux-musl <C-FILE> -o <OUT>`

  ```
  #include <pthread.h>
  #include <signal.h>
  #include <stdio.h>
  #include <string.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <asm/prctl.h>
  #include <sys/syscall.h>

  void sig_func(int sig)
  {
      write(1, "hi!\n", strlen("hi!\n"));
  }

  void func(void *p) { }

  typedef void *(*F)(void *);

  int main()
  {
      pthread_t tid;

      struct sigaction action;
      action.sa_flags = 0;
      action.sa_handler = sig_func;

      if (sigaction(SIGUSR1, &action, NULL) == -1) {
          return 1;
      }

      // This works.
      raise(SIGUSR1);

      pthread_create(&tid, NULL, (F)func, NULL);
      pthread_join(tid, NULL);

      // This makes qemu segfault.
      raise(SIGUSR1);
  }
  ```

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1907969/+subscriptions


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug 1907969] Re: linux-user/i386: Segfault when mixing threads and signals
  2020-12-13 16:23 [Bug 1907969] [NEW] linux-user/i386: Segfault when mixing threads and signals The Lemon Man
  2020-12-16  8:59 ` [Bug 1907969] " The Lemon Man
  2021-05-12 15:16 ` Thomas Huth
@ 2021-07-12  4:17 ` Launchpad Bug Tracker
  2 siblings, 0 replies; 5+ messages in thread
From: Launchpad Bug Tracker @ 2021-07-12  4:17 UTC (permalink / raw)
  To: qemu-devel

[Expired for QEMU because there has been no activity for 60 days.]

** Changed in: qemu
       Status: Incomplete => Expired

-- 
You received this bug notification because you are a member of qemu-
devel-ml, which is subscribed to QEMU.
https://bugs.launchpad.net/bugs/1907969

Title:
  linux-user/i386: Segfault when mixing threads and signals

Status in QEMU:
  Expired

Bug description:
  Given the following C program, qemu-i386 will surely and certainly segfault when executing it.
  The problem is only noticeable if the program is statically linked to musl's libc and, as written
  in the title, it only manifests when targeting i386.

  Removing the pthread calls or the second raise() makes it not
  segfault.

  The crash is in some part of the TCG-generated code, right when it tries to perform a
  %gs-relative access.

  If you want a quick way of cross-compiling this binary:

  * Download a copy of the Zig compiler from https://ziglang.org/download/
  * Compile it with
    `zig cc -target i386-linux-musl <C-FILE> -o <OUT>`

  ```
  #include <pthread.h>
  #include <signal.h>
  #include <stdio.h>
  #include <string.h>
  #include <sys/types.h>
  #include <unistd.h>
  #include <asm/prctl.h>
  #include <sys/syscall.h>

  void sig_func(int sig)
  {
      write(1, "hi!\n", strlen("hi!\n"));
  }

  void func(void *p) { }

  typedef void *(*F)(void *);

  int main()
  {
      pthread_t tid;

      struct sigaction action;
      action.sa_flags = 0;
      action.sa_handler = sig_func;

      if (sigaction(SIGUSR1, &action, NULL) == -1) {
          return 1;
      }

      // This works.
      raise(SIGUSR1);

      pthread_create(&tid, NULL, (F)func, NULL);
      pthread_join(tid, NULL);

      // This makes qemu segfault.
      raise(SIGUSR1);
  }
  ```

To manage notifications about this bug go to:
https://bugs.launchpad.net/qemu/+bug/1907969/+subscriptions


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-07-12  4:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-13 16:23 [Bug 1907969] [NEW] linux-user/i386: Segfault when mixing threads and signals The Lemon Man
2020-12-16  8:59 ` [Bug 1907969] " The Lemon Man
2020-12-17 12:46   ` Laurent Vivier
2021-05-12 15:16 ` Thomas Huth
2021-07-12  4:17 ` Launchpad Bug Tracker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).