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

Thread and memory safety fixes (#562)

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