From 27393da2a58007948e3f726d4d5304e7d1c77b8f Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 23 Jan 2022 01:36:05 +0500 Subject: [PATCH] Don't allow to disable assert and guard (part of #26) --- .github/workflows/test.yml | 4 +--- ChangeLog | 2 +- README.md | 14 +------------- configure.ac | 6 ------ examples/assert_guards.c | 10 ++++------ examples/assert_simple.c | 1 - examples/panic_guards.c | 5 ++--- include/kernaux/assert.h | 31 ++++++------------------------- tests/test_pfa_assert.c | 31 +------------------------------ 9 files changed, 16 insertions(+), 88 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c08afb7..5e4883a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/ChangeLog b/ChangeLog index b03b59e..8dd71e7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,7 @@ 2022-01-22 Alex Kotov - * 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 diff --git a/README.md b/README.md index 95fe626..5f99593 100644 --- a/README.md +++ b/README.md @@ -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)" \ diff --git a/configure.ac b/configure.ac index 0d1dbbf..d5ba7cb 100644 --- a/configure.ac +++ b/configure.ac @@ -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) diff --git a/examples/assert_guards.c b/examples/assert_guards.c index d978e76..7c41d15 100644 --- a/examples/assert_guards.c +++ b/examples/assert_guards.c @@ -1,5 +1,3 @@ -#define KERNAUX_ENABLE_ASSERT -#define KERNAUX_ENABLE_GUARD #include #include @@ -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); diff --git a/examples/assert_simple.c b/examples/assert_simple.c index a84f199..eeddfae 100644 --- a/examples/assert_simple.c +++ b/examples/assert_simple.c @@ -1,4 +1,3 @@ -#define KERNAUX_ENABLE_ASSERT #include #include diff --git a/examples/panic_guards.c b/examples/panic_guards.c index d6e08b3..7971489 100644 --- a/examples/panic_guards.c +++ b/examples/panic_guards.c @@ -1,4 +1,3 @@ -#define KERNAUX_ENABLE_GUARD #include #include @@ -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); } diff --git a/include/kernaux/assert.h b/include/kernaux/assert.h index 05c19db..851ffff 100644 --- a/include/kernaux/assert.h +++ b/include/kernaux/assert.h @@ -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); diff --git a/tests/test_pfa_assert.c b/tests/test_pfa_assert.c index 4077b55..f215f8c 100644 --- a/tests/test_pfa_assert.c +++ b/tests/test_pfa_assert.c @@ -8,13 +8,10 @@ #include #include -#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 }