JinhoKwon
JinhoKwon

Reputation: 9

may be boost ssl handshake function have some memory leak

I got memory leak problem in my code.
when i call socket.handshake() functions,
and then exit main thread, i got memory leak.

but, remove 'socket.handshake' and then
run & exit main thread, memory leak probleam is disappeared.


1. sample code : memory leak

#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <boost/thread.hpp>

using namespace boost;

void ThSslEchoClient()
{
    try
    {
        asio::io_service ios;
        asio::ssl::context context(boost::asio::ssl::context::sslv23);

        context.set_options(
            asio::ssl::context::default_workarounds  
            | asio::ssl::context::no_sslv2  
            | asio::ssl::context::single_dh_use);

        asio::ssl::stream<boost::asio::ip::tcp::socket> socket(ios, context);
        boost::system::error_code ec;
        boost::asio::ip::tcp::endpoint host(boost::asio::ip::address::from_string("127.0.0.1"), 13);

        socket.lowest_layer().connect(host, ec);
        // REMOVE THE FOLLOWING LINE FOR COMPARISON
        socket.handshake(asio::ssl::stream_base::client, ec);

        if (ec)
            throw boost::system::system_error(ec);
    }
    catch (std::exception& _e)
    {
        std::cerr << _e.what() << std::endl;
    }
}

void TestBoostAsioSslEchoClient()
{
    boost::thread_group tg;

    tg.create_thread(ThSslEchoClient);
    tg.join_all();
}

2. sample code : no memory leak.
(just remove 'socket.handshake')

        // REMOVE THE FOLLOWING LINE FOR COMPARISON
        //socket.handshake(asio::ssl::stream_base::client, ec);

I don't know where is wrong. please help me.

Upvotes: 0

Views: 1238

Answers (1)

sehe
sehe

Reputation: 393064

I see no difference on my PC.

I fake a little server on port 13:

while true; do date | sudo netcat -l -p 13; done

I run the program:

make && valgrind ./test

Output without handshake:

sehe@desktop:/tmp$ valgrind ./test
==15878== Memcheck, a memory error detector
==15878== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==15878== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==15878== Command: ./test
==15878== 
==15878== 
==15878== HEAP SUMMARY:
==15878==     in use at exit: 64 bytes in 2 blocks
==15878==   total heap usage: 3,348 allocs, 3,346 frees, 207,156 bytes allocated
==15878== 
==15878== LEAK SUMMARY:
==15878==    definitely lost: 0 bytes in 0 blocks
==15878==    indirectly lost: 0 bytes in 0 blocks
==15878==      possibly lost: 0 bytes in 0 blocks
==15878==    still reachable: 64 bytes in 2 blocks
==15878==         suppressed: 0 bytes in 0 blocks
==15878== Rerun with --leak-check=full to see details of leaked memory
==15878== 
==15878== For counts of detected and suppressed errors, rerun with: -v
==15878== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

With handshake, it prints:

unkn protocol

Leaks reported with handshake:

140 ==16017== 24 bytes in 1 blocks are still reachable in loss record 1 of 6
141 ==16017==    at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
142 ==16017==    by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
154 ==16017== 32 bytes in 1 blocks are still reachable in loss record 2 of 6
155 ==16017==    at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
156 ==16017==    by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
168 ==16017== 32 bytes in 1 blocks are still reachable in loss record 3 of 6
169 ==16017==    at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
170 ==16017==    by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
182 ==16017== 128 bytes in 1 blocks are still reachable in loss record 4 of 6
183 ==16017==    at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
184 ==16017==    by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
196 ==16017== 176 bytes in 1 blocks are still reachable in loss record 5 of 6
197 ==16017==    at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
198 ==16017==    by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
210 ==16017== 600 bytes in 1 blocks are still reachable in loss record 6 of 6
211 ==16017==    at 0x4C2B3F8: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
212 ==16017==    by 0x60031FF: CRYPTO_malloc (in /lib/x86_64-linux-gnu/libcrypto.so.1.0.0)
228 ==16017==    still reachable: 992 bytes in 6 blocks
229 ==16017==         suppressed: 0 bytes in 0 blocks
230 ==16017== 

As you can see, the "leaks" are internal to the crypto lib (part of openssl) and are likely false alerts.

Upvotes: 1

Related Questions