Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / native_client / src / untrusted / nacl / nacl_ext_supply.c
1 /*
2  * Copyright (c) 2014 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include <string.h>
8
9 #include "native_client/src/include/nacl_macros.h"
10 #include "native_client/src/untrusted/irt/irt_dev.h"
11 #include "native_client/src/untrusted/irt/irt_extension.h"
12 #include "native_client/src/untrusted/nacl/nacl_irt.h"
13
14 /*
15  * The declarations here are used as references for nacl_interface_ext_supply()
16  * to store the interface table pointers into. They should all be declared as
17  * __attribute__((weak)) so that the references to the pointers will be NULL
18  * if the user is has not linked in the definition. This makes it so we don't
19  * link in extra definitions that are not utilized.
20  */
21
22 /* Declarations are listed in the same order as in irt.h. */
23 extern struct nacl_irt_basic __libnacl_irt_basic __attribute__((weak));
24 extern struct nacl_irt_fdio __libnacl_irt_fdio __attribute__((weak));
25 extern struct nacl_irt_memory __libnacl_irt_memory __attribute__((weak));
26 extern struct nacl_irt_thread __libnacl_irt_thread __attribute__((weak));
27 extern struct nacl_irt_futex __libnacl_irt_futex __attribute__((weak));
28 extern struct nacl_irt_clock __libnacl_irt_clock __attribute__((weak));
29
30 /* Declarations are listed in the same order as in irt_dev.h. */
31 extern struct nacl_irt_dev_fdio __libnacl_irt_dev_fdio __attribute__((weak));
32 extern struct nacl_irt_dev_filename __libnacl_irt_dev_filename
33   __attribute__((weak));
34 extern struct nacl_irt_tls __libnacl_irt_tls __attribute__((weak));
35 extern struct nacl_irt_dev_getpid __libnacl_irt_dev_getpid
36   __attribute__((weak));
37
38 /*
39  * The following table provides us a simple way to keep track of all the
40  * interfaces we currently support along with their reference and sizes.
41  */
42 struct nacl_irt_ext_struct {
43   const char *interface_ident;
44   void *table;
45   size_t tablesize;
46 };
47
48 static const struct nacl_irt_ext_struct nacl_irt_ext_structs[] = {
49   /* Interfaces are listed in the same order as in irt.h. */
50   {
51     .interface_ident = NACL_IRT_BASIC_v0_1,
52     .table = &__libnacl_irt_basic,
53     .tablesize = sizeof(__libnacl_irt_basic)
54   }, {
55     .interface_ident = NACL_IRT_FDIO_v0_1,
56     .table = &__libnacl_irt_fdio,
57     .tablesize = sizeof(__libnacl_irt_fdio),
58   }, {
59     .interface_ident = NACL_IRT_MEMORY_v0_3,
60     .table = &__libnacl_irt_memory,
61     .tablesize = sizeof(__libnacl_irt_memory),
62   }, {
63     .interface_ident = NACL_IRT_THREAD_v0_1,
64     .table = &__libnacl_irt_thread,
65     .tablesize = sizeof(__libnacl_irt_thread),
66   }, {
67     .interface_ident = NACL_IRT_FUTEX_v0_1,
68     .table = &__libnacl_irt_futex,
69     .tablesize = sizeof(__libnacl_irt_futex),
70   }, {
71     .interface_ident = NACL_IRT_CLOCK_v0_1,
72     .table = &__libnacl_irt_clock,
73     .tablesize = sizeof(__libnacl_irt_clock)
74   },
75
76   /* Interfaces are listed in the same order as in irt_dev.h. */
77   {
78     .interface_ident = NACL_IRT_DEV_FDIO_v0_3,
79     .table = &__libnacl_irt_dev_fdio,
80     .tablesize = sizeof(__libnacl_irt_dev_fdio),
81   }, {
82     .interface_ident = NACL_IRT_DEV_FILENAME_v0_3,
83     .table = &__libnacl_irt_dev_filename,
84     .tablesize = sizeof(__libnacl_irt_dev_filename),
85   }, {
86     .interface_ident = NACL_IRT_DEV_GETPID_v0_1,
87     .table = &__libnacl_irt_dev_getpid,
88     .tablesize = sizeof(__libnacl_irt_dev_getpid)
89   },
90 };
91
92 size_t nacl_interface_ext_supply(const char *interface_ident,
93                                const void *table, size_t tablesize) {
94   for (size_t i = 0; i < NACL_ARRAY_SIZE(nacl_irt_ext_structs); i++) {
95     if (nacl_irt_ext_structs[i].tablesize == tablesize &&
96         strcmp(nacl_irt_ext_structs[i].interface_ident, interface_ident) == 0) {
97       /*
98        * Since the table is pointing to weak references, it can be NULL which
99        * signifies that the variable is not linked. In that case we should
100        * return 0 signifying that the interface was not supplied.
101        */
102       if (nacl_irt_ext_structs[i].table == NULL)
103         return 0;
104
105       memcpy(nacl_irt_ext_structs[i].table, table, tablesize);
106       return tablesize;
107     }
108   }
109
110   return 0;
111 }