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