pub fn stack_pointer() -> *mut u8
Expand description

Returns current stack pointer.

Note, that the stack pointer returned is from the perspective of the caller. From the perspective of stack_pointer function the pointer returned is the frame pointer.

While it is a goal to minimize the amount of stack used by this function, implementations for some targets may be unable to avoid allocating a stack frame. This makes this function suitable for stack exhaustion detection only in conjunction with sufficient padding.

Using stack_pointer to check for stack exhaustion is tricky to get right. It is impossible to know the callee’s frame size, therefore such value must be derived some other way. A common approach is to use stack padding (reserve enough stack space for any function to be called) and check against the padded threshold. If padding is chosen incorrectly, a situation similar to one described below may occur:

  1. For stack exhaustion check, remaining stack is checked against stack_pointer with the padding applied;
  2. Callee allocates more stack than was accounted for with padding, and accesses pages outside the stack, invalidating the execution (by e.g. crashing).