Skip to content

Commit bfb1615

Browse files
committed
Deal with stack alignment on 32-bit OSX
1 parent 3110a63 commit bfb1615

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ unsafe fn grow_the_stack<R, F: FnOnce() -> R>(stack_size: usize, f: F) -> R {
8282
new_limit: usize,
8383
}
8484

85+
// Align to 16-bytes (see below for why)
86+
let stack_size = (stack_size + 15) / 16 * 16;
87+
8588
// Allocate some new stack for oureslves
8689
let mut stack = Vec::<u8>::with_capacity(stack_size);
8790
let new_limit = stack.as_ptr() as usize + 32 * 1024;
@@ -101,7 +104,18 @@ unsafe fn grow_the_stack<R, F: FnOnce() -> R>(stack_size: usize, f: F) -> R {
101104
ret: None,
102105
new_limit: new_limit,
103106
};
104-
__stacker_switch_stacks(stack.as_mut_ptr() as usize + stack_size,
107+
108+
// Make sure the stack is 16-byte aligned which should be enough for all
109+
// platforms right now. Allocations on 64-bit are already 16-byte aligned
110+
// and our switching routine doesn't push any other data, but the routine on
111+
// 32-bit pushes an argument so we need a bit of an offset to get it 16-byte
112+
// aligned when the call is made.
113+
let offset = if cfg!(target_pointer_width = "32") {
114+
12
115+
} else {
116+
0
117+
};
118+
__stacker_switch_stacks(stack.as_mut_ptr() as usize + stack_size - offset,
105119
doit::<R, F> as usize as *const _,
106120
&mut cx as *mut _ as *mut _);
107121

0 commit comments

Comments
 (0)