Skip to content

Commit

Permalink
Merge pull request #14921 from ANR2ME/adhoc_matching
Browse files Browse the repository at this point in the history
[AdhocMatching] Fix some more race condition possibility
  • Loading branch information
hrydgard committed Sep 23, 2021
2 parents 5904fe1 + 4741357 commit bcfdab6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Core/HLE/proAdhoc.cpp
Expand Up @@ -667,6 +667,9 @@ s32_le countChildren(SceNetAdhocMatchingContext * context, const bool excludeTim
*/
SceNetAdhocMatchingMemberInternal * findPeer(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac)
{
if (mac == NULL)
return NULL;

// Iterate Peer List for Matching Target
SceNetAdhocMatchingMemberInternal * peer = context->peerlist;
for (; peer != NULL; peer = peer->next)
Expand Down
26 changes: 23 additions & 3 deletions Core/HLE/sceNetAdhoc.cpp
Expand Up @@ -1809,7 +1809,8 @@ static int sceNetAdhocPdpRecv(int id, void *addr, void * port, void *buf, void *
return WaitBlockingAdhocSocket(threadSocketId, PDP_RECV, id, buf, len, timeout, saddr, sport, "pdp recv");
}

return hleLogSuccessVerboseX(SCENET, ERROR_NET_ADHOC_WOULD_BLOCK, "would block");
VERBOSE_LOG(SCENET, "%08x=sceNetAdhocPdpRecv: would block", ERROR_NET_ADHOC_WOULD_BLOCK); // Temporary fix to avoid a crash on the Logs due to trying to Logs syscall's argument from another thread (ie. AdhocMatchingInput thread)
return ERROR_NET_ADHOC_WOULD_BLOCK; // hleLogSuccessVerboseX(SCENET, ERROR_NET_ADHOC_WOULD_BLOCK, "would block");
}

hleEatMicro(50);
Expand Down Expand Up @@ -6136,6 +6137,9 @@ void broadcastHelloMessage(SceNetAdhocMatchingContext * context)
*/
void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt)
{
// Lock the peer
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);

// Find Peer
SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac);

Expand Down Expand Up @@ -6217,6 +6221,9 @@ void sendAcceptPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma
*/
void sendJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt)
{
// Lock the peer
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);

// Find Peer
SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac);

Expand Down Expand Up @@ -6258,6 +6265,9 @@ void sendJoinPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac,
*/
void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int optlen, void * opt)
{
// Lock the peer
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);

// Allocate Cancel Message Buffer
uint8_t * cancel = (uint8_t *)malloc(5LL + optlen);

Expand All @@ -6282,7 +6292,6 @@ void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma
free(cancel);
}

peerlock.lock();
// Find Peer
SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac);

Expand All @@ -6299,7 +6308,6 @@ void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma
// Delete Peer
else deletePeer(context, peer);
}
peerlock.unlock();
}

/**
Expand All @@ -6311,6 +6319,9 @@ void sendCancelPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * ma
*/
void sendBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac, int datalen, void * data)
{
// Lock the peer
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);

// Find Peer
SceNetAdhocMatchingMemberInternal * peer = findPeer(context, mac);

Expand Down Expand Up @@ -6359,6 +6370,9 @@ void sendBulkDataPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr *
*/
void sendBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac)
{
// Lock the peer
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);

// Find Newborn Child
SceNetAdhocMatchingMemberInternal * newborn = findPeer(context, mac);

Expand Down Expand Up @@ -6406,6 +6420,9 @@ void sendBirthPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac
*/
void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac)
{
// Lock the peer
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);

// Find abandoned Child
SceNetAdhocMatchingMemberInternal * deadkid = findPeer(context, mac);

Expand Down Expand Up @@ -6457,6 +6474,9 @@ void sendDeathPacket(SceNetAdhocMatchingContext * context, SceNetEtherAddr * mac
*/
void sendByePacket(SceNetAdhocMatchingContext * context)
{
// Lock the peer
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);

// Iterate Peers
SceNetAdhocMatchingMemberInternal * peer = context->peerlist;
for (; peer != NULL; peer = peer->next)
Expand Down

0 comments on commit bcfdab6

Please sign in to comment.