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