microsoft / snmalloc

Message passing based allocator
MIT License
1.56k stars 108 forks source link

Segmentation Fault on Apple Silicon Asahi Linux #663

Closed SchrodingerZhu closed 1 week ago

SchrodingerZhu commented 3 months ago
30/87 Test #30: perf-contention-check ...............   Passed    0.90 sec
      Start 31: perf-external_pointer-check
31/87 Test #31: perf-external_pointer-check .........***Exception: SegFault  0.28 sec
      Start 32: perf-low_memory-check
32/87 Test #32: perf-low_memory-check ...............   Passed    0.00 sec
      Start 33: perf-memcpy-check
33/87 Test #33: perf-memcpy-check ...................***Exception: SegFault  0.08 sec
      Start 34: perf-msgpass-check
34/87 Test #34: perf-msgpass-check ..................***Exception: SegFault  0.15 sec
      Start 35: perf-singlethread-check
35/87 Test #35: perf-singlethread-check .............***Exception: SegFault  0.10 sec
      Start 36: perf-startup-check
36/87 Test #36: perf-startup-check ..................***Exception: SegFault  0.13 sec
      Start 37: func-bits-check
37/87 Test #37: func-bits-check .....................   Passed    0.00 sec
      Start 38: func-cheri-check
38/87 Test #38: func-cheri-check ....................   Passed    0.00 sec
      Start 39: func-client_meta-check
39/87 Test #39: func-client_meta-check ..............***Exception: SegFault  0.07 sec
      Start 40: func-domestication-check
40/87 Test #40: func-domestication-check ............***Exception: SegFault  0.06 sec
      Start 41: func-external_pagemap-check
41/87 Test #41: func-external_pagemap-check .........   Passed    0.00 sec
      Start 42: func-first_operation-check
42/87 Test #42: func-first_operation-check ..........***Exception: SegFault  0.06 sec
      Start 43: func-fixed_region-check
43/87 Test #43: func-fixed_region-check .............   Passed    0.06 sec
      Start 44: func-jemalloc-check
44/87 Test #44: func-jemalloc-check .................***Exception: SegFault  0.05 sec
      Start 45: func-malloc-check
45/87 Test #45: func-malloc-check ...................***Exception: SegFault  0.07 sec
      Start 46: func-memcpy-check
46/87 Test #46: func-memcpy-check ...................***Exception: SegFault  0.05 sec
      Start 47: func-memory-check
47/87 Test #47: func-memory-check ...................***Exception: SegFault  0.06 sec
      Start 48: func-memory_usage-check
48/87 Test #48: func-memory_usage-check .............***Exception: SegFault  0.08 sec
      Start 49: func-miracle_ptr-check
49/87 Test #49: func-miracle_ptr-check ..............***Exception: SegFault  0.08 sec
      Start 50: func-pagemap-check
50/87 Test #50: func-pagemap-check ..................***Exception: SegFault  0.07 sec
      Start 51: func-pool-check
51/87 Test #51: func-pool-check .....................   Passed    0.00 sec
      Start 52: func-redblack-check
52/87 Test #52: func-redblack-check .................   Passed    1.14 sec
      Start 53: func-sandbox-check
53/87 Test #53: func-sandbox-check ..................   Passed    0.01 sec
      Start 54: func-sizeclass-check
54/87 Test #54: func-sizeclass-check ................   Passed    0.01 sec
      Start 55: func-statistics-check
55/87 Test #55: func-statistics-check ...............***Exception: SegFault  0.08 sec
      Start 56: func-teardown-check
56/87 Test #56: func-teardown-check .................***Exception: SegFault  0.08 sec
      Start 57: func-thread_alloc_external-check
57/87 Test #57: func-thread_alloc_external-check ....***Exception: SegFault  0.05 sec
      Start 58: func-two_alloc_types-check
58/87 Test #58: func-two_alloc_types-check ..........***Exception: SegFault  0.07 sec
SchrodingerZhu commented 3 months ago

strace can show similar like the following

mprotect(0xffaf10631000, 274877911040, PROT_READ) = -1 EINVAL (Invalid argument)
madvise(0xffaf10631000, 274877911040, MADV_DONTDUMP) = -1 EINVAL (Invalid argument)
mprotect(0xffaf10631000, 4096, PROT_READ|PROT_WRITE) = -1 EINVAL (Invalid argument)
madvise(0xffaf10631000, 4096, MADV_DODUMP) = -1 EINVAL (Invalid argument)
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_ACCERR, si_addr=0xffaf10631dc0} ---
+++ killed by SIGSEGV (core dumped) +++

Asahi Linux utilizes 16k aligned pages.

mjp41 commented 3 months ago

Looks like we need some more #ifdef in: https://github.com/microsoft/snmalloc/blob/835ab5186325736ff5aa8ef53728970d1d74c3bc/src/snmalloc/pal/pal_linux.h#L32-L33

How could we detect this configuration? Perhaps, we should be using

sys_conf(_SC_PAGE_SIZE)

to detect the pagesize properly.

mjp41 commented 3 months ago

It seems this can be done by macros. https://www.man7.org/linux/man-pages/man3/sysconf.3.html

We could change the Posix Pal to:

#ifdef PAGESIZE
    static constexpr size_t page_size = PAGESIZE;
#else
    static constexpr size_t page_size = Aal::smallest_page_size;
#endif
mjp41 commented 3 months ago

FYI, I have a bit set of changes in flight, so can fix after those. Or please feel free to raise a PR.

mjp41 commented 3 months ago

Tried this in #664.

mjp41 commented 1 week ago

@SchrodingerZhu with the CMake configuration is this fixed? Should we do more to fix this?