Skip to contentSkip to conversation

gdbstub: handle a potentially racing TaskState

RFC PATCH In review Alex Bennée 2 participants 2 messages Nov 19, 2021 – Nov 20, 2021

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 gdbstub: handle a potentially racing TaskState

When dealing with multi-threaded userspace programs there is a race condition with the addition of cpu->opaque (aka TaskState). This is due to cpu_copy calling cpu_create which updates the global vCPU list. However the task state isn't set until later. This shouldn't be a problem because the new thread can't have executed anything yet but the gdbstub code does liberally iterate through the CPU list in various places.

This sticking plaster ensure the not yet fully realized vCPU is given an pid of -1 which should be enough to ensure it doesn't show up anywhere else.

In the longer term I think the code that manages the association between vCPUs and attached GDB processes could do with a clean-up and re-factor.

Cc Richard Henderson <[email protected]>

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

---
 gdbstub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
gdbstub.c +1 -1
index 23baaef40e..141d7bc4ec 100644
@@ -94,7 +94,7 @@ static inline int cpu_gdb_index(CPUState *cpu)
 {
 #if defined(CONFIG_USER_ONLY)
     TaskState *ts = (TaskState *) cpu->opaque;
-    return ts->ts_tid;
+    return ts ? ts->ts_tid : -1;
 #else
     return cpu->cpu_index + 1;
 #endif
Signature · 1 line
2.30.2
Tested-by Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Alex Bennée

When dealing with multi-threaded userspace programs there is a race condition with the addition of cpu->opaque (aka TaskState). This is due to cpu_copy calling cpu_create which updates the global vCPU list. However the task state isn't set until later. This shouldn't be a problem because the new thread can't have executed anything yet but the gdbstub code does liberally iterate through the CPU list in various places.

This sticking plaster ensure the not yet fully realized vCPU is given an pid of -1 which should be enough to ensure it doesn't show up anywhere else.

In the longer term I think the code that manages the association between vCPUs and attached GDB processes could do with a clean-up and re-factor.

Cc Richard Henderson <[email protected]>

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

---
  gdbstub.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
gdbstub.c +0 -0
index 23baaef40e..141d7bc4ec 100644

gdbstub.c static inline int cpu_gdb_index(CPUState *cpu) · line 100

Quoted 8 lines
gdbstub.c +1 -1
@@ -94,7 +94,7 @@ static inline int cpu_gdb_index(CPUState *cpu)
  {
  #if defined(CONFIG_USER_ONLY)
      TaskState *ts = (TaskState *) cpu->opaque;
-    return ts->ts_tid;
+    return ts ? ts->ts_tid : -1;
  #else
      return cpu->cpu_index + 1;
  #endif

r~