diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-07-01 11:02:56 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2021-07-01 11:02:56 +0000 |
commit | 99966e53402fd125bb2361e3169d4c038c50b83e (patch) | |
tree | 3a5659cdf3bf27abcc5e8d19ab9af69b86eb7b96 | |
parent | c5f45ad9857245da5d1698d13b2a62f465ec9155 (diff) | |
parent | 45024bd34e962de81059d6124c4fb8f740705319 (diff) | |
download | libfmq-android11-d2-release.tar.gz |
Merge cherrypicks of [15172584, 15172286, 15172594, 15172385, 15172631, 15172632, 15172633, 15172634, 15171221, 15171222, 15171223, 15171541, 15171542, 15171668, 15172342, 15170823, 15170824, 15170825, 15170826, 15170827, 15170828, 15170829, 15172650, 15172651, 15172652, 15172653, 15172654, 15172655, 15172656, 15172657, 15172658, 15172659, 15172660, 15172661, 15172386, 15172387, 15172388, 15172389, 15172670, 15172553, 15172589, 15172343, 15172617, 15172618, 15172619, 15172620, 15172690, 15172691, 15172692, 15172693, 15172694, 15172695, 15172696, 15172697, 15171708, 15171709, 15172554, 15172555, 15172710, 15172621, 15172635, 15172636, 15172698, 15172699, 15172700, 15171543, 15172701, 15172702, 15172703, 15172704, 15171669, 15172622] into rvc-d2-releaseandroid-11.0.0_r48android-11.0.0_r47android-11.0.0_r45android-11.0.0_r44android11-d2-release
Change-Id: Icfab112dffe6a6e4a71568292774a914cd1b8ad0
-rw-r--r-- | include/fmq/MessageQueue.h | 20 | ||||
-rw-r--r-- | tests/msgq_test_client.cpp | 37 |
2 files changed, 52 insertions, 5 deletions
diff --git a/include/fmq/MessageQueue.h b/include/fmq/MessageQueue.h index ca031aa..97fee0e 100644 --- a/include/fmq/MessageQueue.h +++ b/include/fmq/MessageQueue.h @@ -115,7 +115,7 @@ struct MessageQueue { * * @return Whether the write was successful. */ - bool write(const T* data, size_t count); + __attribute__((noinline)) bool write(const T* data, size_t count); /** * Perform a blocking write of 'count' items into the FMQ using EventFlags. @@ -170,7 +170,7 @@ struct MessageQueue { * * @return Whether the read was successful. */ - bool read(T* data, size_t count); + __attribute__((noinline)) bool read(T* data, size_t count); /** * Perform a blocking read operation of 'count' items from the FMQ. Does not @@ -378,7 +378,7 @@ struct MessageQueue { * @return Whether it is possible to write 'nMessages' items of type T * into the FMQ. */ - bool beginWrite(size_t nMessages, MemTransaction* memTx) const; + __attribute__((always_inline)) bool beginWrite(size_t nMessages, MemTransaction* memTx) const; /** * Commit a write of size 'nMessages'. To be only used after a call to beginWrite(). @@ -402,7 +402,7 @@ struct MessageQueue { * @return bool Whether it is possible to read 'nMessages' items of type T * from the FMQ. */ - bool beginRead(size_t nMessages, MemTransaction* memTx) const; + __attribute__((always_inline)) bool beginRead(size_t nMessages, MemTransaction* memTx) const; /** * Commit a read of size 'nMessages'. To be only used after a call to beginRead(). @@ -996,6 +996,12 @@ bool MessageQueue<T, flavor>::beginWrite(size_t nMessages, MemTransaction* resul } auto writePtr = mWritePtr->load(std::memory_order_relaxed); + if (writePtr % sizeof(T) != 0) { + hardware::details::logError( + "The write pointer has become misaligned. Writing to the queue is no longer " + "possible."); + return false; + } size_t writeOffset = writePtr % mDesc->getSize(); /* @@ -1083,6 +1089,12 @@ bool MessageQueue<T, flavor>::beginRead(size_t nMessages, MemTransaction* result * stores to mReadPtr from a different thread. */ auto readPtr = mReadPtr->load(std::memory_order_relaxed); + if (writePtr % sizeof(T) != 0 || readPtr % sizeof(T) != 0) { + hardware::details::logError( + "The write or read pointer has become misaligned. Reading from the queue is no " + "longer possible."); + return false; + } if (writePtr - readPtr > mDesc->getSize()) { mReadPtr->store(writePtr, std::memory_order_release); diff --git a/tests/msgq_test_client.cpp b/tests/msgq_test_client.cpp index ce7e340..271586a 100644 --- a/tests/msgq_test_client.cpp +++ b/tests/msgq_test_client.cpp @@ -72,7 +72,7 @@ protected: } virtual void SetUp() { - static constexpr size_t kNumElementsInQueue = 1024; + static constexpr size_t kNumElementsInQueue = (PAGE_SIZE - 16) / sizeof(uint16_t); mService = waitGetTestService(); ASSERT_NE(mService, nullptr); ASSERT_TRUE(mService->isRemote()); @@ -424,6 +424,41 @@ TEST_F(SynchronizedReadWriteClient, SmallInputReaderTest1) { } /* + * Request mService to write a message to the queue followed by a beginRead(). + * Get a pointer to the memory region for the that first message. Set the write + * counter to the last byte in the ring buffer. Request another write from + * mService. The write should fail because the write address is misaligned. + */ +TEST_F(SynchronizedReadWriteClient, MisalignedWriteCounter) { + const size_t dataLen = 1; + bool ret = mService->requestWriteFmqSync(dataLen); + ASSERT_TRUE(ret); + // begin read and get a MemTransaction object for the first object in the queue + MessageQueueSync::MemTransaction tx; + ASSERT_TRUE(mQueue->beginRead(dataLen, &tx)); + // get a pointer to the beginning of the ring buffer + const auto& region = tx.getFirstRegion(); + uint16_t* firstStart = region.getAddress(); + + // because this is the first location in the ring buffer, we can get + // access to the read and write pointer stored in the fd. 8 bytes back for the + // write counter and 16 bytes back for the read counter + uint64_t* writeCntr = (uint64_t*)((uint8_t*)firstStart - 8); + + // set it to point to the very last byte in the ring buffer + *(writeCntr) = mQueue->getQuantumCount() * mQueue->getQuantumSize() - 1; + ASSERT_TRUE(*writeCntr % sizeof(uint16_t) != 0); + + // this is not actually necessary, but it's the expected the pattern. + mQueue->commitRead(dataLen); + + // This next write will be misaligned and will overlap outside of the ring buffer. + // The write should fail. + ret = mService->requestWriteFmqSync(dataLen); + EXPECT_FALSE(ret); +} + +/* * Request mService to write a small number of messages * to the FMQ. Read and verify each message using * beginRead/Commit read APIs. |