mirror of
https://github.com/tailix/libkernaux.git
synced 2025-02-17 15:45:32 -05:00
Add panic guards
This commit is contained in:
parent
f001868720
commit
5c56f93e2e
7 changed files with 142 additions and 47 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -89,6 +89,7 @@
|
|||
/examples/assert_guards
|
||||
/examples/assert_simple
|
||||
/examples/cmdline
|
||||
/examples/panic_guards
|
||||
/examples/panic_simple
|
||||
/examples/pfa
|
||||
/examples/printf
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
2022-01-20 Alex Kotov <kotovalexarian@gmail.com>
|
||||
|
||||
* include/kernaux/assert.h: Added unconditional assertion (panic)
|
||||
* include/kernaux/assert.h: Added guards for unconditional assertion (panic)
|
||||
|
|
|
@ -20,6 +20,7 @@ if ENABLE_TESTS
|
|||
TESTS = \
|
||||
examples/assert_guards \
|
||||
examples/assert_simple \
|
||||
examples/panic_guards \
|
||||
examples/panic_simple
|
||||
endif
|
||||
|
||||
|
@ -141,6 +142,10 @@ examples_cmdline_SOURCES = \
|
|||
$(libkernaux_a_SOURCES) \
|
||||
examples/cmdline.c
|
||||
|
||||
examples_panic_guards_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
examples/panic_guards.c
|
||||
|
||||
examples_panic_simple_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
examples/panic_simple.c
|
||||
|
|
|
@ -40,6 +40,7 @@ zero). Work-in-progress APIs can change at any time.
|
|||
* [Assert: simple](/examples/assert_simple.c)
|
||||
* [Assert: guards](/examples/assert_guards.c)
|
||||
* [Panic: simple](/examples/panic_simple.c)
|
||||
* [Panic: guards](/examples/panic_guards.c)
|
||||
* Stack trace *(planned)*
|
||||
* Device drivers (for debugging only)
|
||||
* [Serial console](/include/kernaux/console.h) (*work in progress*)
|
||||
|
|
98
configure.ac
98
configure.ac
|
@ -18,9 +18,10 @@ AC_ARG_ENABLE([float], AS_HELP_STRING([--disable-float], [disable f
|
|||
|
||||
dnl Features (disabled by default)
|
||||
AC_ARG_ENABLE([assert], AS_HELP_STRING([--enable-assert], [enable runtime assertions]))
|
||||
AC_ARG_ENABLE([guard], AS_HELP_STRING([--enable-guard], [enable argument guards]))
|
||||
AC_ARG_ENABLE([guard], AS_HELP_STRING([--enable-guard], [enable assert/panic guards]))
|
||||
AC_ARG_ENABLE([guard-cond], AS_HELP_STRING([--enable-guard-cond], [enable condition guard]))
|
||||
AC_ARG_ENABLE([guard-null], AS_HELP_STRING([--enable-guard-null], [enable NULL-guard]))
|
||||
AC_ARG_ENABLE([guard-panic], AS_HELP_STRING([--enable-guard-panic], [enable panic guard]))
|
||||
AC_ARG_ENABLE([tests], AS_HELP_STRING([--enable-tests], [enable tests and examples]))
|
||||
|
||||
dnl Packages (enabled by default)
|
||||
|
@ -47,8 +48,9 @@ AC_ARG_WITH( [libc-strnlen], AS_HELP_STRING([--with-libc-strnlen], [with strn
|
|||
|
||||
AC_DEFUN([do_enable_guard],
|
||||
[
|
||||
if test -z "$enable_guard_cond"; then enable_guard_cond=yes; fi
|
||||
if test -z "$enable_guard_null"; then enable_guard_null=yes; fi
|
||||
if test -z "$enable_guard_cond"; then enable_guard_cond=yes; fi
|
||||
if test -z "$enable_guard_null"; then enable_guard_null=yes; fi
|
||||
if test -z "$enable_guard_panic"; then enable_guard_panic=yes; fi
|
||||
])
|
||||
AS_IF([test "$enable_guard" = yes], do_enable_guard)
|
||||
|
||||
|
@ -86,70 +88,72 @@ AS_IF([test "$with_ntoa" = no -a "$with_units" != no], AC_MSG_ERROR([package `u
|
|||
|
||||
|
||||
dnl Architecture
|
||||
AM_CONDITIONAL([ASM_I386], [test "$host_cpu" = i386])
|
||||
AM_CONDITIONAL([ASM_RISCV64], [test "$host_cpu" = riscv64])
|
||||
AM_CONDITIONAL([ASM_X86_64], [test "$host_cpu" = x86_64])
|
||||
AM_CONDITIONAL([ASM_I386], [test "$host_cpu" = i386])
|
||||
AM_CONDITIONAL([ASM_RISCV64], [test "$host_cpu" = riscv64])
|
||||
AM_CONDITIONAL([ASM_X86_64], [test "$host_cpu" = x86_64])
|
||||
|
||||
dnl Features (enabled by default)
|
||||
AM_CONDITIONAL([ENABLE_FLOAT], [test "$enable_float" != no])
|
||||
AM_CONDITIONAL([ENABLE_FLOAT], [test "$enable_float" != no])
|
||||
|
||||
dnl Features (disabled by default)
|
||||
AM_CONDITIONAL([ENABLE_ASSERT], [test "$enable_assert" = yes])
|
||||
AM_CONDITIONAL([ENABLE_GUARD_COND], [test "$enable_guard_cond" = yes])
|
||||
AM_CONDITIONAL([ENABLE_GUARD_NULL], [test "$enable_guard_null" = yes])
|
||||
AM_CONDITIONAL([ENABLE_TESTS], [test "$enable_tests" = yes])
|
||||
AM_CONDITIONAL([ENABLE_ASSERT], [test "$enable_assert" = yes])
|
||||
AM_CONDITIONAL([ENABLE_GUARD_COND], [test "$enable_guard_cond" = yes])
|
||||
AM_CONDITIONAL([ENABLE_GUARD_NULL], [test "$enable_guard_null" = yes])
|
||||
AM_CONDITIONAL([ENABLE_GUARD_PANIC], [test "$enable_guard_panic" = yes])
|
||||
AM_CONDITIONAL([ENABLE_TESTS], [test "$enable_tests" = yes])
|
||||
|
||||
dnl Packages (enabled by default)
|
||||
AM_CONDITIONAL([WITH_CMDLINE], [test "$with_cmdline" != no])
|
||||
AM_CONDITIONAL([WITH_CONSOLE], [test "$with_console" != no])
|
||||
AM_CONDITIONAL([WITH_ELF], [test "$with_elf" != no])
|
||||
AM_CONDITIONAL([WITH_FRAMEBUFFER], [test "$with_framebuffer" != no])
|
||||
AM_CONDITIONAL([WITH_MBR], [test "$with_mbr" != no])
|
||||
AM_CONDITIONAL([WITH_MULTIBOOT2], [test "$with_multiboot2" != no])
|
||||
AM_CONDITIONAL([WITH_NTOA], [test "$with_ntoa" != no])
|
||||
AM_CONDITIONAL([WITH_PFA], [test "$with_pfa" != no])
|
||||
AM_CONDITIONAL([WITH_PRINTF], [test "$with_printf" != no])
|
||||
AM_CONDITIONAL([WITH_UNITS], [test "$with_units" != no])
|
||||
AM_CONDITIONAL([WITH_CMDLINE], [test "$with_cmdline" != no])
|
||||
AM_CONDITIONAL([WITH_CONSOLE], [test "$with_console" != no])
|
||||
AM_CONDITIONAL([WITH_ELF], [test "$with_elf" != no])
|
||||
AM_CONDITIONAL([WITH_FRAMEBUFFER], [test "$with_framebuffer" != no])
|
||||
AM_CONDITIONAL([WITH_MBR], [test "$with_mbr" != no])
|
||||
AM_CONDITIONAL([WITH_MULTIBOOT2], [test "$with_multiboot2" != no])
|
||||
AM_CONDITIONAL([WITH_NTOA], [test "$with_ntoa" != no])
|
||||
AM_CONDITIONAL([WITH_PFA], [test "$with_pfa" != no])
|
||||
AM_CONDITIONAL([WITH_PRINTF], [test "$with_printf" != no])
|
||||
AM_CONDITIONAL([WITH_UNITS], [test "$with_units" != no])
|
||||
|
||||
dnl Packages (disabled by default)
|
||||
AM_CONDITIONAL([WITH_LIBC_MEMSET], [test "$with_libc_memset" = yes])
|
||||
AM_CONDITIONAL([WITH_LIBC_STRCPY], [test "$with_libc_strcpy" = yes])
|
||||
AM_CONDITIONAL([WITH_LIBC_STRLEN], [test "$with_libc_strlen" = yes])
|
||||
AM_CONDITIONAL([WITH_LIBC_STRNLEN], [test "$with_libc_strnlen" = yes])
|
||||
AM_CONDITIONAL([WITH_LIBC_MEMSET], [test "$with_libc_memset" = yes])
|
||||
AM_CONDITIONAL([WITH_LIBC_STRCPY], [test "$with_libc_strcpy" = yes])
|
||||
AM_CONDITIONAL([WITH_LIBC_STRLEN], [test "$with_libc_strlen" = yes])
|
||||
AM_CONDITIONAL([WITH_LIBC_STRNLEN], [test "$with_libc_strnlen" = yes])
|
||||
|
||||
|
||||
|
||||
dnl Architecture
|
||||
AS_IF([test "$host_cpu" = i386], [AC_DEFINE([ASM_I386], [1], [architecture is i386])])
|
||||
AS_IF([test "$host_cpu" = riscv64], [AC_DEFINE([ASM_RISCV64], [1], [architecture is RISC-V 64-bit])])
|
||||
AS_IF([test "$host_cpu" = x86_64], [AC_DEFINE([ASM_X86_64], [1], [architecture is x86_64])])
|
||||
AS_IF([test "$host_cpu" = i386], [AC_DEFINE([ASM_I386], [1], [architecture is i386])])
|
||||
AS_IF([test "$host_cpu" = riscv64], [AC_DEFINE([ASM_RISCV64], [1], [architecture is RISC-V 64-bit])])
|
||||
AS_IF([test "$host_cpu" = x86_64], [AC_DEFINE([ASM_X86_64], [1], [architecture is x86_64])])
|
||||
|
||||
dnl Features (enabled by default)
|
||||
AS_IF([test "$enable_float" != no], [AC_DEFINE([ENABLE_FLOAT], [1], [enabled floating-point arithmeric])])
|
||||
AS_IF([test "$enable_float" != no], [AC_DEFINE([ENABLE_FLOAT], [1], [enabled floating-point arithmeric])])
|
||||
|
||||
dnl Features (disabled by default)
|
||||
AS_IF([test "$enable_assert" = yes], [AC_DEFINE([KERNAUX_ENABLE_ASSERT], [1], [enabled runtime assertions])])
|
||||
AS_IF([test "$enable_guard_cond" = yes], [AC_DEFINE([KERNAUX_ENABLE_GUARD_COND], [1], [enabled condition guard])])
|
||||
AS_IF([test "$enable_guard_null" = yes], [AC_DEFINE([KERNAUX_ENABLE_GUARD_NULL], [1], [enabled NULL-guard])])
|
||||
AS_IF([test "$enable_tests" = yes], [AC_DEFINE([ENABLE_TESTS], [1], [enabled tests and examples])])
|
||||
AS_IF([test "$enable_assert" = yes], [AC_DEFINE([KERNAUX_ENABLE_ASSERT], [1], [enabled runtime assertions])])
|
||||
AS_IF([test "$enable_guard_cond" = yes], [AC_DEFINE([KERNAUX_ENABLE_GUARD_COND], [1], [enabled condition guard])])
|
||||
AS_IF([test "$enable_guard_null" = yes], [AC_DEFINE([KERNAUX_ENABLE_GUARD_NULL], [1], [enabled NULL-guard])])
|
||||
AS_IF([test "$enable_guard_panic" = yes], [AC_DEFINE([KERNAUX_ENABLE_GUARD_PANIC], [1], [enabled panic guard])])
|
||||
AS_IF([test "$enable_tests" = yes], [AC_DEFINE([ENABLE_TESTS], [1], [enabled tests and examples])])
|
||||
|
||||
dnl Packages (enabled by default)
|
||||
AS_IF([test "$with_cmdline" != no], [AC_DEFINE([WITH_CMDLINE], [1], [with command line parser])])
|
||||
AS_IF([test "$with_console" != no], [AC_DEFINE([WITH_CONSOLE], [1], [with serial console])])
|
||||
AS_IF([test "$with_elf" != no], [AC_DEFINE([WITH_ELF], [1], [with ELF utils])])
|
||||
AS_IF([test "$with_framebuffer" != no], [AC_DEFINE([WITH_FRAMEBUFFER], [1], [with framebuffer])])
|
||||
AS_IF([test "$with_mbr" != no], [AC_DEFINE([WITH_MBR], [1], [with Master Boot Record])])
|
||||
AS_IF([test "$with_multiboot2" != no], [AC_DEFINE([WITH_MULTIBOOT2], [1], [with Multiboot 2 information parser])])
|
||||
AS_IF([test "$with_ntoa" != no], [AC_DEFINE([WITH_NTOA], [1], [with ntoa])])
|
||||
AS_IF([test "$with_pfa" != no], [AC_DEFINE([WITH_PFA], [1], [with Page Frame Allocator])])
|
||||
AS_IF([test "$with_printf" != no], [AC_DEFINE([WITH_PRINTF], [1], [with printf])])
|
||||
AS_IF([test "$with_units", != no], [AC_DEFINE([WITH_UNITS], [1], [with measurement units utils])])
|
||||
AS_IF([test "$with_cmdline" != no], [AC_DEFINE([WITH_CMDLINE], [1], [with command line parser])])
|
||||
AS_IF([test "$with_console" != no], [AC_DEFINE([WITH_CONSOLE], [1], [with serial console])])
|
||||
AS_IF([test "$with_elf" != no], [AC_DEFINE([WITH_ELF], [1], [with ELF utils])])
|
||||
AS_IF([test "$with_framebuffer" != no], [AC_DEFINE([WITH_FRAMEBUFFER], [1], [with framebuffer])])
|
||||
AS_IF([test "$with_mbr" != no], [AC_DEFINE([WITH_MBR], [1], [with Master Boot Record])])
|
||||
AS_IF([test "$with_multiboot2" != no], [AC_DEFINE([WITH_MULTIBOOT2], [1], [with Multiboot 2 information parser])])
|
||||
AS_IF([test "$with_ntoa" != no], [AC_DEFINE([WITH_NTOA], [1], [with ntoa])])
|
||||
AS_IF([test "$with_pfa" != no], [AC_DEFINE([WITH_PFA], [1], [with Page Frame Allocator])])
|
||||
AS_IF([test "$with_printf" != no], [AC_DEFINE([WITH_PRINTF], [1], [with printf])])
|
||||
AS_IF([test "$with_units", != no], [AC_DEFINE([WITH_UNITS], [1], [with measurement units utils])])
|
||||
|
||||
dnl Packages (disabled by default)
|
||||
AS_IF([test "$with_libc_memset" = yes], [AC_DEFINE([WITH_LIBC_MEMSET], [1], [with memset replacement])])
|
||||
AS_IF([test "$with_libc_strcpy" = yes], [AC_DEFINE([WITH_LIBC_STRCPY], [1], [with strcpy replacement])])
|
||||
AS_IF([test "$with_libc_strlen" = yes], [AC_DEFINE([WITH_LIBC_STRLEN], [1], [with strlen replacement])])
|
||||
AS_IF([test "$with_libc_strnlen" = yes], [AC_DEFINE([WITH_LIBC_STRNLEN], [1], [with strnlen replacement])])
|
||||
AS_IF([test "$with_libc_memset" = yes], [AC_DEFINE([WITH_LIBC_MEMSET], [1], [with memset replacement])])
|
||||
AS_IF([test "$with_libc_strcpy" = yes], [AC_DEFINE([WITH_LIBC_STRCPY], [1], [with strcpy replacement])])
|
||||
AS_IF([test "$with_libc_strlen" = yes], [AC_DEFINE([WITH_LIBC_STRLEN], [1], [with strlen replacement])])
|
||||
AS_IF([test "$with_libc_strnlen" = yes], [AC_DEFINE([WITH_LIBC_STRNLEN], [1], [with strnlen replacement])])
|
||||
|
||||
|
||||
|
||||
|
|
75
examples/panic_guards.c
Normal file
75
examples/panic_guards.c
Normal file
|
@ -0,0 +1,75 @@
|
|||
#define KERNAUX_ENABLE_GUARD
|
||||
#include <kernaux/assert.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
static unsigned int count = 0;
|
||||
static const char *last_file = NULL;
|
||||
static int last_line = 0;
|
||||
static const char *last_str = NULL;
|
||||
|
||||
static unsigned int noreturn_count = 0;
|
||||
|
||||
static void assert_cb(
|
||||
const char *const file,
|
||||
const int line,
|
||||
const char *const str
|
||||
) {
|
||||
++count;
|
||||
last_file = file;
|
||||
last_line = line;
|
||||
last_str = str;
|
||||
}
|
||||
|
||||
static void test_panic_return(const bool do_panic)
|
||||
{
|
||||
if (do_panic) KERNAUX_PANIC_RETURN("foo");
|
||||
++noreturn_count;
|
||||
}
|
||||
|
||||
static bool test_panic_retval(const bool do_panic)
|
||||
{
|
||||
if (do_panic) KERNAUX_PANIC_RETVAL("bar", false);
|
||||
++noreturn_count;
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
kernaux_assert_cb = assert_cb;
|
||||
|
||||
test_panic_return(false);
|
||||
|
||||
assert(count == 0);
|
||||
assert(last_file == NULL);
|
||||
assert(last_line == 0);
|
||||
assert(last_str == NULL);
|
||||
assert(noreturn_count == 1);
|
||||
|
||||
assert(test_panic_retval(false));
|
||||
|
||||
assert(count == 0);
|
||||
assert(last_file == NULL);
|
||||
assert(last_line == 0);
|
||||
assert(last_str == NULL);
|
||||
assert(noreturn_count == 2);
|
||||
|
||||
test_panic_return(true);
|
||||
|
||||
assert(count == 1);
|
||||
assert(strcmp(last_file, __FILE__) == 0);
|
||||
assert(last_line == 29);
|
||||
assert(strcmp(last_str, "\"foo\"") == 0);
|
||||
assert(noreturn_count == 2);
|
||||
|
||||
assert(!test_panic_retval(true));
|
||||
|
||||
assert(count == 2);
|
||||
assert(strcmp(last_file, __FILE__) == 0);
|
||||
assert(last_line == 35);
|
||||
assert(strcmp(last_str, "\"bar\"") == 0);
|
||||
assert(noreturn_count == 2);
|
||||
}
|
|
@ -30,6 +30,14 @@ extern "C" {
|
|||
#define KERNAUX_NOTNULL_RETVAL(cond, val) { KERNAUX_ASSERT(cond); }
|
||||
#endif
|
||||
|
||||
#if defined(KERNAUX_ENABLE_GUARD) || defined(KERNAUX_ENABLE_GUARD_PANIC)
|
||||
#define KERNAUX_PANIC_RETURN(msg) { KERNAUX_PANIC(msg); return; }
|
||||
#define KERNAUX_PANIC_RETVAL(msg, val) { KERNAUX_PANIC(msg); return (val); }
|
||||
#else
|
||||
#define KERNAUX_PANIC_RETURN(msg) { KERNAUX_PANIC(msg); }
|
||||
#define KERNAUX_PANIC_RETVAL(msg, val) { KERNAUX_PANIC(msg); }
|
||||
#endif
|
||||
|
||||
void kernaux_assert_do(const char *file, int line, const char *str);
|
||||
|
||||
extern void (*kernaux_assert_cb)(const char *file, int line, const char *str);
|
||||
|
|
Loading…
Add table
Reference in a new issue