Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak on openssl 1.1.1 + libcurl static library #5282

Closed
zmcx16 opened this issue Apr 22, 2020 · 3 comments
Closed

Memory leak on openssl 1.1.1 + libcurl static library #5282

zmcx16 opened this issue Apr 22, 2020 · 3 comments

Comments

@zmcx16
Copy link

zmcx16 commented Apr 22, 2020

I did this

Hi, I occur a memory leak problem on openssl 1.1.1 + libcurl, I did this:

  1. build openssl 1.1.1 static library, build steps:
    $ perl Configure VC-WIN32 --prefix="C:\openssl\2017\win32\libmt" no-asm -DOPENSSL_USE_IPV6=1 no-shared
    $ nmake
    $ nmake test
    $ nmake install

  2. build libcurl:
    $ open VS2017 x86 Native Tools Command Prompt
    $ cd curl/winbuild
    $ open MakefileBuild.vc, append "/NXCOMPAT /DYNAMICBASE" to LFLAGS
    $ nmake /f Makefile.vc mode=static RTLIBCFG=static VC=14.1 WITH_DEVEL="C:\openssl\2017\win32\libmt" WITH_SSL=static ENABLE_IPV6=yes ENABLE_SSPI=no ENABLE_IDN=no ENABLE_WINSSL=no DEBUG=no GEN_PDB=yes MACHINE=x86

  3. import openssl & libcurl into my example code:
    example-code
    vld-output.txt

  4. run the above sample code, the memory leak occurs.

The memory leak occurs conditions:

  1. Only occur memory leak on openssl 1.1.1 (static library: libcrypto.lib, libssl.lib) + libcurl (test 7.65.3, 7.68.0 both), the openssl 1.0.2 work fine (ssleay32.lib, libeay32.lib).
  2. Only occur memory leak on thread function, if I call curl_easy_perform(curl) on main thread, the memory leak doesn't happen.
  3. Only occur memory leak on openssl 1.1.1 static library, if I remove "no-shared" parameter on openssl build, the memory leak doesn't happen.
  4. Only occur memory leak on https reqest, http request doesn't occur memory leak.

I expected the following

No memory leak on openssl 1.1.1 + libcurl statlic library

curl/libcurl version

  • test libcurl: 7.65.3, 7.68.0
  • openssl: 1.1.1

operating system

  • Windows
@bagder
Copy link
Member

bagder commented Apr 22, 2020

I find it very unlikely to be a curl bug. We run all tests with leak detection tools in the CI all the time.

How do you detect that there's a leak? Where is it saying that the leak happens?

If you can make it not leak by changing how you link with openssl then it seems to indicate something weird since then obviously it is the exact same (curl) code being executed

@zmcx16
Copy link
Author

zmcx16 commented Apr 22, 2020

Hi bagder,

Just now, I try download the openssl static library from online and rebuild it with curl, and then the memory leak is gone.

I think the memory leak problem is due to my openssl self-build even the nmake test is pass and the openssl is work but it occurs memory leak. I will keep finding the root cause on openssl build problem.

Thanks for your reply.

@zmcx16
Copy link
Author

zmcx16 commented Apr 22, 2020

Hi bagder,

I found the root cause is I need manually call OPENSSL_thread_stop() on the thread function end.

Thanks for your help 😊

Ref: OPENSSL_thread_stop

The OPENSSL_thread_stop() function deallocates resources associated with the current thread. 
Typically this function will be called automatically by the library when the thread exits. This should 
only be called directly if resources should be freed at an earlier time, or under the circumstances 
described in the NOTES section below.

NOTES
Resources local to a thread are deallocated automatically when the thread exits (e.g. in a pthreads 
environment, when pthread_exit() is called). On Windows platforms this is done in response to a 
DLL_THREAD_DETACH message being sent to the libcrypto32.dll entry point. Some windows 
functions may cause threads to exit without sending this message (for example ExitProcess()). If the 
application uses such functions, then the application must free up OpenSSL resources directly via a 
call to OPENSSL_thread_stop() on each thread. Similarly this message will also not be sent if 
OpenSSL is linked statically, and therefore applications using static linking should also call 
OPENSSL_thread_stop() on each thread. Additionally if OpenSSL is loaded dynamically via 
LoadLibrary() and the threads are not destroyed until after FreeLibrary() is called then each thread 
should call OPENSSL_thread_stop() prior to the FreeLibrary() call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants