Skip to contentSkip to conversation

error: allow local errors to trigger abort

RFC PATCH Superseded Michael S. Tsirkin 4 participants 17 messages Jun 16, 2015 – Jun 17, 2015 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.
    
    To fix, add an API to check errp and set local_err to error_abort
    if errp is error_abort.

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         | 21 ++++++++++++++++-----
 3 files changed, 22 insertions(+), 6 deletions(-)
Signature · 1 line
MST
#1 lore raw

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

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

---
 util/error.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
util/error.c +11 -5
index 14f4351..ccf29ea 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 && (*errp)->err_class == ERROR_CLASS_MAX;
+}
 
 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();
     }
@@ -168,7 +174,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 RFC 1/3] error: don't rely on pointer comparisons

Quoted 7 lines · Michael S. Tsirkin

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

---
 util/error.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

Where is this patch needed?

Is the goal to allow:

Error err = error_abort;

as a way to statically initialize err via copy to behave the same as the global error_abort?

But that's not how you were using it in patch 3. There, you were initializing Error *, so using &error_abort is still useful there, and pointer equality still suffices.

I don't see a convincing use for this patch.

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 RFC 1/3] error: don't rely on pointer comparisons

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

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

---
 util/error.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

Where is this patch needed?

Is the goal to allow:

Quoted 8 lines

Error err = error_abort;

as a way to statically initialize err via copy to behave the same as the global error_abort?

But that's not how you were using it in patch 3. There, you were initializing Error *, so using &error_abort is still useful there, and pointer equality still suffices.

No because &error_abort is Error **.

Quoted 1 line

I don't see a convincing use for this patch.

See cover letter for the motivation.

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

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

In reply to Eric Blake
Quoted 9 lines · Eric Blake
Quoted 7 lines · Michael S. Tsirkin

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

---
 util/error.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
Quoted 5 lines

Where is this patch needed?

Is the goal to allow:

Error err = error_abort;

Oh, my bad for not double checking the layers of indirection involved. The above comment makes no sense type-wise, since error_abort is already 'Error *', and &error_abort is 'Error **'.

Quoted 9 lines

as a way to statically initialize err via copy to behave the same as the global error_abort?

But that's not how you were using it in patch 3. There, you were initializing Error *, so using &error_abort is still useful there, and pointer equality still suffices.

I don't see a convincing use for this patch.

And I retract that, because now I do see the use.

Error *local_err = ...;

as a way to set an abort-on-error pointer 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 with embedded properties.

Sorry for the confusion on my part, and:

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

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

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

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

---
 util/error.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
util/error.c +0 -0
index 14f4351..ccf29ea 100644

util/error.c struct Error · line 24

Quoted 6 lines
util/error.c +2 -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;

Looking at this a bit further, I still wonder if we can do a slightly better job of coming up with something that will SIGSEGV (or SIGBUS) if we (accidentally) try to dereference the pointer (similar to how SIG_IGN is (sighandler_t)1) - because we know that the abort object should never be dereferenced. Something like:

    Error *error_abort = (Error *)1;

with no need for error_abort_st. (Might have to spell it as Error

    *error_abort = (void*)(intptr_t)1;

to shut up compiler warnings)

util/error.c struct Error · line 29

Quoted 5 lines
util/error.c +5 -0
+
+static bool error_is_abort(Error **errp)
+{
+    return errp && *errp && (*errp)->err_class == ERROR_CLASS_MAX;
+}

and this would be:

    return errp && *errp == error_abort;

The rest of this patch is still good. Then in patch 2, you'd have:

Error *error_init_local(Error **errp)

{
    return error_is_abort(errp) ? error_abort : NULL;
}

That is, you still use pointer equality, but at one less level of indirection (equality at the Error* level, not the Error** level).

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

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

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

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

---
 util/error.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
util/error.c +0 -0
index 14f4351..ccf29ea 100644

util/error.c struct Error · line 24

Quoted 6 lines
util/error.c +2 -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;

Looking at this a bit further, I still wonder if we can do a slightly better job of coming up with something that will SIGSEGV (or SIGBUS) if we (accidentally) try to dereference the pointer (similar to how SIG_IGN is (sighandler_t)1) - because we know that the abort object should never be dereferenced. Something like:

    Error *error_abort = (Error *)1;

with no need for error_abort_st. (Might have to spell it as Error

    *error_abort = (void*)(intptr_t)1;

to shut up compiler warnings)

util/error.c struct Error · line 29

Quoted 5 lines
util/error.c +5 -0
+
+static bool error_is_abort(Error **errp)
+{
+    return errp && *errp && (*errp)->err_class == ERROR_CLASS_MAX;
+}

and this would be:

    return errp && *errp == error_abort;

The rest of this patch is still good. Then in patch 2, you'd have:

Quoted 7 lines

Error *error_init_local(Error **errp)

{
    return error_is_abort(errp) ? error_abort : NULL;
}

That is, you still use pointer equality, but at one less level of indirection (equality at the Error* level, not the Error** level).

It's a clever trick, it'd work. But why do tricks? This is never performance-critical, is it? E.g. debugging is easier if pointers actually point to things.

Let's see what do others say.

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

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

Am 17.06.2015 um 08:57 hat Michael S. Tsirkin geschrieben:

Quoted 51 lines · Eric Blake
Quoted 50 lines · Michael S. Tsirkin
Quoted 13 lines

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

---
 util/error.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
util/error.c +0 -0
index 14f4351..ccf29ea 100644

util/error.c struct Error · line 24

Quoted 6 lines
util/error.c +2 -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;

Looking at this a bit further, I still wonder if we can do a slightly better job of coming up with something that will SIGSEGV (or SIGBUS) if we (accidentally) try to dereference the pointer (similar to how SIG_IGN is (sighandler_t)1) - because we know that the abort object should never be dereferenced. Something like:

    Error *error_abort = (Error *)1;

with no need for error_abort_st. (Might have to spell it as Error

    *error_abort = (void*)(intptr_t)1;

to shut up compiler warnings)

util/error.c struct Error · line 29

Quoted 5 lines
util/error.c +5 -0
+
+static bool error_is_abort(Error **errp)
+{
+    return errp && *errp && (*errp)->err_class == ERROR_CLASS_MAX;
+}

and this would be:

    return errp && *errp == error_abort;

The rest of this patch is still good. Then in patch 2, you'd have:

Error *error_init_local(Error **errp)

{
    return error_is_abort(errp) ? error_abort : NULL;
}
Quoted 8 lines
Quoted 2 lines

That is, you still use pointer equality, but at one less level of indirection (equality at the Error* level, not the Error** level).

It's a clever trick, it'd work. But why do tricks? This is never performance-critical, is it? E.g. debugging is easier if pointers actually point to things.

Let's see what do others say.

This isn't about performance, but about failing for invalid use. If we say that dereferencing error_abort is wrong, then letting it fail fast (with a segfault) actually makes debugging easier than silently accessing garbage and trying to figure out why some code misbehaves only later.

Kevin

#8 lore raw

RFC PATCH 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.

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 ccf29ea..7489967 100644
@@ -28,6 +28,11 @@ static bool error_is_abort(Error **errp)
     return errp && *errp && (*errp)->err_class == ERROR_CLASS_MAX;
 }
 
+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
Reviewed-by on v1 2/3
#9 lore raw
Quoted 11 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;
    }
Quoted 4 lines

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 9 lines

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(+)

I like the idea.

util/error.c static bool error_is_abort(Error **errp) · line 33

Quoted 8 lines

+++ b/util/error.c

util/error.c +3 -0
@@ -28,6 +28,11 @@ static bool error_is_abort(Error **errp)
     return errp && *errp && (*errp)->err_class == ERROR_CLASS_MAX;
 }
 
+Error *error_init_local(Error **errp)
+{
+    return error_is_abort(errp) ? *errp : NULL;

What you have works, but see also my ideas in my (latest) reply to 1/3 where you could use pointer equality on error_abort (rather than on &error_abort) as the key factor. After all, the way you have implemented things so far, when taking the *errp branch, you are still effectively returning the error_abort global pointer (you haven't yet used anything that required struct copying of the contents of error_abort).

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

RFC PATCH 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
#11 lore raw

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

Quoted 7 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(-)

And indeed this is the reason things are still at RFC level. I like the idea. It doesn't change anything for a bug-free program, but where we DO have a bug, we now get a stacktrace that aborts as soon as possible rather than delaying to the propagation point and losing some information.

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

Quoted 11 lines
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);

Should be a fairly mechanical patch to catch all the spots; although there are multiple spellings (not all callers name it local_err).

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

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

Quoted 15 lines · Eric Blake
Quoted 7 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(-)

And indeed this is the reason things are still at RFC level. I like the idea. It doesn't change anything for a bug-free program, but where we DO have a bug, we now get a stacktrace that aborts as soon as possible rather than delaying to the propagation point and losing some information.

Quoted 1 line
Quoted 13 lines

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

Quoted 10 lines
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);

Should be a fairly mechanical patch to catch all the spots; although there are multiple spellings (not all callers name it local_err).

I'll try to write an spatch to do this.

Quoted 4 lines
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#13 lore raw
In reply to Michael S. Tsirkin
Quoted 17 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.

Please humor the ignorant: Why is this too late? Any code that does anything between foo(&local_err) and error_propagate is already broken.

Quoted 13 lines

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

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         | 21 ++++++++++++++++-----
 3 files changed, 22 insertions(+), 6 deletions(-)
#14 lore raw
Quoted 15 lines · John Snow
Quoted 12 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;
        }
Quoted 8 lines
Quoted 5 lines

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.

Please humor the ignorant: Why is this too late? Any code that does anything between foo(&local_err) and error_propagate is already broken.

We have lost the stack so it's hard to debug. v2 explains it better.

Quoted 13 lines
Quoted 13 lines

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

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         | 21 ++++++++++++++++-----
 3 files changed, 22 insertions(+), 6 deletions(-)
#15 lore raw
In reply to John Snow

Am 17.06.2015 um 16:26 hat John Snow geschrieben:

Quoted 15 lines · Michael S. Tsirkin
Quoted 12 lines
    
    It's a common idiom:
    
        Error *local_err = NULL;
        ....
        foo(&local_err);
        ...
        if (local_err) {
            error_propagate(errp, local_err);
            return;
        }
Quoted 8 lines
Quoted 5 lines

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.

Please humor the ignorant: Why is this too late? Any code that does anything between foo(&local_err) and error_propagate is already broken.

The interesting part is the stack trace which is truncated if you abort() only in the outermost caller.

Kevin

Quoted 13 lines
Quoted 13 lines

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

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         | 21 ++++++++++++++++-----
 3 files changed, 22 insertions(+), 6 deletions(-)
#16 lore raw
Quoted 22 lines · Kevin Wolf

Am 17.06.2015 um 16:26 hat John Snow geschrieben:

Quoted 21 lines · Michael S. Tsirkin
Quoted 17 lines
    
    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.
Quoted 8 lines
Quoted 2 lines

Please humor the ignorant: Why is this too late? Any code that does anything between foo(&local_err) and error_propagate is already broken.

The interesting part is the stack trace which is truncated if you abort() only in the outermost caller.

Kevin

AHHH, that makes sense now, thank you.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

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

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         | 21 ++++++++++++++++-----
 3 files changed, 22 insertions(+), 6 deletions(-)