18 January, 2017

The unexpected behavior of std::mutex

Hi there, this time I would like to tell about the issue in Microsoft implementation of std::mutex for VS2013. This night I spent in deep debugging trying to understand why my test is failing with std::mutex. but isn't failing with boost::mutex. The only difference was in calling DllMain() for my DLL. It was looked like somebody secretly called LoadLibrary() during std::mutex::lock(). I was very surprised when found that it's truth (Important: our product has static linking with Microsoft CRT):

0:000> kM
 # Child-SP          RetAddr           Call Site
00 000000eb`7ff9e6a8 00007ff8`f0a23189 KERNELBASE!LoadLibraryExW
01 000000eb`7ff9e6b0 00007ff8`f0a23240 JetBrains_Profiler_Windows_Core!Concurrency::details::ReferenceLoadLibrary+0x79
02 000000eb`7ff9e940 00007ff8`f0a261d8 JetBrains_Profiler_Windows_Core!Concurrency::details::RegisterAsyncTimerAndLoadLibrary+0x40
03 000000eb`7ff9e990 00007ff8`f0a8bcce JetBrains_Profiler_Windows_Core!Concurrency::details::SchedulerBase::SchedulerBase+0x538
04 000000eb`7ff9ea60 00007ff8`f0a8bf79 JetBrains_Profiler_Windows_Core!Concurrency::details::ThreadScheduler::ThreadScheduler+0x2e
05 000000eb`7ff9ea90 00007ff8`f0a28c32 JetBrains_Profiler_Windows_Core!Concurrency::details::ThreadScheduler::Create+0x69
06 000000eb`7ff9eaf0 00007ff8`f0a2a546 JetBrains_Profiler_Windows_Core!Concurrency::details::SchedulerBase::CreateWithoutInitializing+0x22
07 000000eb`7ff9eb40 00007ff8`f0a28af9 JetBrains_Profiler_Windows_Core!Concurrency::details::SchedulerBase::GetDefaultScheduler+0x76
08 000000eb`7ff9ebb0 00007ff8`f09f3b75 JetBrains_Profiler_Windows_Core!Concurrency::details::SchedulerBase::CreateContextFromDefaultScheduler+0x9
09 000000eb`7ff9ec00 00007ff8`f09f2f22 JetBrains_Profiler_Windows_Core!Concurrency::details::SchedulerBase::CurrentContext+0x15
0a 000000eb`7ff9ec40 00007ff8`f09f5c78 JetBrains_Profiler_Windows_Core!Concurrency::details::LockQueueNode::LockQueueNode+0x62
0b 000000eb`7ff9ecd0 00007ff8`f09a2852 JetBrains_Profiler_Windows_Core!Concurrency::critical_section::lock+0x18
0c 000000eb`7ff9ed30 00007ff8`f09a2cf5 JetBrains_Profiler_Windows_Core!mtx_do_lock+0xf2
0d 000000eb`7ff9edb0 00007ff8`f0925aa8 JetBrains_Profiler_Windows_Core!_Mtx_lock+0x15
0e 000000eb`7ff9ede0 00007ff8`f0925b6b JetBrains_Profiler_Windows_Core!std::_Mtx_lockX+0x28
0f 000000eb`7ff9ee10 00007ff8`f0925705 JetBrains_Profiler_Windows_Core!std::_Mutex_base::lock+0x2b
10 000000eb`7ff9ee40 00007ff8`f092563c JetBrains_Profiler_Windows_Core!boost::lock_guard<std::mutex>::lock_guard<std::mutex>+0x45
11 000000eb`7ff9ee70 00007ff8`f092448a JetBrains_Profiler_Windows_Core!boost::make_lock_guard<std::mutex>+0x7c
12 000000eb`7ff9eef0 00007ff8`f06395a4 JetBrains_Profiler_Windows_Core!Bridge::Open+0x29a
13 000000eb`7ff9f170 00007ff8`f0636398 JetBrains_Profiler_Windows_Core!BaseProfiler::init_start+0xa4
14 000000eb`7ff9f570 00007ff9`2d400fbc JetBrains_Profiler_Windows_Core!BaseProfiler::Initialize+0x148
15 000000eb`7ff9f670 00007ff9`2d3cdd66 clr!EEToProfInterfaceImpl::Initialize+0x90
16 000000eb`7ff9f6c0 00007ff9`2d31f846 clr!ProfilingAPIUtility::LoadProfiler+0x32a
17 000000eb`7ff9f7b0 00007ff9`2d03f1d3 clr! ?? ::FNODOBFM::`string'+0xe5606
18 000000eb`7ff9f850 00007ff9`2d03b202 clr!ProfilingAPIUtility::InitializeProfiling+0x3f
19 000000eb`7ff9f880 00007ff9`2d03ac65 clr!EEStartupHelper+0x572
1a 000000eb`7ff9faa0 00007ff9`2d03abf4 clr!EEStartup+0x15
1b 000000eb`7ff9faf0 00007ff9`2d02d6ca clr!EnsureEEStarted+0xbf
1c 000000eb`7ff9fb70 00007ff9`2d02e154 clr!CorExeMainInternal+0x7a
1d 000000eb`7ff9fc00 00007ff9`2da581ad clr!CorExeMain+0x14
1e 000000eb`7ff9fc40 00007ff9`2daf10ab mscoreei!CorExeMain+0x112
1f 000000eb`7ff9fca0 00007ff9`3d638364 MSCOREE!CorExeMain_Exported+0xb
20 000000eb`7ff9fcd0 00007ff9`3d8170d1 KERNEL32!BaseThreadInitThunk+0x14
21 000000eb`7ff9fd00 00000000`00000000 ntdll!RtlUserThreadStart+0x21
0:000> r
rax=0000000000000047 rbx=00007ff8f03da37f rcx=000000eb7ff9e710
rdx=0000000000000000 rsi=000000eb7ff9eeb0 rdi=000000eb7ff9eab1
rip=00007ff93a02cc10 rsp=000000eb7ff9e6a8 rbp=0000000000000000
 r8=0000000000000000  r9=0000000000000000 r10=0000000000000007
r11=000000eb7ff9e710 r12=0000000000000001 r13=00000000ffffffff
r14=0000027387f0ae70 r15=0000000000000000
iopl=0         nv up ei pl zr na po nc
cs=0033  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
KERNELBASE!LoadLibraryExW:
00007ff9`3a02cc10 4055            push    rbp
0:000> du 000000eb7ff9e710
000000eb`7ff9e710  "C:\Work\dotnet-products\Bin\x64."
000000eb`7ff9e750  "dbg\JetBrains.Profiler.Windows.C"
000000eb`7ff9e790  "ore.dll"
P.S. Please look at %ProgramFiles(x86)%\Microsoft Visual Studio 12.0\VC\crt\src\thr\mutex.c if you want know more information about that.

No comments: