// SPDX-License-Identifier: GPL-2.0 /* * Test that loads/stores expand the stack segment, or trigger a SEGV, in * various conditions. * * Based on test code by Tom Lane.
*/
/* * Consume stack until the stack pointer is below @target_sp, then do an access * (load or store) at offset @delta from either the base of the stack or the * current stack pointer.
*/
__attribute__ ((noinline)) int consume_stack(unsignedlong target_sp, unsignedlong stack_high, int delta, enum access_type type)
{ unsignedlong target; char stack_cur;
if ((unsignedlong)&stack_cur > target_sp) return consume_stack(target_sp, stack_high, delta, type); else { // We don't really need this, but without it GCC might not // generate a recursive call above.
stack_top_ptr = &stack_cur;
printf("Access OK: %s delta %-7d used size 0x%06x stack high 0x%lx top_ptr %p top sp 0x%lx actual used 0x%lx\n",
type == LOAD ? "load" : "store", delta, stack_used, stack_high,
stack_top_ptr, stack_top_sp, stack_high - stack_top_sp + 1);
return 0;
}
staticint test_one(unsignedint stack_used, int delta, enum access_type type)
{
pid_t pid; int rc;
pid = fork(); if (pid == 0) exit(child(stack_used, delta, type));
assert(waitpid(pid, &rc, 0) != -1);
if (WIFEXITED(rc) && WEXITSTATUS(rc) == 0) return 0;
// We don't expect a non-zero exit that's not a signal
assert(!WIFEXITED(rc));
printf("Faulted: %s delta %-7d used size 0x%06x signal %d\n",
type == LOAD ? "load" : "store", delta, stack_used,
WTERMSIG(rc));
return 1;
}
// This is fairly arbitrary but is well below any of the targets below, // so that the delta between the stack pointer and the target is large. #define DEFAULT_SIZE (32 * _KB)
// We should be able to access anywhere within the rlimit for (delta = page_size; delta <= rlim_cur; delta += page_size)
assert(test_one(DEFAULT_SIZE, delta, type) == 0);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.