Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc

Issue 271513002: [NaCl SDK] nacl_io: Make IRT intercepts (and their test code) more robust. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <sys/types.h> // Include something that will define __GLIBC__. 5 #include <sys/types.h> // Include something that will define __GLIBC__.
6 6
7 // The entire file is wrapped in this #if. We do this so this .cc file can be 7 // The entire file is wrapped in this #if. We do this so this .cc file can be
8 // compiled, even on a non-glibc build. 8 // compiled, even on a non-glibc build.
9 #if defined(__native_client__) && defined(__GLIBC__) 9 #if defined(__native_client__) && defined(__GLIBC__)
10 10
11 #include "nacl_io/kernel_wrap.h" 11 #include "nacl_io/kernel_wrap.h"
12 12
13 #include <alloca.h> 13 #include <alloca.h>
14 #include <assert.h> 14 #include <assert.h>
15 #include <dirent.h> 15 #include <dirent.h>
16 #include <errno.h> 16 #include <errno.h>
17 #include <irt.h> 17 #include <irt.h>
18 #include <irt_syscalls.h> 18 #include <irt_syscalls.h>
19 #include <nacl_stat.h> 19 #include <nacl_stat.h>
20 #include <string.h> 20 #include <string.h>
21 #include <sys/stat.h> 21 #include <sys/stat.h>
22 #include <sys/time.h> 22 #include <sys/time.h>
23 23
24 #include "nacl_io/kernel_intercept.h" 24 #include "nacl_io/kernel_intercept.h"
25 #include "nacl_io/kernel_wrap_real.h" 25 #include "nacl_io/kernel_wrap_real.h"
26 #include "nacl_io/log.h"
26 #include "nacl_io/osmman.h" 27 #include "nacl_io/osmman.h"
27 28
28 29
29 namespace { 30 namespace {
30 31
31 void stat_to_nacl_stat(const struct stat* buf, nacl_abi_stat* nacl_buf) { 32 void stat_to_nacl_stat(const struct stat* buf, nacl_abi_stat* nacl_buf) {
32 memset(nacl_buf, 0, sizeof(struct nacl_abi_stat)); 33 memset(nacl_buf, 0, sizeof(struct nacl_abi_stat));
33 nacl_buf->nacl_abi_st_dev = buf->st_dev; 34 nacl_buf->nacl_abi_st_dev = buf->st_dev;
34 nacl_buf->nacl_abi_st_ino = buf->st_ino; 35 nacl_buf->nacl_abi_st_ino = buf->st_ino;
35 nacl_buf->nacl_abi_st_mode = buf->st_mode; 36 nacl_buf->nacl_abi_st_mode = buf->st_mode;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 REAL(name) = __nacl_irt_##name; 116 REAL(name) = __nacl_irt_##name;
116 117
117 // Switch IRT's pointer to the REAL pointer 118 // Switch IRT's pointer to the REAL pointer
118 #define USE_REAL(name) \ 119 #define USE_REAL(name) \
119 __nacl_irt_##name = (typeof(__nacl_irt_##name)) REAL(name) 120 __nacl_irt_##name = (typeof(__nacl_irt_##name)) REAL(name)
120 121
121 // Switch IRT's pointer to the WRAP function 122 // Switch IRT's pointer to the WRAP function
122 #define USE_WRAP(name) \ 123 #define USE_WRAP(name) \
123 __nacl_irt_##name = (typeof(__nacl_irt_##name)) WRAP(name) 124 __nacl_irt_##name = (typeof(__nacl_irt_##name)) WRAP(name)
124 125
125
126 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \ 126 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \
127 OP(chdir); \ 127 OP(chdir); \
128 OP(close); \ 128 OP(close); \
129 OP(dup); \ 129 OP(dup); \
130 OP(dup2); \ 130 OP(dup2); \
131 OP(exit); \ 131 OP(exit); \
132 OP(fstat); \ 132 OP(fstat); \
133 OP(getcwd); \ 133 OP(getcwd); \
134 OP(getdents); \ 134 OP(getdents); \
135 OP(mkdir); \ 135 OP(mkdir); \
136 OP(open); \ 136 OP(open); \
137 OP(poll);\ 137 OP(poll);\
138 OP(read); \ 138 OP(read); \
139 OP(rmdir); \ 139 OP(rmdir); \
140 OP(seek); \ 140 OP(seek); \
141 OP(stat); \ 141 OP(stat); \
142 OP(select); \ 142 OP(select); \
143 OP(write); \ 143 OP(write); \
144 OP(mmap); \ 144 OP(mmap); \
145 OP(munmap); \ 145 OP(munmap); \
146 OP(open_resource); 146 OP(open_resource);
147 147
148 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); 148 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR);
149 149
150 int WRAP(chdir)(const char* pathname) { 150 int WRAP(chdir)(const char* pathname) {
151 return (ki_chdir(pathname)) ? errno : 0; 151 RTN_ERRNO_IF(ki_chdir(pathname) < 0);
152 return 0;
152 } 153 }
153 154
154 int WRAP(close)(int fd) { 155 int WRAP(close)(int fd) {
155 return (ki_close(fd) < 0) ? errno : 0; 156 RTN_ERRNO_IF(ki_close(fd) < 0);
157 return 0;
156 } 158 }
157 159
158 int WRAP(dup)(int fd, int* newfd) NOTHROW { 160 int WRAP(dup)(int fd, int* newfd) NOTHROW {
159 *newfd = ki_dup(fd); 161 *newfd = ki_dup(fd);
160 return (*newfd < 0) ? errno : 0; 162 RTN_ERRNO_IF(*newfd < 0);
163 return 0;
161 } 164 }
162 165
163 int WRAP(dup2)(int fd, int newfd) NOTHROW { 166 int WRAP(dup2)(int fd, int newfd) NOTHROW {
164 return (ki_dup2(fd, newfd) < 0) ? errno : 0; 167 RTN_ERRNO_IF(ki_dup2(fd, newfd) < 0);
168 return 0;
165 } 169 }
166 170
167 void WRAP(exit)(int status) { 171 void WRAP(exit)(int status) {
168 ki_exit(status); 172 ki_exit(status);
169 } 173 }
170 174
171 int WRAP(fstat)(int fd, struct nacl_abi_stat *nacl_buf) { 175 int WRAP(fstat)(int fd, struct nacl_abi_stat *nacl_buf) {
172 struct stat buf; 176 struct stat buf;
173 memset(&buf, 0, sizeof(struct stat)); 177 memset(&buf, 0, sizeof(struct stat));
174 int res = ki_fstat(fd, &buf); 178 int res = ki_fstat(fd, &buf);
175 if (res < 0) 179 RTN_ERRNO_IF(res < 0);
176 return errno;
177 stat_to_nacl_stat(&buf, nacl_buf); 180 stat_to_nacl_stat(&buf, nacl_buf);
178 return 0; 181 return 0;
179 } 182 }
180 183
181 int WRAP(getcwd)(char* buf, size_t size) { 184 int WRAP(getcwd)(char* buf, size_t size) {
182 if (ki_getcwd(buf, size) == NULL) 185 RTN_ERRNO_IF(ki_getcwd(buf, size) == NULL);
183 return errno;
184 return 0; 186 return 0;
185 } 187 }
186 188
187 int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t *nread) { 189 int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t *nread) {
188 int nacl_offset = 0; 190 int nacl_offset = 0;
189 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s). 191 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
190 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer 192 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer
191 // is enough 193 // is enough
192 char* buf = (char*)alloca(nacl_count); 194 char* buf = (char*)alloca(nacl_count);
193 int offset = 0; 195 int offset = 0;
194 int count; 196 int count;
195 197
196 count = ki_getdents(fd, buf, nacl_count); 198 count = ki_getdents(fd, buf, nacl_count);
197 if (count < 0) 199 RTN_ERRNO_IF(count < 0);
198 return errno;
199 200
200 while (offset < count) { 201 while (offset < count) {
201 dirent* d = (dirent*)(buf + offset); 202 dirent* d = (dirent*)(buf + offset);
202 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)((char*)nacl_buf + nacl_offset); 203 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)((char*)nacl_buf + nacl_offset);
203 nacl_d->nacl_abi_d_ino = d->d_ino; 204 nacl_d->nacl_abi_d_ino = d->d_ino;
204 nacl_d->nacl_abi_d_off = d->d_off; 205 nacl_d->nacl_abi_d_off = d->d_off;
205 nacl_d->nacl_abi_d_reclen = d->d_reclen - d_name_shift; 206 nacl_d->nacl_abi_d_reclen = d->d_reclen - d_name_shift;
206 size_t d_name_len = d->d_reclen - offsetof(dirent, d_name); 207 size_t d_name_len = d->d_reclen - offsetof(dirent, d_name);
207 memcpy(nacl_d->nacl_abi_d_name, d->d_name, d_name_len); 208 memcpy(nacl_d->nacl_abi_d_name, d->d_name, d_name_len);
208 209
209 offset += d->d_reclen; 210 offset += d->d_reclen;
210 nacl_offset += nacl_d->nacl_abi_d_reclen; 211 nacl_offset += nacl_d->nacl_abi_d_reclen;
211 } 212 }
212 213
213 *nread = nacl_offset; 214 *nread = nacl_offset;
214 return 0; 215 return 0;
215 } 216 }
216 217
217 int WRAP(mkdir)(const char* pathname, mode_t mode) { 218 int WRAP(mkdir)(const char* pathname, mode_t mode) {
218 return (ki_mkdir(pathname, mode)) ? errno : 0; 219 RTN_ERRNO_IF(ki_mkdir(pathname, mode) < 0);
220 return 0;
219 } 221 }
220 222
221 int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd, 223 int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd,
222 off_t offset) { 224 off_t offset) {
223 if (flags & MAP_ANONYMOUS) 225 if (flags & MAP_ANONYMOUS)
224 return REAL(mmap)(addr, length, prot, flags, fd, offset); 226 return REAL(mmap)(addr, length, prot, flags, fd, offset);
225 227
226 *addr = ki_mmap(*addr, length, prot, flags, fd, offset); 228 *addr = ki_mmap(*addr, length, prot, flags, fd, offset);
227 return *addr == (void*)-1 ? errno : 0; 229 RTN_ERRNO_IF(*addr == (void*)-1);
230 return 0;
228 } 231 }
229 232
230 int WRAP(munmap)(void* addr, size_t length) { 233 int WRAP(munmap)(void* addr, size_t length) {
231 // Always let the real munmap run on the address range. It is not an error if 234 // Always let the real munmap run on the address range. It is not an error if
232 // there are no mapped pages in that range. 235 // there are no mapped pages in that range.
233 ki_munmap(addr, length); 236 ki_munmap(addr, length);
234 return REAL(munmap)(addr, length); 237 return REAL(munmap)(addr, length);
235 } 238 }
236 239
237 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { 240 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) {
238 *newfd = ki_open(pathname, oflag); 241 *newfd = ki_open(pathname, oflag);
239 return (*newfd < 0) ? errno : 0; 242 RTN_ERRNO_IF(*newfd < 0);
243 return 0;
240 } 244 }
241 245
242 int WRAP(open_resource)(const char* file, int* fd) { 246 int WRAP(open_resource)(const char* file, int* fd) {
243 *fd = ki_open_resource(file); 247 *fd = ki_open_resource(file);
244 return (*fd < 0) ? errno : 0; 248 RTN_ERRNO_IF(*fd < 0);
249 return 0;
245 } 250 }
246 251
247 int WRAP(poll)(struct pollfd *fds, nfds_t nfds, int timeout, int* count) { 252 int WRAP(poll)(struct pollfd *fds, nfds_t nfds, int timeout, int* count) {
248 *count = ki_poll(fds, nfds, timeout); 253 *count = ki_poll(fds, nfds, timeout);
249 return (*count < 0) ? errno : 0; 254 RTN_ERRNO_IF(*count < 0);
250 255 return 0;
251 } 256 }
252 257
253 int WRAP(read)(int fd, void *buf, size_t count, size_t *nread) { 258 int WRAP(read)(int fd, void *buf, size_t count, size_t *nread) {
254 ssize_t signed_nread = ki_read(fd, buf, count); 259 ssize_t signed_nread = ki_read(fd, buf, count);
255 *nread = static_cast<size_t>(signed_nread); 260 *nread = static_cast<size_t>(signed_nread);
256 return (signed_nread < 0) ? errno : 0; 261 RTN_ERRNO_IF(signed_nread < 0);
262 return 0;
257 } 263 }
258 264
259 int WRAP(rmdir)(const char* pathname) { 265 int WRAP(rmdir)(const char* pathname) {
260 return (ki_rmdir(pathname) < 0) ? errno : 0; 266 RTN_ERRNO_IF(ki_rmdir(pathname) < 0);
267 return 0;
261 } 268 }
262 269
263 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) { 270 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) {
264 *new_offset = ki_lseek(fd, offset, whence); 271 *new_offset = ki_lseek(fd, offset, whence);
265 return (*new_offset < 0) ? errno : 0; 272 RTN_ERRNO_IF(*new_offset < 0);
273 return 0;
266 } 274 }
267 275
268 int WRAP(select)(int nfds, fd_set* readfds, fd_set* writefds, 276 int WRAP(select)(int nfds, fd_set* readfds, fd_set* writefds,
269 fd_set* exceptfds, struct timeval* timeout, int* count) { 277 fd_set* exceptfds, struct timeval* timeout, int* count) {
270 *count = ki_select(nfds, readfds, writefds, exceptfds, timeout); 278 *count = ki_select(nfds, readfds, writefds, exceptfds, timeout);
271 return (*count < 0) ? errno : 0; 279 RTN_ERRNO_IF(*count < 0);
280 return 0;
272 } 281 }
273 282
274 int WRAP(stat)(const char *pathname, struct nacl_abi_stat *nacl_buf) { 283 int WRAP(stat)(const char *pathname, struct nacl_abi_stat *nacl_buf) {
275 struct stat buf; 284 struct stat buf;
276 memset(&buf, 0, sizeof(struct stat)); 285 memset(&buf, 0, sizeof(struct stat));
277 int res = ki_stat(pathname, &buf); 286 int res = ki_stat(pathname, &buf);
278 if (res < 0) 287 RTN_ERRNO_IF(res < 0);
279 return errno;
280 stat_to_nacl_stat(&buf, nacl_buf); 288 stat_to_nacl_stat(&buf, nacl_buf);
281 return 0; 289 return 0;
282 } 290 }
283 291
284 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { 292 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) {
285 ssize_t signed_nwrote = ki_write(fd, buf, count); 293 ssize_t signed_nwrote = ki_write(fd, buf, count);
286 *nwrote = static_cast<size_t>(signed_nwrote); 294 *nwrote = static_cast<size_t>(signed_nwrote);
287 return (signed_nwrote < 0) ? errno : 0; 295 RTN_ERRNO_IF(signed_nwrote < 0);
296 return 0;
288 } 297 }
289 298
290 static void assign_real_pointers() { 299 static void assign_real_pointers() {
291 static bool assigned = false; 300 static bool assigned = false;
292 if (!assigned) { 301 if (!assigned) {
293 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) 302 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
294 assigned = true; 303 assigned = true;
295 } 304 }
296 } 305 }
297 306
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 } 405 }
397 406
398 int _real_write(int fd, const void *buf, size_t count, size_t *nwrote) { 407 int _real_write(int fd, const void *buf, size_t count, size_t *nwrote) {
399 CHECK_REAL(write); 408 CHECK_REAL(write);
400 return REAL(write)(fd, buf, count, nwrote); 409 return REAL(write)(fd, buf, count, nwrote);
401 } 410 }
402 411
403 static bool s_wrapped = false; 412 static bool s_wrapped = false;
404 void kernel_wrap_init() { 413 void kernel_wrap_init() {
405 if (!s_wrapped) { 414 if (!s_wrapped) {
415 LOG_TRACE("kernel_wrap_init");
406 assign_real_pointers(); 416 assign_real_pointers();
407 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP) 417 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP)
408 s_wrapped = true; 418 s_wrapped = true;
409 } 419 }
410 } 420 }
411 421
412 void kernel_wrap_uninit() { 422 void kernel_wrap_uninit() {
413 if (s_wrapped) { 423 if (s_wrapped) {
424 LOG_TRACE("kernel_wrap_uninit");
414 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) 425 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL)
415 s_wrapped = false; 426 s_wrapped = false;
416 } 427 }
417 } 428 }
418 429
419 EXTERN_C_END 430 EXTERN_C_END
420 431
421 #endif // defined(__native_client__) && defined(__GLIBC__) 432 #endif // defined(__native_client__) && defined(__GLIBC__)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698