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

APP CRASH (8.0.18446): Memory leaked after thread creation/deletion on Windows #4364

Open
dkim-virsec opened this issue Jul 8, 2020 · 1 comment

Comments

@dkim-virsec
Copy link
Contributor

Describe the bug

There seems to be a memory leak when running applications that repeatedly create threads (letting them finish each time). As shown by the perfmon graph below, the memory usage continues to increase overtime whereas it stays the same when running natively.

To Reproduce

The program is a simple multi-threaded program pasted here:

#include <windows.h>
#include <iostream>
#include <chrono>
#include <thread>

using namespace std;

DWORD WINAPI thread1(LPVOID lpParameter)
{
    unsigned int& size = *((unsigned int*)lpParameter);
    for (int i = 0; i < 20000000 * (rand()%100); i++)
    {
        if ((i%100000000 == 0))
        {
            cout << "thread 1 " << i << endl;
        }
    }
    return 0;
}

DWORD WINAPI thread2(LPVOID lpParameter)
{
    unsigned int& size = *((unsigned int*)lpParameter);
    for (int i = 0; i < 2000000000; i++)
    {
        if ((i % 10000000 == 0))
        {
            cout << "thread 2 " << i << endl;
        }
    }
    return 0;
}

#define NUM 800

int main(int argc, char* argv[])
{
    //BOOL debugging = TRUE;
    //while (debugging) {
    //  BOOL debuggerPresent = FALSE;
    //  if (CheckRemoteDebuggerPresent(GetCurrentProcess(), &debuggerPresent) && debuggerPresent) {
    //      DebugBreak();
    //      debugging = FALSE;
    //  }
    //  Sleep(1000);
    //}
    unsigned int s1 = 10;
    unsigned int s2 = 20;
    DWORD myThreadID[NUM];
    
    HANDLE myHandle[NUM];

    while (1)
    {
        for (int i = 0; i < NUM; i++)
        {
            myHandle[i] = CreateThread(0, 0, thread1, &s1, 0, myThreadID + i);
        }

        //cout << myThreadID << " " << myThreadID2 << endl;

        for (int i = 0; i < NUM; i++)
        {
            WaitForSingleObject(myHandle[i], INFINITE);
        }

        for (int i = 0; i < NUM; i++)
        {
            cout << CloseHandle(myHandle[i]) << endl;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(8000));
    }

    return 0;
}

Versions

  • What version of DynamoRIO are you using? 8.0.18446
  • Does the latest build from
    https://github.com/DynamoRIO/dynamorio/wiki/Latest-Build solve the problem? No
  • What operating system version are you running on? ("Windows 10" is not sufficient: give the release number.) Windows 10 OS build 19041.329
  • Is your application 32-bit or 64-bit? 32-bit application

Additional context
There was a recently filed commit link here that seemed to address something similar to the issue I am facing here. Though I am not sure it is the same issue and whether i#4334 was tested on Windows. Perhaps the same issue seen in that bug has not yet been address on Windows?

Perfmon graph:

image

@derekbruening
Copy link
Contributor

#4334 bailed on Windows due to the test hitting re-attach problems, and manually Windows was not examined, so there may well be some Windows-specific accumulations beyond the fixes in PR #4350 (most of which were cross-OS).

Note that these "leaks" would not be caught by reachability-based leak checkers as they are still reachable (and freed later): they would be called "accumulations" rather than "leaks" most likely, though the impact during the run is the same.

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

No branches or pull requests

2 participants