1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00

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
MmInitMemoryMap();
// Several inits
MmInitHeap();
PsInitSched();
// Interrupts launching
IdtSetup();
KeEnableIRQs();
// Several inits
MmInitHeap();
//PsInitSched();
// Start drivers
IoEnableRtc();
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();
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;
}

View File

@ -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();
}

View File

@ -139,5 +139,6 @@ void KeStartShell(void)
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) {
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);

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