ysbaddaden / gc

A garbage collector for Crystal
95 stars 6 forks source link

Support various POSIX platforms (darwin, freebsd, linux-musl, openbsd) #6

Open ysbaddaden opened 6 years ago

ysbaddaden commented 6 years ago

We only support the linux-gnu target, which is the computer I use everyday. There are a few places that require platform specific stuff that may need to be adapted:

  1. The physical memory getter, see GC_getMemoryLimit in include/memory.h; GC uses this for the default maximum allocatable memory limit;

  2. Pointers to the DATA and BSS sections, which are special fixed-size stacks where (un)initialized constants are allocated; see GC_Collector_init in src/collector.c where I use symbols injected by the linker __data_start, __bss_start and _end; I assume those symbols exist for most ELF executables, but maybe not. Mach-O and PE probably use something entirely different.

  3. The stack bottom detector, only used to detect the beginning of the main thread stack, to properly initialize the stack for the main fiber; see GC.stack_bottom in src/immix.cr;

    • I currently use a private glibc symbol (__libc_stack_end);
    • In linux we can read the 28th value from /proc/self/stat as per proc(5) (warning: don't use File because it's a class and relies on GC.malloc).
    • Other platforms may need to setup a temporary segfault handler, iterate the stack back and wait for a segfault to determine the stack bottom (BDW does that).

Note: the issue is about supporting other POSIX systems, supporting Windows will require to use a File Mapping with INVALID_HANDLE_VALUE instead of an anonymous mmap.

ysbaddaden commented 6 years ago

About 2. [addresses of DATA and BSS sections]:

Documentation on this topic is scarce... sigh.

ysbaddaden commented 6 years ago

The DATA and BSS sections detection has been added in c86fba829222c4c0571a0cf0106dd2e76963437f but hasn't been tested because we need others to be implemented too.

bararchy commented 6 years ago

@ysbaddaden Can support for musl be added? So that a project can be static compiled with the gc

ysbaddaden commented 6 years ago

Yes, this is the one thing to implement (from the issue description):

detect the stack bottom: in linux we can read the 28th value from /proc/self/stat as per proc(5).