Unverified 提交 79b7c4b9 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Fix crash in TcpConnection (#258) (#267)

* C++: verify in libuv static callbacks that the associated C++ instance has not been deallocated (should fix #258)
上级 f670de82
......@@ -36,7 +36,7 @@
"randomatic": "^3.1.1"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint": "^5.12.1",
"gulp": "^4.0.0",
"gulp-clang-format": "^1.0.27",
"gulp-eslint": "^5.0.0",
......@@ -44,7 +44,7 @@
"gulp-replace": "^1.0.0",
"gulp-shell": "^0.6.5",
"gulp-touch-cmd": "0.0.1",
"tap": "^12.1.1"
"tap": "^12.4.0"
},
"optionalDependencies": {
"clang-tools-prebuilt": "^0.1.4"
......
......@@ -25,7 +25,6 @@ public:
/* Struct for the data field of uv_req_t when writing into the connection. */
struct UvWriteData
{
TcpConnection* connection{ nullptr };
uv_write_t req;
uint8_t store[1];
};
......
......@@ -11,7 +11,6 @@ public:
/* Struct for the data field of uv_req_t when sending a datagram. */
struct UvSendData
{
UdpSocket* socket{ nullptr };
uv_udp_send_t req;
uint8_t store[1];
};
......
......@@ -11,7 +11,6 @@ public:
/* Struct for the data field of uv_req_t when writing data. */
struct UvWriteData
{
UnixStreamSocket* socket{ nullptr };
uv_write_t req;
uint8_t store[1];
};
......
......@@ -13,22 +13,36 @@
inline static void onAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf)
{
static_cast<TcpConnection*>(handle->data)->OnUvReadAlloc(suggestedSize, buf);
auto* connection = static_cast<TcpConnection*>(handle->data);
if (connection == nullptr)
return;
connection->OnUvReadAlloc(suggestedSize, buf);
}
inline static void onRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf)
{
static_cast<TcpConnection*>(handle->data)->OnUvRead(nread, buf);
auto* connection = static_cast<TcpConnection*>(handle->data);
if (connection == nullptr)
return;
connection->OnUvRead(nread, buf);
}
inline static void onWrite(uv_write_t* req, int status)
{
auto* writeData = static_cast<TcpConnection::UvWriteData*>(req->data);
TcpConnection* connection = writeData->connection;
auto* writeData = static_cast<TcpConnection::UvWriteData*>(req->data);
auto* handle = req->handle;
auto* connection = static_cast<TcpConnection*>(handle->data);
// Delete the UvWriteData struct (which includes the uv_req_t and the store char[]).
std::free(writeData);
if (connection == nullptr)
return;
// Just notify the TcpConnection when error.
if (status != 0)
connection->OnUvWriteError(status);
......@@ -82,6 +96,9 @@ void TcpConnection::Close()
this->closed = true;
// Tell the UV handle that the TcpConnection has been closed.
this->uvHandle->data = nullptr;
// Don't read more.
err = uv_read_stop(reinterpret_cast<uv_stream_t*>(this->uvHandle));
if (err != 0)
......@@ -195,7 +212,7 @@ void TcpConnection::Write(const uint8_t* data, size_t len)
return;
}
// Cannot write any data at first time. Use uv_write().
if (written == UV_EAGAIN || written == UV_ENOSYS)
else if (written == UV_EAGAIN || written == UV_ENOSYS)
{
// Set written to 0 so pendingLen can be properly calculated.
written = 0;
......@@ -218,7 +235,6 @@ void TcpConnection::Write(const uint8_t* data, size_t len)
// Allocate a special UvWriteData struct pointer.
auto* writeData = static_cast<UvWriteData*>(std::malloc(sizeof(UvWriteData) + pendingLen));
writeData->connection = this;
std::memcpy(writeData->store, data + written, pendingLen);
writeData->req.data = (void*)writeData;
......@@ -262,7 +278,7 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data
return;
}
// Cannot write any data at first time. Use uv_write().
if (written == UV_EAGAIN || written == UV_ENOSYS)
else if (written == UV_EAGAIN || written == UV_ENOSYS)
{
// Set written to 0 so pendingLen can be properly calculated.
written = 0;
......@@ -286,7 +302,6 @@ void TcpConnection::Write(const uint8_t* data1, size_t len1, const uint8_t* data
// Allocate a special UvWriteData struct pointer.
auto* writeData = static_cast<UvWriteData*>(std::malloc(sizeof(UvWriteData) + pendingLen));
writeData->connection = this;
// If the first buffer was not entirely written then splice it.
if (static_cast<size_t>(written) < len1)
{
......
......@@ -11,7 +11,9 @@
inline static void onConnection(uv_stream_t* handle, int status)
{
static_cast<TcpServer*>(handle->data)->OnUvConnection(status);
auto* server = static_cast<TcpServer*>(handle->data);
server->OnUvConnection(status);
}
inline static void onClose(uv_handle_t* handle)
......@@ -147,6 +149,9 @@ void TcpServer::Close()
this->closed = true;
// Tell the UV handle that the TcpServer has been closed.
this->uvHandle->data = nullptr;
MS_DEBUG_DEV("closing %zu active connections", this->connections.size());
for (auto it = this->connections.begin(); it != this->connections.end();)
......
......@@ -16,23 +16,37 @@ static uint8_t ReadBuffer[ReadBufferSize];
inline static void onAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf)
{
static_cast<UdpSocket*>(handle->data)->OnUvRecvAlloc(suggestedSize, buf);
auto* socket = static_cast<UdpSocket*>(handle->data);
if (socket == nullptr)
return;
socket->OnUvRecvAlloc(suggestedSize, buf);
}
inline static void onRecv(
uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned int flags)
{
static_cast<UdpSocket*>(handle->data)->OnUvRecv(nread, buf, addr, flags);
auto* socket = static_cast<UdpSocket*>(handle->data);
if (socket == nullptr)
return;
socket->OnUvRecv(nread, buf, addr, flags);
}
inline static void onSend(uv_udp_send_t* req, int status)
{
auto* sendData = static_cast<UdpSocket::UvSendData*>(req->data);
UdpSocket* socket = sendData->socket;
auto* sendData = static_cast<UdpSocket::UvSendData*>(req->data);
auto* handle = req->handle;
auto* socket = static_cast<UdpSocket*>(handle->data);
// Delete the UvSendData struct (which includes the uv_req_t and the store char[]).
std::free(sendData);
if (socket == nullptr)
return;
// Just notify the UdpSocket when error.
if (status != 0)
socket->OnUvSendError(status);
......@@ -169,6 +183,9 @@ void UdpSocket::Close()
this->closed = true;
// Tell the UV handle that the UdpSocket has been closed.
this->uvHandle->data = nullptr;
// Don't read more.
err = uv_udp_recv_stop(this->uvHandle);
if (err != 0)
......@@ -239,7 +256,6 @@ void UdpSocket::Send(const uint8_t* data, size_t len, const struct sockaddr* add
// Allocate a special UvSendData struct pointer.
auto* sendData = static_cast<UvSendData*>(std::malloc(sizeof(UvSendData) + len));
sendData->socket = this;
std::memcpy(sendData->store, data, len);
sendData->req.data = (void*)sendData;
......
......@@ -17,22 +17,36 @@
inline static void onAlloc(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf)
{
static_cast<UnixStreamSocket*>(handle->data)->OnUvReadAlloc(suggestedSize, buf);
auto* socket = static_cast<UnixStreamSocket*>(handle->data);
if (socket == nullptr)
return;
socket->OnUvReadAlloc(suggestedSize, buf);
}
inline static void onRead(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf)
{
static_cast<UnixStreamSocket*>(handle->data)->OnUvRead(nread, buf);
auto* socket = static_cast<UnixStreamSocket*>(handle->data);
if (socket == nullptr)
return;
socket->OnUvRead(nread, buf);
}
inline static void onWrite(uv_write_t* req, int status)
{
auto* writeData = static_cast<UnixStreamSocket::UvWriteData*>(req->data);
UnixStreamSocket* socket = writeData->socket;
auto* writeData = static_cast<UnixStreamSocket::UvWriteData*>(req->data);
auto* handle = req->handle;
auto* socket = static_cast<UnixStreamSocket*>(handle->data);
// Delete the UvWriteData struct (which includes the uv_req_t and the store char[]).
std::free(writeData);
if (socket == nullptr)
return;
// Just notify the UnixStreamSocket when error.
if (status != 0)
socket->OnUvWriteError(status);
......@@ -117,6 +131,9 @@ void UnixStreamSocket::Close()
this->closed = true;
// Tell the UV handle that the UnixStreamSocket has been closed.
this->uvHandle->data = nullptr;
// Don't read more.
err = uv_read_stop(reinterpret_cast<uv_stream_t*>(this->uvHandle));
if (err != 0)
......@@ -164,7 +181,7 @@ void UnixStreamSocket::Write(const uint8_t* data, size_t len)
return;
}
// Cannot write any data at first time. Use uv_write().
if (written == UV_EAGAIN || written == UV_ENOSYS)
else if (written == UV_EAGAIN || written == UV_ENOSYS)
{
// Set written to 0 so pendingLen can be properly calculated.
written = 0;
......@@ -187,7 +204,6 @@ void UnixStreamSocket::Write(const uint8_t* data, size_t len)
// Allocate a special UvWriteData struct pointer.
auto* writeData = static_cast<UvWriteData*>(std::malloc(sizeof(UvWriteData) + pendingLen));
writeData->socket = this;
std::memcpy(writeData->store, data + written, pendingLen);
writeData->req.data = (void*)writeData;
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论