Debian Codegraph @

tests/corrupt-by-cow-opt.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2013 Joonsoo Kim, LG Electronics.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 }