Add static asserts for InputMessage sizes
We currently don't test the total size of InputMessage.
But that's still important, to ensure it doesn't extend further than
expected.
Add asserts for the sizes of Header and Body.
Bug: none
Test: presubmit
Change-Id: I2dbda2136186b22a9247fde058a9c356b07c808d
diff --git a/include/input/InputTransport.h b/include/input/InputTransport.h
index 94d90ad..d39ee25 100644
--- a/include/input/InputTransport.h
+++ b/include/input/InputTransport.h
@@ -121,11 +121,16 @@
float yCursorPosition;
uint32_t pointerCount;
uint32_t empty3;
- // Note that PointerCoords requires 8 byte alignment.
+ /**
+ * The "pointers" field must be the last field of the struct InputMessage.
+ * When we send the struct InputMessage across the socket, we are not
+ * writing the entire "pointers" array, but only the pointerCount portion
+ * of it as an optimization. Adding a field after "pointers" would break this.
+ */
struct Pointer {
PointerProperties properties;
PointerCoords coords;
- } pointers[MAX_POINTERS];
+ } pointers[MAX_POINTERS] __attribute__((aligned(8)));
int32_t getActionId() const {
uint32_t index = (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
@@ -141,7 +146,7 @@
struct Finished {
uint32_t seq;
- bool handled;
+ uint32_t handled; // actually a bool, but we must maintain 8-byte alignment
inline size_t size() const {
return sizeof(Finished);
diff --git a/libs/input/InputTransport.cpp b/libs/input/InputTransport.cpp
index 8d1dd63..b7937dc 100644
--- a/libs/input/InputTransport.cpp
+++ b/libs/input/InputTransport.cpp
@@ -547,7 +547,7 @@
return UNKNOWN_ERROR;
}
*outSeq = msg.body.finished.seq;
- *outHandled = msg.body.finished.handled;
+ *outHandled = msg.body.finished.handled == 1;
return OK;
}
@@ -1065,7 +1065,7 @@
InputMessage msg;
msg.header.type = InputMessage::Type::FINISHED;
msg.body.finished.seq = seq;
- msg.body.finished.handled = handled;
+ msg.body.finished.handled = handled ? 1 : 0;
return mChannel->sendMessage(&msg);
}
diff --git a/libs/input/tests/StructLayout_test.cpp b/libs/input/tests/StructLayout_test.cpp
index 8d8cf06..0fb6cfc 100644
--- a/libs/input/tests/StructLayout_test.cpp
+++ b/libs/input/tests/StructLayout_test.cpp
@@ -73,4 +73,20 @@
CHECK_OFFSET(InputMessage::Body::Finished, handled, 4);
}
+void TestHeaderSize() {
+ static_assert(sizeof(InputMessage::Header) == 8);
+}
+
+/**
+ * We cannot use the Body::size() method here because it is not static for
+ * the Motion type, where "pointerCount" variable affects the size and can change at runtime.
+ */
+void TestBodySize() {
+ static_assert(sizeof(InputMessage::Body::Key) == 64);
+ static_assert(sizeof(InputMessage::Body::Motion) ==
+ offsetof(InputMessage::Body::Motion, pointers) +
+ sizeof(InputMessage::Body::Motion::Pointer) * MAX_POINTERS);
+ static_assert(sizeof(InputMessage::Body::Finished) == 8);
+}
+
} // namespace android