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 __BIONIC__. 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-bionic build. 9 10#if defined(__native_client__) && defined(__BIONIC__) 11#include <alloca.h> 12#include <assert.h> 13#include <dirent.h> 14#include <errno.h> 15#include <irt_syscalls.h> 16#include <string.h> 17#include <sys/stat.h> 18#include <sys/time.h> 19 20#include "nacl_io/kernel_intercept.h" 21#include "nacl_io/kernel_wrap.h" 22#include "nacl_io/kernel_wrap_real.h" 23#include "nacl_io/osmman.h" 24 25namespace { 26 27void stat_to_nacl_stat(const struct stat* buf, nacl_abi_stat* nacl_buf) { 28 memset(nacl_buf, 0, sizeof(struct nacl_abi_stat)); 29 nacl_buf->nacl_abi_st_dev = buf->st_dev; 30 nacl_buf->nacl_abi_st_ino = buf->st_ino; 31 nacl_buf->nacl_abi_st_mode = buf->st_mode; 32 nacl_buf->nacl_abi_st_nlink = buf->st_nlink; 33 nacl_buf->nacl_abi_st_uid = buf->st_uid; 34 nacl_buf->nacl_abi_st_gid = buf->st_gid; 35 nacl_buf->nacl_abi_st_rdev = buf->st_rdev; 36 nacl_buf->nacl_abi_st_size = buf->st_size; 37 nacl_buf->nacl_abi_st_blksize = buf->st_blksize; 38 nacl_buf->nacl_abi_st_blocks = buf->st_blocks; 39 nacl_buf->nacl_abi_st_atime = buf->st_atime; 40 nacl_buf->nacl_abi_st_mtime = buf->st_mtime; 41 nacl_buf->nacl_abi_st_ctime = buf->st_ctime; 42} 43 44void nacl_stat_to_stat(const nacl_abi_stat* nacl_buf, struct stat* buf) { 45 memset(buf, 0, sizeof(struct stat)); 46 buf->st_dev = nacl_buf->nacl_abi_st_dev; 47 buf->st_ino = nacl_buf->nacl_abi_st_ino; 48 buf->st_mode = nacl_buf->nacl_abi_st_mode; 49 buf->st_nlink = nacl_buf->nacl_abi_st_nlink; 50 buf->st_uid = nacl_buf->nacl_abi_st_uid; 51 buf->st_gid = nacl_buf->nacl_abi_st_gid; 52 buf->st_rdev = nacl_buf->nacl_abi_st_rdev; 53 buf->st_size = nacl_buf->nacl_abi_st_size; 54 buf->st_blksize = nacl_buf->nacl_abi_st_blksize; 55 buf->st_blocks = nacl_buf->nacl_abi_st_blocks; 56 buf->st_atime = nacl_buf->nacl_abi_st_atime; 57 buf->st_mtime = nacl_buf->nacl_abi_st_mtime; 58 buf->st_ctime = nacl_buf->nacl_abi_st_ctime; 59} 60 61} // namespace 62 63// From native_client/src/trusted/service_runtime/include/sys/dirent.h 64 65#ifndef nacl_abi___ino_t_defined 66#define nacl_abi___ino_t_defined 67typedef int64_t nacl_abi___ino_t; 68typedef nacl_abi___ino_t nacl_abi_ino_t; 69#endif 70 71#ifndef nacl_abi___off_t_defined 72#define nacl_abi___off_t_defined 73typedef int64_t nacl_abi__off_t; 74typedef nacl_abi__off_t nacl_abi_off_t; 75#endif 76 77/* We need a way to define the maximum size of a name. */ 78#ifndef MAXNAMLEN 79# ifdef NAME_MAX 80# define MAXNAMLEN NAME_MAX 81# else 82# define MAXNAMLEN 255 83# endif 84#endif 85 86struct nacl_abi_dirent { 87 nacl_abi_ino_t nacl_abi_d_ino; 88 nacl_abi_off_t nacl_abi_d_off; 89 uint16_t nacl_abi_d_reclen; 90 char nacl_abi_d_name[MAXNAMLEN + 1]; 91}; 92 93static const int d_name_shift = offsetof (dirent, d_name) - 94 offsetof (struct nacl_abi_dirent, nacl_abi_d_name); 95 96EXTERN_C_BEGIN 97 98// Macro to get the REAL function pointer 99#define REAL(name) __nacl_irt_##name##_real 100 101// Macro to get the WRAP function 102#define WRAP(name) __nacl_irt_##name##_wrap 103 104// Declare REAL function pointer. 105#define DECLARE_REAL_PTR(name) typeof(__nacl_irt_##name) REAL(name); 106 107// Assign the REAL function pointer. 108#define ASSIGN_REAL_PTR(name) REAL(name) = __nacl_irt_##name; 109 110// Switch IRT's pointer to the REAL pointer 111#define USE_REAL(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))REAL(name) 112 113// Switch IRT's pointer to the WRAP function 114#define USE_WRAP(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))WRAP(name) 115 116#define EXPAND_SYMBOL_LIST_OPERATION(OP) \ 117 OP(chdir); \ 118 OP(close); \ 119 OP(dup); \ 120 OP(dup2); \ 121 OP(exit); \ 122 OP(fchdir); \ 123 OP(fchmod); \ 124 OP(fdatasync); \ 125 OP(fstat); \ 126 OP(fsync); \ 127 OP(getcwd); \ 128 OP(getdents); \ 129 OP(isatty); \ 130 OP(lstat); \ 131 OP(mkdir); \ 132 OP(mmap); \ 133 OP(munmap); \ 134 OP(open); \ 135 OP(open_resource); \ 136 OP(poll); \ 137 OP(read); \ 138 OP(readlink); \ 139 OP(rmdir); \ 140 OP(seek); \ 141 OP(stat); \ 142 OP(truncate); \ 143 OP(write); 144 145EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); 146 147int WRAP(chdir)(const char* pathname) { 148 ERRNO_RTN(ki_chdir(pathname)); 149} 150 151int WRAP(close)(int fd) { 152 ERRNO_RTN(ki_close(fd)); 153} 154 155int WRAP(dup)(int fd, int* newfd) NOTHROW { 156 *newfd = ki_dup(fd); 157 ERRNO_RTN(*newfd); 158} 159 160int WRAP(dup2)(int fd, int newfd) NOTHROW { 161 ERRNO_RTN(ki_dup2(fd, newfd)); 162} 163 164void WRAP(exit)(int status) { 165 ki_exit(status); 166} 167 168int WRAP(fchdir)(int fd) NOTHROW { 169 ERRNO_RTN(ki_fchdir(fd)); 170} 171 172int WRAP(fchmod)(int fd, mode_t mode) NOTHROW { 173 ERRNO_RTN(ki_fchmod(fd, mode)); 174} 175 176int WRAP(fdatasync)(int fd) NOTHROW { 177 ERRNO_RTN(ki_fdatasync(fd)); 178} 179 180int WRAP(fstat)(int fd, struct nacl_abi_stat* nacl_buf) { 181 struct stat buf; 182 memset(&buf, 0, sizeof(struct stat)); 183 int res = ki_fstat(fd, &buf); 184 RTN_ERRNO_IF(res < 0); 185 stat_to_nacl_stat(&buf, nacl_buf); 186 return 0; 187} 188 189int WRAP(fsync)(int fd) NOTHROW { 190 ERRNO_RTN(ki_fsync(fd)); 191} 192 193int WRAP(getcwd)(char* buf, size_t size) { 194 RTN_ERRNO_IF(ki_getcwd(buf, size) == NULL); 195 return 0; 196} 197 198int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, 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; 206 207 count = ki_getdents(fd, buf, nacl_count); 208 RTN_ERRNO_IF(count < 0); 209 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); 218 219 offset += d->d_reclen; 220 nacl_offset += nacl_d->nacl_abi_d_reclen; 221 } 222 223 *nread = nacl_offset; 224 return 0; 225} 226 227int WRAP(isatty)(int fd, int* result) { 228 *result = ki_isatty(fd); 229 RTN_ERRNO_IF(*result == 0); 230 return 0; 231} 232 233int WRAP(lstat)(const char* path, struct nacl_abi_stat* nacl_buf) { 234 struct stat buf; 235 memset(&buf, 0, sizeof(struct stat)); 236 int res = ki_lstat(path, &buf); 237 RTN_ERRNO_IF(res < 0); 238 stat_to_nacl_stat(&buf, nacl_buf); 239 return 0; 240} 241 242int WRAP(mkdir)(const char* pathname, mode_t mode) { 243 ERRNO_RTN(ki_mkdir(pathname, mode)); 244} 245 246int WRAP(mmap)(void** addr, 247 size_t length, 248 int prot, 249 int flags, 250 int fd, 251 int64_t offset) { 252 if (flags & MAP_ANONYMOUS) 253 return REAL(mmap)(addr, length, prot, flags, fd, offset); 254 255 *addr = ki_mmap(*addr, length, prot, flags, fd, offset); 256 RTN_ERRNO_IF(*addr == (void*)-1) 257 return 0; 258} 259 260int WRAP(munmap)(void* addr, size_t length) { 261 // Always let the real munmap run on the address range. It is not an error if 262 // there are no mapped pages in that range. 263 ki_munmap(addr, length); 264 return REAL(munmap)(addr, length); 265} 266 267int WRAP(open)(const char* pathname, int oflag, mode_t mode, int* newfd) { 268 *newfd = ki_open(pathname, oflag, mode); 269 ERRNO_RTN(*newfd); 270} 271 272int WRAP(open_resource)(const char* file, int* fd) { 273 *fd = ki_open_resource(file); 274 ERRNO_RTN(*fd); 275} 276 277int WRAP(poll)(struct pollfd* fds, nfds_t nfds, int timeout, int* count) { 278 *count = ki_poll(fds, nfds, timeout); 279 ERRNO_RTN(*count); 280} 281 282int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) { 283 ssize_t signed_nread = ki_read(fd, buf, count); 284 *nread = static_cast<size_t>(signed_nread); 285 ERRNO_RTN(signed_nread); 286} 287 288int WRAP(readlink)(const char* path, char* buf, size_t count, size_t* nread) { 289 ssize_t signed_nread = ki_readlink(path, buf, count); 290 *nread = static_cast<size_t>(signed_nread); 291 ERRNO_RTN(signed_nread); 292} 293 294int WRAP(rmdir)(const char* pathname) { 295 ERRNO_RTN(ki_rmdir(pathname)); 296} 297 298int WRAP(seek)(int fd, off64_t offset, int whence, int64_t* new_offset) { 299 *new_offset = ki_lseek(fd, offset, whence); 300 ERRNO_RTN(*new_offset); 301} 302 303int WRAP(select)(int nfds, 304 fd_set* readfds, 305 fd_set* writefds, 306 fd_set* exceptfds, 307 struct timeval* timeout, 308 int* count) { 309 *count = ki_select(nfds, readfds, writefds, exceptfds, timeout); 310 ERRNO_RTN(*count); 311} 312 313int WRAP(stat)(const char* pathname, struct nacl_abi_stat* nacl_buf) { 314 struct stat buf; 315 memset(&buf, 0, sizeof(struct stat)); 316 int res = ki_stat(pathname, &buf); 317 RTN_ERRNO_IF(res < 0); 318 stat_to_nacl_stat(&buf, nacl_buf); 319 return 0; 320} 321 322int WRAP(truncate)(const char* name, int64_t len) { 323 ERRNO_RTN(ki_truncate(name, len)); 324} 325 326int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { 327 ssize_t signed_nwrote = ki_write(fd, buf, count); 328 *nwrote = static_cast<size_t>(signed_nwrote); 329 ERRNO_RTN(signed_nwrote); 330} 331 332static void assign_real_pointers() { 333 static bool assigned = false; 334 if (!assigned) { 335 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) 336 assigned = true; 337 } 338} 339 340#define CHECK_REAL(func) \ 341 if (!REAL(func)) { \ 342 assign_real_pointers(); \ 343 if (!REAL(func)) \ 344 return ENOSYS; \ 345 } 346 347// "real" functions, i.e. the unwrapped original functions. 348 349int _real_close(int fd) { 350 CHECK_REAL(close); 351 return REAL(close)(fd); 352} 353 354void _real_exit(int status) { 355 REAL(exit)(status); 356} 357 358int _real_fchdir(int fd) { 359 CHECK_REAL(fchdir); 360 return REAL(fchdir)(fd); 361} 362 363int _real_fchmod(int fd, mode_t mode) { 364 CHECK_REAL(fchmod); 365 return REAL(fchmod)(fd, mode); 366} 367 368int _real_fdatasync(int fd) { 369 CHECK_REAL(fdatasync); 370 return REAL(fdatasync)(fd); 371} 372 373int _real_fstat(int fd, struct stat* buf) { 374 struct nacl_abi_stat st; 375 CHECK_REAL(fstat); 376 377 int err = REAL(fstat)(fd, (struct stat*)&st); 378 if (err) { 379 errno = err; 380 return -1; 381 } 382 383 nacl_stat_to_stat(&st, buf); 384 return 0; 385} 386 387int _real_fsync(int fd) { 388 CHECK_REAL(fsync); 389 return REAL(fsync)(fd); 390} 391 392int _real_getdents(int fd, void* buf, size_t count, size_t* nread) { 393 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s). 394 // See WRAP(getdents) above. 395 char* nacl_buf = (char*)alloca(count); 396 size_t offset = 0; 397 size_t nacl_offset = 0; 398 size_t nacl_nread; 399 CHECK_REAL(getdents); 400 int err = REAL(getdents)(fd, (dirent*)nacl_buf, count, &nacl_nread); 401 if (err) 402 return err; 403 404 while (nacl_offset < nacl_nread) { 405 dirent* d = (dirent*)((char*)buf + offset); 406 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)(nacl_buf + nacl_offset); 407 d->d_ino = nacl_d->nacl_abi_d_ino; 408 d->d_off = nacl_d->nacl_abi_d_off; 409 d->d_reclen = nacl_d->nacl_abi_d_reclen + d_name_shift; 410 size_t d_name_len = 411 nacl_d->nacl_abi_d_reclen - offsetof(nacl_abi_dirent, nacl_abi_d_name); 412 memcpy(d->d_name, nacl_d->nacl_abi_d_name, d_name_len); 413 414 offset += d->d_reclen; 415 offset += nacl_d->nacl_abi_d_reclen; 416 } 417 418 *nread = offset; 419 return 0; 420} 421 422int _real_isatty(int fd, int* result) { 423 *result = isatty(fd); 424 return *result ? 0 : -1; 425} 426 427int _real_lseek(int fd, int64_t offset, int whence, int64_t* new_offset) { 428 CHECK_REAL(seek); 429 nacl_abi_off_t nacl_new_offs; 430 int ret = REAL(seek)(fd, offset, whence, &nacl_new_offs); 431 *new_offset = static_cast<off_t>(nacl_new_offs); 432 return ret; 433} 434 435int _real_lstat(const char* path, struct stat* buf) { 436 struct nacl_abi_stat st; 437 CHECK_REAL(lstat); 438 439 int err = REAL(lstat)(path, (struct stat*)&st); 440 if (err) { 441 errno = err; 442 return -1; 443 } 444 445 nacl_stat_to_stat(&st, buf); 446 return 0; 447} 448 449int _real_mkdir(const char* pathname, mode_t mode) { 450 CHECK_REAL(mkdir); 451 return REAL(mkdir)(pathname, mode); 452} 453 454int _real_mmap(void** addr, 455 size_t length, 456 int prot, 457 int flags, 458 int fd, 459 int64_t offset) { 460 CHECK_REAL(mmap); 461 return REAL(mmap)(addr, length, prot, flags, fd, offset); 462} 463 464int _real_munmap(void* addr, size_t length) { 465 CHECK_REAL(munmap); 466 return REAL(munmap)(addr, length); 467} 468 469int _real_open(const char* pathname, int oflag, mode_t mode, int* newfd) { 470 CHECK_REAL(open); 471 return REAL(open)(pathname, oflag, mode, newfd); 472} 473 474int _real_open_resource(const char* file, int* fd) { 475 CHECK_REAL(open_resource); 476 return REAL(open_resource)(file, fd); 477} 478 479int _real_read(int fd, void* buf, size_t count, size_t* nread) { 480 CHECK_REAL(read); 481 return REAL(read)(fd, buf, count, nread); 482} 483 484int _real_readlink(const char* path, char* buf, size_t count, size_t* nread) { 485 CHECK_REAL(readlink); 486 return REAL(readlink)(path, buf, count, nread); 487} 488 489int _real_rmdir(const char* pathname) { 490 CHECK_REAL(rmdir); 491 return REAL(rmdir)(pathname); 492} 493 494int _real_truncate(const char* pathname, int64_t len) { 495 CHECK_REAL(truncate); 496 return REAL(truncate)(pathname, len); 497} 498 499int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) { 500 CHECK_REAL(write); 501 return REAL(write)(fd, buf, count, nwrote); 502} 503 504int _real_getcwd(char* pathname, size_t len) { 505 CHECK_REAL(getcwd); 506 return REAL(getcwd)(pathname, len); 507} 508 509static bool s_wrapped = false; 510 511void kernel_wrap_init() { 512 if (!s_wrapped) { 513 assign_real_pointers(); 514 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP) 515 s_wrapped = true; 516 } 517} 518 519void kernel_wrap_uninit() { 520 if (s_wrapped) { 521 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) 522 s_wrapped = false; 523 } 524} 525 526EXTERN_C_END 527 528#endif // defined(__native_client__) && defined(__BIONIC__) 529