Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / kernel_wrap_glibc.cc
blob5be9d25b669b2ab009fe4aa2a25e4452475cfe5e
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
3 // found in the LICENSE file.
5 #include <sys/types.h> // Include something that will define __GLIBC__.
6 #include "nacl_io/kernel_wrap.h" // IRT_EXT is turned on in this header.
8 #if !defined(NACL_IO_IRT_EXT) && defined(__native_client__) && \
9 defined(__GLIBC__)
11 #include <alloca.h>
12 #include <assert.h>
13 #include <dirent.h>
14 #include <errno.h>
15 #include <irt.h>
16 #include <irt_syscalls.h>
17 #include <nacl_stat.h>
18 #include <string.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
22 #include "nacl_io/kernel_intercept.h"
23 #include "nacl_io/kernel_wrap_real.h"
24 #include "nacl_io/log.h"
25 #include "nacl_io/nacl_abi_types.h"
26 #include "nacl_io/osmman.h"
27 #include "nacl_io/ostime.h"
29 namespace {
31 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 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_mode = buf->st_mode;
36 nacl_buf->nacl_abi_st_nlink = buf->st_nlink;
37 nacl_buf->nacl_abi_st_uid = buf->st_uid;
38 nacl_buf->nacl_abi_st_gid = buf->st_gid;
39 nacl_buf->nacl_abi_st_rdev = buf->st_rdev;
40 nacl_buf->nacl_abi_st_size = buf->st_size;
41 nacl_buf->nacl_abi_st_blksize = buf->st_blksize;
42 nacl_buf->nacl_abi_st_blocks = buf->st_blocks;
43 nacl_buf->nacl_abi_st_atime = buf->st_atime;
44 nacl_buf->nacl_abi_st_atimensec = buf->st_atimensec;
45 nacl_buf->nacl_abi_st_mtime = buf->st_mtime;
46 nacl_buf->nacl_abi_st_mtimensec = buf->st_mtimensec;
47 nacl_buf->nacl_abi_st_ctime = buf->st_ctime;
48 nacl_buf->nacl_abi_st_ctimensec = buf->st_ctimensec;
51 void nacl_stat_to_stat(const nacl_abi_stat* nacl_buf, struct stat* buf) {
52 memset(buf, 0, sizeof(struct stat));
53 buf->st_dev = nacl_buf->nacl_abi_st_dev;
54 buf->st_ino = nacl_buf->nacl_abi_st_ino;
55 buf->st_mode = nacl_buf->nacl_abi_st_mode;
56 buf->st_nlink = nacl_buf->nacl_abi_st_nlink;
57 buf->st_uid = nacl_buf->nacl_abi_st_uid;
58 buf->st_gid = nacl_buf->nacl_abi_st_gid;
59 buf->st_rdev = nacl_buf->nacl_abi_st_rdev;
60 buf->st_size = nacl_buf->nacl_abi_st_size;
61 buf->st_blksize = nacl_buf->nacl_abi_st_blksize;
62 buf->st_blocks = nacl_buf->nacl_abi_st_blocks;
63 buf->st_atime = nacl_buf->nacl_abi_st_atime;
64 buf->st_atimensec = nacl_buf->nacl_abi_st_atimensec;
65 buf->st_mtime = nacl_buf->nacl_abi_st_mtime;
66 buf->st_mtimensec = nacl_buf->nacl_abi_st_mtimensec;
67 buf->st_ctime = nacl_buf->nacl_abi_st_ctime;
68 buf->st_ctimensec = nacl_buf->nacl_abi_st_ctimensec;
71 } // namespace
73 static const int d_name_shift = offsetof (dirent, d_name) -
74 offsetof (struct nacl_abi_dirent, nacl_abi_d_name);
76 EXTERN_C_BEGIN
78 // Macro to get the REAL function pointer
79 #define REAL(name) __nacl_irt_##name##_real
81 // Macro to get the WRAP function
82 #define WRAP(name) __nacl_irt_##name##_wrap
84 // Declare REAL function pointer.
85 #define DECLARE_REAL_PTR(name) typeof(__nacl_irt_##name) REAL(name);
87 // Assign the REAL function pointer.
88 #define ASSIGN_REAL_PTR(name) \
89 assert(__nacl_irt_##name != NULL); \
90 REAL(name) = __nacl_irt_##name;
92 // Switch IRT's pointer to the REAL pointer
93 #define USE_REAL(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))REAL(name)
95 // Switch IRT's pointer to the WRAP function
96 #define USE_WRAP(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))WRAP(name)
98 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \
99 OP(chdir); \
100 OP(close); \
101 OP(dup); \
102 OP(dup2); \
103 OP(exit); \
104 OP(fstat); \
105 OP(getcwd); \
106 OP(getdents); \
107 OP(mkdir); \
108 OP(open); \
109 OP(poll); \
110 OP(read); \
111 OP(rmdir); \
112 OP(seek); \
113 OP(stat); \
114 OP(select); \
115 OP(write); \
116 OP(mmap); \
117 OP(munmap); \
118 OP(open_resource); \
120 OP(socket); \
121 OP(accept); \
122 OP(bind); \
123 OP(listen); \
124 OP(connect); \
125 OP(send); \
126 OP(sendmsg); \
127 OP(sendto); \
128 OP(recv); \
129 OP(recvmsg); \
130 OP(recvfrom); \
131 OP(getpeername); \
132 OP(getsockname); \
133 OP(getsockopt); \
134 OP(setsockopt); \
135 OP(socketpair); \
136 OP(shutdown); \
138 OP(chmod); \
139 OP(access); \
140 OP(unlink); \
141 OP(fchdir); \
142 OP(fchmod); \
143 OP(fsync); \
144 OP(fdatasync); \
145 OP(lstat); \
146 OP(link); \
147 OP(rename); \
148 OP(readlink); \
149 OP(utimes);
151 // TODO(bradnelson): Add these as well.
152 // OP(epoll_create);
153 // OP(epoll_create1);
154 // OP(epoll_ctl);
155 // OP(epoll_pwait);
156 // OP(ppoll);
157 // OP(pselect);
159 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR);
161 int WRAP(chdir)(const char* pathname) {
162 ERRNO_RTN(ki_chdir(pathname));
165 int WRAP(close)(int fd) {
166 ERRNO_RTN(ki_close(fd));
169 int WRAP(dup)(int fd, int* newfd) NOTHROW {
170 *newfd = ki_dup(fd);
171 RTN_ERRNO_IF(*newfd < 0);
172 return 0;
175 int WRAP(dup2)(int fd, int newfd) NOTHROW {
176 ERRNO_RTN(ki_dup2(fd, newfd));
179 void WRAP(exit)(int status) {
180 ki_exit(status);
183 int WRAP(fstat)(int fd, struct nacl_abi_stat* nacl_buf) {
184 struct stat buf;
185 memset(&buf, 0, sizeof(struct stat));
186 int res = ki_fstat(fd, &buf);
187 RTN_ERRNO_IF(res < 0);
188 stat_to_nacl_stat(&buf, nacl_buf);
189 return 0;
192 int WRAP(getcwd)(char* buf, size_t size) {
193 RTN_ERRNO_IF(ki_getcwd(buf, size) == NULL);
194 return 0;
197 int WRAP(getdents)(int fd, nacl_abi_dirent* nacl_buf, size_t nacl_count,
198 size_t* nread) {
199 int nacl_offset = 0;
200 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
201 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer
202 // is enough
203 char* buf = (char*)alloca(nacl_count);
204 int offset = 0;
205 int count;
207 count = ki_getdents(fd, (dirent*)buf, nacl_count);
208 RTN_ERRNO_IF(count < 0);
210 while (offset < count) {
211 dirent* d = (dirent*)(buf + offset);
212 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)((char*)nacl_buf + nacl_offset);
213 nacl_d->nacl_abi_d_ino = d->d_ino;
214 nacl_d->nacl_abi_d_off = d->d_off;
215 nacl_d->nacl_abi_d_reclen = d->d_reclen - d_name_shift;
216 size_t d_name_len = d->d_reclen - offsetof(dirent, d_name);
217 memcpy(nacl_d->nacl_abi_d_name, d->d_name, d_name_len);
219 offset += d->d_reclen;
220 nacl_offset += nacl_d->nacl_abi_d_reclen;
223 *nread = nacl_offset;
224 return 0;
227 int WRAP(mkdir)(const char* pathname, mode_t mode) {
228 RTN_ERRNO_IF(ki_mkdir(pathname, mode) < 0);
229 return 0;
232 int WRAP(mmap)(void** addr,
233 size_t length,
234 int prot,
235 int flags,
236 int fd,
237 off_t offset) {
238 if (flags & MAP_ANONYMOUS)
239 return REAL(mmap)(addr, length, prot, flags, fd, offset);
241 *addr = ki_mmap(*addr, length, prot, flags, fd, offset);
242 RTN_ERRNO_IF(*addr == (void*)-1);
243 return 0;
246 int WRAP(munmap)(void* addr, size_t length) {
247 // Always let the real munmap run on the address range. It is not an error if
248 // there are no mapped pages in that range.
249 ki_munmap(addr, length);
250 return REAL(munmap)(addr, length);
253 int WRAP(open)(const char* pathname, int oflag, mode_t mode, int* newfd) {
254 *newfd = ki_open(pathname, oflag, mode);
255 RTN_ERRNO_IF(*newfd < 0);
256 return 0;
259 int WRAP(open_resource)(const char* file, int* fd) {
260 *fd = ki_open_resource(file);
261 RTN_ERRNO_IF(*fd < 0);
262 return 0;
265 int WRAP(poll)(struct pollfd* fds, nfds_t nfds, int timeout, int* count) {
266 *count = ki_poll(fds, nfds, timeout);
267 RTN_ERRNO_IF(*count < 0);
268 return 0;
271 int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) {
272 ssize_t signed_nread = ki_read(fd, buf, count);
273 *nread = static_cast<size_t>(signed_nread);
274 RTN_ERRNO_IF(signed_nread < 0);
275 return 0;
278 int WRAP(rmdir)(const char* pathname) {
279 RTN_ERRNO_IF(ki_rmdir(pathname) < 0);
280 return 0;
283 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) {
284 *new_offset = ki_lseek(fd, offset, whence);
285 RTN_ERRNO_IF(*new_offset < 0);
286 return 0;
289 int WRAP(select)(int nfds,
290 fd_set* readfds,
291 fd_set* writefds,
292 fd_set* exceptfds,
293 struct timeval* timeout,
294 int* count) {
295 *count = ki_select(nfds, readfds, writefds, exceptfds, timeout);
296 RTN_ERRNO_IF(*count < 0);
297 return 0;
300 int WRAP(stat)(const char* pathname, struct nacl_abi_stat* nacl_buf) {
301 struct stat buf;
302 memset(&buf, 0, sizeof(struct stat));
303 int res = ki_stat(pathname, &buf);
304 RTN_ERRNO_IF(res < 0);
305 stat_to_nacl_stat(&buf, nacl_buf);
306 return 0;
309 int WRAP(lstat)(const char* pathname, struct nacl_abi_stat* nacl_buf) {
310 struct stat buf;
311 memset(&buf, 0, sizeof(struct stat));
312 int res = ki_lstat(pathname, &buf);
313 RTN_ERRNO_IF(res < 0);
314 stat_to_nacl_stat(&buf, nacl_buf);
315 return 0;
318 int WRAP(link)(const char* pathname, const char* newpath) {
319 ERRNO_RTN(ki_link(pathname, newpath));
322 int WRAP(rename)(const char* pathname, const char* newpath) {
323 ERRNO_RTN(ki_rename(pathname, newpath));
326 int WRAP(readlink)(const char* pathname,
327 char* buf,
328 size_t count,
329 size_t* nread) {
330 int rtn = ki_readlink(pathname, buf, count);
331 RTN_ERRNO_IF(rtn < 0);
332 *nread = rtn;
333 return 0;
336 int WRAP(utimes)(const char *filename, const struct timeval *times) {
337 ERRNO_RTN(ki_utimes(filename, times));
340 int WRAP(chmod)(const char* pathname, mode_t mode) {
341 ERRNO_RTN(ki_chmod(pathname, mode));
344 int WRAP(access)(const char* pathname, int amode) {
345 ERRNO_RTN(ki_access(pathname, amode));
348 int WRAP(unlink)(const char* pathname) {
349 ERRNO_RTN(ki_unlink(pathname));
352 int WRAP(fchdir)(int fd) {
353 ERRNO_RTN(ki_fchdir(fd));
356 int WRAP(fchmod)(int fd, mode_t mode) {
357 ERRNO_RTN(ki_fchmod(fd, mode));
360 int WRAP(fsync)(int fd) {
361 ERRNO_RTN(ki_fsync(fd));
364 int WRAP(fdatasync)(int fd) {
365 ERRNO_RTN(ki_fdatasync(fd));
368 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) {
369 ssize_t signed_nwrote = ki_write(fd, buf, count);
370 *nwrote = static_cast<size_t>(signed_nwrote);
371 RTN_ERRNO_IF(signed_nwrote < 0);
372 return 0;
375 int WRAP(accept)(int sockfd,
376 struct sockaddr* addr,
377 socklen_t* addrlen,
378 int* sd) {
379 *sd = ki_accept(sockfd, addr, addrlen);
380 RTN_ERRNO_IF(*sd < 0);
381 return 0;
384 int WRAP(bind)(int sockfd, const struct sockaddr* addr, socklen_t addrlen) {
385 ERRNO_RTN(ki_bind(sockfd, addr, addrlen));
388 int WRAP(connect)(int sockfd, const struct sockaddr* addr, socklen_t addrlen) {
389 ERRNO_RTN(ki_connect(sockfd, addr, addrlen));
392 int WRAP(getpeername)(int sockfd, struct sockaddr* addr, socklen_t* addrlen) {
393 ERRNO_RTN(ki_getpeername(sockfd, addr, addrlen));
396 int WRAP(getsockname)(int sockfd, struct sockaddr* addr, socklen_t* addrlen) {
397 ERRNO_RTN(ki_getsockname(sockfd, addr, addrlen));
400 int WRAP(getsockopt)(int sockfd,
401 int level,
402 int optname,
403 void* optval,
404 socklen_t* optlen) {
405 ERRNO_RTN(ki_getsockopt(sockfd, level, optname, optval, optlen));
408 int WRAP(setsockopt)(int sockfd,
409 int level,
410 int optname,
411 const void* optval,
412 socklen_t optlen) {
413 ERRNO_RTN(ki_setsockopt(sockfd, level, optname, optval, optlen));
416 int WRAP(listen)(int sockfd, int backlog) {
417 ERRNO_RTN(ki_listen(sockfd, backlog));
420 int WRAP(recv)(int sockfd, void* buf, size_t len, int flags, int* count) {
421 ssize_t signed_nread = ki_recv(sockfd, buf, len, flags);
422 *count = static_cast<int>(signed_nread);
423 RTN_ERRNO_IF(signed_nread < 0);
424 return 0;
427 int WRAP(recvfrom)(int sockfd,
428 void* buf,
429 size_t len,
430 int flags,
431 struct sockaddr* addr,
432 socklen_t* addrlen,
433 int* count) {
434 ssize_t signed_nread = ki_recvfrom(sockfd, buf, len, flags, addr, addrlen);
435 *count = static_cast<int>(signed_nread);
436 RTN_ERRNO_IF(signed_nread < 0);
437 return 0;
440 int WRAP(recvmsg)(int sockfd, struct msghdr* msg, int flags, int* count) {
441 ssize_t signed_nread = ki_recvmsg(sockfd, msg, flags);
442 *count = static_cast<int>(signed_nread);
443 RTN_ERRNO_IF(signed_nread < 0);
444 return 0;
447 ssize_t WRAP(send)(int sockfd, const void* buf, size_t len, int flags,
448 int* count) {
449 ssize_t signed_nread = ki_send(sockfd, buf, len, flags);
450 *count = static_cast<int>(signed_nread);
451 RTN_ERRNO_IF(signed_nread < 0);
452 return 0;
455 ssize_t WRAP(sendto)(int sockfd,
456 const void* buf,
457 size_t len,
458 int flags,
459 const struct sockaddr* addr,
460 socklen_t addrlen,
461 int* count) {
462 ssize_t signed_nread = ki_sendto(sockfd, buf, len, flags, addr, addrlen);
463 *count = static_cast<int>(signed_nread);
464 RTN_ERRNO_IF(signed_nread < 0);
465 return 0;
468 ssize_t WRAP(sendmsg)(int sockfd,
469 const struct msghdr* msg,
470 int flags,
471 int* count) {
472 ssize_t signed_nread = ki_sendmsg(sockfd, msg, flags);
473 *count = static_cast<int>(signed_nread);
474 RTN_ERRNO_IF(signed_nread < 0);
475 return 0;
478 int WRAP(shutdown)(int sockfd, int how) {
479 RTN_ERRNO_IF(ki_shutdown(sockfd, how) < 0);
480 return 0;
483 int WRAP(socket)(int domain, int type, int protocol, int* sd) {
484 *sd = ki_socket(domain, type, protocol);
485 RTN_ERRNO_IF(*sd < 0);
486 return 0;
489 int WRAP(socketpair)(int domain, int type, int protocol, int* sv) {
490 RTN_ERRNO_IF(ki_socketpair(domain, type, protocol, sv) < 0);
491 return 0;
494 static void assign_real_pointers() {
495 static bool assigned = false;
496 if (!assigned) {
497 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
498 assigned = true;
502 #define CHECK_REAL(func) \
503 if (!REAL(func)) { \
504 assign_real_pointers(); \
505 if (!REAL(func)) \
506 return ENOSYS; \
509 // "real" functions, i.e. the unwrapped original functions.
511 int _real_close(int fd) {
512 CHECK_REAL(close);
513 return REAL(close)(fd);
516 void _real_exit(int status) {
517 if (!REAL(exit))
518 assign_real_pointers();
519 REAL(exit)(status);
522 int _real_fstat(int fd, struct stat* buf) {
523 struct nacl_abi_stat st;
524 CHECK_REAL(fstat);
525 int err = REAL(fstat)(fd, &st);
526 if (err) {
527 errno = err;
528 return -1;
531 nacl_stat_to_stat(&st, buf);
532 return 0;
535 int _real_getdents(int fd, void* buf, size_t count, size_t* nread) {
536 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
537 // See WRAP(getdents) above.
538 char* nacl_buf = (char*)alloca(count);
539 size_t offset = 0;
540 size_t nacl_offset = 0;
541 size_t nacl_nread;
542 CHECK_REAL(getdents);
543 int err = REAL(getdents)(fd, (dirent*)nacl_buf, count, &nacl_nread);
544 if (err)
545 return err;
547 while (nacl_offset < nacl_nread) {
548 dirent* d = (dirent*)((char*)buf + offset);
549 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)(nacl_buf + nacl_offset);
550 d->d_ino = nacl_d->nacl_abi_d_ino;
551 d->d_off = nacl_d->nacl_abi_d_off;
552 d->d_reclen = nacl_d->nacl_abi_d_reclen + d_name_shift;
553 size_t d_name_len =
554 nacl_d->nacl_abi_d_reclen - offsetof(nacl_abi_dirent, nacl_abi_d_name);
555 memcpy(d->d_name, nacl_d->nacl_abi_d_name, d_name_len);
557 offset += d->d_reclen;
558 offset += nacl_d->nacl_abi_d_reclen;
561 *nread = offset;
562 return 0;
565 int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) {
566 CHECK_REAL(seek);
567 return REAL(seek)(fd, offset, whence, new_offset);
570 int _real_mkdir(const char* pathname, mode_t mode) {
571 CHECK_REAL(mkdir);
572 return REAL(mkdir)(pathname, mode);
575 int _real_mmap(void** addr,
576 size_t length,
577 int prot,
578 int flags,
579 int fd,
580 off_t offset) {
581 CHECK_REAL(mmap);
582 return REAL(mmap)(addr, length, prot, flags, fd, offset);
585 int _real_munmap(void* addr, size_t length) {
586 CHECK_REAL(munmap);
587 return REAL(munmap)(addr, length);
590 int _real_open(const char* pathname, int oflag, mode_t mode, int* newfd) {
591 CHECK_REAL(open);
592 return REAL(open)(pathname, oflag, mode, newfd);
595 int _real_open_resource(const char* file, int* fd) {
596 CHECK_REAL(open_resource);
597 return REAL(open_resource)(file, fd);
600 int _real_read(int fd, void* buf, size_t count, size_t* nread) {
601 CHECK_REAL(read);
602 return REAL(read)(fd, buf, count, nread);
605 int _real_rmdir(const char* pathname) {
606 CHECK_REAL(rmdir);
607 return REAL(rmdir)(pathname);
610 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) {
611 CHECK_REAL(write);
612 return REAL(write)(fd, buf, count, nwrote);
615 int _real_getcwd(char* pathname, size_t len) {
616 CHECK_REAL(getcwd);
617 return REAL(getcwd)(pathname, len);
620 static bool s_wrapped = false;
621 void kernel_wrap_init() {
622 if (!s_wrapped) {
623 LOG_TRACE("kernel_wrap_init");
624 assign_real_pointers();
625 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP)
626 s_wrapped = true;
630 void kernel_wrap_uninit() {
631 if (s_wrapped) {
632 LOG_TRACE("kernel_wrap_uninit");
633 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL)
634 s_wrapped = false;
638 EXTERN_C_END
640 #endif // defined(__native_client__) && defined(__GLIBC__)