| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <unistd.h> |
| 22 |
#include <sys/mman.h> |
| 23 |
#include <hugetlbfs.h> |
| 24 |
#include "hugetests.h" |
| 25 |
|
| 26 |
#define P "corrupt-by-cow-opt" |
| 27 |
#define DESC \ |
| 28 |
"* Test sanity of cow optimization on page cache. If a page *\n"\ |
| 29 |
"* in page cache has only 1 ref count, it is mapped for a private *\n"\ |
| 30 |
"* mapping directly and is overwritten freely, so next time we *\n"\ |
| 31 |
"* access the page, we can see corrupt data. *\n"\ |
| 32 |
|
| 33 |
int main(int argc, char *argv[]) |
| 34 |
{ |
| 35 |
long hpage_size; |
| 36 |
int fd; |
| 37 |
char *p; |
| 38 |
char c; |
| 39 |
|
| 40 |
test_init(argc, argv); |
| 41 |
|
| 42 |
hpage_size = check_hugepagesize(); |
| 43 |
|
| 44 |
check_free_huge_pages(2); |
| 45 |
|
| 46 |
fd = hugetlbfs_unlinked_fd(); |
| 47 |
if (fd < 0) |
| 48 |
FAIL("hugetlbfs_unlinked_fd()"); |
| 49 |
|
| 50 |
p = mmap(NULL, hpage_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 51 |
if (p == MAP_FAILED) |
| 52 |
FAIL("mmap() 1: %s", strerror(errno)); |
| 53 |
|
| 54 |
*p = 's'; |
| 55 |
verbose_printf("Write %c to %p via shared mapping\n", *p, p); |
| 56 |
munmap(p, hpage_size); |
| 57 |
|
| 58 |
p = mmap(NULL, hpage_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| 59 |
if (p == MAP_FAILED) |
| 60 |
FAIL("mmap() 2: %s", strerror(errno)); |
| 61 |
|
| 62 |
*p = 'p'; |
| 63 |
verbose_printf("Write %c to %p via private mapping\n", *p, p); |
| 64 |
munmap(p, hpage_size); |
| 65 |
|
| 66 |
p = mmap(NULL, hpage_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 67 |
if (p == MAP_FAILED) |
| 68 |
FAIL("mmap() 3: %s", strerror(errno)); |
| 69 |
|
| 70 |
c = *p; |
| 71 |
verbose_printf("Read %c from %p via shared mapping\n", *p, p); |
| 72 |
munmap(p, hpage_size); |
| 73 |
|
| 74 |
if (c != 's') |
| 75 |
FAIL("data corrupt"); |
| 76 |
|
| 77 |
PASS(); |
| 78 |
} |