Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4453193

Bad bytecode when instance field access qualifies static access

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 1.3.0
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description



      Name: tb29552 Date: 05/02/2001


      /*
      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


      Whenever the qualifying primary to a static field or method
      reference is itself a qualified instance field reference, javac
      1.3 is emitting invalid classfiles. The compiler is overly
      optimizing on the fact that the qualifying expression of a static
      reference needs to only be evaluated for side effects, by
      incorrectly assuming that the instance field reference has no
      side effects. According to JLS 15.12.4.1 and 15.11.1, the
      Primary must be evaluated and the value discarded before
      accessing the static field or method; and by 15.11.1, evaluation
      of the instance field can throw a NullPointerException.

      This program demonstrates the errant behavior:

      $ cat Foo.java */
      class Foo {
          static Foo s = null; // in correct code, will cause
                                      // NullPointerExceptions
          Foo i;
          static byte b;
          static void bar() {
              System.out.println("Oops");
          }
          public static void main(String[] args) {
              try {
                  s.i.bar();
              } catch(NullPointerException npe) {
                  System.out.println("OK");
              }
              try {
                  s.i.b++;
                  System.out.println("Oops");
              } catch(NullPointerException npe) {
                  System.out.println("OK");
              }
          }
      }
      /*
      $ javac Foo.java
      $ java Foo
      Oops
      Oops
      $ javap -c Foo
      Compiled from A.java
      class Foo extends java.lang.Object {
          static Foo s;
          Foo i;
          static byte b;
          Foo();
          static void bar();
          public static void main(java.lang.String[]);
      }

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

      Method void bar()
         0 getstatic #2 <Field java.io.PrintStream out>
         3 ldc #3 <String "Oops">
         5 invokevirtual #4 <Method void println(java.lang.String)>
         8 return

      Method void main(java.lang.String[])
         0 getstatic #5 <Field Foo s>
      // missing getinstance <Field Foo i>
         3 pop
         4 invokestatic #6 <Method void bar()>
         7 goto 19
        10 astore_1
        11 getstatic #2 <Field java.io.PrintStream out>
        14 ldc #8 <String "OK">
        16 invokevirtual #4 <Method void println(java.lang.String)>
        19 getstatic #5 <Field Foo s>
      // missing getinstance <Field Foo i>
        22 pop
        23 getstatic #9 <Field byte b>
        26 iconst_1
        27 iadd
        28 i2b
        29 putstatic #9 <Field byte b>
        32 getstatic #2 <Field java.io.PrintStream out>
        35 ldc #3 <String "Oops">
        37 invokevirtual #4 <Method void println(java.lang.String)>
        40 goto 52
        43 astore_1
        44 getstatic #2 <Field java.io.PrintStream out>
        47 ldc #8 <String "OK">
        49 invokevirtual #4 <Method void println(java.lang.String)>
        52 return
      Exception table:
         from to target type
           0 7 10 <Class java.lang.NullPointerException>
          19 40 43 <Class java.lang.NullPointerException>
      */

      (Review ID: 121132)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              gafter Neal Gafter
              Reporter:
              tbell Tim Bell
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: