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 __BIONIC__.
7 #include "nacl_io/kernel_wrap.h" // IRT_EXT is turned on in this header.
9 // The entire file is wrapped in this #if. We do this so this .cc file can be
10 // compiled, even on a non-bionic build.
12 #if !defined(NACL_IO_IRT_EXT) && defined(__native_client__) && \
19 #include <irt_syscalls.h>
24 #include "nacl_io/kernel_intercept.h"
25 #include "nacl_io/kernel_wrap_real.h"
26 #include "nacl_io/nacl_abi_types.h"
27 #include "nacl_io/osmman.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_mtime
= buf
->st_mtime
;
45 nacl_buf
->nacl_abi_st_ctime
= buf
->st_ctime
;
48 void nacl_stat_to_stat(const nacl_abi_stat
* nacl_buf
, struct stat
* buf
) {
49 memset(buf
, 0, sizeof(struct stat
));
50 buf
->st_dev
= nacl_buf
->nacl_abi_st_dev
;
51 buf
->st_ino
= nacl_buf
->nacl_abi_st_ino
;
52 buf
->st_mode
= nacl_buf
->nacl_abi_st_mode
;
53 buf
->st_nlink
= nacl_buf
->nacl_abi_st_nlink
;
54 buf
->st_uid
= nacl_buf
->nacl_abi_st_uid
;
55 buf
->st_gid
= nacl_buf
->nacl_abi_st_gid
;
56 buf
->st_rdev
= nacl_buf
->nacl_abi_st_rdev
;
57 buf
->st_size
= nacl_buf
->nacl_abi_st_size
;
58 buf
->st_blksize
= nacl_buf
->nacl_abi_st_blksize
;
59 buf
->st_blocks
= nacl_buf
->nacl_abi_st_blocks
;
60 buf
->st_atime
= nacl_buf
->nacl_abi_st_atime
;
61 buf
->st_mtime
= nacl_buf
->nacl_abi_st_mtime
;
62 buf
->st_ctime
= nacl_buf
->nacl_abi_st_ctime
;
67 static const int d_name_shift
= offsetof (dirent
, d_name
) -
68 offsetof (struct nacl_abi_dirent
, nacl_abi_d_name
);
72 // Macro to get the REAL function pointer
73 #define REAL(name) __nacl_irt_##name##_real
75 // Macro to get the WRAP function
76 #define WRAP(name) __nacl_irt_##name##_wrap
78 // Declare REAL function pointer.
79 #define DECLARE_REAL_PTR(name) typeof(__nacl_irt_##name) REAL(name);
81 // Assign the REAL function pointer.
82 #define ASSIGN_REAL_PTR(name) REAL(name) = __nacl_irt_##name;
84 // Switch IRT's pointer to the REAL pointer
85 #define USE_REAL(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))REAL(name)
87 // Switch IRT's pointer to the WRAP function
88 #define USE_WRAP(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))WRAP(name)
90 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \
119 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR
);
121 int WRAP(chdir
)(const char* pathname
) {
122 ERRNO_RTN(ki_chdir(pathname
));
125 int WRAP(close
)(int fd
) {
126 ERRNO_RTN(ki_close(fd
));
129 int WRAP(dup
)(int fd
, int* newfd
) NOTHROW
{
134 int WRAP(dup2
)(int fd
, int newfd
) NOTHROW
{
135 ERRNO_RTN(ki_dup2(fd
, newfd
));
138 void WRAP(exit
)(int status
) {
142 int WRAP(fchdir
)(int fd
) NOTHROW
{
143 ERRNO_RTN(ki_fchdir(fd
));
146 int WRAP(fchmod
)(int fd
, mode_t mode
) NOTHROW
{
147 ERRNO_RTN(ki_fchmod(fd
, mode
));
150 int WRAP(fdatasync
)(int fd
) NOTHROW
{
151 ERRNO_RTN(ki_fdatasync(fd
));
154 int WRAP(fstat
)(int fd
, struct nacl_abi_stat
* nacl_buf
) {
156 memset(&buf
, 0, sizeof(struct stat
));
157 int res
= ki_fstat(fd
, &buf
);
158 RTN_ERRNO_IF(res
< 0);
159 stat_to_nacl_stat(&buf
, nacl_buf
);
163 int WRAP(fsync
)(int fd
) NOTHROW
{
164 ERRNO_RTN(ki_fsync(fd
));
167 int WRAP(getcwd
)(char* buf
, size_t size
) {
168 RTN_ERRNO_IF(ki_getcwd(buf
, size
) == NULL
);
172 int WRAP(getdents
)(int fd
, dirent
* nacl_buf
, size_t nacl_count
, size_t* nread
) {
174 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
175 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer
177 char* buf
= (char*)alloca(nacl_count
);
181 count
= ki_getdents(fd
, (dirent
*)buf
, nacl_count
);
182 RTN_ERRNO_IF(count
< 0);
184 while (offset
< count
) {
185 dirent
* d
= (dirent
*)(buf
+ offset
);
186 nacl_abi_dirent
* nacl_d
= (nacl_abi_dirent
*)((char*)nacl_buf
+ nacl_offset
);
187 nacl_d
->nacl_abi_d_ino
= d
->d_ino
;
188 nacl_d
->nacl_abi_d_off
= d
->d_off
;
189 nacl_d
->nacl_abi_d_reclen
= d
->d_reclen
- d_name_shift
;
190 size_t d_name_len
= d
->d_reclen
- offsetof(dirent
, d_name
);
191 memcpy(nacl_d
->nacl_abi_d_name
, d
->d_name
, d_name_len
);
193 offset
+= d
->d_reclen
;
194 nacl_offset
+= nacl_d
->nacl_abi_d_reclen
;
197 *nread
= nacl_offset
;
201 int WRAP(isatty
)(int fd
, int* result
) {
202 *result
= ki_isatty(fd
);
203 RTN_ERRNO_IF(*result
== 0);
207 int WRAP(lstat
)(const char* path
, struct nacl_abi_stat
* nacl_buf
) {
209 memset(&buf
, 0, sizeof(struct stat
));
210 int res
= ki_lstat(path
, &buf
);
211 RTN_ERRNO_IF(res
< 0);
212 stat_to_nacl_stat(&buf
, nacl_buf
);
216 int WRAP(mkdir
)(const char* pathname
, mode_t mode
) {
217 ERRNO_RTN(ki_mkdir(pathname
, mode
));
220 int WRAP(mmap
)(void** addr
,
226 if (flags
& MAP_ANONYMOUS
)
227 return REAL(mmap
)(addr
, length
, prot
, flags
, fd
, offset
);
229 *addr
= ki_mmap(*addr
, length
, prot
, flags
, fd
, offset
);
230 RTN_ERRNO_IF(*addr
== (void*)-1)
234 int WRAP(munmap
)(void* addr
, size_t length
) {
235 // Always let the real munmap run on the address range. It is not an error if
236 // there are no mapped pages in that range.
237 ki_munmap(addr
, length
);
238 return REAL(munmap
)(addr
, length
);
241 int WRAP(open
)(const char* pathname
, int oflag
, mode_t mode
, int* newfd
) {
242 *newfd
= ki_open(pathname
, oflag
, mode
);
246 int WRAP(open_resource
)(const char* file
, int* fd
) {
247 *fd
= ki_open_resource(file
);
251 int WRAP(poll
)(struct pollfd
* fds
, nfds_t nfds
, int timeout
, int* count
) {
252 *count
= ki_poll(fds
, nfds
, timeout
);
256 int WRAP(read
)(int fd
, void* buf
, size_t count
, size_t* nread
) {
257 ssize_t signed_nread
= ki_read(fd
, buf
, count
);
258 *nread
= static_cast<size_t>(signed_nread
);
259 ERRNO_RTN(signed_nread
);
262 int WRAP(readlink
)(const char* path
, char* buf
, size_t count
, size_t* nread
) {
263 ssize_t signed_nread
= ki_readlink(path
, buf
, count
);
264 *nread
= static_cast<size_t>(signed_nread
);
265 ERRNO_RTN(signed_nread
);
268 int WRAP(rmdir
)(const char* pathname
) {
269 ERRNO_RTN(ki_rmdir(pathname
));
272 int WRAP(seek
)(int fd
, off64_t offset
, int whence
, int64_t* new_offset
) {
273 *new_offset
= ki_lseek(fd
, offset
, whence
);
274 ERRNO_RTN(*new_offset
);
277 int WRAP(select
)(int nfds
,
281 struct timeval
* timeout
,
283 *count
= ki_select(nfds
, readfds
, writefds
, exceptfds
, timeout
);
287 int WRAP(stat
)(const char* pathname
, struct nacl_abi_stat
* nacl_buf
) {
289 memset(&buf
, 0, sizeof(struct stat
));
290 int res
= ki_stat(pathname
, &buf
);
291 RTN_ERRNO_IF(res
< 0);
292 stat_to_nacl_stat(&buf
, nacl_buf
);
296 int WRAP(truncate
)(const char* name
, int64_t len
) {
297 ERRNO_RTN(ki_truncate(name
, len
));
300 int WRAP(write
)(int fd
, const void* buf
, size_t count
, size_t* nwrote
) {
301 ssize_t signed_nwrote
= ki_write(fd
, buf
, count
);
302 *nwrote
= static_cast<size_t>(signed_nwrote
);
303 ERRNO_RTN(signed_nwrote
);
306 static void assign_real_pointers() {
307 static bool assigned
= false;
309 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR
)
314 #define CHECK_REAL(func) \
316 assign_real_pointers(); \
321 // "real" functions, i.e. the unwrapped original functions.
323 int _real_close(int fd
) {
325 return REAL(close
)(fd
);
328 void _real_exit(int status
) {
332 int _real_fchdir(int fd
) {
334 return REAL(fchdir
)(fd
);
337 int _real_fchmod(int fd
, mode_t mode
) {
339 return REAL(fchmod
)(fd
, mode
);
342 int _real_fdatasync(int fd
) {
343 CHECK_REAL(fdatasync
);
344 return REAL(fdatasync
)(fd
);
347 int _real_fstat(int fd
, struct stat
* buf
) {
348 struct nacl_abi_stat st
;
351 int err
= REAL(fstat
)(fd
, (struct stat
*)&st
);
357 nacl_stat_to_stat(&st
, buf
);
361 int _real_fsync(int fd
) {
363 return REAL(fsync
)(fd
);
366 int _real_getdents(int fd
, void* buf
, size_t count
, size_t* nread
) {
367 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
368 // See WRAP(getdents) above.
369 char* nacl_buf
= (char*)alloca(count
);
371 size_t nacl_offset
= 0;
373 CHECK_REAL(getdents
);
374 int err
= REAL(getdents
)(fd
, (dirent
*)nacl_buf
, count
, &nacl_nread
);
378 while (nacl_offset
< nacl_nread
) {
379 dirent
* d
= (dirent
*)((char*)buf
+ offset
);
380 nacl_abi_dirent
* nacl_d
= (nacl_abi_dirent
*)(nacl_buf
+ nacl_offset
);
381 d
->d_ino
= nacl_d
->nacl_abi_d_ino
;
382 d
->d_off
= nacl_d
->nacl_abi_d_off
;
383 d
->d_reclen
= nacl_d
->nacl_abi_d_reclen
+ d_name_shift
;
385 nacl_d
->nacl_abi_d_reclen
- offsetof(nacl_abi_dirent
, nacl_abi_d_name
);
386 memcpy(d
->d_name
, nacl_d
->nacl_abi_d_name
, d_name_len
);
388 offset
+= d
->d_reclen
;
389 offset
+= nacl_d
->nacl_abi_d_reclen
;
396 int _real_isatty(int fd
, int* result
) {
397 *result
= isatty(fd
);
398 return *result
? 0 : -1;
401 int _real_lseek(int fd
, int64_t offset
, int whence
, int64_t* new_offset
) {
403 nacl_abi_off_t nacl_new_offs
;
404 int ret
= REAL(seek
)(fd
, offset
, whence
, &nacl_new_offs
);
405 *new_offset
= static_cast<off_t
>(nacl_new_offs
);
409 int _real_lstat(const char* path
, struct stat
* buf
) {
410 struct nacl_abi_stat st
;
413 int err
= REAL(lstat
)(path
, (struct stat
*)&st
);
419 nacl_stat_to_stat(&st
, buf
);
423 int _real_mkdir(const char* pathname
, mode_t mode
) {
425 return REAL(mkdir
)(pathname
, mode
);
428 int _real_mmap(void** addr
,
435 return REAL(mmap
)(addr
, length
, prot
, flags
, fd
, offset
);
438 int _real_munmap(void* addr
, size_t length
) {
440 return REAL(munmap
)(addr
, length
);
443 int _real_open(const char* pathname
, int oflag
, mode_t mode
, int* newfd
) {
445 return REAL(open
)(pathname
, oflag
, mode
, newfd
);
448 int _real_open_resource(const char* file
, int* fd
) {
449 CHECK_REAL(open_resource
);
450 return REAL(open_resource
)(file
, fd
);
453 int _real_read(int fd
, void* buf
, size_t count
, size_t* nread
) {
455 return REAL(read
)(fd
, buf
, count
, nread
);
458 int _real_readlink(const char* path
, char* buf
, size_t count
, size_t* nread
) {
459 CHECK_REAL(readlink
);
460 return REAL(readlink
)(path
, buf
, count
, nread
);
463 int _real_rmdir(const char* pathname
) {
465 return REAL(rmdir
)(pathname
);
468 int _real_truncate(const char* pathname
, int64_t len
) {
469 CHECK_REAL(truncate
);
470 return REAL(truncate
)(pathname
, len
);
473 int _real_write(int fd
, const void* buf
, size_t count
, size_t* nwrote
) {
475 return REAL(write
)(fd
, buf
, count
, nwrote
);
478 int _real_getcwd(char* pathname
, size_t len
) {
480 return REAL(getcwd
)(pathname
, len
);
483 static bool s_wrapped
= false;
485 void kernel_wrap_init() {
487 assign_real_pointers();
488 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP
)
493 void kernel_wrap_uninit() {
495 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL
)
502 #endif // defined(__native_client__) && defined(__BIONIC__)