8160527: Check for final instance field updates can be omitted

Disable final field resolution in the constant pool cache. Correct error message printed at field resolution. New tests.

Reviewed-by: jrose, coleenp
This commit is contained in:
Zoltan Majo 2016-07-06 11:16:36 +02:00
parent 9a3dc1d8cc
commit e0e3bd5582
5 changed files with 217 additions and 14 deletions

@ -576,27 +576,27 @@ void InterpreterRuntime::resolve_get_put(JavaThread* thread, Bytecodes::Code byt
// compute auxiliary field attributes
TosState state = as_TosState(info.field_type());
// We need to delay resolving put instructions on final fields
// until we actually invoke one. This is required so we throw
// exceptions at the correct place. If we do not resolve completely
// in the current pass, leaving the put_code set to zero will
// cause the next put instruction to reresolve.
Bytecodes::Code put_code = (Bytecodes::Code)0;
// We also need to delay resolving getstatic instructions until the
// class is intitialized. This is required so that access to the static
// Put instructions on final fields are not resolved. This is required so we throw
// exceptions at the correct place (when the instruction is actually invoked).
// If we do not resolve an instruction in the current pass, leaving the put_code
// set to zero will cause the next put instruction to the same field to reresolve.
//
// Also, we need to delay resolving getstatic and putstatic instructions until the
// class is initialized. This is required so that access to the static
// field will call the initialization function every time until the class
// is completely initialized ala. in 2.17.5 in JVM Specification.
InstanceKlass* klass = InstanceKlass::cast(info.field_holder());
bool uninitialized_static = ((bytecode == Bytecodes::_getstatic || bytecode == Bytecodes::_putstatic) &&
!klass->is_initialized());
Bytecodes::Code get_code = (Bytecodes::Code)0;
Bytecodes::Code put_code = (Bytecodes::Code)0;
if (is_put && !info.access_flags().is_final() && !uninitialized_static) {
put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
}
Bytecodes::Code get_code = (Bytecodes::Code)0;
if (!uninitialized_static) {
get_code = ((is_static) ? Bytecodes::_getstatic : Bytecodes::_getfield);
if (is_put || !info.access_flags().is_final()) {
put_code = ((is_static) ? Bytecodes::_putstatic : Bytecodes::_putfield);
}
}
cp_cache_entry->set_field(

@ -970,7 +970,7 @@ void LinkResolver::resolve_field(fieldDescriptor& fd,
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 ? "<clinit>" : "<init>");
THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
}

@ -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 <init>:"()V"
stack 2 locals 1
{
aload_0;
dup;
invokespecial Method java/lang/Object.<init>:"()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 <init>:"()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;
}
}

@ -0,0 +1,60 @@
/*
* 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
* @compile TestPutStatic.jasm
* @compile TestPutMain.java
* @run main/othervm TestPutMain
*/
import jdk.test.lib.Asserts;
public class TestPutMain {
public static void main(String[] args) {
boolean exception = false;
try {
TestPutField.test();
} catch (java.lang.IllegalAccessError e) {
exception = true;
}
Asserts.assertTrue(exception, "FAILED: Expected IllegalAccessError for illegal update to final instance field was not thrown.");
exception = false;
try {
TestPutStatic.test();
} catch (java.lang.IllegalAccessError e) {
exception = true;
}
Asserts.assertTrue(exception, "FAILED: Expected IllegalAccessError for illegal update to final static field was not thrown.");
System.out.println("PASSED: Expected IllegalAccessError was thrown.");
}
}

@ -0,0 +1,66 @@
/*
* 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 TestPutStatic
version 53:0
{
final static Field test_field:"I";
public static Method <clinit>:"()V"
stack 2 locals 1
{
bipush 13;
putstatic Field test_field:"I";
return;
}
public static Method aMethod:"()I"
stack 1 locals 1
{
getstatic Field test_field:"I";
istore_0;
bipush 14;
putstatic Field test_field:"I";
iload_0;
ireturn;
}
public static Method test:"()V"
stack 2 locals 1
{
getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
astore_0;
aload_0;
invokestatic Method aMethod:"()I";
invokevirtual Method java/io/PrintStream.println:"(I)V";
aload_0;
getstatic Field test_field:"I";
invokevirtual Method java/io/PrintStream.println:"(I)V";
return;
}
}