<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>voyce &#187; mscorwks</title>
	<atom:link href="http://www.voyce.com/index.php/tag/mscorwks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.voyce.com</link>
	<description>Programming and debugging tidbits</description>
	<lastBuildDate>Sun, 15 Jan 2012 13:10:46 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Beware of using stack-based COM objects from .NET</title>
		<link>http://www.voyce.com/index.php/2010/01/21/beware-of-using-stack-based-com-objects-from-net/</link>
		<comments>http://www.voyce.com/index.php/2010/01/21/beware-of-using-stack-based-com-objects-from-net/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 17:16:57 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WinDbg]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[mscorwks]]></category>
		<category><![CDATA[win32]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=618</guid>
		<description><![CDATA[COM objects that don't have the expected lifetime can cause chaos when combined with .NETs garbage collection system.]]></description>
			<content:encoded><![CDATA[<p>There are all sorts of nasty things to be aware of if you&#8217;re mixing reference-counted COM objects with garbage-collected .NET. For instance, if you&#8217;re implementing COM objects in C++ then you&#8217;re free to allocate them anywhere you like; on the heap or perhaps on the stack if you know they&#8217;re only used in some specific scope.</p>
<p>But what happens if during the lifetime of that stack based COM object, it gets used from .NET? A runtime callable wrapper (RCW) will be created around the object. And this RCW expects to be able to keep the underlying object alive by incrementing its reference count. Of course, the stack-based object will soon go out of scope, and regardless of its reference count the object will be destroyed and the pointer that the RCW contains will no longer be valid. It points into the stack, so when the RCW gets cleaned-up, the CLR will call via this pointer into memory that contains garbage and you&#8217;ll get something nasty like an access violation or illegal instruction exception. </p>
<p><span id="more-618"></span></p>
<p>It&#8217;s fairly easy to reproduce this to see where things go wrong. It can be useful to see where the CLR blows up, and how we can identify this as the cause.</p>
<p>Lets start by creating a simple pseudo-COM object that implements just the bare minimum to be usable:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> MyClass <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> IUnknown
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	MyClass<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">:</span>l<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
	STDMETHOD_<span style="color: #008000;">&#40;</span>ULONG, AddRef<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> InterlockedIncrement<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>l<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
	STDMETHOD_<span style="color: #008000;">&#40;</span>ULONG, Release<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> InterlockedDecrement<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>l<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
	STDMETHOD<span style="color: #008000;">&#40;</span>QueryInterface<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>REFIID iid, <span style="color: #0000ff;">void</span> <span style="color: #000040;">**</span> ppvObject<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>iid <span style="color: #000080;">==</span> IID_IUnknown<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			<span style="color: #000040;">*</span>ppvObject <span style="color: #000080;">=</span> <span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
			AddRef<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                        <span style="color: #0000ff;">return</span> S_OK<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
		<span style="color: #0000ff;">return</span> E_NOINTERFACE<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">long</span> l<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>We&#8217;ll also need a COM visible .NET object that will use the object. It doesn&#8217;t actually need to be COM visible, but that&#8217;s the easiest way to access it from C++, in my opinion.</p>
<p>I&#8217;ve created the COM object in F#. It&#8217;s a trivial class that has a single interface, with a single method that takes the object we pass to it and prints its type. This is enough for the RCW to be created.</p>

<div class="wp_syntax"><div class="code"><pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">open</span> System
<span style="color: #06c; font-weight: bold;">open</span> System<span style="color: #000080;">.</span><span style="color: #505090;">Runtime</span><span style="color: #000080;">.</span><span style="color: #505090;">InteropServices</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">module</span> Module1 <span style="color: #000080;">=</span>
&nbsp;
    <span style="color: #000080;">&#91;</span><span style="color: #000080;">&lt;</span>ComVisible<span style="color: #000080;">&#40;</span><span style="color: #06c; font-weight: bold;">true</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">;</span> InterfaceType<span style="color: #000080;">&#40;</span>ComInterfaceType<span style="color: #000080;">.</span><span style="color: #505090;">InterfaceIsIUnknown</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #000080;">&#93;</span>
    <span style="color: #06c; font-weight: bold;">type</span> <span style="color: #06c; font-weight: bold;">public</span> IConsumer <span style="color: #000080;">=</span> 
        <span style="color: #06c; font-weight: bold;">abstract</span> <span style="color: #06c; font-weight: bold;">member</span> UseObject <span style="color: #000080;">:</span> o<span style="color: #000080;">:</span>obj <span style="color: #000080;">-&gt;</span> unit
&nbsp;
    <span style="color: #000080;">&#91;</span><span style="color: #000080;">&lt;</span>ComVisible<span style="color: #000080;">&#40;</span><span style="color: #06c; font-weight: bold;">true</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">;</span> ClassInterface<span style="color: #000080;">&#40;</span>ClassInterfaceType<span style="color: #000080;">.</span><span style="color: #505090;">None</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&gt;</span><span style="color: #000080;">&#93;</span>
    <span style="color: #06c; font-weight: bold;">type</span> <span style="color: #06c; font-weight: bold;">public</span> Consumer<span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
        <span style="color: #06c; font-weight: bold;">interface</span> IConsumer <span style="color: #06c; font-weight: bold;">with</span>
            <span style="color: #06c; font-weight: bold;">member</span> this<span style="color: #000080;">.</span><span style="color: #505090;">UseObject</span> <span style="color: #000080;">&#40;</span>o<span style="color: #000080;">:</span>obj<span style="color: #000080;">&#41;</span> <span style="color: #000080;">=</span>
                Console<span style="color: #000080;">.</span><span style="color: #505090;">WriteLine</span> <span style="color: #000080;">&#40;</span>sprintf <span style="color: #008080;">&quot;%A&quot;</span> <span style="color: #000080;">&#40;</span>o<span style="color: #000080;">.</span><span style="color: #505090;">GetType</span><span style="color: #000080;">&#40;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span><span style="color: #000080;">&#41;</span></pre></div></div>

<p>We can compile this into a DLL, then run regasm with the /tlb switch to generate a type library (TLB):</p>
<pre>
fsc -o:obj\Debug\testStackObjectsFs.dll Module1.fs
regasm /tlb:testStackObjectsFs.tlb testStackObjectsFs.dll
</pre>
<p>That can be #imported back into our test harness:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#import &quot;testStackObjectsFs.tlb&quot;</span></pre></div></div>

<p>Now we&#8217;re ready to put together some code that creates an instance of our object on the stack and passes it to our .NET component:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> Foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// Create an instance of our &quot;COM object&quot; on the stack</span>
	MyClass obj<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Create a managed object</span>
	testStackObjectsFs<span style="color: #008080;">::</span><span style="color: #007788;">IConsumerPtr</span> mgd<span style="color: #008000;">&#40;</span>__uuidof<span style="color: #008000;">&#40;</span>testStackObjectsFs<span style="color: #008080;">::</span><span style="color: #007788;">Consumer</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// and pass our COM object to it</span>
	mgd<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>UseObject<span style="color: #008000;">&#40;</span>_variant_t<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>obj<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> _tmain<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> argc, _TCHAR<span style="color: #000040;">*</span> argv<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">// Initialise the COM runtime, for our purposes it doesn't</span>
	<span style="color: #666666;">// matter which threading model we use</span>
	CoInitializeEx<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>, COINIT_MULTITHREADED<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Call a separate function, to ensure stack-based objects</span>
	<span style="color: #666666;">// are out-of-scope on return.</span>
	Foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// Wait for some input</span>
	_getch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now, if you run this from within Visual Studio, if you&#8217;re vigilant (and you haven&#8217;t got your debugger set to stop on access violations), then you&#8217;ll notice this in the output window after the return statement executes:<br />
<code><br />
...<br />
The thread 'Win32 Thread' (0x15b0) has exited with code 11001 (0x2af9).<br />
The thread 'Win32 Thread' (0x1110) has exited with code 0 (0x0).<br />
<b>First-chance exception at 0x00850a2b in testStackObjects.exe: 0xC0000005: Access violation reading location 0x00850a2b.</b><br />
The thread 'DebuggerRCThread::ThreadProcStatic' (0x1534) has exited with code 0 (0x0).<br />
The thread 'RPC Callback Thread' (0x12b8) has exited with code 0 (0x0).<br />
...<br />
</code><br />
Lets ramp up WinDbg, attach to the process (that _getch comes in useful here) and find out what&#8217;s going on in a bit more detail.</p>
<p>If we let the app run to the point of failure in WinDbg, we can see that the CLR was in the act of shutting down when it caused the exception:<br />
<code><br />
0:002> kp<br />
ChildEBP RetAddr<br />
WARNING: Frame IP not in any known module. Following frames may be wrong.<br />
00dae3fc 79f4c1b5 0xe06ff8<br />
00dae450 79f4c26c mscorwks!ReleaseTransitionHelper+0x5f<br />
00dae494 79f4c2d0 mscorwks!SafeReleaseHelper+0x8c<br />
00dae4c8 79faaa01 mscorwks!SafeRelease+0x2f<br />
00dae4fc 79faa7c8 mscorwks!IUnkEntry::Free+0x68<br />
00dae510 79faa91d mscorwks!RCW::ReleaseAllInterfaces+0x18<br />
00dae540 79faa949 mscorwks!RCW::ReleaseAllInterfacesCallBack+0xbd<br />
00dae570 7a0792ac mscorwks!RCW::Cleanup+0x22<br />
00dae57c 7a079714 mscorwks!RCWCleanupList::ReleaseRCWListRaw+0x16<br />
00dae5ac 7a0797df mscorwks!RCWCleanupList::ReleaseRCWListInCorrectCtx+0xdf<br />
00dae5fc 79fdc140 mscorwks!RCWCleanupList::CleanupAllWrappers+0x77<br />
00dafe90 79fdc7aa mscorwks!RCWCache::ReleaseWrappersWorker+0x103<br />
00dafed8 79fd9f95 mscorwks!ReleaseRCWsInCaches+0x27<br />
00dafee0 79f3c76a mscorwks!InnerCoEEShutDownCOM+0x1e<br />
00daff14 79f92015 mscorwks!WKS::GCHeap::FinalizerThreadStart+0x1fc<br />
00daffb4 7c80b683 mscorwks!Thread::intermediateThreadProc+0x49<br />
00daffec 00000000 kernel32!BaseThreadStart+0x37<br />
</code><br />
Essentially it&#8217;s cleaning up the currently unused RCWs &#8211; including our malformed one &#8211; and as part of doing this, it&#8217;s calling Release on the underlying COM object, via the mscorwks!SafeRelease function. SafeRelease wraps the call to potentially (and definitely, in this case) dangerous unmanaged code with various exception handlers, enabling it to silently handle access violations. </p>
<p>If we run the app again, and this time break while it&#8217;s waiting for the keypress, before it attempts to clean up the RCWs, then we can examine the wrapper ourselves, using the approach I set out in <a href="http://www.voyce.com/index.php/2009/09/03/getting-iunknown-from-__comobject/">this post</a>.</p>
<p>List all of the untyped COM object wrappers:</p>
<pre>
0:002> !dumpheap -type System.__ComObject
 Address       MT     Size
<font color="blue"><b>01418628</b></font> 79306e60       16
total 1 objects
Statistics:
      MT    Count    TotalSize Class Name
79306e60        1           16 System.__ComObject
Total 1 objects
</pre>
<p>Use the address of the object to obtain its object header:<br />
<code><br />
0:002> dd <font color="blue">1418628</font>-4 L1<br />
01418624  0800<font color="red"><b>0002</b></font><br />
</code><br />
Use the syncblk identifier in the header to get the syncblk:</p>
<pre>
0:002> !syncblk <font color="red">2</font>
Index SyncBlock MonitorHeld Recursion Owning Thread Info  SyncBlock Owner
    2 <font color="darkgreen"><b>001e4d9c</b></font>            0         0 00000000     none    01418628 System.__ComObject
-----------------------------
Total           2
CCW             0
RCW             0
ComClassFactory 0
Free            0
</pre>
<p>Get the address of the RCW from the sync block:<br />
<code><br />
0:008> dd <font color="darkgreen">001e4d9c</font>+1c L1<br />
001e4db8  001e7dc8<br />
0:008> dd 001e7dc8+c L1<br />
001e7dd4  <font color="purple"><b>001de828</b></font><br />
</code><br />
And dump out the relevant bits of the RCW, the vtable of the object, at offset 0&#215;88, and the IUnknown pointer, at offset 0&#215;64:<br />
<code><br />
0:008> dds <font color="purple">001de828</font>+88 L1<br />
001de8b0  0041ac78 testStackObjects!MyClass::`vftable'<br />
0:008> dds <font color="purple">001de828</font>+64 L1<br />
001de88c  0012fe7c<br />
</code><br />
We can use <code>!address</code> to do a quick sanity check on the pointer and verify what we know to be the case; it&#8217;s stack memory:</p>
<pre>
0:008> !address 0012fe7c
    00030000 : 00124000 - 0000c000
                    Type     00020000 MEM_PRIVATE
                    Protect  00000004 PAGE_READWRITE
                    State    00001000 MEM_COMMIT
                    Usage    <b>RegionUsageStack</b>
                    Pid.Tid  490.13dc
</pre>
<p>If we run the app on again to the point that it fails, we can clearly see the address of the object being passed as an argument to <code>mscorwks!IUnkEntry::Free</code>.</p>
<p>So the moral of the story is; don&#8217;t pretend some arbitrary piece of stack memory is a real, reference counted COM object. You may be saving the cost of a heap allocation, but even if your app works OK today, it may not tomorrow when someone introduces a piece of .NET code somewhere in your object graph.</p>
<h4>Bonus Extra Content</h4>
<p>As a bonus tip, here are a couple of WinDbg breakpoints that can be used to dump each RCW as it&#8217;s created and destroyed.<br />
<code><br />
bu 79faa974 "dds @ecx L23; g"<br />
bu 79faa538 "dd @esp+20 L1; dds poi(@esp+20)+88 L1; g"<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2010/01/21/beware-of-using-stack-based-com-objects-from-net/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting IUnknown from __ComObject</title>
		<link>http://www.voyce.com/index.php/2009/09/03/getting-iunknown-from-__comobject/</link>
		<comments>http://www.voyce.com/index.php/2009/09/03/getting-iunknown-from-__comobject/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 22:51:35 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WinDbg]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[mscorwks]]></category>
		<category><![CDATA[RCW]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=330</guid>
		<description><![CDATA[How do you find the unmanaged COM object that's being referenced by a .NET object?]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working in an environment with a lot of mixed managed (F#) and unmanaged (C++ COM) code. One of the big problems with this is the mix of lifetime management techniques; .NET uses garbage collection while COM relies on reference counting. Furthermore .NET garbage collection is somewhat non-deterministic, which adds further complexity.</p>
<p>So quite often in our mixed code-base, we find that the .NET garbage collection process doesn&#8217;t kick in when we need it to. For instance, when we&#8217;ve allocated a lot of memory in the COM code that .NET isn&#8217;t aware of. Memory exhaustion has to get pretty bad for the GC to occur at any other time than during a .NET allocation, either the system-wide low-memory event has to be signalled or an <code>OutOfMemoryException</code> needs to be thrown. In both of these cases it&#8217;s probably too late to do anything about it.</p>
<p>In this case it&#8217;s extremely useful to be able to see what .NET objects are still alive, and what COM objects they&#8217;re hanging on to. Unfortunately this isn&#8217;t as easy as it might seem.<br />
<span id="more-330"></span><br />
The COM object itself hides within a weakly-typed <code>System.__ComObject</code> or a strongly-typed managed wrapper, depending on whether rich type information is available. Furthermore, a runtime controller RCW (runtime callable wrapper) is what actually holds a pointer to the object itself, and this structure is internal to mscorwks.dll.</p>
<p>So how can we untangle this and, on finding a <code>__ComObject</code> that happens to still be alive (i.e. is not reachable in the object graph and is therefore eligible for garbage collection) identify which COM object it&#8217;s hanging on to.</p>
<p>First of all, let&#8217;s see how many <code>__ComObjects</code> are still alive. In this case, it&#8217;s only one (phew!):</p>
<pre>
0:005> !DumpHeap -type __ComObject
 Address       MT     Size
01453b74 79306e60       16
total 1 objects
Statistics:
      MT    Count    TotalSize Class Name
79306e60        1           16 System.__ComObject
Total 1 objects
</pre>
<p>And you remember the layout of .NET objects in memory, don&#8217;t you? Of course you do! The 4 bytes prior to the address displayed (<code>01453b74</code>) are the &#8220;object header&#8221;. The exact content of the header is apparently undocumented. Let&#8217;s see what it contains (at least on a 32-bit platform, your mileage may vary):</p>
<pre>
0:005> dd 01453b74-4 L1
01453b70  08000002
</pre>
<p>According to various sources the object header contains 2 fields; a handle and a sync block index. If the object is an RCW, the handle is always 0&#215;08000. You can use the index with SOS&#8217;s <code>!syncblk</code> command to de-reference it:</p>
<pre>
0:005> !syncblk 2
Index SyncBlock MonitorHeld Recursion Owning Thread Info  SyncBlock Owner
    2 001e0fec            0         0 00000000     none    01453b74 System.__ComObject
-----------------------------
Total           3
CCW             0
RCW             1
ComClassFactory 0
Free            0
</pre>
<p>The sync block itself is an undocumented structure, but after a bit of investigation, it turns out that at offset 0&#215;1c there is a pointer to a further structure that contains the &#8220;interop information&#8221;:</p>
<pre>
0:005> dd 001e0fec+1c L1
001e1008  001e9510
</pre>
<p>And from this, we can obtain a pointer to the RCW itself. We&#8217;re almost there!</p>
<pre>
0:005> dd 001e9510+c L1
001e951c  001e5380
</pre>
<p>The RCW is a pretty large structure, but for our purposes there are only a couple of interesting fields: the IUnknown pointer at 0&#215;64, and the object&#8217;s virtual function table pointer at 0&#215;88. If you use <code>dds</code> you can easily see any symbols associated with these pointers:</p>
<pre>
0:005> dds 01e5380+64 L1
001e53e4  00ef6c24
</pre>
<pre>
0:005> dds 01e5380+88 L1
001e5408  00eb9710 rcwrepro!ATL::CComObject<ctestObject>::`vftable'
</pre>
<p>This is the salient information; we now know exactly what type of COM object we&#8217;re dealing with. This is obviously a bit fragile, given that it relies on structures from mscorwks that may well change in newer versions of the runtime (I&#8217;ll check on .NET 4 when I get a chance). It&#8217;s also a bit of a pain to go through all these steps manually in WinDbg, so I put together a simple extension DLL to do it automatically given the address of the __ComObject. I&#8217;ll upload that and blog about it soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2009/09/03/getting-iunknown-from-__comobject/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Troubleshooting an InvalidProgramException</title>
		<link>http://www.voyce.com/index.php/2008/10/01/troubleshooting-an-invalidprogramexception/</link>
		<comments>http://www.voyce.com/index.php/2008/10/01/troubleshooting-an-invalidprogramexception/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 18:58:53 +0000</pubDate>
		<dc:creator>ian</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WinDbg]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[.local]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[gac]]></category>
		<category><![CDATA[InvalidProgramException]]></category>
		<category><![CDATA[mscoree]]></category>
		<category><![CDATA[mscorjit]]></category>
		<category><![CDATA[mscorwks]]></category>

		<guid isPermaLink="false">http://www.voyce.com/?p=45</guid>
		<description><![CDATA[One of the developers who uses the .NET/COM interop DLL that I wrote recently informed me that when they used it in a certain way, they would get a fatal System.InvalidProgramException. Nasty. So I set about seeing how I could find out exactly why this was happening: here are the gory details&#8230;
To spoil the ending [...]]]></description>
			<content:encoded><![CDATA[<p>One of the developers who uses the .NET/COM interop DLL that I wrote recently informed me that when they used it in a certain way, they would get a fatal System.InvalidProgramException. Nasty. So I set about seeing how I could find out exactly why this was happening: here are the gory details&#8230;</p>
<p>To spoil the ending somewhat, we already knew the precise situation that was causing the problem. This is different to the average issue, where most of the time&#8217;s spent in isolating the conditions that cause it. In this case we had an excellent positive and negative repro case.</p>
<p>The users were attempting to use the legacy .local approach to changing DLL probing. This was originally implemented around the time of Windows 2000, before the current side-by-side system existed, and basically short-circuited the normal DLL search order to inject the current directory as the first potential location. See the <a href="http://msdn.microsoft.com/en-us/library/ms682600%28VS.85%29.aspx">DLL redirection</a> MSDN page for more information.</p>
<p>There was already a version of the assembly in question installed in the GAC, they just wanted to use the .local file to override it. The trouble was, with certain builds of the DLL (we&#8217;ll call it XYZ.dll) in the directory along with the redirection file (appname.exe.local), an fatal InvalidProgramException would be generated by the .NET execution engine. With other builds of the same DLL it worked. Mysterious.</p>
<p>So the first thing to do was to try and find out exactly where the exception was occurring. I ramped up WinDbg, and took a look at what exceptions were being thrown:<br />
<code><br />
(42c.1e90): CLR notification exception - code e0444143 (first chance)<br />
(42c.1e90): Unknown exception - code 02345678 (first chance)<br />
(42c.1e90): C++ EH exception - code e06d7363 (first chance)<br />
(42c.1e90): C++ EH exception - code e06d7363 (first chance)<br />
(42c.1e90): C++ EH exception - code e06d7363 (first chance)<br />
(42c.1e90): CLR exception - code e0434f4d (first chance)<br />
</code><br />
The last one generated a second chance exception and the exit. I changed the exception filter set to stop on .NET notifications &#8211; which are generated when assemblies are loaded &#8211; and for C++ exceptions:<br />
<code><br />
0:000&gt; sxe clrn<br />
0:000&gt; sxe eh<br />
</code><br />
After restarting and running on from the CLR notification (generated whenever an assembly is loaded) I got a stack trace on the C++ exception that was being generated within the CLR:<br />
<code><br />
0:000&gt; kP<br />
ChildEBP RetAddr<br />
0012eea4 78158e89 KERNEL32!RaiseException+0x53<br />
0012eedc 79fce58e MSVCR80!_CxxThrowException+0x46<br />
0012ef0c 7a015309 mscorwks!RealCOMPlusThrow+0xd8<br />
0012f2ac 79f0e795 mscorwks!UnsafeJitFunction+0x365<br />
0012f350 79e87f52 mscorwks!MethodDesc::MakeJitWorker+0x1c1<br />
0012f3a8 79e8809e mscorwks!MethodDesc::DoPrestub+0x486<br />
0012f3f8 00341f3e mscorwks!PreStubWorker+0xeb<br />
</code><br />
Hmmmm, interesting, so it looks like the JIT compiler is failing. I also noticed that there was a strange exception that I didn&#8217;t recognise:<br />
<code><br />
(42c.1e90): Unknown exception - code 02345678 (first chance)<br />
</code><br />
You&#8217;ve got to love those &#8220;made up&#8221; numbers. I added an exception filter for it and restarted again (good job this failure was quick to manifest).<code><br />
</code><br />
Now I got an even more precise fault location:<br />
<code><br />
0:000&gt; kP<br />
ChildEBP RetAddr<br />
0012ec44 790af56c KERNEL32!RaiseException+0x53<br />
0012ec60 7909deea mscorjit!getJit+0x38<br />
0012ec7c 79066362 mscorjit!Compiler::fgFindJumpTargets+0x33d<br />
0012ece0 790614c6 mscorjit!Compiler::fgFindBasicBlocks+0x4d<br />
0012ed20 79061236 mscorjit!Compiler::compCompile+0x2bf<br />
0012edb4 7906118c mscorjit!jitNativeCode+0xb8<br />
0012edec 79f0f9cf mscorjit!CILJit::compileMethod+0x3d<br />
0012ee58 79f0f945 mscorwks!invokeCompileMethodHelper+0x72<br />
0012ee9c 79f0f8da mscorwks!invokeCompileMethod+0x31<br />
0012eef4 79f0ea33 mscorwks!CallCompileMethodWithSEHWrapper+0x84<br />
0012f2ac 79f0e795 mscorwks!UnsafeJitFunction+0x230<br />
0012f350 79e87f52 mscorwks!MethodDesc::MakeJitWorker+0x1c1<br />
0012f3a8 79e8809e mscorwks!MethodDesc::DoPrestub+0x486<br />
0012f3f8 00341f3e mscorwks!PreStubWorker+0xeb<br />
</code><br />
So now its looking like something&#8217;s going wrong deep down in the JITter. After stepping through the disassembly for a while I decided to have a look at precisely what method was being compiled at this point. You can use the DumpMD command from the sos extension to examine the method, which is the first parameter to UnsafeJitFunction (you can see parameters in the stack trace by using kb):<br />
<code><br />
0:000&gt; !DumpMD 0161ad08<br />
Method Name: XXX()<br />
Class: 01670a40<br />
MethodTable: 0161ad18<br />
mdToken: 0601626b<br />
Module: 00d3323c<br />
IsJitted: no<br />
m_CodeOrIL: ffffffff<br />
</code><br />
Now this is suspicious; the method as listed (XXX &#8211; name changed to protect the innocent) isn&#8217;t what we were expecting. In fact, it&#8217;s completely different. Let&#8217;s see what IL is being JITted:<br />
<code><br />
0:000&gt; !DumpIL 161ad08<br />
error decoding IL<br />
</code><br />
OK, so this is completely broken metadata.</p>
<p>Now, given that we were using the .local, we would have expected to see the DLL being loaded from the same directory as the application, so lets take a look and see what we&#8217;ve got loaded:<br />
<code><br />
0:000&gt; !DumpDomain<br />
--------------------------------------<br />
System Domain: 7a3bc8b8<br />
...snip...<br />
Assembly: 001abe28 [C:\WINNT\assembly\GAC_MSIL\XYZ.DLL]<br />
ClassLoader: 001aa0b0<br />
SecurityDescriptor: 001aa018<br />
Module Name<br />
00d3323c C:\WINNT\assembly\GAC_MSIL\XYZ.DLL<br />
</code><br />
So this looks like the culprit; we&#8217;re still loading the version from the GAC, even though there&#8217;s a .local file present. Just to verify this I set a breakpoint on CreateFileW to show me which DLLs were being loaded and by what:<br />
<code><br />
0:000&gt; bu KERNEL32!CreateFileW "dpu (@esp+4) L1"<br />
</code><br />
This tells the debugger to display the unicode string that is the first parameter to CreateFileW whenever it&#8217;s hit.<br />
There are obviously lots of DLLs loaded, even in this simple case, but eventually it led me to the point where the GAC version was being loaded:<br />
<code><br />
0012cf44 001ac5a0 "C:\WINNT\assembly\GAC_MSIL\XYZ.dll"<br />
eax=00000000 ebx=00000001 ecx=79e7f683 edx=7c90eb94 esi=001ac4e0 edi=7c80ac0f<br />
eip=7c810760 esp=0012cf40 ebp=0012cf64 iopl=0 nv up ei pl nz na po nc<br />
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00000202<br />
KERNEL32!CreateFileW:<br />
7c810760 8bff mov edi,edi</code></p>
<p><code>0:000&gt; kp L30<br />
ChildEBP RetAddr<br />
0012cf3c 79e90f12 KERNEL32!CreateFileW<br />
0012cf64 79f29306 mscorwks!WszCreateFile+0x72<br />
0012cfac 79ed27ac mscorwks!PEImage::GetFileHandle+0x60<br />
0012cfdc 79e981ae mscorwks!PEImage::GetLayoutInternal+0xcf<br />
0012d020 79e98e69 mscorwks!PEImage::GetLayout+0x8a<br />
0012d0bc 79e98934 mscorwks!RuntimeOpenImageInternal+0x103<br />
0012d104 79e988a9 mscorwks!GetAssemblyMDInternalImportEx+0x9d<br />
0012d11c 79ec314d mscorwks!CreateMetaDataImport+0x16<br />
0012d13c 79ec30ee mscorwks!CAssemblyManifestImport::Init+0x35<br />
0012d164 79ed39ca mscorwks!CreateAssemblyManifestImport+0x53<br />
0012d180 79ed3943 mscorwks!CreateAssemblyFromManifestFile+0x48<br />
0012d3e8 79ed380f mscorwks!CheckExistsInGAC+0x179<br />
0012d418 79ed320a mscorwks!CreateAssemblyFromCacheLookup+0x9b<br />
...snip...</code></p>
<p>So then I waited until the exception was fired, and used !dlls to show me what other DLLs were loaded:<br />
<code><br />
0:000&gt; !dlls<br />
...<br />
0x00243a68: C:\Temp\interopx\XYZ.dll<br />
Base 0x036a0000 EntryPoint 0x00000000 Size 0x0095c000<br />
Flags 0x90404000 LoadCount 0x00000001 TlsIndex 0x00000000<br />
LDRP_ENTRY_PROCESSED<br />
LDRP_COR_IMAGE<br />
...<br />
</code><br />
Ouch. We&#8217;ve loaded some metadata from the c:\winnt\assembly\GAC_MSIL\XYZ.dll version, and then we&#8217;ve actually loaded the c:\temp\interopx\XYZ.dll module. It turned out that in some cases the metadata matched, and we got away with it, and at other times (for different builds) it didn&#8217;t and we hit the InvalidProgramException.</p>
<p>So, the moral of the story is, don&#8217;t mix .local and the .NET assembly loader. And if you do, prepare to break out your WinDbg skills.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.voyce.com/index.php/2008/10/01/troubleshooting-an-invalidprogramexception/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

