8075520: Varargs access check mishandles capture variables
authordlsmith
Thu, 16 Apr 2015 17:20:20 -0600
changeset 29958 3463809fbb52
parent 29957 7740f9657f56
child 29959 61bfc66866da
8075520: Varargs access check mishandles capture variables 8077786: Check varargs access against inferred signature Reviewed-by: vromero
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
langtools/test/tools/javac/varargs/T8049075/VarargsAndWildcardParameterizedTypeTest.java
langtools/test/tools/javac/varargs/access/OtherPackage.java
langtools/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest.java
langtools/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java
langtools/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java
langtools/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java
langtools/test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out
langtools/test/tools/javac/varargs/access/VarargsInferredPrivateType.java
langtools/test/tools/javac/varargs/access/VarargsInferredPrivateType.out
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Mon Apr 13 18:05:23 2015 -0700
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Thu Apr 16 17:20:20 2015 -0600
@@ -833,20 +833,19 @@
                                     List<Type> 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<AttrContext> env, final Type t, final InferenceContext inferenceContext) {
             if (inferenceContext.free(t)) {
                 inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() {
@@ -856,7 +855,7 @@
                     }
                 });
             } 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);
                 }
--- a/langtools/test/tools/javac/varargs/T8049075/VarargsAndWildcardParameterizedTypeTest.java	Mon Apr 13 18:05:23 2015 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2014, 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 8049075
- * @summary javac, wildcards and generic vararg method invocation not accepted
- * @compile VarargsAndWildcardParameterizedTypeTest.java
- */
-
-class VarargsAndWildcardParameterizedTypeTest {
-    interface I<T> {
-        String m(T... t);
-    }
-
-    void m() {
-        I<? super Integer> i = null;
-        i.m(Integer.valueOf(1), Integer.valueOf(1));
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/access/OtherPackage.java	Thu Apr 16 17:20:20 2015 -0600
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest.java	Thu Apr 16 17:20:20 2015 -0600
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2014, 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 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 {
+    interface I<T> {
+        String m(T... t);
+    }
+
+    void m() {
+        I<? super Integer> i = null;
+        i.m(Integer.valueOf(1), Integer.valueOf(1));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest2.java	Thu Apr 16 17:20:20 2015 -0600
@@ -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 {
+        <T> void m(T... t);
+    }
+
+    interface Box<T> {
+        T get();
+    }
+
+    void m(I i, Box<? extends Number> b) {
+        i.m(b.get());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest3.java	Thu Apr 16 17:20:20 2015 -0600
@@ -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 {
+        <T> void m(Box<? extends T> iter, T... t);
+    }
+
+    interface Box<T> {}
+
+    void m(I i, Box<? extends Number> b) {
+        i.m(b);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/access/VarargsAndWildcardParameterizedTypeTest4.java	Thu Apr 16 17:20:20 2015 -0600
@@ -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 {
+        <T> void m(Box<T> iter, T... t);
+    }
+
+    interface Box<T> {}
+
+    void m(I i, Box<? extends Number> b) {
+        i.m(b);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/access/VarargsInferredPrivateType-source7.out	Thu Apr 16 17:20:20 2015 -0600
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/access/VarargsInferredPrivateType.java	Thu Apr 16 17:20:20 2015 -0600
@@ -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 {
+        <T> void m(T... t);
+    }
+
+    void m(I i) {
+        i.m(otherpackage.OtherPackage.getPrivate());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/varargs/access/VarargsInferredPrivateType.out	Thu Apr 16 17:20:20 2015 -0600
@@ -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