Debian Codegraph @

tests/fork-cow.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2008 David Gibson, IBM Corporation.
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 #define _GNU_SOURCE
20
21 #include <sys/types.h>
22 #include <sys/shm.h>
23 #include <sys/wait.h>
24 #include <sys/mman.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <hugetlbfs.h>
34 #include "hugetests.h"
35
36 /*
37 * Test rationale:
38 *
39 * This checks copy-on-write semantics, specifically the semantics of
40 * a MAP_PRIVATE mapping across a fork(). Some versions of the
41 * powerpc kernel had a bug in huge_ptep_set_wrprotect() which would
42 * fail to flush the hash table after setting the write protect bit in
43 * the parent's page tables, thus allowing the parent to pollute the
44 * child's mapping.
45 */
46
47 #define RANDOM_CONSTANT 0x1234ABCD
48 #define OTHER_CONSTANT 0xfeef5678
49
50 /*
51 * The parent uses this to check if the child terminated badly.
52 */
53 static void sigchld_handler(int signum, siginfo_t *si, void *uc)
54 {
55 if (WEXITSTATUS(si->si_status) != 0)
56 FAIL("Child failed: %d", WEXITSTATUS(si->si_status));
57 if (WIFSIGNALED(si->si_status))
58 FAIL("Child recived signal %s",
59 strsignal(WTERMSIG(si->si_status)));
60 }
61
62 int main(int argc, char ** argv)
63 {
64 int fd, ret, status;
65 void *syncarea;
66 volatile unsigned int *p;
67 volatile unsigned int *trigger, *child_readback;
68 unsigned int parent_readback;
69 long hpage_size;
70 pid_t pid;
71 struct sigaction sa = {
72 .sa_sigaction = sigchld_handler,
73 .sa_flags = SA_SIGINFO,
74 };
75
76 test_init(argc, argv);
77
78 check_free_huge_pages(2);
79
80 if (argc != 1)
81 CONFIG("Usage: fork-cow\n");
82
83 /* Get a shared normal page for synchronization */
84 verbose_printf("Mapping synchronization area..");
85 syncarea = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
86 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
87 if (syncarea == MAP_FAILED)
88 FAIL("mmap() sync area: %s", strerror(errno));
89 verbose_printf("done\n");
90
91 trigger = syncarea;
92 *trigger = 0;
93
94 child_readback = trigger + 1;
95 *child_readback = 0;
96
97 hpage_size = check_hugepagesize();
98
99 fd = hugetlbfs_unlinked_fd();
100 if (fd < 0)
101 CONFIG("hugetlbfs_unlinked_fd() failed\n");
102
103 verbose_printf("Mapping hugepage area...");
104 p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
105 if (p == MAP_FAILED)
106 FAIL("mmap(): %s", strerror(errno));
107 verbose_printf("mapped at %p\n", p);
108
109 /* Touch the page for write in parent */
110 verbose_printf("Parent writes pre-fork...");
111 *p = RANDOM_CONSTANT;
112 verbose_printf("%x\n", RANDOM_CONSTANT);
113
114 ret = sigaction(SIGCHLD, &sa, NULL);
115 if (ret)
116 FAIL("sigaction(): %s", strerror(errno));
117
118 if ((pid = fork()) < 0)
119 FAIL("fork(): %s", strerror(errno));
120
121 if (pid != 0) {
122 /* Parent */
123 verbose_printf("Parent writes post-fork...");
124 *p = ~RANDOM_CONSTANT;
125 verbose_printf("%x\n", ~RANDOM_CONSTANT);
126
127 *trigger = 1;
128
129 while (*trigger != 2)
130 ;
131
132 verbose_printf("Parent reads..");
133 parent_readback = *p;
134 verbose_printf("%x\n", parent_readback);
135
136 *trigger = 3;
137 } else {
138 /* Child */
139 verbose_printf("Child starts..\n");
140
141 while (*trigger != 1)
142 ;
143
144 verbose_printf("Child reads...");
145 *child_readback = *p;
146 verbose_printf("%x\n", *child_readback);
147
148 verbose_printf("Child writes...");
149 *p = OTHER_CONSTANT;
150 verbose_printf("%x\n", OTHER_CONSTANT);
151
152 *trigger = 2;
153
154 while (*trigger != 3)
155 ;
156
157 verbose_printf("Child exits...\n");
158 exit(0);
159 }
160
161 verbose_printf("child_readback = 0x%x, parent_readback = 0x%x\n",
162 *child_readback, parent_readback);
163
164 if (*child_readback != RANDOM_CONSTANT)
165 FAIL("Child read back 0x%x instead of 0x%x",
166 *child_readback, RANDOM_CONSTANT);
167 if (parent_readback != ~RANDOM_CONSTANT)
168 FAIL("Parent read back 0x%x instead of 0x%x",
169 parent_readback, RANDOM_CONSTANT);
170
171 ret = waitpid(pid, &status, 0);
172 if (ret < 0)
173 FAIL("waitpid(): %s", strerror(errno));
174
175 PASS();
176 }