<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Johannes Passing&#039;s Blog &#187; Debugging</title>
	<atom:link href="http://jpassing.com/category/debugging/feed/" rel="self" type="application/rss+xml" />
	<link>http://jpassing.com</link>
	<description></description>
	<lastBuildDate>Tue, 22 May 2012 05:39:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jpassing.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Johannes Passing&#039;s Blog &#187; Debugging</title>
		<link>http://jpassing.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jpassing.com/osd.xml" title="Johannes Passing&#039;s Blog" />
	<atom:link rel='hub' href='http://jpassing.com/?pushpress=hub'/>
		<item>
		<title>Windows Hotpatching: A Walkthrough</title>
		<link>http://jpassing.com/2011/05/03/windows-hotpatching-a-walkthrough/</link>
		<comments>http://jpassing.com/2011/05/03/windows-hotpatching-a-walkthrough/#comments</comments>
		<pubDate>Tue, 03 May 2011 12:00:32 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[WDK]]></category>
		<category><![CDATA[WinDBG]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=45</guid>
		<description><![CDATA[As discussed in the last post, Windows 2003 SP1 introduced a technology known as Hotpatching. An integral part of this technology is Hotpatching, which refers to the process of applying an updated on the fly by using runtime code modification techniques. Although Hotpatching has caught a bit of attention, suprisingly little information has been published [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=45&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As discussed <a href='http://jpassing.com/?p=545'>in the last post</a>, Windows 2003 SP1 introduced a technology known as Hotpatching. An integral part of this technology is <i>Hotpatching</i>, which refers to the process of applying an updated on the fly by using runtime code modification techniques.</p>
<p>Although Hotpatching has caught a bit of attention, suprisingly little information has been published about its inner workings. As the technology is patented, however, there is quite a bit of information that can be obtained by reading the <a href='http://www.patentdebate.com/PATAPP/20040107416'>patent description</a>. Moreover, there is <a href='http://www.openrce.org/articles/full_view/22'>this</a> (admittedly very terse) discussion about the actual implementation of hotpatching.</p>
<p>Armed with this information, it is possible to get into more detail by looking what is actually happening under the hood when a hoftix is applied: I did so and chose KB911897 as an example, which fixes <a href='http://www.microsoft.com/technet/security/bulletin/ms06-030.mspx'>some flaw in mrxsmb.sys and rdbss.sys</a>. I have also gone through the hassle of translating key parts of the respective assembly code back to C.</p>
<h3>Preparing the machine</h3>
<p>First, we need a proper machine image which can be used for the experiment. Unfortunately, KB911897 is an SP1 package, so we have to use an old Win 2003 Server SP1 system to apply this update. Once we have the machine running, we can attach the kernel debugger and see what is happening when the hotfix is installed.</p>
<h3>Observing the update</h3>
<p>When launched with /hotpatch:enable, after some initialization work, the updater calls <tt>NtSetSystemInformation</tt> (which delegates to <tt>ExApplyCodePatch</tt>) to apply the hotpatch. Hotpatching includes a coldpatch, which I do not care about here and the actual hotpatch. The first two calls to <tt>NtSetSystemInformation</tt> (and thus to <tt>ExApplyCodePatch</tt>) are coldpatching-related and I will thus ignore them here. The third call, however, is made to apply the actual hotpatch, so let&#8217;s observe this one further.</p>
<p>Requiring a kernel mode-patch, <tt>ExApplyCodePatch</tt> then calls <tt>MmHotPatchRoutine</tt>, which is where the fun starts. Expressed in C, <tt>MmHotPatchRoutine</tt>, MmHotPatchRoutine roughly looks like this (reverse engineered from assembly, might be slightly incorrect):</p>
<pre>
NTSTATUS MmHotPatchRoutine(
  __in PSYSTEM_HOTPATCH_CODE_INFORMATION RemoteInfo
  )
{
  UNICODE_STRING ImageFileName;
  DWORD Flags = RemoteInfo-&gt;Flags;
  PVOID ImageBaseAddress;
  PVOID ImageHandle;
  NTSTATUS Status, LoadStatus;
  KTHREAD CurrentThread;

  ImageFileName.Length = RemoteInfo-&gt;KernelInfo.NameLength;
  ImageFileName.MaximumLength = RemoteInfo-&gt;KernelInfo.NameLength;
  ImageFileName.Buffer = ( PBYTE ) RemoteInfo + NameOffset;

  CurrentThread = KeGetCurrentThread();
  KeEnterCriticalRegion( CurrentThread );

  KeWaitForSingleObject(
    MmSystemLoadLock,
    WrVirtualMemory,
    0,
    0,
    0 );

  LoadStatus = MmLoadSystemImage(
    &amp;ImageFileName,
    0,
    0,
    0,
    &amp;ImageHandle,
    &amp;ImageBaseAddress );
  if ( NT_SUCCESS( Status ) || Status == STATUS_IMAGE_ALREADY_LOADED )
  {

    Status = MiPerformHotPatch(
      ImageHandle,
      ImageBaseAddress,
      Flags );
    
    if ( NT_SUCCESS( Status ) || LoadStatus == STATUS_IMAGE_ALREADY_LOADED )
    {
      NOTHING;
    }
    else
    {
      MmUnloadSystemImage( ImageHandle );
    }
    
    LoadStatus = Status;
  }


  KeReleaseMutant(
    MmSystemLoadLock,
    1,  // increment
    FALSE,
    FALSE );

  KeLeaveCriticalRegion( CurrentThread );

  return LoadStatus;
}
</pre>
<p>As you see in the code, MmHotPatchRoutine will try load the hotpatch image &#8212; we can verify this in the debugger:</p>
<blockquote><pre>
<b>kd&gt; bp nt!MmLoadSystemImage</b>

<b>kd&gt; g</b>
Breakpoint 3 hit
nt!MmLoadSystemImage:
808ec4b5 6878010000      push    178h

<b>kd&gt; k</b>
ChildEBP RetAddr  
f6acbb28 80990c9e nt!MmLoadSystemImage
f6acbb68 809b2d67 nt!MmHotPatchRoutine+0x59
f6acbba8 808caeff nt!ExApplyCodePatch+0x191
f6acbd50 8082337b nt!NtSetSystemInformation+0xa1e
f6acbd50 7c82ed54 nt!KiFastCallEntry+0xf8
0006bc50 7c821f24 ntdll!KiFastSystemCallRet
0006bd44 7c8304c9 ntdll!ZwSetSystemInformation+0xc
[...]

<b>kd&gt; dt _UNICODE_STRING poi(@esp+4)</b>
ntdll!_UNICODE_STRING
 "\??\c:\windows\system32\drivers\hpf3.tmp"
   +0x000 Length           : 0x50
   +0x002 MaximumLength    : 0x50
   +0x004 Buffer           : 0x81623fa8  "\??\c:\windows\system32\drivers\hpf3.tmp"
   
<b>kd&gt; gu</b>

<b>kd&gt; lm</b>
start    end        module name
[...]           
f6ba4000 f6bad000   hpf3       (deferred)  
[...]
f95cb000 f9641000   mrxsmb     (deferred)  
f9641000 f9671000   rdbss      (deferred)      
[...]
</pre>
</blockquote>
<p>Having loaded the hotpatch image, <tt>MmHotPatchRoutine</tt> proceeds be calling <tt>MiPerformHotPatch</tt>, which looks about like this:</p>
<pre>
NTSTATUS
MiPerformHotPatch(
  IN PLDR_DATA_TABLE_ENTRY ImageHandle,
  IN PVOID ImageBaseAddress,
  IN DWORD Flags
  )
{
  PHOTPATCH_HEADER SectionData ;
  PRTL_PATCH_HEADER Header;    
  NTSTATUS Status;
  PVOID LockVariable;
  PVOID LockedBuffer;
  BOOLEAN f;
  PLDR_DATA_TABLE_ENTRY LdrEntry;

  SectionData = RtlGetHotpatchHeader( ImageBaseAddress );
  if ( ! SectionData  )
  {
    return STATUS_INVALID_PARAMETER;
  }
  
  //
  // Try to get header from MiHotPatchList
  //
  Header = RtlFindRtlPatchHeader(
    MiHotPatchList,
    ImageHandle );

  if ( ! Header )
  {
    PLIST_ENTRY Entry;

    if ( Flags &amp; FLG_HOTPATCH_ACTIVE )
    {
      return STATUS_NOT_SUPPORTED;
    }

    Status = RtlCreateHotPatch(
      &amp;Header,
      SectionData,
      ImageHandle,
      Flags
      );
    if ( ! NT_SUCCESS( Status ) )
    {
      return Status;
    }

    ExAcquireResourceExclusiveLite(
      PsLoadedModuleResource,
      TRUE
      );

    Entry =  PsLoadedModuleList;
    while ( Entry != PsLoadedModuleList )
    {
      LdrEntry = DataTableEntry = CONTAINING_RECORD( Entry,
                                            KLDR_DATA_TABLE_ENTRY,
                                            InLoadOrderLinks )
      if ( LdrEntry-&gt;DllBase DllBase &gt;= MiSessionImageEnd )
      {
        if ( RtlpIsSameImage( Header, LdrEntry ) )
        {
          break;
        }
      }
    }

    ExReleaseResourceLite( PsLoadedModuleResource );

    if ( ! PatchHeader-&gt;TargetDllBase )
    {
      Status = STATUS_DLL_NOT_FOUND ;
    }

    Status = ExLockUserBuffer(
      ImageHandle-&gt;DllBase,
      ImageHandle-&gt;SizeOfImage,
      KernelMode,
      IoWriteAccess,
      LockedBuffer,
      LockVariable
      );
    if ( ! NT_SUCCESS( Status ) )
    {
      FreeHotPatchData( Header );
      return Status;
    }


    Status = RtlInitializeHotPatch(
      ( PRTL_PATCH_HEADER ) Header,
      ( PBYTE ) LockedBuffer - ImageHandle-&gt;DllBase
      );

    ExUnlockUserBuffer( LockVariable );

    if ( ! NT_SUCCESS( Status ) )
    {
      FreeHotPatchData( ImageHandle );
      return Status;
    }

    f = 1;
  }
  else
  {
    if ( ( Flags ^ ImageHandle-&gt;CodeInfo-&gt;Flags ) &amp; FLG_HOTPATCH_ACTIVE )
    {
      return STATUS_NOT_SUPPORTED;
    }

    if ( ! ( ImageHandle-&gt;CodeInfo-&gt;Flags &amp; FLG_HOTPATCH_ACTIVE ) )
    {
      Status = RtlReadHookInformation( Header );
      if ( ! NT_SUCCESS( Status ) )
      {
        return Status;
      }
    }

    f = 0;
  }
  
  Status = MmLockAndCopyMemory(
    ImageHandle-&gt;CodeInfo,
    KernelMode
    );
  if ( NT_SUCCESS( Status ) )
  {
    if ( ! f  )
    {
      return Status;
    }

    LdrEntry-&gt;EntryPointActivationContext = Header;  // ???
    InsertTailList( MiHotPatchList, LdrEntry-&gt;PatchList );
  }
  else
  {
    if ( f ) 
    {
      RtlFreeHotPatchData( Header );
    }
  }

  return Status;
}
</pre>
<p>So <tt>MiPerformHotPatch</tt> inspects the hotpatch information stored in the hotpatch image. This data includes information about which code regions need to be updated. After the neccessary information has been gathered, it applies the code changes.</p>
<p>Two basic problems have to be overcome now: On the one hand, all code sections of drivers are mapped read/execute only. Overwring the instructions thus does not work. On the other hand, the system has to properly synchronize the patching process, i.e. it has to make sure no CPU is currently executing the code that is about to be patched.</p>
<p>To overcome the memory protection problems, Windows facilitates a trick I previously only knew from malware: It creates a memory descriptor list (MDL) for the affected code region, maps the MDL, and updates the code through this mapped region. The memory protection is thus circumvented. As it turns, out, there is even a handy, undocumented helper routine for this purpose: <tt>ExLockUserBuffer</tt>, which is used by <tt>MiPerformHotPatch</tt>.</p>
<p>To proceed, MiPerformHotPatch calls <tt>MmLockAndCopyMemory</tt> to do the actual patching. So how does Windows synchronize the update process? Again, it uses a technique I assumed was a malware trick: It schedules CPU-specific DPCs on all CPUs but the current and keeps those DPCs busy while the current thread is uddating the code. Again, Windows provides a neat routine for that: <tt>KeGenericCallDpc</tt>. In addition to this, Windows raises the IRQL to <i>clock level</i> in order to mask all interrupts.</p>
<p>Here is the pseudo-code for <tt>MmLockAndCopyMemory</tt> and its helper, <tt>MiDoCopyMemory</tt>:</p>
<pre>
NTSTATUS
MmLockAndCopyMemory (
    IN PSYSTEM_HOTPATCH_CODE_INFORMATION PatchInfo,
    IN KPROCESSOR_MODE ProbeMode
    )
{
  PVOID Buffer;
  NTSTATUS Status;
  UINT Index;

  if ( 0 == PatchInfo-&gt;CodeInfo.DescriptorsCount )
  {
    return STATUS_SUCCESS;
  }

  Buffer = ExAllocatePoolWithQuotaTag( 
    9,
    PatchInfo-&gt;CodeInfo.DescriptorsCount * 2,
    'PtoH' );
  if ( ! Buffer )
  {
    return STATUS_INSUFFICIENT_RESOURCES;
  }
  RtlZeroMemory( Buffer, PatchInfo-&gt;CodeInfo.DescriptorsCount * 2 );

  if ( 0 == PatchInfo-&gt;CodeInfo.DescriptorsCount )
  {
    Status = STATUS_INVALID_PARAMETER;
    goto Cleanup;
  }

  for ( Index = 0; Index CodeInfo.DescriptorsCount; Index++ )
  {
    if ( PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeOffset &gt; PatchInfo-&gt;InfoSize ||
       PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeSize &gt; PatchInfo-&gt;InfoSize ||
       PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeOffset +
       PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeSize &gt; PatchInfo-&gt;InfoSize || 
       /* other checks... */ )
    {
      Status = STATUS_INVALID_PARAMETER;
      goto Cleanup;
    }

    Status = ExLockUserBuffer(
      TargetAddress,
      PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeSize
      ProbeMode,
      IoWriteAccess,
      &amp;PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].MappedAddress,
      Buffer[ Index ]
      );
    if ( ! NT_SUCCESS( Status ) )
    {
      goto Cleanup;
    }
  }

  PatchInfo-&gt;Flags |= FLG_HOTPATCH_ACTIVE;

  KeGenericCallDpc(
    MiDoCopyMemory,
    PatchInfo );

  if ( PatchInfo-&gt;Flags &amp; FLG_HOTPATCH_VERIFICATION_ERROR )
  {
    PatchInfo-&gt;Flags &amp;= ~FLG_HOTPATCH_ACTIVE;
    PatchInfo-&gt;Flags &amp;= ~FLG_HOTPATCH_VERIFICATION_ERROR;
    Status = STATUS_DATA_ERROR;
  }

Cleanup:
  if ( PatchInfo-&gt;CodeInfo.DescriptorsCount &gt; 0 )
  {
    for ( Index = 0; Index CodeInfo.DescriptorsCount; Index++ )
    {
      ExUnlockUserBuffer( Buffer[ Index ] );
    }
  }

  ExFreePoolWithTag( Buffer, 0 );
  return Status;
}

VOID MiDoCopyMemory(
  IN PKDPC Dpc,
  IN PSYSTEM_HOTPATCH_CODE_INFORMATION PatchInfo,
  IN ULONG NumberCpus,
  IN DEFERRED_REVERSE_BARRIER ReverseBarrier
  )
{
  KIRQL OldIrql;
  UNREFERENCED_PARAMETER( Dpc );
  NTSTATUS Status;
  ULONG Index;

  OldIrql = KfRaiseIrql( CLOCK1_LEVEL );

  //
  // Decrement reverse barrier count.
  //
  Status = KeSignalCallDpcSynchronize( ReverseBarrier );
  if ( ! NT_SUCCESS( Status ) )
  {
    goto Cleanup;
  }

  PatchInfo-&gt;Flags &amp;= ~FLG_HOTPATCH_VERIFICATION_ERROR;
    
  for ( Index = 0; Index CodeInfo.DescriptorsCount; Index++ )
  {
    if ( PatchInfo-&gt;Flags &amp; FLG_HOTPATCH_ACTIVE )
    {
      if ( PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].ValidationSize != 
        RtlCompareMemory(
          PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].MappedAddress,
          ( PBYTE ) PatchInfo + PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].ValidationOffset,
          PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].ValidationSize ) )
      {

        if ( PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeSize != 
          RtlCompareMemory(
            PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].MappedAddress,
            ( PBYTE ) PatchInfo + PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].OrigCodeOffset,
            PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeSize ) )
        {
          PatchInfo-&gt;Flags &amp;= FLG_HOTPATCH_VERIFICATION_ERROR;
          break;
        }
      }
    }
    else
    {
      if ( PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeSize !=
        RtlComparememory(
          PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].MappedAddress,
          ( PBYTE ) PatchInfo + PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeOffset,
          PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeSize ) )
      {
        PatchInfo-&gt;Flags &amp;= FLG_HOTPATCH_VERIFICATION_ERROR;
        break;
      }
    }
  }

  //loc_479533
  if ( PatchInfo-&gt;Flags &amp; FLG_HOTPATCH_VERIFICATION_ERROR ||
     PatchInfo-&gt;CodeInfo.DescriptorsCount &lt;= 0 )
  {
    goto Cleanup;
  }

  for ( Index = 0; Index CodeInfo.DescriptorsCount; Index++ )
  {
    PVOID Source;
    if ( PatchInfo-&gt;Flags &amp; FLG_HOTPATCH_ACTIVE )
    {
      Source = ( PBYTE ) PatchInfo + PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeOffset;
    }
    else
    {
      Source = ( PBYTE ) PatchInfo + PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].OrigCodeOffset;
    }

    RtlCopyMemory(
      PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].MappedAddress,
      Source,
      PatchInfo-&gt;CodeInfo.CodeDescriptors[ Index ].CodeSize
      );
  }


Cleanup:
   KeSignalCallDpcSynchronize( ReverseBarrier );
   KfLowerIrql( OldIrql );
   KeSignalCallDpcDone( NumberCpus );
}
</pre>
<p>To see the code, in action, we set a breakpoint on nt!MiDoCopyMemory:</p>
<blockquote><pre>
<b>kd&gt; k</b>
ChildEBP RetAddr  
f6acbac0 8087622f nt!MiDoCopyMemory
f6acbae8 80990a10 nt!KeGenericCallDpc+0x3d
f6acbb0c 80990bea nt!MmLockAndCopyMemory+0xf1
f6acbb34 80990cba nt!MiPerformHotPatch+0x143
f6acbb68 809b2d67 nt!MmHotPatchRoutine+0x75
f6acbba8 808caeff nt!ExApplyCodePatch+0x191
f6acbd50 8082337b nt!NtSetSystemInformation+0xa1e
</pre>
</blockquote>
<p>Before letting  MiDoCopyMemory do its work, let&#8217;s see what it is about to do. No modifications have yet been done to mrxsmb:</p>
<blockquote><pre>
<b>kd&gt; !chkimg mrxsmb</b>
0 errors : mrxsmb 

<b>kd&gt; !chkimg rdbss</b>
0 errors : rdbss
</pre>
</blockquote>
<p>The second argument is a structure holding the information garthered previously, peeking into it reveals:</p>
<blockquote><pre>
<b>kd&gt; dd /c 1 poi(esp+8) l 4</b>
81583008  00000001
8158300c  00000149
81583010  00000008   &lt;-- # of code patches
81583014  f9648b1f   &lt;-- hmm...
</pre>
</blockquote>
<p>As it turns out, address 81583014 refers to a variable length array of size 8. Poking aroud with dd, the following listing suggests that the structure is of size 28 bytes:</p>
<blockquote><pre>
<b>kd&gt; dd /c 7 81583014</b>
81583014  f9648b1f fa2afb1f 000000ec 00000005 000000f1 000000f6 00000005
81583030  f9648b24 fa2b2b24 000000fb 00000002 000000fd 000000ff 00000002
8158304c  f96585ef fa2b15ef 00000101 00000005 00000106 0000010b 00000005
81583068  f96585f4 fa2b45f4 00000110 00000002 00000112 00000114 00000002
81583084  f9658569 fa2b3569 00000116 00000005 0000011b 00000120 00000005
815830a0  f965856e fa2b656e 00000125 00000002 00000127 00000129 00000002
815830bc  f9653378 fa2b5378 0000012b 00000005 00000130 00000135 00000005
815830d8  f965337d fa2b837d 0000013a 00000005 0000013f 00000144 00000005
</pre>
</blockquote>
<p>Given that rdbss was loaded to address range f9641000-f9671000, it is obvious that the first 2 columns refer to code addresses. The third, fifth and sixth column looks like an offset, the fourth and seventh like the length of the code change. First, let&#8217;s see where the first column points to:</p>
<blockquote><pre>
<b>kd&gt; u f9648b1f</b>
rdbss!RxInitiateOrContinueThrottling+0x6b:
f9648b1f 90              nop
f9648b20 90              nop
f9648b21 90              nop
f9648b22 90              nop
f9648b23 90              nop
rdbss!RxpCancelRoutine:
f9648b24 8bff            mov     edi,edi
f9648b26 55              push    ebp
f9648b27 8bec            mov     ebp,esp
</pre>
</blockquote>
<p>Now that looks promising, especially since the fourth column holds the value 5. Let&#8217;s look at the second row:</p>
<blockquote><pre>
<b>kd&gt; u f9648b24</b>
rdbss!RxpCancelRoutine:
f9648b24 8bff            mov     edi,edi
</pre>
</blockquote>
<p>No doubt, the first and second row define the two patches necessary to redirect RxpCancelRoutine. But what to replace this code with? As it turns out, the offsets in column three are relative to the structure and point to the code that is to be written:</p>
<blockquote><pre>
<b>kd&gt; u poi(esp+8)+000000ec</b>
815830f4 e9dcc455fd      jmp     7eadf5d5          mov     edi,edi

<b>kd&gt; u poi(esp+8)+000000fb</b>
81583103 ebf9            jmp     815830fe
</pre>
</blockquote>
<p>That makes perfectly sense &#8212; the five nops are to be overwritten by a near jump, the mov edi, edi will be replaced by a short jump. </p>
<p>So let&#8217;s run <tt>MiDoCopyMemory</tt> and have a look at the results. Back in <tt>MmLockAndCopyMemory</tt>, the code referred to by the first to rows look like this:</p>
<blockquote><pre>
<b>kd&gt; u f9648b1f</b>
rdbss!RxInitiateOrContinueThrottling+0x6b:
f9648b1f e9dcc455fd      jmp     hpf3!RxpCancelRoutine (f6ba5000)

<b>kd&gt; u f9648b24</b>
rdbss!RxpCancelRoutine:
f9648b24 ebf9            jmp     rdbss!RxInitiateOrContinueThrottling+0x6b (f9648b1f)
f9648b26 55              push    ebp
f9648b27 8bec            mov     ebp,esp

</pre>
</blockquote>
<p>Voilà, RxpCancelRoutine has been patched and calls are redirected to hpf3!RxpCancelRoutine, the new routine located in the auxiliarry &#8216;hpf3&#8242; driver. All that remains to be done is cleanup (unlocking the memory etc).</p>
<p>That&#8217;s it &#8212; that&#8217;s how Windows applies patches on the fly using hotpatching. Too bad that the technology is so rarely used in practice.</p>
<br />Filed under: <a href='http://jpassing.com/category/debugging/'>Debugging</a>, <a href='http://jpassing.com/category/kernel/'>Kernel</a>, <a href='http://jpassing.com/category/wdk/'>WDK</a>, <a href='http://jpassing.com/category/windbg/'>WinDBG</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=45&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2011/05/03/windows-hotpatching-a-walkthrough/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Windows Hotpatching</title>
		<link>http://jpassing.com/2011/05/01/windows-hotpatching/</link>
		<comments>http://jpassing.com/2011/05/01/windows-hotpatching/#comments</comments>
		<pubDate>Sun, 01 May 2011 12:56:00 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[WDK]]></category>
		<category><![CDATA[WinDBG]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=545</guid>
		<description><![CDATA[Several years ago, with Windows Server 2003 SP1, Microsoft introduced a technology and infrastructure called Hotpatching. The basic intent of this infrastructure is to provide a means to apply hotfixes on the fly, i.e. without having to reboot the system &#8212; even if the hotfix contains changes on critical system components such as the kernel [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=545&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Several years ago, with Windows Server 2003 SP1, Microsoft introduced a technology and infrastructure called <i>Hotpatching</i>. The basic intent of this infrastructure is to provide a means to apply hotfixes on the fly, i.e. without having to reboot the system &#8212; even if the hotfix contains changes on critical system components such as the kernel iteself, important drivers, or user mode libraries such as shell32.dll. </p>
<p>Trying to applying hotfixes on the fly introduces a variety of problems &#8212; the most important being:</p>
<ul>
<li>Patching code that is currently in use</li>
<li>Atomically replacing files on disk that are currently in use and therefore locked</li>
<li>Making sure that all changes take effect for both, processes currently running and processes which are yet to be started (i.e. before the next reboot)</li>
<li>Allowing further hotfixes to be applied on system that has not been rebooted since the last hotfix has been applied in an on-the-fly fashion</li>
</ul>
<p>The Windows Hotpatching infrastructure is capable of handling all these cases &#8212; it is, however, not applicable to all kinds of code fixes. Generally speaking, it can only be used for fixes that merely comprise smallish code changes but do not affect layout or semantics of data structures. A fix for a buffer overflow caused by an off-by-one error, however, is a perfect example for a fix that could certainly be applied using the Hotpatching infrastructure.</p>
<p>That all sounds good and nice, but reality is that we still reboot our machines for just about every update Microsoft provides us, right? </p>
<p>Right. The answer for this is threefold. First, as indicated, some hotfixes can be expected to make changes that cannot be safely applied using the Hotpatching system. Secondly, Hotpatching is used on an opt-in basis, so you will not benefit from it automatically: When a hotpatch-enabled hotfix is applied through Windows Update or by launching the corresponding exe file, it is not used and a reboot will be required. The user has to explicitly specify the <code>/hotpatch:enable</code> switch in order to have the hotfix to be applied on the fly.</p>
<p>In the months after the release of SP1, a certain fraction of the hotfixes issued by Microsoft were indeed hotpatch-enabled and could be applied without a reboot. Interestingly, however, I am not aware of a single hotfix issued since Server 2003 SP2 that supported hotpatching!</p>
<p>And thirdly: Whether Microsoft has lost faith in their hotpatching facility, whether the effort to test such hotfixes turned out to be too high or whether there were other reasons speaking against issueing hotpatch-enabled hotfixes &#8212; I do not know.</p>
<p>Notwithstanding this observation, Hotpatching is an interesting technology that deserves to be looked at in more detail. Although I will not cover the entire infrastructure, I will spend at least one more blog post on the mechanisms implemented in Windows that allow code modifications to be performed on the fly. That is, I will focus on the <i>hotpatching</i> part of the infrastructure and will ignore <i>coldpatching</i> and other, smaller aspects of the infrastructre.</p>
<br />Filed under: <a href='http://jpassing.com/category/debugging/'>Debugging</a>, <a href='http://jpassing.com/category/kernel/'>Kernel</a>, <a href='http://jpassing.com/category/wdk/'>WDK</a>, <a href='http://jpassing.com/category/windbg/'>WinDBG</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/545/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=545&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2011/05/01/windows-hotpatching/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>The hidden danger of forgetting to specify %SystemRoot% in a custom environment block</title>
		<link>http://jpassing.com/2009/12/28/the-hidden-danger-of-forgetting-to-specify-systemroot-in-a-custom-environment-block/</link>
		<comments>http://jpassing.com/2009/12/28/the-hidden-danger-of-forgetting-to-specify-systemroot-in-a-custom-environment-block/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 20:42:02 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[COM]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[cryptoapi]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[procmon]]></category>
		<category><![CDATA[SXS]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=607</guid>
		<description><![CDATA[When spawning a process using CreateProcess and friends, the child process usually inherits the environment (i.e. all environment variables) of the spawning process. Of course, this behavior can be overridden by creating a custom environment block and passing it to the lpEnvironment parameter of CreateProcess. While the MSDN documentation on CreateProcess does contain a remark [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=607&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When spawning a process using CreateProcess and friends, the child process usually inherits the environment (i.e. all environment variables) of the spawning process. Of course, this behavior can be overridden by creating a custom environment block and passing it to the <code>lpEnvironment</code> parameter of <code>CreateProcess</code>.</p>
<p>While the <a href='http://msdn.microsoft.com/en-us/library/ms682425(VS.85).aspx'>MSDN documentation on CreateProcess</a> does contain a remark saying that current directory information (<code>=C:</code> and friends) should be included in such a custom environment block, it does not mention the importance of <code>SystemRoot</code>.</p>
<p>The <code>SystemRoot</code> environment variable usually contains the path c:\windows &#8212; the path that is also accessible using the <code>GetWindowsDirectory</code> function. This environment variable, as it turns out, is not only handy for scripting purposes &#8212; it is, in fact, essential for the proper operation of many libraries.</p>
<p>For very simple programs, forgetting to include <code>SystemRoot</code> in a custom environment block usually goes unnoticed &#8212; even an empty environment block works just fine. In case of more complex applications, however, the omission of this variable can quickly lead to errors &#8212; on Vista, the most common error that can be tracked back to a missing <code>SystemRoot</code> variable is SXS failing to find/load basic system libraries.</p>
<p>Now that we have Windows 7, <code>SystemRoot</code> seems to have become even more important: Now it is not only SXS that requires <code>SystemRoot</code> to be specified properly, but also CryptoAPI.</p>
<p>In my particular case, I was experiencing a 0&#215;80090006 (&#8220;Invalid Signature&#8221;, NTE_BAD_SIGNATURE) error whenever the child process attempted to call <code>CoGetObject</code> to retrieve a pointer to a DCOM object. While this error occured on Windows 7, the same code worked fine on Windows Vista and XP. </p>
<p>Given this more than general error message, it seemed anything but clear to me what the problem was, so I attached a debugger to the child process (using gflags/Image File Execution Options). Once I did that, I got the following messages in my debug output output: </p>
<blockquote><p>
CryptAcquireContext:  CheckSignatureInFile failed at cryptapi.c line 5198<br />
CryptAcquireContext:  Failed to read registry signature value at cryptapi.c line 873
</p></blockquote>
<p>I set a breakpoint on <code>CryptAcquireContextW</code> and looked at the stack trace:</p>
<blockquote><pre>

0:000&gt; k
ChildEBP RetAddr  
0008f8a4 75760a4f ole32!CRandomNumberGenerator::Initialize+0x2e
0008f8b0 75760769 ole32!CRandomNumberGenerator::GenerateRandomNumber+0xd
0008f8e8 757609cf ole32!CStdMarshal::AddIPIDEntry+0x48
0008f93c 75766aae ole32!CStdMarshal::MarshalServerIPID+0x5a
0008f994 75767519 ole32!CStdMarshal::MarshalObjRef+0xb9
0008f9c8 7576778e ole32!MarshalInternalObjRef+0x8c
0008fa4c 757676ba ole32!CRemoteUnknown::CRemoteUnknown+0x3b
0008fa8c 7576754a ole32!CComApartment::InitRemoting+0x19c
0008fa98 7586d83e ole32!CComApartment::StartServer+0x13
0008faa8 757652b3 ole32!InitChannelIfNecessary+0x1e
0008fb20 757fc046 ole32!CoUnmarshalInterface+0x38
0008fb34 757fd3d5 ole32!CObjrefMoniker::Load+0x26
0008fb70 7573cb7f ole32!CObjrefMonikerFactory::ParseDisplayName+0x16f
0008fbbc 7573caae ole32!FindClassMoniker+0x8b
0008fbf4 75789dc7 ole32!MkParseDisplayName+0xbb
0008fc3c 6954ce84 ole32!CoGetObject+0x82
...
</pre>
</blockquote>
<p>Quite obviously, COM, trying to unmarshal an interface, needed a random number and attempted to use CryptoAPI for this purpose. Looking at the paramters of CryptAcquireContext, I saw that the <i>Microsoft Strong Cryptographic Provider</i> was attempted to be loaded &#8212; one of the standard Windows CSPs &#8212; so everything seemed normal.</p>
<p>Guided by the message <i>Failed to read registry signature</i>, I switched to <i>Process Monitor</i> to see which registry key was being queried: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Cryptography\Defaults\Provider\Microsoft Strong Cryptographic Provider.</p>
<p>Taking a look at this key in regedit, it did not take long before spotting <code>SystemRoot</code> as the culprit:</p>
<p><img src="http://jpassing.files.wordpress.com/2009/12/regedit.png?w=500" alt="" title="Regedit" class="alignright size-medium wp-image-610" /></p>
<p>Looking at file system activity in <i>Process Monitor</i> proved this:</p>
<p><img src="http://jpassing.files.wordpress.com/2009/12/procmon3.png?w=500&h=164" alt="Process Monitor" title="Process Monitor" width="500" height="164" class="alignright size-full wp-image-618" /></p>
<p>Interestingly, on Windows Vista, all <code>Image Path</code> values in the CSP registry keys do <i>not</i> use <code>SystemRoot</code> &#8212; they just contain the file name and rely on the library path in order to locate the libraries at runtime. This explains why my code worked fine on Vista.</p>
<p>(While making this change, the developer seemed to forget to change the value&#8217;s type from REG_SZ to REG_EXPAND_SZ though :) )</p>
<p><b>Bottom Line 1</b>: Always, <i>always</i> include <code>SystemRoot</code> when passing a custom environment block to CreateProcess.</p>
<p><b>Bottom Line 2</b>: The case also shows how a seemingly trivial change in Windows (using an absolute path rather then just a file name in the CSP registry key) can lead to an application incompatibility.</p>
<br />Posted in COM, Debugging, Win32 Tagged: api, cryptoapi, process, procmon, SXS, Win32 <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/607/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/607/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/607/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=607&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2009/12/28/the-hidden-danger-of-forgetting-to-specify-systemroot-in-a-custom-environment-block/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>

		<media:content url="http://jpassing.files.wordpress.com/2009/12/regedit.png" medium="image">
			<media:title type="html">Regedit</media:title>
		</media:content>

		<media:content url="http://jpassing.files.wordpress.com/2009/12/procmon3.png" medium="image">
			<media:title type="html">Process Monitor</media:title>
		</media:content>
	</item>
		<item>
		<title>Introducing cfix studio, the Visual Studio AddIn for C/C++ Unit Testing</title>
		<link>http://jpassing.com/2009/06/23/introducing-cfix-studio-the-visual-studio-addin-for-cc-unit-testing/</link>
		<comments>http://jpassing.com/2009/06/23/introducing-cfix-studio-the-visual-studio-addin-for-cc-unit-testing/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 18:32:01 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[cfix]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Visual Assert]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=290</guid>
		<description><![CDATA[N.B. cfix studio was the code name of what has become Visual Assert There is little doubt that native code, and C and C++ in particular, is here to stay. And still, it is pretty obvious that when it comes to tools and IDEs, it is the managed world that has gotten most attention from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=290&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style='color:red;font-style:italic;padding:20px;'>N.B. cfix studio was the code name of what has become <a href='http://www.visualassert.com/'><b>Visual Assert</b></a></span></p>
<p>There is little doubt that native code, and C and C++ in particular, is here to stay. And still, it is pretty obvious that when it comes to tools and IDEs, it is the managed world that has gotten most attention from tool vendors over the past years. </p>
<p>While there are lots and lots of useful tools for native development, many of them probably even better than their managed counterparts, there are some areas where the managed language fraction is far ahead: One of these areas certainly is IDE support for unit testing.</p>
<p>JUnit for Eclipse, TestDriven.Net for Visual Studio and MS Test make test-driven development <i>so much</i> more convenient and efficient that it is almost ridiculous that using command line tools is still state of the art for C/C++ development.</p>
<p>That said, there is great news: With <a href='http://www.cfix-studio.com'>cfix studio</a>, there finally is a solution filling in this gap! cfix studio, <a href='http://jpassing.wordpress.com/2009/06/23/cfix-1-4-released/'>based on the cfix unit testing framework</a>, is a Visual Studio-AddIn that allows you to easily write, manage, run, and debug your unit tests from within Visual Studio. No fiddling with command line tools, complex configuration, or boilerplate code required! </p>
<p>Among <a href='http://www.cfix-studio.com/unit-testing-framework/key-features.html'>lots of other features</a>, cfix studio also has first-class support for multi-architecture development – you can easily switch back and forth between 32-bit and 64-bit and can even mix tests of different architectures in a single test run. Needless to say, cfix studio, like cfix, is also fully compatible to WinUnit. </p>
<p>If that has caught your interest, you are invited to check out the first beta version of cfix studio:</p>
<div style='padding-left:50px;padding-top:20px;padding-bottom:20px;'>
<p><b><a href='http://cfix-studio.com/unit-testing-framework/download.html'>Download cfix studio Beta 1</a></b><br />
Version 1.0.0.3458
</div>
<p>It is free, quick to install and comes with a set of example projects. Give it a try &#8212; and please <a href='https://cfix.fogbugz.com/default.asp?pg=pgPublicEdit&amp;ixArea=16'>let me know about all your crticism, suggestions and other feedback</a>!</p>
<p>Here are some screenshots of cfix studio in action:</p>
<p><a href="http://cfix-studio.com/unit-testing-framework/images/stories/explorer-contextmenu.png"><img src="http://jpassing.files.wordpress.com/2009/06/explorer-contextmenu-thumb.png?w=500" alt="Test Explorer"   class="alignnone size-full wp-image-300" /></a><br />
<i>The Test Explorer allows you to start a single or set of tests, the Run Window shows the results</i></p>
<p><a href='http://cfix-studio.com/unit-testing-framework/images/stories/run-running.png'><img src="http://jpassing.files.wordpress.com/2009/06/run-running-thumb.png?w=500" alt="Run Window" title="Run Window: Viewing test progress"   class="alignnone size-full wp-image-299" /></a><br />
<i>Run Window: Viewing test progress</i></p>
<p><a href="http://cfix-studio.com/unit-testing-framework/images/stories/run-failed-assertion.png"><img src="http://jpassing.files.wordpress.com/2009/06/run-failed-assertion-thumb.png?w=500" alt="Failed Assertion" title="Failed Assertion"   class="alignnone size-full wp-image-303" /></a><br />
<i>Run Window: Viewing test results and details of a failed assertion</i></p>
<p><a href="http://cfix-studio.com/unit-testing-framework/images/stories/debug-failed-assertion.png"><img src="http://jpassing.files.wordpress.com/2009/06/debug-failed-assertion-thumb.png?w=500" alt="Debgging a failed assertion" title="Debgging a failed assertion"   class="alignnone size-full wp-image-294" /></a><br />
<i>When running in the debugger, a failed assertion will hit a breakpoint and the Run Window will show additional details</i></p>
<br />Posted in cfix, Debugging, Testing, Tools, Visual Assert, Visual Studio  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/290/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/290/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/290/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/290/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/290/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/290/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/290/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/290/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/290/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/290/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/290/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/290/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/290/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/290/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=290&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2009/06/23/introducing-cfix-studio-the-visual-studio-addin-for-cc-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>

		<media:content url="http://jpassing.files.wordpress.com/2009/06/explorer-contextmenu-thumb.png" medium="image">
			<media:title type="html">Test Explorer</media:title>
		</media:content>

		<media:content url="http://jpassing.files.wordpress.com/2009/06/run-running-thumb.png" medium="image">
			<media:title type="html">Run Window: Viewing test progress</media:title>
		</media:content>

		<media:content url="http://jpassing.files.wordpress.com/2009/06/run-failed-assertion-thumb.png" medium="image">
			<media:title type="html">Failed Assertion</media:title>
		</media:content>

		<media:content url="http://jpassing.files.wordpress.com/2009/06/debug-failed-assertion-thumb.png" medium="image">
			<media:title type="html">Debgging a failed assertion</media:title>
		</media:content>
	</item>
		<item>
		<title>cfix 1.4 released</title>
		<link>http://jpassing.com/2009/06/23/cfix-1-4-released/</link>
		<comments>http://jpassing.com/2009/06/23/cfix-1-4-released/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 18:27:38 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[cfix]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=311</guid>
		<description><![CDATA[Today, a new version of cfix, the open source unit testing framework for user and kernel mode C and C++, has been released. cfix 1.4, in addition to the existing feature of allowing test runs to be restricted to specific fixtures, now also allows single testcases to be run in isolation, which can be a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=311&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today, a new version of cfix, the open source unit testing framework for user and kernel mode C and C++, has been released. cfix 1.4, in addition to the existing feature of allowing test runs to be restricted to specific fixtures, now also allows single testcases to be run in isolation, which can be a great aid in debugging. Besides several minor fixes, the cfix API has been slightly enhanced and cfix now degrades more gracefully in case of dbghelp-issues.</p>
<p>Updated cfix binaries and source code are now <a href='http://sourceforge.net/project/showfiles.php?group_id=218233&amp;package_id=263204'>available for download</a></p>
<br />Posted in cfix, Debugging, Testing, Tools, Win32  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/311/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/311/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/311/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=311&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2009/06/23/cfix-1-4-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Uniquely Identifying a Module&#8217;s Build</title>
		<link>http://jpassing.com/2009/04/22/uniquely-identifying-a-modules-build/</link>
		<comments>http://jpassing.com/2009/04/22/uniquely-identifying-a-modules-build/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 19:49:26 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[pe]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=237</guid>
		<description><![CDATA[It is common practice to embed a version resource (VS_VERSIONINFO) into PE images such as DLL and EXE files. While this resource mainly serves informational purposes, the version information is occasionaly used to perform certain checks, such as verifying the module&#8217;s suitability for a particular purpose. Under certain circumstances, however, this versioning information may be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=237&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It is common practice to embed a version resource (VS_VERSIONINFO) into PE images such as DLL and EXE files. While this resource mainly serves informational purposes, the version information is occasionaly used to perform certain checks, such as verifying the module&#8217;s suitability for a particular purpose.</p>
<p>Under certain circumstances, however, this versioning information may be too imprecise: Versions are not necessarily incremented after each build, so it is possible that two copies of a module carry the same versioning information, yet differ significantly in their implementation. In such situations, identifying the actual <i>build</i> of the module might become neccessary.</p>
<p>The most common, but by no means the only situation in which this applies in practice concerns debugging &#8212; to identify the PDB file exactly matching a given module, the debugger must be able to recognize the <i>specific</i> build of a module. It thus does not come as a surprise that all images for which debugging information has been generated contain a dedicated identifier for this purpose: The <i>CodeView signature GUID</i>.</p>
<p>Summarizing what Oleg Starodumov <a href='http://www.debuginfo.com/articles/debuginfomatch.html'>has covered in more detail</a>, cl, when directed to generate a PDB file, implicitly creates this GUID and, along with the path to the PDB file, embeds this data into the PE image. For current versions, the relevant structure used to encode this information is CV_INFO_PDB70, which seems to have been documented once, but not any more:</p>
<blockquote><pre>
typedef struct _CV_INFO_PDB70
{
  ULONG CvSignature;
  GUID Signature;
  ULONG Age;
  UCHAR PdbFileName[ ANYSIZE_ARRAY ];
} CV_INFO_PDB70, *PCV_INFO_PDB70;
</pre>
</blockquote>
<p>In order to be able to locate the structure within the PE image, a directory entry of type IMAGE_DEBUG_TYPE_CODEVIEW is written to the image&#8217;s debug directory. The following code listing demonstrates how to obtain the signature GUID of an image:</p>
<blockquote><pre>
#define PtrFromRva( base, rva ) ( ( ( PUCHAR ) base ) + rva )

static PIMAGE_DATA_DIRECTORY GetDebugDataDirectory(
  __in ULONG_PTR LoadAddress
  )
{
  PIMAGE_DOS_HEADER DosHeader = 
    ( PIMAGE_DOS_HEADER ) ( PVOID ) LoadAddress;
  PIMAGE_NT_HEADERS NtHeader = ( PIMAGE_NT_HEADERS ) 
    PtrFromRva( DosHeader, DosHeader-&gt;e_lfanew );
  ASSERT ( IMAGE_NT_SIGNATURE == NtHeader-&gt;Signature );

  return &amp;NtHeader-&gt;OptionalHeader.DataDirectory
      [ IMAGE_DIRECTORY_ENTRY_DEBUG ];
}

NTSTATUS GetDebugGuid(
  __in ULONG_PTR ModuleBaseAddress,
  __out GUID *Guid
  )
{
  PIMAGE_DATA_DIRECTORY DebugDataDirectory;
  PIMAGE_DEBUG_DIRECTORY DebugHeaders;
  ULONG Index;
  ULONG NumberOfDebugDirs;
  ULONG_PTR ModuleBaseAddress;
  NTSTATUS Status;

  DebugDataDirectory  = DebugDataDirectory( ModuleBaseAddress );
  DebugHeaders    = ( PIMAGE_DEBUG_DIRECTORY ) PtrFromRva( 
    ModuleBaseAddress, 
    DebugDataDirectory-&gt;VirtualAddress );

  ASSERT( ( DebugDataDirectory-&gt;Size % sizeof( IMAGE_DEBUG_DIRECTORY ) ) == 0 );
  NumberOfDebugDirs = DebugDataDirectory-&gt;Size / sizeof( IMAGE_DEBUG_DIRECTORY );

  //
  // Lookup CodeView record.
  //
  for ( Index = 0; Index &lt; NumberOfDebugDirs; Index++ )
  {
    PCV_INFO_PDB70 CvInfo;
    if ( DebugHeaders[ Index ].Type != IMAGE_DEBUG_TYPE_CODEVIEW )
    {
      continue;
    }

    CvInfo = ( PCV_INFO_PDB70 ) PtrFromRva( 
      ModuleBaseAddress, 
      DebugHeaders[ Index ].AddressOfRawData );

    if ( CvInfo-&gt;CvSignature != 'SDSR' )
    {
      //
      // Weird, old PDB format maybe.
      //
      return STATUS_xxx_UNRECOGNIZED_CV_HEADER;
    }

    *Guid = CvInfo-&gt;Signature;
    return STATUS_SUCCESS;  
  }

  return STATUS_xxx_CV_GUID_LOOKUP_FAILED;
}
</pre>
</blockquote>
<br />Posted in Debugging, Kernel, Win32 Tagged: Debugging, image, pe <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/237/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/237/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/237/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=237&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2009/04/22/uniquely-identifying-a-modules-build/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>cfix 1.2 Installer Fixed for AMD64</title>
		<link>http://jpassing.com/2008/11/18/cfix-12-installer-fixed-for-amd64/</link>
		<comments>http://jpassing.com/2008/11/18/cfix-12-installer-fixed-for-amd64/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 11:30:09 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[cfix]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[amd64]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=175</guid>
		<description><![CDATA[The cfix 1.2 package as released last week contained a rather stupid bug that the new build, 1.2.0.3244, now fixes: the amd64 binaries cfix64.exe and cfixkr64.sys were wrongly installed as cfix32.exe and cfixkr32.sys, respectively. Not only did this stand in contrast to what the documenation stated, it also resulted in cfix being unable to load [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=175&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The cfix 1.2 package as released last week contained a rather stupid bug that the new build, 1.2.0.3244, now fixes: the amd64 binaries cfix64.exe and cfixkr64.sys were wrongly installed as cfix32.exe and cfixkr32.sys, respectively. Not only did this stand in contrast to what the documenation stated, it also resulted in cfix being unable to load the cfixkr driver on AMD64 platforms.</p>
<p>The new MSI package is now <a href='https://sourceforge.net/project/showfiles.php?group_id=218233&amp;package_id=263204&amp;release_id=639356'>available for download on Sourceforge</a>.</p>
<br />Posted in cfix, Debugging, Kernel, Testing, Win32 Tagged: amd64, cfix, release <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/175/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=175&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/11/18/cfix-12-installer-fixed-for-amd64/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>cfix 1.2 introduces improved C++ support</title>
		<link>http://jpassing.com/2008/11/10/cfix-12-introduces-improved-c-support/</link>
		<comments>http://jpassing.com/2008/11/10/cfix-12-introduces-improved-c-support/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 10:30:20 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[cfix]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[lgpl]]></category>
		<category><![CDATA[oss]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=163</guid>
		<description><![CDATA[cfix 1.2, which has been released today, introduces a number of new features, the most prominent being improved support for C++ and additional execution options. New C++ API To date, cfix has primarily focussed on C as the programming language to write unit tests in. Although C++ has always been supported, cfix has not made [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=163&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>cfix 1.2, which has been released today, introduces a number of new features, the most prominent being improved support for C++ and additional execution options.</p>
<h3>New C++ API</h3>
<p>To date, cfix has primarily focussed on C as the programming language to write unit tests in. Although C++ has always been supported, cfix has not made use of the additional capabilities C++ provides. With version 1.2, cfix makes C++ a first class citizen and introduces an additional API that leverages the benefits of C++ and allows writing test cases in a more convenient manner.</p>
<p>Being implemented on top of the existing C API, the C++ API is not a replacement, but rather an addition to the existing API set.</p>
<p>As the following example suggests, fixtures can now be written as classes, with test cases being implemented as methods:</p>
<blockquote><pre>
#include &lt;cfixcc.h&gt;

class ExampleTest : public <a href="http://cfix.sourceforge.net/doc/TestFixture.html" title="TestFixture">cfixcc::TestFixture</a>
{
public:
  void TestOne() 
  {}
  
  void TestTwo() 
  {}
};

<a href="http://cfix.sourceforge.net/doc/CFIXCC_BEGIN_CLASS.html" title="CFIXCC_BEGIN_CLASS">CFIXCC_BEGIN_CLASS</a>( ExampleTest )
  <a href="http://cfix.sourceforge.net/doc/CFIXCC_METHOD.html" title="CFIXCC_METHOD">CFIXCC_METHOD</a>( TestOne )
  <a href="http://cfix.sourceforge.net/doc/CFIXCC_METHOD.html" title="CFIXCC_METHOD">CFIXCC_METHOD</a>( TestTwo )
<a href="http://cfix.sourceforge.net/doc/CFIXCC_END_CLASS.html" title="CFIXCC_END_CLASS">CFIXCC_END_CLASS</a>()
</pre>
</blockquote>
<p>To learn more about the definition of fixtures, have a look at the <a href='http://cfix.sourceforge.net/doc/TestFixture.html'>respective TestFixture chapter in the cfix documentation</a>.</p>
<p>Regarding the implementation of test cases, cfix adds a new set of <i>type-safe</i>, template-driven assertions that, for instance, allow convenient <a href="http://cfix.sourceforge.net/doc/CFIXCC_ASSERT_EQUALS.html">equality checks</a>:</p>
<blockquote><pre>
void TestOne() 
{
  const wchar_t* testString = L"test";
  
  //
  // Use typesafe assertions...
  //
  <a href="http://cfix.sourceforge.net/doc/CFIXCC_ASSERT_EQUALS.html" title="CFIXCC_ASSERT_EQUALS">CFIXCC_ASSERT_EQUALS</a>( 1, 1 );
  <a href="http://cfix.sourceforge.net/doc/CFIXCC_ASSERT_EQUALS.html" title="CFIXCC_ASSERT_EQUALS">CFIXCC_ASSERT_EQUALS</a>( L"test", testString );
  <a href="http://cfix.sourceforge.net/doc/CFIXCC_ASSERT_EQUALS.html" title="CFIXCC_ASSERT_EQUALS">CFIXCC_ASSERT_EQUALS</a>( wcslen( testString ), ( size_t ) 4 );
  
  //
  // ...log messages...
  //
  <a href="http://cfix.sourceforge.net/doc/CFIX_LOG.html" title="http://cfix.sourceforge.net/doc/CFIX_LOG">CFIX_LOG</a>( L"Test string is %s", testString );
  
  //
  // ...or use the existing "C" assertions.
  //
  <a href="http://cfix.sourceforge.net/doc/CFIX_ASSERT.html" title="CFIX_ASSERT">CFIX_ASSERT</a>( wcslen( testString ) == 4 );
  <a href="http://cfix.sourceforge.net/doc/CFIX_ASSERT_MESSAGE.html" title="CFIX_ASSERT_MESSAGE">CFIX_ASSERT_MESSAGE</a>( testString[ 0 ] == 't', 
    L"Test string should start with a 't'" );
}
</pre>
</blockquote>
<p>Again, have a look at the updated <a href='http://cfix.sourceforge.net/doc/API.html'>API reference</a> for an overview of the new API additions.</p>
<h3>Customizing Test Runs</h3>
<p>Another important new feature is the addition of the new switches -fsf (<i>Shortcut Fixture</i>), -fsr (<i>Shortcut Run</i>), and -fss (<i>Shortcut Run On Failing Setup</i>). Using these switches allows you to specify how a test run should resume when a test case fails.</p>
<p>When a test case fails, the default behavior of cfix is to report the failure, and resume at the next test case. By specifying -fsf, however, the remaining test cases of the same fixture will be skipped and execution resumes at the next fixture. With -fsr, cfix can be requirested to abort the entire run as soon as a single test case fails. </p>
<h3>What else is new in 1.2?</h3>
<ul>
<li><a class="link" href="http://cfix.sourceforge.net/doc/CFIX_ASSERT_MESSAGE.html" title="CFIX_ASSERT_MESSAGE">CFIX_ASSERT_MESSAGE</a></li>
<li>ANSI support for <a class="link" href="http://cfix.sourceforge.net/doc/CFIX_LOG.html" title="CFIX_LOG">CFIX_LOG</a>, <a class="link" href="http://cfix.sourceforge.net/doc/CFIX_INCONCLUSIVE.html" title="CFIX_INCONCLUSIVE">CFIX_INCONCLUSIVE</a>,<br />
					and <a class="link" href="http://cfix.sourceforge.net/doc/CFIX_ASSERT_MESSAGE.html" title="CFIX_ASSERT_MESSAGE">CFIX_ASSERT_MESSAGE</a> (and the entire C++ API)</li>
<li><a class="link" href="http://cfix.sourceforge.net/doc/CfixPeGetValue.html" title="CfixPeGetValue">CfixPeGetValue</a> and <a class="link" href="http://cfix.sourceforge.net/doc/CfixPeSetValue.html" title="CfixPeSetValue">CfixPeSetValue</a></li>
<li>Kernel mode: Drivers do not need to link against aux_klib.lib any more</li>
<li><a class="link" href="http://cfix.sourceforge.net/doc/CFIX_FIXTURE_BEFORE.html" title="CFIX_FIXTURE_BEFORE">Before</a> and <a class="link" href="http://cfix.sourceforge.net/doc/CFIX_FIXTURE_AFTER.html" title="CFIX_FIXTURE_AFTER">After</a> routines</li>
<li>Support for cl 13.00 and Visual Studio 2003 (in addition to 2005 and 2008)</li>
</ul>
<h3>Download</h3>
<p>As always, cfix 1.2 is source and binary compatible to previous versions. The new MSI package and source code can now be <a href='http://sourceforge.net/project/showfiles.php?group_id=218233&amp;package_id=263204'>downloaded on Sourceforge</a>.</p>
<p>cfix is open source and licensed under the <a href='http://www.gnu.org/licenses/lgpl.html'>GNU Lesser General Public License</a>.</p>
<br />Posted in cfix, Debugging, Kernel, Testing, Win32 Tagged: cfix, Kernel, lgpl, oss, release, tdd, unit testing <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/163/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/163/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/163/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=163&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/11/10/cfix-12-introduces-improved-c-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>How GUI Thread Conversion on Svr03 Breaks the SEH Chain</title>
		<link>http://jpassing.com/2008/10/25/how-gui-thread-conversion-on-svr03-damages-the-seh-chain/</link>
		<comments>http://jpassing.com/2008/10/25/how-gui-thread-conversion-on-svr03-damages-the-seh-chain/#comments</comments>
		<pubDate>Sat, 25 Oct 2008 12:53:55 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[NT]]></category>
		<category><![CDATA[SEH]]></category>
		<category><![CDATA[SSDT]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=154</guid>
		<description><![CDATA[The Windows kernel maintains two types of threads &#8212; Non-GUI threads, and GUI threads. Non-GUI threads threads use the default stack size of 12KB (on i386, which this this discussion applies to) and the default System Service Descriptor table (SSDT), KeServiceDescriptorTable. GUI threads, in contrast, are expected to have much larger stack requirements and thus [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=154&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The Windows kernel maintains two <i>types</i> of threads &#8212; <i>Non-GUI threads</i>, and <i>GUI threads</i>. Non-GUI threads threads use the default stack size of 12KB (on i386, which this this discussion applies to) and the default <i>System Service Descriptor table</i> (SSDT), <code>KeServiceDescriptorTable</code>. GUI threads, in contrast, are expected to have much larger stack requirements and thus use an extended stack size of 60 KB (Note: these are the numbers for Svr03 and may vary among releases). More importantly, however, GUI threads use a different SSDT &#8212; <code>KeServiceDescriptorTableShadow</code>. Unlike <code>KeServiceDescriptorTable</code>, which only supports the basic set of system calls, this SSDT also includes all the <i>User</i> and <i>GDI</i> system services.</p>
<p>All threads start off as Non-GUI threads. Once the application makes a call to a system service that does not fall within the default range, however, the NT kernel will suspect this thread to be about to do GUI stuff &#8212; and will convert the thread into a GUI thread.</p>
<p>Converting a thread to a GUI thread naturally has to entail two things &#8212; swapping the SSDT, and enlarging the stack. While swapping the SSDT is not really interesting, enlarging the stack size poses a challenge &#8212; you cannot really <i>enlarge</i> a stack as the nearby pages that would need to be acquired may not be available.</p>
<p>As a consequence, enlarging the stack works by swapping the stack. The old, small stack is exchanged against a newly allocated, larger stack. Now swapping a stack is not really a common thing to do and is pretty easy to get wrong. And well, as it turns out, the Svr03 kernel did in fact get it wrong.</p>
<p>But let&#8217;s start at the beginning. </p>
<p>When the number of the requested system service is found to be beyond the range supported by the default SSDT, <code>KiConvertToGuiThread</code> is called to perform the thread conversion. <code>KiConvertToGuiThread</code> itself is pretty dumb and lets <code>PsConvertToGuiThread</code> do the actual work.</p>
<p>The following pseudo code illustrates what <code>PsConvertToGuiThread</code> does:</p>
<blockquote><pre>
NTSTATUS PsConvertToGuiThread()
{
  //
  // Create the new stack.
  //
  LargeStack = MmCreateKernelStack( ... )
  
  if ( LargeStack == NULL )
  {
    __try
    {
      //
      // Allocation failed -- set last error value.
      //
      NtCurrentTeb()-&gt;LastErrorValue = ERROR_NOT_ENOUGH_MEMORY;
    }
    __except( ... )
    {
    }
    
    //
    // N.B. We are still on the old stack.
    //
    
    //
    // This will copy the old thread's contents to the new stack and 
    // migrate the context of the current thread to the new stack.
    //
    SmallStack = KeSwitchKernelStack( LargeStack, ... );

    //
    // Now we are on the new stack.
    //
    MmDeleteKernelStack( SmallStack, ... );
  }
  ...
  //
  // Notify Win32k.
  //
  
  ( PspW32ProcessCallout )( ... )
  ...
  ( PspW32ThreadCallout ) ( ... )
  
  ...
}
</pre>
</blockquote>
<p>This code looks innocent enough, but infact, it is <i>lying</i>. Too see why, you have to recall <a href='http://jpassing.wordpress.com/?p=65'>how Structured Exception Handling is implemented on i386</a> and how the C compiler makes use of it (I think I have spent way too much time with SEH over the past months&#8230;): The __try/__except-block at the top of the routine will cause to the compiler to emit the typical SEH prolog at the beginning of the function. The purpose of this prolog is to set up an <code>EXCEPTION_REGISTRATION_RECORD</code> and to put this record onto the current thread&#8217;s SEH chain, which in turn is rooted in the PCR. In the same way, the compiler will put an appropriate epilog to the end of the routine.</p>
<p>So while the code above suggests that the SEH stuff is scoped to the very beginning of the function, it will not be until the end of the function has been reached that the <code>EXCEPTION_REGISTRATION_RECORD</code> is torn down and removed from the SEH chain.</p>
<p>And at this point, it should become clear why this becomes a problem in the context of stack swapping. At the point where <code>KeSwitchKernelStack</code> is called, the <code>EXCEPTION_REGISTRATION_RECORD</code> will still be listed in the SEH chain, although it does not serve any particular purpose any more. So <code>KeSwitchKernelStack</code> is called, which will, as indicated before, copy the contents of the old stack to the new stack &#8212; which, of course, includes the <code>EXCEPTION_REGISTRATION_RECORD</code>.</p>
<p>But&#8230;</p>
<p>neither <code>KeSwitchKernelStack</code>, nor <code>PsConvertToGuiThread</code> updates the SEH pointer in the PCR! After the swapping has been conducted and <code>MmDeleteKernelStack</code> has returned, the root of the SEH chain will point to freed memory &#8212; memory where the <code>EXCEPTION_REGISTRATION_RECORD</code> once has been.</p>
<p>Now two things are worth noting. First, <code>PsConvertToGuiThread</code> can be expected to occupy the bottommost stack frame of the kernel stack. A situation where the dangling pointer could harm a caller of <code>PsConvertToGuiThread</code> is thus not possible.</p>
<p>Secondly, <code>PsConvertToGuiThread</code> makes callouts to Win32k by invoking the callbacks pointed to by <code>PspW32ProcessCallout</code> and <code>PspW32ThreadCallout</code>. And in fact, it is only <code>PsConvertToGuiThread</code>&#8216;s luck that these routines are so well behaved that they do not cause the system to bugcheck because of the dangling pointer. If one of these routines (or routines called by these) did anything with the SEH chain going beyond adding another record to the chain and removing it later, odds were that this routine would dereference a stray pointer&#8230; and would bugcheck the system&#8230;</p>
<p><i>It is worth noting that the implementation of <code>PsConvertToGuiThread</code> has changed in Windows Vista, so that the above discussion does not apply to this and later releases.<br />
</i></p>
<br />Posted in Debugging, Kernel Tagged: GUI, Kernel, NT, SEH, SSDT, thread <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/154/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=154&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/10/25/how-gui-thread-conversion-on-svr03-damages-the-seh-chain/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Effective Leak Detection with the Debug CRT and Application Verifier</title>
		<link>http://jpassing.com/2008/09/01/effective-leak-detection-with-the-debug-crt-and-application-verifier/</link>
		<comments>http://jpassing.com/2008/09/01/effective-leak-detection-with-the-debug-crt-and-application-verifier/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 08:47:27 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[WinDBG]]></category>
		<category><![CDATA[Application Verifier]]></category>
		<category><![CDATA[Avrf]]></category>
		<category><![CDATA[CRT]]></category>
		<category><![CDATA[heap]]></category>
		<category><![CDATA[leak]]></category>
		<category><![CDATA[malloc]]></category>
		<category><![CDATA[memory]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=93</guid>
		<description><![CDATA[Programming memory leaks in C or C++ is easy. Even careful programming often cannot avoid the little mistakes that finally end up in your program having a memory leak. Thankfully, however, there are plenty of helpful tools that assist in finding leaks as early as possible. One especially helpful tool for leak detection is the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=93&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Programming memory leaks in C or C++ is easy. Even careful programming often cannot avoid the little mistakes that finally end up in your program having a memory leak. Thankfully, however, there are plenty of helpful tools that assist in finding leaks as early as possible. </p>
<p>One especially helpful tool for leak detection is the debug CRT. Although the leak detection facilities provided by the debug CRT are not as far-reaching as those of, say, UMDH, using the debug CRT is probably the most friction-less way of identifying leaks.</p>
<p>Of course, the debug CRT will only track allocations of the CRT heap. That is, allocations performed using <tt>malloc</tt> or, in case of C++, the default <tt>operator new</tt>. </p>
<p>So how to enable allocation tracking? As it turns out, it is already enabled by default for the debug heap &#8212; so changing the CRT flags using _CrtSetDbgFlag usually is not even neccessary. All there is to do is to call _CrtDumpMemoryLeaks() at the end of the program.</p>
<p>When exactly is &#8220;the end of the program&#8221;? That depends on which CRT you use. Each CRT uses a separate heap and thus, must have its resources be tracked separately. If your application EXE and your DLLs all link against the DLL version of the CRT, the right moment to call _CrtDumpMemoryLeaks() is at the end of main(). If you use the static CRT, the right moment is when the respective module is about to unload &#8212; for an EXE, this is the end of main() again (atexit is another option). For a DLL, however, this is DllMain (in the DLL_PROCESS_DETACH case).</p>
<p>To illustrate how to make use of this CRT feature, consider the following leaky code:</p>
<blockquote><pre>
#include &lt;crtdbg.h&gt;

class Widget
{
private:
  int a;

public:
  Widget() : a( 0 )
  {
  }
};

void UseWidget( Widget* w )
{
}

int __cdecl wmain()
{
  Widget* w = new Widget();
  UseWidget( w );

  _CrtDumpMemoryLeaks();
  return 0;
}
</pre>
</blockquote>
<p>Running the debug build (i.e. a build using the debug CRT) of this program will yield the following output in the debugger:</p>
<blockquote><pre>
Detected memory leaks!
Dumping objects -&gt;
{124} normal block at 0x008C2880, 4 bytes long.
 Data:  00 00 00 00 
Object dump complete.
</pre>
</blockquote>
<p>So we have a memory leak &#8212; allocation #124 is not freed. The default procedure to locate the leak now is to include a call to _CrtSetBreakAlloc( 124 ) in the program and run it in the debugger &#8212; it will break when allocation #124 is performed. While this practice is ok for smaller programs, it will fail as soon as your program is not fully deterministic any more &#8212; most likely because it uses multiple threads. So for many programs, this technique is pretty much worthless.</p>
<p>But before continueing on the topic of how to find the culprit, there is another catch to discuss. Let&#8217;s include this snippet of code into our program:</p>
<blockquote><pre>
class WidgetHolder
{
private:
  Widget* w;

public:
  WidgetHolder() : w( new Widget() )
  {}

  ~WidgetHolder()
  {
    delete w;
  }
};

WidgetHolder widgetHolder;
</pre>
</blockquote>
<p>No leak here &#8212; we are properly cleaning up. But let&#8217;s see what the debugger window tells:</p>
<blockquote><pre>
Detected memory leaks!
Dumping objects -&gt;
{125} normal block at 0x000328C0, 4 bytes long.
 Data:  00 00 00 00 
{124} normal block at 0x00032880, 4 bytes long.
 Data:  00 00 00 00 
Object dump complete.
</pre>
</blockquote>
<p>Urgh. But the reason should be obvious &#8212; when main() is about to return, ~WidgetHolder has not run yet. As a consequence, WidgetHolder&#8217;s allocation has not been freed yet and _CrtDumpMemoryLeaks will treat this as a leak. Unfortunately, there is no good way to avoid such false positives. Of course, this only holds for C++. For C, this problem does not exist.</p>
<p>Ok, back to the problem of locating the leak. We know that allocation #124 is the problem, but assuming our program does more than the simplistic example, breaking on #124 during the next runs is likely to lead us to ever changing locations. So this information is worthless. That leaves the address of the leaked memory &#8212; 0x008C2880.</p>
<p>At this point, we can leverage the fact that the CRT heap is not really a heap but just a wrapper around the RTL heap. Therefore, we can use the incredibly powerful debugging facilities of the RTL heap to help us out. </p>
<p>In order to fix a leak, it is usually extremely helpful to locate the code having conducted the allocation. Once you have this information, it is often trivial to spot the missing free operation. As it turns out, the the RTL heap&#8217;s <i>page heap</i> feature offers this capability.</p>
<p>Open Application Verifier and enable Heap Checks for our application. By default, this enables the <i>full page heap</i>, but the <i>normal page heap</i> is enough for our case. </p>
<p>Note that for the following discussion, I assume you are using the Visual Studio debugger.</p>
<p>Set a breakpoint on the statement immediately following the _CrtDumpMemoryLeaks() statement and run the application until it breaks there. This time, the locations 0x02CDFFA0 and 0x02CDFF40 are reported as being leaked. Do not continue execution yet.</p>
<p>Rather, open WinDBG and attach <i>noninvasively</i> to the debugged process. VisualStudio is already attached, so we cannot perform a <i>real</i> attach, but a noninvasive attach does the trick.</p>
<p>In WinDBG, we now use the !heap extension to query page heap information:</p>
<blockquote><pre>
0:000&gt; !heap -p -a <b>0x02CDFF40</b>
    address 02cdff40 found in
    _HEAP @ 2cd0000
      HEAP_ENTRY Size Prev Flags    UserPtr UserSize - state
        02cdfef8 000c 0000  [00]   02cdff20    00028 - (busy)
        Trace: 02dc
        776380d8 ntdll!RtlDebugAllocateHeap+0x00000030
[...]
        6a2fab29 MSVCR80D!malloc+0x00000019
        6a34908f MSVCR80D!operator new+0x0000000f
        4115c9 Leak!WidgetHolder::WidgetHolder+0x00000049
        415808 Leak!`dynamic initializer for 'widgetHolder''+0x00000028
        <b>6a2e246a MSVCR80D!_initterm+0x0000001a</b>
        411d33 Leak!__tmainCRTStartup+0x00000103
        411c1d Leak!wmainCRTStartup+0x0000000d
        767b19f1 kernel32!BaseThreadInitThunk+0x0000000e
        7764d109 ntdll!_RtlUserThreadStart+0x00000023

0:000&gt; !heap -p -a <b>0x02CDFFA0</b>
    address 02cdffa0 found in
    _HEAP @ 2cd0000
      HEAP_ENTRY Size Prev Flags    UserPtr UserSize - state
        02cdff58 000c 0000  [00]   02cdff80    00028 - (busy)
        Trace: 02e0
        776380d8 ntdll!RtlDebugAllocateHeap+0x00000030
[...]
        6a2fab29 MSVCR80D!malloc+0x00000019
        6a34908f MSVCR80D!operator new+0x0000000f
        <b>411464 Leak!wmain+0x00000044</b>
        411dd6 Leak!__tmainCRTStartup+0x000001a6
        411c1d Leak!wmainCRTStartup+0x0000000d
        767b19f1 kernel32!BaseThreadInitThunk+0x0000000e
        7764d109 ntdll!_RtlUserThreadStart+0x00000023
        
</pre>
</blockquote>
<p>Aha, stack traces! The remaining analysis is almost trivial: 0x02CDFF40 has been allocated on behalf of WidgetHolder::WidgetHolder. WidgetHolder::WidgetHolder, however, is not indirectly invoked by wmain, but rather by MSVCR80D!_initterm! That is a strong indication for this being a global object that can be ignored in this analysis.</p>
<p>0x02CDFFA0, in turn, is allocated by wmain, so this is a real leak. But which allocation is it, exactly? lsa will tell us:</p>
<blockquote><pre>
0:000&gt; lsa Leak!wmain+0x00000044
    33: }
    34: 
    35: int __cdecl wmain()
    36: {
<b>&gt;   37: 	Widget* w = new Widget();</b>
    38: 	UseWidget( w );
    39: 
    40: 	_CrtDumpMemoryLeaks();
    41: 	return 0;
    42: }
</pre>
</blockquote>
<p>There you go, we have found the culprit. </p>
<p>Although simple, I have found this technique to be very effective in practice, as it enables you to find leaks as you develop your code. As Application Verifier should be enabled anyway for any application you are developing on, the technique also turns out to be a lot less laborious than it may seem. It almost certainly is a lot more convenient than routinely doing UMDH runs. To be fair, however, UMDH is able to catch more leaks (non CRT-leaks), so additionally using UMDH remains being a good idea.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/93/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/93/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/93/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/93/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/93/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=93&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/09/01/effective-leak-detection-with-the-debug-crt-and-application-verifier/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging a Debugger Deadlock</title>
		<link>http://jpassing.com/2008/08/23/debugging-a-debugger-deadlock/</link>
		<comments>http://jpassing.com/2008/08/23/debugging-a-debugger-deadlock/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 08:10:28 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[cfix]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[deadlock]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[symbol server]]></category>
		<category><![CDATA[VC]]></category>
		<category><![CDATA[VisualStudio]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=97</guid>
		<description><![CDATA[While I still use VisualStudio 2005 Team System for most of my development, I want to make sure that cfix works properly with VisualStudio 2008 as well. To test that, I recently started a Windows 2003 Server VM, installed VCExpress 2008 and cfix and attempted to run an example project in the VC debugger. As [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=97&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While I still use VisualStudio 2005 Team System for most of my development, I want to make sure that cfix works properly with VisualStudio 2008 as well. </p>
<p>To test that, I recently started a Windows 2003 Server VM, installed VCExpress 2008 and cfix and attempted to run an example project in the VC debugger. As long as no assertions fired, everything seemed fine. I then altered the example&#8217;s source code so that one of the assertion would fail, ran it in the debugger &#8212; and waited. Nothing happened.</p>
<p>When an assertion fires, cfix 1.1 attempts to capture the stack trace to make debugging easier. For this to work, the respective debugging symbols must be loaded, and &#8212; if not yet available &#8212; be downloaded from the symbol server. It is thus not uncommon that it takes a couple of seconds before the failure message appears and the debugger breaks in when an assertion fails. But after about a minute, still nothing happened &#8212; a deadlock seems to have occured.</p>
<p>To make the situation worse, I then noticed that while cfix was hanging, Internet Explorer would not properly start any more. After creating its main window, it would hang as well.</p>
<p>So I rebooted the machine and hooked up a kernel debugger. Luckily, the scenario was easy to reproduce and I could take a closer look&#8230;</p>
<h3>Debugging the deadlock</h3>
<p>First, let&#8217;s find the cfix32 process:</p>
<blockquote><pre>
kd&gt; !process 0 0 <b>cfix32.exe</b>
PROCESS <b>8254e490</b>  SessionId: 0  Cid: 06b0    Peb: 7ffde000  ParentCid: 0658
    DirBase: 06e90000  ObjectTable: e170ee58  HandleCount:  69.
    Image: cfix32.exe
</pre>
</blockquote>
<p>Now that we have the VA, we can switch to the process and load its user mode symbols:</p>
<blockquote><pre>
kd&gt; .process /r /p <b>8254e490  </b>
Implicit process is now 8254e490
.cache forcedecodeuser done
Loading User Symbols
...............................
</pre>
</blockquote>
<p>Looking at this process&#8217; threads, it is easy to see that one stack looks suspicious:</p>
<blockquote><pre>
kd&gt; !process 8254e490  
PROCESS 8254e490  SessionId: 0  Cid: 06b0    
	Peb: 7ffde000  ParentCid: 0658
    DirBase: 06e90000  ObjectTable: e170ee58  HandleCount:  69.
    Image: cfix32.exe
    VadRoot 82455628 Vads 76 Clone 0 Private 491. 
    Modified 8. Locked 0.
    DeviceMap e148f3c0

    [...]

THREAD <b>826276f8 </b> Cid 06b0.06b4  Teb: 7ffdd000 Win32Thread: 
  <b>e1734a38 WAIT: (Unknown) KernelMode Non-Alertable
	f4422b44  SynchronizationEvent</b>
Not impersonating
DeviceMap                 e148f3c0
Owning Process            8254e490       Image:         cfix32.exe
Wait Start TickCount      13474          Ticks: 9138 (0:00:01:31.511)
Context Switch Count      219                 LargeStack
UserTime                  00:00:00.010
KernelTime                00:00:00.010
Win32 Start Address cfix32!wmainCRTStartup (0x010048cf)
Start Address kernel32!BaseProcessStartThunk (0x77e617f8)
Stack Init f4423000 Current f4422aa0 Base f4423000 Limit f441f000 Call 0
Priority 10 BasePriority 8 PriorityDecrement 0

ChildEBP RetAddr  
f4422ab8 808202b6 nt!KiSwapContext+0x25
f4422ad0 8081fb6e nt!KiSwapThread+0x83 
<b>f4422b14 809bae63 nt!KeWaitForSingleObject+0x2e0 </b>
f4422bf4 809bc06d nt!DbgkpQueueMessage+0x178 
f4422c18 8096ba9a nt!DbgkpSendApiMessage+0x45 
f4422cc8 80909942 nt!DbgkMapViewOfSection+0xcf 
f4422d34 8082350b nt!NtMapViewOfSection+0x269 
f4422d34 7c8285ec nt!KiFastCallEntry+0xf8 
00069b94 7c82728b ntdll!KiFastSystemCallRet 
00069b98 7c831e05 ntdll!NtMapViewOfSection+0xc
00069bdc 7c831fd6 ntdll!LdrpMapViewOfDllSection+0x64 
00069ccc 7c833027 ntdll!LdrpMapDll+0x390 
[...]
</pre>
</blockquote>
<p>The thread is blocked &#8212; it is waiting on en event. Events do not have an owner so we have to do a little more to find out, what it is waiting for. Unfortunately, !thread crops the trace, so let us first get the full one:</p>
<blockquote><pre>
kd&gt; .thread <b>826276f8  </b>
kd&gt; kn100
 # ChildEBP RetAddr  
00 f4422ab8 808202b6 nt!KiSwapContext+0x25
01 f4422ad0 8081fb6e nt!KiSwapThread+0x83
02 f4422b14 809bae63 nt!KeWaitForSingleObject+0x2e0
03 f4422bf4 809bc06d nt!DbgkpQueueMessage+0x178
04 f4422c18 8096ba9a nt!DbgkpSendApiMessage+0x45
05 f4422cc8 80909942 nt!DbgkMapViewOfSection+0xcf
06 f4422d34 8082350b nt!NtMapViewOfSection+0x269
07 f4422d34 7c8285ec nt!KiFastCallEntry+0xf8
08 00069b94 7c82728b ntdll!KiFastSystemCallRet
09 00069b98 7c831e05 ntdll!NtMapViewOfSection+0xc
0a 00069bdc 7c831fd6 ntdll!LdrpMapViewOfDllSection+0x64
0b 00069ccc 7c833027 ntdll!LdrpMapDll+0x390
0c 00069f30 7c8330f5 ntdll!LdrpLoadImportModule+0x17c
0d 00069f70 7c8330a4 ntdll!LdrpHandleOneNewFormatImportDescriptor+0x4d
0e 00069f8c 7c833248 ntdll!LdrpHandleNewFormatImportDescriptors+0x1d
0f 0006a014 7c833049 ntdll!LdrpWalkImportDescriptor+0x195
10 0006a264 7c8330f5 ntdll!LdrpLoadImportModule+0x1cb
11 0006a2a4 7c8330a4 ntdll!LdrpHandleOneNewFormatImportDescriptor+0x4d
12 0006a2c0 7c833248 ntdll!LdrpHandleNewFormatImportDescriptors+0x1d
13 0006a348 7c83427d ntdll!LdrpWalkImportDescriptor+0x195
14 0006a5e0 7c834065 ntdll!LdrpLoadDll+0x241
15 0006a85c 77e41bf3 ntdll!LdrLoadDll+0x198
16 0006a8c4 77e41dbd kernel32!LoadLibraryExW+0x1b2
17 0006a8d8 77e41df3 kernel32!LoadLibraryExA+0x1f
18 0006a8f8 46a7870c kernel32!LoadLibraryA+0xb5
19 0006a954 46a93b3e WININET!__delayLoadHelper2+0xfc
1a 0006a994 46a93950 WININET!_tailMerge_RASAPI32_dll+0xd
1b 0006a9a8 46a93a4e WININET!DoConnectoidsExist+0x2b
1c 0006a9d4 46a93abc WININET!GetRasConnections+0x34
1d 0006a9f0 46a8c559 WININET!IsDialUpConnection+0xa9
1e 0006aa0c 46a97a44 WININET!FixProxySettingsForCurrentConnection+0x31
1f 0006b5e4 46aa3774 WININET!InternetQueryOptionA+0xa47
20 0006b748 01d12dc6 WININET!InternetQueryOptionW+0x1fa
21 0006b98c 01d12583 symsrv!StoreWinInet::dumpproxyinfo+0x46
22 0006be04 01d1290a symsrv!StoreWinInet::connect+0x273
23 0006c040 01d05ae7 symsrv!StoreWinInet::find+0x3a
24 0006c134 01d06c47 symsrv!cascade+0x87
25 0006c684 01d06a57 symsrv!SymbolServerByIndexW+0x127
26 0006c8b4 0302e30e symsrv!SymbolServerW+0x77
27 0006ccf4 03018eed dbghelp!symsrvGetFile+0x12e
28 0006d9dc 03019f57 dbghelp!diaLocatePdb+0x33d
29 0006dc58 03041ade dbghelp!diaGetPdb+0x207
2a 0006de7c 0303ff15 dbghelp!GetDebugData+0x2be
2b 0006e324 03040516 dbghelp!modload+0x305
2c 0006e7a4 0304068e dbghelp!LoadModule+0x3f6
2d 0006e9e8 03044eaf dbghelp!GetModule+0x4e
2e 0006ea30 03044bda dbghelp!NTGetProcessModules+0x16f
2f 0006eae8 03032e80 dbghelp!GetProcessModules+0x4a
30 0006ed70 60f03f7a dbghelp!SymInitializeW+0x320
31 0006f17c 60f032b0 cfix!CfixpCaptureStackTrace+0x117 
32 0006f598 100419b5 cfix!CfixPeReportFailedAssertion+0xc5 
WARNING: Stack unwind information not available. 
Following frames may be wrong.
33 0006f7c0 10040f40 VsSample!__CfixFixturePeSimpleAdderTest+0x5ab1
34 0006f8b4 10040db6 VsSample!__CfixFixturePeSimpleAdderTest+0x503c
35 0006fa68 10040a8a VsSample!__CfixFixturePeSimpleAdderTest+0x4eb2
36 0006fb48 60f02b64 VsSample!__CfixFixturePeSimpleAdderTest+0x4b86
37 0006fb84 60f02be6 cfix!CfixsRunTestRoutine+0x33 
38 0006fb94 60f038e9 cfix!CfixsRunTestCaseMethod+0x27 
39 0006fbac 60f03a06 cfix!CfixsRunTestCase+0x25 
3a 0006fbcc 60f03ce5 cfix!CfixsRunTsexecActionMethod+0xfb 
3b 0006fbf0 0100e135 cfix!CfixsRunSequenceAction+0x122 
3c 0006fc2c 0100d5c2 cfix32!CfixrunpRunFixtures+0x90 
3d 0006fc40 0100d85c cfix32!CfixrunsMainWorker+0x3f 
3e 0006fe7c 010046b8 cfix32!CfixrunMain+0x1b9 
3f 0006fee0 0100485e cfix32!wmain+0x80 
40 0006ffc0 77e6f23b cfix32!_wmainCRTStartup+0x12b 
41 0006fff0 00000000 kernel32!BaseProcessStart+0x23
</pre>
</blockquote>
<p>Whoa, what a trace! cfix!CfixpCaptureStackTrace tries to assemble a stack trace, for which it has to initialize dbghelp.dll first. dbghelp!SymInitializeW seeks help of symsrv.dll, which in turn tries to connect to the Microsoft symbol server. Before it can so, it obviously attempts to get its proxy settings straight, which in turn leads to some DLL (rasapi.dll, in case you wonder) being loaded. The loader then calls into the debugging subsystem (nt!Dbgk*). It may be assumed that the loader is notifying the debugger about the DLL having been loaded.</p>
<p>Turining our attention to Internet Explorer, we look at iexplore.exe&#8217;s threads:</p>
<blockquote><pre>
kd&gt; !process 0 0 iexplore.exe
PROCESS <b>824ec3b0</b>  SessionId: 0  Cid: 07f0    Peb: 7ffdb000  ParentCid: 054c
    DirBase: 0307c000  ObjectTable: e15186b8  HandleCount: 225.
    Image: iexplore.exe
    
kd&gt; .process /r /p <b>824ec3b0  </b>
Implicit process is now 824ec3b0
.cache forcedecodeuser done
Loading User Symbols
...............................................

</pre>
</blockquote>
<p>Now, iexplore has <i>lots</i> of threads, but skimming over them, one looked interesting:</p>
<blockquote><pre>
kd&gt; !process 824ec3b0  
PROCESS 824ec3b0  SessionId: 0  Cid: 07f0    
	Peb: 7ffdb000  ParentCid: 054c
    DirBase: 0307c000  ObjectTable: e15186b8  HandleCount: 225.
    Image: iexplore.exe
    VadRoot 824991c8 Vads 168 Clone 0 Private 643. 
    Modified 44. Locked 0.
    DeviceMap e148f3c0

    [...]
    
THREAD 82431980  Cid 07f0.00b8  Teb: 7ffd5000 Win32Thread: 00000000 
  WAIT: (Unknown) UserMode Non-Alertable
	<b>82631eb0  Mutant - owning thread 826276f8</b>
Not impersonating
DeviceMap                 e148f3c0
Owning Process            824ec3b0       Image:         iexplore.exe
Wait Start TickCount      21924          Ticks: 688 (0:00:00:06.889)
Context Switch Count      1             
UserTime                  00:00:00.000
KernelTime                00:00:00.000
Win32 Start Address ntdll!RtlpWorkerThread (0x7c839efb)
Start Address kernel32!BaseThreadStartThunk (0x77e617ec)
Stack Init f40a3000 Current f40a2c78 Base f40a3000 Limit f40a0000 Call 0
Priority 8 BasePriority 8 PriorityDecrement 0

ChildEBP RetAddr  
f40a2c90 808202b6 nt!KiSwapContext+0x25 
f40a2ca8 8081fb6e nt!KiSwapThread+0x83 
f40a2cec 8090e64e nt!KeWaitForSingleObject+0x2e0 
f40a2d50 8082350b nt!NtWaitForSingleObject+0x9a 
f40a2d50 7c8285ec nt!KiFastCallEntry+0xf8 
01e5fdd0 7c827d0b ntdll!KiFastSystemCallRet 
01e5fdd4 77e61d1e ntdll!NtWaitForSingleObject+0xc 
01e5fe44 77e61c8d kernel32!WaitForSingleObjectEx+0xac 
01e5fe58 46a8c54d kernel32!WaitForSingleObject+0x12 
<b>01e5fe74 46a7eeca WININET!FixProxySettingsForCurrentConnection+0x25 </b>
01e5fe8c 46a7ee3f WININET!CFsm_HttpSendRequest::RunSM+0x61 
01e5fea4 46a7efa3 WININET!CFsm::Run+0x39 
01e5fed4 77da5938 WININET!CFsm::RunWorkItem+0x79 
01e5feec 7c83a827 SHLWAPI!ExecuteWorkItem+0x1d 
01e5ff44 7c83aa0b ntdll!RtlpWorkerCallout+0x71 
01e5ff64 7c83aa82 ntdll!RtlpExecuteWorkerRequest+0x4f 
01e5ff78 7c839f60 ntdll!RtlpApcCallout+0x11 
01e5ffb8 77e64829 ntdll!RtlpWorkerThread+0x61 
01e5ffec 00000000 kernel32!BaseThreadStart+0x34 
</pre>
</blockquote>
<p>Now we are getting somewhere. We have seen FixProxySettingsForCurrentConnection in cfix&#8217;s trace already &#8212; but in this case, it is waiting on something. Let&#8217;s see&#8230;</p>
<blockquote><pre>

kd&gt; !object <b>82631eb0  </b>
Object: 82631eb0  Type: (827a5550) Mutant
    ObjectHeader: 82631e98 (old version)
    HandleCount: 3  PointerCount: 6
    Directory Object: e1496420  Name: WininetProxyRegistryMutex
</pre>
</blockquote>
<p>And 826276f8, that&#8217;s the cfix32 thread we have already assessed. Obviously, iexplore waits for cfix to release the WininetProxyRegistryMutex, and cfix waits on someone else.</p>
<p>Turning over to VC, we can find a stack that also contains a call to FixProxySettingsForCurrentConnection on its stack. Again, blocking on WininetProxyRegistryMutex.</p>
<blockquote><pre>
kd&gt; k100
ChildEBP RetAddr  
f4492c90 808202b6 nt!KiSwapContext+0x25
f4492ca8 8081fb6e nt!KiSwapThread+0x83
f4492cec 8090e64e nt!KeWaitForSingleObject+0x2e0
f4492d50 8082350b nt!NtWaitForSingleObject+0x9a
f4492d50 7c8285ec nt!KiFastCallEntry+0xf8
065b95c8 7c827d0b ntdll!KiFastSystemCallRet
065b95cc 77e61d1e ntdll!NtWaitForSingleObject+0xc
065b963c 77e61c8d kernel32!WaitForSingleObjectEx+0xac
065b9650 46a8c54d kernel32!WaitForSingleObject+0x12
<b>065b966c 46a7eeca WININET!FixProxySettingsForCurrentConnection+0x25</b>
065b9684 46a7ee3f WININET!CFsm_HttpSendRequest::RunSM+0x61
065b969c 46a7fefa WININET!CFsm::Run+0x39
065b96b4 46ab0a67 WININET!DoFsm+0x25
065b96dc 46aa1092 WININET!HttpWrapSendRequest+0x148
065b9714 06b231da WININET!HttpSendRequestW+0x5e
065b973c 06b22ea8 SYMSRV!StoreWinInet::request+0x2a
065b9770 06b226cc SYMSRV!StoreWinInet::fileinfo+0x18
065b9780 06b22741 SYMSRV!StoreWinInet::get+0x7c
065b9fc4 06b229a3 SYMSRV!StoreWinInet::open+0x41
065ba204 06b15ae7 SYMSRV!StoreWinInet::find+0xd3
065ba2f8 06b16c47 SYMSRV!cascade+0x87
065ba848 06b16a57 SYMSRV!SymbolServerByIndexW+0x127
065baa78 51412896 SYMSRV!SymbolServerW+0x77
065bb8cc 51413383 mspdb80!LOCATOR::SYMSRV::SymbolServer+0x190
065bbf10 514136f8 mspdb80!LOCATOR::FLocatePdbSymsrv+0x75
065bbf38 514139ce mspdb80!LOCATOR::FLocatePdbPathHelper+0x179
065bc96c 51413cbe mspdb80!LOCATOR::FLocatePdbPath+0x105
065bccb4 51414371 mspdb80!LOCATOR::FLocatePdb+0x1ad
065bd9a8 458cc1e8 mspdb80!PDBCommon::OpenValidate5+0xab
065bd9ec 45959d4c msenc90!enc::EncImageEdit::
                  `scalar deleting destructor'+0x4d
065bda34 45958e62 NatDbgDE!OLPDBOpen+0x93
065be6c0 45958f0a NatDbgDE!OLStart+0x107
065be6fc 45958fae NatDbgDE!LoadOmfForReal+0x23
065be714 45959019 NatDbgDE!LoadSymbols+0x43
065be72c 459590d9 NatDbgDE!OLLoadOmf+0x55
065be75c 45959154 NatDbgDE!SHLoadDll+0xd5
065be7ac 45959247 NatDbgDE!CSymbolHandlerX::SHLoadDll+0x5a
065be844 4595937c NatDbgDE!CModule::Load+0x1a1
065be8ac 4594d002 NatDbgDE!CNativeProcess::NotifyModLoad+0xc8
065be9ec 4594cf6d NatDbgDE!EngineCallback+0xb3
065bea18 45958d3f NatDbgDE!EMCallBackDB+0x4c
065bf050 4594d0dc NatDbgDE!LoadFixups+0x218
065bf0ac 4594d289 NatDbgDE!DebugPacket+0x213
065bfdb4 4594cf39 NatDbgDE!EMFunc+0x40f
065bfddc 4594d73d NatDbgDE!TLCallBack+0x1e
065bfdf4 4594d711 NatDbgDE!TLClientLib::Local_TLFunc+0xc8
065bfe3c 4594d85c NatDbgDE!DMSendPacket+0x121
065bfee8 45959b1d NatDbgDE!NotifyEM+0x3ae
<b>065bff0c 4594d663 NatDbgDE!ProcessLoadDLLEvent+0x47</b>
065bff44 4594d686 NatDbgDE!ProcessDebugEvent+0x30d
065bffb8 77e64829 NatDbgDE!DmPollLoop+0x3c
065bffec 00000000 kernel32!BaseThreadStart+0x34
</pre>
</blockquote>
<p>But &#8212; looking closely, it becomes obvious that this thread must be the one handling debug events, and in fact, the call to ProcessLoadDLLEvent is a strong indication for that this thread is currently handling a DLL load event. And now we have closed the loop &#8212; this thread must be handling the DLL load event for rasapi.dll, the DLL which cfix was about to load. And to do this, VC attempts to acquire the WininetProxyRegistryMutex, which is owned by the original cfix thread. Deadlock.</p>
<p>What is interesting about this situation is that neither party &#8212; cfix, iexplore or VCExpress, and also none of the modules clearly is the culprit and behaving wrong. It is more like a combination of special circumstances that bring up the deadlock as discussed.</p>
<p>It is also notable that I am <i>not</i> using any particular proxy settings on this machine and automatic proxy configuration has been turned off.</p>
<p>So far, I have not experienced the same problem with VS 2003 and VS 2005 &#8212; I thus assume that only VS 2008 is affected by this.</p>
<p>Although I am pretty sure that cfix is not really at fault here, I have to adapt it to avoid this deadlock in the future. Until an updated version is available, you can use <a href='http://jpassing.wordpress.com/?p=104'>this workaround</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/97/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/97/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=97&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/08/23/debugging-a-debugger-deadlock/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>cfix 1.1 may experience hangs when used in the VisualStudio 2008 debugger</title>
		<link>http://jpassing.com/2008/08/23/cfix-11-may-experience-hangs-when-used-in-the-visualstudio-2008-debugger/</link>
		<comments>http://jpassing.com/2008/08/23/cfix-11-may-experience-hangs-when-used-in-the-visualstudio-2008-debugger/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 07:55:23 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[cfix]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[debugger]]></category>
		<category><![CDATA[hang]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=104</guid>
		<description><![CDATA[When used in conjunction with the VisualStudio 2008 debugger, cfix may hang indefinitely as soon as an assertion fails. The reason for this behavior is a Symbol Server-caused deadlock between the debugger and cfix. I am going to discuss the details of this deadlock in a separate post. Until a new version of cfix is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=104&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When used in conjunction with the VisualStudio 2008 debugger, <a href='http://cfix.sf.net/'>cfix</a> may hang indefinitely as soon as an assertion fails. The reason for this behavior is a Symbol Server-caused deadlock between the debugger and cfix. I am going to discuss the details of this deadlock in a separate post.</p>
<p>Until a new version of cfix is available, you can work around this problem as follows: Go to the cfix installation directory and rename or delete symsrv.dll. This will stop cfix from contacting the symbol server. While cfix now will not be able to capture proper stack traces any more, this will prevent the mentioned deadlock situation to arise.</p>
<p>The problem only applies to cfix 1.1 and VS 2008. The problem does not occur when debugging with VS 2003, VS 2005, or cdb/ntsd/WinDBG.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/104/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/104/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/104/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/104/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/104/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=104&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/08/23/cfix-11-may-experience-hangs-when-used-in-the-visualstudio-2008-debugger/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Thread 0:0 is special</title>
		<link>http://jpassing.com/2008/08/08/thread-00-is-special/</link>
		<comments>http://jpassing.com/2008/08/08/thread-00-is-special/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 06:36:41 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[multicore]]></category>
		<category><![CDATA[NT]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[trace]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=72</guid>
		<description><![CDATA[Thread IDs uniquely identify a thread &#8212; this certainly holds for user mode threads and should also hold for kernel mode threads. But there is one kind of thread where the ID does not uniquely identify a KTHREAD &#8212; the Idle thread. On a uniprocessor system, there is only one Idle thread and this idle [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=72&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Thread IDs uniquely identify a thread &#8212; this certainly holds for user mode threads and should also hold for kernel mode threads. But there is one kind of thread where the ID does not uniquely identify a KTHREAD &#8212; the Idle thread.</p>
<p>On a uniprocessor system, there is only one Idle thread and this idle thread will have the thread ID 0 (in process 0). On a multiprocessor system, however, Windows creates one Idle thread per CPU. That makes sense &#8212; however, what may be surprising at first is that although all Idle threads have their own KTHREAD structure, all share the same thread ID 0 (CID 0:0). That is, each multiprocessor system will have multiple threads with ID 0, which in turn means that CID 0:0 does not uniquely identify a single thread. </p>
<p>This is easily verified using the kernel debugger on a multiprocessor system (a quad core in this case):</p>
<blockquote><pre>
0: kd&gt; !running

System Processors f (affinity mask)
  Idle Processors f
<b>All processors idle.</b>

0: kd&gt; !pcr 0
KPCR for Processor 0 at ffdff000:
[...]

	      CurrentThread: <b>8089d8c0</b>
	         NextThread: 00000000
	         IdleThread: 8089d8c0


0: kd&gt; !thread <b>8089d8c0</b>
THREAD 8089d8c0  Cid <b>0000.0000</b>  Teb: 00000000 Win32Thread: 00000000 RUNNING on processor 0
[...]

0: kd&gt; !pcr 1
KPCR for Processor 1 at f7727000:
[...]

	      CurrentThread: <b>f772a090</b>
	         NextThread: 00000000
	         IdleThread: f772a090
	         
0: kd&gt; !thread <b>f772a090</b>
THREAD f772a090  Cid <b>0000.0000</b>  Teb: 00000000 Win32Thread: 00000000 RUNNING on processor 1
[...]

...and so on.
</pre>
</blockquote>
<p>Now, the idle threads are usually not of major intest. Still, it is possible that these threads can become relevant &#8212; such as when doing certain kinds of analysis such as tracing interrupts. If such analysis groups events by the CID of the thread they were captured on (rather than the VA of the KTHREAD structure), the results for CID 0:0 will be wrong. </p>
<p>Not a big thing. Still, it took me a while to figure out that this was indeed the reason for some of my traces containing timestamps in strange order: The traces had been created on a quadcode machine and I did make the mistake to correlate the events by their CID during later analsis. As a result, the traces of the four idle threads were all mixed up&#8230;</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/72/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/72/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/72/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/72/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/72/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=72&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/08/08/thread-00-is-special/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Reaching beyond the top of the stack &#8212; illegal or just bad style?</title>
		<link>http://jpassing.com/2008/06/27/reaching-beyond-the-top-of-the-stack-illegal-or-just-bad-style/</link>
		<comments>http://jpassing.com/2008/06/27/reaching-beyond-the-top-of-the-stack-illegal-or-just-bad-style/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 13:50:42 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[NT]]></category>
		<category><![CDATA[stack]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=68</guid>
		<description><![CDATA[The stack pointer, esp on i386, denotes the top of the stack. All memory below the stack pointer (i.e. higher addresses) is occupied by parameters, variables and return addresses; memory above the stack pointer must be assumed to contain garbage. When programming in assembly, it is equally easy to use memory below and above the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=68&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The stack pointer, esp on i386, denotes the top of the stack. All memory below the stack pointer (i.e. higher addresses) is occupied by parameters, variables and return addresses; memory above the stack pointer must be assumed to contain garbage.</p>
<p>When programming in assembly, it is equally easy to use memory below and above the stack pointer. Reading from or writing to addresses beyond the top of the stack is unusual and under normal circumstances, there is little reason to do so. There are, however, situations &#8212; <i>rare</i> situations &#8212; where it may tempting to temporarily use memory beyond the top of the stack.</p>
<p>That said, the question is whether it is really just a convention and good style <i>not</i> to grab beyond the stack of the stack or whether there are actually reasons why doing so could lead to problems.</p>
<p>When trying to answer this question, one first has to make a distinction between user mode and kernel mode. In user mode Windows, I am unable to come up with a single reason of why usage of memory beyond the top of the stack could lead to problems. So in this case, it is probably merely bad style. </p>
<p>However, things are different in kernel mode. </p>
<p>In one particular routine I recently wrote, I encountered a situation where temporarily violating the rule of not reaching beyond the top of the stack came in handy. The routine worked fine for quite a while. In certain situations, however, it suddenly started to fail due to memory corruption. Interestingly enough, the routine did not fail <i>always</i>, but still rather frequently. </p>
<p>Having identified the specific routine as being the cuplrit, I started single stepping the code. Everything was fine until I reached the point where the memory above the stack pointer was used. The window span only a single instruction. Yet, as soon as I had stepped over the two instructions, the system crashed. I tried it multiple times, and it was prefectly reproduceable when being single-stepped.</p>
<p>So I took a look at the stack contents after every single step I took. To my surprise, as soon as I reached the critical window, the contents of the memory location just beyond the current stack pointer suddenly became messed. Very weird.</p>
<p>After having been scratching my head for a while, that suddenly started to made sense: I was not the only one using the stack &#8212; in between the two instructions, an interrupt must have occured and been dispatched. As my thread happened to be the one currently running, it was my stack that has been used for dispatching it. This also explains why it did not happened always unless I was single-stepping the respective code.</p>
<p>When an interrupt occurs and no privilege-level change has to be performed, the CPU will push the EFLAGS, CS and EIP registers on the stack. That is, the stack of whatever kernel thread happens to be the one currently running on this CPU is reused and the memory locations beyond the stack pointer will be overwritten by these three values. So what I initially interpreted as garbage, actually were the contents of EFLAGS, CS and EIP.</p>
<p>On Windows NT, unlike some other operating systems (FreeBSD, IIRC), handling the interrupt, which involves runing the interrupt service routine (ISR) occurs on the same stack as well. The following stack trace, taken elsewhere, shows an ISR being executed on the stack of the interrupted thread:</p>
<blockquote><pre>
f6bdab4c f99bf153 i8042prt!I8xQueueCurrentMouseInput+0x67
f6bdab78 80884289 i8042prt!I8042MouseInterruptService+0xa58
<b>f6bdab78 f6dd501a nt!KiInterruptDispatch+0x49</b>
f6bdac44 f6dd435f driver!Quux+0x11a 
f6bdac58 f6dd61db driver!Foobar+0x6f 
...
</pre>
</blockquote>
<p>Morale of the story: Using memory beyond the current stack pointer is not only bad practice, it is actually illegal when done in kernel mode.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/68/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/68/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/68/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=68&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/06/27/reaching-beyond-the-top-of-the-stack-illegal-or-just-bad-style/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Explicit Symbol Loading with dbghelp</title>
		<link>http://jpassing.com/2008/06/10/explicit-symbol-loading-with-dbghelp/</link>
		<comments>http://jpassing.com/2008/06/10/explicit-symbol-loading-with-dbghelp/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 18:53:58 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[dbghelp]]></category>
		<category><![CDATA[pe]]></category>
		<category><![CDATA[symbols]]></category>
		<category><![CDATA[trace]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=61</guid>
		<description><![CDATA[When working with symbols, the default case is that you either analyze the current process, a concurrently running process or maybe even the kernel. dbghelp provides special support for these use cases and getting the right symbols to load is usually easy &#8212; having access to the process being analyzed, dbghelp can obtain the necessary [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=61&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When working with symbols, the default case is that you either analyze the current process, a concurrently running process or maybe even the kernel. dbghelp provides special support for these use cases and getting the right symbols to load is usually easy &#8212; having access to the process being analyzed, dbghelp can obtain the necessary module information by itself and will come up with the matching symbols.</p>
<p>Things are not quite as easy when analyzing symbols for a process (or kernel) that is not running any more or executes on a different machine. In this case, manual intervention is necessary &#8212; without your help, dbghelp will not be able to decide which PDB to load.</p>
<p>An example for a case where this applies is writing a binary tracefile. To keep the trace file small and to make writing entries as efficient as possible, it makes sense to write raw instruction pointer and function pointer addresses to the file. This way, resolving symbols and finding the names of the affected routines is delayed until the file is actually read.</p>
<p>However, when the trace file is read, the traced process (or kernel) may not be alive any more, so dbghelp&#8217;s automatisms cannot be used. Moreover, the version of the OS and the individual modules involved may not be the same on the machine where the trace is obtained and the machine where the trace file is read. It is thus crucial that the trace file contains all neccessary information required for the reading application to find and load the matching symbols.</p>
<h3>Capturing the debug information</h3>
<p>That sounds easy enough. But once again, the documentation on this topic is flaky enough to make this a non-trivial task. <a href='http://www.osronline.com/showThread.cfm?link=78064'>This thread</a> on the OSR WinDBG list revealed some important insights, yet the summary is not entirely correct. So I will try to explain it here. Note that the discussion equally applies to user and kernel mode.</p>
<p>Two basic pieces of information need to be captured and stored in the trace file: The load address and the debug data of each module involved. The load address is required in order to later be able to convert from virtual addresses (VAs) to relative virtual addresses (RVAs). The debug data is neccessary for finding the PDB exactly matching the respective module.</p>
<p>The debug data is stored in the PE image and can be obtained via the debug directory. The following diagram illustrates the basic layout of the affected parts of a PE image:</p>
<p><img src="http://jpassing.files.wordpress.com/2008/05/blog-drawing.gif?w=500" alt=""   class="aligncenter size-full wp-image-62" /></p>
<p>Via the _IMAGE_DATA_DIRECTORY structure, which is at index IMAGE_DIRECTORY_ENTRY_DEBUG of the DataDirectory, we can obtain the RVA of the debug directory &#8212; as always, RVAs are relative to the load address. The debug directory is a sequence of IMAGE_DEBUG_DIRECTORY structures.  Although the CodeView entry should be the one of primary interest, expect more than one entry in this sequence. The entry count can be calculated by dividing _IMAGE_DATA_DIRECTORY::Size by sizeof( IMAGE_DEBUG_DIRECTORY ).</p>
<p>The IMAGE_DEBUG_DIRECTORY structures do not contain all necessary information by themselves &#8212; rather, they refer to a blob of debug data located elsewhere in the image. IMAGE_DEBUG_DIRECTORY::AddressOfRawData contains the RVA (again, relative to the load address) to this blob. Note that the blobs are not necessarily laid out in the same order as the corresponding IMAGE_DEBUG_DIRECTORY structures are. (The role of the PointerToRawData member in this context remains unclear.)</p>
<p>Summing up, we need to write to the trace file:</p>
<ul>
<li>The module load address</li>
<li>all IMAGE_DEBUG_DIRECTORY entries</li>
<li>all debug data blobs referred to</li>
</ul>
<p>In order to maintain the relation between the IMAGE_DEBUG_DIRECTORY entries and their corresponding blobs, the AddressOfRawData and PointerToRawData members have to be updated before being written to the file. In order to make loading symbols a bit easier, it makes sense to save the entire sequence of IMAGE_DEBUG_DIRECTORY structs (with updated RVAs) contigously to the file, immediately followed by the debug data blobs. </p>
<h3>Loading symbols</h3>
<p>Having all neccessary information in the trace file, we can now load the symbols. The key is to use SymLoadModuleEx rather than SymLoadModule64 and passing it a MODLOAD_DATA struct:</p>
<ul>
<li>MODLOAD_DATA::data receives a pointer to the sequence of IMAGE_DEBUG_DIRECTORY structs. For each struct, the AddressOfRawData member should be set to 0 and PointerToRawData should receive the RVA of the corresponding debug data blob <i>relative to the beginning of the respective IMAGE_DEBUG_DIRECTORY struct</i>.
<p>As indicated before, it therefore makes sense to write the IMAGE_DEBUG_DIRECTORY structs and the blobs consecutievly to the file and &#8212; at the time of writing &#8212; adjust the AddressOfRawData and PointerToRawData members. This way, you can just assign the entire memory chunk to MODLOAD_DATA::data.</li>
<li>MODLOAD_DATA::size receives the size of the entire memory chunk, including both the sequence of IMAGE_DEBUG_DIRECTORY structs and all blobs.</li>
</ul>
<p>There you go, dbghelp should now be able to identify the proper PDB and load the symbols.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/61/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/61/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=61&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/06/10/explicit-symbol-loading-with-dbghelp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>

		<media:content url="http://jpassing.files.wordpress.com/2008/05/blog-drawing.gif" medium="image" />
	</item>
		<item>
		<title>Fun with low level SEH</title>
		<link>http://jpassing.com/2008/05/20/fun-with-low-level-seh/</link>
		<comments>http://jpassing.com/2008/05/20/fun-with-low-level-seh/#comments</comments>
		<pubDate>Tue, 20 May 2008 19:18:34 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Kernel]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Exception Handling]]></category>
		<category><![CDATA[SEH]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=65</guid>
		<description><![CDATA[Most code that uses Structured Exception Handling does this with the help of the compiler, e.g. by using __try/__except/__finally. Still, it is possible to do everything by hand, i.e. to provide your own exception handlers and set up the exception registration records manually. However, as this entire topic is not documented very well, doing so [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=65&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>
Most code that uses Structured Exception Handling does this with the help of the compiler, e.g. by using __try/__except/__finally. Still, it is possible to do everything by hand, i.e. to provide your own exception handlers and set up the exception registration records manually. However, as this entire topic is not documented very well, doing so opens room for all kind of surprises&#8230;
</p>
<p>
Although more than 10 years old, the best article on this topic still seems to be <a href='http://www.microsoft.com/msj/0197/exception/exception.aspx'>Matt Pirtrek&#8217;s <i>A Crash Course on the Depths of Win32™ Structured Exception Handling</i></a>, which I assume you have read. However, note that this article as well as this post refer to i386 only, albeit both to user and kernel mode.
</p>
<h3>Exception Registration Record Validation</h3>
<p>On the i386, SEH uses a linked list of exception registration records. The first record is pointed to by the first member of the TIB. In user mode, the TIB is part of the TEB, in kernel mode it is part of the KPCR &#8212; in any case, it is at fs:[0]. Each record, besides containing a pointer to the next lower record, stores a pointer to an exception handler routine.
</p>
<p>
Installing an exception registration record is thus straightforward and merely requires adjusting the TIB pointer and having the new record point to next lower record. So I set up my custom exception registration record, registered it properly, verified that all pointers are correct and tried using it. However, I was unpleasently surprised that exeption handling totally failed as soon as my exception registration got involved. !exchain reported an &#8220;Invalid exception stack&#8221;, although checking the pointers manually again seemed to show that the chain of exception registration records was fine and my record seemed ok.
</p>
<p>
Digging a little deeper I found the reason for that &#8212; and in fact I cannot remember ever having heard or read about this requirement before: Windows requires all EXCEPTION_REGISTRATION_RECORDs to be located on the stack. Both RtlDispatchException and RtlUnwind check the location of each EXCEPTION_REGISTRATION_RECORD against the stack limits and abort exception handling as soon as a record is found to be not stack-located. Aborting exception handling in this case means that RaiseException/ExRaiseStatus will just return and execution will be resumed at the caller site as if nothing happened.
</p>
<p>
This requirement is fair enough, actually, but in my case it totally wrecked my design. I did not have the 8 spare bytes to store this record and thus therefore put the record on some dedicated place on the heap. Urgh. Anyway&#8230;
</p>
<p>
As an interesting side note, Windows Server 2003 performs this stack check against both limits &#8212; minimum and maximum address of the stack. Vista, however, only checks against the maximum address (i.e. bottom of the stack) and does not care whether the minimum address (i.e. top of stack) has been exceeded.
</p>
<p>
Moreover, there is another restriction on exception records that only applies to user mode: The handler routine pointed to by the exception record is verified to <i>not</i> point into the stack. This is obviously another security measure to avoid SEH records to point into some overflown buffers.
</p>
<h3>SafeSEH</h3>
<p>
It is worth pointing out that all these checks are unrelated to SafeSEH and are performed regardless of whether your module is SafeSEH compatible or not. Not before these checks have all passed, the exception handler has to undergo the SafeSEH validation: The image base is calculated, the table listing the trusted SEH handlers is looked up and it is checked whether the handler routine pointed to by the current exception record is located in this table.
</p>
<h3>SafeSEH Handler Registration</h3>
<p>
Using SafeSEH is a good thing and I link all my modules with /SafeSEH. So when you use low level SEH, i.e. without using the __try/__except compiler support, the obvious question is how to get your SEH handler to be recognized as a trusted handler and be included in the SafeSEH table. After all, the compiler will not be able to recognize that the routine you have just written will in fact be used as an exception handler. The C compiler does not seem to offer support for that &#8212; luckily however, ml does by providing the .SAFESEH directive.
</p>
<p>
If you like writing your exception handler in assmbler, this is all you need. If, however, you prefer C, this is somewhat unsatisfying. The documentation of .SAFESEH states that it can be used with an <i>extrn proc</i>, but that does not seem to work. My solution was thus to write the actual routine in C and write a little thunk in assembler, which I was then able to register using the .SAFESEH directive:</p>
<blockquote><pre>
.586               
.model flat, stdcall
option casemap :none

extrn RealExceptionHandlerWrittenInC@16

...

ExceptionHandlerThunk proto
.SAFESEH ExceptionHandlerThunk

...

.code

ExceptionHandlerThunk proc
	jmp RealExceptionHandlerWrittenInC@16
ExceptionHandlerThunk endp
</pre>
</blockquote>
<h3>Stupid things you should not do</h3>
<p>
Finally, there is another little quirk that bit me: Do not use EXCEPTION_CONTINUE_SEARCH where ExceptionContinueSearch would have been appropriate. The EXCEPTION_* constants are for use by exception filters as used for __except statements, whereas the Exception* values have to be used for low level exception handlers. Should be obvious, right? :)
</p>
<p>
Having chosen the wrong group of constants, I returned EXCEPTION_CONTINUE_SEARCH from my exception handler to indicate that the handler is unable to handle certain exceptions. However, as it turns out, EXCEPTION_CONTINUE_SEARCH has the value 0 and is thus interpreted as ExceptionContinueExecution. Now, returning ExceptionContinueExecution when being requested to handle an exception raised by ExRaiseStatus is obviously a bad idea and in this case led to a STATUS_NONCONTINUABLE_EXCEPTION. After a few of those had stacked up (in kernel mode), VirtualPC crashed with an unrecoverable CPU error. Nice :)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/65/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/65/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/65/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/65/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/65/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=65&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/05/20/fun-with-low-level-seh/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>The case of the mysterious JVM x64 crashes</title>
		<link>http://jpassing.com/2008/05/11/the-case-of-the-mysterious-jvm-x64-crashes/</link>
		<comments>http://jpassing.com/2008/05/11/the-case-of-the-mysterious-jvm-x64-crashes/#comments</comments>
		<pubDate>Sun, 11 May 2008 11:15:49 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[x64]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=64</guid>
		<description><![CDATA[To date, I did all my Java development uding 32 bit JVMs. After all, as long as you do not have extreme memory requirements, the 64 bit JVM should not buy you much. Today I installed the Java 6 Update 6 x64 JDK on my Vista x64 machine and tried to run some of my [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=64&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>To date, I did all my Java development uding 32 bit JVMs. After all, as long as you do not have extreme memory requirements, the 64 bit JVM should not buy you much. Today I installed the <i>Java 6 Update 6 x64</i> JDK on my Vista x64 machine and tried to run some of my JUnit tests on this VM.
</p>
<p>
With little success:</p>
<blockquote><pre>
#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000772b219b, pid=4188, tid=2272
#
# Java VM: Java HotSpot(TM) 64-Bit Server VM (10.0-b22 mixed mode windows-amd64)
# Problematic frame:
# C  [ntdll.dll+0x5219b]
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#
</pre>
</blockquote>
<p>The project uses a native library via JNI, so of course I immediately suspected this to be the problem.  So I placed a Java breakpoint on the respective System.loadLibrary call with the intent of attaching WinDBG as soon as this breakpoint is hit. In WinDBG, I could then break on the exception and see what the problem is.
</p>
<p>
But to my surprise, the Java breakpoint was not hit &#8212; the VM crashed immediately and I received the same output about an unexpected error having occured. That seemed strange to me &#8212; maybe it was not the fault of the JNI library after all? So I created a simple Hello World application and ran it &#8212; that worked. Then I created this innocent JUnit test:</p>
<blockquote><pre>
public class JTest
{
  @org.junit.Test
  public void testname() throws Exception
  {    
  }
}
</pre>
</blockquote>
<p>This one failed again, yielding the same error message as above. Well, at least that gave me evidence that not my JNI library but the JVM was the culprit of the crash &#8212; but still, the situation seemed weird. Running the test again under WinDBG, I could see that the AV occured during a heap free operation (<i>As a side node, it is annoying that Sun does not supply symbols for its binaries</i>):</p>
<blockquote><pre>
00000000`0404ca88 00000000`7727e7e2 ntdll!RtlCaptureContext+0x8c
00000000`0404ca98 00000000`7727e72b ntdll!RtlpWalkFrameChain+0x52
00000000`0404d018 00000000`773352f2 ntdll!RtlCaptureStackBackTrace+0x4b
00000000`0404d048 00000000`772e1d35 ntdll!RtlpStackTraceDatabaseLogPrefix+0x42
00000000`0404d178 00000000`7715d9fa ntdll! ?? ::FNODOBFM::`string'+0xa93f
00000000`0404d1f8 000007fe`fef0175c kernel32!HeapFree+0xa
*** WARNING: Unable to verify checksum for C:\Program Files\Java\jdk1.6.0_06\jre\bin\server\jvm.dll
*** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Program Files\Java\jdk1.6.0_06\jre\bin\server\jvm.dll - 
00000000`0404d228 00000000`08101c09 msvcrt!free+0x1c
00000000`0404d258 00000000`081026cc jvm!JVM_EnqueueOperation+0x8c139
00000000`0404d288 00000000`040b4937 jvm!JVM_EnqueueOperation+0x8cbfc
00000000`0404d318 00000000`0404d338 0x40b4937
</pre>
</blockquote>
<p>Well, could be a heap corruption &#8212; but it is interesting that crash did not occur during block coalescence or similar operations but during stack trace capturing. As a matter of fact, I always run my machine with user mode stack trace database creation enabled for debugging purposes. So I disabled the stack trace database in gflags, rebooted the machine and &#8212; voilà, the crash disappeared!
</p>
<p>
Wow. I think this is worth being filed as a bug.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/64/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/64/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/64/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=64&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/05/11/the-case-of-the-mysterious-jvm-x64-crashes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Trace and Watch Data &#8212; How does it work</title>
		<link>http://jpassing.com/2008/05/10/trace-and-watch-data-how-does-it-work/</link>
		<comments>http://jpassing.com/2008/05/10/trace-and-watch-data-how-does-it-work/#comments</comments>
		<pubDate>Sat, 10 May 2008 08:45:33 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[WinDBG]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=35</guid>
		<description><![CDATA[One of the builtin WinDBG commands is wt (Trace and Watch Data), which can be used to trace the execution flow of a function. Given source code like the following: void foo() { } void bar() { } int main() { // Some random code... int a = 1, b = 2; // Call a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=35&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the builtin WinDBG commands is <i>wt</i> (Trace and Watch Data), which can be used to trace the execution flow of a function. Given source code like the following:
</p>
<blockquote><pre>
void foo()
{
}

void bar()
{
}

int main()
{
  // Some random code...
  int a = 1, b = 2;
  
  // Call a child function.
  foo();
  
  // More useless code...
  a+=b;
  if ( a == b) a = b;
  
  // Call another child function.
  bar();  
  
  return 0;
}
</pre>
</blockquote>
<p><i>wt</i> will produce the following output:</p>
<blockquote><pre>
0:000&gt; wt
Tracing test!main to return address 00401291
    6     0 [  0] test!main
    1     0 [  1]   test!ILT+5(_foo)
    4     0 [  1]   test!foo
   13     5 [  0] test!main
    1     0 [  1]   test!ILT+0(_bar)
    4     0 [  1]   test!bar
   17    10 [  0] test!main

27 instructions were executed in 26 events 
                                  (0 from other threads)

Function Name         Invocations MinInst MaxInst AvgInst
test!ILT+0(_bar)                1       1       1       1
test!ILT+5(_foo)                1       1       1       1
test!bar                        1       4       4       4
test!foo                        1       4       4       4
test!main                       1      17      17      17

0 system calls were executed
</pre>
</blockquote>
<p>Although helpful, tracing a larger function calling a multitude of other functions slows down the debuggee significantly. An interesting question is thus how <i>wt</i> is implemented. Three possible implementation strategies come to mind:</p>
<ol>
<li>Use single-stepping. After each instruction executed, a debug trap is raised and the debugger is delivered a single-step debugging event. Though all non-branching instructions are probably irrelevant to <i>wt</i>, by intercepting each call and ret instruction, the debugger is able to trace function entry and exit.</li>
<li>Explicitly set breakpoints. The debugger disassembles the function to be traced and places an ordinary breakpoint on each call instruction as well on as the return address of the function. Whenever one of the call-breakpoints fires, the debugger instruments the target function in the same way (i.e. place breakpoints on each call instruction as well as the return address) and continues execution (without single-stepping). By intercepting all function calls and returns, the debugger is able to deduce the call tree. This approach would be similar to <a href='http://sourceforge.net/project/showfiles.php?group_id=183694'>UMSS</a>.
</li>
<li>Use <i>Last Branch Recording</i>. This is a rather new additon to the IA-32 instruction set that allows setting breakpoints on taken branches, interrupts, and exceptions, and to single-step from one branch to the next.
</li>
</ol>
<p>In order to find out, we have to debug the debugger to observe how it debugs the target. We thus start WinDBG, choose our test application as target and let it break on main. We then start another WinDBG instance and attach it to the first WinDBG instance. In order to find out which debugging events are consumed by the first instance, we use the second debugger to trace function calls made by the first debugger.
</p>
<p>All usermode debuggers eventually end up calling ntdll!NtWaitForDebugEvent in a loop &#8212; so to find out which debugging events are consumed, all we need to do is trace all calls to this function. While being an undocumented native function, <a href='http://www.openrce.org/articles/full_view/25'>there is an excellent summary</a> on the inner workings of user mode debugging which also covers ntdll!NtWaitForDebugEvent. Given this information, all we need to do to check whether strategy #1 or strategy #2 has been implemented (I assume #3 may safely be neglected) is to put together a little breakpoint command like the following (line breaks added for clarity):
</p>
<pre>
bp ntdll!NtWaitForDebugEvent "
   r @$t1=poi(esp+10); 
   g @$ra; 
   .if (poi(@$t1)==8) {.echo \"SingleStep\n\" } 
   .else {.printf \"Excp %p\\n\", poi(@$t1+c)};
   g "
</pre>
<p>When entering ntdll!NtWaitForDebugEvent, we store the address of the fourth parameter (which receives a PDBGUI_WAIT_STATE_CHANGE structure) in $t1 and step out of the function. Then we reach into the structure whose address is stored in $t1 and check if the first field marks the event of being of type DbgSingleStepStateChange (0&#215;8) and output an appropriate message. If we receive about 30 single-step events, strategy #1 has probably been chosen. For #2 we would expect to receive 5 breakpoint events.
</p>
<p>Back to the first debugger, we now opt to trace the main function by running <i>wt</i>. This yields the output shown above. Switching to the second debugger again, we now see the following output:</p>
<blockquote><pre>
SingleStep
SingleStep
SingleStep

[...about 20 more...]

SingleStep
SingleStep
SingleStep
SingleStep
SingleStep
</pre>
</blockquote>
<p>Quite obviously, <i>wt</i> implements strategy #1 &#8212; it does single stepping. Although this does not really come as a surprise, it is still unfortunate as it is most likely the slowest approach of tracing calls. And as anybody who has ever used wt can probably confirm, wt is <i>really</i> slow.
</p>
<p>As an interesting side note, as of Linux kernel 2.6.25, ptrace on x86 has been enhanced to facilitate Last Branch Recording on CPUs that support it.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/35/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/35/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=35&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/05/10/trace-and-watch-data-how-does-it-work/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>Walking the stack of the current thread</title>
		<link>http://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/</link>
		<comments>http://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 19:24:46 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[stacktrace dbghelp]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=47</guid>
		<description><![CDATA[StackWalk64 provides a convenient and platform-independent way to walk the stack of a thread. Although useful and powerful indeed, it is definitively one of the less trivial to use functions. The additional fact that the MSDN documentation for StackWalk64 is rather light does not make things easier. There is, however, a decent article on CodeProject [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=47&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href='http://msdn2.microsoft.com/en-us/library/ms680650(VS.85).aspx'>StackWalk64</a> provides a convenient and platform-independent way to walk the stack of a thread. Although useful and powerful indeed, it is definitively one of the less trivial to use functions. The additional fact that the MSDN documentation for StackWalk64 is rather light does not make things easier. There is, however, a <a href='http://www.codeproject.com/KB/threads/StackWalker.aspx'>decent article</a> on CodeProject covering the usage of StackWalk64.</p>
<p>Although you can walk the stack of any thread using StackWalk64, this post only deals with walking the <i>current</i> thread&#8217;s stack. In order to walk the stack of the current thread, you first have to obtain the CONTEXT of the current thread. The naive way to obtain this context would be to call GetThreadContext( GetCurrentThread() ) &#8212; however, as the <a href='http://msdn2.microsoft.com/en-us/library/ms679362(VS.85).aspx'>documentation for GetThreadContext</a> clearly states, the result of this function is undefined if used on the current thread.</p>
<h4>Obtaining a CONTEXT</h4>
<p>64 Bit windows provides the function <a href='http://msdn2.microsoft.com/en-us/library/ms797633.aspx'>RtlCaptureContext</a> for this purpose, so obtaining the context is trivial. 32 Bit Windows, however, does not implement this function, yet it is not hard to come up with a surregate.</p>
<p>One way to obtain the context is to set up a SEH frame, deliberately raise an exception and obtain the context in the exception filter using GetExceptionInformation. Although that works, it is probably the slowest way to do it. An easier and quicker solution is to use inline assembly (we only have to support 32 bit as we have RtlCaptureContext for x64/IA64, so using assembly is ok). Using appropriate instructions, we can manually populate the x86 CONTEXT structure. However, as eip cannot be accessed using a mov instruction, we have to use a little trick to obtain the value of the instruction pointer &#8212; use an (otherwise useless) label:</p>
<blockquote><pre>
CONTEXT Context;
ZeroMemory( &amp;Context, sizeof( CONTEXT ) );
Context.ContextFlags = CONTEXT_CONTROL;

__asm
{
Label:
  mov [Context.Ebp], ebp;
  mov [Context.Esp], esp;
  mov eax, [Label];
  mov [Context.Eip], eax;
}
</pre>
</blockquote>
<h4>Preparing to call StackWalk64</h4>
<p>Having obtained the context, we have to initialize the STACKFRAME64 structure for the first call. This is trivial, but platform-specific.</p>
<p>In order to enable StackWalk64 to use symbol information, dbghelp has to be initialized by calling SymInitialize, passing the current process handle as parameter. Unless you plan to manually call SymLoadModule64 for all affected modules, it is crucial to pass TRUE for the fInvadeProcess argument. If you pass FALSE or forget to call SymInitialize altogether (After all, the documentation of StackWalk64 does not state that calling SymInitialize is required), you will notice that StackWalk64 works on x86 builds, but fails (with last error set to 0x1E7) on x64. Also, as dbghelp is not threadsafe, it is usually a good idea to protect any calls to dbghelp by a critical section</p>
<h4>Calling StackWalk64</h4>
<p>After these provisions have been made, calling StackWalk64 is a snap. The only unfortnate thing is that you cannot really distinguish between StackWalk64 returning FALSE because of a failure or because of the bottom of the stack having been reached as StackWalk64 does not reliably set the last Win32 error.</p>
<p>The following code puts it all together. Note that only a the minimal amount of stack trace information is gathered, i.e. only the return addresses are saved in the array.</p>
<blockquote><pre>
typedef struct _STACKTRACE
{
  //
  // Number of frames in Frames array.
  //
  UINT FrameCount;

  //
  // PC-Addresses of frames. Index 0 contains the topmost frame.
  //
  ULONGLONG Frames[ ANYSIZE_ARRAY ];
} STACKTRACE, *PSTACKTRACE;

/*++
  Routine Description:
    Capture the stack trace based on the given CONTEXT.

  Parameters:
    Context    Thread context. If NULL, the current context is
               used.
    Event      Event structure to populate.
    MaxFrames  Max number of frames the structure can hold.
--*/
#ifdef _M_IX86
  //
  // Disable global optimization and ignore /GS waning caused by 
  // inline assembly.
  //
  #pragma optimize( "g", off )
  #pragma warning( push )
  #pragma warning( disable : 4748 )
#endif
HRESULT CaptureStackTrace(
  __in_opt CONST PCONTEXT InitialContext,
  __in PSTACKTRACE StackTrace,
  __in UINT MaxFrames 
  )
{
  DWORD MachineType;
  CONTEXT Context;
  HRESULT Hr;
  STACKFRAME64 StackFrame;

  ASSERT( StackTrace );
  ASSERT( MaxFrames &gt; 0 );

  if ( InitialContext == NULL )
  {
    //
    // Use current context.
    //
    // N.B. GetThreadContext cannot be used on the current thread.
    // Capture own context - on i386, there is no API for that.
    //
#ifdef _M_IX86
    ZeroMemory( &amp;Context, sizeof( CONTEXT ) );

    Context.ContextFlags = CONTEXT_CONTROL;
    
    //
    // Those three registers are enough.
    //
    __asm
    {
    Label:
      mov [Context.Ebp], ebp;
      mov [Context.Esp], esp;
      mov eax, [Label];
      mov [Context.Eip], eax;
    }
#else
    RtlCaptureContext( &amp;Context );
#endif  
  }
  else
  {
    CopyMemory( &amp;Context, InitialContext, sizeof( CONTEXT ) ); 
  }

  //
  // Set up stack frame.
  //
  ZeroMemory( &amp;StackFrame, sizeof( STACKFRAME64 ) );
#ifdef _M_IX86
  MachineType                 = IMAGE_FILE_MACHINE_I386;
  StackFrame.AddrPC.Offset    = Context.Eip;
  StackFrame.AddrPC.Mode      = AddrModeFlat;
  StackFrame.AddrFrame.Offset = Context.Ebp;
  StackFrame.AddrFrame.Mode   = AddrModeFlat;
  StackFrame.AddrStack.Offset = Context.Esp;
  StackFrame.AddrStack.Mode   = AddrModeFlat;
#elif _M_X64
  MachineType                 = IMAGE_FILE_MACHINE_AMD64;
  StackFrame.AddrPC.Offset    = Context.Rip;
  StackFrame.AddrPC.Mode      = AddrModeFlat;
  StackFrame.AddrFrame.Offset = Context.Rsp;
  StackFrame.AddrFrame.Mode   = AddrModeFlat;
  StackFrame.AddrStack.Offset = Context.Rsp;
  StackFrame.AddrStack.Mode   = AddrModeFlat;
#elif _M_IA64
  MachineType                 = IMAGE_FILE_MACHINE_IA64;
  StackFrame.AddrPC.Offset    = Context.StIIP;
  StackFrame.AddrPC.Mode      = AddrModeFlat;
  StackFrame.AddrFrame.Offset = Context.IntSp;
  StackFrame.AddrFrame.Mode   = AddrModeFlat;
  StackFrame.AddrBStore.Offset= Context.RsBSP;
  StackFrame.AddrBStore.Mode  = AddrModeFlat;
  StackFrame.AddrStack.Offset = Context.IntSp;
  StackFrame.AddrStack.Mode   = AddrModeFlat;
#else
  #error "Unsupported platform"
#endif

  StackTrace-&gt;FrameCount = 0;

  //
  // Dbghelp is is singlethreaded, so acquire a lock.
  //
  // Note that the code assumes that 
  // SymInitialize( GetCurrentProcess(), NULL, TRUE ) has 
  // already been called.
  //
  EnterCriticalSection( &amp;DbgHelpLock );

  while ( StackTrace-&gt;FrameCount &lt; MaxFrames )
  {
    if ( ! StackWalk64(
      MachineType,
      GetCurrentProcess(),
      GetCurrentThread(),
      &amp;StackFrame,
      MachineType == IMAGE_FILE_MACHINE_I386 
        ? NULL
        : &amp;Context,
      NULL,
      SymFunctionTableAccess64,
      SymGetModuleBase64,
      NULL ) )
    {
      //
      // Maybe it failed, maybe we have finished walking the stack.
      //
      break;
    }

    if ( StackFrame.AddrPC.Offset != 0 )
    {
      //
      // Valid frame.
      //
      StackTrace-&gt;Frames[ StackTrace-&gt;FrameCount++ ] = 
        StackFrame.AddrPC.Offset;
    }
    else
    {
      //
      // Base reached.
      //
      break;
    }
  }
  
  LeaveCriticalSection( &amp;DbgHelpLock );

  return S_OK;
}
#ifdef _M_IX86
  #pragma warning( pop )
  #pragma optimize( "g", on )
#endif
</pre>
</blockquote>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/47/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/47/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/47/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/47/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/47/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=47&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/03/12/walking-the-stack-of-the-current-thread/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
		<item>
		<title>When HEAP_NO_SERIALIZE heap operations are not quite &#8216;not serialized&#8217;</title>
		<link>http://jpassing.com/2008/02/28/when-heap_no_serialize-heap-operations-are-not-quite-not-serialized/</link>
		<comments>http://jpassing.com/2008/02/28/when-heap_no_serialize-heap-operations-are-not-quite-not-serialized/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 15:43:36 +0000</pubDate>
		<dc:creator>jpassing</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Verifier]]></category>

		<guid isPermaLink="false">http://jpassing.wordpress.com/?p=41</guid>
		<description><![CDATA[While the RTL Heap performs all heap operation in an interlocked manner by default, it can be requested not to serialize operations by passing the HEAP_NO_SERIALIZE flag to HeapCreate or HeapAlloc. In this case, the caller is in charge to provide proper synchronization. As it turns out, things change when Application Verifier heap checks are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=41&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While the RTL Heap performs all heap operation in an interlocked manner by default, it can be requested not to serialize operations by passing the HEAP_NO_SERIALIZE flag to HeapCreate or HeapAlloc. In this case, the caller is in charge to provide proper synchronization.
</p>
<p>
As it turns out, things change when Application Verifier heap checks are enabled for this process. To check heap operations, Application Verifier intercepts certain operations. Presumably to protect its own internal data structures, at some points, Avrf enters a critical section, as shown in this stack trace: </p>
<blockquote><pre>
0:009&gt; k
ChildEBP RetAddr  
0840f540 77d9cfad ntdll!ZwWaitForSingleObject+0x15
0840f5a4 77d9cf78 ntdll!RtlpWaitOnCriticalSection+0x154
<b>0840f5cc 6db25f92 ntdll!RtlEnterCriticalSection+0x152</b>
0840f5f0 6db29d99 vfbasics!AVrfpFreeMemLockChecks+0x42 
0840f60c 6db2bae8 vfbasics!AVrfpFreeMemNotify+0x39 
0840f668 77731d27 vfbasics!AVrfpRtlFreeHeap+0x148 
0840f67c 6d242757 kernel32!HeapFree+0x14
...
</pre>
</blockquote>
<p>
Entering a critical section is no big thing &#8212; nontheless, it effectvely means that heap operations, whether they are conducted with HEAP_NO_SERIALIZE flag set or not, must be considered to be at least partially serialized by Application Verifier.
</p>
<p>Although this somewhat contradicts the idea of the HEAP_NO_SERIALIZE flag, this should not be a problem in most cases &#8212; especially when HEAP_NO_SERIALIZE has been passed for performance reasons only. However, if HEAP_NO_SERIALIZE has been used for correctness reasons, this might indeed bite you. In my specific case, I used HEAP_NO_SERIALIZE and did the synchronization myself in order to break a deadlock situation. And while the application ran fine (and deadlock-free) without Avrf enabled, it began deadlocking randomly as soon as I enabled Avrf heap checks &#8212; for the very reason just described. This is especially annoying as I am now left with two less-than-optimal choices: Leave Avrf enabled for my test suite and experience random deadlocks or forgo Avrf&#8217;s helpful heap checks in order not to deadlock. Grrr, <a href='http://www.geek.nl/pics/dilbert-arch/dilbert-20060412.gif'>I need more choices!</a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jpassing.wordpress.com/41/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jpassing.wordpress.com/41/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpassing.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpassing.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpassing.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpassing.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpassing.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpassing.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpassing.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpassing.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpassing.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpassing.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpassing.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpassing.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpassing.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpassing.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpassing.com&#038;blog=1468393&#038;post=41&#038;subd=jpassing&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpassing.com/2008/02/28/when-heap_no_serialize-heap-operations-are-not-quite-not-serialized/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2568ab9d93774268403af71d7cadbf11?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jpassing</media:title>
		</media:content>
	</item>
	</channel>
</rss>
