Skip to content

Commit

Permalink
[core] Initialize CRYSPR in startup() (#2425)
Browse files Browse the repository at this point in the history
Multiple threads can initialize the SPR at one time. This is a particular problem with the MbedTLS implementation as some additional static initialization performed for this SPR can lead to a crash.
  • Loading branch information
oviano authored Aug 9, 2022
1 parent 1b30573 commit 7f12138
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ int srt::CUDTUnited::startup()
throw CUDTException(MJ_SETUP, MN_NONE, WSAGetLastError());
#endif

CCryptoControl::globalInit();

PacketFilter::globalInit();

if (m_bGCStatus)
Expand Down
1 change: 1 addition & 0 deletions srtcore/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class CUDTUnited
friend class CUDT;
friend class CUDTGroup;
friend class CRendezvousQueue;
friend class CCryptoControl;

public:
CUDTUnited();
Expand Down
10 changes: 10 additions & 0 deletions srtcore/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ written by
#include "crypto.h"
#include "logging.h"
#include "core.h"
#include "api.h"

using namespace srt_logging;

Expand Down Expand Up @@ -69,6 +70,15 @@ std::string KmStateStr(SRT_KM_STATE state)

using srt_logging::KmStateStr;

void srt::CCryptoControl::globalInit()
{
#ifdef SRT_ENABLE_ENCRYPTION
// We need to force the Cryspr to be initialized during startup to avoid the
// possibility of multiple threads initialzing the same static data later on.
HaiCryptCryspr_Get_Instance();
#endif
}

#if ENABLE_LOGGING
std::string srt::CCryptoControl::FormatKmMessage(std::string hdr, int cmd, size_t srtlen)
{
Expand Down
2 changes: 2 additions & 0 deletions srtcore/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class CCryptoControl

public:

static void globalInit();

bool sendingAllowed()
{
// This function is called to state as to whether the
Expand Down

0 comments on commit 7f12138

Please sign in to comment.