--- old/src/share/classes/java/lang/invoke/MethodHandleNatives.java 2013-09-06 19:25:56.000000000 -0400 +++ new/src/share/classes/java/lang/invoke/MethodHandleNatives.java 2013-09-06 19:25:56.000000000 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2013, 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 @@ -379,6 +379,10 @@ Lookup lookup = IMPL_LOOKUP.in(callerClass); assert(refKindIsValid(refKind)); return lookup.linkMethodHandleConstant((byte) refKind, defc, name, type); + } catch (IllegalAccessException ex) { + Error err = new IllegalAccessError(ex.getMessage()); + // err.initCause(ex); + throw err; } catch (ReflectiveOperationException ex) { Error err = new IncompatibleClassChangeError(); err.initCause(ex); --- /dev/null 2013-09-06 19:25:57.000000000 -0400 +++ new/test/java/lang/invoke/8022701/BogoLoader.java 2013-09-06 19:25:57.000000000 -0400 @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2013, 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. + * + */ + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.Set; +import java.util.Vector; + +public class BogoLoader extends ClassLoader { + + /** + * Use this property to verify that the desired classloading is happening. + */ + private final boolean verbose = Boolean.getBoolean("bogoloader.verbose"); + /** + * Use this property to disable replacement for testing purposes. + */ + private final boolean noReplace = Boolean.getBoolean("bogoloader.noreplace"); + + private Set nonSystem; + private Map replaced; + private final Vector history = new Vector(); + + private boolean useSystemLoader(String name) { + return ! nonSystem.contains(name) && ! replaced.containsKey(name); + } + + public BogoLoader(Set non_system, Map replaced) { + super(Thread.currentThread().getContextClassLoader()); + this.nonSystem = non_system; + this.replaced = replaced; + } + + private byte[] readResource(String className) throws IOException { + return readResource(className, "class"); + } + + private byte[] readResource(String className, String suffix) throws IOException { + // Note to the unwary -- "/" works on Windows, leave it alone. + String fileName = className.replace('.', '/') + "." + suffix; + InputStream origStream = getResourceAsStream(fileName); + if (origStream == null) { + throw new IOException("Resource not found : " + fileName); + } + BufferedInputStream stream = new java.io.BufferedInputStream(origStream); + byte[] data = new byte[stream.available()]; + int how_many = stream.read(data); + // Really ought to deal with the corner cases of stream.available() + return data; + } + + protected byte[] getClass(String name) throws ClassNotFoundException, + IOException { + return readResource(name, "class"); + } + + /** + * Loads the named class from the system class loader unless + * the name appears in either replaced or nonSystem. + * nonSystem classes are loaded into this classloader, + * and replaced classes get their content from the specified array + * of bytes (and are also loaded into this classloader). + */ + protected Class loadClass(String name, boolean resolve) + throws ClassNotFoundException { + Class clazz; + + if (history.contains(name)) { + Class c = this.findLoadedClass(name); + return c; + } + if (useSystemLoader(name)) { + clazz = findSystemClass(name); + if (verbose) System.err.println("Loading system class " + name); + } else { + history.add(name); + try { + byte[] classData = null; + boolean expanded = false; + try { + if (!noReplace && replaced.containsKey(name)) { + if (verbose) System.err.println("Loading replaced class " + name); + classData = replaced.get(name); + } else { + if (verbose) System.err.println("Loading classloader class " + name); + classData = getClass(name); + } + } catch (java.io.EOFException ioe) { + throw new ClassNotFoundException( + "IO Exception in reading class : " + name + " ", ioe); + } + clazz = defineClass(name, classData, 0, classData.length); + } catch (java.io.EOFException ioe) { + throw new ClassNotFoundException( + "IO Exception in reading class : " + name + " ", ioe); + } catch (ClassFormatError ioe) { + throw new ClassNotFoundException( + "ClassFormatError in reading class file: ", ioe); + } catch (IOException ioe) { + throw new ClassNotFoundException( + "IO Exception in reading class file: ", ioe); + } + } + if (clazz == null) { + throw new ClassNotFoundException(name); + } + if (resolve) { + resolveClass(clazz); + } + return clazz; + } +} --- /dev/null 2013-09-06 19:25:58.000000000 -0400 +++ new/test/java/lang/invoke/8022701/InvokeSeveralWays.java 2013-09-06 19:25:58.000000000 -0400 @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2013, 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. + * + */ + +import java.lang.reflect.InvocationTargetException; + +interface MyFunctionalInterface { + void invokeMethodReference(); +} + +class MethodInvoker { + /** + * Use a method handle to invoke. + */ + public static void invoke() { + MyFunctionalInterface fi = null; + fi = new MethodSupplier()::m; + fi.invokeMethodReference(); + } + /** + * Invoke directly. + */ + public static void invoke2() { + MethodSupplier ms = new MethodSupplier(); + ms.m(); + } +} +/** + * + */ +public class InvokeSeveralWays { + public static void main(String args[]) throws Exception { + int failures = 0; + try { + Class.forName("MethodInvoker").getMethod("invoke").invoke(null); + System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); + failures++; + } catch (InvocationTargetException e) { + Throwable c = e.getCause(); + if (c instanceof IllegalAccessError) + System.out.println("EXPECTED: IllegalAccessError " + c); + else { + failures++; + System.out.println("FAIL: Unexpected wrapped exception " + c); + e.printStackTrace(System.out); + } + } catch (Throwable e) { + failures++; + System.out.println("FAIL: Unexpected exception has been caught " + e); + e.printStackTrace(System.out); + } + System.out.println(); + try { + Class.forName("MethodInvoker").getMethod("invoke2").invoke(null); + System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); + failures++; + } catch (InvocationTargetException e) { + Throwable c = e.getCause(); + if (c instanceof IllegalAccessError) + System.out.println("EXPECTED: IllegalAccessError " + c); + else { + failures++; + System.out.println("FAIL: Unexpected wrapped exception " + c); + e.printStackTrace(System.out); + } + } catch (Throwable e) { + failures++; + System.out.println("FAIL: Unexpected exception has been caught " + e); + e.printStackTrace(System.out); + } + System.out.println(); + try { + MethodInvoker.invoke(); + System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); + failures++; + } catch (IllegalAccessError e) { + System.out.println("EXPECTED: IllegalAccessError " + e); + } catch (Throwable e) { + failures++; + System.out.println("FAIL: Unexpected exception has been caught " + e); + e.printStackTrace(System.out); + } + System.out.println(); + try { + MethodInvoker.invoke2(); + System.out.println("FAIL: No exception throw, probably failed to load modified bytecodes for MethodSupplier"); + failures++; + } catch (IllegalAccessError e) { + System.out.println("EXPECTED: IllegalAccessError " + e); + } catch (Throwable e) { + failures++; + System.out.println("FAIL: Unexpected exception has been caught " + e); + e.printStackTrace(System.out); + } + System.out.println(); + if (failures > 0) { + System.out.println("Saw " + failures + " failures"); + throw new Error("Saw " + failures + " failures, see standard out for details"); + } + } +} --- /dev/null 2013-09-06 19:25:58.000000000 -0400 +++ new/test/java/lang/invoke/8022701/MHIllegalAccess.java 2013-09-06 19:25:58.000000000 -0400 @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2013, 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 8022701 + * @summary Illegal access exceptions via methodhandle invocations threw wrong error. + * + * @compile BogoLoader.java InvokeSeveralWays.java MHIllegalAccess.java MethodSupplier.java + * @run main/othervm MHIllegalAccess + */ + + +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.HashSet; + +public class MHIllegalAccess { + /* + * See MethodSupplier.java to see how to produce these bytes. + * They encode that class, except that method m is private, not public. + */ + static byte[] bogusMethodSupplier = { + -54, -2, -70, -66, 0, 0, 0, 52, 0, 28, 10, 0, 6, 0, 14, 9, + 0, 15, 0, 16, 8, 0, 17, 10, 0, 18, 0, 19, 7, 0, 20, 7, + 0, 21, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, + 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110, 101, 78, + 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 1, 109, 1, 0, + 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 19, 77, 101, + 116, 104, 111, 100, 83, 117, 112, 112, 108, 105, 101, 114, 46, 106, 97, 118, + 97, 12, 0, 7, 0, 8, 7, 0, 22, 12, 0, 23, 0, 24, 1, 0, + 4, 103, 111, 111, 100, 7, 0, 25, 12, 0, 26, 0, 27, 1, 0, 14, + 77, 101, 116, 104, 111, 100, 83, 117, 112, 112, 108, 105, 101, 114, 1, 0, + 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, + 116, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 121, + 115, 116, 101, 109, 1, 0, 3, 111, 117, 116, 1, 0, 21, 76, 106, 97, + 118, 97, 47, 105, 111, 47, 80, 114, 105, 110, 116, 83, 116, 114, 101, 97, + 109, 59, 1, 0, 19, 106, 97, 118, 97, 47, 105, 111, 47, 80, 114, 105, + 110, 116, 83, 116, 114, 101, 97, 109, 1, 0, 7, 112, 114, 105, 110, 116, + 108, 110, 1, 0, 21, 40, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, + 47, 83, 116, 114, 105, 110, 103, 59, 41, 86, 0, 33, 0, 5, 0, 6, + 0, 0, 0, 0, 0, 2, 0, 1, 0, 7, 0, 8, 0, 1, 0, 9, + 0, 0, 0, 29, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 1, + -79, 0, 0, 0, 1, 0, 10, 0, 0, 0, 6, 0, 1, 0, 0, 0, + 1, 0, 2, 0, 11, 0, 8, 0, 1, 0, 9, 0, 0, 0, 37, 0, + 2, 0, 1, 0, 0, 0, 9, -78, 0, 2, 18, 3, -74, 0, 4, -79, + 0, 0, 0, 1, 0, 10, 0, 0, 0, 10, 0, 2, 0, 0, 0, 3, + 0, 8, 0, 4, 0, 1, 0, 12, 0, 0, 0, 2, 0, 13,}; + + public static void main(String args[]) throws Throwable { + HashMap replace = new HashMap(); + replace.put("MethodSupplier", bogusMethodSupplier); + HashSet in_bogus = new HashSet(); + in_bogus.add("InvokeSeveralWays"); + in_bogus.add("MyFunctionalInterface"); + in_bogus.add("MethodInvoker"); + BogoLoader bl = new BogoLoader(in_bogus, replace); + Class isw = bl.loadClass("InvokeSeveralWays"); + Object[] arg_for_args = new Object[1]; + arg_for_args[0] = args; + try { + isw.getMethod("main", String[].class).invoke(null, arg_for_args); + } catch (InvocationTargetException e) { + Throwable th = e.getCause(); + throw th == null ? e : th; + } + } +} --- /dev/null 2013-09-06 19:25:59.000000000 -0400 +++ new/test/java/lang/invoke/8022701/MethodSupplier.java 2013-09-06 19:25:59.000000000 -0400 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2013, 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. + * + */ + +/* + * NOTE: This is a sham class that exists to allow compilation + * of the other class files. The test substitutes a different + * version of this class where "m" has visibility "private", which + * is intended to produce particular failures (checked in the test). + * + * To produce the byte array constant in the file, edit this class appropiately, + * compile it, and run the command(s): + + od -td1 MethodSupplier.class | \ + sed -e '1,$s|^0000... *||' \ + -e '1,$s| *$|,|' \ + -e '1,$s| * |, |g' \ + -e '1s|^|{|' \ + -e '$s|,|};|' + */ + +public class MethodSupplier { + public void m() { + System.out.println("good"); + } +} +