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-newlib build. 9 #if defined(__native_client__) && !defined(__GLIBC__) && !defined(__BIONIC__) 10 11 #include "nacl_io/kernel_wrap.h" 12 13 #include <dirent.h> 14 #include <errno.h> 15 #include <irt.h> 16 #include <irt_dev.h> 17 #include <sys/mman.h> 18 #include <sys/stat.h> 19 #include <sys/time.h> 20 21 #include "nacl_io/kernel_intercept.h" 22 #include "nacl_io/kernel_wrap_real.h" 23 #include "nacl_io/log.h" 24 25 EXTERN_C_BEGIN 26 27 // Macro to get the REAL function pointer 28 #define REAL(name) __nacl_irt_##name##_real 29 30 // Macro to get the WRAP function 31 #define WRAP(name) __nacl_irt_##name##_wrap 32 33 // Declare REAL function pointer. 34 #define DECLARE_REAL_PTR(group, name) \ 35 typeof(__libnacl_irt_##group.name) REAL(name); 36 37 // Assign the REAL function pointer. 38 #define ASSIGN_REAL_PTR(group, name) REAL(name) = __libnacl_irt_##group.name; 39 40 // Switch IRT's pointer to the REAL pointer 41 #define USE_REAL(group, name) \ 42 __libnacl_irt_##group.name = (typeof(REAL(name)))REAL(name); 43 44 // Switch the IRT's pointer to the WRAP function 45 #define USE_WRAP(group, name) \ 46 __libnacl_irt_##group.name = (typeof(REAL(name)))WRAP(name); 47 48 extern void __libnacl_irt_dev_filename_init(void); 49 50 extern struct nacl_irt_basic __libnacl_irt_basic; 51 extern struct nacl_irt_fdio __libnacl_irt_fdio; 52 extern struct nacl_irt_dev_fdio __libnacl_irt_dev_fdio; 53 extern struct nacl_irt_dev_filename __libnacl_irt_dev_filename; 54 extern struct nacl_irt_memory __libnacl_irt_memory; 55 56 // Create function pointers to the REAL implementation 57 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \ 58 OP(basic, exit); \ 59 OP(fdio, close); \ 60 OP(fdio, dup); \ 61 OP(fdio, dup2); \ 62 OP(fdio, read); \ 63 OP(fdio, write); \ 64 OP(fdio, seek); \ 65 OP(fdio, fstat); \ 66 OP(fdio, getdents); \ 67 OP(dev_fdio, fchdir); \ 68 OP(dev_fdio, fchmod); \ 69 OP(dev_fdio, fsync); \ 70 OP(dev_fdio, fdatasync); \ 71 OP(dev_fdio, ftruncate); \ 72 OP(dev_fdio, isatty); \ 73 OP(dev_filename, open); \ 74 OP(dev_filename, stat); \ 75 OP(dev_filename, mkdir); \ 76 OP(dev_filename, rmdir); \ 77 OP(dev_filename, chdir); \ 78 OP(dev_filename, getcwd); \ 79 OP(dev_filename, unlink); \ 80 OP(dev_filename, truncate); \ 81 OP(dev_filename, lstat); \ 82 OP(dev_filename, link); \ 83 OP(dev_filename, rename); \ 84 OP(dev_filename, symlink); \ 85 OP(dev_filename, chmod); \ 86 OP(dev_filename, access); \ 87 OP(dev_filename, readlink); \ 88 OP(dev_filename, utimes); \ 89 OP(memory, mmap); \ 90 OP(memory, munmap); 91 92 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); 93 94 int WRAP(close)(int fd) { 95 ERRNO_RTN(ki_close(fd)); 96 } 97 98 int WRAP(dup)(int fd, int* newfd) { 99 *newfd = ki_dup(fd); 100 ERRNO_RTN(*newfd); 101 } 102 103 int WRAP(dup2)(int fd, int newfd) { 104 newfd = ki_dup2(fd, newfd); 105 ERRNO_RTN(newfd); 106 } 107 108 void WRAP(exit)(int status) { 109 ki_exit(status); 110 } 111 112 int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) { 113 ssize_t signed_nread = ki_read(fd, buf, count); 114 *nread = static_cast<size_t>(signed_nread); 115 ERRNO_RTN(signed_nread); 116 } 117 118 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { 119 ssize_t signed_nwrote = ki_write(fd, buf, count); 120 *nwrote = static_cast<size_t>(signed_nwrote); 121 ERRNO_RTN(signed_nwrote); 122 } 123 124 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) { 125 *new_offset = ki_lseek(fd, offset, whence); 126 ERRNO_RTN(*new_offset); 127 } 128 129 int WRAP(fstat)(int fd, struct stat* buf) { 130 ERRNO_RTN(ki_fstat(fd, buf)); 131 } 132 133 int WRAP(getdents)(int fd, dirent* buf, size_t count, size_t* nread) { 134 int rtn = ki_getdents(fd, buf, count); 135 RTN_ERRNO_IF(rtn < 0); 136 *nread = rtn; 137 return 0; 138 } 139 140 int WRAP(fchdir)(int fd) { 141 ERRNO_RTN(ki_fchdir(fd)); 142 } 143 144 int WRAP(fchmod)(int fd, mode_t mode) { 145 ERRNO_RTN(ki_fchmod(fd, mode)); 146 } 147 148 int WRAP(fsync)(int fd) { 149 ERRNO_RTN(ki_fsync(fd)); 150 } 151 152 int WRAP(fdatasync)(int fd) { 153 ERRNO_RTN(ki_fdatasync(fd)); 154 } 155 156 int WRAP(ftruncate)(int fd, off_t length) { 157 ERRNO_RTN(ki_ftruncate(fd, length)); 158 } 159 160 int WRAP(isatty)(int fd, int* result) { 161 *result = ki_isatty(fd); 162 RTN_ERRNO_IF(*result == 0); 163 return 0; 164 } 165 166 int WRAP(mmap)(void** addr, 167 size_t length, 168 int prot, 169 int flags, 170 int fd, 171 off_t offset) { 172 if (flags & MAP_ANONYMOUS) 173 return REAL(mmap)(addr, length, prot, flags, fd, offset); 174 175 *addr = ki_mmap(*addr, length, prot, flags, fd, offset); 176 RTN_ERRNO_IF(*addr == (void*)-1); 177 return 0; 178 } 179 180 int WRAP(munmap)(void* addr, size_t length) { 181 // Always let the real munmap run on the address range. It is not an error if 182 // there are no mapped pages in that range. 183 ki_munmap(addr, length); 184 return REAL(munmap)(addr, length); 185 } 186 187 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { 188 *newfd = ki_open(pathname, oflag); 189 ERRNO_RTN(*newfd); 190 } 191 192 int WRAP(stat)(const char* pathname, struct stat* buf) { 193 ERRNO_RTN(ki_stat(pathname, buf)); 194 } 195 196 int WRAP(mkdir)(const char* pathname, mode_t mode) { 197 ERRNO_RTN(ki_mkdir(pathname, mode)); 198 } 199 200 int WRAP(rmdir)(const char* pathname) { 201 ERRNO_RTN(ki_rmdir(pathname)); 202 } 203 204 int WRAP(chdir)(const char* pathname) { 205 ERRNO_RTN(ki_chdir(pathname)); 206 } 207 208 int WRAP(getcwd)(char* pathname, size_t len) { 209 char* rtn = ki_getcwd(pathname, len); 210 RTN_ERRNO_IF(NULL == rtn); 211 return 0; 212 } 213 214 int WRAP(unlink)(const char* pathname) { 215 ERRNO_RTN(ki_unlink(pathname)); 216 } 217 218 int WRAP(truncate)(const char* pathname, off_t length) { 219 ERRNO_RTN(ki_truncate(pathname, length)); 220 } 221 222 int WRAP(lstat)(const char* pathname, struct stat* buf) { 223 ERRNO_RTN(ki_lstat(pathname, buf)); 224 } 225 226 int WRAP(link)(const char* pathname, const char* newpath) { 227 ERRNO_RTN(ki_link(pathname, newpath)); 228 } 229 230 int WRAP(rename)(const char* pathname, const char* newpath) { 231 ERRNO_RTN(ki_rename(pathname, newpath)); 232 } 233 234 int WRAP(symlink)(const char* pathname, const char* newpath) { 235 ERRNO_RTN(ki_symlink(pathname, newpath)); 236 } 237 238 int WRAP(chmod)(const char* pathname, mode_t mode) { 239 ERRNO_RTN(ki_chmod(pathname, mode)); 240 } 241 242 int WRAP(access)(const char* pathname, int amode) { 243 ERRNO_RTN(ki_access(pathname, amode)); 244 } 245 246 int WRAP(readlink)(const char* pathname, 247 char* buf, 248 size_t count, 249 size_t* nread) { 250 int rtn = ki_readlink(pathname, buf, count); 251 RTN_ERRNO_IF(rtn < 0); 252 *nread = rtn; 253 return 0; 254 } 255 256 int WRAP(utimes)(const char* pathname, const struct timeval times[2]) { 257 ERRNO_RTN(ki_utimes(pathname, times)); 258 } 259 260 static void assign_real_pointers() { 261 static bool assigned = false; 262 if (!assigned) { 263 __libnacl_irt_dev_filename_init(); 264 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) 265 assigned = true; 266 } 267 } 268 269 #define CHECK_REAL(func) \ 270 if (!REAL(func)) \ 271 assign_real_pointers(); 272 273 // "real" functions, i.e. the unwrapped original functions. 274 275 int _real_close(int fd) { 276 CHECK_REAL(close); 277 return REAL(close)(fd); 278 } 279 280 void _real_exit(int status) { 281 CHECK_REAL(exit); 282 REAL(exit)(status); 283 } 284 285 int _real_fstat(int fd, struct stat* buf) { 286 CHECK_REAL(fstat); 287 return REAL(fstat)(fd, buf); 288 } 289 290 int _real_isatty(int fd, int* result) { 291 CHECK_REAL(isatty); 292 return REAL(isatty)(fd, result); 293 } 294 295 int _real_getdents(int fd, void* nacl_buf, size_t nacl_count, size_t* nread) { 296 CHECK_REAL(getdents); 297 return REAL(getdents)(fd, static_cast<dirent*>(nacl_buf), nacl_count, nread); 298 } 299 300 int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) { 301 CHECK_REAL(seek); 302 return REAL(seek)(fd, offset, whence, new_offset); 303 } 304 305 int _real_mkdir(const char* pathname, mode_t mode) { 306 return ENOSYS; 307 } 308 309 int _real_mmap(void** addr, 310 size_t length, 311 int prot, 312 int flags, 313 int fd, 314 off_t offset) { 315 CHECK_REAL(mmap); 316 return REAL(mmap)(addr, length, prot, flags, fd, offset); 317 } 318 319 int _real_munmap(void* addr, size_t length) { 320 CHECK_REAL(munmap); 321 return REAL(munmap)(addr, length); 322 } 323 324 int _real_open(const char* pathname, int oflag, mode_t cmode, int* newfd) { 325 CHECK_REAL(open); 326 return REAL(open)(pathname, oflag, cmode, newfd); 327 } 328 329 int _real_open_resource(const char* file, int* fd) { 330 return ENOSYS; 331 } 332 333 int _real_read(int fd, void* buf, size_t count, size_t* nread) { 334 CHECK_REAL(read); 335 return REAL(read)(fd, buf, count, nread); 336 } 337 338 int _real_rmdir(const char* pathname) { 339 return ENOSYS; 340 } 341 342 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) { 343 CHECK_REAL(write); 344 return REAL(write)(fd, buf, count, nwrote); 345 } 346 347 static bool s_wrapped = false; 348 349 void kernel_wrap_init() { 350 if (!s_wrapped) { 351 LOG_TRACE("kernel_wrap_init"); 352 assign_real_pointers(); 353 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP) 354 s_wrapped = true; 355 } 356 } 357 358 void kernel_wrap_uninit() { 359 if (s_wrapped) { 360 LOG_TRACE("kernel_wrap_uninit"); 361 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) 362 s_wrapped = false; 363 } 364 } 365 366 EXTERN_C_END 367 368 #endif // defined(__native_client__) && !defined(__GLIBC__) ... 369