blob: b8c36dcffc3c3776582f5a4b92ca2aad539be514 [file] [log] [blame]
// Copyright Microsoft and CHERIoT Contributors.
// SPDX-License-Identifier: MIT
#include "compartment.h"
#include "token.h"
#include <cstdlib>
#define TEST_NAME "MessageQueue"
#include "tests.hh"
#include <FreeRTOS-Compat/queue.h>
#include <debug.hh>
#include <errno.h>
#include <queue.h>
#include <timeout.h>
static constexpr size_t ItemSize = 8;
static constexpr size_t MaxItems = 2;
static constexpr char Message[MaxItems][ItemSize] = {"TstMsg0", "TstMsg1"};
extern "C" ErrorRecoveryBehaviour
compartment_error_handler(ErrorState *frame, size_t mcause, size_t mtval)
{
debug_log("Thread {} error handler invoked with mcause {}. PCC: {}",
thread_id_get(),
mcause,
frame->pcc);
return ErrorRecoveryBehaviour::ForceUnwind;
}
void test_queue_unsealed()
{
char bytes[ItemSize];
static MessageQueue *queue;
Timeout timeout{0, 0};
debug_log("Testing queue send operations");
auto checkSpace = [&](size_t expected,
SourceLocation loc = SourceLocation::current()) {
size_t items;
queue_items_remaining(queue, &items);
TEST(items == expected,
"MessageQueue test line {} reports {} items, should contain {}",
loc.line(),
items,
expected);
};
int rv =
queue_create(&timeout, MALLOC_CAPABILITY, &queue, ItemSize, MaxItems);
TEST(queue->elementSize == ItemSize,
"MessageQueue element size is {}, expected {}",
queue->elementSize,
ItemSize);
TEST(queue->queueSize == MaxItems,
"MessageQueue size is {}, expected {}",
queue->queueSize,
MaxItems);
TEST(rv == 0, "MessageQueue creation failed with {}", rv);
rv = queue_send(&timeout, queue, Message[0]);
checkSpace(1);
TEST(rv == 0, "Sending the first message failed with {}", rv);
checkSpace(1);
rv = queue_send(&timeout, queue, Message[1]);
TEST(rv == 0, "Sending the second message failed with {}", rv);
checkSpace(2);
// MessageQueue is full, it should time out.
timeout.remaining = 5;
rv = queue_send(&timeout, queue, Message[1]);
TEST(rv == -ETIMEDOUT,
"Sending to a full queue didn't time out as expected, returned {}",
rv);
checkSpace(2);
debug_log("Testing queue receive operations");
timeout.remaining = 10;
rv = queue_receive(&timeout, queue, bytes);
TEST(rv == 0, "Receiving the first message failed with {}", rv);
TEST(memcmp(Message[0], bytes, ItemSize) == 0,
"First message received but not as expected. Got {}",
bytes);
checkSpace(1);
rv = queue_receive(&timeout, queue, bytes);
TEST(rv == 0, "Receiving the second message failed with {}", rv);
TEST(memcmp(Message[1], bytes, ItemSize) == 0,
"Second message received but not as expected. Got {}",
bytes);
checkSpace(0);
timeout.remaining = 5;
rv = queue_receive(&timeout, queue, bytes);
TEST(
rv == -ETIMEDOUT,
"Receiving from an empty queue didn't time out as expected, returned {}",
rv);
// Check that the items remaining calculations are correct after overflow.
queue_send(&timeout, queue, Message[1]);
checkSpace(1);
queue_receive(&timeout, queue, bytes);
checkSpace(0);
queue_send(&timeout, queue, Message[1]);
checkSpace(1);
queue_receive(&timeout, queue, bytes);
checkSpace(0);
rv = queue_destroy(MALLOC_CAPABILITY, queue);
TEST(rv == 0, "MessageQueue deletion failed with {}", rv);
debug_log("All queue library tests successful");
}
void test_queue_sealed()
{
auto heapSpace = heap_quota_remaining(MALLOC_CAPABILITY);
Timeout t{1};
SObj receiveHandle;
SObj sendHandle;
SObj queue;
char bytes[ItemSize];
int ret =
queue_create_sealed(&t, MALLOC_CAPABILITY, &queue, ItemSize, MaxItems);
TEST(ret == 0, "MessageQueue creation failed with {}", ret);
ret = queue_receive_handle_create_sealed(
&t, MALLOC_CAPABILITY, queue, &receiveHandle);
TEST(
ret == 0, "MessageQueue receive endpoint creation failed with {}", ret);
ret = queue_send_handle_create_sealed(
&t, MALLOC_CAPABILITY, queue, &sendHandle);
TEST(ret == 0, "MessageQueue send endpoint creation failed with {}", ret);
t = UnlimitedTimeout;
ret = queue_send_sealed(&t, receiveHandle, Message[1]);
TEST(
ret == -EINVAL,
"Sending with a receive handle should return -EINVAL ({}), returned {}",
EINVAL,
ret);
ret = queue_receive_sealed(&t, sendHandle, bytes);
TEST(
ret == -EINVAL,
"Sending with a receive handle should return -EINVAL ({}), returned {}",
EINVAL,
ret);
ret = queue_send_sealed(&t, sendHandle, Message[1] + 1);
TEST(ret == -EPERM,
"Sending with short buffer should return -EPERM ({}), returned {}",
EPERM,
ret);
ret = queue_send_sealed(&t, sendHandle, Message[1]);
TEST(
ret == 0, "Sending with valid buffer should return 0, returned {}", ret);
ret = queue_receive_sealed(&t, receiveHandle, bytes + 1);
TEST(ret == -EPERM,
"Receiving with short buffer should return -EPERM ({}), returned {}",
EPERM,
ret);
size_t items;
ret = queue_items_remaining_sealed(receiveHandle, &items);
TEST(ret == 0, "Getting items remaining should return 0, returned {}", ret);
TEST(items == 1, "Items remaining should be 1, is {}", items);
ret = queue_items_remaining_sealed(sendHandle, &items);
TEST(ret == 0, "Getting items remaining should return 0, returned {}", ret);
TEST(items == 1, "Items remaining should be 1, is {}", items);
ret = queue_receive_sealed(&t, receiveHandle, bytes);
TEST(ret == 0,
"Receiving with valid buffer should return 0, returned {}",
ret);
// Put something in the queue before we delete the send handle.
ret = queue_send_sealed(&t, sendHandle, Message[1]);
TEST(
ret == 0, "Sending with valid buffer should return 0, returned {}", ret);
t = 1;
ret = queue_destroy_sealed(&t, MALLOC_CAPABILITY, sendHandle);
TEST(ret == 0, "MessageQueue send destruction failed with {}", ret);
t = 1;
ret = queue_destroy_sealed(&t, MALLOC_CAPABILITY, receiveHandle);
TEST(ret == 0, "MessageQueue receive destruction failed with {}", ret);
ret = queue_destroy_sealed(&t, MALLOC_CAPABILITY, queue);
TEST(ret == 0, "MessageQueue destruction failed with {}", ret);
TEST(heap_quota_remaining(MALLOC_CAPABILITY) == heapSpace,
"Heap space leaked");
debug_log("All queue compartment tests successful");
}
void test_queue_freertos()
{
debug_log("Testing FreeRTOS queues");
auto quotaBegin = heap_quota_remaining(MALLOC_CAPABILITY);
auto freertosQueue = xQueueCreate(10, sizeof(int));
vQueueDelete(freertosQueue);
auto quotaEnd = heap_quota_remaining(MALLOC_CAPABILITY);
TEST(
quotaBegin == quotaEnd,
"The FreeRTOS queue wrapper leaks memory: quota before is {}, after {}",
quotaBegin,
quotaEnd);
debug_log("All FreeRTOS queue tests successful");
}
int test_queue()
{
test_queue_unsealed();
test_queue_sealed();
test_queue_freertos();
debug_log("All queue tests successful");
return 0;
}