From 0c898e3c2cad9a5b2c7a4caf41bd9b544d31ba19 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 23 Apr 2019 21:52:02 +0200 Subject: [PATCH] Put assertions behind a DEBUG flag to make it easy to build without them. --- Kernel/Assertions.h | 9 ++++++--- Kernel/i386.cpp | 2 ++ LibC/assert.cpp | 2 ++ LibC/assert.h | 9 +++++++-- LibC/crt0.cpp | 2 +- LibC/mntent.cpp | 2 +- LibC/stdio.cpp | 8 ++++---- LibC/stdlib.cpp | 12 ++++++------ LibC/string.cpp | 2 +- LibC/sys/wait.cpp | 2 +- LibC/termcap.cpp | 4 ++-- LibC/termios.cpp | 4 ++-- LibC/time.cpp | 6 +++--- LibC/ulimit.cpp | 2 +- LibC/unistd.cpp | 8 ++++---- LibM/math.cpp | 8 ++++---- Makefile.common | 2 +- Userland/sh.cpp | 2 +- 18 files changed, 49 insertions(+), 37 deletions(-) diff --git a/Kernel/Assertions.h b/Kernel/Assertions.h index df8331de6f..8eada59219 100644 --- a/Kernel/Assertions.h +++ b/Kernel/Assertions.h @@ -3,12 +3,15 @@ #include #include +#ifdef DEBUG [[noreturn]] void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func); - #define ASSERT(expr) (static_cast(expr) ? (void)0 : __assertion_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__)) +#define ASSERT_NOT_REACHED() ASSERT(false) +#else +#define ASSERT(expr) +#define ASSERT_NOT_REACHED() CRASH() +#endif #define CRASH() do { asm volatile("ud2"); } while(0) #define RELEASE_ASSERT(x) do { if (!(x)) CRASH(); } while(0) -//#define ASSERT RELEASE_ASSERT -#define ASSERT_NOT_REACHED() ASSERT(false) #define ASSERT_INTERRUPTS_DISABLED() ASSERT(!(cpu_flags() & 0x200)) #define ASSERT_INTERRUPTS_ENABLED() ASSERT(cpu_flags() & 0x200) diff --git a/Kernel/i386.cpp b/Kernel/i386.cpp index 865c7a6755..a5d74a49f6 100644 --- a/Kernel/i386.cpp +++ b/Kernel/i386.cpp @@ -484,6 +484,7 @@ void handle_irq() PIC::eoi(irq); } +#ifdef DEBUG void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func) { asm volatile("cli"); @@ -493,6 +494,7 @@ void __assertion_failed(const char* msg, const char* file, unsigned line, const asm volatile("hlt"); for (;;); } +#endif void sse_init() { diff --git a/LibC/assert.cpp b/LibC/assert.cpp index a123c911da..bf13d8e1e9 100644 --- a/LibC/assert.cpp +++ b/LibC/assert.cpp @@ -5,6 +5,7 @@ extern "C" { +#ifdef DEBUG void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func) { dbgprintf("USERSPACE(%d) ASSERTION FAILED: %s\n%s:%u in %s\n", getpid(), msg, file, line, func); @@ -12,5 +13,6 @@ void __assertion_failed(const char* msg, const char* file, unsigned line, const abort(); for (;;); } +#endif } diff --git a/LibC/assert.h b/LibC/assert.h index 9420363bba..21e380fe4e 100644 --- a/LibC/assert.h +++ b/LibC/assert.h @@ -4,13 +4,18 @@ __BEGIN_DECLS +#ifdef DEBUG __attribute__((noreturn)) void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func); - #define assert(expr) ((expr) ? (void)0 : __assertion_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__)) +#define ASSERT_NOT_REACHED() assert(false) +#else +#define assert(expr) +#define ASSERT_NOT_REACHED() CRASH() +#endif + #define CRASH() do { asm volatile("ud2"); } while(0) #define ASSERT assert #define RELEASE_ASSERT assert -#define ASSERT_NOT_REACHED() assert(false) __END_DECLS diff --git a/LibC/crt0.cpp b/LibC/crt0.cpp index 10176515d3..7d43c391a9 100644 --- a/LibC/crt0.cpp +++ b/LibC/crt0.cpp @@ -48,7 +48,7 @@ int _start(int argc, char** argv, char** env) [[noreturn]] void __cxa_pure_virtual() { - assert(false); + ASSERT_NOT_REACHED(); } void __cxa_atexit() diff --git a/LibC/mntent.cpp b/LibC/mntent.cpp index 792a0f7156..08116ec02b 100644 --- a/LibC/mntent.cpp +++ b/LibC/mntent.cpp @@ -5,7 +5,7 @@ extern "C" { struct mntent* getmntent(FILE*) { - assert(false); + ASSERT_NOT_REACHED(); return nullptr; } diff --git a/LibC/stdio.cpp b/LibC/stdio.cpp index 12a33092ca..2d0f7a5d24 100644 --- a/LibC/stdio.cpp +++ b/LibC/stdio.cpp @@ -401,7 +401,7 @@ FILE* freopen(const char* pathname, const char* mode, FILE* stream) (void)pathname; (void)mode; (void)stream; - assert(false); + ASSERT_NOT_REACHED(); } FILE* fdopen(int fd, const char* mode) @@ -429,19 +429,19 @@ int rename(const char* oldpath, const char* newpath) char* tmpnam(char*) { - assert(false); + ASSERT_NOT_REACHED(); } FILE* popen(const char* command, const char* type) { (void)command; (void)type; - assert(false); + ASSERT_NOT_REACHED(); } int pclose(FILE*) { - assert(false); + ASSERT_NOT_REACHED(); } int remove(const char* pathname) diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp index ecc57f6cad..36157e3682 100644 --- a/LibC/stdlib.cpp +++ b/LibC/stdlib.cpp @@ -63,7 +63,7 @@ void* malloc(size_t size) if (s_malloc_sum_free < real_size) { fprintf(stderr, "malloc(): Out of memory\ns_malloc_sum_free=%u, real_size=%u\n", s_malloc_sum_free, real_size); - assert(false); + ASSERT_NOT_REACHED(); } size_t chunks_needed = real_size / CHUNK_SIZE; @@ -183,7 +183,7 @@ void exit(int status) extern void _fini(); _fini(); _exit(status); - assert(false); + ASSERT_NOT_REACHED(); } int atexit(void (*handler)()) @@ -271,7 +271,7 @@ double strtod(const char* str, char** endptr) (void)str; (void)endptr; dbgprintf("LibC: strtod: '%s'\n", str); - assert(false); + ASSERT_NOT_REACHED(); } float strtof(const char* str, char** endptr) @@ -279,13 +279,13 @@ float strtof(const char* str, char** endptr) (void)str; (void)endptr; dbgprintf("LibC: strtof: '%s'\n", str); - assert(false); + ASSERT_NOT_REACHED(); } double atof(const char* str) { dbgprintf("LibC: atof: '%s'\n", str); - assert(false); + ASSERT_NOT_REACHED(); } int atoi(const char* str) @@ -417,7 +417,7 @@ ldiv_t ldiv(long numerator, long denominator) size_t mbstowcs(wchar_t*, const char*, size_t) { - assert(false); + ASSERT_NOT_REACHED(); } long strtol(const char* str, char** endptr, int base) diff --git a/LibC/string.cpp b/LibC/string.cpp index 3b84340d62..3b9909b834 100644 --- a/LibC/string.cpp +++ b/LibC/string.cpp @@ -366,7 +366,7 @@ char *strtok(char* str, const char* delim) { (void)str; (void)delim; - assert(false); + ASSERT_NOT_REACHED(); } } diff --git a/LibC/sys/wait.cpp b/LibC/sys/wait.cpp index 06825603c0..031d446399 100644 --- a/LibC/sys/wait.cpp +++ b/LibC/sys/wait.cpp @@ -6,7 +6,7 @@ extern "C" { pid_t wait(int* wstatus) { (void)wstatus; - assert(false); + ASSERT_NOT_REACHED(); } } diff --git a/LibC/termcap.cpp b/LibC/termcap.cpp index 9d4e0632ac..9d34bc18f1 100644 --- a/LibC/termcap.cpp +++ b/LibC/termcap.cpp @@ -107,7 +107,7 @@ int tgetnum(const char* id) auto it = caps->find(id); if (it != caps->end()) return atoi((*it).value); - assert(false); + ASSERT_NOT_REACHED(); } char* tgoto(const char* cap, int col, int row) @@ -115,7 +115,7 @@ char* tgoto(const char* cap, int col, int row) (void)cap; (void)col; (void)row; - assert(false); + ASSERT_NOT_REACHED(); } int tputs(const char* str, int affcnt, int (*putc)(int)) diff --git a/LibC/termios.cpp b/LibC/termios.cpp index 8af27ffa06..0e44aad819 100644 --- a/LibC/termios.cpp +++ b/LibC/termios.cpp @@ -29,14 +29,14 @@ int tcflow(int fd, int action) { (void) fd; (void) action; - assert(false); + ASSERT_NOT_REACHED(); } int tcflush(int fd, int queue_selector) { (void)fd; (void)queue_selector; - assert(false); + ASSERT_NOT_REACHED(); } speed_t cfgetispeed(const struct termios* tp) diff --git a/LibC/time.cpp b/LibC/time.cpp index 2709f10cad..59b699062f 100644 --- a/LibC/time.cpp +++ b/LibC/time.cpp @@ -92,12 +92,12 @@ struct tm* gmtime(const time_t* t) char *asctime(const struct tm*) { - assert(false); + ASSERT_NOT_REACHED(); } size_t strftime(char*, size_t, const char*, const struct tm*) { - assert(false); + ASSERT_NOT_REACHED(); } long timezone; @@ -107,7 +107,7 @@ int daylight; void tzset() { - assert(false); + ASSERT_NOT_REACHED(); } } diff --git a/LibC/ulimit.cpp b/LibC/ulimit.cpp index 77f551498a..c16a06fe1a 100644 --- a/LibC/ulimit.cpp +++ b/LibC/ulimit.cpp @@ -7,7 +7,7 @@ long ulimit(int cmd, long newlimit) { (void) cmd; (void) newlimit; - assert(false); + ASSERT_NOT_REACHED(); } } diff --git a/LibC/unistd.cpp b/LibC/unistd.cpp index 28190bdbfd..ab691151e5 100644 --- a/LibC/unistd.cpp +++ b/LibC/unistd.cpp @@ -362,27 +362,27 @@ int access(const char* pathname, int mode) int mknod(const char* pathname, mode_t, dev_t) { (void) pathname; - assert(false); + ASSERT_NOT_REACHED(); } long fpathconf(int fd, int name) { (void) fd; (void) name; - assert(false); + ASSERT_NOT_REACHED(); } long pathconf(const char* path, int name) { (void) path; (void) name; - assert(false); + ASSERT_NOT_REACHED(); } void _exit(int status) { syscall(SC_exit, status); - assert(false); + ASSERT_NOT_REACHED(); } void sync() diff --git a/LibM/math.cpp b/LibM/math.cpp index 41e6190784..68210c7809 100644 --- a/LibM/math.cpp +++ b/LibM/math.cpp @@ -5,25 +5,25 @@ extern "C" { double cos(double) { - assert(false); + ASSERT_NOT_REACHED(); } double sin(double) { - assert(false); + ASSERT_NOT_REACHED(); } double pow(double x, double y) { (void)x; (void)y; - assert(false); + ASSERT_NOT_REACHED(); } double ldexp(double, int exp) { (void)exp; - assert(false); + ASSERT_NOT_REACHED(); } } diff --git a/Makefile.common b/Makefile.common index 8e34eaa45d..e3edefac47 100644 --- a/Makefile.common +++ b/Makefile.common @@ -11,10 +11,10 @@ INCLUDE_FLAGS = -I$(SERENITY_BASE_DIR) -I. -I$(SERENITY_BASE_DIR)/LibC -I$(SEREN LDFLAGS = -L$(SERENITY_BASE_DIR)/LibC -L$(SERENITY_BASE_DIR)/LibCore -L$(SERENITY_BASE_DIR)/LibM -L$(SERENITY_BASE_DIR)/LibGUI -Wl,--gc-sections CLANG_FLAGS = -Wconsumed -m32 -ffreestanding -march=i686 #SUGGEST_FLAGS = -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override #-Wsuggest-attribute=noreturn -DEFINES = -DSANITIZE_PTRS CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(SUGGEST_FLAGS) $(INCLUDE_FLAGS) $(DEFINES) #CXX = clang $(CLANG_FLAGS) CXX = i686-pc-serenity-g++ LD = i686-pc-serenity-g++ AS = i686-pc-serenity-as +DEFINES = -DSANITIZE_PTRS -DDEBUG diff --git a/Userland/sh.cpp b/Userland/sh.cpp index f210f64013..2268b12297 100644 --- a/Userland/sh.cpp +++ b/Userland/sh.cpp @@ -180,7 +180,7 @@ static int runcmd(char* cmd) exit(1); } // We should never get here! - assert(false); + ASSERT_NOT_REACHED(); } int wstatus = 0;