Make function stubs in libc loudly abort instead of silently failing

This commit is contained in:
apio 2022-10-02 18:10:53 +02:00
parent 8e6741ebd6
commit ae95a528f2
8 changed files with 83 additions and 23 deletions

View File

@ -1,3 +1,4 @@
#include <luna.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
@ -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.");

23
libs/libc/include/luna.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef _LUNA_H
#define _LUNA_H
#include <sys/types.h>
#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

View File

@ -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
}

26
libs/libc/src/luna.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <luna.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
#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();
}
}

View File

@ -1,45 +1,46 @@
#include <luna.h>
#include <stdio.h>
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");
}
}

View File

@ -1,3 +1,4 @@
#include <luna.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
@ -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");
}
}

View File

@ -1,3 +1,4 @@
#include <luna.h>
#include <string.h>
extern "C"
@ -42,6 +43,6 @@ extern "C"
char* strchr(const char*, int)
{
return 0;
NOT_IMPLEMENTED("strchr");
}
}

View File

@ -1,3 +1,4 @@
#include <luna.h>
#include <luna/syscall.h>
#include <stdarg.h>
#include <unistd.h>
@ -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);
}
}