Fri, 17 Apr 2015 08:55:59 -0600
8075520: Varargs access check mishandles capture variables
8077786: Check varargs access against inferred signature
Reviewed-by: vromero
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Wed Apr 15 14:45:24 2015 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Fri Apr 17 08:55:59 2015 -0600 1.3 @@ -836,20 +836,19 @@ 1.4 List<Type> formals, 1.5 Warner warn) { 1.6 super.argumentsAcceptable(env, deferredAttrContext, argtypes, formals, warn); 1.7 - //should we expand formals? 1.8 + // should we check varargs element type accessibility? 1.9 if (deferredAttrContext.phase.isVarargsRequired()) { 1.10 - Type typeToCheck = null; 1.11 - if (!checkVarargsAccessAfterResolution) { 1.12 - typeToCheck = types.elemtype(formals.last()); 1.13 - } else if (deferredAttrContext.mode == AttrMode.CHECK) { 1.14 - typeToCheck = types.erasure(types.elemtype(formals.last())); 1.15 - } 1.16 - if (typeToCheck != null) { 1.17 - varargsAccessible(env, typeToCheck, deferredAttrContext.inferenceContext); 1.18 + if (deferredAttrContext.mode == AttrMode.CHECK || !checkVarargsAccessAfterResolution) { 1.19 + varargsAccessible(env, types.elemtype(formals.last()), deferredAttrContext.inferenceContext); 1.20 } 1.21 } 1.22 } 1.23 1.24 + /** 1.25 + * Test that the runtime array element type corresponding to 't' is accessible. 't' should be the 1.26 + * varargs element type of either the method invocation type signature (after inference completes) 1.27 + * or the method declaration signature (before inference completes). 1.28 + */ 1.29 private void varargsAccessible(final Env<AttrContext> env, final Type t, final InferenceContext inferenceContext) { 1.30 if (inferenceContext.free(t)) { 1.31 inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() { 1.32 @@ -859,7 +858,7 @@ 1.33 } 1.34 }); 1.35 } else { 1.36 - if (!isAccessible(env, t)) { 1.37 + if (!isAccessible(env, types.erasure(t))) { 1.38 Symbol location = env.enclClass.sym; 1.39 reportMC(env.tree, MethodCheckDiag.INACCESSIBLE_VARARGS, inferenceContext, t, Kinds.kindName(location), location); 1.40 }
2.1 --- a/test/tools/javac/varargs/T8049075/VarargsAndWildcardParameterizedTypeTest.java Wed Apr 15 14:45:24 2015 -0700 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,40 +0,0 @@ 2.4 -/* 2.5 - * Copyright (c) 2014, 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 8049075 2.30 - * @summary javac, wildcards and generic vararg method invocation not accepted 2.31 - * @compile VarargsAndWildcardParameterizedTypeTest.java 2.32 - */ 2.33 - 2.34 -class VarargsAndWildcardParameterizedTypeTest { 2.35 - interface I<T> { 2.36 - String m(T... t); 2.37 - } 2.38 - 2.39 - void m() { 2.40 - I<? super Integer> i = null; 2.41 - i.m(Integer.valueOf(1), Integer.valueOf(1)); 2.42 - } 2.43 -}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/varargs/access/OtherPackage.java Fri Apr 17 08:55:59 2015 -0600 3.3 @@ -0,0 +1,36 @@ 3.4 +/* 3.5 + * Copyright (c) 2015, 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. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * Auxiliary file for VarargsInferredPrivateType 3.29 + */ 3.30 + 3.31 +package otherpackage; 3.32 + 3.33 +public class OtherPackage { 3.34 + public static Private getPrivate() { 3.35 + return new Private(); 3.36 + } 3.37 + 3.38 + private static class Private {} 3.39 +} 3.40 \ No newline at end of file
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest.java Fri Apr 17 08:55:59 2015 -0600 4.3 @@ -0,0 +1,42 @@ 4.4 +/* 4.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 8049075 4.30 + * @summary javac, wildcards and generic vararg method invocation not accepted 4.31 + * @compile VarargsAndWildcardParameterizedTypeTest.java 4.32 + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest.java 4.33 + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest.java 4.34 + */ 4.35 + 4.36 +class VarargsAndWildcardParameterizedTypeTest { 4.37 + interface I<T> { 4.38 + String m(T... t); 4.39 + } 4.40 + 4.41 + void m() { 4.42 + I<? super Integer> i = null; 4.43 + i.m(Integer.valueOf(1), Integer.valueOf(1)); 4.44 + } 4.45 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java Fri Apr 17 08:55:59 2015 -0600 5.3 @@ -0,0 +1,45 @@ 5.4 +/* 5.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +/* 5.28 + * @test 5.29 + * @bug 8075520 5.30 + * @summary Varargs access check mishandles capture variables 5.31 + * @compile VarargsAndWildcardParameterizedTypeTest2.java 5.32 + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest2.java 5.33 + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest2.java 5.34 + */ 5.35 + 5.36 +class VarargsAndWildcardParameterizedTypeTest2 { 5.37 + interface I { 5.38 + <T> void m(T... t); 5.39 + } 5.40 + 5.41 + interface Box<T> { 5.42 + T get(); 5.43 + } 5.44 + 5.45 + void m(I i, Box<? extends Number> b) { 5.46 + i.m(b.get()); 5.47 + } 5.48 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java Fri Apr 17 08:55:59 2015 -0600 6.3 @@ -0,0 +1,43 @@ 6.4 +/* 6.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +/* 6.28 + * @test 6.29 + * @bug 8075520 6.30 + * @summary Varargs access check mishandles capture variables 6.31 + * @compile VarargsAndWildcardParameterizedTypeTest3.java 6.32 + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest3.java 6.33 + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest3.java 6.34 + */ 6.35 + 6.36 +class VarargsAndWildcardParameterizedTypeTest2 { 6.37 + interface I { 6.38 + <T> void m(Box<? extends T> iter, T... t); 6.39 + } 6.40 + 6.41 + interface Box<T> {} 6.42 + 6.43 + void m(I i, Box<? extends Number> b) { 6.44 + i.m(b); 6.45 + } 6.46 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java Fri Apr 17 08:55:59 2015 -0600 7.3 @@ -0,0 +1,43 @@ 7.4 +/* 7.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 7.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.7 + * 7.8 + * This code is free software; you can redistribute it and/or modify it 7.9 + * under the terms of the GNU General Public License version 2 only, as 7.10 + * published by the Free Software Foundation. 7.11 + * 7.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 7.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 7.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 7.15 + * version 2 for more details (a copy is included in the LICENSE file that 7.16 + * accompanied this code). 7.17 + * 7.18 + * You should have received a copy of the GNU General Public License version 7.19 + * 2 along with this work; if not, write to the Free Software Foundation, 7.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 7.21 + * 7.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 7.23 + * or visit www.oracle.com if you need additional information or have any 7.24 + * questions. 7.25 + */ 7.26 + 7.27 +/* 7.28 + * @test 7.29 + * @bug 8075520 7.30 + * @summary Varargs access check mishandles capture variables 7.31 + * @compile VarargsAndWildcardParameterizedTypeTest4.java 7.32 + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest4.java 7.33 + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest4.java 7.34 + */ 7.35 + 7.36 +class VarargsAndWildcardParameterizedTypeTest2 { 7.37 + interface I { 7.38 + <T> void m(Box<T> iter, T... t); 7.39 + } 7.40 + 7.41 + interface Box<T> {} 7.42 + 7.43 + void m(I i, Box<? extends Number> b) { 7.44 + i.m(b); 7.45 + } 7.46 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out Fri Apr 17 08:55:59 2015 -0600 8.3 @@ -0,0 +1,4 @@ 8.4 +VarargsInferredPrivateType.java:16:10: compiler.err.cant.apply.symbol: kindname.method, m, T[], otherpackage.OtherPackage.Private, kindname.interface, VarargsInferredPrivateType.I, (compiler.misc.inaccessible.varargs.type: otherpackage.OtherPackage.Private, kindname.class, VarargsInferredPrivateType) 8.5 +- compiler.note.unchecked.filename: VarargsInferredPrivateType.java 8.6 +- compiler.note.unchecked.recompile 8.7 +1 error
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType.java Fri Apr 17 08:55:59 2015 -0600 9.3 @@ -0,0 +1,18 @@ 9.4 +/* 9.5 + * @test /nodynamiccopyright/ 9.6 + * @bug 8077786 9.7 + * @summary Check varargs access against inferred signature 9.8 + * @compile/fail/ref=VarargsInferredPrivateType.out -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java 9.9 + * @compile/fail/ref=VarargsInferredPrivateType.out -source 8 -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java 9.10 + * @compile/fail/ref=VarargsInferredPrivateType-source7.out -source 7 -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java 9.11 + */ 9.12 + 9.13 +class VarargsInferredPrivateType { 9.14 + interface I { 9.15 + <T> void m(T... t); 9.16 + } 9.17 + 9.18 + void m(I i) { 9.19 + i.m(otherpackage.OtherPackage.getPrivate()); 9.20 + } 9.21 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType.out Fri Apr 17 08:55:59 2015 -0600 10.3 @@ -0,0 +1,4 @@ 10.4 +VarargsInferredPrivateType.java:16:12: compiler.err.prob.found.req: (compiler.misc.inaccessible.varargs.type: otherpackage.OtherPackage.Private, kindname.class, VarargsInferredPrivateType) 10.5 +- compiler.note.unchecked.filename: VarargsInferredPrivateType.java 10.6 +- compiler.note.unchecked.recompile 10.7 +1 error