<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx</link><description>I have been involved in several conversations recently that have revolved around the joys of runtime code patching. I am always shocked to hear people say that they are ok with this idea of code patching at runtime. Moreover – it shocks me that they think</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8505906</link><pubDate>Thu, 15 May 2008 04:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8505906</guid><dc:creator>molotov</dc:creator><description>&lt;p&gt;Nice post, Jonathan - thanks for explaining &amp;nbsp;in a clear, concise fashion the issues revolving around this.&lt;/p&gt;
</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8506402</link><pubDate>Thu, 15 May 2008 07:52:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8506402</guid><dc:creator>eranb</dc:creator><description>&lt;p&gt;Very interesting post.&lt;/p&gt;
&lt;p&gt;Can you further elaborate on the caching issues involved? Also, why do we need to corral all running threads? Do we care if a thread is now running inside the old function?&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Eran.&lt;/p&gt;
</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8506601</link><pubDate>Thu, 15 May 2008 09:01:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8506601</guid><dc:creator>theelvez</dc:creator><description>&lt;p&gt;Thanks for the comments molotov and Eran!&lt;/p&gt;
&lt;p&gt;Eran - here is one example of a cache issue with the instruction cache (from the Intel IA32 Processors Manuals):&lt;/p&gt;
&lt;p&gt;&amp;quot;For Intel486 processors, a write to an instruction in the cache will modify it in both&lt;/p&gt;
&lt;p&gt;the cache and memory, but if the instruction was prefetched before the write, the old&lt;/p&gt;
&lt;p&gt;version of the instruction could be the one executed. To prevent the old instruction&lt;/p&gt;
&lt;p&gt;from being executed, flush the instruction prefetch unit by coding a jump instruction&lt;/p&gt;
&lt;p&gt;immediately after any write that modifies an instruction.&amp;quot;&lt;/p&gt;
&lt;p&gt;There are other processor dependent issues as well, but they all revolve around the fact that entry points into the new instruction op code can exist when the caches and memory get out of sync. Thanks!&lt;/p&gt;
</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8506611</link><pubDate>Thu, 15 May 2008 09:04:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8506611</guid><dc:creator>theelvez</dc:creator><description>&lt;p&gt;Eran - also about your second point - right - we don't care if someone was already passed that patch area - we just need to manage the accesses to the area around the patch. Thanks.&lt;/p&gt;
</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8517500</link><pubDate>Sat, 17 May 2008 18:56:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8517500</guid><dc:creator>theelvez</dc:creator><description>&lt;p&gt;Daniel Pearson pointed out an error in my post that I want to share with everyone. I led you to believe (from my example disassembly at the end of the post) that the 5 bytes of nop opcode that get overwritten are at the end of the function. They are not - they are actually the 5 bytes preceding the function. It makes totally sense when you think about it - because the 2 byte jump instruction can only jmp 127/128 bytes in either direction respectively. Why is this important? Well if you had a function that was larger than 127 bytes you couldn't reach your nop patch bytes if they were at the end! :)&lt;/p&gt;
&lt;p&gt;It is a subtle but very important point. Thanks Daniel!&lt;/p&gt;
</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8537924</link><pubDate>Fri, 23 May 2008 11:50:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8537924</guid><dc:creator>Eternal Idol</dc:creator><description>&lt;p&gt;The mov edi, edi is used by Microsoft Hotpatching.&lt;/p&gt;</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8790983</link><pubDate>Wed, 30 Jul 2008 09:05:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8790983</guid><dc:creator>clark.li</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt; So what is the moral of the story here? Don’t patch code? Well that may be a little extreme, but the moral is at least to never patch multiple instructions.&lt;/p&gt;
&lt;p&gt;I think that's not enough. I saw the detour library by M$ used CopyMemory to copy op bytes, where CopyMeomory was finally spread to&lt;/p&gt;
&lt;p&gt;rep movsd;&lt;/p&gt;
&lt;p&gt;So even you just want to copy one single instruction, there is also possiblity that another thread gets CPU before all bytes are copied. (Also consider multi-core processor)&lt;/p&gt;
</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8791586</link><pubDate>Wed, 30 Jul 2008 16:38:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8791586</guid><dc:creator>Eternal Idol</dc:creator><description>&lt;p&gt;Sure but that could be easily fixed using instructions like cmpxchg8b.&lt;/p&gt;</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8903363</link><pubDate>Thu, 28 Aug 2008 16:42:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8903363</guid><dc:creator>awana81</dc:creator><description>&lt;p&gt;How does the detours library handle the caching issue?&lt;/p&gt;</description></item><item><title>re: Runtime Code Patching - Not for the Faint of Heart</title><link>http://blogs.msdn.com/itgoestoeleven/archive/2008/05/14/runtime-code-patching-not-for-the-faint-of-heart.aspx#8940274</link><pubDate>Wed, 10 Sep 2008 15:30:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8940274</guid><dc:creator>zhzhtst</dc:creator><description>&lt;p&gt;Great! Can you describe more detail about &amp;quot;Hot Patch&amp;quot; technology?&lt;/p&gt;</description></item></channel></rss>