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

<channel>
	<title>dow.ngra.de &#187; Featured</title>
	<atom:link href="http://dow.ngra.de/category/featured/feed/" rel="self" type="application/rss+xml" />
	<link>http://dow.ngra.de</link>
	<description>no buzzwords allowed</description>
	<lastBuildDate>Sun, 22 Jan 2012 14:52:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ClassLoaderLocal: How to avoid ClassLoader leaks on application redeploy</title>
		<link>http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/</link>
		<comments>http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 07:51:17 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[Featured]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=926</guid>
		<description><![CDATA[&#8220;OutOfMemoryError: PermGen&#8221; is a very common message to see after a few redeploys. The reason why it&#8217;s so common is that it&#8217;s amazingly easy to leak a class loader. It&#8217;s enough to hold a single outside reference to an object instantiated from a class loaded by the said class loader to prevent that class loader [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript">var dzone_url = 'http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/';</script><script type="text/javascript">var dzone_title = 'ClassLoaderLocal: How to avoid ClassLoader leaks on application redeploy';</script><script type="text/javascript">var dzone_blurb = '“OutOfMemoryError: PermGen” is a very common message to see after a few redeploys. The reason why it’s so common is that it’s amazingly easy to leak a class loader. In this post I’ll review how we solved this problem in JavaRebel, and share the solution with you.';</script><script type="text/javascript">var dzone_style = '2';</script><script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script> &#8220;OutOfMemoryError: PermGen&#8221; is a very common message to see after a few redeploys. The reason why it&#8217;s so common is that it&#8217;s amazingly easy to leak a class loader. It&#8217;s enough to hold a single outside reference to an object instantiated from a class loaded by the said class loader to prevent that class loader from being GC-d.</p>
<p>In this post I&#8217;ll review how we solved this problem in <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a>, and share the solution with you. It&#8217;s not a magical solution, but it will help alleviate some of the problems introduced in both libraries and applications in Java EE.</p>
<p>The most common way to leak is to register some kind of a callback object and never deregister it. E.g. look at the following code:<br />
[java]<br />
Core.addListener(new MyListener());<br />
[/java]<br />
If <code>Core</code> is a part of the framework/platform/container then it will hold to <code>MyListener</code> long after the application was redeployed and the class loader left hanging. </p>
<p>Let&#8217;s see if we can do anything to solve this. The <code>Core</code> implementation looks something like this:<br />
[java]<br />
public class Core {<br />
  List listeners = new ArrayList();</p>
<p>  void addListener(Listener l) {<br />
    listeners.add(l);<br />
  }</p>
<p>  void fireListeners() {<br />
    // Exercise for the reader!<br />
  }<br />
}<br />
[/java]</p>
<p>The problem is that <code>listeners</code> provides a strong reference to the <code>Listener</code> object. What if we replace it by a <a href="http://www.ibm.com/developerworks/library/j-refs/">weak one</a>?</p>
<p>[java]<br />
public class Core {<br />
  List listeners = new ArrayList();</p>
<p>  void addListener(Listener l) {<br />
    listeners.add(new WeakReference(l));<br />
  }</p>
<p>  void fireListeners() {<br />
    // Exercise for the reader!<br />
  }<br />
}<br />
[/java]</p>
<p>Unfortunately although this does solve the problem of GC-ing the class loader, it doesn&#8217;t really work. The <code>Listener</code> behind the weak reference will be GC-d at first opportunity and after that it&#8217;ll no longer receive any callbacks. To illustrate why it&#8217;s a problem the code above is basically equivalent to throwing the reference away altogether:</p>
<p>[java]<br />
public class Core {<br />
  List listeners = new ArrayList();</p>
<p>  void addListener(Listener l) {<br />
    // Listener is ignored and GC-d<br />
  }<br />
}<br />
[/java]</p>
<p>Replacing weak reference with a <em>soft</em> one doesn&#8217;t improve the situation, just delays the inevitable a bit further. Both are useful for caches, where objects can be recreated at will, but not in this case where we have an externally created object.</p>
<p>So what do we do? What we&#8217;d like to do is have the <code>Listener</code> reference to depend on the class loader somehow. Unfortunately, to the best of my knowledge, there isn&#8217;t a ready-made solution for that, and there&#8217;s no way to achieve it with any combinations of weak references without causing problems.</p>
<p>What we&#8217;d like to have is an ability to add a strong reference to the class loader: in other words have it carry a custom property:</p>
<p>[java]<br />
void addListener(Listener l) {<br />
  ClassLoader cl = l.getClass().getClassLoader();<br />
  List lls = (List) cl.getProperty(&#8220;CoreListeners&#8221;);<br />
  if (lls == null) {<br />
    lls = new ArrayList();<br />
    cl.putProperty(&#8220;CoreListeners&#8221;, lls);<br />
  }<br />
  lls.add(l);<br />
}<br />
[/java]</p>
<p>That would work, wouldn&#8217;t it? Well, not quite. We also need to save a reference to the class loaders, so that we could later go over all of them. Here the <code>WeakHashMap</code> is useful:</p>
<p>[java]<br />
Map classLoaders = new WeakHashMap();</p>
<p>void addListener(Listener l) {<br />
   //&#8230;<br />
  classLoaders.put(cl, Boolean.TRUE);<br />
}<br />
[/java]</p>
<p>There&#8217;s not <code>WeakHashSet</code> in Java, so we&#8217;re just using a boolean flag as the value.</p>
<p>So this would probably work, but unfortunately class loaders don&#8217;t have a getProperty()/putProperty() API. However, it turns out that with a bit of a hack we can simulate it, by generating a unique class per class loader to hold the properties for us. Let&#8217;s see how it&#8217;s done!</p>
<p>We start with a little boilerplate:<br />
[java]<br />
class ClassLoaderLocalMap {<br />
  private static Method defineMethod;<br />
  private static Method findLoadedClass;</p>
<p>  static {<br />
    try {<br />
      defineMethod = ClassLoader.class.getDeclaredMethod(<br />
         &#8220;defineClass&#8221;,<br />
          new Class[] {<br />
            String.class,<br />
            byte[].class,<br />
            int.class,<br />
            int.class });<br />
      defineMethod.setAccessible(true);</p>
<p>      findLoadedClass =<br />
        ClassLoader.class.getDeclaredMethod(<br />
          &#8220;findLoadedClass&#8221;,<br />
          new Class[] { String.class});<br />
      findLoadedClass.setAccessible(true);<br />
    }<br />
    catch (NoSuchMethodException e) {<br />
      throw new RuntimeException(e);<br />
    }<br />
  }<br />
}<br />
[/java]</p>
<p>This will give us access to <code>ClassLoader</code> protected methods <code>defineClass()</code> and <code>findLoadedClass()</code> later on. Now let&#8217;s setup the basic API:<br />
[java]<br />
public static void put(<br />
  ClassLoader cl,<br />
  Object  key,<br />
  Object value) {<br />
  // Synchronizing over ClassLoader is safest<br />
  synchronized (cl) {<br />
    getLocalMap(cl).put(key, value);<br />
  }<br />
}</p>
<p>public static Object get(<br />
  ClassLoader cl,<br />
  Object key) {<br />
  // Synchronizing over ClassLoader is safest<br />
  synchronized (cl) {<br />
    return getLocalMap(cl).get(key);<br />
  }<br />
}<br />
[/java]</p>
<p><code>getLocalMap()</code> method should return a map of entries associated with the class loader. How should that work?</p>
<p>Next we introduce a map from class loaders to unique holder class names. We also introduce a <code>nextHolderName()</code> method that generates unique names:<br />
[java]<br />
private static final Map classLoaderToHolderClassName =<br />
  Collections.synchronizedMap(new WeakHashMap())<br />
private static volatile int counter = 1;</p>
<p>private static String nextHolderName() {<br />
  return &#8220;ClassLoaderLocalMapHolder$$GEN$$&#8221; + counter++;<br />
}<br />
[/java]</p>
<p>Finally we can implement the <code>getLocalMap()</code> method (to save space I removed all exception handling):<br />
[java]<br />
private static Map getLocalMap(ClassLoader cl) {<br />
  String holderClassName =<br />
    (String) classLoaderToHolderClassName.get(cl);<br />
  if (holderClassName == null) {<br />
    holderClassName= nextHolderName();<br />
    classLoaderToHolderClassName.put(<br />
      cl, holderClassName);<br />
  }</p>
<p>  Class holderClass =<br />
    (Class) findLoadedClass.invoke(<br />
      cl,<br />
      new Object[] {propertiesClassName});</p>
<p>  if (holderClass  == null) {<br />
    byte[] classBytes =<br />
      buildHolderByteCode(holderClassName);</p>
<p>    holderClass = (Class) defineMethod.invoke(cl,<br />
        new Object[] {<br />
          holderClassName,<br />
          classBytes,<br />
          new Integer(0),<br />
          new Integer(classBytes.length)});<br />
  }</p>
<p>  return (Map) holderClass<br />
    .getDeclaredField(&#8220;localMap&#8221;).get(null);<br />
}<br />
[/java]</p>
<p>The last method to implement is <code>buildHolderByteCode</code>. It&#8217;s quite trivial and builds the following class renamed to the unique name:<br />
[java]<br />
public class ClassLoaderLocalMapHolder$$GEN$$X {<br />
  public static final Map localMap = new HashMap();<br />
}<br />
[/java]<br />
The code can be derived using <a href="http://asm.ow2.org/doc/tutorial.html">ASMifier</a> with just a little customization, you can look it up in the full source code.</p>
<p>Although we could now easily implement the original example it makes sense to do just a little bit extra effort and introduce a <code>ClassLoaderLocal</code>, with behavior similar to the <code>ThreadLocal</code>:<br />
[java]<br />
public class ClassLoaderLocal {<br />
  private Object key = new Object();</p>
<p>  public Object get(ClassLoader cl) {<br />
    if (!ClassLoaderProperties.containsKey(cl, key))<br />
      return null;<br />
    return ClassLoaderProperties.get(cl, key);<br />
  }</p>
<p>  public void set(ClassLoader cl, Object value) {<br />
    ClassLoaderProperties.put(cl, key, value);<br />
  }<br />
}<br />
[/java]</p>
<p>So the original example now becomes:</p>
<p>[java]<br />
Map classLoaders = new WeakHashMap();<br />
ClassLoaderLocal cll = new ClassLoaderLocal();</p>
<p>void addListener(Listener l) {<br />
  ClassLoader cl = l.getClass().getClassLoader();<br />
  List lls = (List) cll.get(cl);<br />
  if (lls == null) {<br />
    lls = new ArrayList();<br />
    cll.set(cl, lls);<br />
  }<br />
  lls.add(l);</p>
<p>  classLoaders.put(cl, Boolean.TRUE);<br />
}<br />
[/java]</p>
<p>In this code if any listener comes from a freed class loader, then it will be GC-d from both <code>Core.classLoaders</code> and <code>ClassLoaderProperties.classLoaderToHolderClassName</code>, as both are <code>WeakHashMap</code>s and there are no strong references to the class loaders. The generated <code>ClassLoaderLocalMapHolder$$GEN$$X</code> class will also be GC-d along with the class loader, so we have effectively eliminated a class loader leak without explicit cleanup calls from the user.</p>
<p>I hope this code will be useful for someone. I cannot give any guarantees whether it will work or not and it&#8217;s clearly a hack (though a solid hack).  Please use it if you actually understand what is happening.</p>
<p>If you see a bug in the code or have a good suggestion, please be sure to comment. There could be a free JavaRebel license in it for you :)</p>
<p>Full source code: <a href='http://dow.ngra.de/wp-content/uploads/2009/06/classloaderlocalmap.java'>ClassLoaderLocalMap.java</a>, <a href='http://dow.ngra.de/wp-content/uploads/2009/06/classloaderlocal.java'>ClassLoaderLocal.java</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/06/15/classloaderlocal-how-to-avoid-classloader-leaks-on-application-redeploy/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>The Ultimate Java Puzzler</title>
		<link>http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/</link>
		<comments>http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 15:01:08 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[meme]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=627</guid>
		<description><![CDATA[Why is this particular one the ultimate? Two reasons: It&#8217;s at the very core of the Java language, not some obscure piece of API. It melted my brain when I hit it. UPDATE 2: If you want to test yourself before reading the post take this test. Results are not saved (it&#8217;s a paid feature [...]]]></description>
			<content:encoded><![CDATA[<p>Why is this particular one the ultimate? Two reasons:</p>
<ul>
<li>It&#8217;s at the very core of the Java language, not some obscure piece of API.</li>
<li>It melted my brain when I hit it.</li>
</ul>
<p><strong>UPDATE 2:</strong> If you want to test yourself before reading the post <a href="http://www.classmarker.com/embedded_quizzes/?quiz=5ced4e42711fa927925c23e87b51be94">take this test</a>. Results are not saved (it&#8217;s a paid feature apparently and I just don&#8217;t care enough), but you can post them in the comments.</p>
<p>Let&#8217;s start by setting up the puzzler environment. We&#8217;ll have three classes in two packages. Classes <code>C1</code> and <code>C2</code> will be in package <code>p1</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">p1</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Class <code>C3</code> will be in a separate package <code>p2</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">p2</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006633;">C2</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We will also have the test class <code>p1.Main</code> with the following <code>main</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  C1 c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> p2.<span style="color: #006633;">C3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">m</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#91;</span><span style="color: #339933;">/</span>java<span style="color: #009900;">&#93;</span>
&nbsp;
Note that we<span style="color: #0000ff;">'re calling the method of &lt;code&gt;C1&lt;/code&gt; on an instance of &lt;code&gt;C3&lt;/code&gt;. The output for this example is &quot;3&quot; as you'</span>d expect. <span style="color: #006633;">Now</span> let<span style="color: #0000ff;">'s change the &lt;code&gt;m()&lt;/code&gt; visibility in all three classes to default:
&nbsp;
&lt;pre lang=&quot;java&quot;&gt;
public class C1 {
  /*default*/ int m() {return 1;}
}
public class C2 extends C1 {
  /*default*/ int m() {return 2;}
}
public class C3 extends p1.C2 {
  /*default*/ int m() {return 3;}
}</span></pre></div></div>

<p>The output will now be &#8220;2&#8243;! </p>
<p>Why is that? The <code>Main</code> class that invokes the method does not see the <code>m()</code> method in the <code>C3</code> class, it being in a separate package. As far as it cares the chain ends with <code>C2</code>. But as <code>C2</code> is in the same package it overrides the <code>m()</code> method in <code>C1</code>. This does not seem too intuitive, but that&#8217;s the way it is.</p>
<p>Now let&#8217;s try something different, let&#8217;s change the modifier of <code>C3.m()</code> back to <code>public</code>. What will that do?</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/*default*/</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/*default*/</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006633;">C2</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now <code>Main</code> can clearly see the <code>C3.m()</code> method. But amazingly enough output is still &#8220;2&#8243;! </p>
<p>Apparently <code>C3.m()</code> is not considered to override <code>C2.m()</code> at all. One way to think about it is overriding methods should have access to the super methods (via <code>super.m()</code>). However in this case <code>C3.m()</code> wouldn&#8217;t have access to its super method, as it it not visible to it, being in another package. Therefore <code>C3</code> is considered to be in a completely different invocation chain from <code>C1</code> and <code>C2</code>. Were we to call <code>C3.m()</code> directly from <code>Main</code> the output would actually be &#8220;3&#8243;.</p>
<p>Now let&#8217;s look at one last example. <em>Protected</em> is an interesting visibility. It behaves like <em>default</em> for members in the same package and like <em>public</em> for subclasses. What will happen if we change all of the visibilities to protected?</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006633;">C2</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>My reasoning goes like this: as <code>Main</code> is not a subclass of any classes protected should behave as default in this case and output should be &#8220;2&#8243;. However that is not the case. The crucial thing is that <code>C3.m()</code> has access to <code>super.m()</code> and thus the actual output will be &#8220;3&#8243;. </p>
<p>Personally, when I first encountered this accessibility issue I got thoroughly confused and couldn&#8217;t get it until I did all of this examples through. The intuition I got from this is that if and only if you can access <code>super.m()</code> the subclass is a part of the invocation chain.</p>
<p><strong>UPDATE:</strong> Apparently even though the whole thing is <em>obvious</em> to anyone, the intuition I came up with was wrong. A mysterious commenter know only as &#8220;C&#8221; has provided the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C1 <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/*default*/</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2 <span style="color: #000000; font-weight: bold;">extends</span> C1 <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/*default*/</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C3 <span style="color: #000000; font-weight: bold;">extends</span> p1.<span style="color: #006633;">C2</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/*default*/</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C4 <span style="color: #000000; font-weight: bold;">extends</span> p2.<span style="color: #006633;">C3</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">/*default*/</span> <span style="color: #000066; font-weight: bold;">int</span> m<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that <code>C4</code> is in the package <code>p1</code>. If we now change the <code>Main</code> code as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  C1 c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> C4<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">m</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then it will output &#8220;4&#8243;. However <code>super.m()</code> is not accessible from <code>C4</code> and putting <code>@Override</code> on the <code>C4.m()</code> method will stop the code from compiling. At the same time if we change the <code>main</code> method to:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  p2.<span style="color: #006633;">C3</span> c <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> C4<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>c.<span style="color: #006633;">m</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The output will be &#8220;3&#8243;. This means that <code>C4.m()</code> overrides <code>C2.m()</code> and <code>C1.m()</code>, but not <code>C3.m()</code>. This also makes the issue even more confusing, and the amended intuition is that <strong>a method in a subclass overrides a method in a superclass if and only if the method in the superclass is accessible from the subclass</strong>. Here <em>superclass</em> can be any ancestor, not necessarily the direct parent and the relation has to be transitive.</p>
<p>For the kicker try reading all of this out from the <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html">JVM specification</a> that selects the method to be invoked:</p>
<blockquote><p>
   <i><br />
    Let C be the class of objectref. The actual method to be invoked is selected by the following lookup procedure:</p>
<ul>
<li>If C contains a declaration for an instance method with the same name and descriptor as the resolved method, and the resolved method is accessible from C, then this is the method to be invoked, and the lookup procedure terminates. </li>
<li>Otherwise, if C has a superclass, this same lookup procedure is performed recursively using the direct superclass of C; the method to be invoked is the result of the recursive invocation of this lookup procedure.</li>
<li>Otherwise, an AbstractMethodError is raised. </li>
</ul>
<p></i>
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/02/16/the-ultimate-java-puzzler/feed/</wfw:commentRss>
		<slash:comments>55</slash:comments>
		</item>
		<item>
		<title>Correcting the Billion Dollar Mistake</title>
		<link>http://dow.ngra.de/2009/02/01/correcting-the-billion-dollar-mistake/</link>
		<comments>http://dow.ngra.de/2009/02/01/correcting-the-billion-dollar-mistake/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 20:04:14 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[Featured]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=628</guid>
		<description><![CDATA[Last week I visited Stockholm to speak at the JFokus 2009. The event was quite spectacular, but for me the most interesting part occurred on the evening before the conference. I was sitting at the speaker&#8217;s dinner with Rickard Öberg, Kirk Pepperdine, Simon Ritter and a couple of others. For some reason or other I [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I visited Stockholm to speak at the <a href="http://www.jfokus.se/jfokus/">JFokus 2009</a>. The event was quite spectacular, but for me the most interesting part occurred on the evening before the conference. I was sitting at the speaker&#8217;s dinner with Rickard Öberg, Kirk Pepperdine, Simon Ritter and a couple of others. For some reason or other I started talking to Simon about the problem that&#8217;s recently been on my mind. Perhaps it&#8217;s been eating me after legendary Tony Hoare <a href="http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake">said this</a>:</p>
<blockquote><p>I call it my billion-dollar mistake. It was the invention of the null reference in 1965. [...] This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.</p></blockquote>
<p>Now since I have a background in functional languages, I know that null references are not necessary. Null value is a member of all types, but some types don&#8217;t have or need a natural notion of absence. In fact in Haskell there is no such thing as an &#8220;absent&#8221; value generic to all types &#8212; types either have to declare it explicitly or wrap the underlying value into a <code>Maybe</code> type that has a dedicated <code>Nothing</code> value.</p>
<p>So e.g. the <code>List</code> type has a notion of an empty list encoded into the type:</p>
<pre>
// Roughly: a list is an empty list
// or a pair of a value and a list
data List a = Nil | Cons a (List a)
</pre>
<p>While the only way to encode an absent structure is to use <code>Maybe</code>:</p>
<pre>
// To build a car you need at most one engine
// and zero or more wheels :)
buildCar :: Maybe Engine -> [Wheel]  -> Car
</pre>
<p>So while I was discussing that with Simon, Kirk banged me on the head to get my attention (just joking, banging me on the head is nowhere near enough to get my attention). Apparently they were discussing more or less the same topic with Rickard, and he had a similar solution for Java. I assumed it was the widely discussed <a href="http://www.jetbrains.com/idea/documentation/howto.html"><code>@NotNull</code></a> annotation, but it was way cooler than that.</p>
<p>Basically in the beginning Rickard started by adding <code>@NotNull</code> support to <a href="http://www.qi4j.org/">Qi4J</a> (BTW an amazing piece of engineering, definitely check it out), so that they could automatically insert runtime assertions for the parameters having the <code>@NotNull</code> annotations, something like that:<br />
[java]<br />
class Test {<br />
  void transfer(<br />
    @NotNull Account from,<br />
    Account to,<br />
    double amount) {<br />
      //&#8230;<br />
  }</p>
<p>  public static void main(String[] args) {<br />
    // Succeeds<br />
    transfer(<br />
      new Account(&#8220;Bugs Bunny&#8221;), null, 1000000.0);<br />
    // Throws NullPointerException<br />
    transfer(<br />
      null, new Account(&#8220;Bugs Bunny&#8221;), 1000000.0);<br />
  }<br />
}<br />
[/java]</p>
<p>However soon after that they discovered that they were inserting <code>@NotNull</code>-s everywhere. So they reverted the notion and decided that they will generate the not null assertions for <em>all</em> parameters <em>except</em> the ones marked as <code>@Optional</code>. And this was the point when I thought to myself &#8220;Jevgeni, this is exactly what you were looking for!&#8221; So I turn to Rickard and I say &#8220;Rickard, this is exactly what I was looking for! This is amazing!&#8221; In fact this truly is amazing as it&#8217;s exactly captures the semantics of the <code>Maybe</code> type that I liked so much in the functional languages.</p>
<p>After that we go into a discussion whether or not it is possible to validate this assertion during compile-time. Opinions were mixed on this one, though I personally am convinced that it shouldn&#8217;t be too easy (your opinion on the topic is welcome, also it will make a great master&#8217;s thesis topic). Eventually I get an idea and I say &#8220;Rickard, I bet I could implement a <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> plugin that would check this at runtime in about half an hour in 50 lines of code&#8221;. And of course Rickard goes &#8220;No way!&#8221;, so I&#8217;m challenged. Next day I sit down for half an hour, then for another half an hour (there was no internet!) and voila &#8212; I have a working JavaRebel plugin (in less that 50 lines of code) that will make your methods throw an exception if you try to pass a null reference to a parameter not marked as <code>@Optional</code> (the plugin code deserves another post altogether). Of course I have to show this to Rickard (and he goes &#8220;No way!&#8221;) and we agree to somehow join forces to promote this sane approach (starting with having a single namespace for the <code>@Optional</code> annotation).</p>
<p>So what do we get? The previous example now looks like this:<br />
[java]<br />
@OptionalCheck<br />
class Test {<br />
  void transfer(<br />
    Account from,<br />
    @Optional Account to,<br />
    double amount) {<br />
      //&#8230;<br />
  }</p>
<p>  public static void main(String[] args) {<br />
    // Succeeds<br />
    transfer(<br />
      new Account(&#8220;Bugs Bunny&#8221;), null, 1000000.0);<br />
    // Throws NullPointerException<br />
    transfer(<br />
      null, new Account(&#8220;Bugs Bunny&#8221;), 1000000.0);<br />
  }<br />
}<br />
[/java]<br />
As you can understand if we make all of the code without the <code>@Optional</code> annotation to throw a <code>NullPointerException</code> for nulls we&#8217;ll break a lot of existing code. Therefore at the moment you also have to annotate the class where you want to enable such semantics with <code>@OptionalCheck</code>. </p>
<p>That&#8217;s pretty much it &#8212; you can <a href="http://repos.zeroturnaround.com/maven2/org/zeroturnaround/javarebel-optional-check-plugin/1.0/javarebel-optional-check-plugin-1.0.jar">download the plugin right away</a> and just drop it in the classpath when <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> is enabled (you&#8217;ll need a 2.0 milestone as 1.x was missing the necessary APIs). At the moment both annotations are in the <code>org.optionalalliance</code> package, but when it changes all you have to do is Organize Imports, so I won&#8217;t sweat the naming too much. Please do let us know what do you think of the approach and feel free to advocate it further :)</p>
<p>Cheers,<br />
Jevgeni Kabanov</p>
<p>P.S. The plugin along with the source is also avalable in our Maven repository:</p>
<ul>
<li>URL: <a href="http://repos.zeroturnaround.com/maven2">http://repos.zeroturnaround.com/maven2</a></li>
<li>Group id: <code>org.zeroturnaround</code></li>
<li>Artifact id: <code>javarebel-optional-check-plugin</code></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2009/02/01/correcting-the-billion-dollar-mistake/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Announcing Squill: Not Another ORM</title>
		<link>http://dow.ngra.de/2008/12/09/announcing-squill-not-another-orm/</link>
		<comments>http://dow.ngra.de/2008/12/09/announcing-squill-not-another-orm/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 13:24:22 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=501</guid>
		<description><![CDATA[Remember that post about Typesafe DSLs that had a part one and no follow up? Well, meanwhile Juhan Aasaru and yours truly were joined by Michael Hunger of jexp.de and JEQUEL and together we have created the Squill project that came right out of the ideas in the paper we wrote with Rein Raudjärv. The [...]]]></description>
			<content:encoded><![CDATA[<p><em>Remember that post about Typesafe DSLs that had a part one and no follow up? Well, meanwhile Juhan Aasaru and yours truly were joined by Michael Hunger of jexp.de and JEQUEL and together we have created the Squill project that came right out of the ideas in the paper we wrote with Rein Raudjärv. The announcement follows, enjoy!</em></p>
<p>It is with great pleasure that we announce the first release of Squill. <a href="https://squill.dev.java.net/releases/squill-with-examples-1.0-M1.zip">Download it now</a> or check out the <a href="https://squill.dev.java.net/docs/quickstart.html">quickstart guide</a>, the <a href="https://squill.dev.java.net/docs/tutorial.html">step-by-step tutorial</a> and the <a href="https://squill.dev.java.net/docs/squill-devoxx08.pdf">Devoxx presentation</a>.</p>
<p><a href="https://squill.dev.java.net">Squill</a> is a slick internal DSL for writing SQL queries in pure Java. It uses the database metadata and generics to catch as many errors as possible during compilation and is almost completely typesafe. </p>
<p>At the same time it is designed to allow everything SQL allows you to do, exactly the way SQL is meant to do it. This means that you&#8217;re encouraged to select only the data you need and no hidden queries are generated for you, leaving you in full control of the query performance. Squill supports database-specific extensions, allowing you to both use advanced features and fully tweak your queries.</p>
<p>Squill also has special support for CRUD operations and table relations, adding some sugar over vanilla SQL. A typical Squill query looks like this:<br />
[java]  ComplaintTable c = new ComplaintTable();</p>
<p>  for (Tuple2<String, Integer> tuple2:<br />
    squill<br />
      .from(c, c.customer)<br />
      .where(<br />
        gt(c.customer.isActive, 0),<br />
        notNull(c.percentSolved),<br />
        notNull(c.refoundSum))<br />
      .orderBy(desc(c.customer.id))<br />
      .selectList(<br />
        c.customer.lastName,<br />
        c.percentSolved)) {<br />
    System.out.println(<br />
      &#8220;Customer &#8221; + tuple2.v1 + &#8221; has a complaint solved &#8221; + tuple2.v2 + &#8220;%&#8221;);<br />
  }<br />
[/java]</p>
<p>Squill is a very young project and you can follow (and help) its development by joining the user or developer <a href="https://squill.dev.java.net/servlets/ProjectMailingListList">mailing lists</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/12/09/announcing-squill-not-another-orm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The Performance Cutoff</title>
		<link>http://dow.ngra.de/2008/11/10/the-performance-cutoff/</link>
		<comments>http://dow.ngra.de/2008/11/10/the-performance-cutoff/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 09:36:31 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=456</guid>
		<description><![CDATA[A few days ago I had a small epiphany on a simple yet important issue. I was trying to squeeze those last few percents of performance out of JavaRebel and it came to the point where I started optimizing individual hotspots method-by-method. One of the most common ways to improve execution time of a specific [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I had a small epiphany on a simple yet important issue. I was trying to squeeze those last few percents of performance out of <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> and it came to the point where I started optimizing individual hotspots method-by-method.</p>
<p>One of the most common ways to improve execution time of a specific method is the <em>cutoff</em>. For many complicated enough methods there are some inputs for which you can return immediately and cut off the main execution path. To explain let me use the following example:<br />
[java]<br />
Output doSomething(Input input) {<br />
  // Do something for 200 ms<br />
  return output;<br />
}<br />
[/java]<br />
This method could do anything as long as the complexity is constant, we only care how much time it takes on average. I could also have taken some algorithm (e.g. I started with matrix multiplication), but then we&#8217;d have to bring in complexity estimates and I&#8217;d like to keep it simple for now. </p>
<p>Let&#8217;s assume that for some inputs we could calculate the output in 50ms instead of 200ms. We introduce a check and the code is now:<br />
[java]<br />
Output doSomething(Input input) {<br />
  if (isSimpleInput(input)) {<br />
    // Do something for 50 ms<br />
    return output;<br />
  }</p>
<p>  // Do something for 200 ms<br />
  return output;<br />
}<br />
[/java]<br />
This is a common way to optimize the method execution time. However the question is if this actually optimized anything? It seems like a stupid question, but let&#8217;s estimate the new average execution time.</p>
<p>To do that we need to know two things. The time <i>t</i> it takes to do the <code>isSimpleInput()</code> check and the proportion <i>p</i> of method calls that are &#8220;simple&#8221;. Let&#8217;s assume that <i>t</i> is 20 ms and 10% of the calls are simple. The average execution time can then be calculated using the <a href="http://en.wikipedia.org/wiki/Expected_value">expected value</a> formula (<i>EV = p * v1 + (1 &#8211; p) * v2</i>):<br />
<i>EV = 0.1 * (20 + 50) + (1 &#8211; 0.1) * (20 + 200)<br />
    = 20 + 0.1 * 50 + 0.9 * 200<br />
    = 20 + 5 + 180 = 205</i></p>
<p>This calculation shows that with this values we have actually increased the average method execution time by 5 ms. Note that we are still winning every time the cutoff occurs (<i>20 + 50 << 200</i>), but we are losing on the average.</p>
<p>The same equation can also be applied to measuring several checks or measuring execution time that is dependent on the input, however it gets increasingly more complicated. For me the main value of this is realizing that every cutoff has an inherent cost which I will now pay in mind.</p>
<p>Before you think this is all just math, the epiphany that came to me was not to <em>add</em> a check, it was to <em>remove</em> one. I realized that a particular check I&#8217;m making is rare and expensive enough to directly influence the method execution time and lo and behold, removing it gave a 30% improvement for the benchmark I was using. </p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/11/10/the-performance-cutoff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script kiddies have awesome tools</title>
		<link>http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/</link>
		<comments>http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 16:05:08 +0000</pubDate>
		<dc:creator>Toomas Römer</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=413</guid>
		<description><![CDATA[About 10 years ago a friend of mine showed me an exploit. It was written in C and it tried to spawn a shell at a remote host. It seemed pretty cool. I did not understand the code but the mere idea that almost anybody equipped with a script like that could deface a webpage [...]]]></description>
			<content:encoded><![CDATA[<p>About 10 years ago a friend of mine showed me an exploit. It was written in C and it tried to spawn a shell at a remote host. It seemed pretty cool. I did not understand the code but the mere idea that almost anybody equipped with a script like that could deface a webpage seemed scary.</p>
<p>You did have to compile the c file, have the right devel packages installed and use the correct flags. And then you had to figure out how to use it. A 14yr old could do it.</p>
<p>Today I spent many hours grepping logs, checking the filesystem for new/changed files to figure out how an old WordPress instance was hacked and what had the hacker done there.</p>
<p>Going through the changed files I stumbled upon a php file which had some code prepended. The script had a very long line that started like this:<br />
[PHP]<br />
eval(gzinflate(base64_decode(&#8216;FJ3HcqPsFkUf&#8230;&#8230;..<br />
[/PHP]</p>
<p>Ok, lets check what does the script do. Lets assign the long string to a variable and base64 decode it and inflate the compression.<br />
[PHP]<br />
$script =  base64_decode($script);<br />
$script = gzinflate($script);<br />
echo $script;<br />
[/PHP]</p>
<p>The output was not what I expected.<br />
[PHP]<br />
eval(gzinflate(base64_decode(&#8216;FJ3HjqPcGkUf57&#8230;&#8230;..<br />
[/PHP]</p>
<p>The strings looked similar and I was already looking for an error in my code. Nope, code is correct. There <strong>is</strong> a <strong>slight</strong> change in the string. It seems it was compressed and encoded couple of times. Wow, it means I can have many evals inside evals. Fun!<br />
[PHP]<br />
do {<br />
  // extract the first 28 characters<br />
  // the eval(gzinflate(base64_decode part<br />
  $start =  substr($string, 0, 28);<br />
  // remove the first 30 chars, the eval(gzinflate(base64_decode(&#8216; part<br />
  $string = substr($string, 30);<br />
  // remove the last )));<br />
  $string = substr($string, 0, strlen($string)-4);</p>
<p>  $string =  base64_decode($string);<br />
  $string = gzinflate($string);<br />
  echo &#8220;Iteration:&#8221;.$i++.&#8221;\n&#8221;;<br />
  // iterate as long as we get a eval(gzinflat start<br />
} while ($start == &#8220;eval(gzinflate(base64_decode&#8221;);<br />
[/PHP]</p>
<p>After <b>11</b> iterations I got the code. Kind of reminded me a challenge that was posted to a mailing list and the question was what was the output of the program. That time it was more difficult: base64 encoded perl, that outputted base64 encoded bytecode, that outputted Java source file with a byte array that was byte code for the class file of the solution.</p>
<p>Anyways the 11 iterations gave me this (shot is made from my home computer).<br />
<a href="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies001.png"><img border="0" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies001-300x199.png" alt="Wow!" title="Script Kiddie Console" width="300" height="199" class="size-medium wp-image-419" /></a></p>
<p>Lets see the functionality that it has to offer:</p>
<ul>
<li>Full blown file manager</li>
<li>Quick menu for</li>
<ul>
<li>Finding all suid files</li>
<li>Finding all sgid files</li>
<li>Finding all htaccess files</li>
<li>Finding all writeable folders</li>
<li>&#8230;</li>
</ul>
<li>Interface for the UNIX tool find</li>
<li>Input field for executing commands as webserver user</li>
<li>Tools for installing a backdoor</li>
<ul>
<li>Perl/C flavoured programs that are downloaded from a Singapore server</li>
<li>Compiled/Interpreted &#8211; depending what is available</li>
</ul>
<li>Processes viewer</li>
<li>FTP brute force cracker using users from <em>/etc/passwd</em></li>
<li>System info (CPU, Memory, installed binaries, passwd file, configuration files)</li>
<li>SQL dump utility</li>
<li>Interface for executing PHP code</li>
<li>Self removal</li>
<li>Adding a password for the script</li>
<li>Fancy design!</li>
</ul>
<p>I&#8217;m just amazed. This is way too eazy. So this is how it works:</p>
<ul>
<li>Lets scan the internet for WordPress installation (automated)</li>
<li>Look for vulnerable versions (automated)</li>
<li>Exploit (in this case themes were filled with hidden links &#8211; semi automated)</li>
<li>PROFIT! (automated)</li>
</ul>
<p>How to avoid being hacked:</p>
<ul>
<li>Keep an eye on your WordPress installations</li>
<li>Subscribe to WordPress release emails/RSS and upgrade when needed</li>
<li>Monitor for changed files (for example <a href="http://www.geocities.com/fcheck2000/fcheck.html">fcheck</a>)</li>
<li>Run Apache in chroot to minimize the available software for the Apache user</li>
<li>Any other ideas?</li>
</ul>
<p>PS. The script is 2500 lines of code, supports Windows and Linux and looks great :)</p>
<p>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies001/' title='Script Kiddie Console'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies001-150x150.png" class="attachment-thumbnail" alt="Script Kiddie Console" title="Script Kiddie Console" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies003/' title='Find Wrapper'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies003-150x150.png" class="attachment-thumbnail" alt="Find Wrapper" title="Find Wrapper" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies004/' title='Backdoor installation'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies004-150x150.png" class="attachment-thumbnail" alt="Backdoor installation" title="Backdoor installation" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies005/' title='FTP Brute Force'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies005-150x150.png" class="attachment-thumbnail" alt="FTP Brute Force" title="FTP Brute Force" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies006/' title='System Information'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies006-150x150.png" class="attachment-thumbnail" alt="System Information" title="System Information" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies007/' title='Process viewer'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies007-150x150.png" class="attachment-thumbnail" alt="Process viewer" title="Process viewer" /></a>
<a href='http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/scriptkiddies008/' title='Self Removal'><img width="150" height="150" src="http://dow.ngra.de/wp-content/uploads/2008/11/scriptkiddies008-150x150.png" class="attachment-thumbnail" alt="Self Removal" title="Self Removal" /></a>
</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/11/04/script-kiddies-have-awesome-tools/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>When protected isn&#8217;t protected</title>
		<link>http://dow.ngra.de/2008/11/03/when-protected-isnt-protected/</link>
		<comments>http://dow.ngra.de/2008/11/03/when-protected-isnt-protected/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 10:41:45 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=360</guid>
		<description><![CDATA[For all those spec believers out there: what would you think if I told you that all of the JVM implementations consistently violate the spec for at least one particular instance? Our story begins on a cold and lonely evening when I discover a bug in JavaRebel in conjunction with Xerces XMLEntityManager. The problem was [...]]]></description>
			<content:encoded><![CDATA[<p>For all those spec believers out there: what would you think if I told you that all of the JVM implementations consistently violate the spec for at least one particular instance? </p>
<p>Our story begins on a cold and lonely evening when I discover a bug in <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> in conjunction with Xerces <a href="http://xerces.apache.org/xerces2-j/javadocs/xerces2/org/apache/xerces/impl/XMLEntityManager.html">XMLEntityManager</a>. The problem was with the following method:<br />
[java]<br />
public String[] getRecognizedProperties() {<br />
  return (String[])(RPROPERTIES.clone());<br />
}<br />
[/java]<br />
However when I looked at the class bytecode I saw a different picture:<br />
[java]<br />
{<br />
  GETSTATIC XMLEntityManager.RPROPERTIES : String[]<br />
  INVOKEVIRTUAL Object.clone() : Object[]<br />
  CHECKCAST String[]<br />
  ARETURN<br />
}<br />
[/java]<br />
For those of you who do not spend hours staring at the Java bytecode, it does the following:</p>
<ol>
<li>Retrieve the <code>RPROPERTIES</code> static field value (which is of type  <code>String[]</code>) and put it on the top of the stack.</li>
<li>Call <code>Object.clone()</code> using the top of the stack as target object (in our case the array). The target is popped off the stack and the result is placed on top of the stack instead of it. </li>
<li>Cast the top of the stack to a <code>String[]</code></li>
<li>Return the top of the stack as method result.</li>
</ol>
<p>Whooph. Now that is all fine except for one tiny thing. This is not legal. <code>Object.clone()</code> is a protected method and the JVM spec (see <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#18914">2.7.4</a>, <a href="http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html">INVOKEVIRTUAL</a>) allows calls to protected method only if the following is true:</p>
<ol>
<li>The accessing class is a subclass of the target class or is the same class (check, <code>XMLEntityManager</code> is a subclass of <code>Object</code>, so&#8217;s everything else).</li>
<li>The class of the target object (the actual runtime class of the object, not the target class we use in the signature) is either a subclass of the accessing class or is the same class. </li>
</ol>
<p>The second check may sound crazy, but it restricts the calls to protected methods only to the classes inheriting from the target class, as you would expect. It can also only be enforced during runtime and is the cause of the <code>IllegalAccessError</code> you may see once in a while.</p>
<p>And obviously in our case <code>String[]</code> that is the actual type of the target object is in no way a subclass of the <code>XMLEntityManager</code>. This call should have cause an error in the JVM, but for some inexplicable reason it doesn&#8217;t. Further tests show that this is only limited (AFAIK) to the <code>Object.clone()</code> call, which is <em>public</em> as far as JVM cares.</p>
<p>A little googling turned up two JVM bugs that were discussing the same issue (<a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4750641">bug 1</a>, <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4329886">bug 2</a>). Turns out the Sun <code>javac</code> compiler has been emitting wrong code at least until 1.4.2 and the JVMs have accommodated accordingly right until today. </p>
<p>Does this have any practical importance? Not for most of you, but if you handle Java bytecode in any way be prepared to handle this exception as well. To me it proves that there is more to Java than just the spec.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/11/03/when-protected-isnt-protected/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>When System.currentTimeMillis() is too slow&#8230;</title>
		<link>http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/</link>
		<comments>http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 11:09:14 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javarebel]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=347</guid>
		<description><![CDATA[At the moment I am working on reducing the performance overhead of JavaRebel. Once I got rid of all the obvious bottlenecks and optimizations I fired up the profiler and started searching and destroying ad-hoc hotspots. After some time I got to the point, when the bottleneck was in System.currentTimeMillis(). This deserves some explanation. Since [...]]]></description>
			<content:encoded><![CDATA[<p>At the moment I am working on reducing the performance overhead of <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a>. Once I got rid of all the obvious bottlenecks and optimizations I fired up the profiler and started searching and destroying ad-hoc hotspots. After some time I got to the point, when the bottleneck was in <code>System.currentTimeMillis()</code>.</p>
<p>This deserves some explanation. Since JavaRebel reloads code after the class changes, it needs to poll the actual class file for changes. The way we do it is quite lazy and the class itself causes the resource to be polled. However often there will be a lot of polling involved (more or less any method call on the class will cause a check), so we need to filter those calls. What we did is to check if some amount of time (e.g. 500 ms) has passed since the last check:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lastCheck <span style="color: #339933;">+</span> <span style="color: #cc66cc;">500</span>  <span style="color: #339933;">&gt;</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
lastCheck <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Resource check code</span></pre></div></div>

<p>After some optimization everything else got fast enough that this call started to weigh the performance down. <code>System.currentTimeMillis()</code> causes a system call and often a hardware poll, which is comparatively expensive. I needed some way to eliminate it.</p>
<p>After some thought I decided to cache the call. It may sound crazy to cache the current time, but I only needed the resolution of about half a second, so it was OK for me if the time was lagging behind about that much. What I did is put up a separate thread that would cache the current time and update it every half a sec.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TimeCacheThread <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Thread</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">volatile</span> <span style="color: #000066; font-weight: bold;">long</span> currentTimeMillis <span style="color: #339933;">=</span> 
    <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> TimeCacheThread<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    setDaemon<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">new</span> TimeCacheThread<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>    
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      currentTimeMillis <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then the check became:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lastCheck <span style="color: #339933;">+</span> <span style="color: #cc66cc;">500</span>  <span style="color: #339933;">&gt;</span> TimeCacheThread.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
lastCheck <span style="color: #339933;">=</span> TimeCacheThread.<span style="color: #006633;">currentTimeMillis</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Resource check code</span></pre></div></div>

<p>This check is amazingly fast, because instead of doing a system call we are just making a memory read.  </p>
<p>[EDIT] The original code did not have the field declared volatile with the following explanation: </p>
<blockquote><p>You may also notice the complete lack of any synchronization (or volatile keyword) on the <code>currentTimeMillis</code> static field. Since the type is primitive we can only deal with stale data here and I cared about performance more than I cared about missing a couple of checks.</p></blockquote>
<p>There is a follow-up post (<a href="http://dow.ngra.de/2008/10/28/what-do-we-really-know-about-non-blocking-concurrency-in-java/">What do we really know about non-blocking concurrency in Java?</a>) that explains some more of the reasoning behind initially omitting the volatile and then still putting it in. [/EDIT]</p>
<p>However once I got that far I decided I could do even better. The <code>currentTimeMillis</code> is of type <code>long</code> and we are still doing some arithmetic on it. In fact the only thing we want to know is if a quantum of time has passed since the last check. And we can do that by using a very simple heartbeat counter:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HeartBeatThread <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Thread</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">volatile</span> <span style="color: #000066; font-weight: bold;">int</span> counter <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>  
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> HeartBeatThread<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    setDaemon<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">new</span> HeartBeatThread<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>    
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>      
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      counter<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now the checking code becomes just:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>counter <span style="color: #339933;">==</span> HeartBeatThread.<span style="color: #006633;">counter</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
counter <span style="color: #339933;">=</span> HeartBeatThread.<span style="color: #006633;">counter</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <code>counter</code> field is now of <code>int</code> type and we only do an equals check on a DWORD, which should translate directly to 3 very fast hardware instructions (two memory reads and one conditional jump). Hopefully if you ever find yourself polling something a lot this post will come of help. As for me, it improved JavaRebel performance by 70% on some benchmarks.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/10/27/when-systemcurrenttimemillis-is-too-slow/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
		<item>
		<title>Case study: Is PHP embarrasingly slower than Java?</title>
		<link>http://dow.ngra.de/2008/08/04/optimizing-ip2c-php-implementation/</link>
		<comments>http://dow.ngra.de/2008/08/04/optimizing-ip2c-php-implementation/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 08:50:03 +0000</pubDate>
		<dc:creator>Toomas Römer</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[report]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/?p=140</guid>
		<description><![CDATA[IP2C is a small library that provides IP to country resolution. It uses the free ip-to-country database. IP2C takes the database CSV file that is about 4mb and converts it into a ~600kb binary format and provides PHP and Java frontend to query the database. The library is great, easy to convert an ip to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://firestats.cc/wiki/ip2c">IP2C</a> is a small library that provides IP to country resolution. It uses the free <a href="http://ip-to-country.webhosting.info/">ip-to-country</a> database. IP2C takes the database CSV file that is about 4mb and converts it into a ~600kb binary format and provides PHP and Java frontend to query the database.</p>
<p>The library is great, easy to convert an ip to a country and when using the <a href="http://firestats.cc/browser/trunk/easy_ip2country/flags">country flags</a> from it&#8217;s side project you could spice up your statistics with the country information. This a lot faster than using reverse DNS lookup.</p>
<p>The problem. The PHP implementation is a lot slower. Embarrassingly slower. Without any caching the Java version is able to do ~6000 queries per second. The PHP counterpart can push through ~850 queries. The implementations are the same. The stats provided by the author of the library are 8000 vs 1200. So about the same as my measurements.</p>
<p>I like PHP, I don&#8217;t use it that much anymore but I still care when I see such embarrassing numbers. I took the implementation and started profiling it. Spent the night running different tests and trying to optimize.</p>
<p>General outline of the algorithm is as follows. We take the dotted string IP and convert it to an IPv4 Internet network address (e.g. 69.55.232.153 becomes 1161291929). The DB holds sorted ranges of these addresses. A binary search will happen on these addresses and we have a country for the ip. Take a look at the <a href="http://firestats.cc/browser/trunk/ip2c/php/ip2c.php">implementation</a>.</p>
<p><div id="attachment_159" class="wp-caption alignleft" style="width: 160px"><a href="http://dow.ngra.de/wp-content/uploads/2008/08/vanilla1.png"><img src="http://dow.ngra.de/wp-content/uploads/2008/08/vanilla-150x150.png" alt=" " title="Vanilla Profiling Results" width="150" height="150" class="size-thumbnail wp-image-159" /></a><p class="wp-caption-text"> </p></div><br />
Lets see where the vanilla version of IP2C spends its time at. The results are based on 1000 iterations with <a href="http://xdebug.org/">Xdebug</a> enabled and visualized by <a href="http://kcachegrind.sourceforge.net/cgi-bin/show.cgi">KCacheGrind</a>. It processed about 210 IP addresses during this time.</p>
<p>IO part is surprisingly low. The internal <a href="http://php.net/fseek">fseek</a>, <a href="http://www.php.net/fread">fread</a> constitute to 2% of the execution time. On the other hand the user level fseek which is just a wrapper alone uses 5%. readShort and readInt take 20% of the execution time.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> readShort<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unpack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'n'</span><span style="color: #339933;">,</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_file</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> readInt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span><span style="color: #990000;">unpack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'N'</span><span style="color: #339933;">,</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_file</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> seek<span style="color: #009900;">&#40;</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">fseek</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p><div id="attachment_166" class="wp-caption alignleft" style="width: 160px"><a href="http://dow.ngra.de/wp-content/uploads/2008/08/inlinedfunctions2.png"><img src="http://dow.ngra.de/wp-content/uploads/2008/08/inlinedfunctions2-150x150.png" alt=" " title="Inlined Functions" width="150" height="150" class="size-thumbnail wp-image-166" /></a><p class="wp-caption-text"> </p></div> Functions calls are expensive. Lets eliminate them. readInt, readShort, fseek are now inlined. Recursion changed to iteration (e.g. 14 000 less function calls). Able to process 400 queries per second compared to the previous 210.</p>
<p>We see that the latest profiling results have twice the number of freads and unpacks than fseeks. It seems that fseek is used to seek out the right position, read two numbers with <a href="http://www.php.net/unpack">unpacking</a> them. The implementation confirms that. Luckily we could just read once (2 bytes more) and unpack once (2 unpackings with one invocation).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span><span style="color: #990000;">unpack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'N'</span><span style="color: #339933;">,</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_file</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$np</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'ip'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span><span style="color: #990000;">unpack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'n'</span><span style="color: #339933;">,</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_file</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$np</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'key'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this can be changed to</span>
<span style="color: #000088;">$np</span> <span style="color: #339933;">=</span><span style="color: #990000;">unpack</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Nip/nkey'</span><span style="color: #339933;">,</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">m_file</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>How does this version stack up to the Java version? Lets disable profiling and run 100 000 iterations. Vanilla version processes ~850 IPs, when functions are inlined the number is around 1400. Java version can still do 6000.</p>
<p>Lets try caching. Peeking at the Java <a href="http://firestats.cc/browser/trunk/ip2c/java/src/net/firefang/ip2c/input/RandomAccessBuffer.java">implementation</a> shows that Java caching version (whopping 141 242 IPs per second &#8211; yup 141k) uses just a byte[] array and makes lookups from there instead of seeking and reading from file. Easy, lets do the same in PHP.</p>
<p>We read everything into a string and instead of fread with access the string elements with the offset. For fseek with just set the offset. We are using 600kb more memory but can increase the throughput to ~2800.</p>
<p>As it seems I&#8217;ve just wasted a night, I just should have checked the <a href="http://shootout.alioth.debian.org/gp4/benchmark.php?test=all&#038;lang=php&#038;lang2=javaclient">Computer Language Benchmarks</a>. PHP in the sense of execution speed is uncomparable to Java.</p>
<p>The upside, we can still take the library, eliminate recursions, double unpacks and add caching. A small gain is still a gain.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/08/04/optimizing-ip2c-php-implementation/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Typesafe DSLs in Java: Part 1 &#8212; Typesafe Bytecode</title>
		<link>http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/</link>
		<comments>http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 08:25:29 +0000</pubDate>
		<dc:creator>Jevgeni Kabanov</dc:creator>
				<category><![CDATA[creative]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[bytecode]]></category>
		<category><![CDATA[dsl]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/</guid>
		<description><![CDATA[Domain Specific Languages (DSLs) have been brought to Java under the name of <a href="http://en.wikipedia.org/wiki/Fluent_interface">Fluent Interface</a>. However most of them utilize a lot of strings and untyped behavior to make the interface fluent enough. It turns out that using Java 5 and a bag of tricks we can have the compiler to check a lot more. In this post we'll check out how to write Java bytecode using ASM in a typesafe way.]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript">var dzone_url = "http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/";</script></p>
<div id="dzone_vote_widget" style="float: left; margin-right: 20px;"><script language="javascript" src="http://www.dzone.com/widgets/zoneit.js"></script></div>
<p><em>Domain Specific Languages (DSLs) have been brought to Java under the name of <a href="http://en.wikipedia.org/wiki/Fluent_interface">Fluent Interface</a>. However most of them utilize a lot of strings and untyped behavior to make the interface fluent enough. It turns out that using Java 5 and a bag of tricks we can have the compiler to check a lot more. In this post we'll check out how to write Java bytecode using ASM in a typesafe way.</em></p>
<p><span id="more-50"></span></p>
<h3>Domain Specific Languages</h3>
<p><a href="http://en.wikipedia.org/wiki/Domain-specific_programming_language">Domain specific language</a> usually refers to a small sublangauge that has very little overhead when expressing domain specific data and behaviour.  DSL is a broad term and can refer both to a fully implemented language and a specialized API that looks like a sublanguage, but still written using some general-purpose language. Such DSLs in the latter meaning have been introduced (perhaps independently?) by both the functional and dynamic languages community. Both these communities (and especially functional) took advantage of function composition and operator overloading to build combinator-based languages that look nothing like the original. The functional communities also strongly support the notion of type-safety, therefore the DSLs they create are usually statically typed.</p>
<p>In Java the DSLs are also becoming more popular. The first examples like jMock and Hibernate Criteria were coined as <a href="http://en.wikipedia.org/wiki/Fluent_interface">Fluent Interface</a>. However most of these DSLs are not typesafe and will allow wrong statements to be compiled. In this post we introduce some patterns that help making Java DSLs safer.</p>
<h3>Java bytecode engineering</h3>
<p>Java bytecode is a relatively simple stack-based language. All the code is contained in methods, class structure is pretty much preserved from source (fields and methods, both can be static, constructors and static initializers are turned into methods named "&lt;init&gt;" and "&lt;clinit&gt;" correspondingly). Inside the method we have the variables, referred by an index with 0 being "this", 1 being the first parameter and so on, local variables starting after parameters. We can load and store variables. We also have the stack, where we can push, pop and duplicate values. We have a number of basic operations on the stack (a la add and multiply) as well as method invocation. When invoking the methods parameters are gathered from the stack with last parameter being on top of the stack. Finally we have some flow control, namely conditional (and unconditional) jumps. That pretty much <a href="http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html">sums it up</a>.</p>
<p>One of the best libraries for working with Java bytecode is <a href="http://asm.objectweb.org/">ASM</a>. It provides both a lightweight fast visitor-based interface and a more comfortable tree-based object-oriented interface. Unfortunately both of them (and especially visitor-based one) are completely untyped and debugging the wrong bytecode created using them is a huge pain in the neck. 'Nuff to say that Java bytecode verifier will tell you there's a problem, but will not tell you where exactly it is.</p>
<p>Further we will examine in details the bytecode of the following example:<br />
[java]<br />
public class HelloWorld {<br />
  public static void main(String[] args) {<br />
    System.out.println("Hello, World!");<br />
  }<br />
}<br />
[/java]</p>
<p>If it looks simple to you -- it should. However the Java bytecode is a bit more complex, we have a default constructor that compiler will generate for you and we can do only one operation per line (more correctly per instruction). Next you see the <code>HelloWorld</code> example in Java bytecode (you can get a similar using "javah -c ClassName", although <a href="http://asm.objectweb.org/eclipse/index.html">Bytecode outline</a> Eclipse plugin is more comfortable).</p>
<p>[java]<br />
public class HelloWorld {<br />
  // Default constructor<br />
  public <init>()V<br />
    ALOAD 0<br />
    INVOKESPECIAL java/lang/Object.<init>()V<br />
    RETURN</p>
<p>  // System.out.println("Hello, World!");<br />
  public static main([Ljava/lang/String;)V<br />
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;<br />
    LDC "Hello, World!"<br />
    INVOKEVIRTUAL java/io/PrintStream.println(Ljava/lang/String;)V<br />
    RETURN<br />
}<br />
[/java]</p>
<p>Here's a summary of the instructions in the example:</p>
<ul>
<li>ALOAD loads the local variables to the top of the stack, index 0 is "this"</li>
<li>INVOKESPECIAL in this case invokes super method, it also consumes and Object from the stack.</li>
<li>GETSTATIC retrieves the value of the static fields and puts it on the stack (in this case a PrintStream)</li>
<li>LDC pushes a constant value to the stack</li>
<li>INVOKEVIRTUAL invokes a usual (virtual) method, consuming the parameters and returning the result</li>
<li>RETURN returns from the method</li>
</ul>
<p>If we want to write the same code from Java using ASM it would be almost the same:<br />
[java]<br />
ClassWriter cw = new ClassWriter(0);<br />
MethodVisitor mv;</p>
<p>cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "HelloWorld", null, "java/lang/Object", null);</p>
<p>{<br />
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);<br />
mv.visitCode();<br />
mv.visitVarInsn(ALOAD, 0);<br />
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");<br />
mv.visitInsn(RETURN);<br />
mv.visitMaxs(1, 1);<br />
mv.visitEnd();<br />
}<br />
{<br />
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);<br />
mv.visitCode();<br />
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");<br />
mv.visitLdcInsn("Hello, World!");<br />
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");<br />
mv.visitInsn(RETURN);<br />
mv.visitMaxs(2, 1);<br />
mv.visitEnd();<br />
}<br />
cw.visitEnd();<br />
[/java]</p>
<h3>Typesafe ASM frontend</h3>
<p>Take a look at the <code>mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");</code>. It requires two elements on the stack -- an instance of <code>PrintStream</code> on which the method is called and a <code>String</code>. What we want to do is for compiler to issue an error when the stack in fact does not contain such elements. We propose the following DSL as the basis:<br />
[java]<br />
  new ClassBuilder(cw, V1_4, ACC_PUBLIC, "HelloWorld", "java/lang/Object", null)<br />
    .beginStaticMethod(ACC_PUBLIC | ACC_STATIC, "main", void.class, String[].class)<br />
    .getStatic(System.class, "out", PrintStream.class)<br />
    .push("Hello, World!")<br />
     //Here String.class refers to the type of the first parameter<br />
    .invokeVirtualVoid(INVOKEVIRTUAL, PrintStream.class, "println", String.class)<br />
    .returnVoid()<br />
    .endMethod();<br />
[/java]<br />
Note that now all the types are written as class literals instead of strings. In Java 5, class literals are generified to <code>Class&lt;C&gt;</code>, where <code>C</code> refers to the actual underlying type. This already allows for less mistakes, since the classes are no longer written as strings. However we want more!</p>
<p>The first thing we want to do is to track the stack size. We wouldn't want to allow to <code>pop()</code> off an empty stack. And we would want to ensure that when you invoke a method all the parameters are there. To do that we introduce a simple idiom:</p>
<blockquote><p><strong>The type you return from your DSL method should allow exactly those operations that are possible with the current state.</strong></p></blockquote>
<p>What does it mean in our context? It means that when we build a method we don't have just a <code>MethodBuilder</code> class, but <code>MethodBuilderS0</code>, <code>MethodBuilderS1</code>, <code>MethodBuilderS2</code> and so on, where the <code>S*</code> refers to the stack size. And each of them has only the methods possible with the current stack size. Now the method for pop() will not even show up in autocompletion if the stack is not large enough.</p>
<p>We can apply the same trick to tracking variables, allowing to add only one variable at a time and providing methods <code>loadVar*</code>/<code>stroreVar*</code>. This way our class becomes <code>MethodBuilderS*V*</code> where <code>S</code> stands for stack size and <code>V</code> stands for variable count.</p>
<p>Of course we want more. We want to actually track the stack and variable types. To do that we introduce the following idiom:</p>
<blockquote><p><strong>The DSL type should be parametrized by all of the accumulated types as should be it's methods.</strong></p></blockquote>
<p>What does that mean? Let's return to our example and see what happens if I replace the string "Hello, World!" with an integer 11?</p>
<p><img src='http://dow.ngra.de/wp-content/uploads/2008/03/asm-error-1.PNG' alt='asm-error-1.PNG' /</p>
<p>As we can see from the screenshot the compiler inferred that the argument <code>invokeVirtualVoid()</code> will receive is an Integer (generics do not work with primitive types), whereas we have stated that it should receive a String as a parameter. Another thing we can see is that <code>invokeVirtualVoid()</code> method belongs to the <code>MethodBuilderS2V1&lt;Self, PrintStream, Integer, String[]&gt;</code> class.</p>
<p>As we know the name <code>MethodBuilderS2V1</code> refers to a class that tracks two stack slots and one variable. The types are encoded accordingly in <code>&lt;Self, PrintStream, Integer, String[]&gt;</code> -- the two stack types are <code>PrintStream, Integer</code> and the one variable type us <code>String[]</code>. <code>Self</code> is not important for now.</p>
<p>To understand how the tracking is implemented let's see the <code>push()</code> and <code>pop()</code> implementation (the rest are omitted).<br />
[java]<br />
public class MethodBuilderS2V1 <O, S0, S1, V0> {<br />
  public <S> MethodBuilderS3V1<O, S0, S1, S, V0> push(S value) {<br />
    mv.visitLdcInsn(value);<br />
    return new MethodBuilderS3V1<O, S0, S1, S, V0>(cb, mv);<br />
  }</p>
<p>  public MethodBuilderS1V1<O, S0, V0> pop() {<br />
    mv.visitInsn(POP);<br />
    return new MethodBuilderS1V1<O, S0, V0>(cb, mv);<br />
  }<br />
[/java]<br />
So as you can see the class itself is parametrized by all of the accumulated type parameters (which were one by one added by previously invoked DSL methods). We can see that <code>push()</code> returns an instance of <code>MethodBuilderS3V1</code>. That class tracks exactly one more stack slot, which is passed to it as inferred from the argument (<code>S</code>). <code>pop()</code> on the other hand just discards one stack slot and returns an instance of <code>MethodBuilderS1V1</code>.</p>
<p>So what does the <code>invokeVirtualVoid()</code> look like? First of all, it needs to consume two stack variables, therefore we need at least <code>MethodBuilderS2V*</code> class to call it. Therefore all classes with less stack variables will not have this method. Secondly we need to check that the types in the stack are fitting, an must also allow for some leniency due to subtyping:<br />
[java]<br />
public class MethodBuilderS2V1<O, S0, S1, V0> {<br />
    public MethodBuilderS0V1<O, V0> invokeVirtualVoid(int kind, Class< ? super S0> owner, String name, Class< ? super S1> parameter1) {<br />
    mv.visitMethodInsn(<br />
        kind,<br />
        owner.getName().replace('.', '/'),<br />
        name,<br />
        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {Type.getType(parameter1)}));</p>
<p>    return new MethodBuilderS0V1<O, V0>(cb, mv);<br />
  }<br />
}<br />
[/java]<br />
As you can see it indeed consumes two stack values returning <code>MethodBuilderS0V1</code>. If our method would also return a result we would need to take the result type as well, and return a class with stack depth only one less. The expression "? super S0" means that we require the actual parameter type to be a superclass of the stack type.</p>
<h4>Problem 1: The world isn't perfect</h4>
<p>One of the problems with the DSL we proposed here is that it assumes all the types exist. However it is very often the case that some of the types (most prominently the class currently being created) do not. You can somewhat alleviate the problem by introducing a special placeholder <code>Self</code> type and use it instead of the current class name, but it won't solve the problem of other classes still awaiting construction.</p>
<p>A different (but connected) problem is that you are not always constructing the full method, instead you could be just creating a prelude for a particular method or replacing one instruction with a series of your own. To solve we add this idiom:</p>
<blockquote><p><strong>We should allow escaping from the rigid typesafe world by having unsafe operations, which issue compiler warnings</strong></p></blockquote>
<p>This means that instead of missing <code>pop()</code> from a type with no stack slots we should just deprecate it or otherwise issue a warning. This also means that we should have invoke* methods that take strings as parameter types, similarly deprecated.</p>
<p>However, since we know it's not a perfect world we'd like to at least protect ourselves a bit better. Therefore we add another idiom:</p>
<blockquote><p><strong>Allow users to document their assumptions.</strong></p></blockquote>
<p>This means that when we need to write just a fragment of bytecode we want to document what stack values and variables will it need. For that we introduce methods <code>assumePush()</code>/<code>assumePop()</code> and <code>assumeVar*()</code>, which do not push any values, but just add the corresponding type variables:</p>
<p>[java]<br />
public class MethodBuilderS2V1 <O, S0, S1, V0> {<br />
  public <S> MethodBuilderS3V1<O, S0, S1, S, V0> assumePush(Class<S> type) {<br />
    return new MethodBuilderS3V1<O, S0, S1, S, V0>(cb, mv);<br />
  }</p>
<p>  public MethodBuilderS1V1<O, S0, V0> assumePop() {<br />
    return new MethodBuilderS1V1<O, S0, V0>(cb, mv);<br />
  }</p>
<p>  public <V> MethodBuilderS2V2<O, S0, S1, V0, V> assumeVar1(Class<V> type) {<br />
    return new MethodBuilderS2V2<O, S0, S1, V0, V>(cb, mv);<br />
  }<br />
[/java]</p>
<p>Using them we can document our expectations and let the compiler validate them. The following is an example of how we can use the assumptions to document that we expect <code>PrintStream</code> to be on stack and <code>String[]</code> to be the variable 0.</p>
<p>[java]<br />
  private static <O> void genSayHello(MethodBuilderS0V0<O> mb) {<br />
    mb.assumeVar0(String[].class)<br />
    .assumePush(PrintStream.class)<br />
    .loadVar0(String[].class)<br />
    .push(0)<br />
    .arrayLoad(String[].class, Integer.class, String.class)<br />
    .invokeVirtualVoid(INVOKEVIRTUAL, PrintStream.class, "println", String.class);<br />
  }<br />
[/java]</p>
<h4>Problem 2: Control flow and reuse</h4>
<p>The next problem is how to mix the DSL with the general-purpose control flow and method calls. The problem here is that (although it is hidden from the user) we return a different type every time. Therefore if we add a conditional operation, we would need to save the type in a variable, which would be clumsy, since it contains a lot of inferred types. It also wouldn't cope with changes, since the inferred types would change every time. To solve this we invoke the next idiom:</p>
<blockquote><p><strong>Hide control flow in closures.</strong></p></blockquote>
<p>In fact we introduce two types of closures -- the strongly typed and weakly typed. The weakly typed one loses all accumulated type information and is meant first of all for reusable methods, since they need to take one based type as an argument:</p>
<p>[java]<br />
public interface Closure {<br />
  public <O> void apply(MethodBuilderS0V0<O> mb);<br />
}<br />
[/java]</p>
<p>The strongly typed closures are generated with the class and look like that:</p>
<p>[java]<br />
public class MethodBuilderS2V1 <O, S0, S1, V0> {<br />
  public MethodBuilderS2V1<O, S0, S1, V0> closure(ClosureS2V1 closure) {<br />
    closure.apply(this);<br />
    return this;<br />
  }</p>
<p>  public MethodBuilderS2V1<O, S0, S1, V0> closure(Closure closure) {<br />
    closure.apply(new MethodBuilderS0V0<O>(cb, mv));<br />
    return this;<br />
  }</p>
<p>  interface ClosureS2V1 {<br />
    <O, S0, S1, V0> void apply(MethodBuilderS2V1<O, S0, S1, V0> mb);<br />
  }<br />
}<br />
[/java]</p>
<p>We illustrating using the weakly typed closures to call the method <code>genSayHello()</code> introduced previously:<br />
[java]<br />
    .beginStaticMethod(ACC_PUBLIC | ACC_STATIC, "main", void.class, String[].class)<br />
    .getStatic(System.class, "out", PrintStream.class)<br />
    .closure(new Closure() {<br />
      public <O> void apply(MethodBuilderS0V0<O> mb) {<br />
        genSayHello(mb);<br />
      }<br />
    })</p>
<p>    .returnVoid()<br />
    .endMethod();<br />
[/java]</p>
<p>Of course with the introduction of closures in Java 7 this idiom would be much shorter</p>
<h4>Problem 3: Productivity and performance</h4>
<p>One of the problems with having classes for each stack depth and variable size is that we have a lot of them ((max stack depth + 1) times (max variable count + 1)). And we need to have a reasonably big number as maximum before deprecating the methods and losing track of some stack slots and variables. Writing them by hand would be exteremely unefficient,  therefore we propose to use a generator. </p>
<p>The other problem is that having such a great number of classes and instances at runtime could cause performance problems. The way we could solve it is providing also a source-compatible unsafe library that would provide exactly the same interface, but always return "this" and track no types to allow good perfromance. It cannot be made binary compatible, since every method has a different signature (even though inference hides it from the user), but since those signatures are not present in source it would be a matter of recompiling the application for production, after insuring in development that the code is typesafe.</p>
<h4>Problem 4: Primitive types and operations</h4>
<p>The final problem is one of the most annoying and the one we tackled the least. Since generic types cannot be parametrized by primitive types we have to track boxed types instead of primitives (Integer v/s int). This means that we cannot distinguish among them and they can still cause a runtime error. What's worse we need to somehow indicate to a method that the parameter is a primitive type, but still need to pass to it a boxed one to satisfy the inference. There are several possibilities here, but all of them are relatively ugly. </p>
<p>A similar problem, which is yet to find an elegant solution is that <code>double</code> and <code>long</code> take two stack slots, whereas everything else takes one. And of course some coercions will not work with the current prototype.</p>
<p>If you have any ideas, please let us know.</p>
<h4>But is it real?</h4>
<p>The code is available  <a href="http://code.google.com/p/typed-asm/">in a Google Code repository</a>. The code is just a prototype to show off the typesafe DSLs for our upcoming paper and is yet nowhere ready to even consider its usage. </p>
<p>The generator is written in PHP and driven by a BASH script. It may sound crazy, but it allowed me to prototype the code very quickly.</p>
<p>UPDATE: I also uploaded a <a href="http://typed-asm.googlecode.com/files/TypedAsm.zip">downloadable distribution</a> with 10x20 classes pregenerated for your pleasure. Check out <code>src/Test.java</code> for the example and <code>gen/*</code> for the generated classes. The <code>asm-3.0.jar</code> must be in the classpath.</p>
]]></content:encoded>
			<wfw:commentRss>http://dow.ngra.de/2008/03/24/typesafe-dsls-in-java-part-1-typesafe-bytecode/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

