Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Correct stack alignment calculation
Browse files Browse the repository at this point in the history
Previously align_offset(STACK_ALIGNMENT) was substracted, which is
wrong, because the documentation states:
   > Computes the offset that needs to be applied to the pointer in
   > order to make it aligned.

This patch first substracts STACK_ALIGNMENT and then adds
align_offset(STACK_ALIGNMENT), if the stack was not yet aligned

Also calculating the top with .add(stack.len() - 1) so the stack pointer
points initially to a valid memory address.
  • Loading branch information
haraldh committed Oct 1, 2019
1 parent fba2388 commit 2aa88fc
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,14 @@ impl<'a, Y, R> Coroutine<'a, Y, R> {

unsafe {
// Calculate the aligned top of the stack.
let top = stack.as_mut_ptr().add(stack.len());
let top = top.sub(top.align_offset(STACK_ALIGNMENT));
let top = stack.as_mut_ptr().add(stack.len() - 1);
// If the top isn't aligned yet, calculate the aligned top of the stack.
let top = if top.align_offset(STACK_ALIGNMENT) != 0 {
let top = top.sub(STACK_ALIGNMENT);
top.add(top.align_offset(STACK_ALIGNMENT))
} else {
top
};

// Call into the callback on the specified stack.
jump_init(
Expand Down

0 comments on commit 2aa88fc

Please sign in to comment.