Skip to contentSkip to conversation

error: allow local errors to trigger abort

PATCH In review Michael S. Tsirkin 4 participants 13 messages Jun 17, 2015 – Jun 23, 2015 Block layer

lore
Share & tools
lore
#0 lore raw

v2 cover error: allow local errors to trigger abort

It's a common idiom:

Error *local_err = NULL;
....
foo(&local_err);
...
if (local_err) {
error_propagate(errp, local_err);
return;
}

Unfortunately it means that call to foo(&local_err) will not abort even if errp is set to error_abort.

Instead, we get an abort at error_propagate which is too late, that is, the quality of the stack trace is degraded in that it no longer pinpoints the actual cause of failure.

To fix, add an API to check errp and set local_err to error_abort if errp is error_abort.

This is out of RFC but I'm still not converting all users:
let's merge these patches, then I'll convert all users on top.

Changes from v1:
Check Error * pointer, not the class, as suggested by Eric. Extend commit log messages with explanation by Eric.

Michael S. Tsirkin (3):
error: don't rely on pointer comparisons
error: allow local errors to trigger abort
block/nfs: switch to error_init_local

 include/qapi/error.h |  5 +++++
 block/nfs.c          |  2 +-
 util/error.c         | 22 +++++++++++++++++-----
 3 files changed, 23 insertions(+), 6 deletions(-)
Signature · 1 line
MST
#1 lore raw

PATCH v2 1/3 error: don't rely on pointer comparisons

makes it possible to copy error_abort pointers,
not just pass them on directly.

This is needed because follow-up patches add support for

    Error *local_err = ...;

as a way to set an abort-on-error pointer, which requires that we have more than just a global error_abort abort-on-error pointer, but that any number of pointers all resolve to something specific.

Add an assert statement when class is retrieved, to make sure we still get a core-dump if we (somehow) attempt to output the abort errp by mistake.

---
 util/error.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
util/error.c +12 -5
index 14f4351..e10cb34 100644
@@ -20,7 +20,13 @@ struct Error
     ErrorClass err_class;
 };
 
-Error *error_abort;
+static Error error_abort_st = { .err_class = ERROR_CLASS_MAX };
+Error *error_abort = &error_abort_st;
+
+static bool error_is_abort(Error **errp)
+{
+    return errp && *errp == error_abort;
+}
 
 void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
 {
@@ -40,7 +46,7 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
     va_end(ap);
     err->err_class = err_class;
 
-    if (errp == &error_abort) {
+    if (error_is_abort(errp)) {
         error_report_err(err);
         abort();
     }
@@ -76,7 +82,7 @@ void error_set_errno(Error **errp, int os_errno, ErrorClass err_class,
     va_end(ap);
     err->err_class = err_class;
 
-    if (errp == &error_abort) {
+    if (error_is_abort(errp)) {
         error_report_err(err);
         abort();
     }
@@ -121,7 +127,7 @@ void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
     va_end(ap);
     err->err_class = err_class;
 
-    if (errp == &error_abort) {
+    if (error_is_abort(errp)) {
         error_report_err(err);
         abort();
     }
@@ -144,6 +150,7 @@ Error *error_copy(const Error *err)
 
 ErrorClass error_get_class(const Error *err)
 {
+    assert(err->err_class < ERROR_CLASS_MAX);
     return err->err_class;
 }
 
@@ -168,7 +175,7 @@ void error_free(Error *err)
 
 void error_propagate(Error **dst_errp, Error *local_err)
 {
-    if (local_err && dst_errp == &error_abort) {
+    if (local_err && error_is_abort(dst_errp)) {
         error_report_err(local_err);
         abort();
     } else if (dst_errp && !*dst_errp) {
Signature · 1 line
MST
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] error: don't rely on pointer comparisons

Quoted 9 lines · Michael S. Tsirkin

makes it possible to copy error_abort pointers,
not just pass them on directly.

This is needed because follow-up patches add support for

    Error *local_err = ...;

as a way to set an abort-on-error pointer, which requires that we have more than just a global error_abort abort-on-error pointer, but that any number of pointers all resolve to something specific.

Quoted 6 lines

Add an assert statement when class is retrieved, to make sure we still get a core-dump if we (somehow) attempt to output the abort errp by mistake.

I think you made enough changes from v1 (functional change of an added assertion, but also better justification via improved commit message) that I would have dropped R-b if I were the one submitting it.

Your argument of aiding gdb debugging of error objects by making the pointer point somewhere valid makes sense, when compared to my hack of an invalid pointer that would segfault even when trying to view it through gdb. The added assertion in this version definitely helps avoid code making the mistake of dereferencing the magic error pointer.

Quoted 9 lines
---
 util/error.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
util/error.c +0 -0
index 14f4351..e10cb34 100644

util/error.c struct Error · line 28

Quoted 10 lines
util/error.c +6 -1
@@ -20,7 +20,13 @@ struct Error
     ErrorClass err_class;
 };
 
-Error *error_abort;
+static Error error_abort_st = { .err_class = ERROR_CLASS_MAX };
+Error *error_abort = &error_abort_st;
+
+static bool error_is_abort(Error **errp)
+{
+    return errp && *errp == error_abort;

However, now I don't like the subject line. This is still a pointer comparison (just a different pointer than before). So while I'm now happy with the state of the code, I think a better commit message would be:

error: don't rely on address of global variable

The old implementation used the address of a global pointer variable (&error_abort, type Error**) as a sentinel. This patch changes to using the value of the global pointer variable itself (error_abort, type Error*), so that the sentinel value can be easily copied to other Error* pointers, regardless of their address. The new sentinel points to an actual object, in case it is inspected through a debugger, although working code should never dereference it.

This is needed because...[snipped]

util/error.c Error *error_copy(const Error *err) · line 153

Quoted 5 lines
util/error.c +1 -0
@@ -144,6 +150,7 @@ Error *error_copy(const Error *err)
 
 ErrorClass error_get_class(const Error *err)
 {
+    assert(err->err_class < ERROR_CLASS_MAX);

I also think you should add this assert in error_copy() a few lines above, as well as in error_get_pretty() and error_free() a few lines below.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] error: don't rely on pointer comparisons

In reply to Michael S. Tsirkin

util/error.c void error_free(Error *err) · line 181

Quoted 13 lines · Michael S. Tsirkin

makes it possible to copy error_abort pointers,
not just pass them on directly.

util/error.c +1 -1
@@ -168,7 +175,7 @@ void error_free(Error *err)
 
 void error_propagate(Error **dst_errp, Error *local_err)
 {
-    if (local_err && dst_errp == &error_abort) {
+    if (local_err && error_is_abort(dst_errp)) {
         error_report_err(local_err);
         abort();
     } else if (dst_errp && !*dst_errp) {

As I pointed out on 3/3, this breaks code that does:

if (local_err) {
    error_propagate(errp, local_err);
    ...
}

now that local_err is non-NULL when errp is error_abort. But what if you alter the semantics, and have error_propagate return a bool (true if an error was propagated, false if no error or caller didn't care):

bool error_propagate(Error **dst_errp, Error *local_err)

{
    if (error_is_abort(&local_err)) {
        assert(error_is_abort(dst_errp);
        return false;
    }
    if (local_err && error_is_abort(dst_errp)) {
        error_report_err(local_err);
        abort();
    }
    if (dst_errp && !*dst_errp) {
        *dst_errp = local_err;
        return true;
    }
    if (local_err) {
        error_free(local_err);
    }
    return false;
}

then callers can be modified to this idiom (also has the benefit of being one line shorter):

if (error_propagate(errp, local_err)) {
    ...
}
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] error: don't rely on pointer comparisons

Quoted 46 lines · Eric Blake

util/error.c void error_free(Error *err) · line 181

Quoted 13 lines · Michael S. Tsirkin

makes it possible to copy error_abort pointers,
not just pass them on directly.

util/error.c +1 -1
@@ -168,7 +175,7 @@ void error_free(Error *err)
 
 void error_propagate(Error **dst_errp, Error *local_err)
 {
-    if (local_err && dst_errp == &error_abort) {
+    if (local_err && error_is_abort(dst_errp)) {
         error_report_err(local_err);
         abort();
     } else if (dst_errp && !*dst_errp) {

As I pointed out on 3/3, this breaks code that does:

if (local_err) {
    error_propagate(errp, local_err);
    ...
}

now that local_err is non-NULL when errp is error_abort. But what if you alter the semantics, and have error_propagate return a bool (true if an error was propagated, false if no error or caller didn't care):

bool error_propagate(Error **dst_errp, Error *local_err)

{
    if (error_is_abort(&local_err)) {
        assert(error_is_abort(dst_errp);
        return false;
    }
    if (local_err && error_is_abort(dst_errp)) {
        error_report_err(local_err);
        abort();
    }
    if (dst_errp && !*dst_errp) {
        *dst_errp = local_err;
        return true;
    }
    if (local_err) {
        error_free(local_err);
    }
    return false;
}
Quoted 6 lines

then callers can be modified to this idiom (also has the benefit of being one line shorter):

if (error_propagate(errp, local_err)) {
    ...
}

Caution! The condition you need to test here is "an error has been stored into local_err", *not* "an error was propagated". Different when errp is NULL and local_err has an error.

#5 lore raw

Re: [Qemu-devel] [PATCH v2 1/3] error: don't rely on pointer comparisons

In reply to Michael S. Tsirkin
Quoted 2 lines · Michael S. Tsirkin

makes it possible to copy error_abort pointers,
not just pass them on directly.

Humor me, and start your sentences with a capital letter :)

Quoted 9 lines

This is needed because follow-up patches add support for

    Error *local_err = ...;

as a way to set an abort-on-error pointer, which requires that we have more than just a global error_abort abort-on-error pointer, but that any number of pointers all resolve to something specific.

Add an assert statement when class is retrieved, to make sure we still get a core-dump if we (somehow) attempt to output the abort errp by mistake.

Description could be clearer, but let's discuss the actual patches first.

Quoted 13 lines
---
 util/error.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
util/error.c +0 -0
index 14f4351..e10cb34 100644
@@ -20,7 +20,13 @@ struct Error
     ErrorClass err_class;

util/error.c struct Error · line 29

Quoted 10 lines
util/error.c +7 -1
 };
 
-Error *error_abort;
+static Error error_abort_st = { .err_class = ERROR_CLASS_MAX };
+Error *error_abort = &error_abort_st;
+
+static bool error_is_abort(Error **errp)
+{
+    return errp && *errp == error_abort;
+}

If anything changes the value of error_abort, we're now screwed.

Quoted 31 lines
util/error.c +3 -3
 void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
 {
@@ -40,7 +46,7 @@ void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
     va_end(ap);
     err->err_class = err_class;
 
-    if (errp == &error_abort) {
+    if (error_is_abort(errp)) {
         error_report_err(err);
         abort();
     }
@@ -76,7 +82,7 @@ void error_set_errno(Error **errp, int os_errno, ErrorClass err_class,
     va_end(ap);
     err->err_class = err_class;
 
-    if (errp == &error_abort) {
+    if (error_is_abort(errp)) {
         error_report_err(err);
         abort();
     }
@@ -121,7 +127,7 @@ void error_set_win32(Error **errp, int win32_err, ErrorClass err_class,
     va_end(ap);
     err->err_class = err_class;
 
-    if (errp == &error_abort) {
+    if (error_is_abort(errp)) {
         error_report_err(err);
         abort();
     }

util/error.c Error *error_copy(const Error *err) · line 153

Quoted 4 lines
util/error.c +1 -0
@@ -144,6 +150,7 @@ Error *error_copy(const Error *err)
 
 ErrorClass error_get_class(const Error *err)
 {
+    assert(err->err_class < ERROR_CLASS_MAX);

The assertion makes some sense independent of the rest of this series.

It's not as tight as it could be when the compiler makes ErrorClass signed.

util/error.c void error_free(Error *err) · line 181

Quoted 12 lines
util/error.c +1 -1
     return err->err_class;
 }
 
@@ -168,7 +175,7 @@ void error_free(Error *err)
 
 void error_propagate(Error **dst_errp, Error *local_err)
 {
-    if (local_err && dst_errp == &error_abort) {
+    if (local_err && error_is_abort(dst_errp)) {
         error_report_err(local_err);
         abort();
     } else if (dst_errp && !*dst_errp) {

As Eric pointed out, this isn't quite right.

Your use of ERROR_CLASS_MAX is unobvious, and needs an explanatory comment somewhere. I'd put it right next to its definition if it wasn't defined implicitly.

#6 lore raw

PATCH v2 2/3 error: allow local errors to trigger abort

In reply to Michael S. Tsirkin

It's a common idiom:

    Error *local_err = NULL;
    ....
    foo(&local_err);
    ...
    if (local_err) {
        error_propagate(errp, local_err);
        return;
    }

Unfortunately it means that call to foo(&local_err) will not abort even if errp is set to error_abort.

Instead, we get an abort at error_propagate which is too late, that is, the quality of the stack trace is degraded in that it no longer pinpoints the actual cause of failure.

To fix, add an API to check errp and set local_err to error_abort if errp is error_abort.

---
 include/qapi/error.h | 5 +++++
 util/error.c         | 5 +++++
 2 files changed, 10 insertions(+)
include/qapi/error.h +5 -0
index f44c451..8246a62 100644
@@ -88,6 +88,11 @@ const char *error_get_pretty(Error *err);
 void error_report_err(Error *);
 
 /**
+ * Init a local error. It must be propagated to errp using error_propagate.
+ */
+Error *error_init_local(Error **errp);
+
+/**
  * Propagate an error to an indirect pointer to an error.  This function will
  * always transfer ownership of the error reference and handles the case where
  * dst_err is NULL correctly.  Errors after the first are discarded.
util/error.c +5 -0
index e10cb34..7f5b134 100644
@@ -28,6 +28,11 @@ static bool error_is_abort(Error **errp)
     return errp && *errp == error_abort;
 }
 
+Error *error_init_local(Error **errp)
+{
+    return error_is_abort(errp) ? *errp : NULL;
+}
+
 void error_set(Error **errp, ErrorClass err_class, const char *fmt, ...)
 {
     Error *err;
Signature · 1 line
MST
#7 lore raw

PATCH v2 3/3 block/nfs: switch to error_init_local

In reply to Michael S. Tsirkin

We probably should just switch everyone, this is
just to demonstrate the API usage.

---
 block/nfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/nfs.c +1 -1
index ca9e24e..de4b8c3 100644
@@ -385,7 +385,7 @@ static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
     NFSClient *client = bs->opaque;
     int64_t ret;
     QemuOpts *opts;
-    Error *local_err = NULL;
+    Error *local_err = error_init_local(errp);
 
     client->aio_context = bdrv_get_aio_context(bs);
 
Signature · 1 line
MST
#8 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] block/nfs: switch to error_init_local

Quoted 13 lines · Michael S. Tsirkin

We probably should just switch everyone, this is
just to demonstrate the API usage.

---
 block/nfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/nfs.c +0 -0
index ca9e24e..de4b8c3 100644

block/nfs.c static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, · line 390

Quoted 7 lines
block/nfs.c +1 -1
@@ -385,7 +385,7 @@ static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
     NFSClient *client = bs->opaque;
     int64_t ret;
     QemuOpts *opts;
-    Error *local_err = NULL;
+    Error *local_err = error_init_local(errp);
 
     client->aio_context = bdrv_get_aio_context(bs);

More context:

Quoted 5 lines
block/nfs.c +0 -0
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
    qemu_opts_absorb_qdict(opts, options, &local_err);
    if (local_err) {
        error_propagate(errp, local_err);
        ret = -EINVAL;

Oops. Your initialization means that if the caller passed in &error_abort, then local_err is now non-NULL, and we will attempt to do error_propagate(errp, error_abort), which will abort(). You'd have to change that to
if (!error_is_abort(local_err)) {

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#9 lore raw

Re: [Qemu-devel] [PATCH v2 3/3] block/nfs: switch to error_init_local

Quoted 30 lines · Eric Blake
Quoted 13 lines · Michael S. Tsirkin

We probably should just switch everyone, this is
just to demonstrate the API usage.

---
 block/nfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/nfs.c +0 -0
index ca9e24e..de4b8c3 100644

block/nfs.c static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, · line 390

Quoted 7 lines
block/nfs.c +1 -1
@@ -385,7 +385,7 @@ static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
     NFSClient *client = bs->opaque;
     int64_t ret;
     QemuOpts *opts;
-    Error *local_err = NULL;
+    Error *local_err = error_init_local(errp);
 
     client->aio_context = bdrv_get_aio_context(bs);

More context:

Quoted 5 lines
block/nfs.c +0 -0
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
    qemu_opts_absorb_qdict(opts, options, &local_err);
    if (local_err) {
        error_propagate(errp, local_err);
        ret = -EINVAL;
Quoted 5 lines

Oops. Your initialization means that if the caller passed in &error_abort, then local_err is now non-NULL, and we will attempt to do error_propagate(errp, error_abort), which will abort(). You'd have to change that to
if (!error_is_abort(local_err)) {

Looking at the alternatives, this seems like a reasonable change, does it not?
Except it's a slightly ugly name, we need a new wrapper

     if (!error_is_set(local_err))

which does if (!local_err || local_err == &error_abort) internally.

Quoted 4 lines
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#10 lore raw
In reply to Michael S. Tsirkin
Quoted 18 lines · Michael S. Tsirkin

It's a common idiom:

Error *local_err = NULL;
....
foo(&local_err);
...
if (local_err) {
error_propagate(errp, local_err);
return;
}

Unfortunately it means that call to foo(&local_err) will not abort even if errp is set to error_abort.

Instead, we get an abort at error_propagate which is too late, that is, the quality of the stack trace is degraded in that it no longer pinpoints the actual cause of failure.

Quoted 6 lines

To fix, add an API to check errp and set local_err to error_abort if errp is error_abort.

This is out of RFC but I'm still not converting all users:
let's merge these patches, then I'll convert all users on top.

Let's take a step back and review intended use of Error before and after this series.

Before:

* Parameter Error **errp (by convention the last one)

To create and return an error, do:

      error_setg(errp, ...);

To propagate an existing error (typically one you received from a callee), do:

      error_propagate(errp, ...);

You're not supposed to examine errp, let alone dereference it.

* Actual argument

- to receive an error: address of an Error * variable, its value must be null before the call, and may be examined afterwards

- to ignore errors: null pointer

- to abort on error: &error_abort

This leads to a few patterns:

    T foo(..., Error **errp)
    {
        // pattern: receive, test and propagate error
        Error *err = NULL;
        bar(..., &err);
        if (err) {
            error_propagate(errp, err);
            return ...;
        }
        // pattern: set error
        if (...) {
            error_setg(errp, ...);
            return ...;
        }
        // pattern: pass through error
        // really the first pattern less the test simplified
        baz(..., errp);
        return ...;
    }

Your patch modifies the "actual argument to receive an error" clause:
the variable must now be either null or the value of error_init_local(errp). If it's the latter, then it must be passed to error_propagate(errp, err).

We acquire a new pattern:

        // pattern: receive, test and propagate error
        Error *err = error_init_local(errp);
        bar(..., &err);
        if (err) {
            error_propagate(errp, err);
            return ...;
        }

Let's see whether it can fully replace the existing pattern. Before you can convert err = NULL to err = error_init_local(errp), you have to:

* find the appropriate errp (usually trivial)

* double-check we error_propagate(errp, err) on every path leaving the function

It's perfectly possible that we *don't* propagate on all paths:

        Error *err = NULL;
        bar(..., &err);
        if (error is non-recoverable) {
            error_propagate(errp, err);
            return ...;
        }
        recover and carry on

Here, error_init_local(errp) would be *wrong*.

The Error boilerplate is annoying, but at least there are few ways to get it wrong (the most common one is "Error * variable not null before the call"). I'm most reluctant to add more ways to get it wrong.

Is the gain worth all this additional complexity? The gain is certainly
real: backtrace shows where the error got created instead of where it was propagated to &error_abort. But the existing backtraces have never bothered me. When I get one ending at an error_propagate(errp, err), and I need the real one instead, finding it in the debugger is easy enough. The complexity does bother me.

Here's an utterly trivial way to get some of the gain for none of the
complexity: make error_setg() & friends store caller's __FILE__ and __LINE__.

#11 lore raw
Quoted 3 lines · Markus Armbruster

Here's an utterly trivial way to get some of the gain for none of the
complexity: make error_setg() & friends store caller's __FILE__ and __LINE__.

Yes, please!!

Paolo

#12 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 3 lines · Markus Armbruster

Here's an utterly trivial way to get some of the gain for none of the
complexity: make error_setg() & friends store caller's __FILE__ and __LINE__.

Yes, please!!

I coded it up, will post as soon as its dependencies are in place.