aboutsummaryrefslogtreecommitdiff
path: root/apps/test/chqts/src/shared/send_message.cc
blob: f46417806f388247461c9addf643d0290fc25c7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <shared/send_message.h>

#include <inttypes.h>

#include <shared/abort.h>
#include <shared/dumb_allocator.h>
#include <shared/nano_endian.h>
#include <shared/nano_string.h>

#include <chre.h>

namespace nanoapp_testing {

constexpr size_t kAllocSize = 128;

static DumbAllocator<kAllocSize, 4> gDumbAlloc;

static void freeDumbAllocMessage(void *message, size_t messageSize) {
  if (messageSize > kAllocSize) {
    uint32_t localSize = uint32_t(messageSize);
    sendFatalFailureToHost("freeDumbAllocMessage given oversized message:",
                           &localSize);
  }
  if (!gDumbAlloc.free(message)) {
    uint32_t localPtr =
        reinterpret_cast<size_t>(message) & UINT32_C(0xFFFFFFFF);
    sendFatalFailureToHost("freeDumbAllocMessage given bad pointer:",
                           &localPtr);
  }
}

static void freeHeapMessage(void *message, size_t /* messageSize */) {
  if (gDumbAlloc.contains(message)) {
    uint32_t localPtr =
        reinterpret_cast<size_t>(message) & UINT32_C(0xFFFFFFFF);
    sendFatalFailureToHost("freeHeapMessage given DumbAlloc pointer:",
                           &localPtr);
  }
  chreHeapFree(message);
}

static void fatalError() {
  // Attempt to send a context-less failure message, in the hopes that
  // might get through.
  chreSendMessageToHostEndpoint(nullptr, 0,
                                static_cast<uint32_t>(MessageType::kFailure),
                                CHRE_HOST_ENDPOINT_BROADCAST, nullptr);
  // Whether or not that made it through, unambigiously fail this test
  // by aborting.
  nanoapp_testing::abort();
}

// TODO(b/32114261): Remove this method.
static bool needToPrependMessageType() {
  // TODO: When we have a new API that properly send the messageType,
  //     this method should get the API version and return appropriately.
  //     Eventually we should remove this hacky method.
  return true;
}

static void *getMessageMemory(size_t *size, bool *dumbAlloc) {
  if (needToPrependMessageType()) {
    *size += sizeof(uint32_t);
  }
  void *ret = gDumbAlloc.alloc(*size);
  if (ret != nullptr) {
    *dumbAlloc = true;
  } else {
    // Not expected, but possible if the CHRE is lagging in freeing
    // these messages, or if we're sending a huge message.
    *dumbAlloc = false;
    ret = chreHeapAlloc(static_cast<uint32_t>(*size));
    if (ret == nullptr) {
      fatalError();
    }
  }
  return ret;
}

// TODO(b/32114261): Remove this method.
static void *prependMessageType(MessageType messageType, void *memory) {
  if (!needToPrependMessageType()) {
    return memory;
  }
  uint32_t type =
      nanoapp_testing::hostToLittleEndian(static_cast<uint32_t>(messageType));
  memcpy(memory, &type, sizeof(type));
  uint8_t *ptr = static_cast<uint8_t *>(memory);
  ptr += sizeof(type);
  return ptr;
}

static void internalSendMessage(MessageType messageType, void *data,
                                size_t dataSize, bool dumbAlloc) {
  // Note that if the CHRE implementation occasionally drops a message
  // here, then tests will become flaky.  For now, we consider that to
  // be a flaky CHRE implementation which should fail testing.
  if (!chreSendMessageToHostEndpoint(
          data, dataSize, static_cast<uint32_t>(messageType),
          CHRE_HOST_ENDPOINT_BROADCAST,
          dumbAlloc ? freeDumbAllocMessage : freeHeapMessage)) {
    fatalError();
  }
}

void sendMessageToHost(MessageType messageType, const void *data,
                       size_t dataSize) {
  if ((dataSize == 0) && (data != nullptr)) {
    sendInternalFailureToHost("Bad sendMessageToHost args");
  }
  bool dumbAlloc = true;
  size_t fullMessageSize = dataSize;
  void *myMessageBase = getMessageMemory(&fullMessageSize, &dumbAlloc);
  void *ptr = prependMessageType(messageType, myMessageBase);
  memcpy(ptr, data, dataSize);
  internalSendMessage(messageType, myMessageBase, fullMessageSize, dumbAlloc);
}

void sendStringToHost(MessageType messageType, const char *message,
                      const uint32_t *value) {
  if (message == nullptr) {
    sendInternalFailureToHost("sendStringToHost 'message' is NULL");
  }
  bool dumbAlloc = true;
  const size_t messageStrlen = strlen(message);
  size_t myMessageLen = messageStrlen;
  if (value != nullptr) {
    myMessageLen += kUint32ToHexAsciiBufferMinLen;
  }
  // Add null terminator
  myMessageLen++;

  size_t fullMessageLen = myMessageLen;
  char *fullMessage =
      static_cast<char *>(getMessageMemory(&fullMessageLen, &dumbAlloc));
  char *ptr = static_cast<char *>(prependMessageType(messageType, fullMessage));
  memcpy(ptr, message, messageStrlen);
  ptr += messageStrlen;
  if (value != nullptr) {
    uint32ToHexAscii(
        ptr, fullMessageLen - static_cast<size_t>(ptr - fullMessage), *value);
  }
  // Add the terminator.
  fullMessage[fullMessageLen - 1] = '\0';

  internalSendMessage(messageType, fullMessage, fullMessageLen, dumbAlloc);
}

// Before we abort the nanoapp, we also put this message in the chreLog().
// We have no assurance our message will make it to the Host (not required
// for CHRE implementations), but this will at least make sure our message
// hits the log.
static void logFatalMessage(const char *message, const uint32_t *value) {
  if (value != nullptr) {
    chreLog(CHRE_LOG_ERROR, "TEST ABORT: %s0x%08" PRIX32, message, *value);
  } else {
    chreLog(CHRE_LOG_ERROR, "TEST ABORT: %s", message);
  }
}

void sendFatalFailureToHost(const char *message, const uint32_t *value,
                            AbortBlame reason) {
  sendFailureToHost(message, value);
  logFatalMessage(message, value);
  nanoapp_testing::abort(reason);
}

void sendFatalFailureToHostUint8(const char *message, uint8_t value) {
  uint32_t val = value;
  sendFatalFailureToHost(message, &val);
}

void sendInternalFailureToHost(const char *message, const uint32_t *value,
                               AbortBlame reason) {
  sendStringToHost(MessageType::kInternalFailure, message, value);
  logFatalMessage(message, value);
  nanoapp_testing::abort(reason);
}

}  // namespace nanoapp_testing