Add control for the native stack check with hardware trap (#1682)

Add a new options to control the native stack hw bound check feature:
- Besides the original option `cmake -DWAMR_DISABLE_HW_BOUND_CHECK=1/0`,
  add a new option `cmake -DWAMR_DISABLE_STACK_HW_BOUND_CHECK=1/0`
- When the linear memory hw bound check is disabled, the stack hw bound check
   will be disabled automatically, no matter what the input option is
- When the linear memory hw bound check is enabled, the stack hw bound check
  is enabled/disabled according to the value of input option
- Besides the original option `--bounds-checks=1/0`, add a new option
  `--stack-bounds-checks=1/0` for wamrc

Refer to: https://github.com/bytecodealliance/wasm-micro-runtime/issues/1677
This commit is contained in:
Wenyong Huang
2022-11-07 18:26:33 +08:00
committed by GitHub
parent 810007857b
commit 7fd37190e8
15 changed files with 103 additions and 7 deletions

View File

@ -425,6 +425,7 @@ os_thread_get_stack_boundary()
*/
static os_thread_local_attribute bool thread_signal_inited = false;
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
/* The signal alternate stack base addr */
static os_thread_local_attribute uint8 *sigalt_stack_base_addr;
@ -488,6 +489,7 @@ destroy_stack_guard_pages()
os_mprotect(stack_min_addr, page_size * guard_page_count,
MMAP_PROT_READ | MMAP_PROT_WRITE);
}
#endif /* end of WASM_DISABLE_STACK_HW_BOUND_CHECK == 0 */
static void
mask_signals(int how)
@ -553,13 +555,16 @@ int
os_thread_signal_init(os_signal_handler handler)
{
struct sigaction sig_act;
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
stack_t sigalt_stack_info;
uint32 map_size = SIG_ALT_STACK_SIZE;
uint8 *map_addr;
#endif
if (thread_signal_inited)
return 0;
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
if (!init_stack_guard_pages()) {
os_printf("Failed to init stack guard pages\n");
return -1;
@ -581,13 +586,17 @@ os_thread_signal_init(os_signal_handler handler)
os_printf("Failed to init signal alternate stack\n");
goto fail2;
}
#endif
memset(&prev_sig_act_SIGSEGV, 0, sizeof(struct sigaction));
memset(&prev_sig_act_SIGBUS, 0, sizeof(struct sigaction));
/* Install signal hanlder */
sig_act.sa_sigaction = signal_callback;
sig_act.sa_flags = SA_SIGINFO | SA_ONSTACK | SA_NODEFER;
sig_act.sa_flags = SA_SIGINFO | SA_NODEFER;
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
sig_act.sa_flags |= SA_ONSTACK;
#endif
sigemptyset(&sig_act.sa_mask);
if (sigaction(SIGSEGV, &sig_act, &prev_sig_act_SIGSEGV) != 0
|| sigaction(SIGBUS, &sig_act, &prev_sig_act_SIGBUS) != 0) {
@ -595,12 +604,15 @@ os_thread_signal_init(os_signal_handler handler)
goto fail3;
}
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
sigalt_stack_base_addr = map_addr;
#endif
signal_handler = handler;
thread_signal_inited = true;
return 0;
fail3:
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
memset(&sigalt_stack_info, 0, sizeof(stack_t));
sigalt_stack_info.ss_flags = SS_DISABLE;
sigalt_stack_info.ss_size = map_size;
@ -609,17 +621,21 @@ fail2:
os_munmap(map_addr, map_size);
fail1:
destroy_stack_guard_pages();
#endif
return -1;
}
void
os_thread_signal_destroy()
{
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
stack_t sigalt_stack_info;
#endif
if (!thread_signal_inited)
return;
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
/* Disable signal alternate stack */
memset(&sigalt_stack_info, 0, sizeof(stack_t));
sigalt_stack_info.ss_flags = SS_DISABLE;
@ -629,6 +645,7 @@ os_thread_signal_destroy()
os_munmap(sigalt_stack_base_addr, SIG_ALT_STACK_SIZE);
destroy_stack_guard_pages();
#endif
thread_signal_inited = false;
}
@ -648,6 +665,7 @@ os_signal_unmask()
void
os_sigreturn()
{
#if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
#if defined(__APPLE__)
#define UC_RESET_ALT_STACK 0x80000000
extern int __sigreturn(void *, int);
@ -656,5 +674,6 @@ os_sigreturn()
after exiting the signal handler. */
__sigreturn(NULL, UC_RESET_ALT_STACK);
#endif
#endif
}
#endif /* end of OS_ENABLE_HW_BOUND_CHECK */