From 2bd2b998afc49fb8a31b941e314b06b8c1c5c2d1 Mon Sep 17 00:00:00 2001 From: Matheus Date: Mon, 7 Aug 2023 02:12:18 -0300 Subject: [PATCH] mlibc: emulate sbrk --- patches/mlibc/jinx-working-patch.patch | 43 ++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/patches/mlibc/jinx-working-patch.patch b/patches/mlibc/jinx-working-patch.patch index bf21dcdf..62949388 100644 --- a/patches/mlibc/jinx-working-patch.patch +++ b/patches/mlibc/jinx-working-patch.patch @@ -135,10 +135,10 @@ index 0000000..e5b087a + diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp new file mode 100644 -index 0000000..23128c0 +index 0000000..26d9be2 --- /dev/null +++ mlibc-workdir/sysdeps/astral/generic/generic.cpp -@@ -0,0 +1,448 @@ +@@ -0,0 +1,487 @@ +#include +#include +#include @@ -151,6 +151,45 @@ index 0000000..23128c0 +#include +#include + ++#ifndef MLIBC_BUILDING_RTDL ++// (s)brk is emulated in the libc rather than being ++// an actual system call. ++#define SBRK_BUFFER_SIZE (1024 * 1024 * 16) ++static void *sbrkbuffer; ++static off_t sbrkoffset; ++ ++extern "C" void *sbrk(intptr_t increment) { ++ // allocate the buffer if its the first time sbrk is called ++ if (sbrkbuffer == NULL) { ++ int e = mlibc::sys_vm_map(NULL, SBRK_BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0, &sbrkbuffer); ++ if (e) { ++ errno = e; ++ return NULL; ++ } ++ } ++ ++ if (increment == 0) ++ return sbrkbuffer; ++ ++ off_t newoffset = sbrkoffset + increment; ++ if (newoffset < 0) { ++ // offset goes below what we allocated, this is bad ++ errno = EINVAL; ++ return NULL; ++ } ++ ++ if (newoffset >= SBRK_BUFFER_SIZE) { ++ // new offset goes past what was allocated ++ errno = ENOMEM; ++ return NULL; ++ } ++ ++ void *ret = (void *)((uintptr_t)sbrkbuffer + increment); ++ sbrkoffset = newoffset; ++ return ret; ++} ++#endif ++ +namespace mlibc { + + int sys_setuid(uid_t id) {