[RISCV] Probe residuals, small frames, call frames and realignment (#237170)
With inline stack probing ("probe-stack"="inline-asm", which
-fstack-clash-protection sets), the RISC-V prologue could leave more
than a probe interval of stack unprobed, so a sequence of frames could step
over a guard page:
- An allocation of at most the probe size, such as a whole frame under
4 KiB, was never probed, and neither was the residual left after the
probes of a larger one. A caller with a 10000-byte frame made its calls
3888 bytes below its last probe, and a callee with a 4064-byte local
array then stored first at the bottom of that array: 7952 bytes between
two stack accesses.
- Call frames smaller than the probe size were not probed.
- Realigning SP was probed at most once, and not at all with an RVV area
or a second SP adjustment of at least the probe size, although it moves
SP down by up to MaxAlign - StackAlign bytes: nearly 64 KiB for a 64 KiB
aligned local.
Follow the scheme AArch64 uses (StackProbeMaxUnprobedStack), which GCC's
RISC-V port also assumes (STACK_CLASH_CALLER_GUARD): at most 1024 bytes
above SP may be left unprobed at a call or on function entry, and an
allocation that probes starts from a probed SP.
- An allocation of the prologue probes the new SP when the bytes it
leaves unprobed, plus how far the allocations after it may move SP
before they probe, would exceed 1024: the realignment padding
(MaxAlign - StackAlign), and the largest call frame when call frames
are not reserved. The RVV area, a realignment loop and dynamic
allocations need a probed SP. A callee-saved register spilled to
0(sp) right after the SP adjustment serves as its probe.
- The residual of the RVV area's probing loop is always probed.
- The realignment makes its move with the RVV area's probing loop when
it may be the probe size or more.
- Call frames over 1024 bytes are probed.
The probe after a residual now comes after the CFI that describes it.