aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandre Oliva <oliva@adacore.com>2023-12-14 04:50:45 -0300
committerAlexandre Oliva <oliva@gnu.org>2023-12-20 05:18:21 -0300
commit4e0a467302fea56d63b7a6d17f99c0f388960dc7 (patch)
tree0cdaf393c2c5e6b0b69f02d5ce48e8675155c8b9
parent9fa35dbb901b11d31a897cc88c9258e7cd35b899 (diff)
downloadgcc-4e0a467302fea56d63b7a6d17f99c0f388960dc7.zip
gcc-4e0a467302fea56d63b7a6d17f99c0f388960dc7.tar.gz
gcc-4e0a467302fea56d63b7a6d17f99c0f388960dc7.tar.bz2
strub: sparc64: unbias the stack address [PR112917]
The stack pointer is biased by 2047 bytes on sparc64, so the range it delimits is way off. Unbias the addresses returned by __builtin_stack_address (), so that the strub builtins, inlined or not, can function correctly. I've considered introducing a new target macro, but using STACK_POINTER_OFFSET seems safe, and it enables the register save areas to be scrubbed as well. Because of the large fixed-size outgoing args area next to the register save area on sparc, we still need __strub_leave to not allocate its own frame, otherwise it won't be able to clear part of the frame it should. for gcc/ChangeLog PR middle-end/112917 * builtins.cc (expand_bultin_stack_address): Add STACK_POINTER_OFFSET. * doc/extend.texi (__builtin_stack_address): Adjust.
-rw-r--r--gcc/builtins.cc34
-rw-r--r--gcc/doc/extend.texi23
2 files changed, 54 insertions, 3 deletions
diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 23cc6b6..0f64fee 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -5451,8 +5451,38 @@ expand_builtin_frame_address (tree fndecl, tree exp)
static rtx
expand_builtin_stack_address ()
{
- return convert_to_mode (ptr_mode, copy_to_reg (stack_pointer_rtx),
- STACK_UNSIGNED);
+ rtx ret = convert_to_mode (ptr_mode, copy_to_reg (stack_pointer_rtx),
+ STACK_UNSIGNED);
+
+ /* Unbias the stack pointer, bringing it to the boundary between the
+ stack area claimed by the active function calling this builtin,
+ and stack ranges that could get clobbered if it called another
+ function. It should NOT encompass any stack red zone, that is
+ used in leaf functions.
+
+ On SPARC, the register save area is *not* considered active or
+ used by the active function, but rather as akin to the area in
+ which call-preserved registers are saved by callees. This
+ enables __strub_leave to clear what would otherwise overlap with
+ its own register save area.
+
+ If the address is computed too high or too low, parts of a stack
+ range that should be scrubbed may be left unscrubbed, scrubbing
+ may corrupt active portions of the stack frame, and stack ranges
+ may be doubly-scrubbed by caller and callee.
+
+ In order for it to be just right, the area delimited by
+ @code{__builtin_stack_address} and @code{__builtin_frame_address
+ (0)} should encompass caller's registers saved by the function,
+ local on-stack variables and @code{alloca} stack areas.
+ Accumulated outgoing on-stack arguments, preallocated as part of
+ a function's own prologue, are to be regarded as part of the
+ (caller) function's active area as well, whereas those pushed or
+ allocated temporarily for a call are regarded as part of the
+ callee's stack range, rather than the caller's. */
+ ret = plus_constant (ptr_mode, ret, STACK_POINTER_OFFSET);
+
+ return force_reg (ptr_mode, ret);
}
/* Expand a call to builtin function __builtin_strub_enter. */
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 766e033..4cc3b61b 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -12669,7 +12669,28 @@ situations.
@enddefbuiltin
@deftypefn {Built-in Function} {void *} __builtin_stack_address ()
-This function returns the value of the stack pointer register.
+This function returns the stack pointer register, offset by
+@code{STACK_POINTER_OFFSET}.
+
+Conceptually, the returned address returned by this built-in function is
+the boundary between the stack area allocated for use by its caller, and
+the area that could be modified by a function call, that the caller
+could safely zero-out before or after (but not during) the call
+sequence.
+
+Arguments for a callee may be preallocated as part of the caller's stack
+frame, or allocated on a per-call basis, depending on the target, so
+they may be on either side of this boundary.
+
+Even if the stack pointer is biased, the result is not. The register
+save area on SPARC is regarded as modifiable by calls, rather than as
+allocated for use by the caller function, since it is never in use while
+the caller function itself is running.
+
+Red zones that only leaf functions could use are also regarded as
+modifiable by calls, rather than as allocated for use by the caller.
+This is only theoretical, since leaf functions do not issue calls, but a
+constant offset makes this built-in function more predictable.
@end deftypefn
@node Stack Scrubbing