-
Notifications
You must be signed in to change notification settings - Fork 3
/
mem.c
59 lines (49 loc) · 1.33 KB
/
mem.c
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
#include <spawn.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <sys/time.h>
#include <assert.h>
#ifndef _POSIX_SPAWN_DISABLE_ASLR
#define _POSIX_SPAWN_DISABLE_ASLR 0x0100
#endif
double get_time() {
struct timeval t;
int rc = gettimeofday(&t, NULL);
assert(rc == 0);
return (double) t.tv_sec + (double) t.tv_usec / 1e6;
}
void spin(int howlong) {
double t = get_time();
while ((get_time() - t) < (double) howlong) ;
}
int main(int argc, char* argv[]) {
if (argc == 2 && strcmp(argv[1], "child") == 0) {
// Child process.
int stack_v = 0;
pid_t pid = getpid();
printf("(PID %d) address of stack_v: %p\n", pid, &stack_v);
while (1) {
spin(1);
stack_v = stack_v + 1;
printf("(PID %d) stack_v: %d\n", pid, stack_v);
}
} else {
// Disable ASLR and fork program in a child process.
int ret;
short ps_flags = 0;
pid_t pid;
posix_spawn_file_actions_t actions;
posix_spawnattr_t attrs;
posix_spawn_file_actions_init(&actions);
posix_spawnattr_init(&attrs);
ps_flags |= POSIX_SPAWN_SETEXEC;
ps_flags |= _POSIX_SPAWN_DISABLE_ASLR;
ret = posix_spawnattr_setflags(&attrs, ps_flags);
if (ret == 0) {
char *args[] = { argv[0], "child", NULL };
posix_spawn(&pid, args[0], &actions, &attrs, args, NULL);
}
}
return 0;
}