Don't allow to disable assert and guard (part of #26)

This commit is contained in:
Alex Kotov 2022-01-23 01:36:05 +05:00
parent d51acc9fda
commit 27393da2a5
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
9 changed files with 16 additions and 88 deletions

View File

@ -15,8 +15,6 @@ jobs:
#cc: ['gcc', 'clang', 'tcc']
cc: ['gcc', 'clang']
opt: ['', '-O0', '-O3']
assert: ['--enable-assert', '--disable-assert']
guard: ['--enable-guard', '--disable-guard']
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
@ -31,7 +29,7 @@ jobs:
- name: autogen
run: ./autogen.sh
- name: configure
run: ./configure --enable-tests ${{matrix.assert}} ${{matrix.guard}} CC='${{matrix.cc}}' CFLAGS='${{matrix.opt}} -fPIC'
run: ./configure --enable-tests CC='${{matrix.cc}}' CFLAGS='${{matrix.opt}} -fPIC'
- name: make
run: make
- name: check

View File

@ -1,7 +1,7 @@
2022-01-22 Alex Kotov <kotovalexarian@gmail.com>
* include/kernaux/assert.h: Do not allow to disable guards separately
* README.md: Configuration options now follow semver
* include/kernaux/assert.h: Do not allow to disable asserts and guards
2022-01-21 Alex Kotov <kotovalexarian@gmail.com>

View File

@ -84,16 +84,6 @@ stable options.
### Non-default options
#### Features
* `--enable-assert` - use value of extern variable `kernaux_assert_cb` as a
callback function for internal assertions. You still can use assertions in
your own application (kernel) even if this option was not enabled.
* `--enable-guard` - safely return from functions even when assertions are
disabled. This option doesn't have effect if your assetion function was set
and ends execution of application (kernel). However it prevents crashes and
undefined behavior in other cases.
#### Packages
* `--with-libc` - provides the replacement for some standard C functions. Useful
@ -140,7 +130,7 @@ environment.
```
./autogen.sh
./configure --enable-tests --enable-assert --enable-guard CFLAGS='-fPIC'
./configure --enable-tests CFLAGS='-fPIC'
make
```
@ -157,8 +147,6 @@ without it in `$PATH`:
```
./configure \
--host='i386-elf' \
--enable-assert \
--enable-guard \
--with-libc \
AR="$(which i386-elf-ar)" \
CC="$(which i386-elf-gcc)" \

View File

@ -18,8 +18,6 @@ AC_ARG_ENABLE([bloat], AS_HELP_STRING([--disable-bloat], [disable u
AC_ARG_ENABLE([float], AS_HELP_STRING([--disable-float], [disable floating-point arithmeric]))
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 assert/panic guards]))
AC_ARG_ENABLE([tests], AS_HELP_STRING([--enable-tests], [enable tests and examples]))
dnl Packages (enabled by default)
@ -87,8 +85,6 @@ AM_CONDITIONAL([ENABLE_BLOAT], [test "$enable_bloat" != 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], [test "$enable_guard" = yes])
AM_CONDITIONAL([ENABLE_TESTS], [test "$enable_tests" = yes])
dnl Packages (enabled by default)
@ -121,8 +117,6 @@ AS_IF([test "$enable_bloat" != no], [AC_DEFINE([ENABLE_BLOAT], [1]
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" = yes], [AC_DEFINE([KERNAUX_ENABLE_GUARD], [1], [enabled assert/panic guards])])
AS_IF([test "$enable_tests" = yes], [AC_DEFINE([ENABLE_TESTS], [1], [enabled tests and examples])])
dnl Packages (enabled by default)

View File

@ -1,5 +1,3 @@
#define KERNAUX_ENABLE_ASSERT
#define KERNAUX_ENABLE_GUARD
#include <kernaux/assert.h>
#include <assert.h>
@ -93,7 +91,7 @@ int main()
assert(count == 1);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 30);
assert(last_line == 28);
assert(strcmp(last_str, "value > 100") == 0);
assert(noreturn_count == 4);
@ -101,7 +99,7 @@ int main()
assert(count == 2);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 36);
assert(last_line == 34);
assert(strcmp(last_str, "value > 100") == 0);
assert(noreturn_count == 4);
@ -109,7 +107,7 @@ int main()
assert(count == 3);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 43);
assert(last_line == 41);
assert(strcmp(last_str, "value") == 0);
assert(noreturn_count == 4);
@ -117,7 +115,7 @@ int main()
assert(count == 4);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 49);
assert(last_line == 47);
assert(strcmp(last_str, "value") == 0);
assert(noreturn_count == 4);

View File

@ -1,4 +1,3 @@
#define KERNAUX_ENABLE_ASSERT
#include <kernaux/assert.h>
#include <assert.h>

View File

@ -1,4 +1,3 @@
#define KERNAUX_ENABLE_GUARD
#include <kernaux/assert.h>
#include <assert.h>
@ -61,7 +60,7 @@ int main()
assert(count == 1);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 29);
assert(last_line == 28);
assert(strcmp(last_str, "\"foo\"") == 0);
assert(noreturn_count == 2);
@ -69,7 +68,7 @@ int main()
assert(count == 2);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 35);
assert(last_line == 34);
assert(strcmp(last_str, "\"bar\"") == 0);
assert(noreturn_count == 2);
}

View File

@ -7,36 +7,17 @@ extern "C" {
#define KERNAUX_PANIC(msg) (kernaux_assert_do(__FILE__, __LINE__, #msg))
#ifdef KERNAUX_ENABLE_ASSERT
#define KERNAUX_ASSERT(cond) \
((cond) ? (void)0 : kernaux_assert_do(__FILE__, __LINE__, #cond))
#else
#define KERNAUX_ASSERT(cond) ((void)sizeof((cond)))
#endif
#ifdef KERNAUX_ENABLE_GUARD
#define KERNAUX_ASSERT_RETURN(cond) { KERNAUX_ASSERT(cond); if (!(cond)) return; }
#define KERNAUX_ASSERT_RETVAL(cond, val) { KERNAUX_ASSERT(cond); if (!(cond)) return (val); }
#else
#define KERNAUX_ASSERT_RETURN(cond) { KERNAUX_ASSERT(cond); }
#define KERNAUX_ASSERT_RETVAL(cond, val) { KERNAUX_ASSERT(cond); }
#endif
#ifdef KERNAUX_ENABLE_GUARD
#define KERNAUX_NOTNULL_RETURN(cond) { KERNAUX_ASSERT(cond); if (!(cond)) return; }
#define KERNAUX_NOTNULL_RETVAL(cond, val) { KERNAUX_ASSERT(cond); if (!(cond)) return (val); }
#else
#define KERNAUX_NOTNULL_RETURN(cond) { KERNAUX_ASSERT(cond); }
#define KERNAUX_NOTNULL_RETVAL(cond, val) { KERNAUX_ASSERT(cond); }
#endif
#ifdef KERNAUX_ENABLE_GUARD
#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
#define KERNAUX_ASSERT_RETURN(cond) { KERNAUX_ASSERT(cond); if (!(cond)) return; }
#define KERNAUX_ASSERT_RETVAL(cond, val) { KERNAUX_ASSERT(cond); if (!(cond)) return (val); }
#define KERNAUX_NOTNULL_RETURN(cond) { KERNAUX_ASSERT(cond); if (!(cond)) return; }
#define KERNAUX_NOTNULL_RETVAL(cond, val) { KERNAUX_ASSERT(cond); if (!(cond)) return (val); }
void kernaux_assert_do(const char *file, int line, const char *str);

View File

@ -8,13 +8,10 @@
#include <assert.h>
#include <stddef.h>
#if defined(KERNAUX_ENABLE_ASSERT) || defined(KERNAUX_ENABLE_GUARD)
static unsigned int count = 0;
#endif
static void test();
#ifdef KERNAUX_ENABLE_ASSERT
static void assert_cb(
__attribute__((unused))
const char *const file,
@ -25,78 +22,52 @@ static void assert_cb(
) {
++count;
}
#endif
int main()
{
kernaux_assert_cb = NULL;
test();
#if defined(KERNAUX_ENABLE_ASSERT) || defined(KERNAUX_ENABLE_GUARD)
#ifdef KERNAUX_ENABLE_ASSERT
kernaux_assert_cb = assert_cb;
#endif
test();
#endif
return 0;
}
void test()
{
#ifdef KERNAUX_ENABLE_GUARD
unsigned int acc = 0;
#endif
struct KernAux_PFA pfa;
KernAux_PFA_initialize(&pfa);
#ifdef KERNAUX_ENABLE_GUARD
KernAux_PFA_initialize(NULL);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
assert(!KernAux_PFA_is_available(NULL, KERNAUX_PFA_PAGE_SIZE));
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
assert(!KernAux_PFA_is_available(&pfa, 123));
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
KernAux_PFA_mark_available(NULL, 0, KERNAUX_PFA_PAGE_SIZE);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
KernAux_PFA_mark_available(&pfa, KERNAUX_PFA_PAGE_SIZE, 0);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
KernAux_PFA_mark_unavailable(NULL, 0, KERNAUX_PFA_PAGE_SIZE);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
KernAux_PFA_mark_unavailable(&pfa, KERNAUX_PFA_PAGE_SIZE, 0);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
assert(KernAux_PFA_alloc_pages(NULL, KERNAUX_PFA_PAGE_SIZE) == 0);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
KernAux_PFA_free_pages(NULL, KERNAUX_PFA_PAGE_SIZE, KERNAUX_PFA_PAGE_SIZE);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
#ifdef KERNAUX_ENABLE_GUARD
KernAux_PFA_free_pages(&pfa, 123, KERNAUX_PFA_PAGE_SIZE);
if (kernaux_assert_cb) assert(count == ++acc);
#endif
}