msgbuf: Rework error handling to improve soundness
Design the implementation to uphold the invariant that a non-negative
remaining value implies the cursor pointer is valid, and that under
other conditions error values must be observed by the msgbuf user. The
former is tested with assertions in the implementation. The latter is
enforced by construction.
With this change, all msgbuf instances for which
pldm_msgbuf_init_errno() succeeds must be either completed or discarded
by calls to the pldm_msgbuf_complete*() or pldm_msgbuf_discard() APIs
respectively.
We then build on the properties that:
- pldm_msgbuf_init_errno() is marked with the warn_unused_result
function attribute
- pldm_msgbuf_init_errno() returns errors for invalid buffer
configurations
- The complete and discard APIs are marked with the warn_unused_result
function attribute
- The complete APIs test for negative remaining values and return an
error if encountered.
- The discard API propagates the provided error code
Together these provide the foundation to ensure that buffer access
errors are (eventually) detected.
A msgbuf object is always in one of the uninitialized, valid, invalid,
or completed states. The states are defined as follows:
- Uninitialized: Undefined values for remaining and cursor
- Valid: cursor points to a valid object, remaining is both non-negative
and describes a range contained within the object pointed to
by cursor
- Invalid: The value of remaining is negative. The value of cursor is
unspecified.
- Completed: the value of remaining is INTMAX_MIN and cursor is NULL
msgbuf instances must always be in the completed state by the time
their storage is reclaimed. To enforce this, PLDM_MSGBUF_DEFINE_P()
is introduced both to simplify definition of related variables, and
to exploit the compiler's 'cleanup' attribute. The cleanup function
associated with the msgbuf object asserts that the referenced object is
in the completed state.
From there, update the implementations of the msgbuf APIs such that
exceeding implementation type limits forces the msgbuf object to the
invalid state (in addition to returning an error value) to relieve the
caller from testing the result of all API invocations.
Change-Id: I4d78ddc5f567d4148f2f6d8f3e7570e97c316bbb
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/tests/dsp/platform.cpp b/tests/dsp/platform.cpp
index 9b3a43b..2f88ee4 100644
--- a/tests/dsp/platform.cpp
+++ b/tests/dsp/platform.cpp
@@ -467,8 +467,7 @@
alignas(pldm_msg) unsigned char data[sizeof(pldm_msg_hdr) +
PLDM_GET_PDR_MIN_RESP_BYTES +
sizeof(recordData) - 1 + 1];
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
int rc;
pldm_msg* msg = new (data) pldm_msg;
@@ -709,8 +708,7 @@
data[sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES];
uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
int rc;
pldm_msg* msg = new (data) pldm_msg;
@@ -1466,12 +1464,11 @@
PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
EXPECT_EQ(rc, PLDM_SUCCESS);
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(
buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload,
PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
- EXPECT_EQ(rc, 0);
+ ASSERT_EQ(rc, 0);
uint8_t retFormatVersion;
uint8_t retTransferOperationFlag;
@@ -1505,12 +1502,11 @@
PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
EXPECT_EQ(rc, PLDM_SUCCESS);
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(
buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload,
PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
- EXPECT_EQ(rc, 0);
+ ASSERT_EQ(rc, 0);
uint8_t retFormatVersion;
uint8_t retTransferOperationFlag;
@@ -1544,12 +1540,11 @@
PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
EXPECT_EQ(rc, PLDM_SUCCESS);
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(
buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload,
PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
- EXPECT_EQ(rc, 0);
+ ASSERT_EQ(rc, 0);
uint8_t retFormatVersion;
uint8_t retTransferOperationFlag;
@@ -2017,12 +2012,11 @@
eventDataIntegrityChecksum, response, payloadLength);
EXPECT_EQ(rc, PLDM_SUCCESS);
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(
buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
response->payload, payloadLength);
- EXPECT_EQ(rc, 0);
+ ASSERT_EQ(rc, 0);
uint8_t retCompletionCode;
uint8_t retTid = 0;
@@ -2045,6 +2039,7 @@
sizeof(retEventData));
ASSERT_EQ(rc, 0);
pldm_msgbuf_extract_uint32(buf, retEventDataIntegrityChecksum);
+ ASSERT_EQ(pldm_msgbuf_complete(buf), 0);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(retCompletionCode, completionCode);
@@ -2056,8 +2051,6 @@
EXPECT_EQ(retEventDataSize, eventDataSize);
EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum);
EXPECT_EQ(0, memcmp(pEventData, retEventData, eventDataSize));
-
- EXPECT_EQ(pldm_msgbuf_complete(buf), PLDM_SUCCESS);
}
TEST(PollForPlatformEventMessage, testGoodEncodeResposeP2)
@@ -2078,12 +2071,11 @@
response, payloadLength);
EXPECT_EQ(rc, PLDM_SUCCESS);
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(
buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
response->payload, payloadLength);
- EXPECT_EQ(rc, 0);
+ ASSERT_EQ(rc, 0);
uint8_t retCompletionCode;
uint8_t retTid = 0;
@@ -2092,12 +2084,12 @@
pldm_msgbuf_extract_uint8(buf, retCompletionCode);
pldm_msgbuf_extract_uint8(buf, retTid);
pldm_msgbuf_extract_uint16(buf, retEventId);
+ ASSERT_EQ(pldm_msgbuf_complete(buf), 0);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(retCompletionCode, completionCode);
EXPECT_EQ(retTid, tId);
EXPECT_EQ(retEventId, eventId);
- EXPECT_EQ(pldm_msgbuf_complete(buf), PLDM_SUCCESS);
}
TEST(PollForPlatformEventMessage, testGoodEncodeResposeP3)
@@ -2118,12 +2110,11 @@
response, payloadLength);
EXPECT_EQ(rc, PLDM_SUCCESS);
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(
buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
response->payload, payloadLength);
- EXPECT_EQ(rc, 0);
+ ASSERT_EQ(rc, 0);
uint8_t retCompletionCode;
uint8_t retTid = 0;
@@ -2132,12 +2123,12 @@
pldm_msgbuf_extract_uint8(buf, retCompletionCode);
pldm_msgbuf_extract_uint8(buf, retTid);
pldm_msgbuf_extract_uint16(buf, retEventId);
+ ASSERT_EQ(pldm_msgbuf_complete(buf), PLDM_SUCCESS);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(retCompletionCode, completionCode);
EXPECT_EQ(retTid, tId);
EXPECT_EQ(retEventId, eventId);
- EXPECT_EQ(pldm_msgbuf_complete(buf), PLDM_SUCCESS);
}
TEST(PollForPlatformEventMessage, testGoodEncodeResposeP4)
@@ -2167,12 +2158,11 @@
eventDataIntegrityChecksum, response, payloadLength);
EXPECT_EQ(rc, PLDM_SUCCESS);
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(
buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
response->payload, payloadLength);
- EXPECT_EQ(rc, 0);
+ ASSERT_EQ(rc, 0);
uint8_t retCompletionCode;
uint8_t retTid = 0;
@@ -2191,6 +2181,7 @@
pldm_msgbuf_extract_uint8(buf, retEventClass);
pldm_msgbuf_extract_uint32(buf, retEventDataSize);
pldm_msgbuf_extract_uint32(buf, retEventDataIntegrityChecksum);
+ EXPECT_EQ(pldm_msgbuf_complete(buf), 0);
EXPECT_EQ(rc, PLDM_SUCCESS);
EXPECT_EQ(retCompletionCode, completionCode);
@@ -2201,8 +2192,6 @@
EXPECT_EQ(retEventClass, eventClass);
EXPECT_EQ(retEventDataSize, eventDataSize);
EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum);
-
- EXPECT_EQ(pldm_msgbuf_complete(buf), PLDM_SUCCESS);
}
TEST(PollForPlatformEventMessage, testBadEncodeResponse)
@@ -2336,8 +2325,7 @@
static constexpr const uint8_t eventData = 34;
static constexpr const uint8_t Tid = 0x03;
struct pldm_platform_event_message_req req;
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
size_t len;
void* data;
@@ -2664,27 +2652,26 @@
EXPECT_EQ(rc, PLDM_SUCCESS);
- struct pldm_msgbuf _buf;
- struct pldm_msgbuf* buf = &_buf;
+ PLDM_MSGBUF_DEFINE_P(buf);
rc = pldm_msgbuf_init_errno(
buf, PLDM_MSG_POLL_EVENT_LENGTH,
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<uint8_t*>(eventData.data()), eventData.size());
- EXPECT_EQ(rc, 0);
+ ASSERT_EQ(rc, 0);
uint8_t retFormatVersion;
uint16_t reteventID;
uint32_t retDataTransferHandle;
- EXPECT_EQ(pldm_msgbuf_extract_uint8(buf, retFormatVersion), PLDM_SUCCESS);
- EXPECT_EQ(pldm_msgbuf_extract_uint16(buf, reteventID), PLDM_SUCCESS);
- EXPECT_EQ(pldm_msgbuf_extract_uint32(buf, retDataTransferHandle),
- PLDM_SUCCESS);
+ pldm_msgbuf_extract_uint8(buf, retFormatVersion);
+ pldm_msgbuf_extract_uint16(buf, reteventID);
+ pldm_msgbuf_extract_uint32(buf, retDataTransferHandle);
+ ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), PLDM_SUCCESS);
+
EXPECT_EQ(retFormatVersion, poll_event.format_version);
EXPECT_EQ(reteventID, poll_event.event_id);
EXPECT_EQ(retDataTransferHandle, poll_event.data_transfer_handle);
- EXPECT_EQ(pldm_msgbuf_complete_consumed(buf), PLDM_SUCCESS);
}
#endif