Skip to contentSkip to conversation

hmp: fix "info cpu" segfault

PATCH RESEND Superseded Iwona Kotlarska 2 participants 2 messages Mar 29, 2017 – Mar 30, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v2 1/1 hmp: fix "info cpu" segfault

Running QEMU with "qemu-system-x86_64 -M none -nographic -m 256" and executing "dump-guest-memory /dev/null 0 8192" results in segfault.
Fix by checking if we have CPU.

---
v1 --> v2
Added brackets around if body.
Changed subject.

 target/i386/arch_dump.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
target/i386/arch_dump.c +5 -3
index 5a2e4be5d0..43992a440c 100644
@@ -390,9 +390,11 @@ int cpu_get_dump_info(ArchDumpInfo *info,
     GuestPhysBlock *block;
 
 #ifdef TARGET_X86_64
-    X86CPU *first_x86_cpu = X86_CPU(first_cpu);
-
-    lma = !!(first_x86_cpu->env.hflags & HF_LMA_MASK);
+    X86CPU *first_x86_cpu = NULL;
+    first_x86_cpu = X86_CPU(first_cpu);
+    if (first_cpu != NULL) {
+        lma = !!(first_x86_cpu->env.hflags & HF_LMA_MASK);
+    }
 #endif
 
     if (lma) {
Signature · 1 line
2.12.0
#1 lore raw
Quoted 17 lines · Iwona Kotlarska

Running QEMU with "qemu-system-x86_64 -M none -nographic -m 256" and executing "dump-guest-memory /dev/null 0 8192" results in segfault.
Fix by checking if we have CPU.

---
v1 --> v2
Added brackets around if body.
Changed subject.

 target/i386/arch_dump.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
target/i386/arch_dump.c +0 -0
index 5a2e4be5d0..43992a440c 100644

target/i386/arch_dump.c int cpu_get_dump_info(ArchDumpInfo *info, · line 392

Quoted 3 lines
target/i386/arch_dump.c +0 -0
@@ -390,9 +390,11 @@ int cpu_get_dump_info(ArchDumpInfo *info,
     GuestPhysBlock *block;
 
 #ifdef TARGET_X86_64

[Reordering the diff a little bit to make my comments clearer]

target/i386/arch_dump.c int cpu_get_dump_info(ArchDumpInfo *info, · line 394

Quoted 3 lines
target/i386/arch_dump.c +2 -1
-    X86CPU *first_x86_cpu = X86_CPU(first_cpu);
+    X86CPU *first_x86_cpu = NULL;
+    first_x86_cpu = X86_CPU(first_cpu);

There's no reason to replace that single line with these ones.

target/i386/arch_dump.c int cpu_get_dump_info(ArchDumpInfo *info, · line 397

Quoted 5 lines
target/i386/arch_dump.c +3 -2
-
-    lma = !!(first_x86_cpu->env.hflags & HF_LMA_MASK);
+    if (first_cpu != NULL) {
+        lma = !!(first_x86_cpu->env.hflags & HF_LMA_MASK);
+    }

This part looks OK, although personally I would prefer to write it as:

    lma = first_cpu && (first_x86_cpu->env.hflags & HF_LMA_MASK);

so this can become a single-line patch.

I also wonder if the TARGET_X86_64 #ifdef is really necessary. Is it possible to ever have HF_LMA_MASK set if the target is not x86_64? But that's something for a separate patch, and unrelated to the bug you are fixing.

Quoted 6 lines
target/i386/arch_dump.c +0 -0
 #endif
 
     if (lma) {
Signature · 1 line
2.12.0
Signature · 1 line
Eduardo