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/osmman.h"
30 void stat_to_nacl_stat(const struct stat
* buf
, nacl_abi_stat
* nacl_buf
) {
31 memset(nacl_buf
, 0, sizeof(struct nacl_abi_stat
));
32 nacl_buf
->nacl_abi_st_dev
= buf
->st_dev
;
33 nacl_buf
->nacl_abi_st_ino
= buf
->st_ino
;
34 nacl_buf
->nacl_abi_st_mode
= buf
->st_mode
;
35 nacl_buf
->nacl_abi_st_nlink
= buf
->st_nlink
;
36 nacl_buf
->nacl_abi_st_uid
= buf
->st_uid
;
37 nacl_buf
->nacl_abi_st_gid
= buf
->st_gid
;
38 nacl_buf
->nacl_abi_st_rdev
= buf
->st_rdev
;
39 nacl_buf
->nacl_abi_st_size
= buf
->st_size
;
40 nacl_buf
->nacl_abi_st_blksize
= buf
->st_blksize
;
41 nacl_buf
->nacl_abi_st_blocks
= buf
->st_blocks
;
42 nacl_buf
->nacl_abi_st_atime
= buf
->st_atime
;
43 nacl_buf
->nacl_abi_st_mtime
= buf
->st_mtime
;
44 nacl_buf
->nacl_abi_st_ctime
= buf
->st_ctime
;
47 void nacl_stat_to_stat(const nacl_abi_stat
* nacl_buf
, struct stat
* buf
) {
48 memset(buf
, 0, sizeof(struct stat
));
49 buf
->st_dev
= nacl_buf
->nacl_abi_st_dev
;
50 buf
->st_ino
= nacl_buf
->nacl_abi_st_ino
;
51 buf
->st_mode
= nacl_buf
->nacl_abi_st_mode
;
52 buf
->st_nlink
= nacl_buf
->nacl_abi_st_nlink
;
53 buf
->st_uid
= nacl_buf
->nacl_abi_st_uid
;
54 buf
->st_gid
= nacl_buf
->nacl_abi_st_gid
;
55 buf
->st_rdev
= nacl_buf
->nacl_abi_st_rdev
;
56 buf
->st_size
= nacl_buf
->nacl_abi_st_size
;
57 buf
->st_blksize
= nacl_buf
->nacl_abi_st_blksize
;
58 buf
->st_blocks
= nacl_buf
->nacl_abi_st_blocks
;
59 buf
->st_atime
= nacl_buf
->nacl_abi_st_atime
;
60 buf
->st_mtime
= nacl_buf
->nacl_abi_st_mtime
;
61 buf
->st_ctime
= nacl_buf
->nacl_abi_st_ctime
;
66 // From native_client/src/trusted/service_runtime/include/sys/dirent.h
68 #ifndef nacl_abi___ino_t_defined
69 #define nacl_abi___ino_t_defined
70 typedef int64_t nacl_abi___ino_t
;
71 typedef nacl_abi___ino_t nacl_abi_ino_t
;
74 #ifndef nacl_abi___off_t_defined
75 #define nacl_abi___off_t_defined
76 typedef int64_t nacl_abi__off_t
;
77 typedef nacl_abi__off_t nacl_abi_off_t
;
80 /* We need a way to define the maximum size of a name. */
83 # define MAXNAMLEN NAME_MAX
85 # define MAXNAMLEN 255
89 struct nacl_abi_dirent
{
90 nacl_abi_ino_t nacl_abi_d_ino
;
91 nacl_abi_off_t nacl_abi_d_off
;
92 uint16_t nacl_abi_d_reclen
;
93 char nacl_abi_d_name
[MAXNAMLEN
+ 1];
96 static const int d_name_shift
= offsetof (dirent
, d_name
) -
97 offsetof (struct nacl_abi_dirent
, nacl_abi_d_name
);
101 // Macro to get the REAL function pointer
102 #define REAL(name) __nacl_irt_##name##_real
104 // Macro to get the WRAP function
105 #define WRAP(name) __nacl_irt_##name##_wrap
107 // Declare REAL function pointer.
108 #define DECLARE_REAL_PTR(name) typeof(__nacl_irt_##name) REAL(name);
110 // Assign the REAL function pointer.
111 #define ASSIGN_REAL_PTR(name) REAL(name) = __nacl_irt_##name;
113 // Switch IRT's pointer to the REAL pointer
114 #define USE_REAL(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))REAL(name)
116 // Switch IRT's pointer to the WRAP function
117 #define USE_WRAP(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))WRAP(name)
119 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \
148 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR
);
150 int WRAP(chdir
)(const char* pathname
) {
151 ERRNO_RTN(ki_chdir(pathname
));
154 int WRAP(close
)(int fd
) {
155 ERRNO_RTN(ki_close(fd
));
158 int WRAP(dup
)(int fd
, int* newfd
) NOTHROW
{
163 int WRAP(dup2
)(int fd
, int newfd
) NOTHROW
{
164 ERRNO_RTN(ki_dup2(fd
, newfd
));
167 void WRAP(exit
)(int status
) {
171 int WRAP(fchdir
)(int fd
) NOTHROW
{
172 ERRNO_RTN(ki_fchdir(fd
));
175 int WRAP(fchmod
)(int fd
, mode_t mode
) NOTHROW
{
176 ERRNO_RTN(ki_fchmod(fd
, mode
));
179 int WRAP(fdatasync
)(int fd
) NOTHROW
{
180 ERRNO_RTN(ki_fdatasync(fd
));
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(fsync
)(int fd
) NOTHROW
{
193 ERRNO_RTN(ki_fsync(fd
));
196 int WRAP(getcwd
)(char* buf
, size_t size
) {
197 RTN_ERRNO_IF(ki_getcwd(buf
, size
) == NULL
);
201 int WRAP(getdents
)(int fd
, dirent
* nacl_buf
, size_t nacl_count
, size_t* nread
) {
203 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
204 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer
206 char* buf
= (char*)alloca(nacl_count
);
210 count
= ki_getdents(fd
, buf
, nacl_count
);
211 RTN_ERRNO_IF(count
< 0);
213 while (offset
< count
) {
214 dirent
* d
= (dirent
*)(buf
+ offset
);
215 nacl_abi_dirent
* nacl_d
= (nacl_abi_dirent
*)((char*)nacl_buf
+ nacl_offset
);
216 nacl_d
->nacl_abi_d_ino
= d
->d_ino
;
217 nacl_d
->nacl_abi_d_off
= d
->d_off
;
218 nacl_d
->nacl_abi_d_reclen
= d
->d_reclen
- d_name_shift
;
219 size_t d_name_len
= d
->d_reclen
- offsetof(dirent
, d_name
);
220 memcpy(nacl_d
->nacl_abi_d_name
, d
->d_name
, d_name_len
);
222 offset
+= d
->d_reclen
;
223 nacl_offset
+= nacl_d
->nacl_abi_d_reclen
;
226 *nread
= nacl_offset
;
230 int WRAP(isatty
)(int fd
, int* result
) {
231 *result
= ki_isatty(fd
);
232 RTN_ERRNO_IF(*result
== 0);
236 int WRAP(lstat
)(const char* path
, struct nacl_abi_stat
* nacl_buf
) {
238 memset(&buf
, 0, sizeof(struct stat
));
239 int res
= ki_lstat(path
, &buf
);
240 RTN_ERRNO_IF(res
< 0);
241 stat_to_nacl_stat(&buf
, nacl_buf
);
245 int WRAP(mkdir
)(const char* pathname
, mode_t mode
) {
246 ERRNO_RTN(ki_mkdir(pathname
, mode
));
249 int WRAP(mmap
)(void** addr
,
255 if (flags
& MAP_ANONYMOUS
)
256 return REAL(mmap
)(addr
, length
, prot
, flags
, fd
, offset
);
258 *addr
= ki_mmap(*addr
, length
, prot
, flags
, fd
, offset
);
259 RTN_ERRNO_IF(*addr
== (void*)-1)
263 int WRAP(munmap
)(void* addr
, size_t length
) {
264 // Always let the real munmap run on the address range. It is not an error if
265 // there are no mapped pages in that range.
266 ki_munmap(addr
, length
);
267 return REAL(munmap
)(addr
, length
);
270 int WRAP(open
)(const char* pathname
, int oflag
, mode_t mode
, int* newfd
) {
271 *newfd
= ki_open(pathname
, oflag
, mode
);
275 int WRAP(open_resource
)(const char* file
, int* fd
) {
276 *fd
= ki_open_resource(file
);
280 int WRAP(poll
)(struct pollfd
* fds
, nfds_t nfds
, int timeout
, int* count
) {
281 *count
= ki_poll(fds
, nfds
, timeout
);
285 int WRAP(read
)(int fd
, void* buf
, size_t count
, size_t* nread
) {
286 ssize_t signed_nread
= ki_read(fd
, buf
, count
);
287 *nread
= static_cast<size_t>(signed_nread
);
288 ERRNO_RTN(signed_nread
);
291 int WRAP(readlink
)(const char* path
, char* buf
, size_t count
, size_t* nread
) {
292 ssize_t signed_nread
= ki_readlink(path
, buf
, count
);
293 *nread
= static_cast<size_t>(signed_nread
);
294 ERRNO_RTN(signed_nread
);
297 int WRAP(rmdir
)(const char* pathname
) {
298 ERRNO_RTN(ki_rmdir(pathname
));
301 int WRAP(seek
)(int fd
, off64_t offset
, int whence
, int64_t* new_offset
) {
302 *new_offset
= ki_lseek(fd
, offset
, whence
);
303 ERRNO_RTN(*new_offset
);
306 int WRAP(select
)(int nfds
,
310 struct timeval
* timeout
,
312 *count
= ki_select(nfds
, readfds
, writefds
, exceptfds
, timeout
);
316 int WRAP(stat
)(const char* pathname
, struct nacl_abi_stat
* nacl_buf
) {
318 memset(&buf
, 0, sizeof(struct stat
));
319 int res
= ki_stat(pathname
, &buf
);
320 RTN_ERRNO_IF(res
< 0);
321 stat_to_nacl_stat(&buf
, nacl_buf
);
325 int WRAP(truncate
)(const char* name
, int64_t len
) {
326 ERRNO_RTN(ki_truncate(name
, len
));
329 int WRAP(write
)(int fd
, const void* buf
, size_t count
, size_t* nwrote
) {
330 ssize_t signed_nwrote
= ki_write(fd
, buf
, count
);
331 *nwrote
= static_cast<size_t>(signed_nwrote
);
332 ERRNO_RTN(signed_nwrote
);
335 static void assign_real_pointers() {
336 static bool assigned
= false;
338 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR
)
343 #define CHECK_REAL(func) \
345 assign_real_pointers(); \
350 // "real" functions, i.e. the unwrapped original functions.
352 int _real_close(int fd
) {
354 return REAL(close
)(fd
);
357 void _real_exit(int status
) {
361 int _real_fchdir(int fd
) {
363 return REAL(fchdir
)(fd
);
366 int _real_fchmod(int fd
, mode_t mode
) {
368 return REAL(fchmod
)(fd
, mode
);
371 int _real_fdatasync(int fd
) {
372 CHECK_REAL(fdatasync
);
373 return REAL(fdatasync
)(fd
);
376 int _real_fstat(int fd
, struct stat
* buf
) {
377 struct nacl_abi_stat st
;
380 int err
= REAL(fstat
)(fd
, (struct stat
*)&st
);
386 nacl_stat_to_stat(&st
, buf
);
390 int _real_fsync(int fd
) {
392 return REAL(fsync
)(fd
);
395 int _real_getdents(int fd
, void* buf
, size_t count
, size_t* nread
) {
396 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
397 // See WRAP(getdents) above.
398 char* nacl_buf
= (char*)alloca(count
);
400 size_t nacl_offset
= 0;
402 CHECK_REAL(getdents
);
403 int err
= REAL(getdents
)(fd
, (dirent
*)nacl_buf
, count
, &nacl_nread
);
407 while (nacl_offset
< nacl_nread
) {
408 dirent
* d
= (dirent
*)((char*)buf
+ offset
);
409 nacl_abi_dirent
* nacl_d
= (nacl_abi_dirent
*)(nacl_buf
+ nacl_offset
);
410 d
->d_ino
= nacl_d
->nacl_abi_d_ino
;
411 d
->d_off
= nacl_d
->nacl_abi_d_off
;
412 d
->d_reclen
= nacl_d
->nacl_abi_d_reclen
+ d_name_shift
;
414 nacl_d
->nacl_abi_d_reclen
- offsetof(nacl_abi_dirent
, nacl_abi_d_name
);
415 memcpy(d
->d_name
, nacl_d
->nacl_abi_d_name
, d_name_len
);
417 offset
+= d
->d_reclen
;
418 offset
+= nacl_d
->nacl_abi_d_reclen
;
425 int _real_isatty(int fd
, int* result
) {
426 *result
= isatty(fd
);
427 return *result
? 0 : -1;
430 int _real_lseek(int fd
, int64_t offset
, int whence
, int64_t* new_offset
) {
432 nacl_abi_off_t nacl_new_offs
;
433 int ret
= REAL(seek
)(fd
, offset
, whence
, &nacl_new_offs
);
434 *new_offset
= static_cast<off_t
>(nacl_new_offs
);
438 int _real_lstat(const char* path
, struct stat
* buf
) {
439 struct nacl_abi_stat st
;
442 int err
= REAL(lstat
)(path
, (struct stat
*)&st
);
448 nacl_stat_to_stat(&st
, buf
);
452 int _real_mkdir(const char* pathname
, mode_t mode
) {
454 return REAL(mkdir
)(pathname
, mode
);
457 int _real_mmap(void** addr
,
464 return REAL(mmap
)(addr
, length
, prot
, flags
, fd
, offset
);
467 int _real_munmap(void* addr
, size_t length
) {
469 return REAL(munmap
)(addr
, length
);
472 int _real_open(const char* pathname
, int oflag
, mode_t mode
, int* newfd
) {
474 return REAL(open
)(pathname
, oflag
, mode
, newfd
);
477 int _real_open_resource(const char* file
, int* fd
) {
478 CHECK_REAL(open_resource
);
479 return REAL(open_resource
)(file
, fd
);
482 int _real_read(int fd
, void* buf
, size_t count
, size_t* nread
) {
484 return REAL(read
)(fd
, buf
, count
, nread
);
487 int _real_readlink(const char* path
, char* buf
, size_t count
, size_t* nread
) {
488 CHECK_REAL(readlink
);
489 return REAL(readlink
)(path
, buf
, count
, nread
);
492 int _real_rmdir(const char* pathname
) {
494 return REAL(rmdir
)(pathname
);
497 int _real_truncate(const char* pathname
, int64_t len
) {
498 CHECK_REAL(truncate
);
499 return REAL(truncate
)(pathname
, len
);
502 int _real_write(int fd
, const void* buf
, size_t count
, size_t* nwrote
) {
504 return REAL(write
)(fd
, buf
, count
, nwrote
);
507 int _real_getcwd(char* pathname
, size_t len
) {
509 return REAL(getcwd
)(pathname
, len
);
512 static bool s_wrapped
= false;
514 void kernel_wrap_init() {
516 assign_real_pointers();
517 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP
)
522 void kernel_wrap_uninit() {
524 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL
)
531 #endif // defined(__native_client__) && defined(__BIONIC__)