Skip to content

Commit af6b98f

Browse files
committed
Make the critical level an enum
1 parent c6dd07d commit af6b98f

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

internal/error.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,11 @@ rb_typeddata_is_instance_of_inline(VALUE obj, const rb_data_type_t *data_type)
241241
return RB_TYPE_P(obj, T_DATA) && RTYPEDDATA_P(obj) && (RTYPEDDATA_TYPE(obj) == data_type);
242242
}
243243

244+
typedef enum {
245+
rb_stack_overflow_prevention = 0, // VM stack overflow or about to machine stack overflow
246+
rb_stack_overflow_signal = 1, // machine stack overflow but may be recoverable
247+
rb_stack_overflow_fatal = 2, // fatal machine stack overflow
248+
} ruby_stack_overflow_critical_level;
249+
NORETURN(void rb_ec_stack_overflow(struct rb_execution_context_struct *ec, ruby_stack_overflow_critical_level crit));
250+
244251
#endif /* INTERNAL_ERROR_H */

signal.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,6 @@ static const char *received_signal;
760760
#endif
761761

762762
#if defined(USE_SIGALTSTACK) || defined(_WIN32)
763-
NORETURN(void rb_ec_stack_overflow(rb_execution_context_t *ec, int crit));
764763
# if defined __HAIKU__
765764
# define USE_UCONTEXT_REG 1
766765
# 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)
846845
if (sp_page == fault_page || sp_page == fault_page + 1 ||
847846
(sp_page <= fault_page && fault_page <= bp_page)) {
848847
rb_execution_context_t *ec = GET_EC();
849-
int crit = FALSE;
848+
ruby_stack_overflow_critical_level crit = rb_stack_overflow_signal;
850849
int uplevel = roomof(pagesize, sizeof(*ec->tag)) / 2; /* XXX: heuristic */
851850
while ((uintptr_t)ec->tag->buf / pagesize <= fault_page + 1) {
852851
/* drop the last tag if it is close to the fault,
853852
* otherwise it can cause stack overflow again at the same
854853
* place. */
855-
if ((crit = (!ec->tag->prev || !--uplevel)) != FALSE) break;
854+
if (!ec->tag->prev || !--uplevel) {
855+
crit = rb_stack_overflow_fatal;
856+
break;
857+
}
856858
rb_vm_tag_jmpbuf_deinit(&ec->tag->buf);
857859
ec->tag = ec->tag->prev;
858860
}
859861
reset_sigmask(sig);
860-
rb_ec_stack_overflow(ec, crit + 1);
862+
rb_ec_stack_overflow(ec, crit);
861863
}
862864
}
863865
# else

vm_insnhelper.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,18 @@ vm_stackoverflow(void)
7979
ec_stack_overflow(GET_EC(), TRUE);
8080
}
8181

82-
NORETURN(void rb_ec_stack_overflow(rb_execution_context_t *ec, int crit));
83-
/* critical level
84-
* 0: VM stack overflow or about to machine stack overflow
85-
* 1: machine stack overflow but may be recoverable
86-
* 2: fatal machine stack overflow
87-
*/
8882
void
89-
rb_ec_stack_overflow(rb_execution_context_t *ec, int crit)
83+
rb_ec_stack_overflow(rb_execution_context_t *ec, ruby_stack_overflow_critical_level crit)
9084
{
9185
if (rb_during_gc()) {
9286
rb_bug("system stack overflow during GC. Faulty native extension?");
9387
}
94-
if (crit > 1) {
88+
if (crit >= rb_stack_overflow_fatal) {
9589
ec->raised_flag = RAISED_STACKOVERFLOW;
9690
ec->errinfo = rb_ec_vm_ptr(ec)->special_exceptions[ruby_error_stackfatal];
9791
EC_JUMP_TAG(ec, TAG_RAISE);
9892
}
99-
ec_stack_overflow(ec, crit == 0);
93+
ec_stack_overflow(ec, crit < rb_stack_overflow_signal);
10094
}
10195

10296
static inline void stack_check(rb_execution_context_t *ec);

0 commit comments

Comments
 (0)