Validate flatbuffers before printing in aos_dump, log_cat, and simulated_event_loop
We had a corrupted flatbuffer being logged, and the error was very poor
when it was being printed. Let's try to catch this a lot earlier.
Fix 1: log_cat should validate before printing. That'll prevent us
from wasting time debugging why it is crashing and point the finger a
lot better.
Fix 2: aos_dump should do the same. That'll save us from nasty crashes
there too and focus attention a lot better.
Fix 3: SimulatedEventLoop sender Send should also validate in debug
mode. This will avoid too big a performance hit in the fast path, but
will catch corrupted flatbuffers in any simulations we do, or any log
replay that gets done with debug turned on.
This caught a couple of places where we were missing schemas, so add
those in too.
Change-Id: I1873ddd592d33fe4e64210a2e08aa9b937d61ab8
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/events/event_loop_param_test.cc b/aos/events/event_loop_param_test.cc
index ee77b8a..0fd1078 100644
--- a/aos/events/event_loop_param_test.cc
+++ b/aos/events/event_loop_param_test.cc
@@ -1801,7 +1801,8 @@
auto loop2 = MakePrimary();
auto loop3 = Make();
- const std::string kData("971 is the best");
+ const FlatbufferDetachedBuffer<TestMessage> kMessage =
+ JsonToFlatbuffer<TestMessage>("{}");
std::unique_ptr<aos::RawSender> sender =
loop1->MakeRawSender(configuration::GetChannel(
@@ -1811,29 +1812,29 @@
loop3->MakeRawFetcher(configuration::GetChannel(
loop3->configuration(), "/test", "aos.TestMessage", "", nullptr));
- loop2->OnRun(
- [&]() { EXPECT_TRUE(sender->Send(kData.data(), kData.size())); });
+ loop2->OnRun([&]() {
+ EXPECT_TRUE(sender->Send(kMessage.span().data(), kMessage.span().size()));
+ });
bool happened = false;
loop2->MakeRawWatcher(
configuration::GetChannel(loop2->configuration(), "/test",
"aos.TestMessage", "", nullptr),
- [this, &kData, &fetcher, &happened](const Context &context,
- const void *message) {
+ [this, &kMessage, &fetcher, &happened](const Context &context,
+ const void *message) {
happened = true;
- EXPECT_EQ(std::string_view(kData),
- std::string_view(reinterpret_cast<const char *>(message),
- context.size));
- EXPECT_EQ(std::string_view(kData),
- std::string_view(reinterpret_cast<const char *>(context.data),
- context.size));
+ EXPECT_EQ(
+ kMessage.span(),
+ absl::Span<const uint8_t>(
+ reinterpret_cast<const uint8_t *>(message), context.size));
+ EXPECT_EQ(message, context.data);
ASSERT_TRUE(fetcher->Fetch());
- EXPECT_EQ(std::string_view(kData),
- std::string_view(
- reinterpret_cast<const char *>(fetcher->context().data),
- fetcher->context().size));
+ EXPECT_EQ(kMessage.span(),
+ absl::Span<const uint8_t>(reinterpret_cast<const uint8_t *>(
+ fetcher->context().data),
+ fetcher->context().size));
this->Exit();
});
@@ -1850,7 +1851,8 @@
auto loop2 = MakePrimary();
auto loop3 = Make();
- const std::string kData("971 is the best");
+ const FlatbufferDetachedBuffer<TestMessage> kMessage =
+ JsonToFlatbuffer<TestMessage>("{}");
const aos::monotonic_clock::time_point monotonic_remote_time =
aos::monotonic_clock::time_point(chrono::seconds(1501));
@@ -1868,9 +1870,9 @@
loop3->configuration(), "/test", "aos.TestMessage", "", nullptr));
loop2->OnRun([&]() {
- EXPECT_TRUE(sender->Send(kData.data(), kData.size(), monotonic_remote_time,
- realtime_remote_time, remote_queue_index,
- remote_boot_uuid));
+ EXPECT_TRUE(sender->Send(kMessage.span().data(), kMessage.span().size(),
+ monotonic_remote_time, realtime_remote_time,
+ remote_queue_index, remote_boot_uuid));
});
bool happened = false;
@@ -1904,7 +1906,8 @@
TEST_P(AbstractEventLoopTest, RawSenderSentData) {
auto loop1 = MakePrimary();
- const std::string kData("971 is the best");
+ const FlatbufferDetachedBuffer<TestMessage> kMessage =
+ JsonToFlatbuffer<TestMessage>("{}");
std::unique_ptr<aos::RawSender> sender =
loop1->MakeRawSender(configuration::GetChannel(
@@ -1913,7 +1916,7 @@
const aos::monotonic_clock::time_point monotonic_now = loop1->monotonic_now();
const aos::realtime_clock::time_point realtime_now = loop1->realtime_now();
- EXPECT_TRUE(sender->Send(kData.data(), kData.size()));
+ EXPECT_TRUE(sender->Send(kMessage.span().data(), kMessage.span().size()));
EXPECT_GE(sender->monotonic_sent_time(), monotonic_now);
EXPECT_LE(sender->monotonic_sent_time(),
@@ -1923,7 +1926,7 @@
realtime_now + chrono::milliseconds(100));
EXPECT_EQ(sender->sent_queue_index(), 0u);
- EXPECT_TRUE(sender->Send(kData.data(), kData.size()));
+ EXPECT_TRUE(sender->Send(kMessage.span().data(), kMessage.span().size()));
EXPECT_GE(sender->monotonic_sent_time(), monotonic_now);
EXPECT_LE(sender->monotonic_sent_time(),