duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

commit 02d461c74f2d3c84343f48bc3a4d280397e0fd00
parent 94a805a20b2fabc6a3194a24e92e2a68c85c7cb3
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Sun, 20 Nov 2022 09:42:16 -0500

posix: fix memory corruption in certain (most?) environments

I was using memcpy() instead of memmove() for MOVE(), which will sometimes have
to move overlapping memory areas. It worked on my machine and the build machines
by chance.

Also, straigten out file descriptor splitting/joining. Previous logic was
written with only 64-bit in mind.

Diffstat:
Mposix/vm.c | 24+++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/posix/vm.c b/posix/vm.c @@ -574,7 +574,7 @@ static void MOVE() { // op: 4c dword dst = ppop(); dword src = ppop(); if (memchk(dst+u) && memchk(src+u)) { - memcpy(&vm.mem[dst], &vm.mem[src], u); + memmove(&vm.mem[dst], &vm.mem[src], u); } } @@ -788,8 +788,26 @@ static dword findpath(char *path) { return 0; } +// Return the "high" part of fd. On a 32-bit system, it's always zero. On a +// 64-bit system, it's fd >> 32. +static dword fdhi(int fd) { + if (sizeof(int) > sizeof(dword)) { + return (int64_t)fd >> 32; + } else { + return 0; + } +} + +static int fdjoin(dword lo, dword hi) { + if (sizeof(int) > sizeof(dword)) { + return ((int64_t)hi<<32)|lo; + } else { + return lo; + } +} + static int getfiledesc(dword hdl) { - return (int)(gd(hdl+FILEDESCOFF)|(((int64_t)gd(hdl+FILEDESCOFF+4))<<32)); + return fdjoin(gd(hdl+FILEDESCOFF), gd(hdl+FILEDESCOFF+4)); } static char* getpathfromid(dword fsid) { @@ -861,7 +879,7 @@ static void FOPEN () { // op: 61 dwrite(filesize); dwrite(find("_fseek")); // seek dwrite(fd); - dwrite((int64_t)fd>>32); + dwrite(fdhi(fd)); } static void FREADBUF() { // op: 62