{"action":"created","issue":{"url":"https://api.github.com/repos/google/UIforETW/issues/55","labels_url":"https://api.github.com/repos/google/UIforETW/issues/55/labels{/name}","comments_url":"https://api.github.com/repos/google/UIforETW/issues/55/comments","events_url":"https://api.github.com/repos/google/UIforETW/issues/55/events","html_url":"https://github.com/google/UIforETW/issues/55","id":108793605,"number":55,"title":"Handle leak (not our fault) in EnergyLib64","user":{"login":"ariccio","id":2142308,"avatar_url":"https://avatars.githubusercontent.com/u/2142308?v=3","gravatar_id":"","url":"https://api.github.com/users/ariccio","html_url":"https://github.com/ariccio","followers_url":"https://api.github.com/users/ariccio/followers","following_url":"https://api.github.com/users/ariccio/following{/other_user}","gists_url":"https://api.github.com/users/ariccio/gists{/gist_id}","starred_url":"https://api.github.com/users/ariccio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ariccio/subscriptions","organizations_url":"https://api.github.com/users/ariccio/orgs","repos_url":"https://api.github.com/users/ariccio/repos","events_url":"https://api.github.com/users/ariccio/events{/privacy}","received_events_url":"https://api.github.com/users/ariccio/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-09-29T04:45:06Z","updated_at":"2015-10-06T06:32:43Z","closed_at":null,"body":"I don't think this is UIforETW's fault, but I can't figure out how to contact the Intel Power Gadget team (yet), and need to track it somewhere.\r\n\r\nIt appears that a `HANDLE` is leaked inside the power gadget library:\r\n\r\n```\r\n=======================================\r\nVERIFIER STOP 0000000000000901: pid 0x4D38: A HANDLE was leaked. \r\n\r\n\t0000000000000808 : Value of the leaked handle. Run !htrace <handle> to get additional information about the handle if handle tracing is enabled.\r\n\t0000000D6AC92360 : Address to the allocation stack trace. Run dps <address> to view the allocation stack.\r\n\t0000000D732BEFD0 : Address of the owner dll name. Run du <address> to read the dll name.\r\n\t00007FFA2AFA0000 : Base of the owner dll. Run .reload <dll_name> = <address> to reload the owner dll. Use 'lm' to get more information about the loaded and unloaded modules.\r\n\r\n\r\n=======================================\r\nThis verifier stop is continuable.\r\nAfter debugging it use `go' to continue.\r\n\r\n=======================================\r\n\r\n(4d38.68d4): Break instruction exception - code 80000003 (first chance)\r\nvrfcore!VerifierStopMessageEx+0x6d0:\r\n00007ffa`026d2190 cc              int     3\r\n*** WARNING: Unable to verify checksum for UIforETW_devrel.exe\r\n0:000> !htrace 0000000000000808 \r\n--------------------------------------\r\nHandle = 0x0000000000000808 - OPEN\r\nThread ID = 0x00000000000068d4, Process ID = 0x0000000000004d38\r\n\r\n0x00007ffa429b3a4a: ntdll!NtCreateFile+0x000000000000000a\r\n0x00007ffa02673e6f: vfbasics!AVrfpNtCreateFile+0x00000000000000ff\r\n0x00007ffa3fd21e18: KERNELBASE!CreateFileInternal+0x0000000000000308\r\n0x00007ffa3fd21af6: KERNELBASE!CreateFileW+0x0000000000000066\r\n0x00007ffa0267438e: vfbasics!AVrfpCreateFileWCommon+0x0000000000000166\r\n0x00007ffa0267449a: vfbasics!AVrfpKernelbaseCreateFileW+0x000000000000004a\r\n0x00007ffa0267438e: vfbasics!AVrfpCreateFileWCommon+0x0000000000000166\r\n0x00007ffa02674438: vfbasics!AVrfpKernel32CreateFileW+0x0000000000000058\r\n*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\\Program Files\\Intel\\Power Gadget 3.0\\EnergyLib64.dll - \r\n0x00007ffa2afa4201: EnergyLib64!IntelEnergyLibInitialize+0x0000000000000081\r\n0x00007ff7b19c2d6b: UIforETW_devrel!CPowerStatusMonitor::CPowerStatusMonitor+0x00000000000001ab\r\n0x00007ff7b19cd220: UIforETW_devrel!CUIforETWDlg::CUIforETWDlg+0x00000000000002a0\r\n0x00007ff7b19cc34a: UIforETW_devrel!CUIforETWApp::InitInstance+0x00000000000000ca\r\n0x00007ffa09d500ae: mfc120u!AfxWinMain+0x0000000000000076\r\n\r\n--------------------------------------\r\nParsed 0x997 stack traces.\r\nDumped 0x1 stack traces.\r\n0:000> dps 0000000D6AC92360 \r\n0000000d`6ac92360  0000000d`6ac50470\r\n0000000d`6ac92368  00090000`00007801\r\n0000000d`6ac92370  00007ffa`02674438 vfbasics!AVrfpKernel32CreateFileW+0x58\r\n0000000d`6ac92378  00007ffa`2afa4201 EnergyLib64!IntelEnergyLibInitialize+0x81\r\n0000000d`6ac92380  00007ff7`b19c2d6b UIforETW_devrel!CPowerStatusMonitor::CPowerStatusMonitor+0x1ab [c:\\users\\alexander riccio\\documents\\github\\uiforetw\\uiforetw\\powerstatus.cpp @ 383]\r\n0000000d`6ac92388  00007ff7`b19cd220 UIforETW_devrel!CUIforETWDlg::CUIforETWDlg+0x2a0 [c:\\users\\alexander riccio\\documents\\github\\uiforetw\\uiforetw\\uiforetwdlg.cpp @ 87]\r\n0000000d`6ac92390  00007ff7`b19cc34a UIforETW_devrel!CUIforETWApp::InitInstance+0xca [c:\\users\\alexander riccio\\documents\\github\\uiforetw\\uiforetw\\uiforetw.cpp @ 212]\r\n0000000d`6ac92398  00007ffa`09d500ae mfc120u!AfxWinMain+0x76 [f:\\dd\\vctools\\vc7libs\\ship\\atlmfc\\src\\mfc\\winmain.cpp @ 37]\r\n0000000d`6ac923a0  00007ff7`b19da57a UIforETW_devrel!__tmainCRTStartup+0x162 [f:\\dd\\vctools\\crt\\crtw32\\dllstuff\\crtexe.c @ 618]\r\n0000000d`6ac923a8  00007ffa`42142d92 KERNEL32!BaseThreadInitThunk+0x22\r\n0000000d`6ac923b0  00007ffa`42929f64 ntdll!RtlUserThreadStart+0x34\r\n0000000d`6ac923b8  00007ffa`09d500ae mfc120u!AfxWinMain+0x76 [f:\\dd\\vctools\\vc7libs\\ship\\atlmfc\\src\\mfc\\winmain.cpp @ 37]\r\n0000000d`6ac923c0  00007ff7`b19da57a UIforETW_devrel!__tmainCRTStartup+0x162 [f:\\dd\\vctools\\crt\\crtw32\\dllstuff\\crtexe.c @ 618]\r\n0000000d`6ac923c8  00007ffa`42142d92 KERNEL32!BaseThreadInitThunk+0x22\r\n0000000d`6ac923d0  00007ffa`42929f64 ntdll!RtlUserThreadStart+0x34\r\n0000000d`6ac923d8  00007ffa`42142d92 KERNEL32!BaseThreadInitThunk+0x22\r\n```\r\n\r\n\r\nBreaking at the creation point shows what the handle actually is for:\r\n\r\n```\r\nBreakpoint 1 hit\r\nKERNELBASE!CreateFileW:\r\n00007ffa`3fd21a90 4883ec58        sub     rsp,58h\r\n0:000> r\r\nrax=0000000000000000 rbx=0000000000000003 rcx=00007ffa2ca59880\r\nrdx=00000000c0000000 rsi=0000000000000000 rdi=00000000c0000000\r\nrip=00007ffa3fd21a90 rsp=000000011345de78 rbp=00007ffa2ca59880\r\n r8=0000000000000000  r9=0000000000000000 r10=00007ffa0268c570\r\nr11=000000011345e058 r12=0000000000000000 r13=0000000000000000\r\nr14=00007ffa3fd21a90 r15=0000000000000000\r\niopl=0         nv up ei pl nz na pe nc\r\ncs=0033  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000202\r\nKERNELBASE!CreateFileW:\r\n00007ffa`3fd21a90 4883ec58        sub     rsp,58h\r\n0:000> du 00007ffa2ca59880\r\n00007ffa`2ca59880  \"\\\\.\\EnergyDriver\"\r\n```\r\n\r\nI found this while doing some unrelated refactoring, and, well... grrr."},"comment":{"url":"https://api.github.com/repos/google/UIforETW/issues/comments/145753758","html_url":"https://github.com/google/UIforETW/issues/55#issuecomment-145753758","issue_url":"https://api.github.com/repos/google/UIforETW/issues/55","id":145753758,"user":{"login":"ariccio","id":2142308,"avatar_url":"https://avatars.githubusercontent.com/u/2142308?v=3","gravatar_id":"","url":"https://api.github.com/users/ariccio","html_url":"https://github.com/ariccio","followers_url":"https://api.github.com/users/ariccio/followers","following_url":"https://api.github.com/users/ariccio/following{/other_user}","gists_url":"https://api.github.com/users/ariccio/gists{/gist_id}","starred_url":"https://api.github.com/users/ariccio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ariccio/subscriptions","organizations_url":"https://api.github.com/users/ariccio/orgs","repos_url":"https://api.github.com/users/ariccio/repos","events_url":"https://api.github.com/users/ariccio/events{/privacy}","received_events_url":"https://api.github.com/users/ariccio/received_events","type":"User","site_admin":false},"created_at":"2015-10-06T06:32:43Z","updated_at":"2015-10-06T06:32:43Z","body":"> Be aware that a handle leak may be by design or at least not treated as high priority. Perfect cleanliness during shutdown is often pointless or not practical (see for example crbug.com/490716 which explicitly recommends calling TerminateProcess rather than paying the cost for a theoretically ideal but sometimes expensive shutdown).\r\n\r\nOh I totally agree, for *application-level code*, that is. The Intel Power Gadget API is third party code,  and this is nowhere documented. If a program loads the library, initializes it, and frees it, then it's dealing with a mystery leak. It's quite easy to make my repro trigger this problem. Here it is leaking 6,730 handles:\r\n\r\n![energylibleak](https://cloud.githubusercontent.com/assets/2142308/10301592/f5dcea3c-6bd1-11e5-8367-658100963a34.PNG)\r\n\r\n*`6,730`, next to `Normal` is Task Manager's `Handles` column*.\r\n\r\n...and all it took was:\r\n\r\n```\r\n\tconst size_t maxEnergyLib = 10000u;\r\n\tfor ( size_t i = 0u; i < maxEnergyLib; ++i ) {\r\n\t\tdoEnergyLib( );\r\n\t\t}\r\n```\r\n\r\n\r\nBy leaking thousands of handles, we don't need Application Verifier to demonstrate the presence of a leak, but using Application Verifier will (of course) point to the offending `HANDLE`'s allocation stack."}}