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

<channel>
	<title>Michal Strehovský: home &#187; .NET</title>
	<atom:link href="http://migeel.sk/blog/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://migeel.sk</link>
	<description>Windows development and other random stuff</description>
	<lastBuildDate>Thu, 26 Apr 2012 03:34:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>AVG Mobilation for Windows Phone</title>
		<link>http://migeel.sk/blog/2011/09/08/avg-mobilation-for-windows-phone/</link>
		<comments>http://migeel.sk/blog/2011/09/08/avg-mobilation-for-windows-phone/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 06:38:41 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://migeel.sk/?p=235</guid>
		<description><![CDATA[A new app hit the Windows Phone marketplace today that claims to keep your device safe from malware. I immediately became interested in it because: I don&#8217;t know of any malware for the Windows Phone. Even if there was malware that misuses some kind of hole in the Windows Phone security model, this app wouldn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>A new app hit the Windows Phone marketplace today that claims to keep your device safe from malware. I immediately became interested in it because:</p>
<ol>
<li>I don&#8217;t know of any malware for the Windows Phone.</li>
<li>Even if there was malware that misuses some kind of hole in the Windows Phone security model, this app wouldn&#8217;t be able to catch it because of phone&#8217;s application model (unless the app itself uses some kind of security hole).</li>
<li>After installing it, it claimed it offers real time protection that would suggest it&#8217;s capable of running in the background.</li>
</ol>
<p>I would consider it a joke app, if it didn&#8217;t come from a well-known antivirus company. (Spoiler: It actually is a joke app, but the joke is on the antivirus company.)</p>
<h3>A look inside</h3>
<p>To satisfy my curiosity, I downloaded the XAP file of the app with <a href="http://mktwp7.codeplex.com/">Marketplace Browser and Downloader for Windows Phone 7</a> and opened it with Reflector. Surprise, surprise, this app was ported from Android (or at least that&#8217;s what *Droid namespace names suggest). Funny how the game has changed and instead of porting antivirus software from a Microsoft operating system to Linux, people started doing it the other way around.</p>
<p>The scanning UI is concentrated in the <code>DroidSecurityPOC.Scan</code> class and gets invoked in the <code>OnNavigatedTo</code> method. The <code>OnNavigatedTo</code> method is actually the first nugget:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnNavigatedTo<span style="color: #008000;">&#40;</span>NavigationEventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// uninteresting code removed</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// do the actual scanning, synchronously (we are scared of threads...)</span>
        <span style="color: #008000;">&#40;</span>Application<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span> <span style="color: #0600FF; font-weight: bold;">as</span> App<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">malwareCollection</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ScanContainingMedia</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">library</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// simulate work in the UI even though the scanning is already completed at this point</span>
        <span style="color: #008080; font-style: italic;">// people will love this</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">StartScan</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span></pre></div></div>

<p>The StartScan method looks at the number of files to scan, divides 5 seconds with that number and starts a timer to update the &#8220;currently scanned&#8221; file name in the UI. Scanning will always take 5+ seconds to complete (closer to 5 seconds if you have few files to scan) and most of the time will be spent waiting for the next timer event to fire. Because all the &#8220;scanning&#8221; already happened in the ScanContainingMedia method, long time before the UI was first updated.</p>
<h3>The scanning algorithm</h3>
<p>The <code>DroidSecurityPOC.Data.MalwareCollection</code> class is where the hilarity starts. The <code>ScanContainingMedia</code> method is where all the &#8220;scanning&#8221; happens. It&#8217;s split up in 2 parts: scanning your picture library and scanning your music library. The method doesn&#8217;t look at anything else (but that&#8217;s not much of a surprise given a marketplace application really cannot access anything else).</p>
<p>At this point, I was still giving the app a chance. Maybe it&#8217;s scanning for damaged files that can trigger known exploits in music players or picture viewers. All my hopes disappeared when I looked at the code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> ScanContainingMedia<span style="color: #008000;">&#40;</span>PictureCollection mediaFileCollectiont<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// uninteresting code removed</span>
    <span style="color: #008080; font-style: italic;">// malwareGroup contains a list of known &quot;malware&quot;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// for each picture in the library</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Picture picture <span style="color: #0600FF; font-weight: bold;">in</span> mediaFileCollectiont<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// for each known malware (because HashSet is overrated)</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> str <span style="color: #0600FF; font-weight: bold;">in</span> malwareGroup<span style="color: #008000;">.</span><span style="color: #0000FF;">MalwareGroupList</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// compare malware name with current file name (!!!!!!!)</span>
            <span style="color: #008080; font-style: italic;">// NOTE: We call ToLower() on each string to allocate a new string</span>
            <span style="color: #008080; font-style: italic;">// and never cache the result. This way the garbage collector will</span>
            <span style="color: #008080; font-style: italic;">// be busy picking up redundant trash and we can have some fun time</span>
            <span style="color: #008080; font-style: italic;">// with his daughter.</span>
            <span style="color: #008080; font-style: italic;">// Also, String.Equals(s1, s2, StringComparison.OrdinalIgnoreCase)</span>
            <span style="color: #008080; font-style: italic;">// is for pussies.</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>str<span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> picture<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToLower</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// uninteresting code - add malware to a collection of &quot;effected malware&quot;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Basically, this code couldn&#8217;t be less bothered about the file contents. It only looks at the file name and if it matches the predicate, boom, it&#8217;s jailed. No questions asked. Do not pass Go. Do not collect $200.<br />
The list of &#8220;dangerous file names&#8221; is downloaded from a web service and Rafael Rivera can <a href="http://www.withinwindows.com/2011/09/07/the-only-time-youll-see-avg-security-suite-warn-you-about-malware-on-windows-phone-7/">show you</a> the current &#8220;definition file&#8221;.</p>
<p>The code also contains an unused method that hints at a future update that will actually look at the file contents, but the method makes me really scared:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> ScanEicar<span style="color: #008000;">&#40;</span>Picture picture<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Stream image <span style="color: #008000;">=</span> picture<span style="color: #008000;">.</span><span style="color: #0000FF;">GetImage</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    image<span style="color: #008000;">.</span><span style="color: #0000FF;">Position</span> <span style="color: #008000;">=</span> 0L<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>image<span style="color: #008000;">.</span><span style="color: #0000FF;">Position</span> <span style="color: #008000;">+</span> 0x44L<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;=</span> image<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// the garbage collector still doesn't seem to be busy enough, so</span>
        <span style="color: #008080; font-style: italic;">// let's allocate an array in a tight loop</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">70</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        image<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#40;</span>buffer, <span style="color: #FF0000;">0</span>, 0x44<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// BLAM! Potentially triple the amount of allocated memory by allocating</span>
        <span style="color: #008080; font-style: italic;">// a string with the contents of the buffer. Note each character</span>
        <span style="color: #008080; font-style: italic;">// in a string takes up 2 bytes.</span>
        <span style="color: #008080; font-style: italic;">// Except Convert.ToString will actually return string &quot;System.Byte[]&quot;</span>
        <span style="color: #008080; font-style: italic;">// for each and every call. What the author probably wanted</span>
        <span style="color: #008080; font-style: italic;">// is Encoding.ASCII.GetString().</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>buffer<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            image<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Scanning fail: the call to image.Read() already moved the position</span>
        <span style="color: #008080; font-style: italic;">// by 0x44 bytes. What the author probably wanted to do is</span>
        <span style="color: #008080; font-style: italic;">// image.Position -= 0x43, but if he did that, the while loop would</span>
        <span style="color: #008080; font-style: italic;">// run for each byte in the file, allocating about 210 MB from the heap</span>
        <span style="color: #008080; font-style: italic;">// for a 1 MB file, so the algorithm is probably better off this way.</span>
        image<span style="color: #008000;">.</span><span style="color: #0000FF;">Position</span> <span style="color: #008000;">+=</span> 1L<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    image<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Everything (including the release date) hints at this being some kind of a summer intern project at AVG (if it&#8217;s not, it&#8217;s very disturbing). But AVG, c&#8217;mon. Interns do all kinds of wonky stuff. You really don&#8217;t need to ship all of it&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2011/09/08/avg-mobilation-for-windows-phone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A look at the Windows Phone JIT compiler</title>
		<link>http://migeel.sk/blog/2011/07/16/a-look-at-the-windows-phone-jitter/</link>
		<comments>http://migeel.sk/blog/2011/07/16/a-look-at-the-windows-phone-jitter/#comments</comments>
		<pubDate>Sat, 16 Jul 2011 06:07:04 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://migeel.sk/?p=198</guid>
		<description><![CDATA[When optimizing a very hot path in my code, I sometimes find it useful to see what code the compiler is generating for me. Many times I can spot things that can be easily fixed by rearranging code or adding some typecasts. But getting my hands on the CLR JIT-generated code disassembly on the Windows [...]]]></description>
			<content:encoded><![CDATA[<p>When optimizing a very hot path in my code, I sometimes find it useful to see what code the compiler is generating for me. Many times I can spot things that can be easily fixed by rearranging code or adding some typecasts.</p>
<p>But getting my hands on the CLR JIT-generated code disassembly on the Windows Phone was not easy. If you think it&#8217;s as easy as breaking into the Visual Studio debugger and pressing Ctrl-Alt-D, you&#8217;ll be disappointed:</p>
<pre>No disassembly available.</pre>
<p>Luckily for us, at least the Memory window in Visual Studio still works. Getting our hands on the JITted code will be hard, but not impossible.</p>
<p>Let&#8217;s write a method that will be easy to spot in the memory window:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> MainPage <span style="color: #008000;">:</span> PhoneApplicationPage
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// ...</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> Foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> 0xDEADBEEF<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Now add a call to this method in PhoneApplicationPage_Loaded and set up a breakpoint after the method call to make sure it&#8217;s JITted when the breakpoint is hit. Deploy your project to the emulator and break into the debugger. Now let&#8217;s find the method in memory.</p>
<p>Because we can&#8217;t use unsafe code on Windows Phone, and the System.Runtime.InteropServices.Marshal class is off limits, we have to turn our hopes to reflection. Luckily for us, the System.Reflection.MethodInfo class contains a field named MethodHandle whose Value points to some kind of internal CLR runtime structure (MethodDesc?). Even though it&#8217;s undocumented, we can probably recognize pointers in it and try our luck disassembling memory they point to.</p>
<p>Open the Immediate window in Visual Studio and type:</p>
<pre>
?typeof(MainPage).GetMethod("Foo").MethodHandle.Value
</pre>
<p>Executing the above statement in my debugging session gave me <code>0x0658c910</code>. Looking at that offset in the memory window gave me this:</p>
<pre>
0x0658C910  <strong>e8 a0 b6 03</strong> b8 c5 58 06 0e 00 00 06 01 00 86 00
</pre>
<p>Following the first pointer to <code>0x03b6a0e8</code> (remember, little-endian) will give you this:</p>
<pre>
0x03B6A0E8  5a 89 55 08 83 c4 d0 89 2c 24 8b ec b9 e8 a0 b6
0x03B6A0F8  03 33 c0 89 45 14 89 4d 0c 89 6e 14 89 45 2c 05
0x03B6A108  00 00 00 00 05 00 00 00 00 ba <strong>ef be ad de</strong> 89 55
0x03B6A118  2c 05 00 00 00 00 05 00 00 00 00 8b 55 2c 8b 6d
</pre>
<p>See the string <code>ef be ad de</code>? That has to be our code! Dump the contents of the memory window to a file and save it.</p>
<p>Now fire up your favorite ARM disassembler and load the dumped bytes at offset <code>0x03B6A0E8</code>. Does it look like trash? It is trash! That&#8217;s because the code you are actually looking at is x86, not ARM. How is that possible? The JIT compiler in the Windows Phone emulator produces x86 code. It actually makes sense, because running native code is faster than emulating ARM code. This is probably the reason why the Phone emulator needs hardware virtualization and can&#8217;t run under Hyper-V. Most of it runs as i386 code! To see the actual ARM code of the method, you have to dump it from your physical device.</p>
<p>But because we already have the x86 code dumped, let&#8217;s have a look at it:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">loc_3B6A0E8<span style="color: #339933;">:</span>                            <span style="color: #666666; font-style: italic;">; DATA XREF: seg000:03B6A0F4</span>
                <span style="color: #00007f; font-weight: bold;">pop</span>     <span style="color: #00007f;">edx</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">edx</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">esp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0FFFFFFD0h</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ebp</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">ebp</span><span style="color: #339933;">,</span> <span style="color: #00007f;">esp</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">offset</span> loc_3B6A0E8
                <span style="color: #00007f; font-weight: bold;">xor</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #00007f;">eax</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">14h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">eax</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">0Ch</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ecx</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esi</span><span style="color: #339933;">+</span><span style="color: #0000ff;">14h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ebp</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">2Ch</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">eax</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0DEADBEEFh</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">2Ch</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">edx</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">2Ch</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">ebp</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">0</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">add</span>     <span style="color: #00007f;">esp</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">34h</span>
                <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esi</span><span style="color: #339933;">+</span><span style="color: #0000ff;">14h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ebp</span>
                <span style="color: #00007f; font-weight: bold;">jmp</span>     <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span></pre></div></div>

<p>You&#8217;ll probably quickly notice 4 things:</p>
<ol>
<li>It&#8217;s a particularly chatty way of saying <code>mov edx, 0xDEADBEEF; ret</code>.</li>
<li>The method has a weird prolog and epilog.</li>
<li>The method doesn&#8217;t follow the Intel ABI.</li>
<li>The method uses a rather big <code>add eax, 0</code> instruction as a <code>nop</code>. A <code>nop</code> with side effects.
</ol>
<p>First point can partially be explained by the fact that I was running an unoptimized version (the Debug project configuration), but is closely related to the second, third and fourth point: what we are looking at is code generated by an ARM code generator that was hacked to generate x86 code! The last instruction in the listing is a dead giveaway.</p>
<p>Now let&#8217;s look at the code dumped from my actual device (disassembled with standard 32bit ARM instruction encoding):</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">                <span style="color: #00007f; font-weight: bold;">ADD</span>     R9<span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     PC<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0xC</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">MOV</span>     R2<span style="color: #339933;">,</span> #<span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R2<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R11<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R2<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x2C</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                LDR     R1<span style="color: #339933;">,</span> =<span style="color: #0000ff;">0xDEADBEEF</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R1<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x2C</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                LDR     R1<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x2C</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                LDR     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">ADD</span>     <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0x34</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R11<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                LDR     LR<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f;">BX</span>      LR</pre></div></div>

<p>This code feels much more natural than its x86 version. Now let&#8217;s look at how the code looks if we enable optimizations (the Release configuration):</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">                <span style="color: #00007f; font-weight: bold;">STR</span>     LR<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">SUB</span>     <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0x2C</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">SP</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">ADD</span>     R9<span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     PC<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0xC</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">MOV</span>     R2<span style="color: #339933;">,</span> #<span style="color: #0000ff;">0</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R2<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R11<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                ANDEQ   R0<span style="color: #339933;">,</span> R0<span style="color: #339933;">,</span> R0
                LDR     R1<span style="color: #339933;">,</span> =<span style="color: #0000ff;">0xDEADBEEF</span>
                LDR     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f; font-weight: bold;">ADD</span>     <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> <span style="color: #00007f;">SP</span><span style="color: #339933;">,</span> #<span style="color: #0000ff;">0x30</span>
                <span style="color: #00007f; font-weight: bold;">STR</span>     R9<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R11<span style="color: #339933;">,</span>#<span style="color: #0000ff;">0x14</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                LDR     LR<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span>R9<span style="color: #339933;">,</span>#<span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span>
                <span style="color: #00007f;">BX</span>      LR</pre></div></div>

<p>You&#8217;ll probably notice the code is still not very optimal. As it turns out, the JIT code generator heavily favors code generation speed against code quality. To get most of your CPU cycles, you have to be very careful about how you write your code.</p>
<p>I hope this short post will be useful to you when doing your own Windows Phone .NET code generation investigations. I plan to follow up with some notes on what optimizations you can expect from the Windows Phone CLR code generator that I gathered while optimizing <a href="http://migeel.sk/projects/mgbemu/" title="MGBEmu">my GameBoy emulator</a> to run on my phone.</p>
<p>Two more useful thing to note: when dumping the method to a file, look at the bytes preceding the method body. Every method has some kind of a header that has (apart from other stuff) 2 pointers in it: pointer to the end of the method body and a pointer to the end of method body including the literal pool. It seems like the header is different depending on whether you deploy a retail or debug configuration.</p>
<p>Many times it&#8217;s easier to just dump the whole JIT code heap instead of doing it method by method. After you find the address of your method, just scroll up in the Memory window until you hit uncommited memory region (filled with question marks). Then dump everything starting from there to the end of the heap (a big block of zeroes or question marks).</p>
<p>When it comes to choosing a disassembler, you can try GNU objdump, but if you want something painless, IDA Pro is probably your only option. Get the demo and use <a href="http://www.freemyipod.org/wiki/Working_with_binaries">this workaround</a> to open raw binaries in it.</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2011/07/16/a-look-at-the-windows-phone-jitter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singularity source code released</title>
		<link>http://migeel.sk/blog/2008/03/05/singularity-source-code-released/</link>
		<comments>http://migeel.sk/blog/2008/03/05/singularity-source-code-released/#comments</comments>
		<pubDate>Wed, 05 Mar 2008 21:51:47 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://migeel.sk/blog/2008/03/05/singularity-source-code-released/</guid>
		<description><![CDATA[Microsoft has finally made the source code of it&#8217;s research OS called &#8220;Singularity&#8221; available to general public. Singularity is a prototype operating system coded almost entirely in managed code. It&#8217;s written using Sing#, a language derived from Spec#, which itself has roots in C#. Spec# adds Eiffel-like contracts (loop invariants, preconditions, postconditions, etc.) to C#. [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft has finally made the source code of it&#8217;s research OS called &#8220;<a href="http://research.microsoft.com/os/singularity/">Singularity</a>&#8221; available to general public.</p>
<p>Singularity is a prototype operating system coded almost entirely in managed code. It&#8217;s written using Sing#, a language derived from Spec#, which itself has roots in C#. Spec# adds Eiffel-like contracts (loop invariants, preconditions, postconditions, etc.) to C#. Sing# extends Spec# with low-level constructs required for operating system development and channels required for communication within Singularity&#8217;s microkernel.</p>
<p>Okay, now what does this mean?</p>
<ul>
<li>Singularity&#8217;s code can be mechanically proved correct. This can easily reduce number of possible programming errors by orders of magnitude.</li>
<li>Singularity&#8217;s strong typing creates impenetrable memory boundaries within operating system components and processes. This allows execution of <i>everything</i>, including user processes in ring 0. No more CPU cycles wasted by context switching.</li>
<li>And much much more :)</li>
</ul>
<p>Other projects attempting to create a CLI-based operating systems are <a href="http://www.sharpos.org/">SharpOS</a> (which unfortunatelly uses the aggressive GPLv3 license) and <a href="http://gocosmos.org/">Cosmos</a> (released under a BSD license).</p>
<p>EDIT: I almost forgot the download link for Singularity; you can get it from <a href="http://www.codeplex.com/singularity">Codeplex</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2008/03/05/singularity-source-code-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET framework libraries source code to be released</title>
		<link>http://migeel.sk/blog/2007/10/04/net-framework-libraries-source-code-to-be-released/</link>
		<comments>http://migeel.sk/blog/2007/10/04/net-framework-libraries-source-code-to-be-released/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 22:51:46 +0000</pubDate>
		<dc:creator>Michal Strehovsky</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://migeel.sk/blog/2007/10/04/net-framework-libraries-source-code-to-be-released/</guid>
		<description><![CDATA[According to Scott Guthrie&#8217;s blog, Microsoft will release the source code of .NET framework libraries together with .NET 3.5 and Visual Studio 2008 release later this year. Nice. This means no more uncommented disassemblies of extended .NET classes from Lutz Roeder&#8217;s .NET Reflector (even though it&#8217;s a wonderful tool when you don&#8217;t have the source [...]]]></description>
			<content:encoded><![CDATA[<p>According to <a href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx">Scott Guthrie&#8217;s blog</a>, Microsoft will release the source code of .NET framework libraries together with .NET 3.5 and Visual Studio 2008 release later this year.</p>
<p>Nice. This means no more uncommented disassemblies of extended .NET classes from <a href="http://www.aisto.com/roeder/dotnet/">Lutz Roeder&#8217;s .NET Reflector</a> (even though it&#8217;s a wonderful tool when you don&#8217;t have the source code, having the source code is better).</p>
]]></content:encoded>
			<wfw:commentRss>http://migeel.sk/blog/2007/10/04/net-framework-libraries-source-code-to-be-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

