Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / service_runtime / linux / nacl_host_dir.c
blob55ab3d3ac903885f24e6e63bdad23865759dc954
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Service Runtime. Directory descriptor / Handle abstraction.
35 * Note that we avoid using the thread-specific data / thread local
36 * storage access to the "errno" variable, and instead use the raw
37 * system call return interface of small negative numbers as errors.
40 #include <stdint.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <errno.h>
46 #include <sys/mman.h>
47 #include <unistd.h>
48 #include <dirent.h>
49 #include <linux/types.h>
50 #include <linux/unistd.h>
52 #include "native_client/include/nacl_platform.h"
54 #include "native_client/service_runtime/nacl_config.h"
55 #include "native_client/service_runtime/nacl_log.h"
56 #include "native_client/service_runtime/nacl_host_desc.h"
57 #include "native_client/service_runtime/nacl_host_dir.h"
58 #include "native_client/service_runtime/sel_util.h"
59 #include "native_client/service_runtime/sel_memory.h"
61 #include "native_client/service_runtime/include/sys/dirent.h"
62 #include "native_client/service_runtime/include/sys/errno.h"
63 #include "native_client/service_runtime/include/sys/fcntl.h"
64 #include "native_client/service_runtime/include/sys/mman.h"
65 #include "native_client/service_runtime/include/sys/stat.h"
67 #ifdef _syscall3
68 _syscall3(int, getdents, uint, fd, struct dirent *, dirp, uint, count)
70 int getdents(unsigned int fd, struct dirent* dirp, unsigned int count);
71 #else
72 # include <sys/syscall.h>
73 int getdents(unsigned int fd, struct dirent* dirp, unsigned int count)
75 return syscall(SYS_getdents, fd, dirp, count);
77 #endif
79 int NaClHostDirOpen(struct NaClHostDir *d,
80 char *path)
82 int fd;
84 NaClLog(3, "NaClHostDirOpen(0x%08"PRIxPTR", %s)\n", (uintptr_t) d, path);
85 if (NULL == d) {
86 NaClLog(LOG_FATAL, "NaClHostDirOpen: 'this' is NULL\n");
89 NaClLog(3, "NaClHostDirOpen: invoking open(%s)\n", path);
90 fd = open(path, O_RDONLY);
91 NaClLog(3, "NaClHostDirOpen: got DIR* %d\n", fd);
92 if (-1 == fd) {
93 NaClLog(LOG_ERROR,
94 "NaClHostDirOpen: open returned -1, errno %d\n", errno);
95 return -NaClXlateErrno(errno);
97 d->fd = fd;
98 NaClLog(3, "NaClHostDirOpen: success.\n");
99 return 0;
102 ssize_t NaClHostDirGetdents(struct NaClHostDir *d,
103 void *buf,
104 size_t len)
106 struct dirent *host_direntp = (struct dirent *) buf;
107 struct nacl_abi_dirent *p;
108 int retval;
109 int i;
111 if (NULL == d) {
112 NaClLog(LOG_FATAL, "NaClHostDirGetdents: 'this' is NULL\n");
114 NaClLog(3, "NaClHostDirGetdents(0x%08"PRIxPTR", %u):\n",
115 (uintptr_t) buf, len);
116 retval = getdents(d->fd, host_direntp, len);
117 if (-1 == retval) {
118 return -NaClXlateErrno(errno);
119 } else if (0 == retval) {
120 return 0;
123 NaClLog(3, "NaClHostDirGetdents: returned %d\n", retval);
126 * Scrub inode numbers.
128 i = 0;
129 while (i < retval) {
130 p = (struct nacl_abi_dirent *) (((char *) buf) + i);
132 * Make sure we're not tricked into writing outside of the buffer,
133 * because.... (see below)
135 if ((((char *) &p->nacl_abi_d_ino) + sizeof(p->nacl_abi_d_ino)
136 - (char *) buf) <= retval) {
137 p->nacl_abi_d_ino = 0x6c43614e;
140 * ... we may pick up a user controlled/bogus value by following p
141 * here, so i could make p point to the last byte of the buffer,
142 * and p->* would be beyond the buffer.
144 i += p->nacl_abi_d_reclen;
147 return retval;
150 int NaClHostDirClose(struct NaClHostDir *d)
152 int retval;
154 if (NULL == d) {
155 NaClLog(LOG_FATAL, "NaClHostDirClose: 'this' is NULL\n");
157 NaClLog(3, "NaClHostDirClose(%d)\n", d->fd);
158 retval = close(d->fd);
159 d->fd = -1;
160 return (-1 == retval) ? -NaClXlateErrno(errno) : retval;