Re: sbrk

From: Warner Losh <imp_at_bsdimp.com>
Date: Sun, 26 Nov 2023 04:56:03 UTC
On Sat, Nov 25, 2023 at 8:58 PM Bakul Shah <bakul@iitbombay.org> wrote:

> On Nov 25, 2023, at 7:47 PM, Warner Losh <imp@bsdimp.com> wrote:
>
>
> On Sat, Nov 25, 2023, 8:44 PM <joseph@josephholsten.com> wrote:
>
>> Yes, it’s no longer included. It’s long been deprecated, but that does
>> make porting things like the original vi a bit challenging.
>>
>> Is there a particular project you’re trying to use it for?
>>
>
> It was never included in FreeBSD/arm64 due to the fact that the address
> space is complicated now and there no longer is an area beyond bss that you
> can expand into... let alone contract...
>
> Emacs was not happy with it...
>
>
> Thanks. Note that linux does provide it (may be not perfect but I thought
> FreeBSD cared more about compatibility....).
>
> This came up in trying to compile the v language
> https://github.com/vlang/v
>
> It uses a libgc which seems to be derived from some ancestor of BDW GC and
> there are so many defines my eyes glaze over.
>

I see that it also uses tcc, which I coincidentally was looking at in the
cdefs modernization efforts I've been doing. You'll need at least one patch
to cdefs to get even the basics to compile. And three tests fail (one
doesn't matter, one is constructors and dtors and a third fails to detect
out of bounds access). I think only the ctor/dtor one is going to make
things hard for you. I've not looked into any of the test failures, just a
short-coming in cdefs.h since tcc doesn't support .symver yet.

sbrk can be faked with mmap of a large area up front with MAP_GUARD that's
then grown or shrunk as new sbrk calls happen and remapped with MAP_FIXED.
The only draw-back is you need reserve enough address space for all the
program's memory needs (like GB of space maybe). The MAP_GUARD mappings are
relatively cheap until it's actually used. Heck, you can even map SIGSEGV
to check to see if you've "overflowed" the area to make it bigger (I hate
that I know this trick, thank you Bourne shell).

Looks fun to play with. Maybe I'd help (but I already have too many fun and
even more un-fun projects).

Warner


> Warner
>
> --
>> Joseph Holsten
>> On Nov 25, 2023 at 19:41 -0800, Bakul Shah <bakul@iitbombay.org>, wrote:
>>
>> Does sbrk not exist on FreeBSD-14 on arm64? Is this by design?
>>
>> $ cat sb.c
>> #include <unistd.h>
>> #include <stdio.h>
>> int main(int c, char**v) {
>>         void *x = sbrk(102400);
>>         printf("%p\n", x);
>> }
>> $ cc sb.c
>> ld: error: undefined symbol: sbrk
>> >>> referenced by sb.c
>> >>>               /tmp/sb-e97caf.o:(main)
>> cc: error: linker command failed with exit code 1 (use -v to see
>> invocation)
>>
>>
>