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__) && \
16 #include <irt_syscalls.h>
17 #include <nacl_stat.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"
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
;
73 static const int d_name_shift
= offsetof (dirent
, d_name
) -
74 offsetof (struct nacl_abi_dirent
, nacl_abi_d_name
);
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) \
151 // TODO(bradnelson): Add these as well.
153 // OP(epoll_create1);
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
{
171 RTN_ERRNO_IF(*newfd
< 0);
175 int WRAP(dup2
)(int fd
, int newfd
) NOTHROW
{
176 ERRNO_RTN(ki_dup2(fd
, newfd
));
179 void WRAP(exit
)(int status
) {
183 int WRAP(fstat
)(int fd
, struct nacl_abi_stat
* nacl_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
);
192 int WRAP(getcwd
)(char* buf
, size_t size
) {
193 RTN_ERRNO_IF(ki_getcwd(buf
, size
) == NULL
);
197 int WRAP(getdents
)(int fd
, nacl_abi_dirent
* nacl_buf
, size_t nacl_count
,
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
203 char* buf
= (char*)alloca(nacl_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
;
227 int WRAP(mkdir
)(const char* pathname
, mode_t mode
) {
228 RTN_ERRNO_IF(ki_mkdir(pathname
, mode
) < 0);
232 int WRAP(mmap
)(void** addr
,
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);
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);
259 int WRAP(open_resource
)(const char* file
, int* fd
) {
260 *fd
= ki_open_resource(file
);
261 RTN_ERRNO_IF(*fd
< 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);
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);
278 int WRAP(rmdir
)(const char* pathname
) {
279 RTN_ERRNO_IF(ki_rmdir(pathname
) < 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);
289 int WRAP(select
)(int nfds
,
293 struct timeval
* timeout
,
295 *count
= ki_select(nfds
, readfds
, writefds
, exceptfds
, timeout
);
296 RTN_ERRNO_IF(*count
< 0);
300 int WRAP(stat
)(const char* pathname
, struct nacl_abi_stat
* nacl_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
);
309 int WRAP(lstat
)(const char* pathname
, struct nacl_abi_stat
* nacl_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
);
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
,
330 int rtn
= ki_readlink(pathname
, buf
, count
);
331 RTN_ERRNO_IF(rtn
< 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);
375 int WRAP(accept
)(int sockfd
,
376 struct sockaddr
* addr
,
379 *sd
= ki_accept(sockfd
, addr
, addrlen
);
380 RTN_ERRNO_IF(*sd
< 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
,
405 ERRNO_RTN(ki_getsockopt(sockfd
, level
, optname
, optval
, optlen
));
408 int WRAP(setsockopt
)(int sockfd
,
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);
427 int WRAP(recvfrom
)(int sockfd
,
431 struct sockaddr
* addr
,
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);
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);
447 ssize_t
WRAP(send
)(int sockfd
, const void* buf
, size_t len
, int flags
,
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);
455 ssize_t
WRAP(sendto
)(int sockfd
,
459 const struct sockaddr
* addr
,
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);
468 ssize_t
WRAP(sendmsg
)(int sockfd
,
469 const struct msghdr
* msg
,
472 ssize_t signed_nread
= ki_sendmsg(sockfd
, msg
, flags
);
473 *count
= static_cast<int>(signed_nread
);
474 RTN_ERRNO_IF(signed_nread
< 0);
478 int WRAP(shutdown
)(int sockfd
, int how
) {
479 RTN_ERRNO_IF(ki_shutdown(sockfd
, how
) < 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);
489 int WRAP(socketpair
)(int domain
, int type
, int protocol
, int* sv
) {
490 RTN_ERRNO_IF(ki_socketpair(domain
, type
, protocol
, sv
) < 0);
494 static void assign_real_pointers() {
495 static bool assigned
= false;
497 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR
)
502 #define CHECK_REAL(func) \
504 assign_real_pointers(); \
509 // "real" functions, i.e. the unwrapped original functions.
511 int _real_close(int fd
) {
513 return REAL(close
)(fd
);
516 void _real_exit(int status
) {
518 assign_real_pointers();
522 int _real_fstat(int fd
, struct stat
* buf
) {
523 struct nacl_abi_stat st
;
525 int err
= REAL(fstat
)(fd
, &st
);
531 nacl_stat_to_stat(&st
, buf
);
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
);
540 size_t nacl_offset
= 0;
542 CHECK_REAL(getdents
);
543 int err
= REAL(getdents
)(fd
, (dirent
*)nacl_buf
, count
, &nacl_nread
);
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
;
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
;
565 int _real_lseek(int fd
, off_t offset
, int whence
, off_t
* new_offset
) {
567 return REAL(seek
)(fd
, offset
, whence
, new_offset
);
570 int _real_mkdir(const char* pathname
, mode_t mode
) {
572 return REAL(mkdir
)(pathname
, mode
);
575 int _real_mmap(void** addr
,
582 return REAL(mmap
)(addr
, length
, prot
, flags
, fd
, offset
);
585 int _real_munmap(void* addr
, size_t length
) {
587 return REAL(munmap
)(addr
, length
);
590 int _real_open(const char* pathname
, int oflag
, mode_t mode
, int* newfd
) {
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
) {
602 return REAL(read
)(fd
, buf
, count
, nread
);
605 int _real_rmdir(const char* pathname
) {
607 return REAL(rmdir
)(pathname
);
610 int _real_write(int fd
, const void* buf
, size_t count
, size_t* nwrote
) {
612 return REAL(write
)(fd
, buf
, count
, nwrote
);
615 int _real_getcwd(char* pathname
, size_t len
) {
617 return REAL(getcwd
)(pathname
, len
);
620 static bool s_wrapped
= false;
621 void kernel_wrap_init() {
623 LOG_TRACE("kernel_wrap_init");
624 assign_real_pointers();
625 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP
)
630 void kernel_wrap_uninit() {
632 LOG_TRACE("kernel_wrap_uninit");
633 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL
)
640 #endif // defined(__native_client__) && defined(__GLIBC__)