From f13e39dbcddf961b890ccc786530a825909d1758 Mon Sep 17 00:00:00 2001 From: dlsmith Date: Fri, 17 Apr 2015 08:55:59 -0600 Subject: [PATCH] 8075520: Varargs access check mishandles capture variables 8077786: Check varargs access against inferred signature Reviewed-by: vromero --- .../com/sun/tools/javac/comp/Resolve.java | 19 ++++---- .../javac/varargs/access/OtherPackage.java | 36 +++++++++++++++ ...rargsAndWildcardParameterizedTypeTest.java | 2 + ...argsAndWildcardParameterizedTypeTest2.java | 45 +++++++++++++++++++ ...argsAndWildcardParameterizedTypeTest3.java | 43 ++++++++++++++++++ ...argsAndWildcardParameterizedTypeTest4.java | 43 ++++++++++++++++++ .../VarargsInferredPrivateType-source7.out | 4 ++ .../access/VarargsInferredPrivateType.java | 18 ++++++++ .../access/VarargsInferredPrivateType.out | 4 ++ 9 files changed, 204 insertions(+), 10 deletions(-) create mode 100644 test/tools/javac/varargs/access/OtherPackage.java rename test/tools/javac/varargs/{T8049075 => access}/VarargsAndWildcardParameterizedTypeTest.java (91%) create mode 100644 test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java create mode 100644 test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java create mode 100644 test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java create mode 100644 test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out create mode 100644 test/tools/javac/varargs/access/VarargsInferredPrivateType.java create mode 100644 test/tools/javac/varargs/access/VarargsInferredPrivateType.out diff --git a/src/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/share/classes/com/sun/tools/javac/comp/Resolve.java index 87a29320..6ac4f7a8 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -836,20 +836,19 @@ public class Resolve { List formals, Warner warn) { super.argumentsAcceptable(env, deferredAttrContext, argtypes, formals, warn); - //should we expand formals? + // should we check varargs element type accessibility? if (deferredAttrContext.phase.isVarargsRequired()) { - Type typeToCheck = null; - if (!checkVarargsAccessAfterResolution) { - typeToCheck = types.elemtype(formals.last()); - } else if (deferredAttrContext.mode == AttrMode.CHECK) { - typeToCheck = types.erasure(types.elemtype(formals.last())); - } - if (typeToCheck != null) { - varargsAccessible(env, typeToCheck, deferredAttrContext.inferenceContext); + if (deferredAttrContext.mode == AttrMode.CHECK || !checkVarargsAccessAfterResolution) { + varargsAccessible(env, types.elemtype(formals.last()), deferredAttrContext.inferenceContext); } } } + /** + * Test that the runtime array element type corresponding to 't' is accessible. 't' should be the + * varargs element type of either the method invocation type signature (after inference completes) + * or the method declaration signature (before inference completes). + */ private void varargsAccessible(final Env env, final Type t, final InferenceContext inferenceContext) { if (inferenceContext.free(t)) { inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() { @@ -859,7 +858,7 @@ public class Resolve { } }); } else { - if (!isAccessible(env, t)) { + if (!isAccessible(env, types.erasure(t))) { Symbol location = env.enclClass.sym; reportMC(env.tree, MethodCheckDiag.INACCESSIBLE_VARARGS, inferenceContext, t, Kinds.kindName(location), location); } diff --git a/test/tools/javac/varargs/access/OtherPackage.java b/test/tools/javac/varargs/access/OtherPackage.java new file mode 100644 index 00000000..2bfd4c3f --- /dev/null +++ b/test/tools/javac/varargs/access/OtherPackage.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * Auxiliary file for VarargsInferredPrivateType + */ + +package otherpackage; + +public class OtherPackage { + public static Private getPrivate() { + return new Private(); + } + + private static class Private {} +} \ No newline at end of file diff --git a/test/tools/javac/varargs/T8049075/VarargsAndWildcardParameterizedTypeTest.java b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest.java similarity index 91% rename from test/tools/javac/varargs/T8049075/VarargsAndWildcardParameterizedTypeTest.java rename to test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest.java index c9618766..9879924e 100644 --- a/test/tools/javac/varargs/T8049075/VarargsAndWildcardParameterizedTypeTest.java +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest.java @@ -26,6 +26,8 @@ * @bug 8049075 * @summary javac, wildcards and generic vararg method invocation not accepted * @compile VarargsAndWildcardParameterizedTypeTest.java + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest.java + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest.java */ class VarargsAndWildcardParameterizedTypeTest { diff --git a/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java new file mode 100644 index 00000000..39371972 --- /dev/null +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8075520 + * @summary Varargs access check mishandles capture variables + * @compile VarargsAndWildcardParameterizedTypeTest2.java + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest2.java + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest2.java + */ + +class VarargsAndWildcardParameterizedTypeTest2 { + interface I { + void m(T... t); + } + + interface Box { + T get(); + } + + void m(I i, Box b) { + i.m(b.get()); + } +} diff --git a/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java new file mode 100644 index 00000000..ae468513 --- /dev/null +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8075520 + * @summary Varargs access check mishandles capture variables + * @compile VarargsAndWildcardParameterizedTypeTest3.java + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest3.java + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest3.java + */ + +class VarargsAndWildcardParameterizedTypeTest2 { + interface I { + void m(Box iter, T... t); + } + + interface Box {} + + void m(I i, Box b) { + i.m(b); + } +} diff --git a/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java new file mode 100644 index 00000000..77c61d67 --- /dev/null +++ b/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8075520 + * @summary Varargs access check mishandles capture variables + * @compile VarargsAndWildcardParameterizedTypeTest4.java + * @compile -source 8 VarargsAndWildcardParameterizedTypeTest4.java + * @compile -source 7 VarargsAndWildcardParameterizedTypeTest4.java + */ + +class VarargsAndWildcardParameterizedTypeTest2 { + interface I { + void m(Box iter, T... t); + } + + interface Box {} + + void m(I i, Box b) { + i.m(b); + } +} diff --git a/test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out b/test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out new file mode 100644 index 00000000..87670f62 --- /dev/null +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out @@ -0,0 +1,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) +- compiler.note.unchecked.filename: VarargsInferredPrivateType.java +- compiler.note.unchecked.recompile +1 error diff --git a/test/tools/javac/varargs/access/VarargsInferredPrivateType.java b/test/tools/javac/varargs/access/VarargsInferredPrivateType.java new file mode 100644 index 00000000..efe8d62e --- /dev/null +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType.java @@ -0,0 +1,18 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8077786 + * @summary Check varargs access against inferred signature + * @compile/fail/ref=VarargsInferredPrivateType.out -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java + * @compile/fail/ref=VarargsInferredPrivateType.out -source 8 -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java + * @compile/fail/ref=VarargsInferredPrivateType-source7.out -source 7 -nowarn -XDrawDiagnostics VarargsInferredPrivateType.java OtherPackage.java + */ + +class VarargsInferredPrivateType { + interface I { + void m(T... t); + } + + void m(I i) { + i.m(otherpackage.OtherPackage.getPrivate()); + } +} diff --git a/test/tools/javac/varargs/access/VarargsInferredPrivateType.out b/test/tools/javac/varargs/access/VarargsInferredPrivateType.out new file mode 100644 index 00000000..aae7daba --- /dev/null +++ b/test/tools/javac/varargs/access/VarargsInferredPrivateType.out @@ -0,0 +1,4 @@ +VarargsInferredPrivateType.java:16:12: compiler.err.prob.found.req: (compiler.misc.inaccessible.varargs.type: otherpackage.OtherPackage.Private, kindname.class, VarargsInferredPrivateType) +- compiler.note.unchecked.filename: VarargsInferredPrivateType.java +- compiler.note.unchecked.recompile +1 error -- GitLab