Contents

Blog tags RSS

How I learned about Java's lack of type erasure the hard way

3 December 2011 (programming language android java) (16 comments)

This week, I started playing around with the Android platform. I've been eyeing Yeti, an ML dialect that compiles to JVM and features structural typing. Even though I only have very limited experience in ML (mostly just reading the snippets in the Okasaki, and having helped Maya once with some F# code), it has to be better than Java, right?

After getting the hang of Yeti (integrating it into Android's ant build system, writing Hello World, etc.) I wanted to write something more substantial to evaluate the language for serious use. However, yesterday evening I started getting weird errors as soon as I started creating non-trivial closures in member functions. This is the chronicle of how I tracked down the problem with just some rudimentary Java knowledge and lots of reverse engineering.

I'll illustrate the problem with a very small repro case here; however, a large part of the puzzle was actually minimizing the code that still exhibits the bug.

The bug manifested itself by my application crashing with a java.lang.noSuchFieldError exception while running a function defined inside a class method. Here is a simplified version of the code:

module hu.erdi.Hello.yetihello;

import android.app.Activity;
import android.os.Bundle;

class HelloYeti extends Activity
  var x = 42,
  var y = 0,
  
  void onCreate(Bundle savedInstanceState)
    super#onCreate(savedInstanceState);
    f _ = (y := x);
    f (),
end;

()
      

And here is the exception I get when trying to run it on the Android emulator:

Uncaught handler: thread main exiting due to uncaught exception
java.lang.NoSuchFieldError: hu.erdi.Hello.HelloYeti.$0
        at hu.erdi.Hello.HelloYeti.f(hu/erdi/Hello/yetihello.yeti)
 	at hu.erdi.Hello.HelloYeti.onCreate(hu/erdi/Hello/yetihello.yeti:14)
        ...
      

While that field name, hu.erdi.Hello.HelloYeti.$0 does seem suspicious, disassembling the classfile created by the Yeti compiler with jclassinfo shows there is in fact such a field:

$ jclassinfo --fields bin/classes/hu/erdi/Hello/HelloYeti.class 
[FIELDS]                                                                        
yeti.lang.Num $0 
yeti.lang.Num $1       
      

Looking at the types of these two fields, a good working theory is that these correspond to the member variables x and y in the source. So $0 should be x, and thus it's probably accessed by the code compiled from the y := x statement in the function f. The error message, then, means there is no x field in whatever f tries to read it from.

My first hunch was that f probably mixes up the closure, so I looked at the disassembled version of both definition of f and its usage site:

public void onCreate(android.os.Bundle)
        ;; ...
	5 iconst_1
	6 anewarray java.lang.Object
	9 dup
	10 iconst_0
	11 aload_0
	12 checkcast hu.erdi.Hello.HelloYeti
	15 aastore
	16 astore_2
	17 aload_2
	18 aconst_null
	19 invokestatic hu.erdi.Hello.HelloYeti.f(java.lang.Object[], java.lang.Object)
        ;; ...

static java.lang.Object f(java.lang.Object[], java.lang.Object)
	0 aload_0
	1 iconst_0
	2 aaload
	3 aload_0
	4 iconst_0
	5 aaload
	6 getfield hu.erdi.Hello.HelloYeti.$0
	9 putfield hu.erdi.Hello.HelloYeti.$1
        ;; ...
      

I looked up the opcodes in the JVM spec and reconstructed what happens. The f function gets its closure in the first argument as a list of java.lang.Objects and its "real" arguments follow (a single () value in this case, represented as a Java null). Both the call site and the function definition seems to agree on this convention, so the problem is not in the two mixing up the protocol.

My next idea was to write the same thing (with packing the closure into an array) in Java and see what it gets compiled into. The code I wrote was:

package hu.erdi.Hello;

import android.app.Activity;
import android.os.Bundle;

public class Hello extends Activity
{
    int x = 42;
    int y = 0;

    public void onCreate (Bundle savedInstance)
    {
        super.onCreate (savedInstance);
        Object[] closure = new Object[1];
        closure[0] = this;

        f (closure, null);
    }

    static void f (Object[] closure, Object _)
    {
        Hello _this = (Hello)closure[0];
        _this.y = _this.x;
    }
}        
      

Let's disassemble this version of f:

static void f(java.lang.Object[], java.lang.Object)
	0 aload_0
	1 iconst_0
	2 aaload
	3 checkcast hu.erdi.Hello.Hello
	6 astore_2
	7 aload_2
	8 aload_2
	9 getfield hu.erdi.Hello.Hello.x
	12 putfield hu.erdi.Hello.Hello.y
        ;; ...        
      

I've highlighted the only difference in this particular version; in the actual code I was working with, there were more substantial differences related to how stuff is laid out on the stack. At this point, I started to suspect that maybe types are necessary for the JVM at runtime to access fields, so I wanted to test this this theory. That meant taking the compiled version of the Java code and just removing that single checkcast instruction.

Unfortunately, the only JVM assembler I could find was Jasmin which uses a somewhat different syntax than jclassinfo, so there was no roundtripping. But based on the handful of examples bundled with Jasmin, I managed to rewrite the program and then try it in the Android emulator.

This experiment immediately validated my suspicion: even though the pointer on the stack points to an object of dynamic type hu.erdi.Hello.Hello, the JVM needs the static cast before it can find its field. Why it does so I don't know: since JVM only admits single inheritance, the field offsets should be independent of the actual type.

And thus we come to the solution: I wrote a trivial patch to the Yeti compiler to always checkcast after unpacking from the closure. Using that patch, my code runs just fine on Android.


« Mod-N counters in Agda 
All entries
 Bali »


josef 2013-01-24 15:03:38

This blog post is an inspiration personally to uncover out much more related to this subject. I must confess your clarity widened my sentiments and I'll immediately take your rss feed to keep updated about any coming articles you may possibly produce. You are worthy of thanks for a job nicely done!

Mattress Reviews

coast dress 2013-02-18 06:30:41

<strong>Coast clothing</strong> with an attractive Discount Up To 70% and the most stylish <strong>coast fashion</strong> are popular with ladies who are quest for the latest fashion.As one of the leading exporter of coast dresses at this district, our <strong>coast store</strong> provides all kinds of stunning <strong>coast dresses uk</strong> with the aim to satisfy even the selective customer. From the charming <strong>coast maxi dress</strong> for bridals or for any parties to the colorful <strong>coast prom dresses</strong>, from the lovely designed <strong>coast bridesmaid dresses</strong> to the fabulous <strong>coast bridal dresses</strong>, all these coast clothes with great discount are of free shipping and no sale tax all over the world.

http://www.coastdressonlineshop.com/

coach outlet store online (http://www.ecocoachoutletonline.com/) 2013-03-08 07:39:48

http://www.ecocoachoutletonline.com/ coach outlet store online

http://www.cocoachoutletusaonline.com/ coach outlet online

http://www.acoachfactoryusaoutlet.com/ coach factory outlet

http://www.cocoachfactory.com/ coach factory

http://coacoao.peaksoverpoverty.org/2012/12/20/coach-outlet-store-online/ Coach outlet store online

http://www.itpradaoutletstore.org/

Ralph by Ralph (http://www.goralphlauren.com/) 2013-04-08 10:03:26

In Developing Hollywood, undoubtedly fashion is the fastest growing industry. As the founder of this two well-known clothing brand, Ralph Lauren for Men(Polo by Ralph Lauren) and Ralph Lauren for Women (Lauren by Ralph Lauren), Ralph Lauren, (born Ralph Lifshitz October 14, 1939 in New York City) is an American fashion designer and business executive, best known for his Polo Ralph Lauren clothing brand. He heads a highly successful, international corporation producing billions in revenues, already has $ 7.5 billion in assets, although this falls far from the top three of the world's richest, but in Hollywood,in the world of fashion, it is well-deserved that Ralph de Lauren's the richest man. Shopping Ralph by Ralph Clothing from our website will get more surprising gifts.

coach purses outlet (http://www.usitccoachpurses.com/) 2013-04-15 09:20:49

<p>&quot;I don't think men mind perspiring as much as women and you'd have a lot of women who would be wearing pale <strong>cheap coach purses</strong> in the fine weather and who are really conscious of underarm <strong>chan luu bracelet</strong>.While the collab does indeed look cool, why wait until May 1 for <strong>chan luu</strong> to drop?. True story: <strong>coach purses outlet</strong> is the most amazing polish we've ever seen.Coach outlet members is right around the <strong>chan luu necklace</strong>, and we know you all have done your taxes or filed your extensions and are ready to see what's been happening this week, right?After all, if something's only gonna be in http://www.ecocoachoutletonline.com <strong>coach outlet</strong> style a few months, why pay big bucks for designer http://www.usitccoachpurses.com? Best yet, with ilovechanluu, you're able to get a custom-tailored, bespoke <strong>coach outlet store</strong> purse at a fraction of what a one-off garment would cost. Now Supezas has just launched a <strong>chan luu sale</strong> campaign to introduce their latest collection of personalized silk dresses. I probably wear a piece of Coach outlet jewelry five times a week! </p>

red beats by dre (http://www.shopsbeatsheadphones.com) 2013-04-19 03:04:26

Obviously <strong>Studio Beats headphones by dr dre</strong>, listening to loud sounds over prolonged periods is not a good idea, because <strong>Monster beats On Ear Headphones</strong> hearing damage is cumulative and can be permanent. To make matters worse, the better the monitoring equipment (in other words, the lower its distortion levels) the quieter it seems to be, so it's easier to end up listening <strong>beats headphone pink</strong> at dangerously high levels. With <strong>green beats by dre</strong> loudspeakers, even really good ones, you tend to know when <strong>White beats by dre</strong> is getting too loud, because your internal organs start being reorganised with each <strong>White Solo Beats by Dre</strong> beat of the bass drum, the ornaments start falling off the mantelpiece, and the neighbours send for the Environmental Health officers! None of these <strong>beats by dre earphones</strong> side effects happen when using headphones, and <strong>Bluetooth Headphones sale</strong> even the blood spurting from ruptured eardrums is concealed by the headphone earpieces!

pandawill 2013-04-23 05:00:14

z The Pandawill an online store, operates various brands,a variety of a variety of large-screen smart phones and tablet PCs,and the necessities of life.Lenovo tablet pc

the best china wholesale 2013-04-23 05:29:43

Y Tablets, smart phones, online sales, the cheapest electronic wholesale market, China - pandawill.comUltraFire CREE R5

http://www.oakleywomenssunglasses.com/ (http://www.oakleywomenssunglasses.com/) 2013-04-28 08:57:54

We appreciate for sharing the good products with us. Without a pair of dark glasses, <strong>oakley womens sunglasses</strong> don't say you really understand fashion, even if you just used to play the model case, so what? In Europe and the star is not that right, no matter indoor or outdoor, <strong>Oakley Special Edition Sunglass sunglasses</strong> are always hand a big fly, want to know that the sunglasses Yu Mingxing, like in a girlfriend, in a quarterly is definitely not stingy.<br> For boys go sell of course,<strong> oakley sunglasses men</strong> the most popular, a big glasses. Romantic love in the film &quot;brokenhearted 33 days&quot;, the article plays wang, and he wears a black big glasses of restoring ancient ways is nifty and fashion. However, not everyone is suitable.<p href="http://www.oakleywomenssunglasses.com/" href="http://www.oakleywomenssunglasses.com/specials.html" href="http://www.oakleywomenssunglasses.com/products_new.html" class="STYLE1">Color too deep sunglasses will probably because of the traffic signal recognition and accidents, <strong>Oakley M Frame Sunglass </strong>poor ability of color too shallow sunglasses there is less than the role of keep out sunshine, at present only for red, green and yellow colors of sunglasses made strict rules traffic signal recognition ability, <strong>oakley mens sunglasses</strong> other color is not relevant provision, but there is a recruit, <strong>Oakley Batwolf Sunglass</strong> you might as well give it a try: when choosing sunglasses that is must try when buy, oakley sunglasses womens look around the environment change size color, color difference changes little sunglasses in general its color is more appropriate. As we all know, <strong>Oakley Asian Fit Sunglass</strong> sunglasses is the most one to one function is the uv protection.</p> cxf </br>

adsfa 2013-04-29 07:20:37

K The gradual improvement of living standards, demand for mobile phones is not limited to communications, is the pursuit of the appearance of the phone and entertainment properties, then in the end what kind of phone is able to meet all these requirements? Pandawill special launch lifestylish digital shopping guide series, holding a purse you accurate shot, to buy their favorite products. FreeLander PD10 3G

aluminium baseball bats 2013-05-02 05:57:54

Welcome to <strong>http://www.compositebaseballbatssale.com/</strong>. Do you know what are the best baseball bats in online shop? To make a good performance, you need to have a good <strong>composite baseball bats</strong>. Some people prefer choosing <strong>aluminum baseball bats</strong> because they are guarantee of a good performance, while some would go for <strong>wood baseball bats</strong> beacause of their cheap prices. But in <strong>cheap baseball bats</strong> online store, you do not need to consider that much, all the <strong>big barrel baseball bats</strong> are reasonablely priced and the online store provides free shipping!

Adidas Jeremy Scott 2013-05-13 10:38:31

Polarized glasses tend to be liable for people suffering from eyesight disorders regarding long-distance imaginative and prescient vision suchashypermetropia and presbyopia, which nonetheless want to utilize the thrills of venture athletics. There are specific restrictions associated with polarized glasses. Men and women donning polarized glasses may not be able to notice certain pain relief capabilities about snow, precisely as it prevents a bunch of Cheap Beats By Dre in the light source rays highlighting traditional hunting had. This will likely end up being dangerous within slim the rocks. Another component alongside polarized glasses is that they will help make LCDs almost invisible from several sides because LCDs are also polarized light source. Herpes simplex virus glasses were created for people who own eyesight disorders, nonetheless which do not would like to put on boring spectacles. They want any corrective instruments power like the contact lenses regarding spectacles in addition to contact lenses nonetheless were created in a modern stylish approach which also caters to the style good sense regarding the individual. Herpes simplex virus glasses tend to be purchased according to the power regarding the lens since prescribed by a good oculist. They could also magnify or minimize photos since dictated by the requirements in the wearer. Herpes simplex virus glasses can be purchased with regard to all kinds of individuals who want lesser central time-span contact lenses as well as individuals who want high solutions. Even bifocals tend to be availableGraffiti Beats by dre which make it possible for that individual to see around in addition to far away objects clearly. Currently being glasses, herpes simplex virus glasses not only better that imaginative and prescient vision, nonetheless they also secure that face from the damaging ultraviolet radiations of the solar. Polarized herpes simplex virus sunglasses secure that face from extreme glares from water floors and walls, excellent skiing conditions as well as open roads. Forget about that Jimmy Choos, the Armani fits in addition to Christian Dior fragrances. When you are looking for low-priced high end, high-end style stores that market its wares at unholy price ranges tend to be truly not worth it. Paying greatly only so you could easily get beats by dre studio any expensive name computer painted on a compact bottle regarding fragrance as well as on your night case is actually something which famous people only can afford. With regard to lower mortals for instance you and me, you can fashionable sunglasses. That cliché which states that counterfeited is the sincerest way of flattery applies with regard to glasses, very. Almost all key sunglass manufacturers value its products so higher so it produces any demand in the market for cheap flea-market replicas. These kinds of look-alike glasses look practically a similar in addition to are made of practically a similar materials, nonetheless complete they provide the same safety as well as sewing as the originals. Replicas may present short-term enjoyment Adidas Jeremy Scott in the quite cheap priceFree Reprint Articles or blog posts, nonetheless it is not advisable to go with regard to these kinds of counterfeits as they present a reduced amount of safety to the face. It is always better to covering a few a lot more pounds whenever the basic safety regarding face is concerned. Shopping bags in addition to shoes and boots are so passé. These kinds of days, a bunch of people are coordinating its glasses to the wardrobe in the day. If you're looking for a good fiscal in addition to cost effective strategy to replace your glimpse, then, get started putting together any sunglass clothing, with a bunch of styles, appears to be and colours regarding glasses. This can be also the least expensive approach to keep up with all the hottest fashion pattern even though maintaining high field of vision Pink Beats By Dre and a dazzling individuality. Set which directly into the current circumstance regarding donning cool bling, and also the result is actually spectacular in addition to eye-catching! Believe me personally, in case you glimpse with regard to an event within diamond-encrusted glasses, people are certain to sit up in addition to take notice. In addition to help make several psychological sounds! Isn’t which just what style is all about?

http://www.sadotopia.com/member/blog_post_view.php?postId=16325 http://mixnoo.com/member/blog_post_view.php?postId=166329

coach store online 2013-05-14 05:55:36

<p>For some of us, the only <strong>chan luu bracelet</strong> we have. If that sounds familiar, it will be no problem staring this <strong>Marc Jacobs Sale</strong> in the face as you down your booze.Steve Madden's popular &quot;MARC JACOBS&quot; ankle strap heels are $79.95 at stevemadden.com.Throughout the http://www.marcjacobssale.co.uk <strong>Marc Jacobs UK</strong> she wears tassel cheap coach purses and jewels with conch pearls, and at parties her flapper cheap coach purses is always completed with &quot;The <strong>Marc By Marc Jacobs Clutch</strong>&quot; - a headdress of diamonds and cultured marc jacobs handbdags, while daisy designed http://www.usitccoachpurses.net <strong>coach outlet store</strong> rings sit on her fingers.This is a http://www.marcjacobsbagsstore.com <strong>marc jacobs bags</strong> every woman raves about and I'm definitely an advocate of it. I use it to hydrate and protect my cheap coach purses as well as a hand marc by marc jacobs or to ease any bit of dry skin.<strong>chan luu</strong> may suffer accusations of nepotism from some quarters, but as we perch on a display case of the <strong>cheap coach purses</strong> store's stock, while random <strong>coach purses outlet</strong> pulsates in the background, I am afforded a close-up view of her http://www.voguecoachoutlet.com <strong>coach outlet online</strong>.With Mother's Day on Sunday, it's time to take a look back at our style-trailblazing cheap coach purses, past and present. </p>

coach store online 2013-05-14 05:56:30

For some of us, the only [url=http://www.chanluustore.org/][b]chan luu bracelet[/b][/url] we have. If that sounds familiar, it will be no problem staring this [url=http://www.marcjacobssale.co.uk/][b]Marc Jacobs Sale[/b][/url] in the face as you down your booze.Steve Madden's popular &quot;MARC JACOBS&quot; ankle strap heels are $79.95 at stevemadden.com.Throughout the [url=http://www.marcjacobssale.co.uk/]http://www.marcjacobssale.co.uk[/url] [url=http://www.marcjacobssale.co.uk/][b]Marc Jacobs UK[/b][/url] she wears tassel cheap coach purses and jewels with conch pearls, and at parties her flapper cheap coach purses is always completed with &quot;The [b][url=http://www.marcjacobsbagsstore.com/]Marc By Marc Jacobs Clutch[/url][/b]&quot; - a headdress of diamonds and cultured marc jacobs handbdags, while daisy designed [url=http://www.usitccoachpurses.net/]http://www.usitccoachpurses.net[/url] [url=http://www.usitccoachpurses.net/][b]coach outlet store[/b][/url] rings sit on her fingers.This is a [url=http://www.marcjacobsbagsstore.com/]http://www.marcjacobsbagsstore.com[/url] [url=http://www.marcjacobsbagsstore.com/][b]marc jacobs bags[/b][/url] every woman raves about and I'm definitely an advocate of it. I use it to hydrate and protect my cheap coach purses as well as a hand marc by marc jacobs or to ease any bit of dry skin.[url=http://www.chanluustore.org/][b]chan luu[/b][/url] may suffer accusations of nepotism from some quarters, but as we perch on a display case of the [url=http://www.usitccoachpurses.net/][b]cheap coach purses[/b][/url] store's stock, while random [url=http://www.usitccoachpurses.net/][b]coach purses outlet[/b][/url] pulsates in the background, I am afforded a close-up view of her [url=http://www.voguecoachoutlet.com/]http://www.voguecoachoutlet.com[/url] [url=http://www.voguecoachoutlet.com/][b]coach outlet online[/b][/url].With Mother's Day on Sunday, it's time to take a look back at our style-trailblazing cheap coach purses, past and present.

UltraFire WF-502B 2013-05-16 03:37:00

H I just wanted to comment on your blog and say I really enjoyed reading your blog here. It was very informative and I also digg the way you write! Keep it up and I'll be back soon to find out more mate.UltraFire WF-502B


New comment