This fixes fcontext on my PowerBook G4 running Void Linux
ppc-musl-20190901, NetBSD/macppc 8.1, or OpenBSD/macppc 6.6-current,
all with g++. These systems use fcontext for *ppc32_sysv_elf*
(PowerPC 32-bit System V ELF). The assembly code was wrong for BSD
and crashing on Linux musl.
Linux returns a transfer_t in memory (through a hidden pointer in R3),
but other systems (at least NetBSD and OpenBSD) return a transfer_t in
registers R3:R4. jump_fcontext() and ontop_fcontext() were always
using the hidden pointer. Add checks for `#ifdef__linux__`; start
using R3:R4 on other systems.
make_fcontext() was calling _exit(0) through the insecure BSS PLT.
Set R30 to use the secure PLT. This prevents a crash when musl's
ld.so loads the executable; musl seems to require the secure PLT.
Fix ontop_fcontext() to restore the hidden pointer on Linux. It was
passing the wrong context's hidden pointer to the ontop-function fn(),
so fn() returned a transfer_t to the wrong stack. When fn() was
context_exit() in <boost/context/continuation_fcontext.hpp>, it freed
the old stack, then returned `transfer_t{ nullptr, nullptr }` to free
memory. This crashed on Linux musl.
Now that ontop_fcontext() restores the hidden pointer, it must stop
abusing the same pointer to pass a transfer_t argument to fn(). Add a
new ontop_fcontext_tail() in C++, which takes arguments in registers
and allocates a transfer_t. The code is in C++ so it can free the
transfer_t argument if fn() throws a C++ exception.
Rearrange the context frame to shrink it from 244 to 240 bytes. This
fixes the stack alignment: the ABI requires R1 % 16 == 0, and
make_fcontext() respects this, but jump_fcontext() was adding 244 to
R1, so the new context ran with a misaligned stack (244 % 16 == 4).
Remove R13 from the context frame, so new contexts stop loading R13
with garbage. The ABI uses R13 to point to the executable's small
data, so R13 should have the same value in every context.
Add the backchain to the context frame; make room by moving LR to the
caller's frame. Order CR, R14 to R31, F14 to F31 at the frame's end,
as is typical for this ABI. Provide 8-byte alignment for FPSCR and
F14 to F31, to avoid a misalignment penalty.
Since OpenBSD 6.4 (https://www.openbsd.org/64.html), the stack pointer
must point to MAP_STACK memory, or the kernel may kill the process
with a signal. All stack allocators must pass MAP_STACK to mmap(2).
Define BOOST_CONTEXT_USE_MAP_STACK on OpenBSD, and don't define it on
other systems. This doesn't check for old versions of OpenBSD without
MAP_STACK; but OpenBSD has stopped maintaining versions before 6.4.
If BOOST_CONTEXT_USE_MAP_STACK is defined, then cause the stack
allocators to pass MAP_STACK to mmap(2):
- fixedsize_stack uses mmap/munmap instead of malloc/free. This
comes from a patch in OpenBSD Ports.
- protected_fixedsize_stack adds MAP_STACK to the mmap flags (as it
does in OpenBSD Ports). Assume that systems with MAP_STACK also
have MAP_ANON; this is true on OpenBSD. Delete POSIX comment,
because I can't find MAP_ANON nor MAP_ANONYMOUS in POSIX, so these
mmap calls don't conform to POSIX.
- pooled_fixedsize_stack can't call munmap, because the pool's free
doesn't know the allocation's size. Instead use posix_memalign to
allocate memory, then mmap to replace the pages with MAP_STACK
pages, so the pool's free may call std::free.
OpenBSD has no <ucontext.h>, so edit test/Jamfile.v2 to skip ucontext
tests on OpenBSD.
This commit and https://github.com/boostorg/test/pull/231 causes
libs/context/test `b2 full` to pass on OpenBSD 6.5 for 64-bit x86.
`b2 fc` fails because the allocator in test_fcontext.cpp does not use
MAP_STACK. The tests seem not to cover pooled_fixedsize_stack nor
protected_fixedsize_stack, but they still pass when I temporarily hack
callcc to use those allocators instead of fixedsize_stack.
The existing ontop_fcontext implementation for ppc64 ELFv2
violates the ABI by not storing the callback entry address
in %r12 before branching. This results in crashes on this
platform.
This commit addresses this and allows the context library
to function as expected on ppc64 platforms using the ELFv2 ABI.
It seems the macho code may have been modeled after the elf version,
which writes the returned transport_t through a pointer in eax,
however macho is expected to return its transport_t result in eax:edx.
The macho code mistakenly wrote its "data" return value through eax.
This happens to overwrite the saved fc_x87_cw before it is loaded,
resulting in floating-point exceptions and crashes in unrelated code.
- fix a cast from void* to the wrong dynamic type. The code cast from
tuple<decay_t<Fn>>* -> void* -> tuple<Fn>, which worked when an rvalue
was passed to resume_with(), but broke if the function was passed
by lvalue-reference, because it resulted in a cast from Fn to Fn&.
- remove use of std::tuple - no need to instantiate it for every callable.
Signed-off-by: Damian Jarek <damian.jarek93@gmail.com>
- in context of #91
Add a .file directive to all *_elf_gas.S files to prevent the linker
adding a host build-system path as a FILE symbol to the object file.
Without this, the gnu linker adds a symbol like this:
thanks to Douglas Royds <douglas.royds@taitradio.com>
openembedded/openembedded-core