Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #include "aos/events/shm_event_loop.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 2 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 3 | #include <string_view> |
| 4 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 5 | #include "aos/events/event_loop_param_test.h" |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 6 | #include "aos/realtime.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "glog/logging.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 8 | #include "gtest/gtest.h" |
| 9 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "aos/events/test_message_generated.h" |
Jim Ostrowski | 2192ddb | 2020-06-24 19:07:31 -0700 | [diff] [blame] | 11 | #include "aos/network/team_number.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 13 | namespace aos { |
| 14 | namespace testing { |
| 15 | namespace { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 16 | namespace chrono = ::std::chrono; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 17 | |
| 18 | class ShmEventLoopTestFactory : public EventLoopTestFactory { |
| 19 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | ShmEventLoopTestFactory() { |
| 21 | // Put all the queue files in ${TEST_TMPDIR} if it is set, otherwise |
| 22 | // everything will be reusing /dev/shm when sharded. |
| 23 | char *test_tmpdir = getenv("TEST_TMPDIR"); |
| 24 | if (test_tmpdir != nullptr) { |
| 25 | FLAGS_shm_base = std::string(test_tmpdir) + "/aos"; |
| 26 | } |
| 27 | |
| 28 | // Clean up anything left there before. |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 29 | unlink((FLAGS_shm_base + "/test/aos.TestMessage.v4").c_str()); |
| 30 | unlink((FLAGS_shm_base + "/test1/aos.TestMessage.v4").c_str()); |
| 31 | unlink((FLAGS_shm_base + "/test2/aos.TestMessage.v4").c_str()); |
| 32 | unlink((FLAGS_shm_base + "/test2/aos.TestMessage.v4").c_str()); |
| 33 | unlink((FLAGS_shm_base + "/aos/aos.timing.Report.v4").c_str()); |
| 34 | unlink((FLAGS_shm_base + "/aos/aos.logging.LogMessageFbs.v4").c_str()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 37 | ~ShmEventLoopTestFactory() { FLAGS_override_hostname = ""; } |
| 38 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 39 | ::std::unique_ptr<EventLoop> Make(std::string_view name) override { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 40 | if (configuration()->has_nodes()) { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 41 | FLAGS_override_hostname = |
| 42 | std::string(my_node()->hostname()->string_view()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 43 | } |
| 44 | ::std::unique_ptr<ShmEventLoop> loop(new ShmEventLoop(configuration())); |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 45 | loop->set_name(name); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 46 | return std::move(loop); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 49 | ::std::unique_ptr<EventLoop> MakePrimary(std::string_view name) override { |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 50 | if (configuration()->has_nodes()) { |
Austin Schuh | 898f497 | 2020-01-11 17:21:25 -0800 | [diff] [blame] | 51 | FLAGS_override_hostname = |
| 52 | std::string(my_node()->hostname()->string_view()); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 53 | } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 54 | ::std::unique_ptr<ShmEventLoop> loop = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop(configuration())); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 56 | primary_event_loop_ = loop.get(); |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 57 | loop->set_name(name); |
| 58 | return std::move(loop); |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); } |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 62 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 63 | void Exit() override { CHECK_NOTNULL(primary_event_loop_)->Exit(); } |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 64 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 65 | void SleepFor(::std::chrono::nanoseconds duration) override { |
| 66 | ::std::this_thread::sleep_for(duration); |
| 67 | } |
| 68 | |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 69 | private: |
Austin Schuh | 44019f9 | 2019-05-19 19:58:27 -0700 | [diff] [blame] | 70 | ::aos::ShmEventLoop *primary_event_loop_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 73 | auto CommonParameters() { |
| 74 | return ::testing::Combine( |
| 75 | ::testing::Values([]() { return new ShmEventLoopTestFactory(); }), |
| 76 | ::testing::Values(ReadMethod::COPY, ReadMethod::PIN), |
| 77 | ::testing::Values(DoTimingReports::kYes, DoTimingReports::kNo)); |
| 78 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 79 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 80 | INSTANTIATE_TEST_CASE_P(ShmEventLoopCommonTest, AbstractEventLoopTest, |
| 81 | CommonParameters()); |
Brian Silverman | 7716297 | 2020-08-12 19:52:40 -0700 | [diff] [blame] | 82 | |
Austin Schuh | 6bae825 | 2021-02-07 22:01:49 -0800 | [diff] [blame] | 83 | INSTANTIATE_TEST_CASE_P(ShmEventLoopCommonDeathTest, AbstractEventLoopDeathTest, |
| 84 | CommonParameters()); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 85 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 86 | } // namespace |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 87 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 88 | bool IsRealtime() { |
| 89 | int scheduler; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | PCHECK((scheduler = sched_getscheduler(0)) != -1); |
| 91 | |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 92 | { |
| 93 | // If we are RT, logging the scheduler will crash us. Mark that we just |
| 94 | // don't care. |
| 95 | aos::ScopedNotRealtime nrt; |
| 96 | LOG(INFO) << "scheduler is " << scheduler; |
| 97 | } |
| 98 | |
| 99 | const bool result = scheduler == SCHED_FIFO || scheduler == SCHED_RR; |
| 100 | // Confirm that the scheduler matches AOS' interpretation of if we are |
| 101 | // realtime or not. |
| 102 | if (result) { |
| 103 | aos::CheckRealtime(); |
| 104 | } else { |
| 105 | aos::CheckNotRealtime(); |
| 106 | } |
| 107 | return result; |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 108 | } |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 109 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 110 | class ShmEventLoopTest : public ::testing::TestWithParam<ReadMethod> { |
| 111 | public: |
| 112 | ShmEventLoopTest() { |
| 113 | if (GetParam() == ReadMethod::PIN) { |
| 114 | factory_.PinReads(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | ShmEventLoopTestFactory *factory() { return &factory_; } |
| 119 | |
| 120 | private: |
| 121 | ShmEventLoopTestFactory factory_; |
| 122 | }; |
| 123 | |
| 124 | using ShmEventLoopDeathTest = ShmEventLoopTest; |
| 125 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 126 | // Tests that every handler type is realtime and runs. There are threads |
| 127 | // involved and it's easy to miss one. |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 128 | TEST_P(ShmEventLoopTest, AllHandlersAreRealtime) { |
| 129 | auto loop = factory()->MakePrimary("primary"); |
| 130 | auto loop2 = factory()->Make("loop2"); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 131 | |
| 132 | loop->SetRuntimeRealtimePriority(1); |
| 133 | |
| 134 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 135 | |
| 136 | bool did_onrun = false; |
| 137 | bool did_timer = false; |
| 138 | bool did_watcher = false; |
| 139 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 140 | auto timer = loop->AddTimer([this, &did_timer]() { |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 141 | EXPECT_TRUE(IsRealtime()); |
| 142 | did_timer = true; |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 143 | factory()->Exit(); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 144 | }); |
| 145 | |
| 146 | loop->MakeWatcher("/test", [&did_watcher](const TestMessage &) { |
| 147 | EXPECT_TRUE(IsRealtime()); |
| 148 | did_watcher = true; |
| 149 | }); |
| 150 | |
| 151 | loop->OnRun([&loop, &did_onrun, &sender, timer]() { |
| 152 | EXPECT_TRUE(IsRealtime()); |
| 153 | did_onrun = true; |
| 154 | timer->Setup(loop->monotonic_now() + chrono::milliseconds(100)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 155 | |
| 156 | aos::Sender<TestMessage>::Builder msg = sender.MakeBuilder(); |
| 157 | TestMessage::Builder builder = msg.MakeBuilder<TestMessage>(); |
| 158 | builder.add_value(200); |
| 159 | msg.Send(builder.Finish()); |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 160 | }); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 161 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 162 | factory()->Run(); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 163 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 164 | EXPECT_TRUE(did_onrun); |
| 165 | EXPECT_TRUE(did_timer); |
| 166 | EXPECT_TRUE(did_watcher); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 167 | } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 168 | |
| 169 | // Tests that missing a deadline inside the function still results in PhasedLoop |
| 170 | // running at the right offset. |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 171 | TEST_P(ShmEventLoopTest, DelayedPhasedLoop) { |
| 172 | auto loop1 = factory()->MakePrimary("primary"); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 173 | |
| 174 | ::std::vector<::aos::monotonic_clock::time_point> times; |
| 175 | |
| 176 | constexpr chrono::milliseconds kOffset = chrono::milliseconds(400); |
| 177 | |
| 178 | loop1->AddPhasedLoop( |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 179 | [this, ×, &loop1, &kOffset](int count) { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 180 | const ::aos::monotonic_clock::time_point monotonic_now = |
| 181 | loop1->monotonic_now(); |
| 182 | |
| 183 | // Compute our offset. |
| 184 | const ::aos::monotonic_clock::duration remainder = |
| 185 | monotonic_now.time_since_epoch() - |
| 186 | chrono::duration_cast<chrono::seconds>( |
| 187 | monotonic_now.time_since_epoch()); |
| 188 | |
| 189 | // Make sure we we are called near where we should be even when we |
| 190 | // delay. |
| 191 | constexpr chrono::milliseconds kEpsilon(200); |
| 192 | EXPECT_LT(remainder, kOffset + kEpsilon); |
| 193 | EXPECT_GT(remainder, kOffset - kEpsilon); |
| 194 | |
| 195 | // Confirm that we see the missed count when we sleep. |
| 196 | if (times.size() == 0) { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 197 | CHECK_EQ(count, 1); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 198 | } else { |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 199 | CHECK_EQ(count, 3); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | times.push_back(loop1->monotonic_now()); |
| 203 | if (times.size() == 2) { |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 204 | factory()->Exit(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Now, add a large delay. This should push us up to 3 cycles. |
| 208 | ::std::this_thread::sleep_for(chrono::milliseconds(2500)); |
| 209 | }, |
| 210 | chrono::seconds(1), kOffset); |
| 211 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 212 | factory()->Run(); |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 213 | |
| 214 | EXPECT_EQ(times.size(), 2u); |
| 215 | } |
| 216 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 217 | // Test GetWatcherSharedMemory in a few basic scenarios. |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 218 | TEST_P(ShmEventLoopDeathTest, GetWatcherSharedMemory) { |
| 219 | auto generic_loop1 = factory()->MakePrimary("primary"); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 220 | ShmEventLoop *const loop1 = static_cast<ShmEventLoop *>(generic_loop1.get()); |
| 221 | const auto channel = configuration::GetChannel( |
| 222 | loop1->configuration(), "/test", TestMessage::GetFullyQualifiedName(), |
| 223 | loop1->name(), loop1->node()); |
| 224 | |
| 225 | // First verify it handles an invalid channel reasonably. |
| 226 | EXPECT_DEATH(loop1->GetWatcherSharedMemory(channel), |
| 227 | "No watcher found for channel"); |
| 228 | |
| 229 | // Then, actually create a watcher, and verify it returns something sane. |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 230 | absl::Span<const char> shared_memory; |
| 231 | bool ran = false; |
| 232 | loop1->MakeWatcher("/test", [this, &shared_memory, |
| 233 | &ran](const TestMessage &message) { |
| 234 | EXPECT_FALSE(ran); |
| 235 | ran = true; |
| 236 | // If we're using pinning, then we can verify that the message is actually |
| 237 | // in the specified region. |
| 238 | if (GetParam() == ReadMethod::PIN) { |
| 239 | EXPECT_GE(reinterpret_cast<const char *>(&message), |
| 240 | shared_memory.begin()); |
| 241 | EXPECT_LT(reinterpret_cast<const char *>(&message), shared_memory.end()); |
| 242 | } |
| 243 | factory()->Exit(); |
| 244 | }); |
| 245 | shared_memory = loop1->GetWatcherSharedMemory(channel); |
| 246 | EXPECT_FALSE(shared_memory.empty()); |
| 247 | |
| 248 | auto loop2 = factory()->Make("sender"); |
| 249 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 250 | generic_loop1->OnRun([&sender]() { |
| 251 | auto builder = sender.MakeBuilder(); |
| 252 | TestMessage::Builder test_builder(*builder.fbb()); |
| 253 | test_builder.add_value(1); |
| 254 | CHECK(builder.Send(test_builder.Finish())); |
| 255 | }); |
| 256 | factory()->Run(); |
| 257 | EXPECT_TRUE(ran); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 260 | TEST_P(ShmEventLoopTest, GetSenderSharedMemory) { |
| 261 | auto generic_loop1 = factory()->MakePrimary("primary"); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 262 | ShmEventLoop *const loop1 = static_cast<ShmEventLoop *>(generic_loop1.get()); |
| 263 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 264 | // Check that GetSenderSharedMemory returns non-null/non-empty memory span. |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 265 | auto sender = loop1->MakeSender<TestMessage>("/test"); |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 266 | const absl::Span<char> shared_memory = loop1->GetSenderSharedMemory(&sender); |
| 267 | EXPECT_FALSE(shared_memory.empty()); |
| 268 | |
| 269 | auto builder = sender.MakeBuilder(); |
| 270 | uint8_t *buffer; |
| 271 | builder.fbb()->CreateUninitializedVector(5, 1, &buffer); |
| 272 | EXPECT_GE(reinterpret_cast<char *>(buffer), shared_memory.begin()); |
| 273 | EXPECT_LT(reinterpret_cast<char *>(buffer), shared_memory.end()); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 276 | TEST_P(ShmEventLoopTest, GetFetcherPrivateMemory) { |
| 277 | auto generic_loop1 = factory()->MakePrimary("primary"); |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 278 | ShmEventLoop *const loop1 = static_cast<ShmEventLoop *>(generic_loop1.get()); |
| 279 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 280 | // Check that GetFetcherPrivateMemory returns non-null/non-empty memory span. |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 281 | auto fetcher = loop1->MakeFetcher<TestMessage>("/test"); |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 282 | const auto private_memory = loop1->GetFetcherPrivateMemory(&fetcher); |
| 283 | EXPECT_FALSE(private_memory.empty()); |
| 284 | |
| 285 | auto loop2 = factory()->Make("sender"); |
| 286 | auto sender = loop2->MakeSender<TestMessage>("/test"); |
| 287 | { |
| 288 | auto builder = sender.MakeBuilder(); |
| 289 | TestMessage::Builder test_builder(*builder.fbb()); |
| 290 | test_builder.add_value(1); |
| 291 | CHECK(builder.Send(test_builder.Finish())); |
| 292 | } |
| 293 | |
| 294 | ASSERT_TRUE(fetcher.Fetch()); |
| 295 | EXPECT_GE(fetcher.context().data, private_memory.begin()); |
| 296 | EXPECT_LT(fetcher.context().data, private_memory.end()); |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 299 | // Tests that corrupting the bytes around the data buffer results in a crash. |
| 300 | TEST_P(ShmEventLoopDeathTest, OutOfBoundsWrite) { |
| 301 | auto loop1 = factory()->Make("loop1"); |
| 302 | std::unique_ptr<aos::RawSender> sender = |
| 303 | loop1->MakeRawSender(configuration::GetChannel( |
| 304 | loop1->configuration(), "/test", "aos.TestMessage", "", nullptr)); |
| 305 | for (size_t i = 0; i < kChannelDataRedzone; ++i) { |
| 306 | SCOPED_TRACE(std::to_string(i)); |
| 307 | EXPECT_DEATH( |
| 308 | { |
| 309 | ++static_cast<char *>(sender->data())[-1 - i]; |
| 310 | sender->Send(0); |
| 311 | }, |
| 312 | "Somebody wrote outside the buffer of their message"); |
| 313 | EXPECT_DEATH( |
| 314 | { |
| 315 | ++static_cast<char *>(sender->data())[sender->size() + i]; |
| 316 | sender->Send(0); |
| 317 | }, |
| 318 | "Somebody wrote outside the buffer of their message"); |
| 319 | } |
| 320 | } |
| 321 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 322 | // TODO(austin): Test that missing a deadline with a timer recovers as expected. |
| 323 | |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 324 | INSTANTIATE_TEST_CASE_P(ShmEventLoopCopyTest, ShmEventLoopTest, |
| 325 | ::testing::Values(ReadMethod::COPY)); |
| 326 | INSTANTIATE_TEST_CASE_P(ShmEventLoopPinTest, ShmEventLoopTest, |
| 327 | ::testing::Values(ReadMethod::PIN)); |
| 328 | INSTANTIATE_TEST_CASE_P(ShmEventLoopCopyDeathTest, ShmEventLoopDeathTest, |
| 329 | ::testing::Values(ReadMethod::COPY)); |
| 330 | INSTANTIATE_TEST_CASE_P(ShmEventLoopPinDeathTest, ShmEventLoopDeathTest, |
| 331 | ::testing::Values(ReadMethod::PIN)); |
| 332 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 333 | } // namespace testing |
| 334 | } // namespace aos |