github.com/goccy/go-jit@v0.0.0-20200514131505-ff78d45cf6af/internal/ccall/jit-signal.c (about) 1 /* 2 * jit-signal.c - Internal management routines to use Operating System 3 * signals for libjit exceptions handling. 4 * 5 * Copyright (C) 2006 Southern Storm Software, Pty Ltd. 6 * 7 * This file is part of the libjit library. 8 * 9 * The libjit library is free software: you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public License 11 * as published by the Free Software Foundation, either version 2.1 of 12 * the License, or (at your option) any later version. 13 * 14 * The libjit library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with the libjit library. If not, see 21 * <http://www.gnu.org/licenses/>. 22 */ 23 24 #include "jit-internal.h" 25 26 #ifdef JIT_USE_SIGNALS 27 28 #include <signal.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 32 /* 33 * Use SIGSEGV for builtin libjit exception. 34 */ 35 static void sigsegv_handler(int signum, siginfo_t *info, void *uap) 36 { 37 jit_exception_builtin(JIT_RESULT_NULL_REFERENCE); 38 } 39 40 /* 41 * Use SIGFPE for builtin libjit exception. 42 */ 43 static void sigfpe_handler(int signum, siginfo_t *info, void *uap) 44 { 45 switch(info->si_code) 46 { 47 case FPE_INTDIV: 48 jit_exception_builtin(JIT_RESULT_DIVISION_BY_ZERO); 49 break; 50 case FPE_INTOVF: 51 jit_exception_builtin(JIT_RESULT_OVERFLOW); 52 break; 53 case FPE_FLTDIV: 54 jit_exception_builtin(JIT_RESULT_DIVISION_BY_ZERO); 55 break; 56 case FPE_FLTOVF: 57 jit_exception_builtin(JIT_RESULT_OVERFLOW); 58 break; 59 case FPE_FLTUND: 60 jit_exception_builtin(JIT_RESULT_ARITHMETIC); 61 break; 62 case FPE_FLTSUB: 63 jit_exception_builtin(JIT_RESULT_ARITHMETIC); 64 break; 65 default: 66 jit_exception_builtin(JIT_RESULT_ARITHMETIC); 67 break; 68 } 69 } 70 71 /* 72 * Initialize signal handlers. 73 */ 74 void _jit_signal_init(void) 75 { 76 struct sigaction sa_fpe, sa_segv; 77 78 sa_fpe.sa_sigaction = sigfpe_handler; 79 sigemptyset(&sa_fpe.sa_mask); 80 sa_fpe.sa_flags = SA_SIGINFO; 81 if (sigaction(SIGFPE, &sa_fpe, 0)) { 82 perror("Sigaction SIGFPE"); 83 exit(1); 84 } 85 86 sa_segv.sa_sigaction = sigsegv_handler; 87 sigemptyset(&sa_segv.sa_mask); 88 sa_segv.sa_flags = SA_SIGINFO; 89 if (sigaction(SIGSEGV, &sa_segv, 0)) { 90 perror("Sigaction SIGSEGV"); 91 exit(1); 92 } 93 } 94 95 #endif /* JIT_USE_SIGNALS */