--- old/src/share/vm/interpreter/linkResolver.cpp 2016-06-29 16:52:32.730960180 +0200 +++ new/src/share/vm/interpreter/linkResolver.cpp 2016-06-29 16:52:32.598960177 +0200 @@ -970,7 +970,7 @@ if (is_initialized_static_final_update || is_initialized_instance_final_update) { ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string(), - current_klass()->external_name(), + m()->name()->as_C_string(), is_static ? "" : ""); THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string()); } --- /dev/null 2016-06-03 09:16:56.569627796 +0200 +++ new/test/runtime/Final/TestPutField.jasm 2016-06-29 16:52:32.598960177 +0200 @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2016, 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. + * + */ + +public class TestPutField +version 53:0 +{ + +final Field test_field:"I"; + + +public Method :"()V" + stack 2 locals 1 +{ + aload_0; + dup; + invokespecial Method java/lang/Object.:"()V"; + bipush 13; + putfield Field test_field:"I"; + return; +} + +public Method aMethod:"()I" + stack 2 locals 2 +{ + aload_0; + getfield Field test_field:"I"; + istore_1; + aload_0; + bipush 14; + putfield Field test_field:"I"; + iload_1; + ireturn; +} + + +public static Method test:"()V" + stack 2 locals 2 +{ + new class TestPutField; + astore_0; + aload_0; + invokespecial Method :"()V"; + getstatic Field java/lang/System.out:"Ljava/io/PrintStream;"; + astore_1; + aload_1; + aload_0; + invokevirtual Method aMethod:"()I"; + invokevirtual Method java/io/PrintStream.println:"(I)V"; + aload_1; + aload_0; + getfield Field test_field:"I"; + invokevirtual Method java/io/PrintStream.println:"(I)V"; + return; +} + +} --- old/src/share/vm/interpreter/interpreterRuntime.cpp 2016-06-29 16:52:32.770960182 +0200 +++ new/src/share/vm/interpreter/interpreterRuntime.cpp 2016-06-29 16:52:32.598960177 +0200 @@ -592,9 +592,24 @@ !klass->is_initialized()); Bytecodes::Code get_code = (Bytecodes::Code)0; + /* Do not cache the result of the resolution for putfield instructions + * to instance final fields. + * + * A putfield instruction targeting an instance final field must throw + * an IllegalAccessError if the instruction is not in an instance + * initializer method . Without the check below, a putfield + * in an initializer method is resolved; subsequent putfield instructions + * to the same field then use cached information and thus do not pass + * through the VM (i.e., checks will not be executed for those instructions). + */ + bool final_instance_update = info.field_holder()->major_version() >= 53 && + info.has_initialized_final_update() && + (bytecode == Bytecodes::_putfield || bytecode == Bytecodes::_nofast_putfield); + + if (!uninitialized_static) { get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield); - if (is_put || !info.access_flags().is_final()) { + if ((is_put && !final_instance_update) || !info.access_flags().is_final()) { put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield); } } --- /dev/null 2016-06-03 09:16:56.569627796 +0200 +++ new/test/runtime/Final/TestPutFieldMain.java 2016-06-29 16:52:32.686960179 +0200 @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2016, 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 8160527 + * @summary The VM does not always perform checks added by 8157181 when updating final instance fields + * @library /testlibrary + * @compile TestPutField.jasm + * @run main/othervm TestPutFieldMain + */ + +import jdk.test.lib.Asserts; + +public class TestPutFieldMain { + public static void main(String[] args) { + boolean exception = false; + try { + TestPutField.test(); + } catch (java.lang.IllegalAccessError e) { + exception = true; + } + + Asserts.assertTrue(exception, "FAILED: Excepted IllegalAccessError was not thrown."); + System.out.println("PASSED: Expected IllegalAccessError was thrown."); + } +}