OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 /* | |
8 * This test verifies that SysV shared memory, created via shmget and | |
9 * mapped via shmat, can be mmap'd over in parts. | |
10 */ | |
11 | |
12 #include <stdio.h> | |
13 #include <stdint.h> | |
14 #include <stdlib.h> | |
15 #include <unistd.h> | |
16 #include <sys/types.h> | |
17 #include <sys/stat.h> | |
18 #include <fcntl.h> | |
19 #include <sys/ipc.h> | |
20 #include <sys/shm.h> | |
21 #include <sys/mman.h> | |
22 | |
23 #include "native_client/src/trusted/platform_qualify/linux/sysv_shm_and_mmap.h" | |
24 | |
25 #define SYSVSHM_SIZE 65536 | |
26 #define MMAP_OFFSET 4096 | |
27 #define MMAP_SIZE 4096 | |
28 | |
29 #ifdef DEBUG_SHM_AND_MMAP | |
30 # define DPRINTF(arglist) do { printf arglist; } while (0) | |
31 #else | |
32 # define DPRINTF(arglist) do { ; } while (0) | |
33 #endif | |
34 | |
35 static void FillWithPattern(void *memory, | |
36 size_t size, | |
37 int counter) { | |
38 unsigned char *mem_ptr = (unsigned char *) memory; | |
39 size_t ix; | |
40 | |
41 for (ix = 0; ix < size; ++ix) { | |
42 mem_ptr[ix] = counter; | |
43 ++counter; | |
44 counter &= 0xff; | |
45 } | |
46 } | |
47 | |
48 static int VerifyPattern(void *memory, | |
49 size_t size, | |
50 int counter) { | |
51 unsigned char *mem_ptr = (unsigned char *) memory; | |
52 size_t ix; | |
53 | |
54 for (ix = 0; ix < size; ++ix) { | |
55 if (mem_ptr[ix] != counter) { | |
56 fprintf(stderr, | |
57 "Memory at %p is wrong: expected %d, got %d\n", | |
58 mem_ptr + ix, | |
59 counter, | |
60 mem_ptr[ix]); | |
61 return 0; | |
62 } | |
63 ++counter; | |
64 counter &= 0xff; | |
65 } | |
66 return 1; | |
67 } | |
68 | |
69 | |
70 /* | |
71 * Verify the allocated shared memory ID. | |
72 */ | |
73 int NaClPlatformQualifySysVShmId(int shm_id) { | |
74 void *shm_addr; | |
75 void *shm_addr2; | |
76 void *mmap_addr; | |
77 struct shmid_ds shm_ds; | |
78 | |
79 shm_addr = shmat(shm_id, (const void *) NULL, 0); | |
80 DPRINTF(("shmat -> %p\n", shm_addr)); | |
81 if (NULL == shm_addr) { | |
82 perror("platform_qualify: sysv_shm_and_mmap: shmat"); | |
83 return 2; | |
84 } | |
85 | |
86 FillWithPattern(shm_addr, SYSVSHM_SIZE, 0); | |
87 | |
88 if (-1 == shmctl(shm_id, IPC_STAT, &shm_ds)) { | |
89 perror("platform_qualify: sysv_shm_and_mmap: no shmctl IPC_STAT\n"); | |
90 return 3; | |
91 } | |
92 if (1 != shm_ds.shm_nattch) { | |
93 fprintf(stderr, | |
94 "platform_qualify: sysv_shm_and_mmap: shm_nattch (%lu) != 1\n", | |
95 (unsigned long) shm_ds.shm_nattch); | |
96 return 4; | |
97 } | |
98 | |
99 mmap_addr = (void *) ((uintptr_t) shm_addr + MMAP_OFFSET); | |
100 DPRINTF(("mmap addr %p\n", mmap_addr)); | |
101 if (MAP_FAILED == mmap(mmap_addr, | |
102 MMAP_SIZE, | |
103 PROT_READ | PROT_WRITE, | |
104 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, | |
105 -1, | |
106 0)) { | |
107 perror("platform_qualify: sysv_shm_and_mmap: mmap"); | |
108 return 5; | |
109 } | |
110 DPRINTF(("mmap succeeded\n")); | |
111 /* | |
112 * Splitting an shmat mapping will increase the shm_nattch count by 1. | |
113 */ | |
114 | |
115 if (-1 == shmctl(shm_id, IPC_STAT, &shm_ds)) { | |
116 perror("platform_qualify: sysv_shm_and_mmap: no shmctl IPC_STAT\n"); | |
117 return 6; | |
118 } | |
119 if (2 != shm_ds.shm_nattch) { | |
120 fprintf(stderr, | |
121 "platform_qualify: sysv_shm_and_mmap: shm_nattch (%lu) != 2\n", | |
122 (unsigned long) shm_ds.shm_nattch); | |
123 return 7; | |
124 } | |
125 | |
126 FillWithPattern(mmap_addr, MMAP_SIZE, 1); | |
127 | |
128 shm_addr2 = shmat(shm_id, (const void *) NULL, 0); | |
129 DPRINTF(("shmat -> %p\n", shm_addr2)); | |
130 if (NULL == shm_addr2) { | |
131 perror("platform_qualify: sysv_shm_and_mmap: shmat 2nd time\n"); | |
132 return 8; | |
133 } | |
134 | |
135 if (-1 == shmctl(shm_id, IPC_STAT, &shm_ds)) { | |
136 perror("platform_qualify: sysv_shm_and_mmap: no shmctl IPC_STAT\n"); | |
137 return 9; | |
138 } | |
139 if (3 != shm_ds.shm_nattch) { | |
140 fprintf(stderr, | |
141 "platform_qualify: sysv_shm_and_mmap: shm_nattch (%lu) != 3\n", | |
142 (unsigned long) shm_ds.shm_nattch); | |
143 return 10; | |
144 } | |
145 | |
146 if (!VerifyPattern(shm_addr2, SYSVSHM_SIZE, 0)) { | |
147 fprintf(stderr, | |
148 "platform_qualify: writing to mmap memory overwrote" | |
149 " sysv shm memory?!?\n"); | |
150 return 11; | |
151 } | |
152 FillWithPattern(shm_addr2, SYSVSHM_SIZE, 0); | |
153 if (!VerifyPattern(mmap_addr, MMAP_SIZE, 1)) { | |
154 fprintf(stderr, | |
155 "platform_qualify: writng to shm memory" | |
156 " overwrite mmap memory?!?\n"); | |
157 return 12; | |
158 } | |
159 if (MAP_FAILED == mmap(shm_addr, | |
160 SYSVSHM_SIZE, | |
161 PROT_READ | PROT_WRITE, | |
162 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, | |
163 -1, | |
164 0)) { | |
165 perror("platform_qualify: sysv_shm_and_mmap: mmap over all" | |
166 " shm_addr failed\n"); | |
167 return 13; | |
168 } | |
169 | |
170 if (-1 == shmctl(shm_id, IPC_STAT, &shm_ds)) { | |
171 perror("platform_qualify: sysv_shm_and_mmap: no shmctl IPC_STAT\n"); | |
172 return 14; | |
173 } | |
174 if (1 != shm_ds.shm_nattch) { | |
175 fprintf(stderr, | |
176 "platform_qualify: sysv_shm_and_mmap: shm_nattch (%lu) != 1\n", | |
177 (unsigned long) shm_ds.shm_nattch); | |
178 return 15; | |
179 } | |
180 | |
181 if (MAP_FAILED == mmap(shm_addr2, | |
182 SYSVSHM_SIZE, | |
183 PROT_READ | PROT_WRITE, | |
184 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, | |
185 -1, | |
186 0)) { | |
187 perror("platform_qualify: sysv_shm_and_mmap: mmap over all" | |
188 " shm_addr2 failed\n"); | |
189 return 16; | |
190 } | |
191 if (-1 == shmctl(shm_id, IPC_STAT, &shm_ds)) { | |
192 perror("platform_qualify: sysv_shm_and_mmap: no shmctl IPC_STAT\n"); | |
193 return 17; | |
194 } | |
195 if (0 != shm_ds.shm_nattch) { | |
196 fprintf(stderr, | |
197 "platform_qualify: sysv_shm_and_mmap: over-mmap'd shm does not" | |
198 " reduce shm_nattch (%lu)\n", | |
199 (unsigned long) shm_ds.shm_nattch); | |
200 return 18; | |
201 } | |
202 | |
203 return 0; | |
204 } | |
205 | |
206 /* | |
207 * If this code is interrupted or for some reason segvs, then the | |
208 * shared memory is leaked. In that case, use "ipcs -m" to look for | |
209 * segments with key of 0 and bytes of 65536, and then use "ipcrm -m | |
210 * shmid" for the corresponding shmid number to garbage collect the | |
211 * shared memory segment. | |
212 */ | |
213 | |
214 | |
215 /* | |
216 * To build as a standalone, build with -DSysVShmAndMmapHasProblems=main. | |
217 */ | |
218 int NaClPlatformQualifySysVShmAndMmapHasProblems(void) { | |
219 int err_code = 0; | |
220 int shm_id = -1; | |
221 | |
222 shm_id = shmget(IPC_PRIVATE, | |
223 SYSVSHM_SIZE, | |
224 IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); | |
225 DPRINTF(("shmget -> %d\n", shm_id)); | |
226 | |
227 if (-1 == shm_id) { | |
228 perror("platform_qualify: sysv_shm_and_mmap: shmget"); | |
229 return 1; | |
230 } | |
231 | |
232 err_code = NaClPlatformQualifySysVShmId(shm_id); | |
233 | |
234 if (-1 == shmctl(shm_id, IPC_RMID, NULL)) { | |
235 perror("platform_qualify: sysv_shm_and_mmap: shmctl IPC_RMID failed\n"); | |
236 | |
237 /* | |
238 * Set the err code to a release failure only if we do not already have | |
239 * a more interesting failure to report. | |
240 */ | |
241 if (!err_code) err_code = 19; | |
242 } | |
243 | |
244 return err_code; | |
245 } | |
OLD | NEW |