Make the critical level an enum

This commit is contained in:
Nobuyoshi Nakada 2025-04-25 11:15:37 +09:00 committed by Nobuyoshi Nakada
parent c6dd07d66f
commit af6b98f7a2
3 changed files with 16 additions and 13 deletions

View file

@ -241,4 +241,11 @@ rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type)
return RB_TYPE_P(obj, T_DATA) && RTYPEDDATA_P(obj) && (RTYPEDDATA_TYPE(obj) == data_type); return RB_TYPE_P(obj, T_DATA) && RTYPEDDATA_P(obj) && (RTYPEDDATA_TYPE(obj) == data_type);
} }
typedef enum {
rb_stack_overflow_prevention = 0, // VM stack overflow or about to machine stack overflow
rb_stack_overflow_signal = 1, // machine stack overflow but may be recoverable
rb_stack_overflow_fatal = 2, // fatal machine stack overflow
} ruby_stack_overflow_critical_level;
NORETURN(void rb_ec_stack_overflow(struct rb_execution_context_struct *ec, ruby_stack_overflow_critical_level crit));
#endif /* INTERNAL_ERROR_H */ #endif /* INTERNAL_ERROR_H */

View file

@ -760,7 +760,6 @@ static const char *received_signal;
#endif #endif
#if defined(USE_SIGALTSTACK) || defined(_WIN32) #if defined(USE_SIGALTSTACK) || defined(_WIN32)
NORETURN(void rb_ec_stack_overflow(rb_execution_context_t *ec, int crit));
# if defined __HAIKU__ # if defined __HAIKU__
# define USE_UCONTEXT_REG 1 # define USE_UCONTEXT_REG 1
# elif !(defined(HAVE_UCONTEXT_H) && (defined __i386__ || defined __x86_64__ || defined __amd64__)) # elif !(defined(HAVE_UCONTEXT_H) && (defined __i386__ || defined __x86_64__ || defined __amd64__))
@ -846,18 +845,21 @@ check_stack_overflow(int sig, const uintptr_t addr, const ucontext_t *ctx)
if (sp_page == fault_page || sp_page == fault_page + 1 || if (sp_page == fault_page || sp_page == fault_page + 1 ||
(sp_page <= fault_page && fault_page <= bp_page)) { (sp_page <= fault_page && fault_page <= bp_page)) {
rb_execution_context_t *ec = GET_EC(); rb_execution_context_t *ec = GET_EC();
int crit = FALSE; ruby_stack_overflow_critical_level crit = rb_stack_overflow_signal;
int uplevel = roomof(pagesize, sizeof(*ec->tag)) / 2; /* XXX: heuristic */ int uplevel = roomof(pagesize, sizeof(*ec->tag)) / 2; /* XXX: heuristic */
while ((uintptr_t)ec->tag->buf / pagesize <= fault_page + 1) { while ((uintptr_t)ec->tag->buf / pagesize <= fault_page + 1) {
/* drop the last tag if it is close to the fault, /* drop the last tag if it is close to the fault,
* otherwise it can cause stack overflow again at the same * otherwise it can cause stack overflow again at the same
* place. */ * place. */
if ((crit = (!ec->tag->prev || !--uplevel)) != FALSE) break; if (!ec->tag->prev || !--uplevel) {
crit = rb_stack_overflow_fatal;
break;
}
rb_vm_tag_jmpbuf_deinit(&ec->tag->buf); rb_vm_tag_jmpbuf_deinit(&ec->tag->buf);
ec->tag = ec->tag->prev; ec->tag = ec->tag->prev;
} }
reset_sigmask(sig); reset_sigmask(sig);
rb_ec_stack_overflow(ec, crit + 1); rb_ec_stack_overflow(ec, crit);
} }
} }
# else # else

View file

@ -79,24 +79,18 @@ vm_stackoverflow(void)
ec_stack_overflow(GET_EC(), TRUE); ec_stack_overflow(GET_EC(), TRUE);
} }
NORETURN(void rb_ec_stack_overflow(rb_execution_context_t *ec, int crit));
/* critical level
* 0: VM stack overflow or about to machine stack overflow
* 1: machine stack overflow but may be recoverable
* 2: fatal machine stack overflow
*/
void void
rb_ec_stack_overflow(rb_execution_context_t *ec, int crit) rb_ec_stack_overflow(rb_execution_context_t *ec, ruby_stack_overflow_critical_level crit)
{ {
if (rb_during_gc()) { if (rb_during_gc()) {
rb_bug("system stack overflow during GC. Faulty native extension?"); rb_bug("system stack overflow during GC. Faulty native extension?");
} }
if (crit > 1) { if (crit >= rb_stack_overflow_fatal) {
ec->raised_flag = RAISED_STACKOVERFLOW; ec->raised_flag = RAISED_STACKOVERFLOW;
ec->errinfo = rb_ec_vm_ptr(ec)->special_exceptions[ruby_error_stackfatal]; ec->errinfo = rb_ec_vm_ptr(ec)->special_exceptions[ruby_error_stackfatal];
EC_JUMP_TAG(ec, TAG_RAISE); EC_JUMP_TAG(ec, TAG_RAISE);
} }
ec_stack_overflow(ec, crit == 0); ec_stack_overflow(ec, crit < rb_stack_overflow_signal);
} }
static inline void stack_check(rb_execution_context_t *ec); static inline void stack_check(rb_execution_context_t *ec);