Tue, 26 Jan 2016 21:08:18 +0000
8130304: Inference: NodeNotFoundException thrown with deep generic method call chain
Summary: Bug in Tarjan implementation is generating node ids which can overflow 32 bits
Reviewed-by: vromero
1.1 --- a/src/share/classes/com/sun/tools/javac/util/GraphUtils.java Mon Jan 25 08:48:11 2016 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/GraphUtils.java Tue Jan 26 21:08:18 2016 +0000 1.3 @@ -103,34 +103,60 @@ 1.4 * directed graph in linear time. Works on TarjanNode. 1.5 */ 1.6 public static <D, N extends TarjanNode<D>> List<? extends List<? extends N>> tarjan(Iterable<? extends N> nodes) { 1.7 - ListBuffer<List<N>> cycles = new ListBuffer<>(); 1.8 + Tarjan<D, N> tarjan = new Tarjan<>(); 1.9 + return tarjan.findSCC(nodes); 1.10 + } 1.11 + 1.12 + //where 1.13 + private static class Tarjan<D, N extends TarjanNode<D>> { 1.14 + 1.15 + /** Unique node identifier. */ 1.16 + int index = 0; 1.17 + 1.18 + /** List of SCCs found so far. */ 1.19 + ListBuffer<List<N>> sccs = new ListBuffer<>(); 1.20 + 1.21 + /** Stack of all reacheable nodes from given root. */ 1.22 ListBuffer<N> stack = new ListBuffer<>(); 1.23 - int index = 0; 1.24 - for (N node: nodes) { 1.25 - if (node.index == -1) { 1.26 - index += tarjan(node, index, stack, cycles); 1.27 + 1.28 + private List<? extends List<? extends N>> findSCC(Iterable<? extends N> nodes) { 1.29 + for (N node : nodes) { 1.30 + if (node.index == -1) { 1.31 + findSCC(node); 1.32 + } 1.33 + } 1.34 + return sccs.toList(); 1.35 + } 1.36 + 1.37 + private void findSCC(N v) { 1.38 + visitNode(v); 1.39 + for (TarjanNode<D> tn : v.getAllDependencies()) { 1.40 + @SuppressWarnings("unchecked") 1.41 + N n = (N)tn; 1.42 + if (n.index == -1) { 1.43 + //it's the first time we see this node 1.44 + findSCC(n); 1.45 + v.lowlink = Math.min(v.lowlink, n.lowlink); 1.46 + } else if (stack.contains(n)) { 1.47 + //this node is already reachable from current root 1.48 + v.lowlink = Math.min(v.lowlink, n.index); 1.49 + } 1.50 + } 1.51 + if (v.lowlink == v.index) { 1.52 + //v is the root of a SCC 1.53 + addSCC(v); 1.54 } 1.55 } 1.56 - return cycles.toList(); 1.57 - } 1.58 1.59 - private static <D, N extends TarjanNode<D>> int tarjan(N v, int index, ListBuffer<N> stack, ListBuffer<List<N>> cycles) { 1.60 - v.index = index; 1.61 - v.lowlink = index; 1.62 - index++; 1.63 - stack.prepend(v); 1.64 - v.active = true; 1.65 - for (TarjanNode<D> nd: v.getAllDependencies()) { 1.66 - @SuppressWarnings("unchecked") 1.67 - N n = (N)nd; 1.68 - if (n.index == -1) { 1.69 - tarjan(n, index, stack, cycles); 1.70 - v.lowlink = Math.min(v.lowlink, n.lowlink); 1.71 - } else if (stack.contains(n)) { 1.72 - v.lowlink = Math.min(v.lowlink, n.index); 1.73 - } 1.74 + private void visitNode(N n) { 1.75 + n.index = index; 1.76 + n.lowlink = index; 1.77 + index++; 1.78 + stack.prepend(n); 1.79 + n.active = true; 1.80 } 1.81 - if (v.lowlink == v.index) { 1.82 + 1.83 + private void addSCC(N v) { 1.84 N n; 1.85 ListBuffer<N> cycle = new ListBuffer<>(); 1.86 do { 1.87 @@ -138,9 +164,8 @@ 1.88 n.active = false; 1.89 cycle.add(n); 1.90 } while (n != v); 1.91 - cycles.add(cycle.toList()); 1.92 + sccs.add(cycle.toList()); 1.93 } 1.94 - return index; 1.95 } 1.96 1.97 /**
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/generics/inference/8130304/T8130304.java Tue Jan 26 21:08:18 2016 +0000 2.3 @@ -0,0 +1,74 @@ 2.4 +/* 2.5 + * Copyright (c) 2016, 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. Oracle designates this 2.11 + * particular file as subject to the "Classpath" exception as provided 2.12 + * by Oracle in the LICENSE file that accompanied this code. 2.13 + * 2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.17 + * version 2 for more details (a copy is included in the LICENSE file that 2.18 + * accompanied this code). 2.19 + * 2.20 + * You should have received a copy of the GNU General Public License version 2.21 + * 2 along with this work; if not, write to the Free Software Foundation, 2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.23 + * 2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.25 + * or visit www.oracle.com if you need additional information or have any 2.26 + * questions. 2.27 + */ 2.28 + 2.29 +/** 2.30 + * @test 2.31 + * @bug 8130304 2.32 + * @summary Inference: NodeNotFoundException thrown with deep generic method call chain 2.33 + * @compile T8130304.java 2.34 + */ 2.35 +class T8130304 { 2.36 + 2.37 + void test() { 2.38 + outer( 2.39 + inner(), 2.40 + inner(), 2.41 + inner(), 2.42 + inner(), 2.43 + inner(), 2.44 + inner(), 2.45 + inner(), 2.46 + inner(), 2.47 + inner(), 2.48 + inner(), 2.49 + inner(), 2.50 + inner(), 2.51 + inner(), 2.52 + inner(), 2.53 + inner(), 2.54 + inner(), 2.55 + inner(), 2.56 + inner(), 2.57 + inner(), 2.58 + inner(), 2.59 + inner(), 2.60 + inner(), 2.61 + inner(), 2.62 + inner(), 2.63 + inner(), 2.64 + inner(), 2.65 + inner(), 2.66 + inner(), 2.67 + inner(), 2.68 + inner(), 2.69 + inner()); 2.70 + } 2.71 + 2.72 + <T> void outer(T... ts) { } 2.73 + 2.74 + <T,V,W extends V> T inner() { 2.75 + return null; 2.76 + } 2.77 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/generics/inference/8130304/T8130304b.java Tue Jan 26 21:08:18 2016 +0000 3.3 @@ -0,0 +1,48 @@ 3.4 +/* 3.5 + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. Oracle designates this 3.11 + * particular file as subject to the "Classpath" exception as provided 3.12 + * by Oracle in the LICENSE file that accompanied this code. 3.13 + * 3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.17 + * version 2 for more details (a copy is included in the LICENSE file that 3.18 + * accompanied this code). 3.19 + * 3.20 + * You should have received a copy of the GNU General Public License version 3.21 + * 2 along with this work; if not, write to the Free Software Foundation, 3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.23 + * 3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.25 + * or visit www.oracle.com if you need additional information or have any 3.26 + * questions. 3.27 + */ 3.28 + 3.29 +/** 3.30 + * @test 3.31 + * @bug 8130304 3.32 + * @summary Inference: NodeNotFoundException thrown with deep generic method call chain 3.33 + * @compile T8130304b.java 3.34 + */ 3.35 +class T8130304b { 3.36 + 3.37 + void test() { 3.38 + TestZ r = null; 3.39 + Crazy<String, String> x = r.m3("X"); 3.40 + r.m1(r.m4(r.m2(x, r.m3("a")), t -> "x"), r.m3("a")); 3.41 + } 3.42 + 3.43 + interface Crazy<A, B> { } 3.44 + 3.45 + interface TestZ { 3.46 + public <A, B> Crazy<A, B> m1(Crazy<A, ? extends B>... d); 3.47 + public <A, B, C> Crazy<A, Crazy<B, C>> m2(Crazy<A, B> e, Crazy<A, C> f); 3.48 + public <A> Crazy<A, A> m3(A g); 3.49 + public <A, B, C> Crazy<A, C> m4(Crazy<A, B> h, java.util.function.Function<B, C> i); 3.50 + } 3.51 +}