| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 | 9 |
| 10 #include "native_client/src/include/elf_auxv.h" |
| 11 #include "native_client/src/include/elf32.h" |
| 12 #include "native_client/src/include/nacl_macros.h" |
| 10 #include "native_client/src/untrusted/irt/irt_interfaces.h" | 13 #include "native_client/src/untrusted/irt/irt_interfaces.h" |
| 11 #include "native_client/src/untrusted/nacl/nacl_irt.h" | 14 #include "native_client/src/untrusted/nacl/nacl_irt.h" |
| 12 | 15 |
| 13 static int stub_close(int fd) { | 16 static int stub_close(int fd) { |
| 14 return ENOSYS; | 17 return ENOSYS; |
| 15 } | 18 } |
| 16 | 19 |
| 17 static int stub_dup(int fd, int *newfd) { | 20 static int stub_dup(int fd, int *newfd) { |
| 18 return ENOSYS; | 21 return ENOSYS; |
| 19 } | 22 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 36 | 39 |
| 37 static int stub_fstat(int fd, struct stat *st) { | 40 static int stub_fstat(int fd, struct stat *st) { |
| 38 return ENOSYS; | 41 return ENOSYS; |
| 39 } | 42 } |
| 40 | 43 |
| 41 static int stub_getdents(int fd, struct dirent *dirent, size_t count, | 44 static int stub_getdents(int fd, struct dirent *dirent, size_t count, |
| 42 size_t *nread) { | 45 size_t *nread) { |
| 43 return ENOSYS; | 46 return ENOSYS; |
| 44 } | 47 } |
| 45 | 48 |
| 49 struct nacl_irt_instance __libnacl_irt_instance; |
| 46 struct nacl_irt_basic __libnacl_irt_basic; | 50 struct nacl_irt_basic __libnacl_irt_basic; |
| 47 struct nacl_irt_memory __libnacl_irt_memory; | 51 struct nacl_irt_memory __libnacl_irt_memory; |
| 48 struct nacl_irt_tls __libnacl_irt_tls; | 52 struct nacl_irt_tls __libnacl_irt_tls; |
| 49 struct nacl_irt_clock __libnacl_irt_clock; | 53 struct nacl_irt_clock __libnacl_irt_clock; |
| 50 struct nacl_irt_dev_getpid __libnacl_irt_dev_getpid; | 54 struct nacl_irt_dev_getpid __libnacl_irt_dev_getpid; |
| 51 | 55 |
| 52 struct nacl_irt_dev_filename __libnacl_irt_dev_filename; | 56 struct nacl_irt_dev_filename __libnacl_irt_dev_filename; |
| 53 struct nacl_irt_dev_fdio __libnacl_irt_dev_fdio; | 57 struct nacl_irt_dev_fdio __libnacl_irt_dev_fdio; |
| 54 | 58 |
| 55 struct nacl_irt_fdio __libnacl_irt_fdio = { | 59 struct nacl_irt_fdio __libnacl_irt_fdio = { |
| 56 stub_close, | 60 stub_close, |
| 57 stub_dup, | 61 stub_dup, |
| 58 stub_dup2, | 62 stub_dup2, |
| 59 stub_read, | 63 stub_read, |
| 60 stub_write, | 64 stub_write, |
| 61 stub_seek, | 65 stub_seek, |
| 62 stub_fstat, | 66 stub_fstat, |
| 63 stub_getdents, | 67 stub_getdents, |
| 64 }; | 68 }; |
| 65 | 69 |
| 66 TYPE_nacl_irt_query __nacl_irt_query; | 70 TYPE_nacl_irt_query __nacl_irt_query; |
| 67 | 71 |
| 72 struct nacl_interface_table { |
| 73 const char *name; |
| 74 void *table; |
| 75 size_t size; |
| 76 }; |
| 77 |
| 78 static const struct nacl_interface_table irt_interfaces[] = { |
| 79 { NACL_IRT_INSTANCE_v0_1, &__libnacl_irt_instance, sizeof(__libnacl_irt_instan
ce) }, |
| 80 { NACL_IRT_BASIC_v0_1, &__libnacl_irt_basic, sizeof(__libnacl_irt_basic) }, |
| 81 { NACL_IRT_DEV_FDIO_v0_1, &__libnacl_irt_fdio, sizeof(__libnacl_irt_fdio) }, |
| 82 { NACL_IRT_FILENAME_v0_1, &__libnacl_irt_dev_filename, sizeof(struct nacl_irt_
filename) }, |
| 83 { NACL_IRT_DEV_FILENAME_v0_2, &__libnacl_irt_dev_filename, sizeof(__libnacl_ir
t_dev_filename) }, |
| 84 { NACL_IRT_MEMORY_v0_1, &__libnacl_irt_memory, sizeof(struct nacl_irt_memory_v
0_1) }, |
| 85 { NACL_IRT_MEMORY_v0_2, &__libnacl_irt_memory, sizeof(struct nacl_irt_memory_v
0_2) }, |
| 86 { NACL_IRT_MEMORY_v0_3, &__libnacl_irt_memory, sizeof(struct nacl_irt_memory)
}, |
| 87 { NACL_IRT_TLS_v0_1, &__libnacl_irt_tls, sizeof(__libnacl_irt_tls) }, |
| 88 { NACL_IRT_CLOCK_v0_1, &__libnacl_irt_clock, sizeof(__libnacl_irt_clock) }, |
| 89 { NACL_IRT_DEV_GETPID_v0_1, &__libnacl_irt_dev_getpid, |
| 90 sizeof(__libnacl_irt_dev_getpid) }, |
| 91 }; |
| 92 |
| 93 int nacl_irt_interface_set(const char *interface_ident, |
| 94 void *table, size_t tablesize, void *old_table) { |
| 95 int i; |
| 96 for (i = 0; i < NACL_ARRAY_SIZE(irt_interfaces); ++i) { |
| 97 if (0 == strcmp(interface_ident, irt_interfaces[i].name)) { |
| 98 const size_t size = irt_interfaces[i].size; |
| 99 if (size <= tablesize) { |
| 100 memcpy(old_table, irt_interfaces[i].table, size); |
| 101 memcpy(irt_interfaces[i].table, table, size); |
| 102 return size; |
| 103 } |
| 104 break; |
| 105 } |
| 106 } |
| 107 return 0; |
| 108 } |
| 68 | 109 |
| 69 /* | 110 /* |
| 70 * Avoid a dependency on libc's strlen function. | 111 * Avoid a dependency on libc's strlen function. |
| 71 */ | 112 */ |
| 72 static size_t my_strlen(const char *s) { | 113 static size_t my_strlen(const char *s) { |
| 73 size_t len = 0; | 114 size_t len = 0; |
| 74 while (*s++) ++len; | 115 while (*s++) ++len; |
| 75 return len; | 116 return len; |
| 76 } | 117 } |
| 77 | 118 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 99 | 140 |
| 100 /* | 141 /* |
| 101 * TODO(robertm): make the helper below globally accessible. | 142 * TODO(robertm): make the helper below globally accessible. |
| 102 */ | 143 */ |
| 103 static void __libnacl_fatal(const char* message) { | 144 static void __libnacl_fatal(const char* message) { |
| 104 __libnacl_message(message); | 145 __libnacl_message(message); |
| 105 __libnacl_abort(); | 146 __libnacl_abort(); |
| 106 | 147 |
| 107 } | 148 } |
| 108 | 149 |
| 109 | |
| 110 int __libnacl_irt_query(const char *interface, | 150 int __libnacl_irt_query(const char *interface, |
| 111 void *table, size_t table_size) { | 151 void *table, size_t table_size) { |
| 112 if (NULL == __nacl_irt_query) { | 152 if (NULL == __nacl_irt_query) { |
| 113 __libnacl_fatal("No IRT interface query routine!\n"); | 153 __libnacl_fatal("No IRT interface query routine!\n"); |
| 114 } | 154 } |
| 115 if (__nacl_irt_query(interface, table, table_size) != table_size) { | 155 if (__nacl_irt_query(interface, table, table_size) != table_size) { |
| 116 return 0; | 156 return 0; |
| 117 } | 157 } |
| 118 return 1; | 158 return 1; |
| 119 } | 159 } |
| 120 | 160 |
| 121 void __libnacl_mandatory_irt_query(const char *interface, | 161 void __libnacl_mandatory_irt_query(const char *interface, |
| 122 void *table, size_t table_size) { | 162 void *table, size_t table_size) { |
| 123 if (!__libnacl_irt_query(interface, table, table_size)) { | 163 if (!__libnacl_irt_query(interface, table, table_size)) { |
| 124 __libnacl_fatal("IRT interface query failed for essential interface\n"); | 164 __libnacl_fatal("IRT interface query failed for essential interface\n"); |
| 125 } | 165 } |
| 126 } | 166 } |
| 127 | 167 |
| 128 /* | |
| 129 * Used to lazily initialize an IRT interface function. | |
| 130 */ | |
| 131 int __libnacl_irt_init_fn(void *interface_field, void (*init)(void)) { | 168 int __libnacl_irt_init_fn(void *interface_field, void (*init)(void)) { |
| 132 if (*((void **) interface_field) == NULL) { | 169 if (*((void **) interface_field) == NULL) { |
| 133 init(); | 170 init(); |
| 134 if (*((void **) interface_field) == NULL) { | 171 if (*((void **) interface_field) == NULL) { |
| 135 errno = ENOSYS; | 172 errno = ENOSYS; |
| 136 return 0; | 173 return 0; |
| 137 } | 174 } |
| 138 } | 175 } |
| 139 return 1; | 176 return 1; |
| 140 } | 177 } |
| OLD | NEW |