osdep.h: Prohibit disabling assert() in supported builds
RFC PATCH 1/1 osdep.h: Prohibit disabling assert() in supported builds
We already have several files that knowingly require assert() to work. While we do NOT want to encourage the use of 'assert(side-effects)' (that is a bad practice that prevents copy-and-paste of code to other projects that CAN disable assertions; plus it costs unnecessary reviewer mental cycles to remember our project policy on crippling asserts), we DO want to send a message that anyone that disables assertions has to tweak code in order to compile, making it obvious that we are not going to support their efforts.
---
First mentioned as an idea here:
http://lists.nongnu.org/archive/html/qemu-devel/2017-07/msg06084.html
but I'm titling this RFC as I'm not 100% convinced we want to make it a project-wide, rather than a per-file decision.
include/qemu/osdep.h | 12 ++++++++++++ hw/scsi/mptsas.c | 4 ---- hw/virtio/virtio.c | 4 ---- 3 files changed, 12 insertions(+), 8 deletions(-)
include/qemu/osdep.h +12 -0
@@ -107,6 +107,18 @@ extern int daemon(int, int);#include "glib-compat.h" #include "qemu/typedefs.h" +/* +* We have a lot of unaudited code that will fail in strange ways if +* you disable assertions at compile-time. You are on your own if +* you cripple these safety-checks. +*/ +#ifdef NDEBUG +#error building with NDEBUG is not supported +#endif +#ifdef G_DISABLE_ASSERT +#error building with G_DISABLE_ASSERT is not supported +#endif +#ifndef O_LARGEFILE #define O_LARGEFILE 0#endif
hw/scsi/mptsas.c +0 -4
@@ -1236,11 +1236,7 @@ static void *mptsas_load_request(QEMUFile *f, SCSIRequest *sreq) n =qemu_get_be32 (f);/* TODO: add a way for SCSIBusInfo's load_request to fail, * and fail migration instead of asserting here. -* When we do, we might be able to re-enable NDEBUG below. */ -#ifdef NDEBUG -#error building with NDEBUG is not supported -#endif assert (n >=0 );pci_dma_sglist_init (&req->qsg, pci, n);
hw/virtio/virtio.c +0 -4
@@ -1025,11 +1025,7 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)/* TODO: teach all callers that this can fail, and return failure instead * of asserting here. -* When we do, we might be able to re-enable NDEBUG below. */ -#ifdef NDEBUG -#error building with NDEBUG is not supported -#endif assert (ARRAY_SIZE (data.in_addr) >= data.in_num);assert (ARRAY_SIZE (data.out_addr) >= data.out_num);
Signature · 1 line
2.13.5
Quoted 10 lines · Eric Blake
We already have several files that knowingly require
assert()to work. While we do NOT want to encourage the use of 'assert(side-effects)' (that is a bad practice that prevents copy-and-paste of code to other projects that CAN disable assertions; plus it costs unnecessary reviewer mental cycles to remember our project policy on crippling asserts), we DO want to send a message that anyone that disables assertions has to tweak code in order to compile, making it obvious that we are not going to support their efforts.
Quoted 7 lines
Signed-off-by Eric Blake <[email protected]>---
First mentioned as an idea here:
http://lists.nongnu.org/archive/html/qemu-devel/2017-07/msg06084.html
but I'm titling this RFC as I'm not 100% convinced we want to make it a project-wide, rather than a per-file decision.
I think project-wide is the correct way.
Quoted 61 lines
include/qemu/osdep.h | 12 ++++++++++++ hw/scsi/mptsas.c | 4 ---- hw/virtio/virtio.c | 4 ---- 3 files changed, 12 insertions(+), 8 deletions(-)include/qemu/osdep.h +12
-0@@ -107,6 +107,18 @@ extern int daemon(int, int);#include "glib-compat.h" #include "qemu/typedefs.h" +/* +* We have a lot of unaudited code that will fail in strange ways if +* you disable assertions at compile-time. You are on your own if +* you cripple these safety-checks. +*/ +#ifdef NDEBUG +#error building with NDEBUG is not supported +#endif +#ifdef G_DISABLE_ASSERT +#error building with G_DISABLE_ASSERT is not supported +#endif +#ifndef O_LARGEFILE #define O_LARGEFILE 0#endif hw/scsi/mptsas.c +0
-4@@ -1236,11 +1236,7 @@ static void *mptsas_load_request(QEMUFile *f, SCSIRequest *sreq) n =qemu_get_be32 (f);/* TODO: add a way for SCSIBusInfo's load_request to fail, * and fail migration instead of asserting here. -* When we do, we might be able to re-enable NDEBUG below. */ -#ifdef NDEBUG -#error building with NDEBUG is not supported -#endif assert (n >=0 );pci_dma_sglist_init (&req->qsg, pci, n);hw/virtio/virtio.c +0
-4@@ -1025,11 +1025,7 @@ void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)/* TODO: teach all callers that this can fail, and return failure instead * of asserting here. -* When we do, we might be able to re-enable NDEBUG below. */ -#ifdef NDEBUG -#error building with NDEBUG is not supported -#endif assert (ARRAY_SIZE (data.in_addr) >= data.in_num);assert (ARRAY_SIZE (data.out_addr) >= data.out_num);
Quoted 10 lines · Eric Blake
We already have several files that knowingly require
assert()to work. While we do NOT want to encourage the use of 'assert(side-effects)' (that is a bad practice that prevents copy-and-paste of code to other projects that CAN disable assertions; plus it costs unnecessary reviewer mental cycles to remember our project policy on crippling asserts), we DO want to send a message that anyone that disables assertions has to tweak code in order to compile, making it obvious that we are not going to support their efforts.
Quoted 7 lines
Signed-off-by Eric Blake <[email protected]>---
First mentioned as an idea here:
http://lists.nongnu.org/archive/html/qemu-devel/2017-07/msg06084.html
but I'm titling this RFC as I'm not 100% convinced we want to make it a project-wide, rather than a per-file decision.
I think we should make this project-wide. Otherwise we will have the discussion again and again whether it is ok to compile with NDEBUG or not.
Quoted 10 lines · Eric Blake
We already have several files that knowingly require
assert()to work. While we do NOT want to encourage the use of 'assert(side-effects)' (that is a bad practice that prevents copy-and-paste of code to other projects that CAN disable assertions; plus it costs unnecessary reviewer mental cycles to remember our project policy on crippling asserts), we DO want to send a message that anyone that disables assertions has to tweak code in order to compile, making it obvious that we are not going to support their efforts.
Quoted 7 lines
Signed-off-by Eric Blake <[email protected]>---
First mentioned as an idea here:
http://lists.nongnu.org/archive/html/qemu-devel/2017-07/msg06084.html
but I'm titling this RFC as I'm not 100% convinced we want to make it a project-wide, rather than a per-file decision.
I'm fine with project-wide, but does it have to be a hard error? Can we make it a warning? Same effect by default, but it lets people experiment more easily with --disable-werror.
Quoted 3 lines · Markus Armbruster
I'm fine with project-wide, but does it have to be a hard error? Can we make it a warning? Same effect by default, but it lets people experiment more easily with --disable-werror.
It won't be the same effect by default for our releases, which don't use -Werror.
I don't think we want to make it easy for people to turn this off -- and it's only commenting out a single #error line if anybody really does want to mess about with it.
thanks
-- PMM
Quoted 11 lines · Peter Maydell
Quoted 3 lines · Markus Armbruster
I'm fine with project-wide, but does it have to be a hard error? Can we make it a warning? Same effect by default, but it lets people experiment more easily with --disable-werror.
It won't be the same effect by default for our releases, which don't use -Werror.
I don't think we want to make it easy for people to turn this off -- and it's only commenting out a single #error line if anybody really does want to mess about with it.
Two lines, actually, since we are also prohibiting disabling g_assert().
Anyways, as the topic came up again on the list, should I promote this patch from RFC to actual patch? Whose tree would it go in through?
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 10 lines · Eric Blake
We already have several files that knowingly require
assert()to work. While we do NOT want to encourage the use of 'assert(side-effects)' (that is a bad practice that prevents copy-and-paste of code to other projects that CAN disable assertions; plus it costs unnecessary reviewer mental cycles to remember our project policy on crippling asserts), we DO want to send a message that anyone that disables assertions has to tweak code in order to compile, making it obvious that we are not going to support their efforts.
Quoted 7 lines
Signed-off-by Eric Blake <[email protected]>---
First mentioned as an idea here:
http://lists.nongnu.org/archive/html/qemu-devel/2017-07/msg06084.html
but I'm titling this RFC as I'm not 100% convinced we want to make it a project-wide, rather than a per-file decision.
I think project wide is just right for what you describe: tell the people before disabling assertions you have to change the code (and preferably at least examine each usage of assert project-wide).
OTOH I do think this is to some degree institutionalizing a bad practice (you say we do not want to do that, but IMHO refusing to build with NDEBUG makes only sense if we want to alter the semantic of assert so that once bad becomes acceptable). I can live with that, but I'm not happy about it. Have we considered rolling our own construct which is designed to exhibit the properties we desire?
I mean, if it's about the side effects we could create something like q_assert(cond) and state that cond is evaluate exactly once (and depending on what we want to have, make the actions on !cond (calling abort(), producing a diagnostic message) compile time tweak-able or not). I assume we could then convert each usage of assert to the safe q_assert programmatically.
Regards,
Halil
Quoted 6 lines · Halil Pasic
OTOH I do think this is to some degree institutionalizing a bad practice (you say we do not want to do that, but IMHO refusing to build with NDEBUG makes only sense if we want to alter the semantic of assert so that once bad becomes acceptable). I can live with that, but I'm not happy about it. Have we considered rolling our own construct which is designed to exhibit the properties we desire?
I've thought about it, and it may have even been discussed on the list perhaps (although I didn't go searching to verify).
Quoted 7 lines
I mean, if it's about the side effects we could create something like
q_assert(cond)and state that cond is evaluate exactly once (and depending on what we want to have, make the actions on !cond (callingabort(), producing a diagnostic message) compile time tweak-able or not). I assume we could then convert each usage of assert to the safeq_assertprogrammatically.
I'd prefer that if we are going to introduce our own construct that always evaluates side effects, and only has a compile-time switch on whether to abort() or (foolishly) plow on, that we name it something without 'assert' in the name, so that reviewers don't have to be confused about remembering which variant evaluates side effects. Maybe:
q_verify(cond)
is a good name, which performs the side effects of 'cond' no matter what, but also allows us to abort() if cond fails vs. ignore the failure; perhaps where we make a compile-time decision between the two behaviors as a configure --enable-FOO flag.
Converting all existing assert() and g_assert() to q_verify() should be fairly simple, if we like the idea.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 28 lines · Eric Blake
Quoted 6 lines · Halil Pasic
OTOH I do think this is to some degree institutionalizing a bad practice (you say we do not want to do that, but IMHO refusing to build with NDEBUG makes only sense if we want to alter the semantic of assert so that once bad becomes acceptable). I can live with that, but I'm not happy about it. Have we considered rolling our own construct which is designed to exhibit the properties we desire?
I've thought about it, and it may have even been discussed on the list perhaps (although I didn't go searching to verify).
Quoted 7 lines
I mean, if it's about the side effects we could create something like
q_assert(cond)and state that cond is evaluate exactly once (and depending on what we want to have, make the actions on !cond (callingabort(), producing a diagnostic message) compile time tweak-able or not). I assume we could then convert each usage of assert to the safeq_assertprogrammatically.I'd prefer that if we are going to introduce our own construct that always evaluates side effects, and only has a compile-time switch on whether to
abort()or (foolishly) plow on, that we name it something without 'assert' in the name, so that reviewers don't have to be confused about remembering which variant evaluates side effects. Maybe:
q_verify(cond)
Quoted 7 lines
is a good name, which performs the side effects of 'cond' no matter what, but also allows us to
abort()if cond fails vs. ignore the failure; perhaps where we make a compile-time decision between the two behaviors as a configure --enable-FOO flag.Converting all existing
assert()andg_assert()toq_verify()should be fairly simple, if we like the idea.
About as simple as expanding tabs.
CODING_STYLE demands spaces rather than tabs since 2009. 455 out of 3542 C source files have tabs. Only a minority of them is imported from other projects and thus exempt from CODING_STYLE.
I vote for frying bigger fish.
I also vote for using standard C when standard C is servicable.
Quoted 19 lines · Markus Armbruster
Quoted 17 lines · Eric Blake
Quoted 6 lines · Halil Pasic
OTOH I do think this is to some degree institutionalizing a bad practice (you say we do not want to do that, but IMHO refusing to build with NDEBUG makes only sense if we want to alter the semantic of assert so that once bad becomes acceptable). I can live with that, but I'm not happy about it. Have we considered rolling our own construct which is designed to exhibit the properties we desire?
I'd prefer that if we are going to introduce our own construct that always evaluates side effects, and only has a compile-time switch on whether to
abort()or (foolishly) plow on, that we name it something without 'assert' in the name, so that reviewers don't have to be confused about remembering which variant evaluates side effects. Maybe:
Quoted 7 lines
Quoted 2 lines
q_verify(cond)I vote for frying bigger fish.
I also vote for using standard C when standard C is servicable.
So if it were up to me alone, the answer is:
I'm NOT going to add any new construct (whether spelled q_verify() or otherwise), and will merely document in the commit message that we discussed this as an alternative (so someone who wants to disable #error can get a git history of what went into the decision).
Also, it sounds like we want to keep it #error, not #warn.
But if anyone else has strong opinions before we promote this from RFC to actual patch, I'm still interested in your arguments.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 4 lines · Eric Blake
Quoted 3 lines · Markus Armbruster
Quoted 1 line · Eric Blake
On 08/22/2017 06:19 AM, Halil Pasic wrote:
[...]
Quoted 19 lines
Quoted 11 lines
Quoted 7 lines
I'd prefer that if we are going to introduce our own construct that always evaluates side effects, and only has a compile-time switch on whether to
abort()or (foolishly) plow on, that we name it something without 'assert' in the name, so that reviewers don't have to be confused about remembering which variant evaluates side effects. Maybe:
q_verify(cond)I vote for frying bigger fish.
I also vote for using standard C when standard C is servicable.
So if it were up to me alone, the answer is:
I'm NOT going to add any new construct (whether spelled
q_verify()or otherwise), and will merely document in the commit message that we discussed this as an alternative (so someone who wants to disable #error can get a git history of what went into the decision).
Quoted 4 lines
Also, it sounds like we want to keep it #error, not #warn.
But if anyone else has strong opinions before we promote this from RFC to actual patch, I'm still interested in your arguments.
You asked for opinions, so here's mine: I agree with you, please do *not* add a new QEMU-specific construct here. assert() should be a well-known C construct that every programmer should have understood. You also need it for other projects. If you haven't understood that it's a macro and has side-effects, you should learn it (e.g. during patch review), not avoid it, otherwise you'll run into problems in another project that is using it again.
But IMHO we should still try to get rid of wrong usage of assert() in the QEMU sources. So maybe we could allow building with NDEBUG one day for the brave people who need the extra percent of additional speed. But as long as we're not there, I think this patch is a good thing to avoid wrong expectations.
Thomas
Quoted 11 lines · Thomas Huth
You asked for opinions, so here's mine: I agree with you, please do *not* add a new QEMU-specific construct here.
assert()should be a well-known C construct that every programmer should have understood. You also need it for other projects. If you haven't understood that it's a macro and has side-effects, you should learn it (e.g. during patch review), not avoid it, otherwise you'll run into problems in another project that is using it again.But IMHO we should still try to get rid of wrong usage of
assert()in the QEMU sources. So maybe we could allow building with NDEBUG one day for the brave people who need the extra percent of additional speed.
It's not only about the side effects. There are a couple cases in migration (IIRC) where our error propagation is not up to the task, and failing assertions are used to validate untrusted input. NDEBUG in that case can introduce a known vulnerability.
Quoted 2 lines
But as long as we're not there, I think this patch is a good thing to avoid wrong expectations.
Agreed.
Paolo
Quoted 18 lines · Paolo Bonzini
Quoted 11 lines · Thomas Huth
You asked for opinions, so here's mine: I agree with you, please do *not* add a new QEMU-specific construct here.
assert()should be a well-known C construct that every programmer should have understood. You also need it for other projects. If you haven't understood that it's a macro and has side-effects, you should learn it (e.g. during patch review), not avoid it, otherwise you'll run into problems in another project that is using it again.But IMHO we should still try to get rid of wrong usage of
assert()in the QEMU sources. So maybe we could allow building with NDEBUG one day for the brave people who need the extra percent of additional speed.It's not only about the side effects. There are a couple cases in migration (IIRC) where our error propagation is not up to the task, and failing assertions are used to validate untrusted input. NDEBUG in that case can introduce a known vulnerability.
Quoted 4 lines
Quoted 2 lines
But as long as we're not there, I think this patch is a good thing to avoid wrong expectations.
Agreed.
Mmm. The only thing that makes me hesitate is that this is moving the flagging up that we rely on assert() causing a failure away from the places where we do that -- if we want in future to fix these migration issues then they'll get harder to find. (If we already think we have more of those that aren't marked by testing NDEBUG then we'd already need to do a wider code audit anyway, though...)
thanks
-- PMM
Quoted 35 lines · Eric Blake
Quoted 19 lines · Markus Armbruster
Quoted 17 lines · Eric Blake
Quoted 6 lines · Halil Pasic
OTOH I do think this is to some degree institutionalizing a bad practice (you say we do not want to do that, but IMHO refusing to build with NDEBUG makes only sense if we want to alter the semantic of assert so that once bad becomes acceptable). I can live with that, but I'm not happy about it. Have we considered rolling our own construct which is designed to exhibit the properties we desire?
I'd prefer that if we are going to introduce our own construct that always evaluates side effects, and only has a compile-time switch on whether to
abort()or (foolishly) plow on, that we name it something without 'assert' in the name, so that reviewers don't have to be confused about remembering which variant evaluates side effects. Maybe:Quoted 7 lines
Quoted 2 lines
q_verify(cond)I vote for frying bigger fish.
I also vote for using standard C when standard C is servicable.
So if it were up to me alone, the answer is:
I'm NOT going to add any new construct (whether spelled
q_verify()or otherwise), and will merely document in the commit message that we discussed this as an alternative (so someone who wants to disable #error can get a git history of what went into the decision).
Quoted 5 lines
Also, it sounds like we want to keep it #error, not #warn.
But if anyone else has strong opinions before we promote this from RFC to actual patch, I'm still interested in your arguments.
I'm fine with this outcome: it is a minimal solution to a real problem. My initiative was a kind of bike-shedding: any devel with enough exposure to qemu to matter will learn soon enough that the assert macro is special in this project.
Regards,
Halil