Skip to contentSkip to conversation

hw/core/generic-loader: Fix crash when running without CPU

PATCH Applied Thomas Huth 5 participants 10 messages Jan 25, 2017 – Apr 23, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/core/generic-loader: Fix crash when running without CPU

When running QEMU with "-M none -device loader,file=kernel.elf", it currently crashes with a segmentation fault, because the "none"-machine does not have any CPU by default and the generic loader code tries to dereference s->cpu. Fix it by adding an appropriate check for a NULL pointer.

---
 hw/core/generic-loader.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/core/generic-loader.c +5 -4
index 58f1f02..4601267 100644
@@ -137,20 +137,21 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
 #endif
 
     if (s->file) {
+        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;
+
         if (!s->force_raw) {
             size = load_elf_as(s->file, NULL, NULL, &entry, NULL, NULL,
-                               big_endian, 0, 0, 0, s->cpu->as);
+                               big_endian, 0, 0, 0, as);
 
             if (size < 0) {
                 size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
-                                      s->cpu->as);
+                                      as);
             }
         }
 
         if (size < 0 || s->force_raw) {
             /* Default to the maximum size being the machine's ram size */
-            size = load_image_targphys_as(s->file, s->addr, ram_size,
-                                          s->cpu->as);
+            size = load_image_targphys_as(s->file, s->addr, ram_size, as);
         } else {
             s->addr = entry;
         }
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 17 lines · Thomas Huth

When running QEMU with "-M none -device loader,file=kernel.elf", it currently crashes with a segmentation fault, because the "none"-machine does not have any CPU by default and the generic loader code tries to dereference s->cpu. Fix it by adding an appropriate check for a NULL pointer.

---
 hw/core/generic-loader.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/core/generic-loader.c +0 -0
index 58f1f02..4601267 100644

hw/core/generic-loader.c static void generic_loader_realize(DeviceState *dev, Error **errp) · line 140

Quoted 4 lines
hw/core/generic-loader.c +1 -0
@@ -137,20 +137,21 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
 #endif
 
     if (s->file) {
+        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;

Should we just abort if s->cpu is NULL?

Laurent

#2 lore raw
Quoted 20 lines · Laurent Vivier
Quoted 19 lines · Thomas Huth

When running QEMU with "-M none -device loader,file=kernel.elf", it currently crashes with a segmentation fault, because the "none"-machine does not have any CPU by default and the generic loader code tries to dereference s->cpu. Fix it by adding an appropriate check for a NULL pointer.

---
 hw/core/generic-loader.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/core/generic-loader.c +0 -0
index 58f1f02..4601267 100644
@@ -137,20 +137,21 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
 #endif
 
Quoted 4 lines

hw/core/generic-loader.c static void generic_loader_realize(DeviceState *dev, Error **errp) · line 140

Quoted 2 lines
hw/core/generic-loader.c +1 -0
     if (s->file) {
+        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;

Should we just abort if s->cpu is NULL?

I agree, what is the use case where you are loading images without a CPU?

If there is a use case (maybe some KVM thing?) then this patch looks fine to me.

Thanks,

Alistair

Quoted 4 lines

Laurent

#3 lore raw
Quoted 21 lines · Alistair Francis
Quoted 20 lines · Laurent Vivier
Quoted 19 lines · Thomas Huth

When running QEMU with "-M none -device loader,file=kernel.elf", it currently crashes with a segmentation fault, because the "none"-machine does not have any CPU by default and the generic loader code tries to dereference s->cpu. Fix it by adding an appropriate check for a NULL pointer.

---
 hw/core/generic-loader.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/core/generic-loader.c +0 -0
index 58f1f02..4601267 100644
@@ -137,20 +137,21 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
 #endif
 
Quoted 8 lines
Quoted 4 lines

hw/core/generic-loader.c static void generic_loader_realize(DeviceState *dev, Error **errp) · line 140

Quoted 2 lines
hw/core/generic-loader.c +1 -0
     if (s->file) {
+        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;

Should we just abort if s->cpu is NULL?

I agree, what is the use case where you are loading images without a CPU?

If there is a use case (maybe some KVM thing?) then this patch looks fine to me.

I think there is no real use case yet. But this fix is 1) simpler than doing an error_report() + exit() here, and 2) maybe the vision of constructing machines on the fly with QEMU will eventually come true one day in the distant future, so with that patch here, the code would already be prepared for the case when QEMU gets started without CPUs and the CPUs are then later added via QOM...
Well, I don't mind ... if you prefer an error message instead, feel free to suggest another patch. I'm fine as long as we do not simply crash with a segmentation fault here.

Thomas

Reviewed-by
#4 lore raw
Quoted 31 lines · Thomas Huth
Quoted 21 lines · Alistair Francis
Quoted 20 lines · Laurent Vivier
Quoted 19 lines · Thomas Huth

When running QEMU with "-M none -device loader,file=kernel.elf", it currently crashes with a segmentation fault, because the "none"-machine does not have any CPU by default and the generic loader code tries to dereference s->cpu. Fix it by adding an appropriate check for a NULL pointer.

---
 hw/core/generic-loader.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/core/generic-loader.c +0 -0
index 58f1f02..4601267 100644
@@ -137,20 +137,21 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
 #endif
 
Quoted 8 lines
Quoted 4 lines

hw/core/generic-loader.c static void generic_loader_realize(DeviceState *dev, Error **errp) · line 140

Quoted 2 lines
hw/core/generic-loader.c +1 -0
     if (s->file) {
+        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;

Should we just abort if s->cpu is NULL?

I agree, what is the use case where you are loading images without a CPU?

If there is a use case (maybe some KVM thing?) then this patch looks fine to me.

Quoted 9 lines

I think there is no real use case yet. But this fix is 1) simpler than doing an error_report() + exit() here, and 2) maybe the vision of constructing machines on the fly with QEMU will eventually come true one day in the distant future, so with that patch here, the code would already be prepared for the case when QEMU gets started without CPUs and the CPUs are then later added via QOM...
Well, I don't mind ... if you prefer an error message instead, feel free to suggest another patch. I'm fine as long as we do not simply crash with a segmentation fault here.

OK, the use of NULL as "as" seems reasonable (this is what uses load_elf()), so:

Reviewed-by
#5 lore raw
Quoted 30 lines · Laurent Vivier
Quoted 29 lines · Thomas Huth
Quoted 28 lines · Alistair Francis
Quoted 20 lines · Laurent Vivier
Quoted 19 lines · Thomas Huth

When running QEMU with "-M none -device loader,file=kernel.elf", it currently crashes with a segmentation fault, because the "none"-machine does not have any CPU by default and the generic loader code tries to dereference s->cpu. Fix it by adding an appropriate check for a NULL pointer.

---
 hw/core/generic-loader.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/core/generic-loader.c +0 -0
index 58f1f02..4601267 100644
@@ -137,20 +137,21 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
 #endif
 
Quoted 4 lines

hw/core/generic-loader.c static void generic_loader_realize(DeviceState *dev, Error **errp) · line 140

Quoted 2 lines
hw/core/generic-loader.c +1 -0
     if (s->file) {
+        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;

Should we just abort if s->cpu is NULL?

I agree, what is the use case where you are loading images without a CPU?

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

If there is a use case (maybe some KVM thing?) then this patch looks fine to me.

I think there is no real use case yet. But this fix is 1) simpler than doing an error_report() + exit() here, and 2) maybe the vision of constructing machines on the fly with QEMU will eventually come true one day in the distant future, so with that patch here, the code would already be prepared for the case when QEMU gets started without CPUs and the CPUs are then later added via QOM...

Ok, that is a good enough reason for me.

Thanks,

Alistair

Quoted 11 lines
Quoted 3 lines

Well, I don't mind ... if you prefer an error message instead, feel free to suggest another patch. I'm fine as long as we do not simply crash with a segmentation fault here.

OK, the use of NULL as "as" seems reasonable (this is what uses load_elf()), so:

#6 lore raw
In reply to Thomas Huth
Quoted 6 lines · Thomas Huth

I think there is no real use case yet. But this fix is 1) simpler than doing an error_report() + exit() here, and 2) maybe the vision of constructing machines on the fly with QEMU will eventually come true one day in the distant future, so with that patch here, the code would already be prepared for the case when QEMU gets started without CPUs and the CPUs are then later added via QOM...

For the latter to work you'd need to do something more anyway, because you still need to be able to say "load this image into this CPU's address space", even if the CPUs are getting added later via QOM. You'd need to say "delay the image load until we actually have a CPU to load it for" or something... Passing a NULL address space to load_elf_as() etc is saying "I am definitely a board which has system_address_space as its one and only address space", which is OK if you're a board model but a bit more dubious if you're a generic device like this one.

thanks
-- PMM

#8 lore raw
In reply to Thomas Huth
Quoted 32 lines · Thomas Huth

When running QEMU with "-M none -device loader,file=kernel.elf", it currently crashes with a segmentation fault, because the "none"-machine does not have any CPU by default and the generic loader code tries to dereference s->cpu. Fix it by adding an appropriate check for a NULL pointer.

---
 hw/core/generic-loader.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/core/generic-loader.c +4 -2
index 58f1f02..4601267 100644
@@ -137,20 +137,21 @@ static void generic_loader_realize(DeviceState *dev, Error **errp)
 #endif
 
     if (s->file) {
+        AddressSpace *as = s->cpu ? s->cpu->as :  NULL;
+
         if (!s->force_raw) {
             size = load_elf_as(s->file, NULL, NULL, &entry, NULL, NULL,
-                               big_endian, 0, 0, 0, s->cpu->as);
+                               big_endian, 0, 0, 0, as);
 
             if (size < 0) {
                 size = load_uimage_as(s->file, &entry, NULL, NULL, NULL, NULL,
-                                      s->cpu->as);
+                                      as);
             }

hw/core/generic-loader.c static void generic_loader_realize(DeviceState *dev, Error **errp) · line 157

Quoted 10 lines
hw/core/generic-loader.c +1 -2
         }
 
         if (size < 0 || s->force_raw) {
             /* Default to the maximum size being the machine's ram size */
-            size = load_image_targphys_as(s->file, s->addr, ram_size,
-                                          s->cpu->as);
+            size = load_image_targphys_as(s->file, s->addr, ram_size, as);
         } else {
             s->addr = entry;
         }

Ping?

Thomas

#7 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell
Quoted 6 lines · Thomas Huth

I think there is no real use case yet. But this fix is 1) simpler than doing an error_report() + exit() here, and 2) maybe the vision of constructing machines on the fly with QEMU will eventually come true one day in the distant future, so with that patch here, the code would already be prepared for the case when QEMU gets started without CPUs and the CPUs are then later added via QOM...

Quoted 10 lines

For the latter to work you'd need to do something more anyway, because you still need to be able to say "load this image into this CPU's address space", even if the CPUs are getting added later via QOM. You'd need to say "delay the image load until we actually have a CPU to load it for" or something... Passing a NULL address space to load_elf_as() etc is saying "I am definitely a board which has system_address_space as its one and only address space", which is OK if you're a board model but a bit more dubious if you're a generic device like this one.

Doesn't this not just crashing at least move us a step closer in the right direction (even if it isn't the correct address space)? The other option of just printing an error and exit doesn't seem super useful.

Either is better then a segfault though, so we need to do something here.

Thanks,

Alistair

Quoted 4 lines

thanks
-- PMM

#9 lore raw
In reply to Thomas Huth
Quoted 6 lines · Thomas Huth
Quoted 5 lines · Thomas Huth

When running QEMU with "-M none -device loader,file=kernel.elf", it currently crashes with a segmentation fault, because the "none"-machine does not have any CPU by default and the generic loader code tries to dereference s->cpu. Fix it by adding an appropriate check for a NULL pointer.

Applied to -trivial, thanks!

/mjt