2020-06-15 07:18:57 -07:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2023-10-14 01:06:00 -07:00
|
|
|
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
2020-06-15 07:18:57 -07:00
|
|
|
│ │
|
2020-12-27 17:18:44 -08:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 07:18:57 -07:00
|
|
|
│ │
|
2020-12-27 17:18:44 -08:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 07:18:57 -07:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2025-01-04 21:11:53 -08:00
|
|
|
#include "libc/assert.h"
|
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/struct/sigaction.h"
|
|
|
|
#include "libc/calls/struct/siginfo.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#include "libc/sysv/consts/sa.h"
|
|
|
|
#include "libc/sysv/consts/sig.h"
|
|
|
|
#include "libc/sysv/consts/ss.h"
|
|
|
|
#include "libc/thread/thread.h"
|
|
|
|
#include "libc/thread/tls.h"
|
2024-07-21 06:41:30 -07:00
|
|
|
|
|
|
|
/**
|
2025-01-04 21:11:53 -08:00
|
|
|
* stack overflow test #5
|
|
|
|
* - make sure fork() preserves sigaltstack()
|
|
|
|
* - make sure fork() preserves guard page status
|
2024-07-21 06:41:30 -07:00
|
|
|
*/
|
|
|
|
|
2025-01-04 21:11:53 -08:00
|
|
|
jmp_buf recover;
|
2024-07-21 06:41:30 -07:00
|
|
|
|
2025-01-04 21:11:53 -08:00
|
|
|
void CrashHandler(int sig, siginfo_t *si, void *ctx) {
|
|
|
|
unassert(__is_stack_overflow(si, ctx));
|
|
|
|
longjmp(recover, 123);
|
2023-10-14 01:06:00 -07:00
|
|
|
}
|
|
|
|
|
2024-07-23 04:04:19 -07:00
|
|
|
int StackOverflow(int d) {
|
|
|
|
char A[8];
|
|
|
|
for (int i = 0; i < sizeof(A); i++)
|
|
|
|
A[i] = d + i;
|
|
|
|
if (__veil("r", d))
|
|
|
|
return StackOverflow(d + 1) + A[d % sizeof(A)];
|
|
|
|
return 0;
|
2023-10-14 01:06:00 -07:00
|
|
|
}
|
2024-07-21 06:41:30 -07:00
|
|
|
|
|
|
|
void *MyPosixThread(void *arg) {
|
2025-01-04 21:11:53 -08:00
|
|
|
int pid;
|
|
|
|
unassert(__get_tls()->tib_sigstack_addr);
|
|
|
|
unassert((pid = fork()) != -1);
|
|
|
|
if (!pid) {
|
|
|
|
int jumpcode;
|
|
|
|
if (!(jumpcode = setjmp(recover))) {
|
|
|
|
StackOverflow(1);
|
|
|
|
_Exit(1);
|
|
|
|
}
|
|
|
|
unassert(123 == jumpcode);
|
|
|
|
} else {
|
|
|
|
int ws;
|
|
|
|
unassert(wait(&ws) != -1);
|
|
|
|
unassert(!ws);
|
|
|
|
pthread_exit(0);
|
|
|
|
}
|
2024-07-21 06:41:30 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
|
|
struct sigaction sa;
|
2025-01-04 21:11:53 -08:00
|
|
|
sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
|
2024-07-21 06:41:30 -07:00
|
|
|
sigemptyset(&sa.sa_mask);
|
2025-01-04 21:11:53 -08:00
|
|
|
sa.sa_sigaction = CrashHandler;
|
|
|
|
unassert(!sigaction(SIGBUS, &sa, 0));
|
|
|
|
unassert(!sigaction(SIGSEGV, &sa, 0));
|
2024-07-21 06:41:30 -07:00
|
|
|
|
2025-01-04 21:11:53 -08:00
|
|
|
pthread_t th;
|
2024-07-21 06:41:30 -07:00
|
|
|
pthread_attr_t attr;
|
2025-01-04 21:11:53 -08:00
|
|
|
unassert(!pthread_attr_init(&attr));
|
|
|
|
unassert(!pthread_attr_setguardsize(&attr, getpagesize()));
|
|
|
|
unassert(!pthread_attr_setsigaltstacksize_np(&attr, SIGSTKSZ));
|
|
|
|
unassert(!pthread_create(&th, &attr, MyPosixThread, 0));
|
|
|
|
unassert(!pthread_attr_destroy(&attr));
|
|
|
|
unassert(!pthread_join(th, 0));
|
2024-07-21 06:41:30 -07:00
|
|
|
}
|