Unverified 提交 1e3bdac0 authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi 提交者: GitHub

Thread and memory safety fixes (#562)

上级 f2f8a5fc
......@@ -63,6 +63,7 @@ namespace Channel
virtual ~ChannelSocket();
public:
void Close();
void SetListener(Listener* listener);
void Send(json& jsonMessage);
void SendLog(char* message, size_t messageLen);
......
......@@ -135,7 +135,7 @@ public:
static const int64_t pid;
thread_local static Channel::ChannelSocket* channel;
static const size_t bufferSize {50000};
static char buffer[];
thread_local static char buffer[];
};
/* Logging macros. */
......
......@@ -11,6 +11,10 @@ public:
explicit MediaSoupError(const char* description) : std::runtime_error(description)
{
}
public:
static const size_t bufferSize{ 2000 };
thread_local static char buffer[];
};
class MediaSoupTypeError : public MediaSoupError
......@@ -26,44 +30,32 @@ public:
do \
{ \
MS_ERROR("throwing MediaSoupError: " desc, ##__VA_ARGS__); \
\
static char buffer[2000]; \
\
std::snprintf(buffer, 2000, desc, ##__VA_ARGS__); \
throw MediaSoupError(buffer); \
std::snprintf(MediaSoupError::buffer, MediaSoupError::bufferSize, desc, ##__VA_ARGS__); \
throw MediaSoupError(MediaSoupError::buffer); \
} while (false)
#define MS_THROW_ERROR_STD(desc, ...) \
do \
{ \
MS_ERROR_STD("throwing MediaSoupError: " desc, ##__VA_ARGS__); \
\
static char buffer[2000]; \
\
std::snprintf(buffer, 2000, desc, ##__VA_ARGS__); \
throw MediaSoupError(buffer); \
std::snprintf(MediaSoupError::buffer, MediaSoupError::bufferSize, desc, ##__VA_ARGS__); \
throw MediaSoupError(MediaSoupError::buffer); \
} while (false)
#define MS_THROW_TYPE_ERROR(desc, ...) \
do \
{ \
MS_ERROR("throwing MediaSoupTypeError: " desc, ##__VA_ARGS__); \
\
static char buffer[2000]; \
\
std::snprintf(buffer, 2000, desc, ##__VA_ARGS__); \
throw MediaSoupTypeError(buffer); \
std::snprintf(MediaSoupError::buffer, MediaSoupError::bufferSize, desc, ##__VA_ARGS__); \
throw MediaSoupTypeError(MediaSoupError::buffer); \
} while (false)
#define MS_THROW_TYPE_ERROR_STD(desc, ...) \
do \
{ \
MS_ERROR_STD("throwing MediaSoupTypeError: " desc, ##__VA_ARGS__); \
\
static char buffer[2000]; \
\
std::snprintf(buffer, 2000, desc, ##__VA_ARGS__); \
throw MediaSoupTypeError(buffer); \
std::snprintf(MediaSoupError::buffer, MediaSoupError::bufferSize, desc, ##__VA_ARGS__); \
throw MediaSoupTypeError(MediaSoupError::buffer); \
} while (false)
// clang-format on
......
......@@ -69,6 +69,7 @@ namespace PayloadChannel
virtual ~PayloadChannelSocket();
public:
void Close();
void SetListener(Listener* listener);
void Send(json& jsonMessage, const uint8_t* payload, size_t payloadLen);
void Send(json& jsonMessage);
......
......@@ -45,8 +45,8 @@ namespace RTC
static std::vector<bool>& GetPorts(Transport transport, const std::string& ip);
private:
static std::unordered_map<std::string, std::vector<bool>> mapUdpIpPorts;
static std::unordered_map<std::string, std::vector<bool>> mapTcpIpPorts;
thread_local static std::unordered_map<std::string, std::vector<bool>> mapUdpIpPorts;
thread_local static std::unordered_map<std::string, std::vector<bool>> mapTcpIpPorts;
};
} // namespace RTC
......
......@@ -222,7 +222,7 @@ namespace Utils
static const std::string GetRandomString(size_t len)
{
static char buffer[64];
char buffer[64];
static const char chars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b',
'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
......
......@@ -23,6 +23,7 @@
'src/DepOpenSSL.cpp',
'src/DepUsrSCTP.cpp',
'src/Logger.cpp',
'src/MediaSoupErrors.cpp',
'src/Settings.cpp',
'src/Worker.cpp',
'src/Utils/Crypto.cpp',
......
......@@ -36,6 +36,14 @@ namespace Channel
std::free(this->writeBuffer);
}
void ChannelSocket::Close()
{
MS_TRACE_STD();
this->consumerSocket.Close();
this->producerSocket.Close();
}
void ChannelSocket::SetListener(Listener* listener)
{
MS_TRACE_STD();
......
......@@ -8,7 +8,7 @@
const int64_t Logger::pid{ static_cast<int64_t>(uv_os_getpid()) };
thread_local Channel::ChannelSocket* Logger::channel{ nullptr };
char Logger::buffer[Logger::bufferSize];
thread_local char Logger::buffer[Logger::bufferSize];
/* Class methods. */
......
#define MS_CLASS "MediaSoupError"
#include "MediaSoupErrors.hpp"
thread_local char MediaSoupError::buffer[MediaSoupError::bufferSize];
......@@ -38,6 +38,14 @@ namespace PayloadChannel
delete this->ongoingNotification;
}
void PayloadChannelSocket::Close()
{
MS_TRACE_STD();
this->consumerSocket.Close();
this->producerSocket.Close();
}
void PayloadChannelSocket::SetListener(Listener* listener)
{
MS_TRACE();
......
......@@ -26,8 +26,8 @@ namespace RTC
{
/* Class variables. */
std::unordered_map<std::string, std::vector<bool>> PortManager::mapUdpIpPorts;
std::unordered_map<std::string, std::vector<bool>> PortManager::mapTcpIpPorts;
thread_local std::unordered_map<std::string, std::vector<bool>> PortManager::mapUdpIpPorts;
thread_local std::unordered_map<std::string, std::vector<bool>> PortManager::mapTcpIpPorts;
/* Class methods. */
......
......@@ -1206,8 +1206,8 @@ namespace RTC
// Mangle RTP header extensions.
{
static uint8_t buffer[4096];
static std::vector<RTC::RtpPacket::GenericExtension> extensions;
thread_local static uint8_t buffer[4096];
thread_local static std::vector<RTC::RtpPacket::GenericExtension> extensions;
// This happens just once.
if (extensions.capacity() != 24)
......
......@@ -62,7 +62,7 @@ namespace RTC
this->probationPacket->SetTimestamp(Utils::Crypto::GetRandomUInt(0, 4294967295));
// Add BWE related RTP header extensions.
static uint8_t buffer[4096];
thread_local static uint8_t buffer[4096];
std::vector<RTC::RtpPacket::GenericExtension> extensions;
uint8_t extenLen;
......
......@@ -770,7 +770,7 @@ namespace RTC
if (notification->sn_header.sn_length > 0)
{
static const size_t BufferSize{ 1024 };
static char buffer[BufferSize];
thread_local static char buffer[BufferSize];
uint32_t len = notification->sn_header.sn_length;
......@@ -838,7 +838,7 @@ namespace RTC
if (notification->sn_header.sn_length > 0)
{
static const size_t BufferSize{ 1024 };
static char buffer[BufferSize];
thread_local static char buffer[BufferSize];
uint32_t len = notification->sn_header.sn_length;
......@@ -879,7 +879,7 @@ namespace RTC
case SCTP_REMOTE_ERROR:
{
static const size_t BufferSize{ 1024 };
static char buffer[BufferSize];
thread_local static char buffer[BufferSize];
uint32_t len = notification->sn_remote_error.sre_length - sizeof(struct sctp_remote_error);
......@@ -915,7 +915,7 @@ namespace RTC
case SCTP_SEND_FAILED_EVENT:
{
static const size_t BufferSize{ 1024 };
static char buffer[BufferSize];
thread_local static char buffer[BufferSize];
uint32_t len =
notification->sn_send_failed_event.ssfe_length - sizeof(struct sctp_send_failed_event);
......
......@@ -353,7 +353,7 @@ namespace RTC
}
MS_DUMP(" size: %zu bytes", this->size);
static char transactionId[25];
char transactionId[25];
for (int i{ 0 }; i < 12; ++i)
{
......@@ -385,7 +385,7 @@ namespace RTC
}
if (this->messageIntegrity != nullptr)
{
static char messageIntegrity[41];
char messageIntegrity[41];
for (int i{ 0 }; i < 20; ++i)
{
......
......@@ -83,7 +83,7 @@ namespace Utils
{
MS_TRACE();
static sockaddr_storage addrStorage;
sockaddr_storage addrStorage;
char ipBuffer[INET6_ADDRSTRLEN] = { 0 };
int err;
......
......@@ -19,7 +19,7 @@
/* Static. */
static constexpr size_t BufferOutSize{ 65536 };
static uint8_t BufferOut[BufferOutSize];
thread_local static uint8_t BufferOut[BufferOutSize];
static const uint8_t Base64Table[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
......
......@@ -44,6 +44,12 @@ Worker::~Worker()
{
MS_TRACE();
// Delete the Channel.
delete this->channel;
// Delete the PayloadChannel.
delete this->payloadChannel;
if (!this->closed)
Close();
}
......@@ -70,10 +76,10 @@ void Worker::Close()
this->mapRouters.clear();
// Close the Channel.
delete this->channel;
this->channel->Close();
// Close the PayloadChannel.
delete this->payloadChannel;
this->payloadChannel->Close();
}
void Worker::FillJson(json& jsonObject) const
......
......@@ -113,10 +113,10 @@ UnixStreamSocket::~UnixStreamSocket()
{
MS_TRACE_STD();
delete[] this->buffer;
if (!this->closed)
Close();
delete[] this->buffer;
}
void UnixStreamSocket::Close()
......
......@@ -135,14 +135,14 @@ extern "C" int run_worker(
Worker worker(channel, payloadChannel);
// Free static stuff.
DepLibUV::ClassDestroy();
DepLibSRTP::ClassDestroy();
Utils::Crypto::ClassDestroy();
DepLibWebRTC::ClassDestroy();
RTC::DtlsTransport::ClassDestroy();
DepUsrSCTP::ClassDestroy();
DepLibUV::ClassDestroy();
// Wait a bit so peding messages to stdout/Channel arrive to the Node
// Wait a bit so pending messages to stdout/Channel arrive to the Node
// process.
uv_sleep(200);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论