26 #ifdef HAVE_VALGRIND_MEMCHECK_H
27 # include <valgrind/memcheck.h>
28 # ifndef VALGRIND_MAKE_MEM_DEFINED
29 # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n))
31 # ifndef VALGRIND_MAKE_MEM_UNDEFINED
32 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
35 # define VALGRIND_MAKE_MEM_DEFINED(p, n) 0
36 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) 0
39 #if defined(__native_client__) && defined(NACL_NEWLIB)
43 #ifdef NEED_RUBY_ATOMIC_OPS
64 #if defined(__BEOS__) || defined(__HAIKU__)
69 # define NSIG (_SIGMAX + 1)
161 {
"VTALRM", SIGVTALRM},
188 {
"DANGER", SIGDANGER},
191 {
"MIGRATE", SIGMIGRATE},
200 {
"RETRACT", SIGRETRACT},
217 if (strcmp(sigs->
signm, nm) == 0)
228 if (sigs->
signo == no)
278 if (!
NIL_P(sig)) argnum = 2;
284 if (signo < 0 || signo >
NSIG) {
302 if (strncmp(signm,
"SIG", 3) == 0) signm += 3;
388 #define killpg(pg, sig) kill(-(pg), (sig))
399 switch (
TYPE(argv[0])) {
416 if (strncmp(
"SIG", s, 3) == 0)
436 if (argc <= 1)
return INT2FIX(0);
440 for (i=1; i<
argc; i++) {
446 const rb_pid_t
self = (
GET_THREAD() ==
GET_VM()->main_thread) ? getpid() : -1;
449 for (i=1; i<
argc; i++) {
452 if ((sig != 0) && (
self != -1) && (pid ==
self)) {
474 if (t < 0 &&
kill(pid, sig))
482 else if (
kill(pid, sig) < 0) {
501 #define sighandler_t sh_t
503 #define sighandler_t ruby_sighandler_t
507 #ifdef USE_SIGALTSTACK
509 #define SIGINFO_ARG , siginfo_t *info, void *ctx
515 #ifdef USE_SIGALTSTACK
523 if (size < MINSIGSTKSZ)
526 #if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
529 pagesize = (
int)sysconf(_SC_PAGE_SIZE);
542 stack_t newSS, oldSS;
545 rb_bug(
"rb_register_sigaltstack: th->altstack not initialized\n");
547 newSS.ss_sp = th->altstack;
551 sigaltstack(&newSS, &oldSS);
559 struct sigaction sigact, old;
562 rb_trap_accept_nativethreads[signum] = 0;
565 sigemptyset(&sigact.sa_mask);
566 #ifdef USE_SIGALTSTACK
568 sigact.sa_flags = SA_SIGINFO;
570 sigact.sa_handler = handler;
575 if (signum == SIGCHLD && handler == SIG_IGN)
576 sigact.sa_flags |= SA_NOCLDWAIT;
578 #if defined(SA_ONSTACK) && defined(USE_SIGALTSTACK)
579 if (signum == SIGSEGV
584 sigact.sa_flags |= SA_ONSTACK;
587 if (sigaction(signum, &sigact, &old) < 0) {
592 if (old.sa_flags & SA_SIGINFO)
595 return old.sa_handler;
612 return signal(signum, handler);
616 #define ruby_signal(sig,handler) ( signal((sig),(handler)))
619 ruby_nativethread_signal(
int signum,
sighandler_t handler)
623 old =
signal(signum, handler);
624 rb_trap_accept_nativethreads[signum] = 1;
635 struct sigaction old;
637 if (sigaction(sig,
NULL, &old) < 0)
return FALSE;
638 func = old.sa_handler;
644 if (func == SIG_IGN)
return 1;
660 #if !defined(BSD_SIGNAL) && !defined(POSIX_SIGNAL)
678 #ifdef HAVE_PTHREAD_SIGMASK
688 #ifdef HAVE_PTHREAD_SIGMASK
714 #if defined(USE_SIGALTSTACK) || defined(_WIN32)
716 #if defined(HAVE_UCONTEXT_H) && defined __linux__ && (defined __i386__ || defined __x86_64__)
717 # define USE_UCONTEXT_REG 1
719 #ifdef USE_UCONTEXT_REG
721 check_stack_overflow(
const uintptr_t addr,
const ucontext_t *
ctx)
724 const greg_t sp = ctx->uc_mcontext.gregs[REG_RSP];
726 const greg_t sp = ctx->uc_mcontext.gregs[REG_ESP];
728 enum {pagesize = 4096};
730 const uintptr_t fault_page = addr / pagesize;
734 if (sp_page == fault_page || sp_page == fault_page + 1) {
747 check_stack_overflow(
const void *addr)
749 int ruby_stack_overflowed_p(
const rb_thread_t *,
const void *);
751 if (ruby_stack_overflowed_p(th, addr)) {
757 #define CHECK_STACK_OVERFLOW() check_stack_overflow(0)
759 #define FAULT_ADDRESS info->si_addr
760 # ifdef USE_UCONTEXT_REG
761 # define CHECK_STACK_OVERFLOW() check_stack_overflow((uintptr_t)FAULT_ADDRESS, ctx)
763 # define CHECK_STACK_OVERFLOW() check_stack_overflow(FAULT_ADDRESS)
765 #define MESSAGE_FAULT_ADDRESS " at %p", FAULT_ADDRESS
768 #define CHECK_STACK_OVERFLOW() (void)0
770 #ifndef MESSAGE_FAULT_ADDRESS
771 #define MESSAGE_FAULT_ADDRESS
784 #if defined __APPLE__ || defined __linux__
806 static int segv_received = 0;
814 char msg[] =
"SEGV received in SEGV handler\n";
816 err = write(2, msg,
sizeof(msg));
823 ruby_disable_gc_stress = 1;
832 volatile unsigned long old_interrupt_mask = cur_th->
interrupt_mask;
978 if (!
NIL_P(command)) {
986 if (strncmp(
RSTRING_PTR(command),
"SYSTEM_DEFAULT", 14) == 0) {
992 if (strncmp(
RSTRING_PTR(command),
"SIG_IGN", 7) == 0) {
997 else if (strncmp(
RSTRING_PTR(command),
"SIG_DFL", 7) == 0) {
1002 else if (strncmp(
RSTRING_PTR(command),
"DEFAULT", 7) == 0) {
1007 if (strncmp(
RSTRING_PTR(command),
"IGNORE", 6) == 0) {
1012 if (strncmp(
RSTRING_PTR(command),
"EXIT", 4) == 0) {
1034 switch (
TYPE(vsig)) {
1037 if (sig < 0 || sig >=
NSIG) {
1051 if (strncmp(
"SIG", s, 3) == 0)
1054 if (sig == 0 && strcmp(s,
"EXIT") != 0)
1077 if (oldfunc == SIG_IGN) oldcmd =
rb_str_new2(
"IGNORE");
1099 if (signo == SIGSEGV)
1103 if (signo == SIGBUS)
1107 if (signo == SIGILL)
1111 if (signo == SIGFPE)
1117 if (signo == SIGVTALRM)
1187 return trap(sig, func, cmd);
1220 if (old != SIG_DFL) {
1226 #if defined(SIGCLD) || defined(SIGCHLD)
1228 init_sigchld(
int sig)
1234 if (oldfunc != SIG_DFL && oldfunc != SIG_IGN) {
1237 GET_VM()->trap_list[sig].cmd = 0;
1256 #ifndef RUBY_DEBUG_ENV
1257 #define ruby_enable_coredump 0
1332 if (!ruby_enable_coredump) {
1337 # ifdef USE_SIGALTSTACK
1348 init_sigchld(SIGCLD);
1349 #elif defined(SIGCHLD)
1350 init_sigchld(SIGCHLD);
static VALUE sig_list(void)
static void signal_exec(VALUE cmd, int safe, int sig)
#define MESSAGE_FAULT_ADDRESS
static void signal_enque(int sig)
void ruby_thread_stack_overflow(rb_thread_t *th)
void rb_bug(const char *fmt,...)
static VALUE trap(int sig, sighandler_t func, VALUE command)
const char * rb_obj_classname(VALUE)
VALUE rb_iv_set(VALUE, const char *, VALUE)
VALUE rb_eval_cmd(VALUE, VALUE, int)
#define UNLIMITED_ARGUMENTS
#define GetProcPtr(obj, ptr)
void ruby_kill(rb_pid_t pid, int sig)
static int signal_ignored(int sig)
SSL_METHOD *(* func)(void)
static VALUE interrupt_init(int argc, VALUE *argv, VALUE self)
static void rb_enable_interrupt(void)
VALUE rb_str_new_cstr(const char *)
void rb_threadptr_signal_raise(rb_thread_t *th, int sig)
VALUE rb_iv_get(VALUE, const char *)
static struct signals siglist[]
RETSIGTYPE ruby_sigaction_t(int)
void rb_signal_exec(rb_thread_t *th, int sig)
#define ruby_enable_coredump
void rb_raise(VALUE exc, const char *fmt,...)
#define SafeStringValue(v)
static VALUE esignal_signo(VALUE self)
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
static struct @175 signal_buff
static sighandler_t trap_handler(VALUE *cmd, int sig)
rb_atomic_t ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp, rb_atomic_t newval)
static VALUE sig_signame(VALUE recv, VALUE signo)
sighandler_t posix_signal(int signum, sighandler_t handler)
VALUE rb_block_proc(void)
int ruby_disable_gc_stress
static int reserved_signal_p(int signo)
void rb_thread_wakeup_timer_thread(void)
#define StringValuePtr(v)
static RETSIGTYPE sighandler(int sig)
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
static void install_sighandler(int signum, sighandler_t handler)
VALUE rb_check_to_integer(VALUE, const char *)
void rb_threadptr_check_signal(rb_thread_t *mth)
VALUE rb_call_super(int, const VALUE *)
static sighandler_t default_handler(int sig)
static VALUE sig_trap(int argc, VALUE *argv)
static int signm2signo(const char *nm)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
VALUE rb_thread_current(void)
VALUE rb_sprintf(const char *format,...)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
const char * ruby_signal_name(int)
VALUE rb_f_kill(int, VALUE *)
void rb_alias(VALUE, ID, ID)
static const char * signo2signm(int no)
rb_thread_t * ruby_current_thread
#define VALGRIND_MAKE_MEM_DEFINED(p, n)
rb_hash_aset(hash, RARRAY_AREF(key_value_pair, 0), RARRAY_AREF(key_value_pair, 1))
static void rb_disable_interrupt(void)
void rb_bug_errno(const char *mesg, int errno_arg)
void rb_sys_fail(const char *mesg)
#define CHECK_STACK_OVERFLOW()
sighandler_t signal(int signum, sighandler_t handler)
unsigned long interrupt_mask
void rb_thread_execute_interrupts(VALUE th)
void ruby_sig_finalize(void)
struct rb_vm_struct::@190 trap_list[RUBY_NSIG]
int rb_signal_buff_size(void)
static VALUE esignal_init(int argc, VALUE *argv, VALUE self)
void ruby_default_signal(int)
static int trap_signm(VALUE vsig)
const char * rb_id2name(ID id)
#define ruby_signal(sig, handler)
int rb_sigaltstack_size(void)
VALUE rb_define_module(const char *name)
int rb_get_next_signal(void)
VALUE rb_check_string_type(VALUE)
static rb_thread_t * GET_THREAD(void)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
rb_atomic_t ruby_atomic_exchange(rb_atomic_t *ptr, rb_atomic_t val)
void rb_threadptr_signal_exit(rb_thread_t *th)