#include <sys/types.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "stress.h"
static pid_t pid;
static int failsafe;
static int
tobemangled(void) {
volatile int i, j;
j = 2;
for (i = 0; i < 100; i++)
j = j + 3;
j = j / 2;
return (j);
}
static void
mangle(void) {
int i;
char *p = (void *)tobemangled;
i = arc4random() % 50;
p[i] = arc4random() & 0xff;
}
static void
hand(int i __unused) {
_exit(1);
}
static void
ahand(int i __unused) {
if (pid != 0) {
kill(pid, SIGKILL);
}
_exit(EXIT_SUCCESS);
}
int
setup(int nb __unused)
{
return (0);
}
void
cleanup(void)
{
}
int
test(void)
{
void *st;
struct rlimit rl;
if (failsafe)
return (0);
while (done_testing == 0) {
signal(SIGALRM, ahand);
alarm(3);
if ((pid = fork()) == 0) {
rl.rlim_max = rl.rlim_cur = 0;
if (setrlimit(RLIMIT_CORE, &rl) == -1)
warn("setrlimit");
st = (void *)trunc_page((unsigned long)tobemangled);
if (mprotect(st, PAGE_SIZE, PROT_WRITE | PROT_READ |
PROT_EXEC) == -1)
err(1, "mprotect()");
signal(SIGALRM, hand);
signal(SIGILL, hand);
signal(SIGFPE, hand);
signal(SIGSEGV, hand);
signal(SIGBUS, hand);
signal(SIGURG, hand);
signal(SIGSYS, hand);
signal(SIGTRAP, hand);
mangle();
failsafe = 1;
(void)tobemangled();
_exit(EXIT_SUCCESS);
} else if (pid > 0) {
if (waitpid(pid, NULL, 0) == -1)
warn("waitpid(%d)", pid);
alarm(0);
} else
err(1, "fork(), %s:%d", __FILE__, __LINE__);
}
return (0);
}