Repository navigation
Description
I used the server->setServerEventHandler to add an event handler.When a client connected, I will bind some vars to the connection(use the ctx->setUserData), Then I use "eb->runAfterDelay" function to do some work after few seconds, I will use ctx->getUserData to get the vars I added before.
This is my test code:
#include <thrift/lib/cpp2/server/ThriftServer.h>
#include "ThriftTest.h"
using namespace apache::thrift;
using namespace apache::thrift::server;
using namespace apache::thrift::async;
using namespace apache::thrift::transport;
using namespace apache::thrift::test::cpp2;
struct SomeStruct {
};
class TestServerEventHandler
: public server::TServerEventHandler
, public TProcessorEventHandler
, public TProcessorEventHandlerFactory
, public std::enable_shared_from_this<TestServerEventHandler> {
public:
std::shared_ptr<TProcessorEventHandler> getEventHandler() {
return shared_from_this();
}
void newConnection(TConnectionContext* ctx) {
// add some vars to the ctx
SomeStruct* s = new SomeStruct();
ctx->setUserData(s);
Cpp2ConnContext* cpp2ctx = dynamic_cast<Cpp2ConnContext*>(ctx);
ThriftTestAsyncClient* client = cpp2ctx->getDuplexClient<ThriftTestAsyncClient>();
TEventBase* eb = client->getChannel()->getEventBase();
eb->runAfterDelay([ctx]() mutable {
void* p = ctx->getUserData();
if (p) {
std::cout << "do some work here..." << std::endl;
} else {
std::cout << "maybe the client is close" << std::endl;
}
}, 10000);
}
};
class ThriftTestInterface : public ThriftTestSvIf {
};
int main(int argc, char* argv[]) {
auto server = std::make_shared<ThriftServer>();
server->setPort(8888);
server->setInterface(std::unique_ptr<ThriftTestInterface>(new ThriftTestInterface));
server->setDuplex(true);
std::shared_ptr<TestServerEventHandler> handler = std::make_shared<TestServerEventHandler>();
server->setServerEventHandler(handler);
server->serve();
return 0;
}My question is :
If a client connected to my server(I used "telnet localhost 8888" for a test), and then close the connection immediately (maybe CTRL+C). After 10s, I will use "void* p = ctx->getUserData();" to get my vars from a closed ctx, p is nullptr, so it printed "maybe the client is close". The code can work, and no crash. But, if I use the "valgrind" tool to do a memcheck, It will report :
==23976== Invalid read of size 8
==23976== at 0x49F7B9: TestServerEventHandler::newConnection(apache::thrift::server::TConnectionContext*)::{lambda()#1}::operator()() (t.cpp:35)
==23976== by 0x4A114A: std::_Function_handler<void (), TestServerEventHandler::newConnection(apache::thrift::server::TConnectionContext*)::{lambda()#1}>::_M_invoke(std::_Any_data const&) (functional:2071)
==23976== by 0x4B1007: std::function<void ()>::operator()() const (functional:2464)
==23976== by 0x5DFF128: folly::EventBase::CobTimeout::timeoutExpired() (EventBase.cpp:114)
==23976== by 0x5DFB3FB: folly::AsyncTimeout::libeventCallback(int, short, void*) (AsyncTimeout.cpp:151)
==23976== by 0x8D7FA3B: event_process_active_single_queue (event.c:1340)
==23976== by 0x8D7FA3B: event_process_active (event.c:1407)
==23976== by 0x8D7FA3B: event_base_loop (event.c:1604)
==23976== by 0x5DFFEFD: folly::EventBase::loopBody(int) (EventBase.cpp:277)
==23976== by 0x5E00E99: folly::EventBase::loopForever() (EventBase.cpp:366)
==23976== by 0x64DEC47: apache::thrift::Cpp2Worker::serve() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64DFD2C: apache::thrift::server::TServer::run() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x655FE78: apache::thrift::concurrency::NumaRunnable::run() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x5B2300A: apache::thrift::concurrency::PthreadThread::threadMain(void*) (in /usr/local/lib/libthrift.so.7.0.0)
==23976== Address 0xe66da78 is 120 bytes inside a block of size 336 free'd
==23976== at 0x4C270B1: operator delete(void*) (vg_replace_malloc.c:507)
==23976== by 0x64D279F: apache::thrift::Cpp2Connection::~Cpp2Connection() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64E3047: std::_Sp_counted_ptr<apache::thrift::Cpp2Connection*, (__gnu_cxx::_Lock_policy)2>::_M_dispose() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x4A0805: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:144)
==23976== by 0x49FA68: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:546)
==23976== by 0x64D706B: std::__shared_ptr<apache::thrift::Cpp2Connection, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D7085: std::shared_ptr<apache::thrift::Cpp2Connection>::~shared_ptr() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D4546: apache::thrift::Cpp2Connection::channelClosed(folly::exception_wrapper&&)::{lambda()#1}::operator()() const (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D54FF: folly::ScopeGuardImpl<apache::thrift::Cpp2Connection::channelClosed(folly::exception_wrapper&&)::{lambda()#1}>::execute() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D543B: folly::ScopeGuardImpl<apache::thrift::Cpp2Connection::channelClosed(folly::exception_wrapper&&)::{lambda()#1}>::~ScopeGuardImpl() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D474B: apache::thrift::Cpp2Connection::channelClosed(folly::exception_wrapper&&) (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64C7D3A: apache::thrift::HeaderServerChannel::messageChannelEOF() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976==
==23976== Invalid read of size 8
==23976== at 0x49E5A8: apache::thrift::server::TConnectionContext::getUserData() const (TConnectionContext.h:98)
==23976== by 0x49F7CE: TestServerEventHandler::newConnection(apache::thrift::server::TConnectionContext*)::{lambda()#1}::operator()() (t.cpp:35)
==23976== by 0x4A114A: std::_Function_handler<void (), TestServerEventHandler::newConnection(apache::thrift::server::TConnectionContext*)::{lambda()#1}>::_M_invoke(std::_Any_data const&) (functional:2071)
==23976== by 0x4B1007: std::function<void ()>::operator()() const (functional:2464)
==23976== by 0x5DFF128: folly::EventBase::CobTimeout::timeoutExpired() (EventBase.cpp:114)
==23976== by 0x5DFB3FB: folly::AsyncTimeout::libeventCallback(int, short, void*) (AsyncTimeout.cpp:151)
==23976== by 0x8D7FA3B: event_process_active_single_queue (event.c:1340)
==23976== by 0x8D7FA3B: event_process_active (event.c:1407)
==23976== by 0x8D7FA3B: event_base_loop (event.c:1604)
==23976== by 0x5DFFEFD: folly::EventBase::loopBody(int) (EventBase.cpp:277)
==23976== by 0x5E00E99: folly::EventBase::loopForever() (EventBase.cpp:366)
==23976== by 0x64DEC47: apache::thrift::Cpp2Worker::serve() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64DFD2C: apache::thrift::server::TServer::run() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x655FE78: apache::thrift::concurrency::NumaRunnable::run() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== Address 0xe66da80 is 128 bytes inside a block of size 336 free'd
==23976== at 0x4C270B1: operator delete(void*) (vg_replace_malloc.c:507)
==23976== by 0x64D279F: apache::thrift::Cpp2Connection::~Cpp2Connection() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64E3047: std::_Sp_counted_ptr<apache::thrift::Cpp2Connection*, (__gnu_cxx::_Lock_policy)2>::_M_dispose() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x4A0805: std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() (shared_ptr_base.h:144)
==23976== by 0x49FA68: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() (shared_ptr_base.h:546)
==23976== by 0x64D706B: std::__shared_ptr<apache::thrift::Cpp2Connection, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D7085: std::shared_ptr<apache::thrift::Cpp2Connection>::~shared_ptr() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D4546: apache::thrift::Cpp2Connection::channelClosed(folly::exception_wrapper&&)::{lambda()#1}::operator()() const (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D54FF: folly::ScopeGuardImpl<apache::thrift::Cpp2Connection::channelClosed(folly::exception_wrapper&&)::{lambda()#1}>::execute() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D543B: folly::ScopeGuardImpl<apache::thrift::Cpp2Connection::channelClosed(folly::exception_wrapper&&)::{lambda()#1}>::~ScopeGuardImpl() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64D474B: apache::thrift::Cpp2Connection::channelClosed(folly::exception_wrapper&&) (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976== by 0x64C7D3A: apache::thrift::HeaderServerChannel::messageChannelEOF() (in /usr/local/lib/libthriftcpp2.so.7.0.0)
==23976==It says that "Invalid read of size".
So I'm not sure is it safe? I just read a null pointer, and no write to it.
Or suggest me a better way?
Thanks a lot!
Activity
alandau commentedon Oct 17, 2014
I think the problem is not in accessing p, but rather in accessing ctx from inside the lambda. When the connection is closed, ctx is deleted, so you're accessing freed memory.
As for checking connection status, when the connection gets closed, you'll get a connectionDestroyed callback on the same event handler.
thrift/lib/cpp2/test/StreamingTest.cpp: fix SIOF bug and heap-use-aft…
Remove `sudo: required` from `.travis.yml` (#37)
Add e2e regression test for Cpp2Channel::writeError upgrade-teardown UAF