<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bradley Grainger</title>
 <link href="http://bradleygrainger.com/atom.xml" rel="self"/>
 <link href="http://bradleygrainger.com/"/>
 <updated>2011-09-10T11:40:31-07:00</updated>
 <id>http://bradleygrainger.com/</id>
 <author>
   <name>Bradley Grainger</name>
   <email>bgrainger@gmail.com</email>
 </author>

 
 <entry>
   <title>How to use GitHub Pages on Windows</title>
   <link href="http://bradleygrainger.com/2011/09/07/how-to-use-github-pages-on-windows.html"/>
   <updated>2011-09-07T00:00:00-07:00</updated>
   <id>http://bradleygrainger.com/2011/09/07/how-to-use-github-pages-on-windows</id>
   <content type="html">&lt;h2&gt;Install&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;a href=&quot;http://rubyinstaller.org/&quot;&gt;RubyInstaller&lt;/a&gt;. (I used version 1.9.2-p290.)&lt;/li&gt;
&lt;li&gt;Install the RubyInstaller &lt;a href=&quot;https://github.com/oneclick/rubyinstaller/wiki/Development-Kit&quot;&gt;DevKit&lt;/a&gt; (be sure to customise the install path, e.g., to &lt;code&gt;C:\RubyDevKit&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Install &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/install&quot;&gt;Jekyll&lt;/a&gt;: Run &quot;Start Command Prompt with Ruby&quot;, then &lt;code&gt;gem install jekyll&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Install &lt;a href=&quot;http://python.org/download/&quot;&gt;Python&lt;/a&gt;. (I used Python 2.7.2 Windows Installer.)&lt;/li&gt;
&lt;li&gt;Install Python &lt;a href=&quot;http://pypi.python.org/pypi/setuptools&quot;&gt;setuptools&lt;/a&gt;. (I used setuptools-0.6c11.win32-py2.7.)&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;C:\Python27\Scripts&lt;/code&gt; to your path.&lt;/li&gt;
&lt;li&gt;Install &lt;a href=&quot;http://pygments.org/&quot;&gt;pygments&lt;/a&gt;: &lt;code&gt;easy_install pygments&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Apply the &lt;a href=&quot;https://gist.github.com/1185645&quot;&gt;patch&lt;/a&gt; (linked &lt;a href=&quot;http://madhur.github.com/blog/2011/09/01/runningjekyllwindows.html&quot;&gt;here&lt;/a&gt;) to prevent &quot;Liquid error: bad file descriptor&quot; being output when you use pygments: &lt;code&gt;cd /c/Ruby192/lib/ruby/gems/1.9.1/gems/albino-1.3.3/lib; patch &amp;lt; 0001-albino-windows-refactor.patch&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;Set up the GitHub Pages Repository&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Install &lt;a href=&quot;http://code.google.com/p/msysgit/&quot;&gt;msysGit&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Create a new repo as per the &lt;a href=&quot;http://pages.github.com/&quot;&gt;GitHub Pages documentation&lt;/a&gt;.

&lt;ul&gt;
&lt;li&gt;An easy way to get started is to copy or fork &lt;a href=&quot;https://github.com/mojombo/mojombo.github.com&quot;&gt;an existing repo&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Write and save a post in the &lt;code&gt;_posts&lt;/code&gt; folder.&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;Test&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Run &quot;Start Command Prompt with Ruby&quot; and change to your pages repo's working copy.&lt;/li&gt;
&lt;li&gt;Enter &lt;code&gt;jekyll --auto --server&lt;/code&gt; to build your site and launch a web server that rebuilds your site every time you save in your editor.&lt;/li&gt;
&lt;li&gt;Browse to &lt;a href=&quot;http://localhost:4000&quot;&gt;http://localhost:4000&lt;/a&gt; to see your site.&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;Publish&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a GitHub repository named after your user name (e.g., &lt;code&gt;bgrainger.github.com&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Push your local repository to GitHub.&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;Further Reading&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://madhur.github.com/blog/2011/09/01/runningjekyllwindows.html&quot;&gt;Running Jekyll on Windows&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.alexrothenberg.com/2011/01/27/moved-blog-to-jekyll-and-github-pages.html&quot;&gt;Moved Blog to Jekyll and GitHub Pages&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Profiling JScript, Part 1</title>
   <link href="http://bradleygrainger.com/2004/11/01/profiling-jscript-part-1.html"/>
   <updated>2004-11-01T00:00:00-08:00</updated>
   <id>http://bradleygrainger.com/2004/11/01/profiling-jscript-part-1</id>
   <content type="html">&lt;p&gt;Finishing my rewrite of the Logos Bible Software Home Page wasn't sufficiently appealing, so I started looking at JScript.dll in more detail.
I decided to get a feel for the architecture of jscript.dll by examining the call stack for a simple property access on one of our COM objects.
Placing a breakpoint in &lt;code&gt;CLbxScriptUtil::get_Date&lt;/code&gt; (the function invoked by &lt;code&gt;Application.ScriptUtil.Date&lt;/code&gt; in JScript) gave the following call stack:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ScriptUt.dll!CLbxScriptUtil::get_Date()  Line 158   C++
oleaut32.dll!_DispCallFunc@32()  + 0xc3
oleaut32.dll!CTypeInfo2::Invoke()  + 0x20c
oleaut32.dll!CTypeInfo2::Invoke()  + 0xc4
oleaut32.dll!CTypeInfo2::Invoke()  + 0xc4
ScriptUt.dll!ATL::IDispatchImpl::Invoke()  Line 4412    C++
jscript.dll!IDispatchInvoke()  + 0x6f
jscript.dll!InvokeDispatch()  + 0x72
jscript.dll!VAR::InvokeByName()  + 0x10c19
jscript.dll!CScriptRuntime::Run()  + 0xcd5d
jscript.dll!ScrFncObj::Call()  + 0x6a
jscript.dll!JsEval()  + 0xe2
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;jscript.dll!IDispatchInvoke() looked like an interesting function. Getting its decorated name out of jscript.dll and running it through
&lt;a href=&quot;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/undecoratesymbolname.asp&quot;&gt;UnDecorateSymbolName&lt;/a&gt;
gave the following method signature: &lt;em&gt;(Note: parameter types and names cleaned up by referring to the IDispatch::Invoke() documentation)&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;__stdcall&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDispatchInvoke&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CSession&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pSession&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDispatch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pDispatch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DISPID&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dispIdMember&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;REFIID&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;riid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;LCID&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lcid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;WORD&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;DISPPARAMS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pDispParams&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;VARIANT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pVarResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;EXCEPINFO&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pExcepInfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;puArgErr&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Although I can't prove it, I'm going to assume that this is a helper function used by JScript internally whenever IDispatch::Invoke
needs to be called. A simple profiling exercise would be to log how many times this function is called. You can see that when it's
calling our code, it immediately invokes ATL::IDispatchImpl (in the common header file atlcom.h). We could log all calls to Invoke
by changing atlcom.h and recompiling almost every source file in our application. Or we could try to intercept the function at runtime
and log accesses that way. Not only is the latter method a lot quicker (no recompilation necessary), but it greatly increases the
chances of GPFs (since this would involve programatically changing the bytes of a running application). It seemed like the obvious choice.&lt;/p&gt;

&lt;p&gt;I decided to write three functions: one to hook the IDispatchInvoke function, one to unhook it, and the hook function itself (which would
just increment a counter). For ease of testing, I'd invoke these functions from a JScript-accessible COM method. I chose to make
&lt;code&gt;ScriptUtil.Application&lt;/code&gt; install the hook, &lt;code&gt;ScriptUtil.Parent&lt;/code&gt; remove it, and &lt;code&gt;ScriptUtil.Date&lt;/code&gt; log the current
counter value.&lt;/p&gt;

&lt;p&gt;While a &amp;ldquo;production&amp;rdquo; version of this code would need to handle different base addresses for JScript, it's more expedient
to just hard-code the addresses of the code I want to change. The &lt;code&gt;nop&lt;/code&gt; bytes that need to be changed to a long jump are at
&lt;code&gt;0x75C6672E&lt;/code&gt;; the &lt;code&gt;mov edi, edi&lt;/code&gt; that needs to be changed to a short jump is at &lt;code&gt;0x75C66733&lt;/code&gt;. We also need
the address of our hook function (which is easy to get at runtime by using a function pointer). The fourth address needed is that of
the five &lt;code&gt;nop&lt;/code&gt; bytes in our hook function that we'll overwrite with a jump back to the third byte of JScript's IDispatchInvoke function.&lt;/p&gt;

&lt;p&gt;The blog post about &lt;code&gt;mov edi, edi&lt;/code&gt; that I mentioned in the last installment had a cryptic reference to something called
&amp;ldquo;Detours&amp;rdquo;. A little googling showed that &lt;a href=&quot;http://research.microsoft.com/sn/detours/&quot;&gt;Detours&lt;/a&gt; is a Microsoft Research
project that enables interception of arbitrary functions at runtime. It seems quite sophisticated, but assumes that the function being
&amp;ldquo;detoured&amp;rdquo; doesn't have the &lt;code&gt;mov edi, edi&lt;/code&gt; instruction at the beginning. As a result, Detours needs to copy the
first bytes of the function to a temporary location, since it overwrites code that's essential for the function's correct operation. Secondly,
while you can call the original function (through a &amp;ldquo;trampoline&amp;rdquo;), you need to supply all the original parameters (just like a
normal function call). While this makes sense conceptually, it could lead to subtle bugs if the calling convention or parameter types
weren't declared identically. I'm not currently using Detours, but did take a few ideas from their description of its implementation.&lt;/p&gt;

&lt;p&gt;My implementation is fairly simple. I have a &lt;a href=&quot;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_naked_attribute.asp&quot;&gt;&lt;code&gt;__declspec(naked)&lt;/code&gt;&lt;/a&gt; hook function that increments a global counter
and has five &lt;code&gt;nop&lt;/code&gt; bytes that are reserved for replacement with a long jump. A C++ helper class, &lt;code&gt;CEnablePageWrite&lt;/code&gt;,
calls &lt;a href=&quot;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/flushinstructioncache.asp&quot;&gt;FlushInstructionCache&lt;/a&gt;
and &lt;a href=&quot;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/virtualprotect.asp&quot;&gt;VirtualProtect&lt;/a&gt; (in
its constructor) to enable writing to the page containing the code to be modified; the destructor restores the original protection to the page.
Consultation of the &lt;a href=&quot;http://developer.intel.com/design/pentium4/manuals/index_new.htm&quot;&gt;Intel Architecture Software Developer's Manual&lt;/a&gt;,
Volume 2: Instruction Set Reference Manual gave the necessary bytes
for the &lt;code&gt;jmp&lt;/code&gt; opcodes. The hook installation function creates the short and long jumps in jscript.dll, and creates the long
jump inside the hook function. The hook uninstallation function restores the jscript.dll bytes to what they were originally. &lt;code&gt;CLbxScriptUtil::get_Date&lt;/code&gt;
just logs the current value of the counter to the logfile.&lt;/p&gt;

&lt;p&gt;All this code is running well on my machine; you're welcome to come over and take a look at details of the implementation or at its
output.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Profiling JScript, Part 0</title>
   <link href="http://bradleygrainger.com/2004/10/30/profiling-jscript-part-0.html"/>
   <updated>2004-10-30T00:00:00-07:00</updated>
   <id>http://bradleygrainger.com/2004/10/30/profiling-jscript-part-0</id>
   <content type="html">&lt;p&gt;As is my wont, I recently started looking at JScript.dll disassembly again and wondering about the possibility of implementing a JScript
profiler. It seems that with XP SP2 I now have symbols for JScript.dll on my system, which makes it a lot easier to understand the code.
(Symbols provide function names and parameter lists for those functions.) While reading the assembly listings, I noticed that
most functions look like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;nasm&quot;&gt;&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;edi&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;ebp&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;mov&lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;ebp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;esp&lt;/span&gt;
   &lt;span class=&quot;nf&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;ret&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The second and third lines are standard x86 function prologue (setting up the stack), but I couldn't understand the apparently-useless &lt;code&gt;mov edi, edi&lt;/code&gt; (or &lt;code&gt;edi = edi&lt;/code&gt; in C++) instruction at the begining
of every function. There's also a five-byte gap between functions (that seems to be always five bytes, and not a variable
number, e.g., to force functions to align on 16-byte boundaries). A quick Google search turned up &lt;a href=&quot;http://blogs.msdn.com/ishai/&quot;&gt;Ishai's&lt;/a&gt; blog post
&quot;&lt;a href=&quot;http://blogs.msdn.com/ishai/archive/2004/06/24/165143.aspx&quot;&gt;Why does the compiler generate a MOV EDI, EDI instruction at the beginning of functions?&lt;/a&gt;&quot;.
In short, this extraneous two-byte instruction (and the five bytes of &lt;code&gt;nop&lt;/code&gt; (no operation, i.e., do nothing) at the end)
are inserted to enable hot-patching of the application. The five &lt;code&gt;nop&lt;/code&gt; bytes can be replaced with a &quot;long jump&quot;
instruction (the &lt;code&gt;jmp&lt;/code&gt; opcode, and a 32-bit address to jump to), while the two-byte &lt;code&gt;mov edi, edi&lt;/code&gt; instruction can
be replaced with a &quot;short jump&quot; instruction (the &lt;code&gt;jmp&lt;/code&gt; opcode, and an 8-bit offset) that jumps to the long jump.
But why use &lt;code&gt;mov edi, edi&lt;/code&gt; instead of two &lt;code&gt;nop&lt;/code&gt; instructions? If you are patching the application
while it's running, it's theoretically possible that one thread could have executed the first &lt;code&gt;nop&lt;/code&gt; but not the second. If your
patch thread then changes both bytes (to the short jump), the thread that's about to read the second &lt;code&gt;nop&lt;/code&gt; instruction
will instead read the offset your patch just inserted, and attempt to interpret that offset as a valid x86 opcode. Bad things would
almost certainly happen. &lt;code&gt;mov edi, edi&lt;/code&gt; takes two bytes, and so it's impossible for the instruction pointer to be
pointing at the second byte.
This should enable us to hook certain functions in JScript.dll very easily. Since JScript.dll is loaded into our process's address
space, we can change the bytes making up its code easily. We first get the address of our profiling function and overwrite the five &lt;code&gt;nop&lt;/code&gt; bytes
with a jump to that address. We then change the &lt;code&gt;mov edi, edi&lt;/code&gt; instruction to jump to our long jump. Now, whenever
that function is called, execution will quickly pass to our profiling function. We just have to make sure we return to the right
instruction with the stack &lt;i&gt;exactly&lt;/i&gt; as we found it. How hard could that be?&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>First VS2005 bug report</title>
   <link href="http://bradleygrainger.com/2004/07/28/first-vs2005-bug-report.html"/>
   <updated>2004-07-28T00:00:00-07:00</updated>
   <id>http://bradleygrainger.com/2004/07/28/first-vs2005-bug-report</id>
   <content type="html">&lt;p&gt;Microsoft has recently launched a &lt;a href=&quot;http://lab.msdn.microsoft.com/ProductFeedback/&quot;&gt;Product Feedback&lt;/a&gt; site, which includes a bug reporting section. This turns out to be a great place to report bugs they introduced in VS2002/2003 and still haven't fixed.&lt;/p&gt;

&lt;p&gt;An example of this would be a subtle, insidious bug in compiler-generated code that was introduced with ATL 7.0 in Visual Studio 2002. This was the problem that caused LDLS to crash if Dragon Naturally Speaking was running. It's easy enough to work around, but it would be nice to have the auto-generated code be bug free. Here's my &lt;a href=&quot;http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=b1f188b6-5e35-4284-8e7e-57daf2a66be0&quot;&gt;bug report&lt;/a&gt;; let's see what happens with it&amp;hellip;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update 2011-09-09:&lt;/strong&gt; The bug is still tracked on &lt;a href=&quot;https://connect.microsoft.com/VisualStudio/feedback/details/100561/incorrect-logic-in-dllcanunloadnow-for-vc-atl-project&quot;&gt;Connect&lt;/a&gt; all these years later. And Microsoft fixed it!&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Cracking a DLL, Part 2</title>
   <link href="http://bradleygrainger.com/2004/03/29/cracking-a-dll-part-2.html"/>
   <updated>2004-03-29T00:00:00-08:00</updated>
   <id>http://bradleygrainger.com/2004/03/29/cracking-a-dll-part-2</id>
   <content type="html">&lt;h2&gt;Introduction&lt;/h2&gt;

&lt;p&gt;In &lt;a href=&quot;../26/cracking-a-dll-part-1.html&quot;&gt;Part 1&lt;/a&gt;, I explained the motivation
for cracking X.dll, and gave a brief look at what a bottom-up investigation of the DLL revealed. For a variety
of reasons, that approach ultimately ended in failure. I tried again, this time with a top-down approach. (I also tried harder.)&lt;/p&gt;

&lt;h2&gt;Top-down Approach&lt;/h2&gt;

&lt;h3&gt;Finding Differences&lt;/h3&gt;

&lt;p&gt;I decided to run the code twice, once with the current date, and again with the system time
set to a month in the future. Any time I reached an &quot;if&quot; statement in the code, I would note whether the code took
the if branch or the else branch. (Technically, I would note whether the program jumped to a different instruction
or not each time it reached a conditional jump instruction. If statements are coded using the x86
conditional jump instructions: &lt;code&gt;ja&lt;/code&gt; (jump if above), &lt;code&gt;jae&lt;/code&gt; (jump if above or equal), &lt;code&gt;je&lt;/code&gt; (jump if equal), &lt;code&gt;jne&lt;/code&gt; (jump
if not equal), etc. The &quot;conditional&quot; part of the jump tests the processor flags, which are set by a previous instruction
(e.g., &lt;code&gt;cmp&lt;/code&gt; (to compare two numbers), or &lt;code&gt;test&lt;/code&gt; (to test a bit in a number)).)&lt;/p&gt;

&lt;p&gt;The first time through, I would step through the &lt;code&gt;CreateData&lt;/code&gt; function in X.dll, and not step into
any functions it called. I would note the return value of each function, and if they returned something different, I'd run the
program again (with both dates), stepping into that function to see where the execution differed that time.&lt;/p&gt;

&lt;h3&gt;Set Up&lt;/h3&gt;

&lt;p&gt;Firstly, I disassembled the entire DLL by running &lt;code&gt;dumpbin /disasm:bytes X.dll &amp;gt; X.asm&lt;/code&gt;. This
provided the assembly language instructions along with the equivalent machine code bytes. That would let me tie the
disassembly I was seeing in the debugger back to the actual bytes of the DLL if I ever needed to make any modifications.&lt;/p&gt;

&lt;h3&gt;Debugging CreateData&lt;/h3&gt;

&lt;p&gt;I fired up the program and stepped into the &lt;code&gt;CreateData&lt;/code&gt; function. This immediately dumped me into
disassembly view, since no source code was available. I selected the instructions (from the current location to the first
&lt;code&gt;ret&lt;/code&gt; (return from function) statement) in the debugger window and copied it into
&lt;a href=&quot;http://www.textpad.com/&quot;&gt;TextPad&lt;/a&gt;. As I recognised standard assembly language conventions (set
up function call frame, allocate local variables, call functions with parameters, etc.), I annotated the source code with what I thought
it was doing. I also annotated the source with the code path that was taken.&lt;/p&gt;

&lt;p&gt;Very quickly I found a difference in the behaviour of the program with the different dates. With a valid run date,
the function at offset &lt;code&gt;0x1C6B4&lt;/code&gt; (hereinafter called &lt;code&gt;Init&lt;/code&gt;) would return &lt;code&gt;0x00D40BA8&lt;/code&gt;. In the
future, this function would return 0. This value was stored in memory (in a global variable). The next function that was
called would take a while to execute with a valid date (probably loading data from disk), but would throw an exception
when called with a future date. The &lt;code&gt;Init&lt;/code&gt; function obviously deserved more scrutiny.&lt;/p&gt;

&lt;h3&gt;Debugging Init&lt;/h3&gt;

&lt;p&gt;As with &lt;code&gt;CreateData&lt;/code&gt;, I copied the source code for &lt;code&gt;Init&lt;/code&gt; to TextPad and started annotating it as I stepped
through. This time I found a point where the program would not follow a &lt;code&gt;jae&lt;/code&gt; instruction when running with a valid date,
but would jump when running with a date in the future:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;nasm&quot;&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;20008&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;E88&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; Function calls of mystery&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;2000289&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;2000530&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;cmp&lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;; First comparison -- edx=0 before and after bomb-date&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;jne&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C7C0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;cmp&lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;1087h&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; eax=0x1085 before bomb date, eax=0x1089 after bomb date&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;jae&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C7E6&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; jumps after bomb date, doesn&amp;#39;t jump before&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;jmp&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C7C2&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; program always jumps here (before bomb date)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The condition being tested was whether &lt;code&gt;eax&lt;/code&gt; (an Intel x86 register, i.e.,
a variable) was greater than or equal to 4231. There were three function calls in a row immediately before this test; I
decided to examine each in sequence.&lt;/p&gt;

&lt;p&gt;When I stepped into the first function, I realised that I had seen this function before: it was the function that
called &lt;code&gt;GetLocalTime&lt;/code&gt; and validated the date, returning a floating point number. The second function that &lt;code&gt;Init&lt;/code&gt;
called was simple to figure out; it just converted that floating point number to an integer. (I had become confused
about what was happening where with my bottom-up approach the day before; I now saw that these were two
separate functions.) The third function was complicated and had lots of if/else statements, but the end result was
that it divided one 64-bit integer (the number of days) by another 64-bit integer (the hardcoded value 9) and
returned a 64-bit integer.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;cmp&lt;/code&gt; tests following those function calls were now simple to figure out. The first checked that the
result of this division was less than 2&lt;sup&gt;32&lt;/sup&gt; (specifically, that the upper 32 bits of the result were 0). The second checked
whether the lower 32 bits of the result were less than 4231. In short, X was checking that fewer than 4231 9-day periods
had elapsed since 1 Jan 1900. (It appears that the programmer was trying to be slightly sneaky here. By dividing
by 9, he changed the hardcoded value that he was checking against (4231) to something one might not be expecting.
Indeed, at first look, I had no idea what 4231 referred to.) If the test succeeded, &lt;code&gt;Init&lt;/code&gt; loaded the magic number
&lt;code&gt;0x00D40BA8&lt;/code&gt; out of memory and returned it, otherwise it returned 0.&lt;/p&gt;

&lt;h3&gt;Changing the DLL&lt;/h3&gt;

&lt;p&gt;The hardest part was deciding how to change the DLL. Do I extend the number of 9-day periods that are allowed?
Should I skip over the whole date check, and just return the magic number? Perhaps just change the test so that
it always jumps to the correct instruction, whether or not eax &amp;lt; 4231?
Adding or modifying any jump instructions would require me to pull out a hex calculator and perform some subtraction,
because the instruction to which you jump is specified as an offset from the current instruction (to save bytes). This
sounded like hard work, so instead I decided to just comment out the whole &lt;code&gt;jae&lt;/code&gt; instruction. I did this
by overwriting the appropriate bytes in the file with &lt;code&gt;0x90&lt;/code&gt;, the opcode for &lt;code&gt;nop&lt;/code&gt; (no operation; do nothing).
The original source code looked like this (my annotations added after semicolons):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;nasm&quot;&gt;&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; use the number 9 as the hardcoded parameter to DivInt64&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;20008&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;E88&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; call GetCurrentTimeAsDouble&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;2000289&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; call ConvertDoubleToInt64&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;2000530&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; call DivInt64 (divide one 64-bit integer by another)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;cmp&lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;; make sure upper 32 bits of quotient are 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;jne&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C7C0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;cmp&lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;eax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;1087h&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;; is quotient &amp;lt; 4231?&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;jae&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C7E6&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; greater than or equal, jump to BadDate&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;jmp&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C7C2&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; must be less than, jump to GoodDate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;I edited the DLL in a binary editor, replacing the opcodes for the &lt;code&gt;cmp&lt;/code&gt; and &lt;code&gt;jae&lt;/code&gt; instructions
(seven bytes total) with &lt;code&gt;0x90&lt;/code&gt;. This effectively comments out the comparison aginst 4231 and the jump if the value
is greater, letting the program fall through to the unconditional &lt;code&gt;jmp&lt;/code&gt; at address &lt;code&gt;2001C7BE&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;nasm&quot;&gt;&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;; use the number 9 as the hardcoded parameter to DivInt64&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;push&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;20008&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;E88&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; call GetCurrentTimeAsDouble&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;2000289&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; call ConvertDoubleToInt64&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;2000530&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; call DivInt64 (divide one 64-bit integer by another)&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;cmp&lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;edx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;; make sure upper 32 bits of quotient are 0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;jne&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C7C0&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;                  &lt;span class=&quot;c1&quot;&gt;; do nothing&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;nop&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;jmp&lt;/span&gt;     &lt;span class=&quot;mi&quot;&gt;2001&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C7C2&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;; always jump to GoodDate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;It's not the most elegant fix in the world; I certainly changed much more than I needed
to. A good one byte fix would have been to change the comparison against &lt;code&gt;0x1087&lt;/code&gt; to &lt;code&gt;0x7f001087&lt;/code&gt; (a date
centuries in the future). But why be satisfied with changing one whole byte? We could change the number to &lt;code&gt;0x40001087&lt;/code&gt;,
changing just one bit. I'll try even harder next time.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Cracking a DLL, Part 1</title>
   <link href="http://bradleygrainger.com/2004/03/26/cracking-a-dll-part-1.html"/>
   <updated>2004-03-26T00:00:00-08:00</updated>
   <id>http://bradleygrainger.com/2004/03/26/cracking-a-dll-part-1</id>
   <content type="html">&lt;h2&gt;Preface&lt;/h2&gt;

&lt;p&gt;At the last company meeting, it was revealed that the resident company &quot;cr4x0r&quot; had removed a bomb date
from a DLL used in an upcoming product.&lt;/p&gt;

&lt;p&gt;The company meeting was a bit light on technical detail, so (thanks to Jacob's prompting) I'm going to blog how it was done for any interested parties.&lt;/p&gt;

&lt;h2&gt;Background and Motivation&lt;/h2&gt;

&lt;p&gt;The specialised search engine is implemented by a library named X. Somehow, someone found out that X.dll would stop working after 1 April 2004.&lt;/p&gt;

&lt;p&gt;Since this DLL is the core of the product, it was essential to modify it so it would continue to work. Not having the source code for the DLL, this was a non-trivial operation.&lt;/p&gt;

&lt;h2&gt;Possible Approaches&lt;/h2&gt;

&lt;p&gt;All that we have to work with is the machine code output from the Delphi compiler. Somewhere in the DLL
must be a check of the current time against a bomb date. Maybe the bomb date is hard-coded in the DLL,
maybe it is in the datafiles. Maybe the bomb date is the actual date, maybe it's the number of 100-nanosecond
intervals since 1 Jan 1601. Searching for the bomb date itself would not be profitable; however, there must
be some code with the following structure:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CurrentDate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BombDate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;then&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;proceed&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;crash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;There were two ways to go about finding this code (or the assembly language representation of it) in the DLL:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Bottom-up&lt;/strong&gt; &amp;mdash; find the code that gets the current time and examine the surrounding code in hopes of finding the check.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Top-down&lt;/strong&gt; &amp;mdash; debug the program one line at a time, with the system clock set before and after the bomb date, looking for differences in program flow (i.e., finding the if/else statement).&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;I elected to pursue a bottom-up approach first; it seemed that this would require less assembly language code to be examined.&lt;/p&gt;

&lt;h2&gt;Bottom-up Approach&lt;/h2&gt;

&lt;h3&gt;Finding the Code That Gets the Current Time&lt;/h3&gt;

&lt;p&gt;At some point, X.dll has to ask the operating system what the current time is. Windows provides the functions
&lt;code&gt;GetSystemTime&lt;/code&gt;, &lt;code&gt;GetSystemTimeAsFileTime&lt;/code&gt; and &lt;code&gt;GetLocalTime&lt;/code&gt; for this purpose. If we were
lucky, we would find a direct call to one of these functions in X.dll. If we were unlucky, X would descramble
the desired function name at run time (so that you couldn't find the name in the binary) and dynamically link to kernel32.dll
in order to call the function.&lt;/p&gt;

&lt;p&gt;I fired up &lt;a href=&quot;http://www.dependencywalker.com&quot;&gt;Dependency Walker&lt;/a&gt; and, sure enough, X.dll contained a reference to &lt;code&gt;GetLocalTime&lt;/code&gt; (in kernel32.dll).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/crack-depends.png&quot; alt=&quot;Dependency Walker window showing import of GetLocalTime&quot; /&gt;&lt;/p&gt;

&lt;p&gt;(This reference is stored so the OS loader can fix up addresses when the DLL is loaded. Because DLLs can be loaded at different addresses in memory (and because
kernel32.dll is different in different versions of Windows), the actual location of
the &lt;code&gt;GetLocalTime&lt;/code&gt; function can't be hardcoded in X.dll. Instead, a &lt;em&gt;relocation table&lt;/em&gt; is used.
The relocation table basically looks like this (well, in pseudo-code):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetLocalTimeStub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LPSYSTEMTIME&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lpSystemTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetLocalTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetSystemTimeStub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LPSYSTEMTIME&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lpSystemTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetSystemTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The DLL's compiled code calls the &lt;code&gt;GetLocalTimeStub&lt;/code&gt; stub function instead of the real kernel32.dll function.
At load time, the OS walks the relocation table and fills in the actual address of the functions in other DLLs. Since those
DLLs have already been loaded into memory, the real addresses of the functions are known.)
I suddenly realised that I didn't know how to read relocation tables. I wanted to put a breakpoint on
&lt;code&gt;GetLocalTimeStub&lt;/code&gt; so that I could debug the program at the point where it called this function, but I didn't
know where this function was located in X.dll. However, Dependency Walker told me that
&lt;code&gt;GetLocalTime&lt;/code&gt; is located at an offset of &lt;code&gt;0x15A68&lt;/code&gt; in kernel32.dll. Combine that with
&lt;a href=&quot;http://www.sysinternals.com/ntw2k/freeware/procexp.shtml&quot;&gt;Process Explorer&lt;/a&gt; telling me that LDLS.exe
loaded kernel32.dll at &lt;code&gt;0x77E60000&lt;/code&gt; and we know that the code for &lt;code&gt;GetLocalTime&lt;/code&gt; is located at
&lt;code&gt;0x77E75A68&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/crack-procexp.png&quot; alt=&quot;Process Explorer window showing address of Kernel32.dll&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the debugger, it's as simple as pulling up the Disassembly window, entering that address
in the address box, and setting a breakpoint on that byte of code. (With debugging symbols installed, the debugger will
even helpfully put &quot;_GetLocalTime@4:&quot; above that line of assembly language.)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/crack-disasm.png&quot; alt=&quot;Disassembly of GetLocalTime&quot; /&gt;&lt;/p&gt;

&lt;h3&gt;Analysing the Code That Gets the Current Time&lt;/h3&gt;

&lt;p&gt;We now invoked the &lt;code&gt;CreateData&lt;/code&gt; function in X.dll. The debugger immediately stopped on a call
to &lt;code&gt;GetLocalTime&lt;/code&gt;. The call stack window revealed that we were four functions deep inside X.dll.
The function that was calling &lt;code&gt;GetLocalTime&lt;/code&gt; was the first thing to investigate. It allocated 32 bytes on the stack (the
size of a SYSTEMTIME struct), passed the address of that struct to &lt;code&gt;GetLocalTime&lt;/code&gt;, then called another function with
the &lt;code&gt;st.wYear&lt;/code&gt;, &lt;code&gt;st.wMonth&lt;/code&gt;, and &lt;code&gt;st.wDay&lt;/code&gt; values. This in turn called a third function which appeared to check the validity
of the date. It ensured that the year was between 1 and 9999, that the month was between 1 and 12, and that the
day of the month was between 1 and the maximum number of days in that month (having already called a fourth
function to determine if the current year was a leap year). Since we just got the date from the OS, it seemed unlikely
that it would be invalid, but it can't hurt to check, I guess. It then calculated (based on the year/month/day values) the number
of days since 1 Jan 1900 (just making a guess at this epoch start date, actually, but it's probably somewhere around that time)
and converted that to a floating point number. A similar validity-checking procedure was carried out with the hour,
minute, and second values, and then they were converted to a fraction between 0 and 1 and added to that floating point number. The
program then immediately converted that floating point number to an integer, stripping off the fractional part.
We continued debugging the code, making notes on what we thought the program was currently doing, until
we realised that I was hopelessly lost. We hadn't written down the execution path of the program in great detail, and
I didn't know where I was in the program. The date check might already have been done. Or the date
might just have been stored in memory, and the check was going to be performed elsewhere. At that moment, D.F. called
and invited us to see a movie. It seemed like we had reached a good stopping place.&lt;/p&gt;

&lt;p&gt;Continue reading &lt;a href=&quot;../29/cracking-a-dll-part-2.html&quot;&gt;Part 2&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ATL proxy/stub DllCanUnloadNow bug</title>
   <link href="http://bradleygrainger.com/2004/03/26/atl-proxy-stub-dllcanunloadnow-bug.html"/>
   <updated>2004-03-26T00:00:00-08:00</updated>
   <id>http://bradleygrainger.com/2004/03/26/atl-proxy-stub-dllcanunloadnow-bug</id>
   <content type="html">&lt;p&gt;All our of COM server DLLs have proxy/stub code, which is used when data needs to be marshalled across
process boundaries. For example, &lt;code&gt;LibSys.exe&lt;/code&gt; loads the actual code for the &lt;code&gt;LbxMetadataCache&lt;/code&gt; object,
but &lt;code&gt;LDLS.exe&lt;/code&gt; calls this code. &lt;em&gt;Proxy&lt;/em&gt; code is loaded into the &lt;code&gt;LDLS.exe&lt;/code&gt; process,
which forwards the call to the &lt;em&gt;stub&lt;/em&gt; code in &lt;code&gt;LibSys.exe&lt;/code&gt;, which then calls the actual &lt;code&gt;LbxMetadataCache&lt;/code&gt;
code. By and large, this proxy/stub code is created automatically for you.
With ATL, you have the choice of building the proxy/stub as a separate DLL, or merging the proxy/stub code
with your main COM DLL. We chose the latter route, since it halves the number of DLLs we need to ship. Whichever
route you choose, the ATL COM server project wizard generates the appropriate code for you. Part of the generated
code is the &lt;code&gt;DllCanUnloadNow&lt;/code&gt; function. This method returns &lt;code&gt;S_OK&lt;/code&gt; (aka true) if it's safe for Windows
to unload the DLL, or &lt;code&gt;S_FALSE&lt;/code&gt; (aka false) if it's not. Your implementation should keep track of whether any
clients are using the objects in your DLL, and only return &lt;code&gt;S_OK&lt;/code&gt; if you're not being used any more.
If the proxy/stub code is merged into your DLL, your &lt;code&gt;DllCanUnloadNow&lt;/code&gt; method has two things to
keep track of:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Are any clients using the COM objects defined in this DLL?&lt;/li&gt;
&lt;li&gt;Are any clients using proxy code to communicate with COM objects loaded into another process?&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;You can only unload if the answer to both of these questions is &quot;no&quot;.&lt;/p&gt;

&lt;p&gt;ATL 7.0 changes the auto-generated code for this function significantly. Here is the code from ATL 6.0:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;n&quot;&gt;STDAPI&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DllCanUnloadNow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#ifdef _MERGE_PROXYSTUB&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// proxy/stub code&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PrxDllCanUnloadNow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_OK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#endif&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// check module lock count&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetLockCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_OK&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_FALSE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Here is the code generated by ATL 7.0:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;n&quot;&gt;STDAPI&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DllCanUnloadNow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#ifdef _MERGE_PROXYSTUB&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// proxy/stub code&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;HRESULT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PrxDllCanUnloadNow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FAILED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;#endif&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;// check module lock count&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_AtlModule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DllCanUnloadNow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The problem is that &lt;code&gt;S_FALSE&lt;/code&gt; is still a success value, and &lt;code&gt;FAILED(hr)&lt;/code&gt; will be false. So even if the proxy
says &quot;Don't unload me; clients are still using me as a proxy&quot;, the DLL will be unloaded if no clients are using the DLL directly.&lt;/p&gt;

&lt;p&gt;So, how did we find this out? If Dragon NaturallySpeaking is running in the background, the system becomes a lot more
aggressive about checking for DLLs that are no longer being used. It will very frequently query &lt;code&gt;metcache.dll&lt;/code&gt; to find
out whether it can be unloaded. The proxy says, &quot;I'm still in use, don't unload me&quot;. &lt;code&gt;DllCanUnloadNow&lt;/code&gt; ignores that
result and asks if any clients are using the COM object directly. This is false, so the DLL tells Windows to unload it. Windows
then unloads the DLL. Fractions of a second later, LDLS calls the &lt;code&gt;LbxMetadataCache&lt;/code&gt; proxy code, which &lt;em&gt;no
longer exists in memory!&lt;/em&gt; This, as you might expect, is no good. An exception occurs, and error tips start popping up
on the user's screen.&lt;/p&gt;

&lt;p&gt;I fixed the problem by changing:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;    &lt;span class=&quot;n&quot;&gt;HRESULT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PrxDllCanUnloadNow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FAILED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;to:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;    &lt;span class=&quot;n&quot;&gt;HRESULT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PrxDllCanUnloadNow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;S_OK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This causes the DLL to stay in memory if any clients are using it as a proxy. Only when no one is using it as a proxy and no one is using the COM objects directly will the DLL be unloaded.&lt;/p&gt;

&lt;p&gt;See also &lt;a href=&quot;/2004/07/28/first-vs2005-bug-report.html&quot;&gt;First VS2005 bug report&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Code Comments</title>
   <link href="http://bradleygrainger.com/2003/09/24/code-comments.html"/>
   <updated>2003-09-24T00:00:00-07:00</updated>
   <id>http://bradleygrainger.com/2003/09/24/code-comments</id>
   <content type="html">&lt;p&gt;One of our interns left some, uh, interesting comments in the code. Here are some I found just going through one source file. (Comments are word-wrapped here.  In the original source, each comment was on just one line (up to 500 characters long).)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// the &quot;i&quot; on the end of the [regular expression] won't work if it's in a string format (which it must be to get that i on the end...AAH, CATCH-22, MY ANCIENT NEMESIS)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// This is necessary because this main window actually modifies the start date passed in from the properties dialog (global variables at work, oh ya), so when we pass it back in if we decide to open the properties dialog again once we've looked at our calendar, the start date is bad and causes the properties dialog to crash!  I fixed this by simply keeping track of what the properties dialog returns separately from the gStartDate variable.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// Get all the g00dz from the source window&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// we are about to perfom some serious loadification&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// MAN, I HOPE THEY DON'T EVER DECIDE TO HAVE A DIFFERENT NUMBER OF DAYS IN THE WEEK THAN SEVEN!@#!@3~4112!@&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// Collect the latest and greatest saving xml from the peerless and altogether unmatched CreateSaveXML function&lt;/code&gt;
[ editor's note: I deleted that function. ]&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// append the goodness to the main document's XML container for subsequent savification&lt;/code&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>static in C++</title>
   <link href="http://bradleygrainger.com/2003/08/28/static-in-cplusplus.html"/>
   <updated>2003-08-28T00:00:00-07:00</updated>
   <id>http://bradleygrainger.com/2003/08/28/static-in-cplusplus</id>
   <content type="html">&lt;p&gt;What does the C/C++ keyword &lt;code&gt;static&lt;/code&gt; mean? When used inside a function, e.g.:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;it means that a single instance of this variable is allocated for the lifetime of the program. In terms of storage,
it's very similar to a global, but its scope is restricted to that function.&lt;/p&gt;

&lt;p&gt;C++ inherits this sense of &lt;code&gt;static&lt;/code&gt; for class definitions. If a variable is declared as &lt;code&gt;static&lt;/code&gt; in a class, there is one instance of that variable (again, allocated very similarly to a global) that is shared between all
instances of that class. Declaring the variable as &lt;code&gt;protected&lt;/code&gt; or &lt;code&gt;private&lt;/code&gt; controls its scope.&lt;/p&gt;

&lt;p&gt;There is another meaning of &lt;code&gt;static&lt;/code&gt;. In C, it was the opposite of &lt;code&gt;extern&lt;/code&gt;; it meant that a variable had internal linkage semantics and wasn't visible from any other translation unit. (That is, you could only access it from within the file in which it was defined.) This use of &lt;code&gt;static&lt;/code&gt; is deprecated in C++; use an unnamed namespace instead:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;&lt;span class=&quot;n&quot;&gt;namespace&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s_variable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Since this use is deprecated, &lt;code&gt;static&lt;/code&gt; should only be used inside a class or function definition. This rule would prohibit, for example, declaring static constants (or worse yet, variables) in a header file. The problem with this is that every source file that includes that header file gets its own copy of the constant or variable. There are no errors about duplicate symbols, because the linker respects the old C meaning of internal linkage. In the case of constants, barring linker optimisation, the constant would appear multiple times in the binary, instead of just once. In the case of variables, although it might initially appear that the functions in different source files are sharing the variable, each source file has its own copy and changes to the variable won't be seen between different source files.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Changes to Asynchronous Pluggable Protocol Handlers</title>
   <link href="http://bradleygrainger.com/2003/08/27/changes-to-asynchronous-pluggable-protocol-handlers.html"/>
   <updated>2003-08-27T00:00:00-07:00</updated>
   <id>http://bradleygrainger.com/2003/08/27/changes-to-asynchronous-pluggable-protocol-handlers</id>
   <content type="html">&lt;p&gt;After dealing with yet another customer (through tech support) who was having problems directly attributable to a full/corrupted Temporary Internet Files folder, I decided to change the implementation of our &lt;strong&gt;lbxfile:&lt;/strong&gt; and &lt;strong&gt;lbxres:&lt;/strong&gt; protocols. (We're alpha testing, after all :-).)&lt;/p&gt;

&lt;h2&gt;Current Approach&lt;/h2&gt;

&lt;p&gt;Even though we can open a stream directly on an embedded file, our protocols work by saving the data to a temp file (so that the embedded file isn't locked while IE is accessing it). Until now, this temp file has been stored in the Temporary Internet Files folder (TIF).&lt;/p&gt;

&lt;h2&gt;Problems with Current Approach&lt;/h2&gt;

&lt;p&gt;There appear to be numerous problems with using the UrlCache functions to store this data in TIF:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The size of this folder seems to grow without limit, even though there is a limit set in the IE &gt; Tools &gt; Internet Options &gt; General &gt; Temporary Internet Files/Settings dialog. D.D. just reported deleting 10GB of files. Just accessing a few reports can write a megabyte of data to the cache (Common.js is 184KB).&lt;/li&gt;
&lt;li&gt;The files are never cleaned up properly. This is a minor point, but many megabytes of source code are easily accessible in TIF.&lt;/li&gt;
&lt;li&gt;When the cache gets full, new files are deleted without warning. A symptom of this is when View &gt; Source in IE opens an empty Notepad window. The file is successfully written to the cache, but is deleted immediately before the filename of the cache file is returned to the client. This manifests itself as users' command bars failing to load (because the images for the buttons are inaccessible).&lt;/li&gt;
&lt;li&gt;Filling the cache breaks other components/programs. IE's View &gt; Source is one example, but Libronix Update also uses the cache to download files. Way back in the original Alpha days, we had a machine that couldn't launch Libronix Update because the .lbxupd file itself was deleted before Libronix Update could open it.&lt;/li&gt;
&lt;li&gt;Other weird errors. NewVerseTemplate.xslt mysteriously failing to load was almost certainly a TIF problem; changing &lt;code&gt;.Item&lt;/code&gt; to &lt;code&gt;.OpenStream&lt;/code&gt; (in BibleUtility.js) fixed it.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;New Approach&lt;/h2&gt;

&lt;p&gt;It still seems advantageous to write the data to a temporary file (to avoid locking problems). We're now writing it to a location we control: a subfolder of the user's temp folder. lbxfile: writes files to &lt;code&gt;%TEMP%\lbxfile&lt;/code&gt; and lbxres: writes files to &lt;code&gt;%TEMP%\lbxres&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Problems with New Approach&lt;/h2&gt;

&lt;p&gt;There is still the thorny issue of file lifetime management. The protocols create an object (that implements IStream) that reads from the temp file. It would be nice if we could delete the temp file as soon as this object is released. However, as part of the protocol operations, we call &lt;code&gt;ReportProgress(BINDSTATUS_CACHEFILENAMEAVAILABLE)&lt;/code&gt;, which lets the caller know the name of our temp file. If we omit this call, IE will still load dialogs and reports properly, but &lt;code&gt;UrlDownloadToCacheFile&lt;/code&gt; or &lt;code&gt;UrlDownloadToFile&lt;/code&gt;, which are used by CommandBars and TrayIcons, will fail. However, by reporting the filename, we're implicitly granting clients the right to access that file later on. Our current solution is to have a worker thread that runs in the background. Every five minutes it wakes up, scans the temp folder and deletes files that were last accessed over ten minutes ago. The folders get cleaned when the thread is started (when FileProt.dll or ResProt.dll is loaded) but are left intact when the app shuts down (since there isn't enough time to set a DllBeingUnloaded event then wait for the thread to wake up and delete everything when the DLL is being unloaded). (If we wanted maximum purity, the app itself could delete the known folders after waiting for threads to shut down.)
The result of this is that at most fifteen minutes worth of data (probably several megabytes) could be left in the user's temp folder in between runs of the program.&lt;/p&gt;

&lt;h2&gt;So?&lt;/h2&gt;

&lt;p&gt;This will be in the release after 2.1 Alpha 3, which should probably be an alpha (instead of a beta) release just because of this change. The code's still fairly untested under heavy use (I zipped the shell and some addins on my system, and it worked well enough), so it will be good to have some reports that it's working correctly. There probably won't be any amazing success stories with it (maybe one or two users who were having weird problems will be fixed?), but we shouldn't see any more problems that I currently attribute to TIF (unless there's a bug in the new implementation).&lt;/p&gt;
</content>
 </entry>
 
 
</feed>
