diff options
-rw-r--r-- | cmd/srv9p/static9p.c | 3 | ||||
-rw-r--r-- | lib9p/9p.generated.c | 5 | ||||
-rwxr-xr-x | lib9p/idl.gen | 3 | ||||
-rw-r--r-- | lib9p/include/lib9p/9p.generated.h | 2 | ||||
-rw-r--r-- | lib9p/include/lib9p/9p.h | 3 | ||||
-rw-r--r-- | lib9p/map.h | 2 | ||||
-rw-r--r-- | lib9p/srv.c | 2 | ||||
-rw-r--r-- | libcr/CMakeLists.txt | 3 | ||||
-rw-r--r-- | libcr/coroutine.c | 16 | ||||
-rw-r--r-- | libcr_ipc/include/libcr_ipc/_linkedlist.h | 2 | ||||
-rw-r--r-- | libcr_ipc/include/libcr_ipc/select.h | 9 | ||||
-rw-r--r-- | libhw/host_alarmclock.c | 2 | ||||
-rw-r--r-- | libhw/host_net.c | 2 | ||||
-rw-r--r-- | libhw/rp2040_hwspi.c | 3 | ||||
-rw-r--r-- | libhw/rp2040_hwtimer.c | 3 | ||||
-rw-r--r-- | libhw/w5500_ll.h | 4 | ||||
-rw-r--r-- | libmisc/CMakeLists.txt | 3 | ||||
-rw-r--r-- | libmisc/assert.c | 23 | ||||
-rw-r--r-- | libmisc/include/libmisc/assert.h | 24 | ||||
-rw-r--r-- | libmisc/include/libmisc/endian.h | 3 | ||||
-rw-r--r-- | libmisc/include/libmisc/vcall.h | 3 | ||||
-rw-r--r-- | libusb/CMakeLists.txt | 1 | ||||
-rw-r--r-- | libusb/usb_common.c | 3 |
23 files changed, 86 insertions, 38 deletions
diff --git a/cmd/srv9p/static9p.c b/cmd/srv9p/static9p.c index 3632c26..90f3856 100644 --- a/cmd/srv9p/static9p.c +++ b/cmd/srv9p/static9p.c @@ -4,8 +4,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <assert.h> - +#include <libmisc/assert.h> #include <libmisc/vcall.h> #include "static9p.h" diff --git a/lib9p/9p.generated.c b/lib9p/9p.generated.c index 6136433..1a00f10 100644 --- a/lib9p/9p.generated.c +++ b/lib9p/9p.generated.c @@ -1,11 +1,12 @@ -/* Generated by `./lib9p/idl.gen lib9p/idl/2002-9P2000.9p lib9p/idl/2005-9P2000.u.9p lib9p/idl/2012-9P2000.e.9p`. DO NOT EDIT! */ +/* Generated by `lib9p/idl.gen lib9p/idl/2002-9P2000.9p lib9p/idl/2005-9P2000.u.9p lib9p/idl/2012-9P2000.e.9p`. DO NOT EDIT! */ -#include <assert.h> #include <stdbool.h> #include <stddef.h> /* for size_t */ #include <inttypes.h> /* for PRI* macros */ #include <string.h> /* for memset() */ +#include <libmisc/assert.h> + #include <lib9p/9p.h> #include "internal.h" diff --git a/lib9p/idl.gen b/lib9p/idl.gen index ec42cfd..3be1d7e 100755 --- a/lib9p/idl.gen +++ b/lib9p/idl.gen @@ -681,12 +681,13 @@ def gen_c(versions: set[str], typs: list[Type]) -> str: ret = f"""/* Generated by `{' '.join(sys.argv)}`. DO NOT EDIT! */ -#include <assert.h> #include <stdbool.h> #include <stddef.h> /* for size_t */ #include <inttypes.h> /* for PRI* macros */ #include <string.h> /* for memset() */ +#include <libmisc/assert.h> + #include <lib9p/9p.h> #include "internal.h" diff --git a/lib9p/include/lib9p/9p.generated.h b/lib9p/include/lib9p/9p.generated.h index d5e94d1..feca8c9 100644 --- a/lib9p/include/lib9p/9p.generated.h +++ b/lib9p/include/lib9p/9p.generated.h @@ -1,4 +1,4 @@ -/* Generated by `./lib9p/idl.gen lib9p/idl/2002-9P2000.9p lib9p/idl/2005-9P2000.u.9p lib9p/idl/2012-9P2000.e.9p`. DO NOT EDIT! */ +/* Generated by `lib9p/idl.gen lib9p/idl/2002-9P2000.9p lib9p/idl/2005-9P2000.u.9p lib9p/idl/2012-9P2000.e.9p`. DO NOT EDIT! */ #ifndef _LIB9P_9P_H_ #error Do not include <lib9p/9p.generated.h> directly; include <lib9p/9p.h> instead diff --git a/lib9p/include/lib9p/9p.h b/lib9p/include/lib9p/9p.h index 171ad3b..9d22354 100644 --- a/lib9p/include/lib9p/9p.h +++ b/lib9p/include/lib9p/9p.h @@ -7,10 +7,11 @@ #ifndef _LIB9P_9P_H_ #define _LIB9P_9P_H_ -#include <assert.h> #include <stdbool.h> #include <sys/types.h> /* for ssize_t */ +#include <libmisc/assert.h> + #include <lib9p/linux-errno.h> /* configuration **************************************************************/ diff --git a/lib9p/map.h b/lib9p/map.h index c816bde..f42bb2c 100644 --- a/lib9p/map.h +++ b/lib9p/map.h @@ -84,7 +84,7 @@ static VAL_T *MAP_METHOD(NAME,store)(struct NAME *m, KEY_T k, VAL_T v) { m->items[i].val = v; return &(m->items[i].val); } - __builtin_unreachable(); + assert_notreached("should have returned from inside for() loop"); } /** diff --git a/lib9p/srv.c b/lib9p/srv.c index 9192794..e4212e5 100644 --- a/lib9p/srv.c +++ b/lib9p/srv.c @@ -4,7 +4,6 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <assert.h> #include <alloca.h> #include <inttypes.h> /* for PRI* */ #include <stdio.h> /* for fprintf(), stderr */ @@ -14,6 +13,7 @@ #include <libcr_ipc/chan.h> #include <libcr_ipc/mutex.h> #include <libcr_ipc/select.h> +#include <libmisc/assert.h> #include <libmisc/vcall.h> #define IMPLEMENTATION_FOR_LIB9P_SRV_H YES diff --git a/libcr/CMakeLists.txt b/libcr/CMakeLists.txt index ae7c8fe..fbc7618 100644 --- a/libcr/CMakeLists.txt +++ b/libcr/CMakeLists.txt @@ -8,6 +8,9 @@ target_include_directories(libcr SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/incl target_sources(libcr INTERFACE coroutine.c ) +target_link_libraries(libcr INTERFACE + libmisc +) target_compile_options(libcr INTERFACE -fno-split-stack ) diff --git a/libcr/coroutine.c b/libcr/coroutine.c index 41d987e..ed1c8e0 100644 --- a/libcr/coroutine.c +++ b/libcr/coroutine.c @@ -4,12 +4,13 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <assert.h> #include <setjmp.h> /* for setjmp(), longjmp(), jmp_buf */ #include <stdint.h> /* for uint8_t */ #include <stdio.h> /* for printf(), fprintf(), stderr */ #include <stdlib.h> /* for aligned_alloc(), free() */ +#include <libmisc/assert.h> + #include <libcr/coroutine.h> /* Configuration **************************************************************/ @@ -363,17 +364,6 @@ static cid_t coroutine_running = 0; #define debugf(...) #endif -#ifdef __GLIBC__ - #define assertf(expr, ...) ({ \ - if (!(expr)) { \ - errorf("assertion: " __VA_ARGS__); \ - __assert_fail(#expr, __FILE__, __LINE__, __func__); \ - } \ - }) -#else - #define assertf(expr, ...) assert(expr) -#endif - static inline const char* coroutine_state_str(enum coroutine_state state) { assert(state < ARRAY_LEN(coroutine_state_strs)); return coroutine_state_strs[state]; @@ -470,7 +460,7 @@ cid_t coroutine_add_with_stack_size(size_t stack_size, cr_fn_t fn, void *args) { debugf("...stack_base=%p\n", stack_base); /* run until cr_begin() */ cr_plat_call_with_stack(stack_base, fn, args); - __builtin_unreachable(); /* should cr_begin() instead of returning */ + assert_notreached("should cr_begin() instead of returning"); } assert_cid_state(child, state == CR_RUNNABLE); if (parent) diff --git a/libcr_ipc/include/libcr_ipc/_linkedlist.h b/libcr_ipc/include/libcr_ipc/_linkedlist.h index 43b7286..e5aa52a 100644 --- a/libcr_ipc/include/libcr_ipc/_linkedlist.h +++ b/libcr_ipc/include/libcr_ipc/_linkedlist.h @@ -7,7 +7,7 @@ #ifndef _LIBCR_IPC__LINKEDLIST_H_ #define _LIBCR_IPC__LINKEDLIST_H_ -#include <assert.h> +#include <libmisc/assert.h> /* singly linked list *********************************************************/ diff --git a/libcr_ipc/include/libcr_ipc/select.h b/libcr_ipc/include/libcr_ipc/select.h index f094662..ce14416 100644 --- a/libcr_ipc/include/libcr_ipc/select.h +++ b/libcr_ipc/include/libcr_ipc/select.h @@ -5,11 +5,12 @@ */ #include <alloca.h> /* for alloca() */ -#include <assert.h> /* for assert() */ #include <stdarg.h> /* for va_* */ #include <stddef.h> /* for size_t */ #include <stdlib.h> /* for random() */ +#include <libmisc/assert.h> + #include <libcr_ipc/chan.h> #ifndef _LIBCR_IPC_SELECT_H_ @@ -98,7 +99,7 @@ static inline enum _cr_select_class _cr_select_getclass(struct cr_select_arg arg case _CR_SELECT_OP_DEFAULT: return _CR_SELECT_CLASS_DEFAULT; default: - __builtin_unreachable(); + assert_notreached("invalid arg.op"); } } @@ -154,14 +155,14 @@ static size_t cr_select_v(size_t arg_cnt, struct cr_select_arg arg_vec[]) { seen++; } } - __builtin_unreachable(); + assert_notreached("should have returned from inside for() loop"); } if (cnt_default) { for (size_t i = 0; i < arg_cnt; i++) if (_cr_select_getclass(arg_vec[i]) == _CR_SELECT_CLASS_DEFAULT) return i; - __builtin_unreachable(); + assert_notreached("should have returned from inside for() loop"); } struct _cr_select_waiters waiters = { diff --git a/libhw/host_alarmclock.c b/libhw/host_alarmclock.c index 141927c..4f43fc7 100644 --- a/libhw/host_alarmclock.c +++ b/libhw/host_alarmclock.c @@ -4,13 +4,13 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <assert.h> #include <errno.h> #include <error.h> #include <signal.h> #include <time.h> #include <libcr/coroutine.h> +#include <libmisc/assert.h> #include <libmisc/vcall.h> #define IMPLEMENTATION_FOR_LIBHW_GENERIC_ALARMCLOCK_H YES diff --git a/libhw/host_net.c b/libhw/host_net.c index 88bda49..29886f3 100644 --- a/libhw/host_net.c +++ b/libhw/host_net.c @@ -6,7 +6,6 @@ #define _GNU_SOURCE /* for pthread_sigqueue(3gnu) */ /* misc */ -#include <assert.h> /* for assert() */ #include <errno.h> /* for errno, EAGAIN, EINVAL */ #include <error.h> /* for error(3gnu) */ #include <stdlib.h> /* for abs(), shutdown(), SHUT_RD, SHUT_WR, SHUT_RDWR */ @@ -20,6 +19,7 @@ #include <signal.h> /* for siginfo_t, struct sigaction, enum sigval, sigaction(), SA_SIGINFO */ #include <libcr/coroutine.h> +#include <libmisc/assert.h> #include <libmisc/vcall.h> #include <libhw/generic/alarmclock.h> diff --git a/libhw/rp2040_hwspi.c b/libhw/rp2040_hwspi.c index 4edcdf7..29cfb91 100644 --- a/libhw/rp2040_hwspi.c +++ b/libhw/rp2040_hwspi.c @@ -4,11 +4,10 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <assert.h> - #include <hardware/spi.h> /* pico-sdk:hardware_spi */ #include <hardware/gpio.h> /* pico-sdk:hardware_gpio */ +#include <libmisc/assert.h> #include <libmisc/vcall.h> #define IMPLEMENTATION_FOR_LIBHW_RP2040_HWSPI_H YES diff --git a/libhw/rp2040_hwtimer.c b/libhw/rp2040_hwtimer.c index 54bdab3..69c2a50 100644 --- a/libhw/rp2040_hwtimer.c +++ b/libhw/rp2040_hwtimer.c @@ -4,12 +4,11 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <assert.h> - #include <hardware/irq.h> /* pico-sdk:hardware_irq */ #include <hardware/timer.h> /* pico-sdk:hardware_timer */ #include <libcr/coroutine.h> +#include <libmisc/assert.h> #include <libmisc/vcall.h> #define IMPLEMENTATION_FOR_LIBHW_GENERIC_ALARMCLOCK_H YES diff --git a/libhw/w5500_ll.h b/libhw/w5500_ll.h index db66f01..1f578a9 100644 --- a/libhw/w5500_ll.h +++ b/libhw/w5500_ll.h @@ -10,12 +10,12 @@ #ifndef _LIBHW_W5500_LL_H_ #define _LIBHW_W5500_LL_H_ -#include <assert.h> /* for assert(), static_assert() */ #include <stdint.h> /* for uint{n}_t */ #include <string.h> /* for memcmp() */ -#include <libmisc/vcall.h> /* for VCALL() */ +#include <libmisc/assert.h> /* for assert(), static_assert() */ #include <libmisc/endian.h> /* for uint16be_t */ +#include <libmisc/vcall.h> /* for VCALL() */ #include <libhw/generic/net.h> /* for struct net_eth_addr, struct net_ip4_addr */ #include <libhw/generic/spi.h> /* for implements_spi */ diff --git a/libmisc/CMakeLists.txt b/libmisc/CMakeLists.txt index 0c5c019..1f30906 100644 --- a/libmisc/CMakeLists.txt +++ b/libmisc/CMakeLists.txt @@ -5,4 +5,7 @@ add_library(libmisc INTERFACE) target_include_directories(libmisc SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_sources(libmisc INTERFACE + assert.c +) target_compile_options(libmisc INTERFACE "$<$<COMPILE_LANGUAGE:C>:-fplan9-extensions>") diff --git a/libmisc/assert.c b/libmisc/assert.c new file mode 100644 index 0000000..7817ba3 --- /dev/null +++ b/libmisc/assert.c @@ -0,0 +1,23 @@ +/* libmisc/assert.h - More assertions + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <stdio.h> /* for fprintf(), stderr */ +#include <stdlib.h> /* for abort() */ + +#include <libmisc/assert.h> + +#ifndef NDEBUG +__attribute__((__noreturn__)) void +__assert_msg_fail(const char *expr, + const char *file, unsigned int line, const char *func, + const char *msg) { + fprintf(stderr, "error: %s:%u:%s(): assertion \"%s\" failed%s%s\n", + file, line, func, + expr, + msg ? ": " : "", msg); + abort(); +} +#endif diff --git a/libmisc/include/libmisc/assert.h b/libmisc/include/libmisc/assert.h new file mode 100644 index 0000000..e7e0eb0 --- /dev/null +++ b/libmisc/include/libmisc/assert.h @@ -0,0 +1,24 @@ +/* libmisc/assert.h - More assertions + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBMISC_ASSERT_H_ +#define _LIBMISC_ASSERT_H_ + +#ifdef NDEBUG +# define assert_msg(expr, msg) ((void)0) +#else +# define assert_msg(expr, msg) do { if (!(expr)) __assert_msg_fail(#expr, __FILE__, __LINE__, __func__, msg); } while (0) +__attribute__((__noreturn__)) void +__assert_msg_fail(const char *expr, + const char *file, unsigned int line, const char *func, + const char *msg); +#endif + +#define assert(expr) assert_msg(expr, NULL) /* C89, POSIX-2001 */ +#define assert_notreached(msg) do { assert_msg(false, "not reached"); __builtin_unreachable(); } while (0) +#define static_assert _Static_assert /* C11 */ + +#endif /* _LIBMISC_ASSERT_H_ */ diff --git a/libmisc/include/libmisc/endian.h b/libmisc/include/libmisc/endian.h index 24d7d42..b1bc55c 100644 --- a/libmisc/include/libmisc/endian.h +++ b/libmisc/include/libmisc/endian.h @@ -7,9 +7,10 @@ #ifndef _LIBMISC_ENDIAN_H_ #define _LIBMISC_ENDIAN_H_ -#include <assert.h> #include <stdint.h> /* for uint{n}_t */ +#include <libmisc/assert.h> + /* Big endian *****************************************************************/ typedef struct { diff --git a/libmisc/include/libmisc/vcall.h b/libmisc/include/libmisc/vcall.h index ea9402e..9b54c06 100644 --- a/libmisc/include/libmisc/vcall.h +++ b/libmisc/include/libmisc/vcall.h @@ -7,9 +7,10 @@ #ifndef _LIBMISC_VCALL_H_ #define _LIBMISC_VCALL_H_ -#include <assert.h> /* for assert() and static_assert() */ #include <stddef.h> /* for offsetof() */ +#include <libmisc/assert.h> + #define VCALL(o, m, ...) \ ({ \ assert(o); \ diff --git a/libusb/CMakeLists.txt b/libusb/CMakeLists.txt index f5d6ae2..c23f30e 100644 --- a/libusb/CMakeLists.txt +++ b/libusb/CMakeLists.txt @@ -14,4 +14,5 @@ target_link_libraries(libusb INTERFACE tinyusb_board libcr_ipc + libmisc ) diff --git a/libusb/usb_common.c b/libusb/usb_common.c index ceabad3..17dba0a 100644 --- a/libusb/usb_common.c +++ b/libusb/usb_common.c @@ -7,11 +7,12 @@ #include <stdint.h> /* for uint{n}_t types */ #include <stddef.h> /* for size_t */ #include <string.h> /* memcpy(newlib) */ -#include <assert.h> /* for assert(newlib) */ #include <stdlib.h> /* for malloc(pico_malloc), realloc(pico_malloc), reallocarray(pico_malloc) */ #include "bsp/board_api.h" /* for board_init(), board_init_after_usb(), board_usb_get_serial(TinyUSB) */ #include "tusb.h" /* for various tusb_*_t types */ +#include <libmisc/assert.h> + #include <libusb/tusb_helpers.h> /* for LANGID_*, TU_UTF16() */ #include <libusb/usb_common.h> |