Skip to contentSkip to conversation

Error handling in cpu_x86_create

Qemu-devel In review Jan Kiszka 3 participants 6 messages Jul 30, 2013 – Aug 5, 2013

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

Hi Igor,

just noticed by chance that error handling in cpu_x86_create is likely broken after your changes. Any error after cpu = ...object_new() will not properly release the CPU object again nor report NULL to the caller. That means errors should slip through, no? And cpu_x86_init looks similar.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux
#1 lore raw
Quoted 6 lines · On Tue, 30 Jul 2013 13:00:40 +0200 Jan Kiszka <[email protected]> wrote:

Hi Igor,

just noticed by chance that error handling in cpu_x86_create is likely broken after your changes. Any error after cpu = ...object_new() will not properly release the CPU object again nor report NULL to the caller. That means errors should slip through, no? And cpu_x86_init looks similar.

Failure of object_new() in cpu_x86_create() is not checked since it should never fail.

As for following error handling caller of cpu_x86_create(..., errp) should use errp to check for errors and release failed cpu.

cpu_x86_init() that calls it has:
===
out:

    if (error) {
        fprintf(stderr, "%s\n", error_get_pretty(error));
        error_free(error);
        if (cpu != NULL) {
            object_unref(OBJECT(cpu));

===
similar code-path exists in pc_new_cpu()

end goal is to replace cpu_x86_create() with just object_new(FOO_CPU), but that needs to x86 CPU subclasses and properties in place so we could remove/isolate legacy stuff to legacy hook.

Did you hit any particular problem with current code?

Quoted 3 lines

Jan

author
#2 lore raw
Quoted 18 lines · Igor Mammedov
Quoted 6 lines · On Tue, 30 Jul 2013 13:00:40 +0200 Jan Kiszka <[email protected]> wrote:

Hi Igor,

just noticed by chance that error handling in cpu_x86_create is likely broken after your changes. Any error after cpu = ...object_new() will not properly release the CPU object again nor report NULL to the caller. That means errors should slip through, no? And cpu_x86_init looks similar.

Failure of object_new() in cpu_x86_create() is not checked since it should never fail.

As for following error handling caller of cpu_x86_create(..., errp) should use errp to check for errors and release failed cpu.

cpu_x86_init() that calls it has:
===
out:

Quoted 7 lines
    if (error) {
        fprintf(stderr, "%s\n", error_get_pretty(error));
        error_free(error);
        if (cpu != NULL) {
            object_unref(OBJECT(cpu));

===
similar code-path exists in pc_new_cpu()

Not truly:

    cpu = cpu_x86_create(cpu_model, icc_bridge, errp);
    if (!cpu) {
        return cpu;
    }

    object_property_set_int(OBJECT(cpu), apic_id, "apic-id", &local_err);
    object_property_set_bool(OBJECT(cpu), true, "realized", &local_err);

    if (local_err) {
        if (cpu != NULL) {
            object_unref(OBJECT(cpu));
            cpu = NULL;
        }

That's what made me think the error is supposed to be derived from cpu == NULL, rather than from errp. So the actual uncleanness is here: errp should be checked instead of cpu, right?

Jan

Quoted 11 lines

end goal is to replace cpu_x86_create() with just object_new(FOO_CPU), but that needs to x86 CPU subclasses and properties in place so we could remove/isolate legacy stuff to legacy hook.

Did you hit any particular problem with current code?

Quoted 3 lines

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux
#3 lore raw

PATCH 1/1 target-i386: Fix X86CPU error handling

In reply to Jan Kiszka

Error **errp argument is not for emitting warnings, it means an error has occurred and the caller should not make any assumptions about the state of other return values (unless otherwise documented).

Therefore cpu_x86_create() must unref the new X86CPU itself, and pc_new_cpu() must check for an Error rather than NULL return value.

While at it, clean up a superfluous NULL check.

---
 hw/i386/pc.c      | 13 ++++++-------
 target-i386/cpu.c |  6 +++++-
 2 files changed, 11 insertions(+), 8 deletions(-)
hw/i386/pc.c +6 -7
index a2b9d88..6a0b042 100644
@@ -912,20 +912,19 @@ static X86CPU *pc_new_cpu(const char *cpu_model, int64_t apic_id,
     X86CPU *cpu;
     Error *local_err = NULL;
 
-    cpu = cpu_x86_create(cpu_model, icc_bridge, errp);
-    if (!cpu) {
-        return cpu;
+    cpu = cpu_x86_create(cpu_model, icc_bridge, &local_err);
+    if (local_err != NULL) {
+        error_propagate(errp, local_err);
+        return NULL;
     }
 
     object_property_set_int(OBJECT(cpu), apic_id, "apic-id", &local_err);
     object_property_set_bool(OBJECT(cpu), true, "realized", &local_err);
 
     if (local_err) {
-        if (cpu != NULL) {
-            object_unref(OBJECT(cpu));
-            cpu = NULL;
-        }
         error_propagate(errp, local_err);
+        object_unref(OBJECT(cpu));
+        cpu = NULL;
     }
     return cpu;
 }
target-i386/cpu.c +5 -1
index 71ab915..2efbeca 100644
@@ -1824,7 +1824,11 @@ X86CPU *cpu_x86_create(const char *cpu_model, DeviceState *icc_bridge,
     }
 
 out:
-    error_propagate(errp, error);
+    if (error != NULL) {
+        error_propagate(errp, error);
+        object_unref(OBJECT(cpu));
+        cpu = NULL;
+    }
     g_strfreev(model_pieces);
     return cpu;
 }
Signature · 1 line
1.8.1.4
#4 lore raw

Re: [Qemu-devel] [PATCH for-1.6] target-i386: Fix X86CPU error handling

Am 02.08.2013 19:22, schrieb Andreas Färber:

Quoted 7 lines

Error **errp argument is not for emitting warnings, it means an error has occurred and the caller should not make any assumptions about the state of other return values (unless otherwise documented).

Therefore cpu_x86_create() must unref the new X86CPU itself, and pc_new_cpu() must check for an Error rather than NULL return value.

Quoted 6 lines

While at it, clean up a superfluous NULL check.

Ping! Jan, does this address your concerns / use cases? -rc2 is on Wednesday.

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
author
#5 lore raw

Re: [Qemu-devel] [PATCH for-1.6] target-i386: Fix X86CPU error handling

Quoted 10 lines · Andreas Färber

Am 02.08.2013 19:22, schrieb Andreas Färber:

Quoted 9 lines

Error **errp argument is not for emitting warnings, it means an error has occurred and the caller should not make any assumptions about the state of other return values (unless otherwise documented).

Therefore cpu_x86_create() must unref the new X86CPU itself, and pc_new_cpu() must check for an Error rather than NULL return value.

While at it, clean up a superfluous NULL check.

Quoted 7 lines
Quoted 4 lines

Ping! Jan, does this address your concerns / use cases? -rc2 is on Wednesday.

Yes, thanks, this looks good.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux