2 * Copyright (c) 2012 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.
10 #include "native_client/src/include/portability.h"
12 #include "native_client/src/shared/platform/nacl_check.h"
13 #include "native_client/src/shared/platform/nacl_host_desc.h"
14 #include "native_client/src/shared/platform/nacl_log.h"
16 #include "native_client/src/trusted/gio/gio_shm.h"
17 #include "native_client/src/trusted/service_runtime/include/bits/mman.h"
18 #include "native_client/src/trusted/service_runtime/include/sys/stat.h"
19 #include "native_client/src/trusted/service_runtime/nacl_config.h"
20 #include "native_client/src/trusted/service_runtime/sel_util.h"
22 #include "native_client/src/trusted/desc/nacl_desc_base.h"
23 #include "native_client/src/trusted/desc/nacl_desc_effector_trusted_mem.h"
24 #include "native_client/src/trusted/desc/nacl_desc_imc_shm.h"
27 * This code maps in GIO_SHM_WINDOWSIZE bytes at a time for doing
28 * "I/O" from/to the shared memory object. This value must be an
29 * integer multiple of NACL_MAP_PAGESIZE.
31 #define GIO_SHM_WINDOWSIZE (16 * NACL_MAP_PAGESIZE)
34 * Release current window if it exists, then map in window at the
35 * provided new_window_offset. This is akin to filbuf.
37 * Preconditions: 0 == (new_win_offset & (NACL_MAP_PAGESIZE - 1))
38 * new_win_offset < self->shm_sz
40 static int NaClGioShmSetWindow(struct NaClGioShm *self,
41 size_t new_win_offset) {
46 "NaClGioShmSetWindow: new_win_offset 0x%"NACL_PRIxS"\n",
48 if (0 != (new_win_offset & (NACL_MAP_PAGESIZE - 1))) {
50 ("NaClGioShmSetWindow: internal error, requested"
51 " new window offset 0x%"NACL_PRIxS" is not aligned.\n"),
55 if (new_win_offset >= self->shm_sz) {
57 ("NaClGioShmSetWindow: setting window beyond end of shm object"
58 " offset 0x%"NACL_PRIxS", size 0x%"NACL_PRIxS"\n"),
59 new_win_offset, self->shm_sz);
62 if (NULL != self->cur_window) {
63 NaClDescUnmapUnsafe(self->shmp, (void *) self->cur_window,
66 self->cur_window = NULL;
67 self->window_size = 0;
70 * The Map virtual function will NOT pad space beyond the end of the
71 * memory mapping object with zero-filled pages. This is done for
72 * user code in nacl_syscall_common.c (NaClSysMmap), and the Map
73 * virtual function exposes the behavioral inconsistencies wrt
74 * allowing but ignoring mapping an offset beyond the end of file
75 * (linux) versus disallowing the mapping (MapViewOfFileEx).
77 * Here, we know the actual size of the shm object, and can deal
80 actual_len = GIO_SHM_WINDOWSIZE;
81 if (actual_len > self->shm_sz - new_win_offset) {
82 actual_len = self->shm_sz - new_win_offset;
85 (*((struct NaClDescVtbl const *) self->shmp->base.vtbl)->
87 NaClDescEffectorTrustedMem(),
90 NACL_ABI_PROT_READ | NACL_ABI_PROT_WRITE,
92 (nacl_off64_t) new_win_offset);
94 "NaClGioShmSetWindow: Map returned 0x%"NACL_PRIxPTR"\n",
96 if (NaClPtrIsNegErrno(&map_result)) {
100 self->cur_window = (char *) map_result;
101 self->window_size = actual_len;
102 self->window_offset = new_win_offset;
107 static ssize_t NaClGioShmReadOrWrite(struct Gio *vself,
111 struct NaClGioShm *self = (struct NaClGioShm *) vself;
112 size_t new_window_offset;
115 size_t window_remain;
119 ("NaClGioShmReadOrWrite: 0x%"NACL_PRIxPTR","
120 " 0x%"NACL_PRIxPTR", 0x%"NACL_PRIxS", %d\n"),
127 NaClLog(4, "NaClGioShmReadOrWrite: count 0x%"NACL_PRIxS"\n", count);
128 if (self->io_offset >= self->shm_sz) {
131 NaClLog(4, " cur_window 0x%"NACL_PRIxPTR"\n",
132 (uintptr_t) self->cur_window);
133 NaClLog(4, " io_offset 0x%"NACL_PRIxS"\n", self->io_offset);
134 NaClLog(4, "window_offset 0x%"NACL_PRIxS"\n", self->window_offset);
135 if (NULL == self->cur_window
136 || self->io_offset < self->window_offset
137 || self->window_offset + self->window_size <= self->io_offset) {
139 * io_offset is outside the window. move the window so that
142 NaClLog(4, "Seek required\n");
144 new_window_offset = (self->io_offset
145 & (~(((size_t) NACL_MAP_PAGESIZE) - 1)));
146 NaClLog(4, "new_window_offset 0x%"NACL_PRIxS"\n", new_window_offset);
147 CHECK(0 == (new_window_offset &
148 (((size_t) NACL_MAP_PAGESIZE)-1)));
149 if (!NaClGioShmSetWindow(self, new_window_offset)) {
157 NaClLog(4, "no seek required\n");
159 NaClLog(4, " cur_window 0x%"NACL_PRIxPTR"\n",
160 (uintptr_t) self->cur_window);
161 NaClLog(4, " io_offset 0x%"NACL_PRIxS"\n", self->io_offset);
162 NaClLog(4, "window_offset 0x%"NACL_PRIxS"\n", self->window_offset);
164 CHECK(self->window_offset <= self->io_offset);
165 CHECK(self->io_offset < self->window_offset + self->window_size);
168 window_end = self->window_offset + self->window_size;
169 if (window_end > self->shm_sz) {
170 window_end = self->shm_sz;
172 window_remain = window_end - self->io_offset;
174 NaClLog(4, "remaining in window 0x%"NACL_PRIxS"\n", window_remain);
176 CHECK(window_remain <= GIO_SHM_WINDOWSIZE);
178 if (transfer > window_remain) {
179 transfer = window_remain;
182 NaClLog(4, "transfer 0x%"NACL_PRIxS"\n", transfer);
186 ("about to \"write\" memcpy(0x%"NACL_PRIxPTR", "
187 " 0x%"NACL_PRIxPTR", 0x%"NACL_PRIxS" bytes)\n"),
188 (uintptr_t) (self->cur_window
189 + (self->io_offset - self->window_offset)),
193 memcpy(self->cur_window + (self->io_offset - self->window_offset),
198 ("about to \"read\" memcpy(0x%"NACL_PRIxPTR", "
199 " 0x%"NACL_PRIxPTR", 0x%"NACL_PRIxS" bytes)\n"),
201 (uintptr_t) (self->cur_window
202 + (self->io_offset - self->window_offset)),
206 self->cur_window + (self->io_offset - self->window_offset),
209 self->io_offset += transfer;
212 buf = (void *)((uintptr_t) buf + transfer);
219 static ssize_t NaClGioShmRead(struct Gio *vself,
222 return NaClGioShmReadOrWrite(vself, buf, count, 0);
225 static ssize_t NaClGioShmWrite(struct Gio *vself,
228 return NaClGioShmReadOrWrite(vself, (void *) buf, count, 1);
231 static off_t NaClGioShmSeek(struct Gio *vself,
234 struct NaClGioShm *self = (struct NaClGioShm *) vself;
235 size_t new_pos = (size_t) -1;
237 NaClLog(4, "NaClGioShmSeek(0x%"NACL_PRIxPTR", %ld (0x%lx), %d)\n",
238 (uintptr_t) vself, (long) offset, (long) offset, whence);
240 * Note that if sizeof(new_pos) < sizeof(offset), we are dropping
241 * high-order bits and we do not detect this. However, the check
242 * after the switch keeps the values somewhat sane: we will never
243 * set the I/O offset to be outside the range [0, self->shm_sz].
247 new_pos = (size_t) offset;
250 new_pos = self->io_offset + offset;
253 new_pos = self->shm_sz + offset;
256 /* allow equality, so setting to the end of file is okay */
257 if (self->shm_sz < new_pos) {
258 NaClLog(4, " invalid offset\n");
262 NaClLog(4, " setting to %ld (0x%lx)\n", (long) new_pos, (long) new_pos);
263 /* sizeof(off_t) >= sizeof(size_t) */
264 self->io_offset = new_pos;
265 return (off_t) self->io_offset;
268 static int NaClGioShmFlush(struct Gio *vself) {
269 UNREFERENCED_PARAMETER(vself);
273 static int NaClGioShmClose(struct Gio *vself) {
274 struct NaClGioShm *self = (struct NaClGioShm *) vself;
276 if (NULL != self->cur_window) {
277 NaClDescUnmapUnsafe(self->shmp, (void *) self->cur_window,
280 self->cur_window = NULL;
282 if (NULL == self->shmp) {
283 NaClLog(LOG_ERROR, "NaClGioShmClose: double close detected\n");
288 NaClDescUnref(self->shmp);
289 self->shmp = NULL; /* double close will fault */
293 static void NaClGioShmDtor(struct Gio *vself) {
294 struct NaClGioShm *self = (struct NaClGioShm *) vself;
297 * Users of Gio objects are expected to Close then Dtor, but Dtor
298 * should cleanup regardless.
300 if (NULL != self->shmp) {
301 if (-1 == (*vself->vtbl->Close)(vself)) {
302 NaClLog(LOG_ERROR, "NaClGioShmDtor: auto Close failed!\n");
307 self->base.vtbl = NULL;
310 const struct GioVtbl kNaClGioShmVtbl = {
320 static int NaClGioShmCtorIntern(struct NaClGioShm *self,
321 struct NaClDesc *shmp,
323 struct nacl_abi_stat stbuf;
327 self->base.vtbl = NULL;
330 self->cur_window = NULL;
332 if (0 != (vfret = (*((struct NaClDescVtbl const *) shmp->base.vtbl)->
333 Fstat)(shmp, &stbuf))) {
334 NaClLog(1, "NaClGioShmCtorIntern: Fstat virtual function returned %d\n",
339 * nacl_abi_off_t is signed 32-bit quantity, but we don't want to
340 * hardwire in that knowledge here.
342 * size_t is unsigned, and may be 32-bits or 64-bits, depending on
343 * the underlying host OS.
345 * we want to ensure that the shm's size, as reported by the desc
346 * abstraction and thus is in nacl_abi_off_t, is at least that
347 * claimed by the ctor argument. so, if (as Integers)
349 * stbuf.nacl_abi_st_size < shm_size
351 * holds, this is an error. however, the value-preserving cast rule
354 * Note that for signed sizes (ssize_t), the kernel ABI generally
355 * only reserve -1 for error, and asking for an I/O operation via a
356 * size_t that would succeed but yield a ssize_t return value that
357 * is negative is okay, since -1 is never valid as an I/O size on a
358 * von Neuman machine (except for a writev where the iov entries
359 * overlap): there just isn't that much data to read/write, when the
360 * instructions also take up space in the process address space.
361 * Whether requiring the programmer to detect this corner case is
362 * advisable is a different argument -- similar to negative ssize_t
363 * sizes, the syscall can just succeed with a partial transfer to
364 * avoid returning -1 on a success, just as we could avoid returning
365 * negative values; in practice, we do the latter, since we often
366 * see code written that tests for syscall error by comparing the
367 * return value to see if it is less than zero, rather than if it is
370 if (stbuf.nacl_abi_st_size < 0) {
371 NaClLog(LOG_ERROR, "NaClGioShmCtorIntern: actual shm size negative\n");
374 if (stbuf.nacl_abi_st_size <= (nacl_abi_off_t) SIZE_T_MAX
375 && (size_t) stbuf.nacl_abi_st_size < shm_size) {
377 "NaClGioShmCtorIntern: claimed shm file size greater than"
378 " actual shm segment size\n");
381 if (OFF_T_MAX < SIZE_T_MAX && (size_t) OFF_T_MAX < shm_size) {
383 ("NaClGioShmCtorIntern: claimed shm file size greater than"
384 " off_t max value, %"NACL_PRId64"\n"),
385 (int64_t) OFF_T_MAX);
389 self->shmp = NaClDescRef(shmp);
392 self->shm_sz = shm_size;
393 self->window_offset = 0;
395 self->base.vtbl = &kNaClGioShmVtbl;
397 if (!NaClGioShmSetWindow(self, 0)) {
399 ("NaClGioShmCtorIntern: initial seek to beginning failed\n"));
400 NaClDescUnref(self->shmp);
403 self->base.vtbl = NULL;
412 int NaClGioShmCtor(struct NaClGioShm *self,
413 struct NaClDesc *shmp,
418 CHECK(shm_size == NaClRoundAllocPage(shm_size));
420 rv = NaClGioShmCtorIntern(self, shmp, shm_size);
425 int NaClGioShmAllocCtor(struct NaClGioShm *self,
427 struct NaClDescImcShm *shmp;
430 CHECK(shm_size == NaClRoundAllocPage(shm_size));
432 shmp = malloc(sizeof *shmp);
436 if (!NaClDescImcShmAllocCtor(shmp, shm_size, /* executable= */ 0)) {
441 rv = NaClGioShmCtorIntern(self, (struct NaClDesc *) shmp, shm_size);
442 NaClDescUnref((struct NaClDesc *) shmp);