Correcting a bug on real hw caused by malloc that non zeroises the mem it allocates

This commit is contained in:
Adrien Bourmault 2019-05-12 18:01:29 +02:00
parent 39091a44c1
commit 9f20e63df7
6 changed files with 31 additions and 15 deletions

View File

@ -47,14 +47,14 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
// Memory & scheduler // Memory & scheduler
MmInitMemoryMap(); MmInitMemoryMap();
// Several inits
MmInitHeap();
PsInitSched();
// Interrupts launching // Interrupts launching
IdtSetup(); IdtSetup();
KeEnableIRQs(); KeEnableIRQs();
// Several inits
MmInitHeap();
//PsInitSched();
// Start drivers // Start drivers
IoEnableRtc(); IoEnableRtc();
IoEnableKeyb(); IoEnableKeyb();

View File

@ -46,8 +46,8 @@ error_t KalAllocMemoryEx(void **ptr, size_t req, int flags, size_t align)
brk = (size_t)_heap_start + MmGetHeapSize(); brk = (size_t)_heap_start + MmGetHeapSize();
req = _ALIGN_UP(req + brk, align) - brk; req = _ALIGN_UP(req + brk, align) - brk;
//DebugLog("MALLOC: start=%p, size=%lx, brk=%p, req=%lx\n", /* DebugLog("MALLOC: start=%p, size=%lx, brk=%p, req=%lx\n", */
// _heap_start, MmGetHeapSize(), brk, req); /* _heap_start, MmGetHeapSize(), brk, req); */
rc = MmGrowHeap(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"); KeStartPanic("KalAllocMemory: Out of memory");
} }
*ptr = (void *)brk;
if (flags & M_ZEROED) { if (flags & M_ZEROED) {
memzero(*ptr, req); memzero(*ptr, req);
} }
*ptr = (void *)brk;
assert(*ptr); assert(*ptr);
return rc; return rc;
} }

View File

@ -34,6 +34,7 @@ noreturn void PoShutdownQemu(void)
IoWriteWordOnPort(0x604, 0x2000); IoWriteWordOnPort(0x604, 0x2000);
KeCrashSystem();
__builtin_unreachable(); __builtin_unreachable();
} }
@ -45,6 +46,7 @@ noreturn void PoShutdownVirtualbox(void)
IoWriteWordOnPort(0x4004, 0x3400); IoWriteWordOnPort(0x4004, 0x3400);
KeCrashSystem();
__builtin_unreachable(); __builtin_unreachable();
} }
@ -56,6 +58,7 @@ noreturn void PoShutdownBochs(void)
IoWriteWordOnPort(0xB004, 0x2000); IoWriteWordOnPort(0xB004, 0x2000);
KeCrashSystem();
__builtin_unreachable(); __builtin_unreachable();
} }

View File

@ -139,5 +139,6 @@ void KeStartShell(void)
KePauseCPU(); KePauseCPU();
} }
KernLog("[EOI]\n");
} }

View File

@ -52,7 +52,7 @@ error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size,
if (!*pbuf) { if (!*pbuf) {
buf = malloc(sizeof *buf); buf = malloc(sizeof *buf);
if (!buf) return ENOMEM; if (!buf) return ENOMEM;
buf->flags |= BF_BALLOC; buf->flags = BF_BALLOC;
} }
else { else {
buf = *pbuf; buf = *pbuf;
@ -64,6 +64,7 @@ error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size,
buf->size = size; buf->size = size;
buf->state = mode; buf->state = mode;
//buf->buf = NULL;
if (source == NULL) { if (source == NULL) {
KalAllocMemoryEx((void **)&buf->buf, buf->size, M_ZEROED, 0); KalAllocMemoryEx((void **)&buf->buf, buf->size, M_ZEROED, 0);

View File

@ -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 vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
{ {
size_t ret; size_t ret;
error_t rc; error_t rc = EOK;
Buffer_t *buf = NULL; Buffer_t *buf = NULL;
assert(str && fmt); assert(str && fmt);
@ -89,16 +89,26 @@ size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
goto fail; goto fail;
} }
rc = BOpenPureBuf(&buf, BS_WRONLY, n-1);
// n-1 to leave place for the '\0' // n-1 to leave place for the '\0'
if (BOpenPureBuf(&buf, BS_WRONLY, n-1) != EOK) { if (rc != EOK) {
goto fail; goto fail;
} }
rc = vbprintf(buf, fmt, ap); rc = vbprintf(buf, fmt, ap);
// We don't mind EOFs, just just return how much was successfully written // We don't mind EOFs, just just return how much was successfully written
if (rc != EOK && !(rc == EENDF && !(buf->flags & BF_EOF))) { if (rc != EOK) {
if (rc == EENDF) {
if (!(buf->flags & BF_EOF)) {
goto fail; goto fail;
} }
}
else {
goto fail;
}
}
ret = (size_t)buf->wp - (size_t)buf->buf; 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; return ret;
fail: fail:
assert(!"vsnprintf() failure"); KeStartPanic("vsnprintf() failure\nRC: %d\nbuf->flags & BF_EOF: %d\n",
rc, buf->flags & BF_EOF);
*str = 0; *str = 0;
return 0; return 0;
} }