Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
import java.util.Optional;
import java.util.StringJoiner;

import com.oracle.svm.core.reflect.fieldaccessor.UnsafeFieldAccessorFactory;
import jdk.internal.access.JavaLangReflectAccess;
import jdk.internal.reflect.FieldAccessor;
import org.graalvm.compiler.core.common.NumUtil;
import org.graalvm.compiler.core.common.SuppressFBWarnings;
import org.graalvm.nativeimage.AnnotationAccess;
Expand Down Expand Up @@ -1881,6 +1884,9 @@ final class Target_jdk_internal_reflect_ReflectionFactory {
@Alias //
private static ReflectionFactory soleInstance;

@Alias //
private JavaLangReflectAccess langReflectAccess;

/**
* This substitution eliminates the SecurityManager check in the original method, which would
* make some build-time verifications fail.
Expand All @@ -1890,15 +1896,20 @@ public static ReflectionFactory getReflectionFactory() {
return soleInstance;
}

/**
* Do not use the field handle based field accessor but the one based on unsafe. It takes effect
* when {@code Target_java_lang_reflect_Field#fieldAccessorField#fieldAccessor} is recomputed at
* runtime. See also GR-39586.
*/
@TargetElement(onlyWith = JDK19OrLater.class)
@Substitute
static boolean useFieldHandleAccessor() {
return false;
public FieldAccessor newFieldAccessor(Field field0, boolean override) {
Field field = field0;
Field root = langReflectAccess.getRoot(field);
if (root != null) {
// FieldAccessor will use the root unless the modifiers have
// been overridden
if (root.getModifiers() == field.getModifiers() || !override) {
field = root;
}
}
boolean isFinal = Modifier.isFinal(field.getModifiers());
boolean isReadOnly = isFinal && (!override || langReflectAccess.isTrustedFinalField(field));
return UnsafeFieldAccessorFactory.newFieldAccessor(field, isReadOnly);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2001, 2005, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

package com.oracle.svm.core.reflect.fieldaccessor;

import java.lang.reflect.Field;

class UnsafeBooleanFieldAccessorImpl extends UnsafeFieldAccessorImpl {
UnsafeBooleanFieldAccessorImpl(Field field) {
super(field);
}

@Override
public Object get(Object obj) throws IllegalArgumentException {
return Boolean.valueOf(getBoolean(obj));
}

@Override
public boolean getBoolean(Object obj) throws IllegalArgumentException {
ensureObj(obj);
return unsafe.getBoolean(obj, fieldOffset);
}

@Override
public byte getByte(Object obj) throws IllegalArgumentException {
throw newGetByteIllegalArgumentException();
}

@Override
public char getChar(Object obj) throws IllegalArgumentException {
throw newGetCharIllegalArgumentException();
}

@Override
public short getShort(Object obj) throws IllegalArgumentException {
throw newGetShortIllegalArgumentException();
}

@Override
public int getInt(Object obj) throws IllegalArgumentException {
throw newGetIntIllegalArgumentException();
}

@Override
public long getLong(Object obj) throws IllegalArgumentException {
throw newGetLongIllegalArgumentException();
}

@Override
public float getFloat(Object obj) throws IllegalArgumentException {
throw newGetFloatIllegalArgumentException();
}

@Override
public double getDouble(Object obj) throws IllegalArgumentException {
throw newGetDoubleIllegalArgumentException();
}

@Override
public void set(Object obj, Object value)
throws IllegalArgumentException, IllegalAccessException {
ensureObj(obj);
if (isFinal) {
throwFinalFieldIllegalAccessException(value);
}
if (value == null) {
throwSetIllegalArgumentException(value);
}
if (value instanceof Boolean) {
unsafe.putBoolean(obj, fieldOffset, ((Boolean) value).booleanValue());
return;
}
throwSetIllegalArgumentException(value);
}

@Override
public void setBoolean(Object obj, boolean z)
throws IllegalArgumentException, IllegalAccessException {
ensureObj(obj);
if (isFinal) {
throwFinalFieldIllegalAccessException(z);
}
unsafe.putBoolean(obj, fieldOffset, z);
}

@Override
public void setByte(Object obj, byte b)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(b);
}

@Override
public void setChar(Object obj, char c)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(c);
}

@Override
public void setShort(Object obj, short s)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(s);
}

@Override
public void setInt(Object obj, int i)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(i);
}

@Override
public void setLong(Object obj, long l)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(l);
}

@Override
public void setFloat(Object obj, float f)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(f);
}

@Override
public void setDouble(Object obj, double d)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2001, 2005, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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.
*/

package com.oracle.svm.core.reflect.fieldaccessor;

import java.lang.reflect.Field;

class UnsafeByteFieldAccessorImpl extends UnsafeFieldAccessorImpl {
UnsafeByteFieldAccessorImpl(Field field) {
super(field);
}

@Override
public Object get(Object obj) throws IllegalArgumentException {
return Byte.valueOf(getByte(obj));
}

@Override
public boolean getBoolean(Object obj) throws IllegalArgumentException {
throw newGetBooleanIllegalArgumentException();
}

@Override
public byte getByte(Object obj) throws IllegalArgumentException {
ensureObj(obj);
return unsafe.getByte(obj, fieldOffset);
}

@Override
public char getChar(Object obj) throws IllegalArgumentException {
throw newGetCharIllegalArgumentException();
}

@Override
public short getShort(Object obj) throws IllegalArgumentException {
return getByte(obj);
}

@Override
public int getInt(Object obj) throws IllegalArgumentException {
return getByte(obj);
}

@Override
public long getLong(Object obj) throws IllegalArgumentException {
return getByte(obj);
}

@Override
public float getFloat(Object obj) throws IllegalArgumentException {
return getByte(obj);
}

@Override
public double getDouble(Object obj) throws IllegalArgumentException {
return getByte(obj);
}

@Override
public void set(Object obj, Object value)
throws IllegalArgumentException, IllegalAccessException {
ensureObj(obj);
if (isFinal) {
throwFinalFieldIllegalAccessException(value);
}
if (value == null) {
throwSetIllegalArgumentException(value);
}
if (value instanceof Byte) {
unsafe.putByte(obj, fieldOffset, ((Byte) value).byteValue());
return;
}
throwSetIllegalArgumentException(value);
}

@Override
public void setBoolean(Object obj, boolean z)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(z);
}

@Override
public void setByte(Object obj, byte b)
throws IllegalArgumentException, IllegalAccessException {
ensureObj(obj);
if (isFinal) {
throwFinalFieldIllegalAccessException(b);
}
unsafe.putByte(obj, fieldOffset, b);
}

@Override
public void setChar(Object obj, char c)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(c);
}

@Override
public void setShort(Object obj, short s)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(s);
}

@Override
public void setInt(Object obj, int i)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(i);
}

@Override
public void setLong(Object obj, long l)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(l);
}

@Override
public void setFloat(Object obj, float f)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(f);
}

@Override
public void setDouble(Object obj, double d)
throws IllegalArgumentException, IllegalAccessException {
throwSetIllegalArgumentException(d);
}
}
Loading