-
Notifications
You must be signed in to change notification settings - Fork 2
/
mmap_stale_pmd.c
74 lines (61 loc) · 1.54 KB
/
mmap_stale_pmd.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define MiB(a) ((a)*1024*1024)
#define KiB(a) ((a)*1024)
void err_exit(char *op)
{
fprintf(stderr, "%s: %s\n", op, strerror(errno));
exit(1);
}
int main(int argc, char *argv[])
{
volatile int a __attribute__((__unused__));
char *buffer = "HELLO WORLD!";
char *data;
int fd, err, ret = 0;
if (argc < 2) {
printf("Usage: %s <pmem file>\n", basename(argv[0]));
exit(0);
}
fd = open(argv[1], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
if (fd < 0)
err_exit("fd");
/*
* This allows us to map a huge zero page, and we do it at a non-zero
* offset for a little additional testing.
*/
ftruncate(fd, 0);
ftruncate(fd, MiB(4));
data = mmap(NULL, KiB(4), PROT_READ, MAP_SHARED, fd, KiB(4));
/*
* This faults in a 2MiB zero page to satisfy the read.
* 'a' is volatile so this read doesn't get optimized out.
*/
a = data[0];
pwrite(fd, buffer, strlen(buffer), KiB(4));
/*
* Try and use the mmap to read back the data we just wrote with
* pwrite(). If the kernel bug is present the mapping from the 2MiB
* zero page will still be intact, and we'll read back zeros instead.
*/
if (strncmp(buffer, data, strlen(buffer))) {
fprintf(stderr, "strncmp mismatch: '%s' vs '%s'\n", buffer,
data);
ret = 1;
}
err = munmap(data, KiB(4));
if (err < 0)
err_exit("munmap");
err = close(fd);
if (err < 0)
err_exit("close");
return ret;
}