1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:07:35 +00:00

Put assertions behind a DEBUG flag to make it easy to build without them.

This commit is contained in:
Andreas Kling 2019-04-23 21:52:02 +02:00
parent f3754b8429
commit 0c898e3c2c
18 changed files with 49 additions and 37 deletions

View file

@ -3,12 +3,15 @@
#include <Kernel/kstdio.h> #include <Kernel/kstdio.h>
#include <Kernel/i386.h> #include <Kernel/i386.h>
#ifdef DEBUG
[[noreturn]] void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func); [[noreturn]] void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func);
#define ASSERT(expr) (static_cast<bool>(expr) ? (void)0 : __assertion_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__)) #define ASSERT(expr) (static_cast<bool>(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 CRASH() do { asm volatile("ud2"); } while(0)
#define RELEASE_ASSERT(x) do { if (!(x)) CRASH(); } 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_DISABLED() ASSERT(!(cpu_flags() & 0x200))
#define ASSERT_INTERRUPTS_ENABLED() ASSERT(cpu_flags() & 0x200) #define ASSERT_INTERRUPTS_ENABLED() ASSERT(cpu_flags() & 0x200)

View file

@ -484,6 +484,7 @@ void handle_irq()
PIC::eoi(irq); PIC::eoi(irq);
} }
#ifdef DEBUG
void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func) void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func)
{ {
asm volatile("cli"); asm volatile("cli");
@ -493,6 +494,7 @@ void __assertion_failed(const char* msg, const char* file, unsigned line, const
asm volatile("hlt"); asm volatile("hlt");
for (;;); for (;;);
} }
#endif
void sse_init() void sse_init()
{ {

View file

@ -5,6 +5,7 @@
extern "C" { extern "C" {
#ifdef DEBUG
void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func) 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); 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(); abort();
for (;;); for (;;);
} }
#endif
} }

View file

@ -4,13 +4,18 @@
__BEGIN_DECLS __BEGIN_DECLS
#ifdef DEBUG
__attribute__((noreturn)) void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func); __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(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 CRASH() do { asm volatile("ud2"); } while(0)
#define ASSERT assert #define ASSERT assert
#define RELEASE_ASSERT assert #define RELEASE_ASSERT assert
#define ASSERT_NOT_REACHED() assert(false)
__END_DECLS __END_DECLS

View file

@ -48,7 +48,7 @@ int _start(int argc, char** argv, char** env)
[[noreturn]] void __cxa_pure_virtual() [[noreturn]] void __cxa_pure_virtual()
{ {
assert(false); ASSERT_NOT_REACHED();
} }
void __cxa_atexit() void __cxa_atexit()

View file

@ -5,7 +5,7 @@ extern "C" {
struct mntent* getmntent(FILE*) struct mntent* getmntent(FILE*)
{ {
assert(false); ASSERT_NOT_REACHED();
return nullptr; return nullptr;
} }

View file

@ -401,7 +401,7 @@ FILE* freopen(const char* pathname, const char* mode, FILE* stream)
(void)pathname; (void)pathname;
(void)mode; (void)mode;
(void)stream; (void)stream;
assert(false); ASSERT_NOT_REACHED();
} }
FILE* fdopen(int fd, const char* mode) FILE* fdopen(int fd, const char* mode)
@ -429,19 +429,19 @@ int rename(const char* oldpath, const char* newpath)
char* tmpnam(char*) char* tmpnam(char*)
{ {
assert(false); ASSERT_NOT_REACHED();
} }
FILE* popen(const char* command, const char* type) FILE* popen(const char* command, const char* type)
{ {
(void)command; (void)command;
(void)type; (void)type;
assert(false); ASSERT_NOT_REACHED();
} }
int pclose(FILE*) int pclose(FILE*)
{ {
assert(false); ASSERT_NOT_REACHED();
} }
int remove(const char* pathname) int remove(const char* pathname)

View file

@ -63,7 +63,7 @@ void* malloc(size_t size)
if (s_malloc_sum_free < real_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); 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; size_t chunks_needed = real_size / CHUNK_SIZE;
@ -183,7 +183,7 @@ void exit(int status)
extern void _fini(); extern void _fini();
_fini(); _fini();
_exit(status); _exit(status);
assert(false); ASSERT_NOT_REACHED();
} }
int atexit(void (*handler)()) int atexit(void (*handler)())
@ -271,7 +271,7 @@ double strtod(const char* str, char** endptr)
(void)str; (void)str;
(void)endptr; (void)endptr;
dbgprintf("LibC: strtod: '%s'\n", str); dbgprintf("LibC: strtod: '%s'\n", str);
assert(false); ASSERT_NOT_REACHED();
} }
float strtof(const char* str, char** endptr) float strtof(const char* str, char** endptr)
@ -279,13 +279,13 @@ float strtof(const char* str, char** endptr)
(void)str; (void)str;
(void)endptr; (void)endptr;
dbgprintf("LibC: strtof: '%s'\n", str); dbgprintf("LibC: strtof: '%s'\n", str);
assert(false); ASSERT_NOT_REACHED();
} }
double atof(const char* str) double atof(const char* str)
{ {
dbgprintf("LibC: atof: '%s'\n", str); dbgprintf("LibC: atof: '%s'\n", str);
assert(false); ASSERT_NOT_REACHED();
} }
int atoi(const char* str) 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) size_t mbstowcs(wchar_t*, const char*, size_t)
{ {
assert(false); ASSERT_NOT_REACHED();
} }
long strtol(const char* str, char** endptr, int base) long strtol(const char* str, char** endptr, int base)

View file

@ -366,7 +366,7 @@ char *strtok(char* str, const char* delim)
{ {
(void)str; (void)str;
(void)delim; (void)delim;
assert(false); ASSERT_NOT_REACHED();
} }
} }

View file

@ -6,7 +6,7 @@ extern "C" {
pid_t wait(int* wstatus) pid_t wait(int* wstatus)
{ {
(void)wstatus; (void)wstatus;
assert(false); ASSERT_NOT_REACHED();
} }
} }

View file

@ -107,7 +107,7 @@ int tgetnum(const char* id)
auto it = caps->find(id); auto it = caps->find(id);
if (it != caps->end()) if (it != caps->end())
return atoi((*it).value); return atoi((*it).value);
assert(false); ASSERT_NOT_REACHED();
} }
char* tgoto(const char* cap, int col, int row) 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)cap;
(void)col; (void)col;
(void)row; (void)row;
assert(false); ASSERT_NOT_REACHED();
} }
int tputs(const char* str, int affcnt, int (*putc)(int)) int tputs(const char* str, int affcnt, int (*putc)(int))

View file

@ -29,14 +29,14 @@ int tcflow(int fd, int action)
{ {
(void) fd; (void) fd;
(void) action; (void) action;
assert(false); ASSERT_NOT_REACHED();
} }
int tcflush(int fd, int queue_selector) int tcflush(int fd, int queue_selector)
{ {
(void)fd; (void)fd;
(void)queue_selector; (void)queue_selector;
assert(false); ASSERT_NOT_REACHED();
} }
speed_t cfgetispeed(const struct termios* tp) speed_t cfgetispeed(const struct termios* tp)

View file

@ -92,12 +92,12 @@ struct tm* gmtime(const time_t* t)
char *asctime(const struct tm*) char *asctime(const struct tm*)
{ {
assert(false); ASSERT_NOT_REACHED();
} }
size_t strftime(char*, size_t, const char*, const struct tm*) size_t strftime(char*, size_t, const char*, const struct tm*)
{ {
assert(false); ASSERT_NOT_REACHED();
} }
long timezone; long timezone;
@ -107,7 +107,7 @@ int daylight;
void tzset() void tzset()
{ {
assert(false); ASSERT_NOT_REACHED();
} }
} }

View file

@ -7,7 +7,7 @@ long ulimit(int cmd, long newlimit)
{ {
(void) cmd; (void) cmd;
(void) newlimit; (void) newlimit;
assert(false); ASSERT_NOT_REACHED();
} }
} }

View file

@ -362,27 +362,27 @@ int access(const char* pathname, int mode)
int mknod(const char* pathname, mode_t, dev_t) int mknod(const char* pathname, mode_t, dev_t)
{ {
(void) pathname; (void) pathname;
assert(false); ASSERT_NOT_REACHED();
} }
long fpathconf(int fd, int name) long fpathconf(int fd, int name)
{ {
(void) fd; (void) fd;
(void) name; (void) name;
assert(false); ASSERT_NOT_REACHED();
} }
long pathconf(const char* path, int name) long pathconf(const char* path, int name)
{ {
(void) path; (void) path;
(void) name; (void) name;
assert(false); ASSERT_NOT_REACHED();
} }
void _exit(int status) void _exit(int status)
{ {
syscall(SC_exit, status); syscall(SC_exit, status);
assert(false); ASSERT_NOT_REACHED();
} }
void sync() void sync()

View file

@ -5,25 +5,25 @@ extern "C" {
double cos(double) double cos(double)
{ {
assert(false); ASSERT_NOT_REACHED();
} }
double sin(double) double sin(double)
{ {
assert(false); ASSERT_NOT_REACHED();
} }
double pow(double x, double y) double pow(double x, double y)
{ {
(void)x; (void)x;
(void)y; (void)y;
assert(false); ASSERT_NOT_REACHED();
} }
double ldexp(double, int exp) double ldexp(double, int exp)
{ {
(void)exp; (void)exp;
assert(false); ASSERT_NOT_REACHED();
} }
} }

View file

@ -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 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 CLANG_FLAGS = -Wconsumed -m32 -ffreestanding -march=i686
#SUGGEST_FLAGS = -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override #-Wsuggest-attribute=noreturn #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) CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(SUGGEST_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
#CXX = clang $(CLANG_FLAGS) #CXX = clang $(CLANG_FLAGS)
CXX = i686-pc-serenity-g++ CXX = i686-pc-serenity-g++
LD = i686-pc-serenity-g++ LD = i686-pc-serenity-g++
AS = i686-pc-serenity-as AS = i686-pc-serenity-as
DEFINES = -DSANITIZE_PTRS -DDEBUG

View file

@ -180,7 +180,7 @@ static int runcmd(char* cmd)
exit(1); exit(1);
} }
// We should never get here! // We should never get here!
assert(false); ASSERT_NOT_REACHED();
} }
int wstatus = 0; int wstatus = 0;