1 : /*
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
4 : * found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * Service descriptions.
9 : */
10 :
11 : #include <stdio.h>
12 : #include <stdlib.h>
13 : #include <string.h>
14 :
15 : #include "native_client/src/shared/srpc/nacl_srpc.h"
16 : #include "native_client/src/shared/srpc/nacl_srpc_internal.h"
17 :
18 : #ifndef SIZE_T_MAX
19 : # define SIZE_T_MAX (~((size_t) 0))
20 : #endif
21 :
22 :
23 : /*
24 : * The struct used to describe methods within services.
25 : */
26 : struct NaClSrpcMethodDesc {
27 : char const *name;
28 : char const *input_types;
29 : char const *output_types;
30 : /**
31 : * function pointer used to process calls to the named method
32 : */
33 : NaClSrpcMethod handler;
34 : };
35 : typedef struct NaClSrpcMethodDesc NaClSrpcMethodDesc;
36 :
37 : /*
38 : * Forward declarations for static "built-in" methods.
39 : */
40 : static void ServiceDiscovery(NaClSrpcRpc* rpc,
41 : NaClSrpcArg** in_args,
42 : NaClSrpcArg** out_args,
43 : NaClSrpcClosure* done);
44 :
45 : /*
46 : * Get the next text element (name, input types, or output types). These
47 : * elements are delimited by ':', '\n', or '\0'.
48 : */
49 1332 : static char* GetOneElement(const char* string) {
50 1332 : size_t len = 0;
51 : char* buf;
52 :
53 9219 : while (':' != string[len] && '\n' != string[len] && '\0' != string[len]) {
54 6555 : len++;
55 : }
56 1332 : buf = malloc(len + 1);
57 1332 : if (NULL == buf) {
58 0 : return NULL;
59 : }
60 1332 : strncpy(buf, string, len);
61 1332 : buf[len] = '\0';
62 1332 : return buf;
63 : }
64 :
65 : /* * Get the components of one method description. Returns a pointer to
66 : * the delimiter if successful or NULL if not.
67 : */
68 444 : static const char* ParseOneEntry(const char* entry_fmt,
69 : char** name,
70 : char** input_types,
71 : char** output_types) {
72 444 : char* namebuf = NULL;
73 444 : char* insbuf = NULL;
74 444 : char* outsbuf = NULL;
75 :
76 444 : *name = NULL;
77 444 : *input_types = NULL;
78 444 : *output_types = NULL;
79 :
80 : /* Don't try to parse NULL strings. */
81 444 : if (NULL == entry_fmt) {
82 0 : goto cleanup;
83 : }
84 : /* Get the name into a buffer, and ensure it is followed by ':'. */
85 444 : namebuf = GetOneElement(entry_fmt);
86 444 : if (NULL == namebuf) {
87 0 : goto cleanup;
88 : }
89 444 : entry_fmt += strlen(namebuf);
90 444 : if (':' != *entry_fmt) {
91 0 : goto cleanup;
92 : }
93 444 : entry_fmt++;
94 : /* Get the input types into a buffer, and ensure it is followed by ':'. */
95 444 : insbuf = GetOneElement(entry_fmt);
96 444 : if (NULL == insbuf) {
97 0 : goto cleanup;
98 : }
99 444 : entry_fmt += strlen(insbuf);
100 444 : if (':' != *entry_fmt) {
101 0 : goto cleanup;
102 : }
103 444 : entry_fmt++;
104 : /* Get the output types into a buffer. */
105 444 : outsbuf = GetOneElement(entry_fmt);
106 444 : if (NULL == outsbuf) {
107 0 : goto cleanup;
108 : }
109 444 : entry_fmt += strlen(outsbuf);
110 : /* Copy the result strings out. */
111 444 : *name = namebuf;
112 444 : *input_types = insbuf;
113 444 : *output_types = outsbuf;
114 : /* Return a pointer to the next character after the elements. */
115 444 : return entry_fmt;
116 :
117 : cleanup:
118 0 : free(insbuf);
119 0 : free(namebuf);
120 0 : return NULL;
121 : }
122 :
123 104 : static void FreeMethods(NaClSrpcMethodDesc* methods, uint32_t method_count) {
124 : uint32_t i;
125 :
126 104 : if (NULL == methods) {
127 104 : return;
128 : }
129 424 : for (i = 0; i < method_count; ++i) {
130 321 : if (NULL == methods[i].name) {
131 : /* We have reached the end of the portion set by ParseOneEntry calls. */
132 1 : break;
133 : }
134 320 : free((char*) methods[i].name);
135 320 : free((char*) methods[i].input_types);
136 320 : free((char*) methods[i].output_types);
137 : }
138 104 : free(methods);
139 : }
140 :
141 : /*
142 : * The method tables passed to construction do not contain "intrinsic" methods
143 : * such as service discovery and shutdown. Build a complete table including
144 : * those from a given input.
145 : */
146 79 : static NaClSrpcMethodDesc* BuildMethods(
147 : const struct NaClSrpcHandlerDesc* methods,
148 : uint32_t* method_count) {
149 : static const char* kSDDescString = "service_discovery::C";
150 79 : NaClSrpcMethodDesc* complete_methods = NULL;
151 : uint32_t i;
152 : const char* nul_loc;
153 :
154 : /* Compute the number of methods to export. */
155 79 : *method_count = 0;
156 295 : while (NULL != methods[*method_count].entry_fmt)
157 137 : ++*method_count;
158 : /* Add one extra method for service discovery. */
159 79 : ++*method_count;
160 : /* Allocate the method descriptors. One extra for NULL termination. */
161 79 : complete_methods = (NaClSrpcMethodDesc*)
162 79 : calloc(*method_count + 1, sizeof(*complete_methods));
163 79 : if (NULL == complete_methods) {
164 0 : return NULL;
165 : }
166 : /* Copy the methods passed in, adding service discovery as element zero. */
167 79 : nul_loc = ParseOneEntry(kSDDescString,
168 79 : (char**) &complete_methods[0].name,
169 79 : (char**) &complete_methods[0].input_types,
170 79 : (char**) &complete_methods[0].output_types);
171 79 : if (nul_loc == NULL) {
172 0 : goto cleanup;
173 : }
174 79 : complete_methods[0].handler = ServiceDiscovery;
175 216 : for (i = 0; i < *method_count - 1; ++i) {
176 137 : nul_loc = ParseOneEntry(methods[i].entry_fmt,
177 137 : (char**) &complete_methods[i + 1].name,
178 137 : (char**) &complete_methods[i + 1].input_types,
179 137 : (char**) &complete_methods[i + 1].output_types);
180 137 : if (nul_loc == NULL) {
181 0 : goto cleanup;
182 : }
183 137 : complete_methods[i + 1].handler = methods[i].handler;
184 : }
185 : /* Add the NULL terminator */
186 79 : complete_methods[*method_count].name = NULL;
187 79 : complete_methods[*method_count].input_types = NULL;
188 79 : complete_methods[*method_count].output_types = NULL;
189 79 : complete_methods[*method_count].handler = NULL;
190 : /* Return the array */
191 79 : return complete_methods;
192 :
193 : cleanup:
194 0 : FreeMethods(complete_methods, *method_count);
195 0 : return NULL;
196 : }
197 :
198 : /*
199 : * Builds a service discovery string from an array of method descriptions.
200 : */
201 79 : static char* BuildSDString(const NaClSrpcMethodDesc* methods,
202 : uint32_t method_count,
203 : nacl_abi_size_t *length) {
204 : uint32_t i;
205 : char* p;
206 : char* str;
207 :
208 : /* Find the total length of the method description strings. */
209 79 : *length = 1;
210 295 : for (i = 0; i < method_count; ++i) {
211 432 : *length += nacl_abi_size_t_saturate(
212 432 : strlen(methods[i].name) + 1 +
213 432 : strlen(methods[i].input_types) + 1 +
214 216 : strlen(methods[i].output_types) + 1);
215 : }
216 : /* Allocate the string. */
217 79 : str = (char*) malloc(*length + 1);
218 79 : if (NULL == str) {
219 0 : return NULL;
220 : }
221 : /* Concatenate the method description strings into the string. */
222 79 : p = str;
223 295 : for (i = 0; i < method_count; ++i) {
224 216 : size_t buf_limit = str + *length - p;
225 : /* TODO(robertm): explore use portability_io.h */
226 : #if NACL_WINDOWS
227 : p += _snprintf(p, buf_limit, "%s:%s:%s\n",
228 : methods[i].name,
229 : methods[i].input_types,
230 : methods[i].output_types);
231 : #else
232 648 : p += snprintf(p, buf_limit, "%s:%s:%s\n",
233 216 : methods[i].name,
234 216 : methods[i].input_types,
235 216 : methods[i].output_types);
236 : #endif
237 : }
238 79 : *p = '\0';
239 : /* Return the resulting string. */
240 79 : return str;
241 : }
242 :
243 : /*
244 : * Create a service descriptor from an array of methods.
245 : */
246 79 : int NaClSrpcServiceHandlerCtor(NaClSrpcService* service,
247 : const NaClSrpcHandlerDesc* handler_desc) {
248 : char* service_str;
249 : nacl_abi_size_t str_length;
250 79 : NaClSrpcMethodDesc* methods = NULL;
251 79 : uint32_t method_count = 0;
252 :
253 : /* Initialize the struct, so that failures can be cleaned up properly. */
254 79 : service->service_string = NULL;
255 79 : service->service_string_length = 0;
256 79 : service->rpc_descr = NULL;
257 79 : service->rpc_count = 0;
258 : /* Add the service_discovery method to the table. */
259 79 : methods = BuildMethods(handler_desc, &method_count);
260 79 : if (NULL == methods) {
261 0 : goto cleanup;
262 : }
263 79 : service_str = BuildSDString(methods, method_count, &str_length);
264 79 : if (NULL == service_str) {
265 0 : goto cleanup;
266 : }
267 79 : service->service_string = service_str;
268 79 : service->service_string_length = str_length;
269 79 : service->rpc_descr = methods;
270 79 : service->rpc_count = method_count;
271 79 : return 1;
272 : cleanup:
273 0 : FreeMethods(methods, method_count);
274 0 : return 0;
275 : }
276 :
277 48 : int NaClSrpcServiceStringCtor(NaClSrpcService* service, const char* str) {
278 48 : NaClSrpcMethodDesc* methods = NULL;
279 : const char* p;
280 : uint32_t i;
281 : uint32_t rpc_count;
282 : size_t rpc_count_size_t;
283 :
284 : /* Initialize the struct, so that failures can be cleaned up properly. */
285 48 : service->service_string = NULL;
286 48 : service->service_string_length = 0;
287 48 : service->rpc_descr = NULL;
288 48 : service->rpc_count = 0;
289 : /* Count the number of rpc methods */
290 48 : rpc_count = 0;
291 324 : for (p = str; *p != '\0'; ) {
292 228 : char* next_p = strchr(p, '\n');
293 228 : if (NULL == next_p) {
294 : /* malformed input -- no remaining \n character was found */
295 0 : goto cleanup;
296 : }
297 228 : p = next_p + 1;
298 228 : ++rpc_count;
299 228 : if (0 == rpc_count) {
300 : /* uint32_t overflow detected. */
301 0 : goto cleanup;
302 : }
303 : }
304 : /*
305 : * The front end knows the next comparison is useless due to the range of
306 : * uint32_t. And furthermore at least one version of gcc knows that a
307 : * cast doesn't really change that fact. Hence, assign to a new variable
308 : * of size_t type.
309 : */
310 48 : rpc_count_size_t = (size_t) rpc_count;
311 : /* Allocate and clear the descriptor array */
312 48 : if (rpc_count_size_t >= SIZE_T_MAX / sizeof(*methods)) {
313 0 : goto cleanup;
314 : }
315 48 : methods = (NaClSrpcMethodDesc*) malloc(rpc_count_size_t * sizeof(*methods));
316 48 : if (NULL == methods) {
317 0 : goto cleanup;
318 : }
319 48 : memset(methods, 0, rpc_count_size_t * sizeof(*methods));
320 : /* Parse the list of method descriptions */
321 48 : p = str;
322 276 : for (i = 0; i < rpc_count; ++i) {
323 : const char* newline_loc;
324 :
325 228 : newline_loc = ParseOneEntry(p,
326 228 : (char**) &methods[i].name,
327 228 : (char**) &methods[i].input_types,
328 228 : (char**) &methods[i].output_types);
329 228 : if (NULL == newline_loc || '\n' != *newline_loc) {
330 : goto cleanup;
331 : }
332 228 : p = newline_loc + 1;
333 : /* The handler is not set from service_discovery strings */
334 228 : methods[i].handler = NULL;
335 : }
336 48 : service->service_string = strdup(str);
337 48 : service->service_string_length = nacl_abi_size_t_saturate(strlen(str));
338 48 : service->rpc_descr = methods;
339 48 : service->rpc_count = rpc_count;
340 48 : return 1;
341 :
342 : cleanup:
343 0 : FreeMethods(methods, rpc_count);
344 0 : return 0;
345 : }
346 :
347 : /*
348 : * Destroy a service descriptor.
349 : */
350 213 : void NaClSrpcServiceDtor(NaClSrpcService* service) {
351 213 : if (NULL == service) {
352 324 : return;
353 : }
354 : /* Free the method descriptors. */
355 103 : FreeMethods((NaClSrpcMethodDesc*) service->rpc_descr, service->rpc_count);
356 : /* Free the service discovery string. */
357 104 : free((char*) service->service_string);
358 : }
359 :
360 0 : void NaClSrpcServicePrint(const NaClSrpcService *service) {
361 : uint32_t i;
362 :
363 0 : if (NULL == service) {
364 0 : printf("NULL service\n");
365 0 : return;
366 : }
367 0 : printf("RPC %-20s %-10s %-10s\n", "Name", "Input args", "Output args");
368 0 : for (i = 0; i < service->rpc_count; ++i) {
369 0 : printf("%3u %-20s %-10s %-10s\n",
370 : (unsigned int) i,
371 0 : service->rpc_descr[i].name,
372 0 : service->rpc_descr[i].input_types,
373 0 : service->rpc_descr[i].output_types);
374 : }
375 : }
376 :
377 0 : uint32_t NaClSrpcServiceMethodCount(const NaClSrpcService *service) {
378 0 : if (NULL == service) {
379 0 : return 0;
380 : }
381 0 : return service->rpc_count;
382 : }
383 :
384 463 : static int SignatureMatches(NaClSrpcMethodDesc* method_desc,
385 : char const* signature) {
386 : struct {
387 : char const* field;
388 : char terminator;
389 : } matcher[3];
390 : size_t ix;
391 :
392 463 : matcher[0].field = method_desc->name;
393 463 : matcher[0].terminator = ':';
394 463 : matcher[1].field = method_desc->input_types;
395 463 : matcher[1].terminator = ':';
396 463 : matcher[2].field = method_desc->output_types;
397 463 : matcher[2].terminator = '\0';
398 :
399 832 : for (ix = 0; ix < sizeof(matcher) / sizeof(matcher[0]); ++ix) {
400 709 : size_t length = strlen(matcher[ix].field);
401 1085 : if (strncmp(matcher[ix].field, signature, length) ||
402 376 : matcher[ix].terminator != signature[length]) {
403 340 : return 0;
404 : }
405 369 : signature += length + 1;
406 : }
407 123 : return 1;
408 : }
409 :
410 123 : uint32_t NaClSrpcServiceMethodIndex(const NaClSrpcService* service,
411 : char const* signature) {
412 : uint32_t i;
413 :
414 123 : if (NULL == service) {
415 0 : return kNaClSrpcInvalidMethodIndex;
416 : }
417 463 : for (i = 0; i < service->rpc_count; ++i) {
418 463 : if (SignatureMatches(&(service->rpc_descr[i]), signature)) {
419 123 : return i;
420 : }
421 : }
422 0 : return kNaClSrpcInvalidMethodIndex;
423 : }
424 :
425 456 : int NaClSrpcServiceMethodNameAndTypes(const NaClSrpcService* service,
426 : uint32_t rpc_number,
427 : const char** name,
428 : const char** input_types,
429 : const char** output_types) {
430 456 : if (rpc_number >= service->rpc_count) {
431 : /* This ensures that the method is in the user-defined set. */
432 0 : return 0;
433 : } else {
434 456 : *name = service->rpc_descr[rpc_number].name;
435 456 : *input_types = service->rpc_descr[rpc_number].input_types;
436 456 : *output_types = service->rpc_descr[rpc_number].output_types;
437 : }
438 456 : return 1;
439 : }
440 :
441 68 : NaClSrpcMethod NaClSrpcServiceMethod(const NaClSrpcService* service,
442 : uint32_t rpc_number) {
443 68 : if (NULL == service || rpc_number >= service->rpc_count) {
444 0 : return NULL;
445 : } else {
446 68 : return service->rpc_descr[rpc_number].handler;
447 : }
448 : }
449 :
450 26 : static void ServiceDiscovery(NaClSrpcRpc* rpc,
451 : NaClSrpcArg** in_args,
452 : NaClSrpcArg** out_args,
453 : NaClSrpcClosure* done) {
454 : UNREFERENCED_PARAMETER(in_args);
455 26 : rpc->result = NACL_SRPC_RESULT_APP_ERROR;
456 26 : if (NULL == rpc->channel || NULL == rpc->channel->server) {
457 0 : done->Run(done);
458 26 : return;
459 : }
460 26 : if (out_args[0]->u.count >= rpc->channel->server->service_string_length) {
461 52 : strncpy(out_args[0]->arrays.carr,
462 26 : rpc->channel->server->service_string,
463 26 : rpc->channel->server->service_string_length);
464 : /* Set the length of the string actually returned. */
465 26 : out_args[0]->u.count = rpc->channel->server->service_string_length;
466 26 : rpc->result = NACL_SRPC_RESULT_OK;
467 : }
468 26 : done->Run(done);
469 : }
|