Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 10fe676b authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

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
parent 3b37f9a0
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -121,11 +121,16 @@ struct InputMessage {
            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 InputMessage {

        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);
+2 −2
Original line number Diff line number Diff line
@@ -547,7 +547,7 @@ status_t InputPublisher::receiveFinishedSignal(uint32_t* outSeq, bool* outHandle
        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 @@ status_t InputConsumer::sendUnchainedFinishedSignal(uint32_t seq, bool handled)
    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);
}

+16 −0
Original line number Diff line number Diff line
@@ -73,4 +73,20 @@ void TestInputMessageAlignment() {
  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