Skip to content
Snippets Groups Projects
Forked from Glasgow Haskell Compiler / GHC
Source project has a limited visibility.
  • Erik de Castro Lopo's avatar
    0119fbc0
    Fix GHCi segfault during startup on linux-powerpc (#2972). · 0119fbc0
    Erik de Castro Lopo authored and pcapriotti's avatar pcapriotti committed
    Slightly modified version of a patch from Ben Collins <bcollins@ubuntu.com>
    who did the final debugging that showed the segfault was being caused the
    memory protection mechanism.
    
    Due to the requirement of "jump islands" to handle 24 bit relative jump
    offsets, GHCi on PowerPC did not use mmap to load object files like the
    other architectures. Instead, it allocated memory using malloc and fread
    to load the object code. However there is a quirk in the GNU libc malloc
    implementation. For memory regions over certain size (dynamic and
    configurable), malloc will use mmap to obtain the required memory instead
    of sbrk and malloc's call to mmap sets the memory readable and writable,
    but not executable. That means when GHCi loads code into a memory region
    that was mmapped instead of malloc-ed and tries to execute it we get a
    segfault.
    
    This solution drops the malloc/fread object loading in favour of using
    mmap and then puts the jump island for each object code module at the
    end of the mmaped region for that object.
    
    This patch may also be a solution on other ELF based powerpc systems
    but does not work on darwin-powerpc.
    
    MERGED from commit 3e6c9308
    0119fbc0
    History
    Fix GHCi segfault during startup on linux-powerpc (#2972).
    Erik de Castro Lopo authored and pcapriotti's avatar pcapriotti committed
    Slightly modified version of a patch from Ben Collins <bcollins@ubuntu.com>
    who did the final debugging that showed the segfault was being caused the
    memory protection mechanism.
    
    Due to the requirement of "jump islands" to handle 24 bit relative jump
    offsets, GHCi on PowerPC did not use mmap to load object files like the
    other architectures. Instead, it allocated memory using malloc and fread
    to load the object code. However there is a quirk in the GNU libc malloc
    implementation. For memory regions over certain size (dynamic and
    configurable), malloc will use mmap to obtain the required memory instead
    of sbrk and malloc's call to mmap sets the memory readable and writable,
    but not executable. That means when GHCi loads code into a memory region
    that was mmapped instead of malloc-ed and tries to execute it we get a
    segfault.
    
    This solution drops the malloc/fread object loading in favour of using
    mmap and then puts the jump island for each object code module at the
    end of the mmaped region for that object.
    
    This patch may also be a solution on other ELF based powerpc systems
    but does not work on darwin-powerpc.
    
    MERGED from commit 3e6c9308
Code owners
Assign users and groups as approvers for specific file changes. Learn more.