From 9f20e63df7ebbe7b6a858c58824e5ab18038b3a8 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 12 May 2019 18:01:29 +0200 Subject: [PATCH] Correcting a bug on real hw caused by malloc that non zeroises the mem it allocates --- kaleid/kernel/init/init.c | 8 ++++---- kaleid/kernel/mm/malloc.c | 8 ++++---- kaleid/kernel/po/shtdwn.c | 3 +++ kaleid/kernel/sh/shell.c | 1 + kaleid/libbuf/bopen.c | 3 ++- kaleid/libc/sprintf.c | 23 +++++++++++++++++------ 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 11f80a4..71f7fa3 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -47,14 +47,14 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Memory & scheduler MmInitMemoryMap(); - // Several inits - MmInitHeap(); - PsInitSched(); - // Interrupts launching IdtSetup(); KeEnableIRQs(); + // Several inits + MmInitHeap(); + //PsInitSched(); + // Start drivers IoEnableRtc(); IoEnableKeyb(); diff --git a/kaleid/kernel/mm/malloc.c b/kaleid/kernel/mm/malloc.c index e873ca4..cddcace 100644 --- a/kaleid/kernel/mm/malloc.c +++ b/kaleid/kernel/mm/malloc.c @@ -46,8 +46,8 @@ error_t KalAllocMemoryEx(void **ptr, size_t req, int flags, size_t align) brk = (size_t)_heap_start + MmGetHeapSize(); req = _ALIGN_UP(req + brk, align) - brk; - //DebugLog("MALLOC: start=%p, size=%lx, brk=%p, req=%lx\n", - // _heap_start, MmGetHeapSize(), brk, req); + /* DebugLog("MALLOC: start=%p, size=%lx, brk=%p, req=%lx\n", */ + /* _heap_start, MmGetHeapSize(), brk, req); */ rc = MmGrowHeap(req); @@ -59,12 +59,12 @@ error_t KalAllocMemoryEx(void **ptr, size_t req, int flags, size_t align) KeStartPanic("KalAllocMemory: Out of memory"); } + *ptr = (void *)brk; + if (flags & M_ZEROED) { memzero(*ptr, req); } - *ptr = (void *)brk; - assert(*ptr); return rc; } diff --git a/kaleid/kernel/po/shtdwn.c b/kaleid/kernel/po/shtdwn.c index d54db81..a5bc5e5 100644 --- a/kaleid/kernel/po/shtdwn.c +++ b/kaleid/kernel/po/shtdwn.c @@ -34,6 +34,7 @@ noreturn void PoShutdownQemu(void) IoWriteWordOnPort(0x604, 0x2000); + KeCrashSystem(); __builtin_unreachable(); } @@ -45,6 +46,7 @@ noreturn void PoShutdownVirtualbox(void) IoWriteWordOnPort(0x4004, 0x3400); + KeCrashSystem(); __builtin_unreachable(); } @@ -56,6 +58,7 @@ noreturn void PoShutdownBochs(void) IoWriteWordOnPort(0xB004, 0x2000); + KeCrashSystem(); __builtin_unreachable(); } diff --git a/kaleid/kernel/sh/shell.c b/kaleid/kernel/sh/shell.c index 3b0a8a9..9da298c 100644 --- a/kaleid/kernel/sh/shell.c +++ b/kaleid/kernel/sh/shell.c @@ -139,5 +139,6 @@ void KeStartShell(void) KePauseCPU(); } + KernLog("[EOI]\n"); } diff --git a/kaleid/libbuf/bopen.c b/kaleid/libbuf/bopen.c index 4a5f395..7c533df 100644 --- a/kaleid/libbuf/bopen.c +++ b/kaleid/libbuf/bopen.c @@ -52,7 +52,7 @@ error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size, if (!*pbuf) { buf = malloc(sizeof *buf); if (!buf) return ENOMEM; - buf->flags |= BF_BALLOC; + buf->flags = BF_BALLOC; } else { buf = *pbuf; @@ -64,6 +64,7 @@ error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size, buf->size = size; buf->state = mode; + //buf->buf = NULL; if (source == NULL) { KalAllocMemoryEx((void **)&buf->buf, buf->size, M_ZEROED, 0); diff --git a/kaleid/libc/sprintf.c b/kaleid/libc/sprintf.c index 6fa46e2..14e111b 100644 --- a/kaleid/libc/sprintf.c +++ b/kaleid/libc/sprintf.c @@ -78,7 +78,7 @@ size_t snprintf(char *str, size_t n, const char *fmt, ...) size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap) { size_t ret; - error_t rc; + error_t rc = EOK; Buffer_t *buf = NULL; assert(str && fmt); @@ -89,15 +89,25 @@ size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap) goto fail; } + rc = BOpenPureBuf(&buf, BS_WRONLY, n-1); + // n-1 to leave place for the '\0' - if (BOpenPureBuf(&buf, BS_WRONLY, n-1) != EOK) { + if (rc != EOK) { goto fail; } + rc = vbprintf(buf, fmt, ap); - + // We don't mind EOFs, just just return how much was successfully written - if (rc != EOK && !(rc == EENDF && !(buf->flags & BF_EOF))) { - goto fail; + if (rc != EOK) { + if (rc == EENDF) { + if (!(buf->flags & BF_EOF)) { + goto fail; + } + } + else { + goto fail; + } } ret = (size_t)buf->wp - (size_t)buf->buf; @@ -114,7 +124,8 @@ size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap) return ret; fail: - assert(!"vsnprintf() failure"); + KeStartPanic("vsnprintf() failure\nRC: %d\nbuf->flags & BF_EOF: %d\n", + rc, buf->flags & BF_EOF); *str = 0; return 0; }