sandbox: exception handling
v2 cover sandbox: exception handling
Currently if an exception SIGILL, SIGBUS, SIGSEGV occurs the sandbox stops execution. This does not match the behavior on other architectures.
Instead print the current program counter and if any the involved UEFI binaries. Then depending on a customizing system either reset the system or exit to the OS.
When testing UEFI binaries like the Self Certification Test exceptions may occur. Without information about the UEFI binary where the exception was caused it is difficult to analyze the cause.
The exception command is implemented for the sandbox. This command allows to trigger SIGILL or SIGSEGV.
v2:
add a customizing switch
set SA_NODEFER flag for sigaction provide a unit test for the exception command
Heinrich Schuchardt (4):
sandbox: add handler for exceptions
cmd: sandbox: implement exception command
efi_selftest: implement exception test for sandbox
test: unit test for exception command
arch/Kconfig | 1 + arch/sandbox/Kconfig | 9 ++++ arch/sandbox/cpu/os.c | 40 ++++++++++++++++++ arch/sandbox/cpu/start.c | 4 ++ arch/sandbox/lib/interrupts.c | 35 ++++++++++++++++ cmd/Kconfig | 2 +- cmd/Makefile | 1 + cmd/sandbox/Makefile | 3 ++ cmd/sandbox/exception.c | 41 +++++++++++++++++++ include/os.h | 17 ++++++++ .../efi_selftest_miniapp_exception.c | 2 + test/py/tests/test_sandbox_exit.py | 24 +++++++++++ 12 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 cmd/sandbox/Makefile create mode 100644 cmd/sandbox/exception.c
--
2.28.0
PATCH v2 1/4 sandbox: add handler for exceptions
Add a handler for SIGILL, SIGBUS, SIGSEGV.
When an exception occurs print the program counter and the loaded UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y or exit to the OS otherwise.
---
v2:
add a customizing switch
set SA_NODEFER flag for sigaction
--- arch/sandbox/Kconfig | 9 ++++++++ arch/sandbox/cpu/os.c | 40 +++++++++++++++++++++++++++++++++++ arch/sandbox/cpu/start.c | 4 ++++ arch/sandbox/lib/interrupts.c | 35 ++++++++++++++++++++++++++++++ include/os.h | 17 +++++++++++++++ 5 files changed, 105 insertions(+)
arch/sandbox/Kconfig +9 -0
@@ -51,6 +51,15 @@ config HOST_64BIT endchoice +config SANDBOX_CRASH_RESET + bool "Reset on crash" + help + If an illegal instruction or an illegal memory access occurs, the + sandbox by default writes a crash dump and exits. If you set this + flag, the sandbox is reset instead. This may be useful when running + test suites like the UEFI self certification test which continue + with the next test after a crash. + config SANDBOX_BITS_PER_LONG int default 32 if HOST_32BIT
arch/sandbox/cpu/os.c +40 -0
@@ -3,6 +3,8 @@* Copyright (c) 2011 The Chromium OS Authors. */ +#define _GNU_SOURCE +#include <dirent.h> #include <errno.h> #include <fcntl.h>
@@ -15,11 +17,13 @@#include <string.h> #include <termios.h> #include <time.h> +#include <ucontext.h> #include <unistd.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/time.h> #include <sys/types.h> +#include <linux/compiler_attributes.h> #include <linux/types.h> #include <asm/getopt.h>
@@ -191,6 +195,42 @@ static void os_sigint_handler(int sig)raise (SIGINT ); } +static void os_signal_handler (int sig,siginfo_t *info,void *con) +{ +ucontext_t __maybe_unused *context = con; +unsigned long pc; + +#if defined(__x86_64__) + pc = context->uc_mcontext.gregs[REG_RIP ]; +#elif defined(__aarch64__) + pc = context->uc_mcontext.pc; +#elif defined(__riscv) + pc = context->uc_mcontext.__gregs[REG_PC ]; +#else +const char msg[] = +"\nUnsupported architecture, cannot read program counter\n" ; + +os_write (1 , msg,sizeof (msg)); + pc =0 ; +#endif + +os_signal_action (sig, pc); +} + +int os_setup_signal_handlers (void ) +{ +struct sigaction act; + + act.sa_sigaction = os_signal_handler; +sigemptyset (&act.sa_mask); + act.sa_flags =SA_SIGINFO |SA_NODEFER ; +if (sigaction (SIGILL , &act,NULL ) || +sigaction (SIGBUS , &act,NULL ) || +sigaction (SIGSEGV , &act,NULL )) +return -1 ; +return 0 ; +} +/* Put tty into raw mode so <tab> and <ctrl+c> work */ void os_tty_raw (int fd,bool allow_sigs) {
arch/sandbox/cpu/start.c +4 -0
@@ -451,6 +451,10 @@ int main(int argc, char *argv[])if (ret) goto err; + ret =os_setup_signal_handlers (); +if (ret) + goto err; +#if CONFIG_VAL (SYS_MALLOC_F_LEN ) gd->malloc_base =CONFIG_MALLOC_F_ADDR ;#endif
arch/sandbox/lib/interrupts.c +35 -0
@@ -6,7 +6,13 @@*/ #include <common.h> +#include <efi_loader.h> #include <irq_func.h> +#include <os.h> +#include <asm-generic/signal.h> +#include <asm/u-boot-sandbox.h> + +DECLARE_GLOBAL_DATA_PTR ;int interrupt_init (void ) {
@@ -21,3 +27,32 @@ int disable_interrupts(void) {return 0 ; } + +void os_signal_action (int sig,unsigned long pc) +{ +efi_restore_gd (); + +switch (sig) { +case SIGILL : +printf ("\nIllegal instruction\n" ); +break ; +case SIGBUS : +printf ("\nBus error\n" ); +break ; +case SIGSEGV : +printf ("\nSegmentation violation\n" ); +break ; +default : +break ; + } +printf ("pc = 0x%lx, " , pc); +printf ("pc_reloc = 0x%lx\n\n" , pc - gd->reloc_off); +efi_print_image_infos ((void *)pc); + +if (IS_ENABLED (CONFIG_SANDBOX_CRASH_RESET )) { +printf ("resetting ...\n\n" ); +sandbox_reset (); + }else { +sandbox_exit (); + } +}
include/os.h +17 -0
@@ -407,4 +407,21 @@ void *os_find_text_base(void);*/ void os_relaunch (char *argv[]); +/** +* os_setup_signal_handlers() - setup signal handlers +* +* Install signal handlers for SIGBUS and SIGSEGV. +* +* Return: 0 for success +*/ +int os_setup_signal_handlers (void ); + +/** +* os_signal_action() - handle a signal +* +* @sig: signal +* @pc: program counter +*/ +void os_signal_action (int sig,unsigned long pc); +#endif
--
2.28.0
PATCH v2 1/4 sandbox: add handler for exceptions
Hi Heinrich,
Quoted 12 lines · Heinrich Schuchardt
Add a handler for SIGILL, SIGBUS, SIGSEGV.
When an exception occurs print the program counter and the loaded UEFI binaries and reset the system if
CONFIG_SANDBOX_CRASH_RESET=yor exit to the OS otherwise.Signed-off-by Heinrich Schuchardt <[email protected]>---
v2:
add a customizing switch
setSA_NODEFERflag for sigaction
Quoted 7 lines
--- arch/sandbox/Kconfig | 9 ++++++++ arch/sandbox/cpu/os.c | 40 +++++++++++++++++++++++++++++++++++ arch/sandbox/cpu/start.c | 4 ++++ arch/sandbox/lib/interrupts.c | 35 ++++++++++++++++++++++++++++++ include/os.h | 17 +++++++++++++++ 5 files changed, 105 insertions(+)
Do you think a command-line flag might be useful too/instead?
PATCH v2 1/4 sandbox: add handler for exceptions
Quoted 23 lines · Simon Glass
Hi Heinrich,
Quoted 12 lines · Heinrich Schuchardt
Add a handler for SIGILL, SIGBUS, SIGSEGV.
When an exception occurs print the program counter and the loaded UEFI binaries and reset the system if
CONFIG_SANDBOX_CRASH_RESET=yor exit to the OS otherwise.Signed-off-by Heinrich Schuchardt <[email protected]>---
v2:
add a customizing switch
setSA_NODEFERflag for sigactionQuoted 7 lines
--- arch/sandbox/Kconfig | 9 ++++++++ arch/sandbox/cpu/os.c | 40 +++++++++++++++++++++++++++++++++++ arch/sandbox/cpu/start.c | 4 ++++ arch/sandbox/lib/interrupts.c | 35 ++++++++++++++++++++++++++++++ include/os.h | 17 +++++++++++++++ 5 files changed, 105 insertions(+)
Quoted 4 lines
Reviewed-by Simon Glass <[email protected]>Do you think a command-line flag might be useful too/instead?
For my needs the CONFIG flag is enough.
I could not find the mail for patch 2/4 in my inbox. I have tested on aarch64 that the exception work here too.
Best regards
Heinrich
PATCH v2 1/4 sandbox: add handler for exceptions
Quoted 23 lines · Simon Glass
Hi Heinrich,
Quoted 12 lines · Heinrich Schuchardt
Add a handler for SIGILL, SIGBUS, SIGSEGV.
When an exception occurs print the program counter and the loaded UEFI binaries and reset the system if
CONFIG_SANDBOX_CRASH_RESET=yor exit to the OS otherwise.Signed-off-by Heinrich Schuchardt <[email protected]>---
v2:
add a customizing switch
setSA_NODEFERflag for sigactionQuoted 7 lines
--- arch/sandbox/Kconfig | 9 ++++++++ arch/sandbox/cpu/os.c | 40 +++++++++++++++++++++++++++++++++++ arch/sandbox/cpu/start.c | 4 ++++ arch/sandbox/lib/interrupts.c | 35 ++++++++++++++++++++++++++++++ include/os.h | 17 +++++++++++++++ 5 files changed, 105 insertions(+)
Quoted 4 lines
Reviewed-by Simon Glass <[email protected]>Do you think a command-line flag might be useful too/instead?
For my needs the CONFIG flag is enough.
I could not find the mail for patch 2/4 in my inbox. I have tested on aarch64 that the exception work here too.
Best regards
Heinrich
Applied to u-boot-dm, thanks!
PATCH v2 2/4 cmd: sandbox: implement exception command
In reply to Heinrich SchuchardtImplement the commands
* exception undefined - execute an illegal instruction
* exception sigsegv - cause a segment violation
Here is a possible output:
=> exception undefined
Illegal instruction
pc = 0x55eb8d0a7575, pc_reloc = 0x57575
Resetting ...
---
v2:
no change
--- cmd/Kconfig | 2 +- cmd/Makefile | 1 + cmd/sandbox/Makefile | 3 +++ cmd/sandbox/exception.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 cmd/sandbox/Makefile create mode 100644 cmd/sandbox/exception.c
cmd/Kconfig +1 -1
@@ -1682,7 +1682,7 @@ config CMD_EFIDEBUG config CMD_EXCEPTION bool "exception - raise exception" - depends on ARM || RISCV || X86 + depends on ARM || RISCV || SANDBOX || X86 help Enable the 'exception' command which allows to raise an exception.
cmd/Makefile +1 -0
@@ -193,6 +193,7 @@ obj-$(CONFIG_CMD_AVB) += avb.o obj-$(CONFIG_ARM) += arm/ obj-$(CONFIG_RISCV) += riscv/ +obj-$(CONFIG_SANDBOX) += sandbox/ obj-$(CONFIG_X86) += x86/ obj-$(CONFIG_ARCH_MVEBU) += mvebu/
cmd/sandbox/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0+ + +obj-$(CONFIG_CMD_EXCEPTION) += exception.o
cmd/sandbox/exception.c new file +41 -0
41 lines not on this page · raw message
--
2.28.0
PATCH v2 2/4 cmd: sandbox: implement exception command
Implement the commands
* exception undefined - execute an illegal instruction
* exception sigsegv - cause a segment violation
Here is a possible output:
=> exception undefined
Illegal instruction
pc = 0x55eb8d0a7575, pc_reloc = 0x57575
Resetting ...
---
v2:
no change
--- cmd/Kconfig | 2 +- cmd/Makefile | 1 + cmd/sandbox/Makefile | 3 +++ cmd/sandbox/exception.c | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 cmd/sandbox/Makefile create mode 100644 cmd/sandbox/exception.c
Applied to u-boot-dm, thanks!
PATCH v2 3/4 efi_selftest: implement exception test for sandbox
In reply to Heinrich SchuchardtProvide a unit test that causes an illegal instruction to occur.
The test can be run with the following commands:
=> setenv efi_selftest exception
=> bootefi selftest
This might be the output:
Executing 'exception'
EFI application triggers exception.
Illegal instruction
pc = 0x1444d016, pc_reloc = 0xffffaa078e8dd016
UEFI image [0x0000000000000000:0xffffffffffffffff] '/\selftest'
UEFI image [0x000000001444b000:0x0000000014451fff] pc=0x2016 '/bug.efi'
Resetting ...It would tell us that the exception was triggered by an instruction 0x2016 bytes after the load address of the binary with filename /bug.efi.
---
v2:
no change
--- lib/efi_selftest/efi_selftest_miniapp_exception.c | 2 ++ 1 file changed, 2 insertions(+)
lib/efi_selftest/efi_selftest_miniapp_exception.c +2 -0
@@ -33,6 +33,8 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle, asmvolatile (".word 0xe7f7defb\n" );#elif defined(CONFIG_RISCV ) asmvolatile (".word 0xffffffff\n" ); +#elif defined(CONFIG_SANDBOX ) + asmvolatile (".word 0xffffffff\n" );#elif defined(CONFIG_X86 ) asmvolatile (".word 0xffff\n" );#endif
--
2.28.0
PATCH v2 3/4 efi_selftest: implement exception test for sandbox
Quoted 21 lines · Heinrich Schuchardt
Provide a unit test that causes an illegal instruction to occur.
The test can be run with the following commands:
=> setenv
efi_selftestexception
=> bootefi selftestThis might be the output:
Executing 'exception' EFI application triggers exception. Illegal instruction pc = 0x1444d016, pc_reloc = 0xffffaa078e8dd016 UEFI image [0x0000000000000000:0xffffffffffffffff] '/\selftest' UEFI image [0x000000001444b000:0x0000000014451fff] pc=0x2016 '/bug.efi' Resetting ...It would tell us that the exception was triggered by an instruction
0x2016bytes after the load address of the binary with filename /bug.efi.
Quoted 7 lines
Signed-off-by Heinrich Schuchardt <[email protected]>---
v2:
no change--- lib/efi_selftest/efi_selftest_miniapp_exception.c | 2 ++ 1 file changed, 2 insertions(+)
lib/efi_selftest/efi_selftest_miniapp_exception.c efi_status_t EFIAPI efi_main(efi_handle_t handle, · line 37
Quoted 11 lines
lib/efi_selftest/efi_selftest_miniapp_exception.c +2
-0@@ -33,6 +33,8 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle, asmvolatile (".word 0xe7f7defb\n" );#elif defined(CONFIG_RISCV ) asmvolatile (".word 0xffffffff\n" ); +#elif defined(CONFIG_SANDBOX ) + asmvolatile (".word 0xffffffff\n" );
Does this work on ARM hosts?
Quoted 6 lines
lib/efi_selftest/efi_selftest_miniapp_exception.c +0
-1#elif defined(CONFIG_X86 ) asmvolatile (".word 0xffff\n" );#endif --2.28.0
PATCH v2 3/4 efi_selftest: implement exception test for sandbox
Quoted 33 lines · Simon Glass
Quoted 21 lines · Heinrich Schuchardt
Provide a unit test that causes an illegal instruction to occur.
The test can be run with the following commands:
=> setenv
efi_selftestexception
=> bootefi selftestThis might be the output:
Executing 'exception' EFI application triggers exception. Illegal instruction pc = 0x1444d016, pc_reloc = 0xffffaa078e8dd016 UEFI image [0x0000000000000000:0xffffffffffffffff] '/\selftest' UEFI image [0x000000001444b000:0x0000000014451fff] pc=0x2016 '/bug.efi' Resetting ...It would tell us that the exception was triggered by an instruction
0x2016bytes after the load address of the binary with filename /bug.efi.Quoted 7 lines
Signed-off-by Heinrich Schuchardt <[email protected]>---
v2:
no change--- lib/efi_selftest/efi_selftest_miniapp_exception.c | 2 ++ 1 file changed, 2 insertions(+)Reviewed-by Simon Glass <[email protected]>Quoted 1 line
Quoted 12 lines
lib/efi_selftest/efi_selftest_miniapp_exception.c efi_status_t EFIAPI efi_main(efi_handle_t handle, · line 37
Quoted 10 lines
lib/efi_selftest/efi_selftest_miniapp_exception.c +2
-0@@ -33,6 +33,8 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle, asmvolatile (".word 0xe7f7defb\n" );#elif defined(CONFIG_RISCV ) asmvolatile (".word 0xffffffff\n" ); +#elif defined(CONFIG_SANDBOX ) + asmvolatile (".word 0xffffffff\n" );Does this work on ARM hosts?
Yes it works on aarch64.
Best regards
Heinrich
Quoted 8 lines
Quoted 8 lines
lib/efi_selftest/efi_selftest_miniapp_exception.c +0
-1#elif defined(CONFIG_X86 ) asmvolatile (".word 0xffff\n" );#endif --2.28.0
PATCH v2 3/4 efi_selftest: implement exception test for sandbox
Quoted 30 lines · Simon Glass
Quoted 21 lines · Heinrich Schuchardt
Provide a unit test that causes an illegal instruction to occur.
The test can be run with the following commands:
=> setenv
efi_selftestexception
=> bootefi selftestThis might be the output:
Executing 'exception' EFI application triggers exception. Illegal instruction pc = 0x1444d016, pc_reloc = 0xffffaa078e8dd016 UEFI image [0x0000000000000000:0xffffffffffffffff] '/\selftest' UEFI image [0x000000001444b000:0x0000000014451fff] pc=0x2016 '/bug.efi' Resetting ...It would tell us that the exception was triggered by an instruction
0x2016bytes after the load address of the binary with filename /bug.efi.Quoted 7 lines
Signed-off-by Heinrich Schuchardt <[email protected]>---
v2:
no change--- lib/efi_selftest/efi_selftest_miniapp_exception.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 3 lines
Reviewed-by Simon Glass <[email protected]>Quoted 1 line
Applied to u-boot-dm, thanks!
PATCH v2 4/4 test: unit test for exception command
In reply to Heinrich SchuchardtTest that an exception SIGILL is answered by a reset on the sandbox if CONFIG_SANDBOX_CRASH_RESET=y or by exiting to the OS otherwise.
---
v2:
new patch
--- arch/Kconfig | 1 + test/py/tests/test_sandbox_exit.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
arch/Kconfig +1 -0
@@ -112,6 +112,7 @@ config SANDBOX imply BITREVERSE select BLOBLIST imply CMD_DM + imply CMD_EXCEPTION imply CMD_GETTIME imply CMD_HASH imply CMD_IO
test/py/tests/test_sandbox_exit.py +24 -0
@@ -19,3 +19,27 @@ def test_ctrl_c(u_boot_console): u_boot_console.kill(signal.SIGINT) assert(u_boot_console.validate_exited()) + + at pytest.mark.boardspec('sandbox') + at pytest.mark.buildconfigspec('cmd_exception') + at pytest.mark.buildconfigspec('sandbox_crash_reset') +def test_exception_reset(u_boot_console): + """Test that SIGILL causes a reset.""" + + u_boot_console.run_command('exception undefined', wait_for_prompt=False) + m = u_boot_console.p.expect(['resetting ...', 'U-Boot']) + if m != 0: + raise Exception('SIGILL did not lead to reset') + m = u_boot_console.p.expect(['U-Boot', '=>']) + if m != 0: + raise Exception('SIGILL did not lead to reset') + u_boot_console.restart_uboot() + + at pytest.mark.boardspec('sandbox') + at pytest.mark.buildconfigspec('cmd_exception') + at pytest.mark.notbuildconfigspec('sandbox_crash_reset') +def test_exception_exit(u_boot_console): + """Test that SIGILL causes a reset.""" + + u_boot_console.run_command('exception undefined', wait_for_prompt=False) + assert(u_boot_console.validate_exited())
--
2.28.0
PATCH v2 4/4 test: unit test for exception command
Quoted 4 lines · Heinrich Schuchardt
Test that an exception SIGILL is answered by a reset on the sandbox if
CONFIG_SANDBOX_CRASH_RESET=yor by exiting to the OS otherwise.
Quoted 8 lines
Signed-off-by Heinrich Schuchardt <[email protected]>---
v2:
new patch--- arch/Kconfig | 1 + test/py/tests/test_sandbox_exit.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)