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 : // NaCl inter-module communication primitives.
9 :
10 : // Used for UINT32_MAX
11 : #if !NACL_WINDOWS
12 : # ifndef __STDC_LIMIT_MACROS
13 : # define __STDC_LIMIT_MACROS
14 : # endif
15 : #include <stdint.h>
16 : #endif
17 :
18 : // TODO(robertm): stdio.h is included for NULL only - find a better way
19 : #include <stdio.h>
20 :
21 : #include "native_client/src/include/portability.h"
22 :
23 : #include "native_client/src/shared/imc/nacl_imc.h"
24 : #include "native_client/src/shared/platform/nacl_log.h"
25 :
26 : namespace nacl {
27 :
28 : static int InitHeader(IOVec *vec,
29 : MessageHeader *header,
30 : const void* buffer,
31 0 : size_t length) {
32 0 : vec->base = const_cast<void*>(buffer);
33 : if (length > UINT32_MAX) {
34 : return -1;
35 : }
36 0 : vec->length = static_cast<unsigned int>(length);
37 :
38 0 : header->iov = vec;
39 0 : header->iov_length = 1;
40 0 : header->handles = NULL;
41 0 : header->handle_count = 0;
42 0 : header->flags = 0;
43 :
44 0 : return 0;
45 : }
46 :
47 0 : int Send(Handle socket, const void* buffer, size_t length, int flags) {
48 : MessageHeader header;
49 : IOVec vec;
50 : int retval;
51 :
52 0 : retval = InitHeader(&vec, &header, buffer, length);
53 0 : if (retval) {
54 0 : return retval;
55 : }
56 :
57 0 : return SendDatagram(socket, &header, flags);
58 : }
59 :
60 0 : int Receive(Handle socket, void* buffer, size_t length, int flags) {
61 : MessageHeader header;
62 : IOVec vec;
63 : int retval;
64 :
65 0 : retval = InitHeader(&vec, &header, buffer, length);
66 0 : if (retval) {
67 0 : return retval;
68 : }
69 :
70 0 : return ReceiveDatagram(socket, &header, flags);
71 : }
72 :
73 19474 : bool MessageSizeIsValid(const MessageHeader *message) {
74 19474 : size_t cur_bytes = 0;
75 : static size_t const kMax = static_cast<size_t>(~static_cast<uint32_t>(0));
76 : /* we assume that sizeof(uint32_t) <= sizeof(size_t) */
77 :
78 59046 : for (size_t ix = 0; ix < message->iov_length; ++ix) {
79 39572 : if (kMax - cur_bytes < message->iov[ix].length) {
80 0 : return false;
81 : }
82 39572 : cur_bytes += message->iov[ix].length; /* no overflow is possible */
83 : }
84 19474 : return true;
85 : }
86 :
87 : } // namespace nacl
|