Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / libraries / nacl_io / kernel_wrap_glibc.cc
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.
4
5 #include <sys/types.h>  // Include something that will define __GLIBC__.
6
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.
9 #if defined(__native_client__) && defined(__GLIBC__)
10
11 #include "nacl_io/kernel_wrap.h"
12
13 #include <alloca.h>
14 #include <assert.h>
15 #include <dirent.h>
16 #include <errno.h>
17 #include <irt.h>
18 #include <irt_syscalls.h>
19 #include <nacl_stat.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23
24 #include "nacl_io/kernel_intercept.h"
25 #include "nacl_io/kernel_wrap_real.h"
26 #include "nacl_io/log.h"
27 #include "nacl_io/osmman.h"
28
29 namespace {
30
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;
46 }
47
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;
63 }
64
65 }  // namespace
66
67 // From native_client/src/trusted/service_runtime/include/sys/dirent.h
68
69 #ifndef nacl_abi___ino_t_defined
70 #define nacl_abi___ino_t_defined
71 typedef int64_t nacl_abi___ino_t;
72 typedef nacl_abi___ino_t nacl_abi_ino_t;
73 #endif
74
75 #ifndef nacl_abi___off_t_defined
76 #define nacl_abi___off_t_defined
77 typedef int64_t nacl_abi__off_t;
78 typedef nacl_abi__off_t nacl_abi_off_t;
79 #endif
80
81 /* We need a way to define the maximum size of a name. */
82 #ifndef MAXNAMLEN
83 # ifdef NAME_MAX
84 #  define MAXNAMLEN NAME_MAX
85 # else
86 #  define MAXNAMLEN 255
87 # endif
88 #endif
89
90 struct nacl_abi_dirent {
91   nacl_abi_ino_t nacl_abi_d_ino;
92   nacl_abi_off_t nacl_abi_d_off;
93   uint16_t       nacl_abi_d_reclen;
94   char           nacl_abi_d_name[MAXNAMLEN + 1];
95 };
96
97 static const int d_name_shift = offsetof (dirent, d_name) -
98   offsetof (struct nacl_abi_dirent, nacl_abi_d_name);
99
100 EXTERN_C_BEGIN
101
102 // Macro to get the REAL function pointer
103 #define REAL(name) __nacl_irt_##name##_real
104
105 // Macro to get the WRAP function
106 #define WRAP(name) __nacl_irt_##name##_wrap
107
108 // Declare REAL function pointer.
109 #define DECLARE_REAL_PTR(name) typeof(__nacl_irt_##name) REAL(name);
110
111 // Assign the REAL function pointer.
112 #define ASSIGN_REAL_PTR(name)        \
113   assert(__nacl_irt_##name != NULL); \
114   REAL(name) = __nacl_irt_##name;
115
116 // Switch IRT's pointer to the REAL pointer
117 #define USE_REAL(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))REAL(name)
118
119 // Switch IRT's pointer to the WRAP function
120 #define USE_WRAP(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))WRAP(name)
121
122 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \
123   OP(chdir);                             \
124   OP(close);                             \
125   OP(dup);                               \
126   OP(dup2);                              \
127   OP(exit);                              \
128   OP(fstat);                             \
129   OP(getcwd);                            \
130   OP(getdents);                          \
131   OP(mkdir);                             \
132   OP(open);                              \
133   OP(poll);                              \
134   OP(read);                              \
135   OP(rmdir);                             \
136   OP(seek);                              \
137   OP(stat);                              \
138   OP(select);                            \
139   OP(write);                             \
140   OP(mmap);                              \
141   OP(munmap);                            \
142   OP(open_resource);                     \
143                                          \
144   OP(socket);                            \
145   OP(accept);                            \
146   OP(bind);                              \
147   OP(listen);                            \
148   OP(connect);                           \
149   OP(send);                              \
150   OP(sendmsg);                           \
151   OP(sendto);                            \
152   OP(recv);                              \
153   OP(recvmsg);                           \
154   OP(recvfrom);                          \
155   OP(getpeername);                       \
156   OP(getsockname);                       \
157   OP(getsockopt);                        \
158   OP(setsockopt);                        \
159   OP(socketpair);                        \
160   OP(shutdown);                          \
161                                          \
162   OP(access);                            \
163   OP(unlink);                            \
164   OP(fchdir);                            \
165   OP(fchmod);                            \
166   OP(fsync);                             \
167   OP(fdatasync);                         \
168   OP(lstat);                             \
169   OP(readlink);                          \
170   OP(utimes);
171
172 // TODO(bradnelson): Add these as well.
173 // OP(epoll_create);
174 // OP(epoll_create1);
175 // OP(epoll_ctl);
176 // OP(epoll_pwait);
177 // OP(ppoll);
178 // OP(pselect);
179
180 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR);
181
182 int WRAP(chdir)(const char* pathname) {
183   ERRNO_RTN(ki_chdir(pathname));
184 }
185
186 int WRAP(close)(int fd) {
187   ERRNO_RTN(ki_close(fd));
188 }
189
190 int WRAP(dup)(int fd, int* newfd) NOTHROW {
191   *newfd = ki_dup(fd);
192   RTN_ERRNO_IF(*newfd < 0);
193   return 0;
194 }
195
196 int WRAP(dup2)(int fd, int newfd) NOTHROW {
197   ERRNO_RTN(ki_dup2(fd, newfd));
198 }
199
200 void WRAP(exit)(int status) {
201   ki_exit(status);
202 }
203
204 int WRAP(fstat)(int fd, struct nacl_abi_stat* nacl_buf) {
205   struct stat buf;
206   memset(&buf, 0, sizeof(struct stat));
207   int res = ki_fstat(fd, &buf);
208   RTN_ERRNO_IF(res < 0);
209   stat_to_nacl_stat(&buf, nacl_buf);
210   return 0;
211 }
212
213 int WRAP(getcwd)(char* buf, size_t size) {
214   RTN_ERRNO_IF(ki_getcwd(buf, size) == NULL);
215   return 0;
216 }
217
218 int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t* nread) {
219   int nacl_offset = 0;
220   // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
221   // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer
222   // is enough
223   char* buf = (char*)alloca(nacl_count);
224   int offset = 0;
225   int count;
226
227   count = ki_getdents(fd, buf, nacl_count);
228   RTN_ERRNO_IF(count < 0);
229
230   while (offset < count) {
231     dirent* d = (dirent*)(buf + offset);
232     nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)((char*)nacl_buf + nacl_offset);
233     nacl_d->nacl_abi_d_ino = d->d_ino;
234     nacl_d->nacl_abi_d_off = d->d_off;
235     nacl_d->nacl_abi_d_reclen = d->d_reclen - d_name_shift;
236     size_t d_name_len = d->d_reclen - offsetof(dirent, d_name);
237     memcpy(nacl_d->nacl_abi_d_name, d->d_name, d_name_len);
238
239     offset += d->d_reclen;
240     nacl_offset += nacl_d->nacl_abi_d_reclen;
241   }
242
243   *nread = nacl_offset;
244   return 0;
245 }
246
247 int WRAP(mkdir)(const char* pathname, mode_t mode) {
248   RTN_ERRNO_IF(ki_mkdir(pathname, mode) < 0);
249   return 0;
250 }
251
252 int WRAP(mmap)(void** addr,
253                size_t length,
254                int prot,
255                int flags,
256                int fd,
257                off_t offset) {
258   if (flags & MAP_ANONYMOUS)
259     return REAL(mmap)(addr, length, prot, flags, fd, offset);
260
261   *addr = ki_mmap(*addr, length, prot, flags, fd, offset);
262   RTN_ERRNO_IF(*addr == (void*)-1);
263   return 0;
264 }
265
266 int WRAP(munmap)(void* addr, size_t length) {
267   // Always let the real munmap run on the address range. It is not an error if
268   // there are no mapped pages in that range.
269   ki_munmap(addr, length);
270   return REAL(munmap)(addr, length);
271 }
272
273 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) {
274   *newfd = ki_open(pathname, oflag);
275   RTN_ERRNO_IF(*newfd < 0);
276   return 0;
277 }
278
279 int WRAP(open_resource)(const char* file, int* fd) {
280   *fd = ki_open_resource(file);
281   RTN_ERRNO_IF(*fd < 0);
282   return 0;
283 }
284
285 int WRAP(poll)(struct pollfd* fds, nfds_t nfds, int timeout, int* count) {
286   *count = ki_poll(fds, nfds, timeout);
287   RTN_ERRNO_IF(*count < 0);
288   return 0;
289 }
290
291 int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) {
292   ssize_t signed_nread = ki_read(fd, buf, count);
293   *nread = static_cast<size_t>(signed_nread);
294   RTN_ERRNO_IF(signed_nread < 0);
295   return 0;
296 }
297
298 int WRAP(rmdir)(const char* pathname) {
299   RTN_ERRNO_IF(ki_rmdir(pathname) < 0);
300   return 0;
301 }
302
303 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) {
304   *new_offset = ki_lseek(fd, offset, whence);
305   RTN_ERRNO_IF(*new_offset < 0);
306   return 0;
307 }
308
309 int WRAP(select)(int nfds,
310                  fd_set* readfds,
311                  fd_set* writefds,
312                  fd_set* exceptfds,
313                  struct timeval* timeout,
314                  int* count) {
315   *count = ki_select(nfds, readfds, writefds, exceptfds, timeout);
316   RTN_ERRNO_IF(*count < 0);
317   return 0;
318 }
319
320 int WRAP(stat)(const char* pathname, struct nacl_abi_stat* nacl_buf) {
321   struct stat buf;
322   memset(&buf, 0, sizeof(struct stat));
323   int res = ki_stat(pathname, &buf);
324   RTN_ERRNO_IF(res < 0);
325   stat_to_nacl_stat(&buf, nacl_buf);
326   return 0;
327 }
328
329 int WRAP(lstat)(const char* pathname, struct nacl_abi_stat* nacl_buf) {
330   struct stat buf;
331   memset(&buf, 0, sizeof(struct stat));
332   int res = ki_lstat(pathname, &buf);
333   RTN_ERRNO_IF(res < 0);
334   stat_to_nacl_stat(&buf, nacl_buf);
335   return 0;
336 }
337
338 int WRAP(readlink)(const char* pathname,
339                    char* buf,
340                    size_t count,
341                    size_t* nread) {
342   int rtn = ki_readlink(pathname, buf, count);
343   RTN_ERRNO_IF(rtn < 0);
344   *nread = rtn;
345   return 0;
346 }
347
348 int WRAP(utimes)(const char *filename, const struct timeval *times) {
349   ERRNO_RTN(ki_utimes(filename, times));
350 }
351
352 int WRAP(access)(const char* pathname, int amode) {
353   ERRNO_RTN(ki_access(pathname, amode));
354 }
355
356 int WRAP(unlink)(const char* pathname) {
357   ERRNO_RTN(ki_unlink(pathname));
358 }
359
360 int WRAP(fchdir)(int fd) {
361   ERRNO_RTN(ki_fchdir(fd));
362 }
363
364 int WRAP(fchmod)(int fd, mode_t mode) {
365   ERRNO_RTN(ki_fchmod(fd, mode));
366 }
367
368 int WRAP(fsync)(int fd) {
369   ERRNO_RTN(ki_fsync(fd));
370 }
371
372 int WRAP(fdatasync)(int fd) {
373   ERRNO_RTN(ki_fdatasync(fd));
374 }
375
376 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) {
377   ssize_t signed_nwrote = ki_write(fd, buf, count);
378   *nwrote = static_cast<size_t>(signed_nwrote);
379   RTN_ERRNO_IF(signed_nwrote < 0);
380   return 0;
381 }
382
383 int WRAP(accept)(int sockfd,
384                  struct sockaddr* addr,
385                  socklen_t* addrlen,
386                  int* sd) {
387   *sd = ki_accept(sockfd, addr, addrlen);
388   RTN_ERRNO_IF(*sd < 0);
389   return 0;
390 }
391
392 int WRAP(bind)(int sockfd, const struct sockaddr* addr, socklen_t addrlen) {
393   ERRNO_RTN(ki_bind(sockfd, addr, addrlen));
394 }
395
396 int WRAP(connect)(int sockfd, const struct sockaddr* addr, socklen_t addrlen) {
397   ERRNO_RTN(ki_connect(sockfd, addr, addrlen));
398 }
399
400 int WRAP(getpeername)(int sockfd, struct sockaddr* addr, socklen_t* addrlen) {
401   ERRNO_RTN(ki_getpeername(sockfd, addr, addrlen));
402 }
403
404 int WRAP(getsockname)(int sockfd, struct sockaddr* addr, socklen_t* addrlen) {
405   ERRNO_RTN(ki_getsockname(sockfd, addr, addrlen));
406 }
407
408 int WRAP(getsockopt)(int sockfd,
409                      int level,
410                      int optname,
411                      void* optval,
412                      socklen_t* optlen) {
413   ERRNO_RTN(ki_getsockopt(sockfd, level, optname, optval, optlen));
414 }
415
416 int WRAP(setsockopt)(int sockfd,
417                      int level,
418                      int optname,
419                      const void* optval,
420                      socklen_t optlen) {
421   ERRNO_RTN(ki_setsockopt(sockfd, level, optname, optval, optlen));
422 }
423
424 int WRAP(listen)(int sockfd, int backlog) {
425   ERRNO_RTN(ki_listen(sockfd, backlog));
426 }
427
428 int WRAP(recv)(int sockfd, void* buf, size_t len, int flags, int* count) {
429   ssize_t signed_nread = ki_recv(sockfd, buf, len, flags);
430   *count = static_cast<int>(signed_nread);
431   RTN_ERRNO_IF(signed_nread < 0);
432   return 0;
433 }
434
435 int WRAP(recvfrom)(int sockfd,
436                    void* buf,
437                    size_t len,
438                    int flags,
439                    struct sockaddr* addr,
440                    socklen_t* addrlen,
441                    int* count) {
442   ssize_t signed_nread = ki_recvfrom(sockfd, buf, len, flags, addr, addrlen);
443   *count = static_cast<int>(signed_nread);
444   RTN_ERRNO_IF(signed_nread < 0);
445   return 0;
446 }
447
448 int WRAP(recvmsg)(int sockfd, struct msghdr* msg, int flags, int* count) {
449   ssize_t signed_nread = ki_recvmsg(sockfd, msg, flags);
450   *count = static_cast<int>(signed_nread);
451   RTN_ERRNO_IF(signed_nread < 0);
452   return 0;
453 }
454
455 ssize_t WRAP(send)(int sockfd, const void* buf, size_t len, int flags,
456                    int* count) {
457   ssize_t signed_nread = ki_send(sockfd, buf, len, flags);
458   *count = static_cast<int>(signed_nread);
459   RTN_ERRNO_IF(signed_nread < 0);
460   return 0;
461 }
462
463 ssize_t WRAP(sendto)(int sockfd,
464                      const void* buf,
465                      size_t len,
466                      int flags,
467                      const struct sockaddr* addr,
468                      socklen_t addrlen,
469                      int* count) {
470   ssize_t signed_nread = ki_sendto(sockfd, buf, len, flags, addr, addrlen);
471   *count = static_cast<int>(signed_nread);
472   RTN_ERRNO_IF(signed_nread < 0);
473   return 0;
474 }
475
476 ssize_t WRAP(sendmsg)(int sockfd,
477                       const struct msghdr* msg,
478                       int flags,
479                       int* count) {
480   ssize_t signed_nread = ki_sendmsg(sockfd, msg, flags);
481   *count = static_cast<int>(signed_nread);
482   RTN_ERRNO_IF(signed_nread < 0);
483   return 0;
484 }
485
486 int WRAP(shutdown)(int sockfd, int how) {
487   RTN_ERRNO_IF(ki_shutdown(sockfd, how) < 0);
488   return 0;
489 }
490
491 int WRAP(socket)(int domain, int type, int protocol, int* sd) {
492   *sd = ki_socket(domain, type, protocol);
493   RTN_ERRNO_IF(*sd < 0);
494   return 0;
495 }
496
497 int WRAP(socketpair)(int domain, int type, int protocol, int* sv) {
498   RTN_ERRNO_IF(ki_socketpair(domain, type, protocol, sv) < 0);
499   return 0;
500 }
501
502 static void assign_real_pointers() {
503   static bool assigned = false;
504   if (!assigned) {
505     EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
506     assigned = true;
507   }
508 }
509
510 #define CHECK_REAL(func) \
511   if (!REAL(func))       \
512     assign_real_pointers();
513
514 // "real" functions, i.e. the unwrapped original functions.
515
516 int _real_close(int fd) {
517   CHECK_REAL(close);
518   return REAL(close)(fd);
519 }
520
521 void _real_exit(int status) {
522   CHECK_REAL(exit);
523   REAL(exit)(status);
524 }
525
526 int _real_fstat(int fd, struct stat* buf) {
527   struct nacl_abi_stat st;
528   CHECK_REAL(fstat);
529   int err = REAL(fstat)(fd, &st);
530   if (err) {
531     errno = err;
532     return -1;
533   }
534
535   nacl_stat_to_stat(&st, buf);
536   return 0;
537 }
538
539 int _real_getdents(int fd, void* buf, size_t count, size_t* nread) {
540   // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
541   // See WRAP(getdents) above.
542   char* nacl_buf = (char*)alloca(count);
543   size_t offset = 0;
544   size_t nacl_offset = 0;
545   size_t nacl_nread;
546   CHECK_REAL(getdents);
547   int err = REAL(getdents)(fd, (dirent*)nacl_buf, count, &nacl_nread);
548   if (err)
549     return err;
550
551   while (nacl_offset < nacl_nread) {
552     dirent* d = (dirent*)((char*)buf + offset);
553     nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)(nacl_buf + nacl_offset);
554     d->d_ino = nacl_d->nacl_abi_d_ino;
555     d->d_off = nacl_d->nacl_abi_d_off;
556     d->d_reclen = nacl_d->nacl_abi_d_reclen + d_name_shift;
557     size_t d_name_len =
558         nacl_d->nacl_abi_d_reclen - offsetof(nacl_abi_dirent, nacl_abi_d_name);
559     memcpy(d->d_name, nacl_d->nacl_abi_d_name, d_name_len);
560
561     offset += d->d_reclen;
562     offset += nacl_d->nacl_abi_d_reclen;
563   }
564
565   *nread = offset;
566   return 0;
567 }
568
569 int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) {
570   CHECK_REAL(seek);
571   return REAL(seek)(fd, offset, whence, new_offset);
572 }
573
574 int _real_mkdir(const char* pathname, mode_t mode) {
575   CHECK_REAL(mkdir);
576   return REAL(mkdir)(pathname, mode);
577 }
578
579 int _real_mmap(void** addr,
580                size_t length,
581                int prot,
582                int flags,
583                int fd,
584                off_t offset) {
585   CHECK_REAL(mmap);
586   return REAL(mmap)(addr, length, prot, flags, fd, offset);
587 }
588
589 int _real_munmap(void* addr, size_t length) {
590   CHECK_REAL(munmap);
591   return REAL(munmap)(addr, length);
592 }
593
594 int _real_open(const char* pathname, int oflag, mode_t cmode, int* newfd) {
595   CHECK_REAL(open);
596   return REAL(open)(pathname, oflag, cmode, newfd);
597 }
598
599 int _real_open_resource(const char* file, int* fd) {
600   CHECK_REAL(open_resource);
601   return REAL(open_resource)(file, fd);
602 }
603
604 int _real_read(int fd, void* buf, size_t count, size_t* nread) {
605   CHECK_REAL(read);
606   return REAL(read)(fd, buf, count, nread);
607 }
608
609 int _real_rmdir(const char* pathname) {
610   CHECK_REAL(rmdir);
611   return REAL(rmdir)(pathname);
612 }
613
614 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) {
615   CHECK_REAL(write);
616   return REAL(write)(fd, buf, count, nwrote);
617 }
618
619 static bool s_wrapped = false;
620 void kernel_wrap_init() {
621   if (!s_wrapped) {
622     LOG_TRACE("kernel_wrap_init");
623     assign_real_pointers();
624     EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP)
625     s_wrapped = true;
626   }
627 }
628
629 void kernel_wrap_uninit() {
630   if (s_wrapped) {
631     LOG_TRACE("kernel_wrap_uninit");
632     EXPAND_SYMBOL_LIST_OPERATION(USE_REAL)
633     s_wrapped = false;
634   }
635 }
636
637 EXTERN_C_END
638
639 #endif  // defined(__native_client__) && defined(__GLIBC__)