1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
// Syscalls to find again in a recording: a file opened, written and closed,
// from the main thread, from a second thread, from a forked child, and from
// a signal handler (which has to return through the dispatcher too).
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <xmmintrin.h>
// MXCSR's flush-to-zero and denormals-are-zero: what -ffast-math turns on.
enum { flush_to_zero = 0x8040 };
static int fd;
static void say(const char *text) {
if (write(fd, text, strlen(text)) < 0) _exit(1);
}
static void on_signal(int signo) {
(void)signo;
say("signal\n");
}
// A thread starts with the floating-point modes of the thread that made it.
static void *thread(void *arg) {
say((_mm_getcsr() & flush_to_zero) == flush_to_zero ? arg : "thread without its maker's modes\n");
return NULL;
}
int main(void) {
fd = open("/dev/null", O_WRONLY);
say("hello from main\n");
// Every signal blocked around a syscall: the agent must keep its own deliverable.
sigset_t all, old;
sigfillset(&all);
sigprocmask(SIG_BLOCK, &all, &old);
say("masked\n");
sigprocmask(SIG_SETMASK, &old, NULL);
struct sigaction action = {.sa_handler = on_signal};
sigfillset(&action.sa_mask);
sigaction(SIGUSR1, &action, NULL);
raise(SIGUSR1);
pthread_t t;
_mm_setcsr(_mm_getcsr() | flush_to_zero);
pthread_create(&t, NULL, thread, "thread\n");
pthread_join(t, NULL);
pid_t child = fork();
if (child == 0) {
say("child\n");
_exit(3);
}
int status = 0;
waitpid(child, &status, 0);
say(WEXITSTATUS(status) == 3 ? "child exited 3\n" : "child lost\n");
close(fd);
return getpid() == getppid();
}
|