When running under libfuzzer for example, it's better to trap, so
libfuzzer generates a crash input file than to just print an error
message and exit gracefully.

The same can apply when running under a hardware debugger, so make
this option available generally.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 common/Kconfig | 9 +++++++++
 common/misc.c  | 2 ++
 2 files changed, 11 insertions(+)

diff --git a/common/Kconfig b/common/Kconfig
index ad211d1fa519..0664ffcb43a5 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -376,6 +376,7 @@ config RELOCATABLE
 
 choice
        prompt "Configure action on fatal error"
+       default PANIC_TRAP if SANDBOX
        default PANIC_RESET
 
        config PANIC_POWEROFF
@@ -407,6 +408,14 @@ choice
                  fatal error, so you don't have to reset it manually.
                  This is the recommended configuration in production.
 
+       config PANIC_TRAP
+               bool "invoke a trap instruction"
+               help
+                 This option triggers the trap instruction emitted by
+                 __builtin_trap() on fatal errors.
+                 This is mainly useful when running under a debugger
+                 or debugging instrumentation.
+
 endchoice
 
 config PROMPT
diff --git a/common/misc.c b/common/misc.c
index f2c084a0df4b..67f88af1a3df 100644
--- a/common/misc.c
+++ b/common/misc.c
@@ -305,6 +305,8 @@ static void __noreturn do_panic(bool stacktrace, const char 
*fmt, va_list ap)
 
        if (IS_ENABLED(CONFIG_PANIC_POWEROFF))
                poweroff_machine(0);
+       else if (IS_ENABLED(CONFIG_PANIC_TRAP))
+               __builtin_trap();
        else
                restart_machine(0);
 }
-- 
2.39.5


Reply via email to