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 cmode, int* newfd) { 268 *newfd = ki_open(pathname, oflag); 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 344// "real" functions, i.e. the unwrapped original functions. 345 346int _real_close(int fd) { 347 CHECK_REAL(close); 348 return REAL(close)(fd); 349} 350 351void _real_exit(int status) { 352 REAL(exit)(status); 353} 354 355int _real_fchdir(int fd) { 356 CHECK_REAL(fchdir); 357 return REAL(fchdir)(fd); 358} 359 360int _real_fchmod(int fd, mode_t mode) { 361 CHECK_REAL(fchmod); 362 return REAL(fchmod)(fd, mode); 363} 364 365int _real_fdatasync(int fd) { 366 CHECK_REAL(fdatasync); 367 return REAL(fdatasync)(fd); 368} 369 370int _real_fstat(int fd, struct stat* buf) { 371 struct nacl_abi_stat st; 372 CHECK_REAL(fstat); 373 374 int err = REAL(fstat)(fd, (struct stat*)&st); 375 if (err) { 376 errno = err; 377 return -1; 378 } 379 380 nacl_stat_to_stat(&st, buf); 381 return 0; 382} 383 384int _real_fsync(int fd) { 385 CHECK_REAL(fsync); 386 return REAL(fsync)(fd); 387} 388 389int _real_getdents(int fd, void* buf, size_t count, size_t* nread) { 390 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s). 391 // See WRAP(getdents) above. 392 char* nacl_buf = (char*)alloca(count); 393 size_t offset = 0; 394 size_t nacl_offset = 0; 395 size_t nacl_nread; 396 CHECK_REAL(getdents); 397 int err = REAL(getdents)(fd, (dirent*)nacl_buf, count, &nacl_nread); 398 if (err) 399 return err; 400 401 while (nacl_offset < nacl_nread) { 402 dirent* d = (dirent*)((char*)buf + offset); 403 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)(nacl_buf + nacl_offset); 404 d->d_ino = nacl_d->nacl_abi_d_ino; 405 d->d_off = nacl_d->nacl_abi_d_off; 406 d->d_reclen = nacl_d->nacl_abi_d_reclen + d_name_shift; 407 size_t d_name_len = 408 nacl_d->nacl_abi_d_reclen - offsetof(nacl_abi_dirent, nacl_abi_d_name); 409 memcpy(d->d_name, nacl_d->nacl_abi_d_name, d_name_len); 410 411 offset += d->d_reclen; 412 offset += nacl_d->nacl_abi_d_reclen; 413 } 414 415 *nread = offset; 416 return 0; 417} 418 419int _real_isatty(int fd, int* result) { 420 *result = isatty(fd); 421 return *result ? 0 : -1; 422} 423 424int _real_lseek(int fd, int64_t offset, int whence, int64_t* new_offset) { 425 CHECK_REAL(seek); 426 nacl_abi_off_t nacl_new_offs; 427 int ret = REAL(seek)(fd, offset, whence, &nacl_new_offs); 428 *new_offset = static_cast<off_t>(nacl_new_offs); 429 return ret; 430} 431 432int _real_lstat(const char* path, struct stat* buf) { 433 struct nacl_abi_stat st; 434 CHECK_REAL(fstat); 435 436 int err = REAL(lstat)(path, (struct stat*)&st); 437 if (err) { 438 errno = err; 439 return -1; 440 } 441 442 nacl_stat_to_stat(&st, buf); 443 return 0; 444} 445 446int _real_mkdir(const char* pathname, mode_t mode) { 447 CHECK_REAL(mkdir); 448 return REAL(mkdir)(pathname, mode); 449} 450 451int _real_mmap(void** addr, 452 size_t length, 453 int prot, 454 int flags, 455 int fd, 456 int64_t offset) { 457 CHECK_REAL(mmap); 458 return REAL(mmap)(addr, length, prot, flags, fd, offset); 459} 460 461int _real_munmap(void* addr, size_t length) { 462 CHECK_REAL(munmap); 463 return REAL(munmap)(addr, length); 464} 465 466int _real_open(const char* pathname, int oflag, mode_t cmode, int* newfd) { 467 CHECK_REAL(open); 468 return REAL(open)(pathname, oflag, cmode, newfd); 469} 470 471int _real_open_resource(const char* file, int* fd) { 472 CHECK_REAL(open_resource); 473 return REAL(open_resource)(file, fd); 474} 475 476int _real_read(int fd, void* buf, size_t count, size_t* nread) { 477 CHECK_REAL(read); 478 return REAL(read)(fd, buf, count, nread); 479} 480 481int _real_readlink(const char* path, char* buf, size_t count, size_t* nread) { 482 CHECK_REAL(readlink); 483 return REAL(readlink)(path, buf, count, nread); 484} 485 486int _real_rmdir(const char* pathname) { 487 CHECK_REAL(rmdir); 488 return REAL(rmdir)(pathname); 489} 490 491int _real_truncate(const char* pathname, int64_t len) { 492 CHECK_REAL(truncate); 493 return REAL(truncate)(pathname, len); 494} 495 496int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) { 497 CHECK_REAL(write); 498 return REAL(write)(fd, buf, count, nwrote); 499} 500 501static bool s_wrapped = false; 502void kernel_wrap_init() { 503 if (!s_wrapped) { 504 assign_real_pointers(); 505 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP) 506 s_wrapped = true; 507 } 508} 509 510void kernel_wrap_uninit() { 511 if (s_wrapped) { 512 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) 513 s_wrapped = false; 514 } 515} 516 517EXTERN_C_END 518 519#endif // defined(__native_client__) && defined(__GLIBC__) 520