7079713: javac hangs when compiling a class that references a cyclically inherited class

Wed, 31 Aug 2011 16:15:19 +0100

author
mcimadamore
date
Wed, 31 Aug 2011 16:15:19 +0100
changeset 1072
d0257833498e
parent 1071
b86277584776
child 1073
f85d980faaf8

7079713: javac hangs when compiling a class that references a cyclically inherited class
Summary: Types.membersClosure needs to handle pathological cases of cyclic inheritance
Reviewed-by: jjg, jjh

src/share/classes/com/sun/tools/javac/code/Types.java file | annotate | diff | comparison | revisions
test/tools/javac/7079713/TestCircularClassfile.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Wed Aug 31 16:11:28 2011 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Wed Aug 31 16:15:19 2011 +0100
     1.3 @@ -2119,6 +2119,8 @@
     1.4              }
     1.5          }
     1.6  
     1.7 +        List<TypeSymbol> seenTypes = List.nil();
     1.8 +
     1.9          /** members closure visitor methods **/
    1.10  
    1.11          public CompoundScope visitType(Type t, Boolean skipInterface) {
    1.12 @@ -2127,21 +2129,33 @@
    1.13  
    1.14          @Override
    1.15          public CompoundScope visitClassType(ClassType t, Boolean skipInterface) {
    1.16 -            ClassSymbol csym = (ClassSymbol)t.tsym;
    1.17 -            Entry e = _map.get(csym);
    1.18 -            if (e == null || !e.matches(skipInterface)) {
    1.19 -                CompoundScope membersClosure = new CompoundScope(csym);
    1.20 -                if (!skipInterface) {
    1.21 -                    for (Type i : interfaces(t)) {
    1.22 -                        membersClosure.addSubScope(visit(i, skipInterface));
    1.23 +            if (seenTypes.contains(t.tsym)) {
    1.24 +                //this is possible when an interface is implemented in multiple
    1.25 +                //superclasses, or when a classs hierarchy is circular - in such
    1.26 +                //cases we don't need to recurse (empty scope is returned)
    1.27 +                return new CompoundScope(t.tsym);
    1.28 +            }
    1.29 +            try {
    1.30 +                seenTypes = seenTypes.prepend(t.tsym);
    1.31 +                ClassSymbol csym = (ClassSymbol)t.tsym;
    1.32 +                Entry e = _map.get(csym);
    1.33 +                if (e == null || !e.matches(skipInterface)) {
    1.34 +                    CompoundScope membersClosure = new CompoundScope(csym);
    1.35 +                    if (!skipInterface) {
    1.36 +                        for (Type i : interfaces(t)) {
    1.37 +                            membersClosure.addSubScope(visit(i, skipInterface));
    1.38 +                        }
    1.39                      }
    1.40 +                    membersClosure.addSubScope(visit(supertype(t), skipInterface));
    1.41 +                    membersClosure.addSubScope(csym.members());
    1.42 +                    e = new Entry(skipInterface, membersClosure);
    1.43 +                    _map.put(csym, e);
    1.44                  }
    1.45 -                membersClosure.addSubScope(visit(supertype(t), skipInterface));
    1.46 -                membersClosure.addSubScope(csym.members());
    1.47 -                e = new Entry(skipInterface, membersClosure);
    1.48 -                _map.put(csym, e);
    1.49 +                return e.compoundScope;
    1.50              }
    1.51 -            return e.compoundScope;
    1.52 +            finally {
    1.53 +                seenTypes = seenTypes.tail;
    1.54 +            }
    1.55          }
    1.56  
    1.57          @Override
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/7079713/TestCircularClassfile.java	Wed Aug 31 16:15:19 2011 +0100
     2.3 @@ -0,0 +1,167 @@
     2.4 +/*
     2.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 7079713
    2.30 + * @summary javac hangs when compiling a class that references a cyclically inherited class
    2.31 + * @run main TestCircularClassfile
    2.32 + */
    2.33 +
    2.34 +import java.io.*;
    2.35 +import java.net.URI;
    2.36 +import java.util.Arrays;
    2.37 +import javax.tools.JavaCompiler;
    2.38 +import javax.tools.JavaFileObject;
    2.39 +import javax.tools.SimpleJavaFileObject;
    2.40 +import javax.tools.StandardJavaFileManager;
    2.41 +import javax.tools.StandardLocation;
    2.42 +import javax.tools.ToolProvider;
    2.43 +
    2.44 +import com.sun.source.util.JavacTask;
    2.45 +
    2.46 +public class TestCircularClassfile {
    2.47 +
    2.48 +    enum SourceKind {
    2.49 +        A_EXTENDS_B("class B {} class A extends B { void m() {} }"),
    2.50 +        B_EXTENDS_A("class A { void m() {} } class B extends A {}");
    2.51 +
    2.52 +        String sourceStr;
    2.53 +
    2.54 +        private SourceKind(String sourceStr) {
    2.55 +            this.sourceStr = sourceStr;
    2.56 +        }
    2.57 +
    2.58 +        SimpleJavaFileObject getSource() {
    2.59 +            return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) {
    2.60 +                @Override
    2.61 +                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    2.62 +                    return sourceStr;
    2.63 +                }
    2.64 +            };
    2.65 +        }
    2.66 +    }
    2.67 +
    2.68 +    enum TestKind {
    2.69 +        REPLACE_A("A.class"),
    2.70 +        REPLACE_B("B.class");
    2.71 +
    2.72 +        String targetClass;
    2.73 +
    2.74 +        private TestKind(String targetClass) {
    2.75 +            this.targetClass = targetClass;
    2.76 +        }
    2.77 +    }
    2.78 +
    2.79 +    enum ClientKind {
    2.80 +        METHOD_CALL1("A a = null; a.m();"),
    2.81 +        METHOD_CALL2("B b = null; b.m();"),
    2.82 +        CONSTR_CALL1("new A();"),
    2.83 +        CONSTR_CALL2("new B();"),
    2.84 +        ASSIGN1("A a = null; B b = a;"),
    2.85 +        ASSIGN2("B b = null; A a = b;");
    2.86 +
    2.87 +        String mainMethod;
    2.88 +
    2.89 +        private ClientKind(String mainMethod) {
    2.90 +            this.mainMethod = mainMethod;
    2.91 +        }
    2.92 +
    2.93 +        SimpleJavaFileObject getSource() {
    2.94 +            return new SimpleJavaFileObject(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE) {
    2.95 +                @Override
    2.96 +                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    2.97 +                    return "class Test { public static void main(String[] args) { #M } }"
    2.98 +                            .replace("#M", mainMethod);
    2.99 +                }
   2.100 +            };
   2.101 +        }
   2.102 +    }
   2.103 +
   2.104 +    public static void main(String... args) throws Exception {
   2.105 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   2.106 +        StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
   2.107 +        int count = 0;
   2.108 +        for (SourceKind sk1 : SourceKind.values()) {
   2.109 +            for (SourceKind sk2 : SourceKind.values()) {
   2.110 +                for (TestKind tk : TestKind.values()) {
   2.111 +                    for (ClientKind ck : ClientKind.values()) {
   2.112 +                        new TestCircularClassfile("sub_"+count++, sk1, sk2, tk, ck).check(comp, fm);
   2.113 +                    }
   2.114 +                }
   2.115 +            }
   2.116 +        }
   2.117 +    }
   2.118 +
   2.119 +    static String workDir = System.getProperty("user.dir");
   2.120 +
   2.121 +    String destPath;
   2.122 +    SourceKind sk1;
   2.123 +    SourceKind sk2;
   2.124 +    TestKind tk;
   2.125 +    ClientKind ck;
   2.126 +
   2.127 +    TestCircularClassfile(String destPath, SourceKind sk1, SourceKind sk2, TestKind tk, ClientKind ck) {
   2.128 +        this.destPath = destPath;
   2.129 +        this.sk1 = sk1;
   2.130 +        this.sk2 = sk2;
   2.131 +        this.tk = tk;
   2.132 +        this.ck = ck;
   2.133 +    }
   2.134 +
   2.135 +    void check(JavaCompiler comp, StandardJavaFileManager fm) throws Exception {
   2.136 +        //step 1: compile first source code in the test subfolder
   2.137 +        File destDir = new File(workDir, destPath); destDir.mkdir();
   2.138 +        //output dir must be set explicitly as we are sharing the fm (see bug 7026941)
   2.139 +        fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
   2.140 +        JavacTask ct = (JavacTask)comp.getTask(null, fm, null,
   2.141 +                null, null, Arrays.asList(sk1.getSource()));
   2.142 +        ct.generate();
   2.143 +
   2.144 +        //step 2: compile second source code in a temp folder
   2.145 +        File tmpDir = new File(destDir, "tmp"); tmpDir.mkdir();
   2.146 +        //output dir must be set explicitly as we are sharing the fm (see bug 7026941)
   2.147 +        fm.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(tmpDir));
   2.148 +        ct = (JavacTask)comp.getTask(null, fm, null,
   2.149 +                null, null, Arrays.asList(sk2.getSource()));
   2.150 +        ct.generate();
   2.151 +
   2.152 +        //step 3: move a classfile from the temp folder to the test subfolder
   2.153 +        File fileToMove = new File(tmpDir, tk.targetClass);
   2.154 +        File target = new File(destDir, tk.targetClass);
   2.155 +        boolean success = fileToMove.renameTo(target);
   2.156 +
   2.157 +        if (!success) {
   2.158 +            throw new AssertionError("error when moving file " + tk.targetClass);
   2.159 +        }
   2.160 +
   2.161 +        //step 4: compile the client class against the classes in the test subfolder
   2.162 +        //input/output dir must be set explicitly as we are sharing the fm (see bug 7026941)
   2.163 +        fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
   2.164 +        fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(destDir));
   2.165 +        ct = (JavacTask)comp.getTask(null, fm, null,
   2.166 +                null, null, Arrays.asList(ck.getSource()));
   2.167 +
   2.168 +        ct.generate();
   2.169 +    }
   2.170 +}

mercurial