From 96e6ecaf7d70db519342d5018143fc59d2e1ff37 Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Sun, 10 Nov 2019 16:04:25 +0100 Subject: [PATCH] core: Add abort. * lib/stub/__raise.c: New file. * kaem.run: Use it. * src/posix.c (__raise)[SYSTEM_LIBC]: New macro. (abort_): New function: possibly use it. * include/mes/builtins.h: Declare it. * src/builtins.c (mes_builtins): Register it. --- build-aux/configure-lib.sh | 1 + include/mes/builtins.h | 1 + include/mes/lib.h | 1 + kaem.run | 1 + lib/mes/div.c | 2 +- lib/stdlib/abort.c | 11 ++++++----- lib/stub/__raise.c | 27 +++++++++++++++++++++++++++ src/builtins.c | 1 + src/posix.c | 19 +++++++++++++++++++ 9 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 lib/stub/__raise.c diff --git a/build-aux/configure-lib.sh b/build-aux/configure-lib.sh index 2e561d02..0f09d489 100644 --- a/build-aux/configure-lib.sh +++ b/build-aux/configure-lib.sh @@ -107,6 +107,7 @@ lib/mes/ntoab.c lib/mes/oputc.c lib/mes/ultoa.c lib/mes/utoa.c +lib/stub/__raise.c " if test $mes_libc = mes; then diff --git a/include/mes/builtins.h b/include/mes/builtins.h index 331be984..c5f5aacc 100644 --- a/include/mes/builtins.h +++ b/include/mes/builtins.h @@ -103,6 +103,7 @@ struct scm *module_variable (struct scm *module, struct scm *name); struct scm *module_ref (struct scm *module, struct scm *name); struct scm *module_define_x (struct scm *module, struct scm *name, struct scm *value); /* src/posix.c */ +struct scm *abort_ (); struct scm *exit_ (struct scm *x); struct scm *peek_byte (); struct scm *read_byte (); diff --git a/include/mes/lib.h b/include/mes/lib.h index 3504207f..549cca1d 100644 --- a/include/mes/lib.h +++ b/include/mes/lib.h @@ -84,5 +84,6 @@ unsigned long __mesabi_uldiv (unsigned long a, unsigned long b, void *__memcpy (void *dest, void const *src, size_t n); void *__memmove (void *dest, void const *src, size_t n); void *__memset (void *s, int c, size_t n); +int __raise (int signal); #endif //__MES_LIB_H diff --git a/kaem.run b/kaem.run index 31452ee1..827fdac0 100644 --- a/kaem.run +++ b/kaem.run @@ -37,6 +37,7 @@ M2-Planet \ -f lib/mes/mini-write.c \ -f lib/linux/${mes_cpu}-mes-m2/syscall.c \ -f include/linux/${mes_cpu}/syscall.h \ + -f lib/stub/__raise.c \ -f lib/linux/brk.c \ -f lib/m2/malloc.c \ -f lib/string/memset.c \ diff --git a/lib/mes/div.c b/lib/mes/div.c index a192a96c..4ea0ce32 100644 --- a/lib/mes/div.c +++ b/lib/mes/div.c @@ -33,7 +33,7 @@ struct ldiv_t int __raise (int); -#if __TINYC__ +#if __TINYC__ || SYSTEM_LIBC #define __raise(x) -1 #endif diff --git a/lib/stdlib/abort.c b/lib/stdlib/abort.c index e496561d..7c55da9c 100644 --- a/lib/stdlib/abort.c +++ b/lib/stdlib/abort.c @@ -23,9 +23,10 @@ void abort (void) { - if (raise (SIGABRT) < 0) { /* could not raise SIGABRT */ - /* Fail in any way possible */ - unsigned char* x = (unsigned char*) 0; - *x = 2; - } + if (raise (SIGABRT) < 0) /* could not raise SIGABRT */ + { + /* Fail in any way possible */ + unsigned char* x = (unsigned char*) 0; + *x = 2; + } } diff --git a/lib/stub/__raise.c b/lib/stub/__raise.c new file mode 100644 index 00000000..9467513c --- /dev/null +++ b/lib/stub/__raise.c @@ -0,0 +1,27 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2020 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +// CONSTANT SIGABRT 0 + +int +__raise (int signum) +{ + return -1; +} diff --git a/src/builtins.c b/src/builtins.c index c602e05e..c4ebb927 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -213,6 +213,7 @@ mes_builtins (struct scm *a) /*:((internal)) */ a = init_builtin (builtin_type, "module-ref", 2, &module_ref, a); a = init_builtin (builtin_type, "module-define!", 3, &module_define_x, a); /* src/posix.c */ + a = init_builtin (builtin_type, "abort", 0, &abort_, a); a = init_builtin (builtin_type, "exit", 1, &exit_, a); a = init_builtin (builtin_type, "peek-byte", 0, &peek_byte, a); a = init_builtin (builtin_type, "read-byte", 0, &read_byte, a); diff --git a/src/posix.c b/src/posix.c index ed887f11..d59bfc90 100644 --- a/src/posix.c +++ b/src/posix.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,24 @@ #include #include +#if SYSTEM_LIBC +#define __raise(x) -1 +#endif + +struct scm * +abort_ () /*:((name . "abort")) */ +{ + if (g_debug > 0) + eputs ("abort!\n"); + if (__raise (SIGABRT) < 0) /* could not raise SIGABRT */ + { + /* Fail in any way possible */ + char* x = 0; + x[0] = 2; + } + return cell_unspecified; +} + struct scm * exit_ (struct scm *x) /*:((name . "exit")) */ {