lib: sbi: add some macros to detect BUG at runtime
PATCH v4 1/2 lib: sbi: add some macros to detect BUG at runtime
Three macros are added. One is called BUG, which is used to put in an unreachable branch. One is called BUG_ON, which is used to check bugs and assert conditions are opposite. One is called SBI_ASSERT, used for assertion checking.
--- include/sbi/sbi_console.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
include/sbi/sbi_console.h +19 -0
@@ -11,6 +11,8 @@#define __SBI_CONSOLE_H__#include <sbi/sbi_types.h> +#include <sbi/riscv_asm.h> +#include <sbi/sbi_hart.h> struct sbi_console_device {/** Name of the console device */
@@ -51,4 +53,21 @@ struct sbi_scratch;int sbi_console_init (struct sbi_scratch *scratch); +#define BUG () do { \ + sbi_printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \ + sbi_hart_hang(); \ +} while (0) + +#define BUG_ON (cond) do { \ + if (cond) \ + BUG(); \ +} while (0) + +#define SBI_ASSERT (cond) do { \ + if (!(cond)) { \ + sbi_printf("ASSERT: %s:%d/%s(): Assertion `%s` failed.\n", __FILE__,__LINE__,__func__, #cond);\ + sbi_hart_hang(); \ + } \ +} while (0) +#endif
Signature · 1 line
2.30.2
PATCH v4 2/2 lib: sbi: Add runtime bug detection for csr_read_num/csr_write_num/misa_string
--- lib/sbi/riscv_asm.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/sbi/riscv_asm.c +4 -0
@@ -11,6 +11,7 @@#include <sbi/riscv_encoding.h> #include <sbi/sbi_error.h> #include <sbi/sbi_platform.h> +#include <sbi/sbi_console.h> /* determine CPU extension, return non-zero support */ int misa_extension_imp (char ext)
@@ -75,6 +76,7 @@ void misa_string(int xlen, char *out, unsigned int out_sz) out[pos++] ='8' ;break ;default : +BUG ();return ; } }
@@ -134,6 +136,7 @@ unsigned long csr_read_num(int csr_num)#endif default : +BUG ();break ; };
@@ -197,6 +200,7 @@ void csr_write_num(int csr_num, unsigned long val) switchcase_csr_write_16(CSR_MHPMEVENT16, val)default : +BUG ();break ; };
Signature · 1 line
2.30.2
PATCH v4 2/2 lib: sbi: Add runtime bug detection for csr_read_num/csr_write_num/misa_string
On Thu, Sep 16, 2021 at 10:03 AM Xiang W <[email protected]> wrote:
Please try to write some description here and keep the patch subject within the 80 character limit.
Quoted 2 lines
Signed-off-by Xiang W <[email protected]>
Otherwise it looks good to me.
I have updated patch subject and description at the time of merging this patch. Applied this patch to the riscv/opensbi repo.
Regards,
Anup
Quoted 48 lines
--- lib/sbi/riscv_asm.c | 4 ++++ 1 file changed, 4 insertions(+)lib/sbi/riscv_asm.c +4
-1@@ -11,6 +11,7 @@#include <sbi/riscv_encoding.h> #include <sbi/sbi_error.h> #include <sbi/sbi_platform.h> +#include <sbi/sbi_console.h> /* determine CPU extension, return non-zero support */ int misa_extension_imp (char ext)@@ -75,6 +76,7 @@ void misa_string(int xlen, char *out, unsigned int out_sz) out[pos++] ='8' ;break ;default : +BUG ();return ; } }@@ -134,6 +136,7 @@ unsigned long csr_read_num(int csr_num)#endif default : +BUG ();break ; };@@ -197,6 +200,7 @@ void csr_write_num(int csr_num, unsigned long val) switchcase_csr_write_16(CSR_MHPMEVENT16, val)default : +BUG ();break ; }; --2.30.2
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 1/2 lib: sbi: add some macros to detect BUG at runtime
In reply to Xiang WQuoted 16 lines · Xiang W
Three macros are added. One is called BUG, which is used to put in an unreachable branch. One is called
BUG_ON, which is used to check bugs and assert conditions are opposite. One is calledSBI_ASSERT, used for assertion checking.Signed-off-by Xiang W <[email protected]>--- include/sbi/sbi_console.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)include/sbi/sbi_console.h +0
-0
include/sbi/sbi_console.h line 14
Quoted 4 lines
include/sbi/sbi_console.h +1
-0@@ -11,6 +11,8 @@#define __SBI_CONSOLE_H__#include <sbi/sbi_types.h> +#include <sbi/riscv_asm.h>
Including "sbi/riscv_asm.h" is redundant with the use of sbi_hart_hang().
include/sbi/sbi_console.h struct sbi_scratch; · line 57
Quoted 10 lines
include/sbi/sbi_console.h +3
-0+#include <sbi/sbi_hart.h> struct sbi_console_device {/** Name of the console device */ @@ -51,4 +53,21 @@ struct sbi_scratch;int sbi_console_init (struct sbi_scratch *scratch); +#define BUG () do { \ + sbi_printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
This line should be within 80 characters.
include/sbi/sbi_console.h struct sbi_scratch; · line 68
Quoted 11 lines
include/sbi/sbi_console.h +11
-0+sbi_hart_hang (); \ +} while (0) + +#define BUG_ON (cond) do { \ + if (cond) \ + BUG(); \ +} while (0) + +#define SBI_ASSERT (cond) do { \ + if (!(cond)) { \ + sbi_printf("ASSERT: %s:%d/%s(): Assertion `%s` failed.\n", __FILE__,__LINE__,__func__, #cond);\
same as above.
Quoted 9 lines
include/sbi/sbi_console.h +4
-1+ sbi_hart_hang(); \ + } \ +}while (0 ) + #endif --2.30.2
Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Otherwise, this patch looks good.
I have taken care of the above minor comments while merging this patch. Applied this patch to the riscv/opensbi repo.
Regards,
Anup
PATCH v4 1/2 lib: sbi: add some macros to detect BUG at runtime
Quoted 68 lines · Anup Patel
Quoted 16 lines · Xiang W
Three macros are added. One is called BUG, which is used to put in an unreachable branch. One is called
BUG_ON, which is used to check bugs and assert conditions are opposite. One is calledSBI_ASSERT, used for assertion checking.Signed-off-by Xiang W <[email protected]>---
include/sbi/sbi_console.h| 19 +++++++++++++++++++1 file changed, 19 insertions(+)include/sbi/sbi_console.h +0
-0include/sbi/sbi_console.h line 14
Quoted 4 lines
include/sbi/sbi_console.h +1
-0@@ -11,6 +11,8 @@#define __SBI_CONSOLE_H__#include <sbi/sbi_types.h> +#include <sbi/riscv_asm.h> Including "
sbi/riscv_asm.h" is redundant with the use ofsbi_hart_hang().include/sbi/sbi_console.h struct sbi_scratch; · line 57
Quoted 10 lines
include/sbi/sbi_console.h +3
-0+#include <sbi/sbi_hart.h> struct sbi_console_device {/** Name of the console device */ @@ -51,4 +53,21 @@ struct sbi_scratch;int sbi_console_init (struct sbi_scratch *scratch); +#define BUG () do { \ + sbi_printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \This line should be within 80 characters.
include/sbi/sbi_console.h struct sbi_scratch; · line 68
Quoted 11 lines
include/sbi/sbi_console.h +11
-0+sbi_hart_hang (); \ +} while (0) + +#define BUG_ON (cond) do { \ + if (cond) \ + BUG(); \ +} while (0) + +#define SBI_ASSERT (cond) do { \ + if (!(cond)) { \ + sbi_printf("ASSERT: %s:%d/%s(): Assertion `%s` failed.\n", __FILE__,__LINE__,__func__, #cond);\same as above.
Quoted 9 lines
include/sbi/sbi_console.h +4
-1+ sbi_hart_hang(); \ + } \ +}while (0 ) + #endif --2.30.2
Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbiOtherwise, this patch looks good.
Quoted 4 lines
Reviewed-by Anup Patel <[email protected]>I have taken care of the above minor comments while merging this patch. Applied this patch to the riscv/opensbi repo.
Hi Anup,
I?m concerned about the fact this was committed despite two of us raising concerns about the APIs themselves that remain unaddressed in v4 (one of them was half addressed in that SBI_ASSERT was added, but BUG_ON, the thing we suggested might not be a good idea to have, remains and so now there are *two* ways of writing the same thing). Here?s the relevant part of the thread for v2:
Me:
Quoted 9 lines
Mitchell Horne:
Quoted 6 lines
Maybe it should be named ASSERT or
SBI_ASSERTthen? It does not seem like your other patch even uses this however.In my opinion,
BUG()andBUG_ON()are confusing names to begin with; they do not obviously describe their semantics. If you insist on using these names, their behaviour should match Linux.
Quoted 5 lines
Also BUG is a bit of a lazy interface; line numbers change over time, really you should be using some kind of panic function that takes a format string with a meaningful message. That also gives the user some hope of knowing what went wrong and maybe working around or fixing it. And why not reuse
sbi_hart_hangrather than inlining another copy of it?
Obviously the matching Linux part has been fixed, as has reusing sbi_hart_hang, but the other fundamental API concerns remain. I believe these APIs encourage lazy code writing that will result in useless error messages unless you?re a developer who can go read the source (and know exactly what files it was built from), rather than forcing people to think about writing an error message to communicate the problem to the user or developer. Just because Linux defines BUG and BUG_ON doesn?t mean that they?re a good idea and that OpenSBI should copy them (but even Linux also provides a printf-like panic function).
Jess
PATCH v4 1/2 lib: sbi: add some macros to detect BUG at runtime
Quoted 75 lines · Jessica Clarke
Quoted 68 lines · Anup Patel
Quoted 16 lines · Xiang W
Three macros are added. One is called BUG, which is used to put in an unreachable branch. One is called
BUG_ON, which is used to check bugs and assert conditions are opposite. One is calledSBI_ASSERT, used for assertion checking.Signed-off-by Xiang W <[email protected]>---
include/sbi/sbi_console.h| 19 +++++++++++++++++++1 file changed, 19 insertions(+)include/sbi/sbi_console.h +0
-0include/sbi/sbi_console.h line 14
Quoted 4 lines
include/sbi/sbi_console.h +1
-0@@ -11,6 +11,8 @@#define __SBI_CONSOLE_H__#include <sbi/sbi_types.h> +#include <sbi/riscv_asm.h> Including "
sbi/riscv_asm.h" is redundant with the use ofsbi_hart_hang().include/sbi/sbi_console.h struct sbi_scratch; · line 57
Quoted 10 lines
include/sbi/sbi_console.h +3
-0+#include <sbi/sbi_hart.h> struct sbi_console_device {/** Name of the console device */ @@ -51,4 +53,21 @@ struct sbi_scratch;int sbi_console_init (struct sbi_scratch *scratch); +#define BUG () do { \ + sbi_printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \This line should be within 80 characters.
include/sbi/sbi_console.h struct sbi_scratch; · line 68
Quoted 11 lines
include/sbi/sbi_console.h +11
-0+sbi_hart_hang (); \ +} while (0) + +#define BUG_ON (cond) do { \ + if (cond) \ + BUG(); \ +} while (0) + +#define SBI_ASSERT (cond) do { \ + if (!(cond)) { \ + sbi_printf("ASSERT: %s:%d/%s(): Assertion `%s` failed.\n", __FILE__,__LINE__,__func__, #cond);\same as above.
Quoted 9 lines
include/sbi/sbi_console.h +4
-1+ sbi_hart_hang(); \ + } \ +}while (0 ) + #endif --2.30.2
Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbiOtherwise, this patch looks good.
Quoted 4 lines
Reviewed-by Anup Patel <[email protected]>I have taken care of the above minor comments while merging this patch. Applied this patch to the riscv/opensbi repo.
Quoted 7 lines
Hi Anup,
I?m concerned about the fact this was committed despite two of us raising concerns about the APIs themselves that remain unaddressed in v4 (one of them was half addressed in thatSBI_ASSERTwas added, butBUG_ON, the thing we suggested might not be a good idea to have, remains and so now there are *two* ways of writing the same thing). Here?s the relevant part of the thread for v2:
I thought all comments were already addressed. When there are no Reviewed-by, I generally wait for a week before reviewing it myself and merging it. I guess I should wait a little more when there are no Reviewed-by tags.
I also request you (and everyone), if there are unaddressed comments then please drop a one-line statement in the latest patch so that we don't accidentally ignore unaddressed comments.
Overall, I did not find anything objectionable at my end.
Quoted 17 lines
Me:
Quoted 9 lines
Mitchell Horne:
Quoted 6 lines
Maybe it should be named ASSERT or
SBI_ASSERTthen? It does not seem like your other patch even uses this however.In my opinion,
BUG()andBUG_ON()are confusing names to begin with; they do not obviously describe their semantics. If you insist on using these names, their behaviour should match Linux.Quoted 5 lines
Also BUG is a bit of a lazy interface; line numbers change over time, really you should be using some kind of panic function that takes a format string with a meaningful message. That also gives the user some hope of knowing what went wrong and maybe working around or fixing it. And why not reuse
sbi_hart_hangrather than inlining another copy of it?
Quoted 9 lines
Obviously the matching Linux part has been fixed, as has reusing
sbi_hart_hang, but the other fundamental API concerns remain. I believe these APIs encourage lazy code writing that will result in useless error messages unless you?re a developer who can go read the source (and know exactly what files it was built from), rather than forcing people to think about writing an error message to communicate the problem to the user or developer. Just because Linux defines BUG andBUG_ONdoesn?t mean that they?re a good idea and that OpenSBI should copy them (but even Linux also provides a printf-like panic function).
I am not attached to the BUG()/BUG_ON() macro names and the way they are implemented so feel free to send a patch for doing this better way. These macros are recently added so it is better to improve them now before they are spread throughout the entire code base.
Regards,
Anup
Quoted 3 lines
Jess