Skip to content

Commit

Permalink
fix: Fiber alignment for allocation
Browse files Browse the repository at this point in the history
If the user isn't using FTL_FIBER_STACK_GUARD_PAGES, we must still set an alignment. Stacks require at least 16 byte alignment for basically all platforms. (Also passing zero for an alignment to AlignedAlloc() is an error)

Fixes #156
  • Loading branch information
RichieSams committed Nov 21, 2023
1 parent 317ef86 commit 380ae27
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion source/fiber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ Fiber::Fiber(size_t stackSize, FiberStartRoutine startRoutine, void *arg)
: m_arg(arg) {
#if defined(FTL_FIBER_STACK_GUARD_PAGES)
m_systemPageSize = SystemPageSize();
const size_t alignment = SystemPageSize();
#else
m_systemPageSize = 0;
// All systems require stacks that are at least 16 byte aligned
const size_t alignment = 16;
#endif

m_stackSize = RoundUp(stackSize, m_systemPageSize);
// We add a guard page both the top and the bottom of the stack
m_stack = AlignedAlloc(m_systemPageSize + m_stackSize + m_systemPageSize, m_systemPageSize);
m_stack = AlignedAlloc(m_systemPageSize + m_stackSize + m_systemPageSize, alignment);
m_context = boost_context::make_fcontext(static_cast<char *>(m_stack) + m_systemPageSize + stackSize, stackSize, startRoutine);

FTL_VALGRIND_REGISTER(static_cast<char *>(m_stack) + m_systemPageSize, static_cast<char *>(m_stack) + m_systemPageSize + stackSize);
Expand Down

0 comments on commit 380ae27

Please sign in to comment.