Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: native_client_sdk/src/libraries/nacl_io/kernel_wrap_newlib.cc

Issue 271513002: [NaCl SDK] nacl_io: Make IRT intercepts (and their test code) more robust. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <sys/types.h> // Include something that will define __GLIBC__. 5 #include <sys/types.h> // Include something that will define __GLIBC__.
6 6
7 // The entire file is wrapped in this #if. We do this so this .cc file can be 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. 8 // compiled, even on a non-newlib build.
9 #if defined(__native_client__) && !defined(__GLIBC__) && !defined(__BIONIC__) 9 #if defined(__native_client__) && !defined(__GLIBC__) && !defined(__BIONIC__)
10 10
11 #include "nacl_io/kernel_wrap.h" 11 #include "nacl_io/kernel_wrap.h"
12 12
13 #include <dirent.h> 13 #include <dirent.h>
14 #include <errno.h> 14 #include <errno.h>
15 #include <irt.h> 15 #include <irt.h>
16 #include <irt_dev.h> 16 #include <irt_dev.h>
17 #include <sys/mman.h> 17 #include <sys/mman.h>
18 #include <sys/stat.h> 18 #include <sys/stat.h>
19 #include <sys/time.h> 19 #include <sys/time.h>
20 20
21 #include "nacl_io/kernel_intercept.h" 21 #include "nacl_io/kernel_intercept.h"
22 #include "nacl_io/kernel_wrap_real.h" 22 #include "nacl_io/kernel_wrap_real.h"
23 #include "nacl_io/log.h"
23 24
24 EXTERN_C_BEGIN 25 EXTERN_C_BEGIN
25 26
26 // Macro to get the REAL function pointer 27 // Macro to get the REAL function pointer
27 #define REAL(name) __nacl_irt_##name##_real 28 #define REAL(name) __nacl_irt_##name##_real
28 29
29 // Macro to get the WRAP function 30 // Macro to get the WRAP function
30 #define WRAP(name) __nacl_irt_##name##_wrap 31 #define WRAP(name) __nacl_irt_##name##_wrap
31 32
32 // Declare REAL function pointer. 33 // Declare REAL function pointer.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 OP(dev_filename, link); \ 83 OP(dev_filename, link); \
83 OP(dev_filename, rename); \ 84 OP(dev_filename, rename); \
84 OP(dev_filename, symlink); \ 85 OP(dev_filename, symlink); \
85 OP(dev_filename, chmod); \ 86 OP(dev_filename, chmod); \
86 OP(dev_filename, access); \ 87 OP(dev_filename, access); \
87 OP(dev_filename, readlink); \ 88 OP(dev_filename, readlink); \
88 OP(dev_filename, utimes); \ 89 OP(dev_filename, utimes); \
89 OP(memory, mmap); \ 90 OP(memory, mmap); \
90 OP(memory, munmap); 91 OP(memory, munmap);
91 92
92
93 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); 93 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR);
94 94
95 int WRAP(close)(int fd) { 95 int WRAP(close)(int fd) {
96 return (ki_close(fd) < 0) ? errno : 0; 96 ERRNO_RTN(ki_close(fd));
97 } 97 }
98 98
99 int WRAP(dup)(int fd, int* newfd) { 99 int WRAP(dup)(int fd, int* newfd) {
100 *newfd = ki_dup(fd); 100 *newfd = ki_dup(fd);
101 return (*newfd < 0) ? errno : 0; 101 ERRNO_RTN(*newfd);
102 } 102 }
103 103
104 int WRAP(dup2)(int fd, int newfd) { 104 int WRAP(dup2)(int fd, int newfd) {
105 newfd = ki_dup2(fd, newfd); 105 newfd = ki_dup2(fd, newfd);
106 return (newfd < 0) ? errno : 0; 106 ERRNO_RTN(newfd);
107 } 107 }
108 108
109 void WRAP(exit)(int status) { 109 void WRAP(exit)(int status) {
110 ki_exit(status); 110 ki_exit(status);
111 } 111 }
112 112
113 int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) { 113 int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) {
114 ssize_t signed_nread = ki_read(fd, buf, count); 114 ssize_t signed_nread = ki_read(fd, buf, count);
115 *nread = static_cast<size_t>(signed_nread); 115 *nread = static_cast<size_t>(signed_nread);
116 return (signed_nread < 0) ? errno : 0; 116 ERRNO_RTN(signed_nread);
117 } 117 }
118 118
119 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { 119 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) {
120 ssize_t signed_nwrote = ki_write(fd, buf, count); 120 ssize_t signed_nwrote = ki_write(fd, buf, count);
121 *nwrote = static_cast<size_t>(signed_nwrote); 121 *nwrote = static_cast<size_t>(signed_nwrote);
122 return (signed_nwrote < 0) ? errno : 0; 122 ERRNO_RTN(signed_nwrote);
123 } 123 }
124 124
125 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) { 125 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) {
126 *new_offset = ki_lseek(fd, offset, whence); 126 *new_offset = ki_lseek(fd, offset, whence);
127 return (*new_offset < 0) ? errno : 0; 127 ERRNO_RTN(*new_offset);
128 } 128 }
129 129
130 int WRAP(fstat)(int fd, struct stat* buf) { 130 int WRAP(fstat)(int fd, struct stat* buf) {
131 return (ki_fstat(fd, buf) < 0) ? errno : 0; 131 ERRNO_RTN(ki_fstat(fd, buf));
132 } 132 }
133 133
134 int WRAP(getdents)(int fd, dirent* buf, size_t count, size_t* nread) { 134 int WRAP(getdents)(int fd, dirent* buf, size_t count, size_t* nread) {
135 int rtn = ki_getdents(fd, buf, count); 135 int rtn = ki_getdents(fd, buf, count);
136 if (rtn < 0) 136 RTN_ERRNO_IF(rtn < 0);
137 return errno;
138 *nread = rtn; 137 *nread = rtn;
139 return 0; 138 return 0;
140 } 139 }
141 140
142 int WRAP(fchdir)(int fd) { 141 int WRAP(fchdir)(int fd) {
143 return (ki_fchdir(fd) < 0) ? errno : 0; 142 ERRNO_RTN(ki_fchdir(fd));
144 } 143 }
145 144
146 int WRAP(fchmod)(int fd, mode_t mode) { 145 int WRAP(fchmod)(int fd, mode_t mode) {
147 return (ki_fchmod(fd, mode) < 0) ? errno : 0; 146 ERRNO_RTN(ki_fchmod(fd, mode));
148 } 147 }
149 148
150 int WRAP(fsync)(int fd) { 149 int WRAP(fsync)(int fd) {
151 return (ki_fsync(fd) < 0) ? errno : 0; 150 ERRNO_RTN(ki_fsync(fd));
152 } 151 }
153 152
154 int WRAP(fdatasync)(int fd) { 153 int WRAP(fdatasync)(int fd) {
155 return (ki_fdatasync(fd) < 0) ? errno : 0; 154 ERRNO_RTN(ki_fdatasync(fd));
156 } 155 }
157 156
158 int WRAP(ftruncate)(int fd, off_t length) { 157 int WRAP(ftruncate)(int fd, off_t length) {
159 return (ki_ftruncate(fd, length) < 0) ? errno : 0; 158 ERRNO_RTN(ki_ftruncate(fd, length));
160 } 159 }
161 160
162 int WRAP(isatty)(int fd, int* result) { 161 int WRAP(isatty)(int fd, int* result) {
163 *result = ki_isatty(fd); 162 *result = ki_isatty(fd);
164 if (*result == 0) 163 RTN_ERRNO_IF(*result == 0);
165 return errno;
166 return 0; 164 return 0;
167 } 165 }
168 166
169 int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd, 167 int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd,
170 off_t offset) { 168 off_t offset) {
171 if (flags & MAP_ANONYMOUS) 169 if (flags & MAP_ANONYMOUS)
172 return REAL(mmap)(addr, length, prot, flags, fd, offset); 170 return REAL(mmap)(addr, length, prot, flags, fd, offset);
173 171
174 *addr = ki_mmap(*addr, length, prot, flags, fd, offset); 172 *addr = ki_mmap(*addr, length, prot, flags, fd, offset);
175 return *addr == (void*)-1 ? errno : 0; 173 RTN_ERRNO_IF(*addr == (void*)-1);
174 return 0;
176 } 175 }
177 176
178 int WRAP(munmap)(void* addr, size_t length) { 177 int WRAP(munmap)(void* addr, size_t length) {
179 // Always let the real munmap run on the address range. It is not an error if 178 // Always let the real munmap run on the address range. It is not an error if
180 // there are no mapped pages in that range. 179 // there are no mapped pages in that range.
181 ki_munmap(addr, length); 180 ki_munmap(addr, length);
182 return REAL(munmap)(addr, length); 181 return REAL(munmap)(addr, length);
183 } 182 }
184 183
185 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { 184 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) {
186 *newfd = ki_open(pathname, oflag); 185 *newfd = ki_open(pathname, oflag);
187 return (*newfd < 0) ? errno : 0; 186 ERRNO_RTN(*newfd);
188 } 187 }
189 188
190 int WRAP(stat)(const char* pathname, struct stat* buf) { 189 int WRAP(stat)(const char* pathname, struct stat* buf) {
191 return (ki_stat(pathname, buf) < 0) ? errno : 0; 190 ERRNO_RTN(ki_stat(pathname, buf));
192 } 191 }
193 192
194 int WRAP(mkdir)(const char* pathname, mode_t mode) { 193 int WRAP(mkdir)(const char* pathname, mode_t mode) {
195 return (ki_mkdir(pathname, mode) < 0) ? errno : 0; 194 ERRNO_RTN(ki_mkdir(pathname, mode));
196 } 195 }
197 196
198 int WRAP(rmdir)(const char* pathname) { 197 int WRAP(rmdir)(const char* pathname) {
199 return (ki_rmdir(pathname) < 0) ? errno : 0; 198 ERRNO_RTN(ki_rmdir(pathname));
200 } 199 }
201 200
202 int WRAP(chdir)(const char* pathname) { 201 int WRAP(chdir)(const char* pathname) {
203 return (ki_chdir(pathname) < 0) ? errno : 0; 202 ERRNO_RTN(ki_chdir(pathname));
204 } 203 }
205 204
206 int WRAP(getcwd)(char* pathname, size_t len) { 205 int WRAP(getcwd)(char* pathname, size_t len) {
207 char* rtn = ki_getcwd(pathname, len); 206 char* rtn = ki_getcwd(pathname, len);
208 if (NULL == rtn) 207 RTN_ERRNO_IF(NULL == rtn);
209 return errno;
210 return 0; 208 return 0;
211 } 209 }
212 210
213 int WRAP(unlink)(const char* pathname) { 211 int WRAP(unlink)(const char* pathname) {
214 return (ki_unlink(pathname) < 0) ? errno : 0; 212 ERRNO_RTN(ki_unlink(pathname));
215 } 213 }
216 214
217 int WRAP(truncate)(const char* pathname, off_t length) { 215 int WRAP(truncate)(const char* pathname, off_t length) {
218 return (ki_truncate(pathname, length) < 0) ? errno : 0; 216 ERRNO_RTN(ki_truncate(pathname, length));
219 } 217 }
220 218
221 int WRAP(lstat)(const char* pathname, struct stat* buf) { 219 int WRAP(lstat)(const char* pathname, struct stat* buf) {
222 return (ki_lstat(pathname, buf) < 0) ? errno : 0; 220 ERRNO_RTN(ki_lstat(pathname, buf));
223 } 221 }
224 222
225 int WRAP(link)(const char* pathname, const char* newpath) { 223 int WRAP(link)(const char* pathname, const char* newpath) {
226 return (ki_link(pathname, newpath) < 0) ? errno : 0; 224 ERRNO_RTN(ki_link(pathname, newpath));
227 } 225 }
228 226
229 int WRAP(rename)(const char* pathname, const char* newpath) { 227 int WRAP(rename)(const char* pathname, const char* newpath) {
230 return (ki_rename(pathname, newpath) < 0) ? errno : 0; 228 ERRNO_RTN(ki_rename(pathname, newpath));
231 } 229 }
232 230
233 int WRAP(symlink)(const char* pathname, const char* newpath) { 231 int WRAP(symlink)(const char* pathname, const char* newpath) {
234 return (ki_symlink(pathname, newpath) < 0) ? errno : 0; 232 ERRNO_RTN(ki_symlink(pathname, newpath));
235 } 233 }
236 234
237 int WRAP(chmod)(const char* pathname, mode_t mode) { 235 int WRAP(chmod)(const char* pathname, mode_t mode) {
238 return (ki_chmod(pathname, mode) < 0) ? errno : 0; 236 ERRNO_RTN(ki_chmod(pathname, mode));
239 } 237 }
240 238
241 int WRAP(access)(const char* pathname, int amode) { 239 int WRAP(access)(const char* pathname, int amode) {
242 return (ki_access(pathname, amode) < 0) ? errno : 0; 240 ERRNO_RTN(ki_access(pathname, amode));
243 } 241 }
244 242
245 int WRAP(readlink)(const char* pathname, char *buf, 243 int WRAP(readlink)(const char* pathname, char *buf,
246 size_t count, size_t *nread) { 244 size_t count, size_t *nread) {
247 int rtn = ki_readlink(pathname, buf, count); 245 int rtn = ki_readlink(pathname, buf, count);
248 if (rtn < 0) 246 RTN_ERRNO_IF(rtn < 0);
249 return errno;
250 *nread = rtn; 247 *nread = rtn;
251 return 0; 248 return 0;
252 } 249 }
253 250
254 int WRAP(utimes)(const char* pathname, const struct timeval times[2]) { 251 int WRAP(utimes)(const char* pathname, const struct timeval times[2]) {
255 return (ki_utimes(pathname, times) < 0) ? errno : 0; 252 ERRNO_RTN(ki_utimes(pathname, times));
256 } 253 }
257 254
258 static void assign_real_pointers() { 255 static void assign_real_pointers() {
259 static bool assigned = false; 256 static bool assigned = false;
260 if (!assigned) { 257 if (!assigned) {
261 __libnacl_irt_dev_filename_init(); 258 __libnacl_irt_dev_filename_init();
262 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) 259 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
263 assigned = true; 260 assigned = true;
264 } 261 }
265 } 262 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 332
336 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) { 333 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) {
337 CHECK_REAL(write); 334 CHECK_REAL(write);
338 return REAL(write)(fd, buf, count, nwrote); 335 return REAL(write)(fd, buf, count, nwrote);
339 } 336 }
340 337
341 static bool s_wrapped = false; 338 static bool s_wrapped = false;
342 339
343 void kernel_wrap_init() { 340 void kernel_wrap_init() {
344 if (!s_wrapped) { 341 if (!s_wrapped) {
342 LOG_TRACE("kernel_wrap_init");
345 assign_real_pointers(); 343 assign_real_pointers();
346 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP) 344 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP)
347 s_wrapped = true; 345 s_wrapped = true;
348 } 346 }
349 } 347 }
350 348
351 void kernel_wrap_uninit() { 349 void kernel_wrap_uninit() {
352 if (s_wrapped) { 350 if (s_wrapped) {
351 LOG_TRACE("kernel_wrap_uninit");
353 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) 352 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL)
354 s_wrapped = false; 353 s_wrapped = false;
355 } 354 }
356 } 355 }
357 356
358 EXTERN_C_END 357 EXTERN_C_END
359 358
360 #endif // defined(__native_client__) && !defined(__GLIBC__) ... 359 #endif // defined(__native_client__) && !defined(__GLIBC__) ...
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698