Align all shared memory offsets to word boundary.
Since the read pointer, write pointer, data buffer and
EventFlag word occupy contiguous memory locations as per the default
MessageQueue/MQDescriptor constructors, ensure that all
offsets are aligned.
Bug: 34194487
Test: FMQ unit tests
Change-Id: I050d60f5481a13b8e45a42d00d6217bf7fcd7651
diff --git a/base/include/hidl/MQDescriptor.h b/base/include/hidl/MQDescriptor.h
index ed9279a..8cdbd4c 100644
--- a/base/include/hidl/MQDescriptor.h
+++ b/base/include/hidl/MQDescriptor.h
@@ -21,6 +21,7 @@
#include <cutils/native_handle.h>
#include <hidl/HidlSupport.h>
#include <utils/NativeHandle.h>
+#include <utils/Log.h>
namespace android {
namespace hardware {
@@ -107,6 +108,18 @@
* needed for blocking FMQ operations.
*/
static constexpr int32_t kMinGrantorCountForEvFlagSupport = EVFLAGWORDPOS + 1;
+
+ //TODO(b/34160777) Identify a better solution that supports remoting.
+ static inline size_t alignToWordBoundary(size_t length) {
+ constexpr size_t kAlignmentSize = 64;
+ LOG_ALWAYS_FATAL_IF(kAlignmentSize % __WORDSIZE != 0, "Incompatible word size");
+ return (length + kAlignmentSize/8 - 1) & ~(kAlignmentSize/8 - 1U);
+ }
+
+ static inline size_t isAlignedToWordBoundary(size_t offset) {
+ constexpr size_t kAlignmentSize = 64;
+ return (offset & (kAlignmentSize/8 - 1)) == 0;
+ }
private:
::android::hardware::hidl_vec<GrantorDescriptor> mGrantors;
::android::hardware::details::hidl_pointer<native_handle_t> mHandle;
@@ -144,6 +157,8 @@
mFlags(flavor) {
mGrantors.resize(grantors.size());
for (size_t i = 0; i < grantors.size(); ++i) {
+ LOG_ALWAYS_FATAL_IF(isAlignedToWordBoundary(grantors[i].offset) == false,
+ "Grantor offsets need to be aligned");
mGrantors[i] = grantors[i];
}
}
@@ -176,7 +191,7 @@
mGrantors[grantorPos] = {
0 /* grantor flags */,
0 /* fdIndex */,
- static_cast<uint32_t>(offset),
+ static_cast<uint32_t>(alignToWordBoundary(offset)),
memSize[grantorPos]
};
}