This commit is contained in:
bumbread 2022-06-07 17:02:23 +11:00
parent aa247b3d4b
commit 3252f1f10a
14 changed files with 156 additions and 22 deletions

View File

@ -16,7 +16,7 @@ shift /1
set CIABATTA_OPTIONS=-Iinc -Wall -g -gcodeview -nodefaultlibs -D_CRT_SECURE_NO_WARNINGS
set PLATFORM=win
if "%1"=="fast" (
if "%1"=="test" (
goto :skip_crt_compilation
)
:: For each C file in code/ we check whether it's OS-dependent.

View File

@ -20,5 +20,5 @@ extern void _assert_error(
}
printf("\tFile: %s\n", file);
printf("\tLine: %s\n", line);
_compiler_brk();
abort();
}

View File

@ -1,4 +1,6 @@
#include <_os.h>
#include <locale.h>
#include <stdbool.h>
#include <stdlib.h>
@ -71,6 +73,7 @@ void mainCRTStartup() {
srand(0);
setlocale(LC_ALL, "C");
_os_init_eh();
int exit_code = main(arg_count, args);
ExitProcess(exit_code);

View File

@ -1,2 +1,10 @@
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
void _os_exit(int code)
{
ExitProcess(code);
}

57
code/os/win/except.c Normal file
View File

@ -0,0 +1,57 @@
#include <_os.h>
#define WIN32_LEAN_AND_MEAN
#include<Windows.h>
#include <signal.h>
#include <stddef.h>
#include <_macros.h>
typedef struct SignalMapping {
DWORD code;
int signal;
} SignalMapping;
static SignalMapping map[] = {
{EXCEPTION_ACCESS_VIOLATION, SIGSEGV},
{EXCEPTION_IN_PAGE_ERROR, SIGSEGV},
{EXCEPTION_ARRAY_BOUNDS_EXCEEDED, SIGSEGV},
{EXCEPTION_DATATYPE_MISALIGNMENT, SIGALIGN},
{EXCEPTION_BREAKPOINT, SIGBREAK},
{EXCEPTION_FLT_DENORMAL_OPERAND, SIGFPE},
{EXCEPTION_FLT_DIVIDE_BY_ZERO, SIGFPE},
{EXCEPTION_FLT_INEXACT_RESULT, SIGFPE},
{EXCEPTION_FLT_INVALID_OPERATION, SIGFPE},
{EXCEPTION_FLT_OVERFLOW, SIGFPE},
{EXCEPTION_FLT_STACK_CHECK, SIGFPE},
{EXCEPTION_FLT_UNDERFLOW, SIGFPE},
{EXCEPTION_ILLEGAL_INSTRUCTION, SIGILL},
{EXCEPTION_INT_DIVIDE_BY_ZERO, SIGFPE},
{EXCEPTION_INT_OVERFLOW, SIGFPE},
{EXCEPTION_PRIV_INSTRUCTION, SIGILL},
{EXCEPTION_SINGLE_STEP, SIGSTEP},
};
static LONG _win32_handler(EXCEPTION_POINTERS *ExceptionInfo)
{
EXCEPTION_RECORD *exception = ExceptionInfo->ExceptionRecord;
DWORD code = exception->ExceptionCode;
int signal = -1;
for(int mapping = 0; mapping != _countof(map); ++mapping) {
if(code == map[mapping].code) {
signal = map[mapping].signal;
}
}
if(signal != -1) {
raise(signal);
}
return EXCEPTION_CONTINUE_SEARCH;
}
void _os_init_eh() {
void *res = AddVectoredExceptionHandler(1, &_win32_handler);
if(res == NULL) {
ExitProcess(-69420);
}
}

View File

@ -7,7 +7,7 @@ struct FILE {
int unused;
};
void _file_write(void* ctx, size_t n, const char str[]) {
void _os_file_write(void* ctx, size_t n, const char str[]) {
DWORD written = 0;
WriteFile((HANDLE) ctx, str, n, &written, NULL);
}

46
code/signal.c Normal file
View File

@ -0,0 +1,46 @@
#include <_os.h>
#include <signal.h>
void _signal_default_handler(int sig)
{
}
void _signal_ignore_handler(int sig)
{
// ignore :kekw:
}
static void (*(handlers[]))(int) = {
[SIGINT] = _signal_ignore_handler,
[SIGILL] = _signal_ignore_handler,
[SIGFPE] = _signal_ignore_handler,
[SIGSEGV] = _signal_ignore_handler,
[SIGTERM] = _signal_ignore_handler,
[SIGABRT] = _signal_ignore_handler,
[SIGBREAK] = _signal_ignore_handler,
[SIGALIGN] = _signal_ignore_handler,
[SIGSTEP] = _signal_ignore_handler,
};
void (*signal(int sig, void (*func)(int)))(int)
{
if(_SIG_MIN <= sig && sig <= _SIG_MAX) {
handlers[sig] = func;
return func;
}
return SIG_ERR;
}
int raise(int sig)
{
if(_SIG_MIN <= sig && sig <= _SIG_MAX) {
handlers[sig](sig);
if(sig == SIGFPE || sig == SIGILL || sig == SIGSEGV) {
_os_exit(-69420);
}
return 1;
}
return 0;
}

View File

@ -40,12 +40,12 @@ static void string_write(void *ctx, size_t n, const char *restrict str) {
}
int fprintf(FILE *file, const char *restrict fmt, ...) {
CALL_PRINTF(fmt_print_char, file, _file_write, fmt);
CALL_PRINTF(fmt_print_char, file, _os_file_write, fmt);
return result;
}
int printf(const char *restrict fmt, ...) {
CALL_PRINTF(fmt_print_char, stdout, _file_write, fmt);
CALL_PRINTF(fmt_print_char, stdout, _os_file_write, fmt);
return result;
}
@ -62,11 +62,11 @@ int sprintf(char *restrict s, const char *restrict fmt, ...) {
}
int vfprintf(FILE *file, const char *restrict fmt, va_list args) {
return fmt_print_char(file, _file_write, fmt, args);
return fmt_print_char(file, _os_file_write, fmt, args);
}
int vprintf(const char *restrict fmt, va_list args) {
return fmt_print_char(stdout, _file_write, fmt, args);
return fmt_print_char(stdout, _os_file_write, fmt, args);
}
int vsnprintf(char *restrict s, size_t n, const char *restrict fmt, va_list args) {

View File

@ -6,3 +6,4 @@
#define _con(a,b) a ## b
#define _countof(arr) (sizeof (arr) / sizeof ((arr)[0]))

View File

@ -15,5 +15,7 @@
#endif
// OS-dependent IO Functions
void _file_write(void* ctx, size_t n, const char str[]);
void _os_file_write(void* ctx, size_t n, const char str[]);
void _os_exit(int code);
void _os_init_eh();
typedef struct FILE FILE;

View File

@ -1,5 +1,6 @@
#pragma once
#include "_compiler.h"
#include "_macros.h"
extern void _assert_error(
char *cond,

View File

@ -3,19 +3,24 @@
typedef int sig_atomic_t;
// TODO: implement this
#define SIG_DFL 0
#define SIG_ERR 1
#define SIG_IGN 2
// TODO: idk about SIG_ERR, for now this
#define SIG_ERR ((void(*)(int))0)
#define SIG_DFL _signal_default_handler
#define SIG_IGN _signal_ignore_handler
// not sure why but windows picked these, we can change it later
#define SIGINT 2
#define SIGILL 4
#define SIGFPE 8
#define SIGSEGV 11
#define SIGTERM 15
#define SIGBREAK 21
#define SIGABRT 22
// Note(bumbread): from the impl standpoint the numbers are arbitrary
#define _SIG_MIN 0
#define SIGINT 1
#define SIGILL 2
#define SIGFPE 3
#define SIGSEGV 4
#define SIGTERM 5
#define SIGABRT 6
// These guys are impl defined
#define SIGBREAK 7
#define SIGALIGN 8
#define SIGSTEP 9
#define _SIG_MAX 9
void (*signal(int sig, void (*func)(int)))(int);
int raise(int sig);

View File

@ -4,10 +4,21 @@
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>
#include <inttypes.h>
#include <signal.h>
void my_va_handler(int a) {
printf("NULLPTR deref or something idk not an expert in signals\n");
}
int main(int argc, char** argv) {
uint64_t mynumber = 4;
printf("Hello, guys %"PRIu64"\n", mynumber);
signal(SIGSEGV, my_va_handler);
signal(SIGFPE, my_va_handler);
int a = INT_MAX;
a /= 0;
return 0;
}