From ae95a528f20e0ae5fc5db4a837e520afd24446ec Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 2 Oct 2022 18:10:53 +0200 Subject: [PATCH] Make function stubs in libc loudly abort instead of silently failing --- apps/src/init.c | 7 ++++--- libs/libc/include/luna.h | 23 +++++++++++++++++++++++ libs/libc/include/unistd.h | 1 + libs/libc/src/luna.cpp | 26 ++++++++++++++++++++++++++ libs/libc/src/stdio.cpp | 21 +++++++++++---------- libs/libc/src/stdlib.cpp | 11 ++++++----- libs/libc/src/string.cpp | 3 ++- libs/libc/src/unistd.cpp | 14 ++++++++++---- 8 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 libs/libc/include/luna.h create mode 100644 libs/libc/src/luna.cpp diff --git a/apps/src/init.c b/apps/src/init.c index 553750cb..435f3c84 100644 --- a/apps/src/init.c +++ b/apps/src/init.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,7 +13,7 @@ static void print(const char* message) int main() { - if (syscall(SYS_gettid) == 0) // why are we the idle task? + if (gettid() == 0) // why are we the idle task? { println("SHENANIGANS! init is tid 0 (which is reserved for the idle task)"); abort(); @@ -21,7 +22,7 @@ int main() println("Welcome to Luna from a C init!"); println(""); - syscall(SYS_sleep, 1000); + sleep(1); print("Your kernel version is "); @@ -31,7 +32,7 @@ int main() print(version); println("\n"); - syscall(SYS_sleep, 2000); + sleep(2); println("Press any key to restart."); diff --git a/libs/libc/include/luna.h b/libs/libc/include/luna.h new file mode 100644 index 00000000..5383caa1 --- /dev/null +++ b/libs/libc/include/luna.h @@ -0,0 +1,23 @@ +#ifndef _LUNA_H +#define _LUNA_H + +#include + +#define noreturn __attribute__((noreturn)) + +#ifdef __cplusplus +extern "C" +{ +#endif + + pid_t gettid(); + unsigned int msleep(unsigned int); + noreturn void __luna_abort(const char* message); + +#ifdef __cplusplus +} +#endif + +#define NOT_IMPLEMENTED(message) __luna_abort("not implemented: " message "\n") + +#endif \ No newline at end of file diff --git a/libs/libc/include/unistd.h b/libs/libc/include/unistd.h index cb3fd234..0e2fe102 100644 --- a/libs/libc/include/unistd.h +++ b/libs/libc/include/unistd.h @@ -10,6 +10,7 @@ extern "C" int execvp(const char*, char* const[]); pid_t fork(void); long syscall(long, ...); + unsigned int sleep(unsigned int); #ifdef __cplusplus } diff --git a/libs/libc/src/luna.cpp b/libs/libc/src/luna.cpp new file mode 100644 index 00000000..b1001242 --- /dev/null +++ b/libs/libc/src/luna.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +#define noreturn __attribute__((noreturn)) + +extern "C" +{ + pid_t gettid() + { + return syscall(SYS_gettid); + } + + unsigned int msleep(unsigned int ms) + { + return syscall(SYS_sleep, ms); + } + + noreturn void __luna_abort(const char* message) + { + syscall(SYS_write, message, strlen(message)); + syscall(SYS_exit); + __builtin_unreachable(); + } +} \ No newline at end of file diff --git a/libs/libc/src/stdio.cpp b/libs/libc/src/stdio.cpp index 0b548b8e..ca3b4d71 100644 --- a/libs/libc/src/stdio.cpp +++ b/libs/libc/src/stdio.cpp @@ -1,45 +1,46 @@ +#include #include extern "C" { int fclose(FILE*) { - return -1; + NOT_IMPLEMENTED("fclose"); } int fflush(FILE*) { - return -1; + NOT_IMPLEMENTED("fflush"); } FILE* fopen(const char*, const char*) { - return 0; + NOT_IMPLEMENTED("fopen"); } int fprintf(FILE*, const char*, ...) { - return -1; + NOT_IMPLEMENTED("fprintf"); } size_t fread(void*, size_t, size_t, FILE*) { - return 0; + NOT_IMPLEMENTED("fread"); } int fseek(FILE*, long, int) { - return -1; + NOT_IMPLEMENTED("fseek"); } long ftell(FILE*) { - return -1; + NOT_IMPLEMENTED("ftell"); } size_t fwrite(const void*, size_t, size_t, FILE*) { - return 0; + NOT_IMPLEMENTED("fwrite"); } void setbuf(FILE*, char*) { - return; + NOT_IMPLEMENTED("setbuf"); } int vfprintf(FILE*, const char*, va_list) { - return -1; + NOT_IMPLEMENTED("vfprintf"); } } \ No newline at end of file diff --git a/libs/libc/src/stdlib.cpp b/libs/libc/src/stdlib.cpp index 3aa49bb5..845ad10f 100644 --- a/libs/libc/src/stdlib.cpp +++ b/libs/libc/src/stdlib.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -22,22 +23,22 @@ extern "C" int atexit(void (*)(void)) { - return -1; + NOT_IMPLEMENTED("atexit"); } int atoi(const char*) { - return 0; + NOT_IMPLEMENTED("atoi"); } void free(void*) { - return; + NOT_IMPLEMENTED("free"); } char* getenv(const char*) { - return 0; + NOT_IMPLEMENTED("getenv"); } void* malloc(size_t) { - return 0; + NOT_IMPLEMENTED("malloc"); } } \ No newline at end of file diff --git a/libs/libc/src/string.cpp b/libs/libc/src/string.cpp index d776c0e5..bd696df4 100644 --- a/libs/libc/src/string.cpp +++ b/libs/libc/src/string.cpp @@ -1,3 +1,4 @@ +#include #include extern "C" @@ -42,6 +43,6 @@ extern "C" char* strchr(const char*, int) { - return 0; + NOT_IMPLEMENTED("strchr"); } } \ No newline at end of file diff --git a/libs/libc/src/unistd.cpp b/libs/libc/src/unistd.cpp index 6660c91d..cfdb9d58 100644 --- a/libs/libc/src/unistd.cpp +++ b/libs/libc/src/unistd.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,19 +7,19 @@ extern "C" { int execv(const char*, char* const[]) { - return -1; + NOT_IMPLEMENTED("execv"); } int execve(const char*, char* const[], char* const[]) { - return -1; + NOT_IMPLEMENTED("execve"); } int execvp(const char*, char* const[]) { - return -1; + NOT_IMPLEMENTED("execvp"); } pid_t fork(void) { - return -1; + NOT_IMPLEMENTED("fork"); } long syscall(long number, ...) @@ -55,4 +56,9 @@ extern "C" va_end(ap); return result; } + + unsigned int sleep(unsigned int seconds) + { + return msleep(seconds * 1000); + } } \ No newline at end of file