Search in sources :

Example 56 with Field

use of com.oracle.truffle.espresso.impl.Field in project graal by oracle.

the class Target_sun_misc_Unsafe method putDouble.

@Substitution(hasReceiver = true)
public static void putDouble(@SuppressWarnings("unused") @JavaType(Unsafe.class) StaticObject self, @JavaType(Object.class) StaticObject holder, long offset, double value, @Inject Meta meta) {
    if (isNullOrArray(holder)) {
        UnsafeAccess.getIfAllowed(meta).putDouble(unwrapNullOrArray(holder), offset, value);
        return;
    }
    // TODO(peterssen): Current workaround assumes it's a field access, encoding is offset <->
    // field index.
    Field f = getInstanceFieldFromIndex(holder, Math.toIntExact(offset) - SAFETY_FIELD_OFFSET);
    assert f != null;
    f.setDouble(holder, value);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field)

Example 57 with Field

use of com.oracle.truffle.espresso.impl.Field in project graal by oracle.

the class ClassRedefinition method detectClassChanges.

// detect all types of class changes, but return early when a change that require arbitrary
// changes
private static ClassChange detectClassChanges(ParserKlass newParserKlass, ObjectKlass oldKlass, DetectedChange collectedChanges, ParserKlass finalParserKlass) throws RedefintionNotSupportedException {
    ClassChange result = ClassChange.NO_CHANGE;
    ParserKlass oldParserKlass = oldKlass.getLinkedKlass().getParserKlass();
    boolean isPatched = finalParserKlass != null;
    // detect method changes (including constructors)
    ParserMethod[] newParserMethods = newParserKlass.getMethods();
    List<Method> oldMethods = new ArrayList<>(Arrays.asList(oldKlass.getDeclaredMethods()));
    List<ParserMethod> newMethods = new ArrayList<>(Arrays.asList(newParserMethods));
    Map<Method, ParserMethod> bodyChanges = new HashMap<>();
    List<ParserMethod> newSpecialMethods = new ArrayList<>(1);
    boolean constantPoolChanged = false;
    if (!Arrays.equals(oldParserKlass.getConstantPool().getRawBytes(), newParserKlass.getConstantPool().getRawBytes())) {
        constantPoolChanged = true;
    }
    Iterator<Method> oldIt = oldMethods.iterator();
    Iterator<ParserMethod> newIt;
    while (oldIt.hasNext()) {
        Method oldMethod = oldIt.next();
        ParserMethod oldParserMethod = oldMethod.getLinkedMethod().getParserMethod();
        // verify that there is a new corresponding method
        newIt = newMethods.iterator();
        while (newIt.hasNext()) {
            ParserMethod newMethod = newIt.next();
            if (isSameMethod(oldParserMethod, newMethod)) {
                // detect method changes
                ClassChange change = detectMethodChanges(oldParserMethod, newMethod);
                switch(change) {
                    case NO_CHANGE:
                        if (isPatched) {
                            checkForSpecialConstructor(collectedChanges, bodyChanges, newSpecialMethods, oldMethod, oldParserMethod, newMethod);
                        } else if (constantPoolChanged) {
                            if (isObsolete(oldParserMethod, newMethod, oldParserKlass.getConstantPool(), newParserKlass.getConstantPool())) {
                                result = ClassChange.CONSTANT_POOL_CHANGE;
                                collectedChanges.addMethodBodyChange(oldMethod, newMethod);
                            } else {
                                collectedChanges.addUnchangedMethod(oldMethod);
                            }
                        } else {
                            collectedChanges.addUnchangedMethod(oldMethod);
                        }
                        break;
                    case METHOD_BODY_CHANGE:
                        result = change;
                        if (isPatched) {
                            checkForSpecialConstructor(collectedChanges, bodyChanges, newSpecialMethods, oldMethod, oldParserMethod, newMethod);
                        } else {
                            collectedChanges.addMethodBodyChange(oldMethod, newMethod);
                        }
                        break;
                    default:
                        return change;
                }
                newIt.remove();
                oldIt.remove();
                break;
            }
        }
    }
    if (isPatched) {
        ParserMethod[] finalMethods = finalParserKlass.getMethods();
        // map found changed methods
        for (Map.Entry<Method, ParserMethod> entry : bodyChanges.entrySet()) {
            Method oldMethod = entry.getKey();
            ParserMethod changed = entry.getValue();
            for (int i = 0; i < newParserMethods.length; i++) {
                if (newParserMethods[i] == changed) {
                    collectedChanges.addMethodBodyChange(oldMethod, finalMethods[i]);
                    break;
                }
            }
        }
        // map found new methods
        newMethods.addAll(newSpecialMethods);
        for (ParserMethod changed : newMethods) {
            for (int i = 0; i < newParserMethods.length; i++) {
                if (newParserMethods[i] == changed) {
                    collectedChanges.addNewMethod(finalMethods[i]);
                    break;
                }
            }
        }
    } else {
        collectedChanges.addNewMethods(newMethods);
    }
    for (Method oldMethod : oldMethods) {
        collectedChanges.addRemovedMethod(oldMethod.getMethodVersion());
    }
    if (!oldMethods.isEmpty()) {
        result = ClassChange.REMOVE_METHOD;
    } else if (!newMethods.isEmpty()) {
        result = ClassChange.ADD_METHOD;
    }
    if (isPatched) {
        result = ClassChange.CLASS_NAME_CHANGED;
    }
    // detect field changes
    Field[] oldFields = oldKlass.getDeclaredFields();
    ParserField[] newFields = newParserKlass.getFields();
    ArrayList<Field> oldFieldsList = new ArrayList<>(Arrays.asList(oldFields));
    ArrayList<ParserField> newFieldsList = new ArrayList<>(Arrays.asList(newFields));
    Map<ParserField, Field> compatibleFields = new HashMap<>();
    Iterator<Field> oldFieldsIt = oldFieldsList.iterator();
    Iterator<ParserField> newFieldsIt;
    while (oldFieldsIt.hasNext()) {
        Field oldField = oldFieldsIt.next();
        newFieldsIt = newFieldsList.iterator();
        // search for a new corresponding field
        while (newFieldsIt.hasNext()) {
            ParserField newField = newFieldsIt.next();
            // first look for a perfect match
            if (isUnchangedField(oldField, newField, compatibleFields)) {
                // A nested anonymous inner class may contain a field reference to the outer
                // class instance. Since we match against the patched (inner class rename rules
                // applied) if the current class was patched (renamed) the resulting outer
                // field pointer will have a changed type. Hence we should mark it as a new
                // field.
                Matcher matcher = InnerClassRedefiner.ANON_INNER_CLASS_PATTERN.matcher(oldField.getType().toString());
                if (isPatched && matcher.matches()) {
                    break;
                }
                oldFieldsIt.remove();
                newFieldsIt.remove();
                break;
            }
        }
    }
    if (!newFieldsList.isEmpty()) {
        if (isPatched) {
            ParserField[] finalFields = finalParserKlass.getFields();
            // lookup the final new field based on the index in the parser field array
            for (ParserField parserField : newFieldsList) {
                for (int i = 0; i < newFields.length; i++) {
                    if (parserField == newFields[i]) {
                        collectedChanges.addNewField(finalFields[i]);
                        break;
                    }
                }
            }
        } else {
            collectedChanges.addNewFields(newFieldsList);
        }
        result = ClassChange.SCHEMA_CHANGE;
    }
    if (!oldFieldsList.isEmpty()) {
        collectedChanges.addRemovedFields(oldFieldsList);
        result = ClassChange.SCHEMA_CHANGE;
    }
    // detect class-level changes
    if (newParserKlass.getFlags() != oldParserKlass.getFlags()) {
        result = ClassChange.SCHEMA_CHANGE;
    }
    collectedChanges.addCompatibleFields(compatibleFields);
    // detect changes to superclass and implemented interfaces
    Klass superKlass = oldKlass.getSuperKlass();
    if (!newParserKlass.getSuperKlass().equals(oldParserKlass.getSuperKlass())) {
        result = ClassChange.CLASS_HIERARCHY_CHANGED;
        superKlass = getLoadedKlass(newParserKlass.getSuperKlass(), oldKlass);
    }
    collectedChanges.addSuperKlass((ObjectKlass) superKlass);
    ObjectKlass[] newSuperInterfaces = oldKlass.getSuperInterfaces();
    if (!Arrays.equals(newParserKlass.getSuperInterfaces(), oldParserKlass.getSuperInterfaces())) {
        result = ClassChange.CLASS_HIERARCHY_CHANGED;
        newSuperInterfaces = new ObjectKlass[newParserKlass.getSuperInterfaces().length];
        for (int i = 0; i < newParserKlass.getSuperInterfaces().length; i++) {
            newSuperInterfaces[i] = (ObjectKlass) getLoadedKlass(newParserKlass.getSuperInterfaces()[i], oldKlass);
        }
    }
    collectedChanges.addSuperInterfaces(newSuperInterfaces);
    return result;
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) RedefineAddedField(com.oracle.truffle.espresso.impl.RedefineAddedField) Field(com.oracle.truffle.espresso.impl.Field) ParserField(com.oracle.truffle.espresso.impl.ParserField) ParserKlass(com.oracle.truffle.espresso.impl.ParserKlass) ParserMethod(com.oracle.truffle.espresso.impl.ParserMethod) ParserMethod(com.oracle.truffle.espresso.impl.ParserMethod) Method(com.oracle.truffle.espresso.impl.Method) ParserField(com.oracle.truffle.espresso.impl.ParserField) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ParserKlass(com.oracle.truffle.espresso.impl.ParserKlass) HashMap(java.util.HashMap) EconomicMap(org.graalvm.collections.EconomicMap) Map(java.util.Map)

Example 58 with Field

use of com.oracle.truffle.espresso.impl.Field in project graal by oracle.

the class EspressoInterop method isMemberReadable.

@ExportMessage
static boolean isMemberReadable(StaticObject receiver, String member, @Cached @Exclusive LookupInstanceFieldNode lookupField) {
    receiver.checkNotForeign();
    Field f = lookupField.execute(getInteropKlass(receiver), member);
    if (f != null) {
        return true;
    }
    return // 
    notNull(receiver) && receiver.getKlass() == receiver.getKlass().getMeta().java_lang_Class && (CLASS_TO_STATIC.equals(member) || STATIC_TO_CLASS.equals(member));
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Example 59 with Field

use of com.oracle.truffle.espresso.impl.Field in project graal by oracle.

the class EspressoInterop method getMembers.

@ExportMessage
@TruffleBoundary
static Object getMembers(StaticObject receiver, @SuppressWarnings("unused") boolean includeInternal) {
    receiver.checkNotForeign();
    if (isNull(receiver)) {
        return EmptyKeysArray.INSTANCE;
    }
    ArrayList<String> members = new ArrayList<>();
    if (receiver.getKlass() == receiver.getKlass().getMeta().java_lang_Class) {
        // SVM does not like ArrayList.addAll(). Do manual copy.
        for (String s : CLASS_KEYS) {
            members.add(s);
        }
    }
    ObjectKlass k = getInteropKlass(receiver);
    for (Field f : k.getFieldTable()) {
        if (f.isPublic() && !f.isRemoved()) {
            members.add(f.getNameAsString());
        }
    }
    for (Method.MethodVersion m : k.getVTable()) {
        if (LookupVirtualMethodNode.isCandidate(m.getMethod())) {
            // Note: If there are overloading, the same key may appear twice.
            // TODO: Cache the keys array in the Klass.
            members.add(m.getMethod().getInteropString());
        }
    }
    // SVM does not like ArrayList.toArray(). Do manual copy.
    String[] array = new String[members.size()];
    int pos = 0;
    for (String str : members) {
        array[pos++] = str;
    }
    return new KeysArray(array);
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) ArrayList(java.util.ArrayList) EmptyKeysArray(com.oracle.truffle.espresso.impl.EmptyKeysArray) KeysArray(com.oracle.truffle.espresso.impl.KeysArray) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) Method(com.oracle.truffle.espresso.impl.Method) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 60 with Field

use of com.oracle.truffle.espresso.impl.Field in project graal by oracle.

the class EspressoInterop method isMemberModifiable.

@ExportMessage
static boolean isMemberModifiable(StaticObject receiver, String member, @Cached @Exclusive LookupInstanceFieldNode lookup) {
    receiver.checkNotForeign();
    Field f = lookup.execute(getInteropKlass(receiver), member);
    if (f != null) {
        return !f.isFinalFlagSet();
    }
    return false;
}
Also used : Field(com.oracle.truffle.espresso.impl.Field) ExportMessage(com.oracle.truffle.api.library.ExportMessage)

Aggregations

Field (com.oracle.truffle.espresso.impl.Field)60 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)14 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)8 Klass (com.oracle.truffle.espresso.impl.Klass)7 Method (com.oracle.truffle.espresso.impl.Method)5 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)5 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)5 ExportMessage (com.oracle.truffle.api.library.ExportMessage)4 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)4 Meta (com.oracle.truffle.espresso.meta.Meta)4 ArrayList (java.util.ArrayList)4 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)3 ParserField (com.oracle.truffle.espresso.impl.ParserField)3 REF_getField (com.oracle.truffle.espresso.classfile.Constants.REF_getField)2 REF_putField (com.oracle.truffle.espresso.classfile.Constants.REF_putField)2 Type (com.oracle.truffle.espresso.descriptors.Symbol.Type)2 NativeType (com.oracle.truffle.espresso.ffi.NativeType)2 ParserKlass (com.oracle.truffle.espresso.impl.ParserKlass)2 ParserMethod (com.oracle.truffle.espresso.impl.ParserMethod)2 RedefineAddedField (com.oracle.truffle.espresso.impl.RedefineAddedField)2