Skip to content

Commit

Permalink
Assert that message send/receive is thread-synchronized properly. (#1…
Browse files Browse the repository at this point in the history
…9493)

Sending and receiving of messages needs to happen with the Matter lock
held (i.e. either on the Matter thread, or on some other thread after
it takes the lock).  Add asserts to that effect to catch accidental
misuse, which can lead to data races and data corruption.
  • Loading branch information
bzbarsky-apple authored Jun 13, 2022
1 parent e649f62 commit dc3862a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <messaging/EphemeralExchangeDispatch.h>
#include <messaging/ExchangeContext.h>
#include <messaging/ExchangeMgr.h>
#include <platform/LockTracker.h>
#include <protocols/Protocols.h>
#include <protocols/secure_channel/Constants.h>

Expand Down Expand Up @@ -132,6 +133,10 @@ void ExchangeContext::UpdateSEDIntervalMode(bool activeMode)
CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgType, PacketBufferHandle && msgBuf,
const SendFlags & sendFlags)
{
// This is the first point all outgoing messages funnel through. Ensure
// that our message sends are all synchronized correctly.
assertChipStackLockedByCurrentThread();

bool isStandaloneAck =
(protocolId == Protocols::SecureChannel::Id) && msgType == to_underlying(Protocols::SecureChannel::MsgType::StandaloneAck);
if (!isStandaloneAck)
Expand Down
5 changes: 5 additions & 0 deletions src/transport/TransportMgrBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <transport/TransportMgrBase.h>

#include <lib/support/CodeUtils.h>
#include <platform/LockTracker.h>
#include <transport/TransportMgr.h>
#include <transport/raw/Base.h>

Expand Down Expand Up @@ -57,6 +58,10 @@ CHIP_ERROR TransportMgrBase::MulticastGroupJoinLeave(const Transport::PeerAddres

void TransportMgrBase::HandleMessageReceived(const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msg)
{
// This is the first point all incoming messages funnel through. Ensure
// that our message receipts are all synchronized correctly.
assertChipStackLockedByCurrentThread();

if (msg->HasChainedBuffer())
{
// Something in the lower levels messed up.
Expand Down

0 comments on commit dc3862a

Please sign in to comment.