[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfriends-ml 10351] Neal さんからの bytecode puzzle その後メールが 着ました



高橋(徹)です。

Neal Gafterさんから、「Joshuaさんとの歓談会」において
出された11番目のpuzzleについて後日談(?)ともいえるメールが
着ました。puzzleの問題と似たケースで、生成されるバイト
コードがVerifyErrorを生じるケースがあるそうです。

メールを英文のままですが、以下に示します。

------ ここから ------
Subject: Hello, and a comment about our dinner discussion
Date: Wed, 23 Oct 2002 10:21:47 -0700

Mr. Toru-

I would like to thank you and your reading group for a
very enjoyable dinner while I was in Japan.  I wanted
to write to you about the bytecode puzzle I tried to
show you at the table, but I think I got the details wrong.

The following test case is derived from Sun bug 4368664.

class T {
     public static void main(String[] args) {
         try {
             float[][] a = null;
             a[0] = new float[0];
         } catch (NullPointerException ex) {
             System.out.println("correct");
         }
     }
}

The question was: what is the correct bytecode to generate
for the statements inside the try block?  It turns out that
the obvious code will not verify:

$ java -version
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-
b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)
$ javac T.java
$ java T
Exception in thread "main" java.lang.VerifyError: (class: T, 
method: 
main signature: ([Ljava/lang/String;)V) Incompatible types for 
storing 
into array of arrays or objects
$ javap -c T
Compiled from T.java
class T extends java.lang.Object {
     T();
     public static void main(java.lang.String[]);
}

Method T()
    0 aload_0
    1 invokespecial #1 <Method java.lang.Object()>
    4 return

Method void main(java.lang.String[])
    0 aconst_null
    1 astore_1
    2 aload_1
    3 iconst_0
    4 iconst_0
    5 newarray float
    7 aastore
    8 goto 20
   11 astore_1
   12 getstatic #3 <Field java.io.PrintStream out>
   15 ldc #4 <String "correct">
   17 invokevirtual #5 <Method void println(java.lang.String)>
   20 return
Exception table:
    from   to  target type
      0     8    11   <Class java.lang.NullPointerException>
$

I'm fixing javac for 1.4.2 to generate code that WILL verify,
but this behavior was puzzling to us for a little while.

-Neal



---
Toru TAKAHASHI