Skip to contentSkip to conversation

target/i386: allow access_ptr to force slow path on failed probe

RFC PATCH In review Alex Bennée 3 participants 5 messages Aug 7, 2024 – Aug 13, 2024

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 target/i386: allow access_ptr to force slow path on failed probe

When we are using TCG plugin memory callbacks probe_access_internal will return TLB_MMIO to force the slow path for memory access. This results in probe_access returning NULL but the x86 access_ptr function happily accepts an empty haddr resulting in segfault hilarity.

Check for an empty haddr to prevent the segfault and enable plugins to track all the memory operations for the x86 save/restore helpers.

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

Fixes 6d03226b42 (plugins: force slow path when plugins instrument memory ops)
---
 target/i386/tcg/access.c | 4 ++++
 1 file changed, 4 insertions(+)
target/i386/tcg/access.c +4 -0
index 56a1181ea5..8ea5c453a0 100644
@@ -58,6 +58,10 @@ static void *access_ptr(X86Access *ac, vaddr addr, unsigned len)
 
     assert(addr >= ac->vaddr);
 
+    if (!ac->haddr1) {
+        return NULL;
+    }
+
 #ifdef CONFIG_USER_ONLY
     assert(offset <= ac->size1 - len);
     return ac->haddr1 + offset;
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Alex Bennée

When we are using TCG plugin memory callbacks probe_access_internal will return TLB_MMIO to force the slow path for memory access. This results in probe_access returning NULL but the x86 access_ptr function happily accepts an empty haddr resulting in segfault hilarity.

Check for an empty haddr to prevent the segfault and enable plugins to track all the memory operations for the x86 save/restore helpers.

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

Fixes 6d03226b42 (plugins: force slow path when plugins instrument memory ops)
---
  target/i386/tcg/access.c | 4 ++++
  1 file changed, 4 insertions(+)
target/i386/tcg/access.c +0 -0
index 56a1181ea5..8ea5c453a0 100644

target/i386/tcg/access.c static void *access_ptr(X86Access *ac, vaddr addr, unsigned len) · line 67

Quoted 10 lines
target/i386/tcg/access.c +4 -0
@@ -58,6 +58,10 @@ static void *access_ptr(X86Access *ac, vaddr addr, unsigned len)
 
      assert(addr >= ac->vaddr);
 
+    if (!ac->haddr1) {
+        return NULL;
+    }
+
  #ifdef CONFIG_USER_ONLY
      assert(offset <= ac->size1 - len);
      return ac->haddr1 + offset;

I confirm that this patch fixes the crash I reported here: https://gitlab.com/qemu-project/qemu/-/issues/2489

Thanks,

Signature · 1 line
Alexandre
#2 lore raw
In reply to Alex Bennée
Quoted 20 lines · Alex Bennée

When we are using TCG plugin memory callbacks probe_access_internal will return TLB_MMIO to force the slow path for memory access. This results in probe_access returning NULL but the x86 access_ptr function happily accepts an empty haddr resulting in segfault hilarity.

Check for an empty haddr to prevent the segfault and enable plugins to track all the memory operations for the x86 save/restore helpers.

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

Fixes 6d03226b42 (plugins: force slow path when plugins instrument memory ops)
---
  target/i386/tcg/access.c | 4 ++++
  1 file changed, 4 insertions(+)
target/i386/tcg/access.c +0 -0
index 56a1181ea5..8ea5c453a0 100644

target/i386/tcg/access.c static void *access_ptr(X86Access *ac, vaddr addr, unsigned len) · line 67

Quoted 10 lines
target/i386/tcg/access.c +4 -0
@@ -58,6 +58,10 @@ static void *access_ptr(X86Access *ac, vaddr addr, unsigned len)
 
      assert(addr >= ac->vaddr);
 
+    if (!ac->haddr1) {
+        return NULL;
+    }
+
  #ifdef CONFIG_USER_ONLY
      assert(offset <= ac->size1 - len);
      return ac->haddr1 + offset;

You need to remove the test_ptr macro below as well.

r~

#3 lore raw
Quoted 21 lines · Richard Henderson
Quoted 20 lines · Alex Bennée

When we are using TCG plugin memory callbacks probe_access_internal will return TLB_MMIO to force the slow path for memory access. This results in probe_access returning NULL but the x86 access_ptr function happily accepts an empty haddr resulting in segfault hilarity. Check for an empty haddr to prevent the segfault and enable plugins to
track all the memory operations for the x86 save/restore helpers.

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

Fixes 6d03226b42 (plugins: force slow path when plugins instrument memory ops)
---
  target/i386/tcg/access.c | 4 ++++
  1 file changed, 4 insertions(+)
target/i386/tcg/access.c +0 -0
index 56a1181ea5..8ea5c453a0 100644
@@ -58,6 +58,10 @@ static void *access_ptr(X86Access *ac, vaddr addr, unsigned len)
        assert(addr >= ac->vaddr);
  +    if (!ac->haddr1) {
Quoted 8 lines

target/i386/tcg/access.c static void *access_ptr(X86Access *ac, vaddr addr, unsigned len) · line 67

Quoted 6 lines
target/i386/tcg/access.c +3 -0
+        return NULL;
+    }
+
  #ifdef CONFIG_USER_ONLY
      assert(offset <= ac->size1 - len);
      return ac->haddr1 + offset;

You need to remove the test_ptr macro below as well.

So we fall-back to the slow path for linux-user as well?

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#4 lore raw
Quoted 32 lines · Alex Bennée
Quoted 21 lines · Richard Henderson
Quoted 20 lines · Alex Bennée

When we are using TCG plugin memory callbacks probe_access_internal will return TLB_MMIO to force the slow path for memory access. This results in probe_access returning NULL but the x86 access_ptr function happily accepts an empty haddr resulting in segfault hilarity. Check for an empty haddr to prevent the segfault and enable plugins to
track all the memory operations for the x86 save/restore helpers.

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

Fixes 6d03226b42 (plugins: force slow path when plugins instrument memory ops)
---
   target/i386/tcg/access.c | 4 ++++
   1 file changed, 4 insertions(+)
target/i386/tcg/access.c +0 -0
index 56a1181ea5..8ea5c453a0 100644
@@ -58,6 +58,10 @@ static void *access_ptr(X86Access *ac, vaddr addr, unsigned len)
         assert(addr >= ac->vaddr);
   +    if (!ac->haddr1) {
Quoted 8 lines

target/i386/tcg/access.c static void *access_ptr(X86Access *ac, vaddr addr, unsigned len) · line 67

Quoted 6 lines
target/i386/tcg/access.c +3 -0
+        return NULL;
+    }
+
   #ifdef CONFIG_USER_ONLY
       assert(offset <= ac->size1 - len);
       return ac->haddr1 + offset;

You need to remove the test_ptr macro below as well.

Quoted 1 line

So we fall-back to the slow path for linux-user as well?

So we don't ignore the null, and dereference it anyway.

r~