Search in sources :

Example 6 with RuntimeConstantPool

use of com.oracle.truffle.espresso.classfile.RuntimeConstantPool in project graal by oracle.

the class VM method computeEnclosingClass.

/**
 * Return the enclosing class; or null for: primitives, arrays, anonymous classes (declared
 * inside methods).
 */
private static Klass computeEnclosingClass(ObjectKlass klass) {
    InnerClassesAttribute innerClasses = (InnerClassesAttribute) klass.getAttribute(InnerClassesAttribute.NAME);
    if (innerClasses == null) {
        return null;
    }
    RuntimeConstantPool pool = klass.getConstantPool();
    boolean found = false;
    Klass outerKlass = null;
    for (InnerClassesAttribute.Entry entry : innerClasses.entries()) {
        if (entry.innerClassIndex != 0) {
            Symbol<Name> innerDescriptor = pool.classAt(entry.innerClassIndex).getName(pool);
            // Check decriptors/names before resolving.
            if (innerDescriptor.equals(klass.getName())) {
                Klass innerKlass = pool.resolvedKlassAt(klass, entry.innerClassIndex);
                found = (innerKlass == klass);
                if (found && entry.outerClassIndex != 0) {
                    outerKlass = pool.resolvedKlassAt(klass, entry.outerClassIndex);
                }
            }
        }
        if (found) {
            break;
        }
    }
    // the system could allow a spoof of an inner class to gain access rights.
    return outerKlass;
}
Also used : RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) InnerClassesAttribute(com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name)

Example 7 with RuntimeConstantPool

use of com.oracle.truffle.espresso.classfile.RuntimeConstantPool in project graal by oracle.

the class VM method JVM_GetDeclaredClasses.

@VmImpl(isJni = true)
@JavaType(Class[].class)
public StaticObject JVM_GetDeclaredClasses(@JavaType(Class.class) StaticObject self) {
    Meta meta = getMeta();
    Klass klass = self.getMirrorKlass();
    if (klass.isPrimitive() || klass.isArray()) {
        return meta.java_lang_Class.allocateReferenceArray(0);
    }
    ObjectKlass instanceKlass = (ObjectKlass) klass;
    InnerClassesAttribute innerClasses = (InnerClassesAttribute) instanceKlass.getAttribute(InnerClassesAttribute.NAME);
    if (innerClasses == null || innerClasses.entries().length == 0) {
        return meta.java_lang_Class.allocateReferenceArray(0);
    }
    RuntimeConstantPool pool = instanceKlass.getConstantPool();
    List<Klass> innerKlasses = new ArrayList<>();
    for (InnerClassesAttribute.Entry entry : innerClasses.entries()) {
        if (entry.innerClassIndex != 0 && entry.outerClassIndex != 0) {
            // Check to see if the name matches the class we're looking for
            // before attempting to find the class.
            Symbol<Name> outerDescriptor = pool.classAt(entry.outerClassIndex).getName(pool);
            // Check decriptors/names before resolving.
            if (outerDescriptor.equals(instanceKlass.getName())) {
                Klass outerKlass = pool.resolvedKlassAt(instanceKlass, entry.outerClassIndex);
                if (outerKlass == instanceKlass) {
                    Klass innerKlass = pool.resolvedKlassAt(instanceKlass, entry.innerClassIndex);
                    // HotSpot:
                    // Throws an exception if outer klass has not declared k as
                    // an inner klass
                    // Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
                    // TODO(peterssen): The check in HotSpot is redundant.
                    innerKlasses.add(innerKlass);
                }
            }
        }
    }
    return meta.java_lang_Class.allocateReferenceArray(innerKlasses.size(), new IntFunction<StaticObject>() {

        @Override
        public StaticObject apply(int index) {
            return innerKlasses.get(index).mirror();
        }
    });
}
Also used : Meta(com.oracle.truffle.espresso.meta.Meta) ArrayList(java.util.ArrayList) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) Name(com.oracle.truffle.espresso.descriptors.Symbol.Name) Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) InnerClassesAttribute(com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute) JavaType(com.oracle.truffle.espresso.substitutions.JavaType)

Example 8 with RuntimeConstantPool

use of com.oracle.truffle.espresso.classfile.RuntimeConstantPool in project graal by oracle.

the class VM method JVM_GetPermittedSubclasses.

@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(Class[].class)
public StaticObject JVM_GetPermittedSubclasses(@JavaType(Class.class) StaticObject self) {
    Klass k = self.getMirrorKlass();
    if (!(k instanceof ObjectKlass)) {
        return StaticObject.NULL;
    }
    ObjectKlass klass = (ObjectKlass) k;
    if (!klass.isSealed()) {
        return StaticObject.NULL;
    }
    char[] classes = ((PermittedSubclassesAttribute) klass.getAttribute(PermittedSubclassesAttribute.NAME)).getClasses();
    StaticObject[] permittedSubclasses = new StaticObject[classes.length];
    RuntimeConstantPool pool = klass.getConstantPool();
    int nClasses = 0;
    for (int index : classes) {
        Klass permitted;
        try {
            permitted = pool.resolvedKlassAt(klass, index);
        } catch (EspressoException e) {
            /* Suppress and continue */
            continue;
        }
        if (permitted instanceof ObjectKlass) {
            permittedSubclasses[nClasses++] = permitted.mirror();
        }
    }
    if (nClasses == permittedSubclasses.length) {
        return StaticObject.createArray(getMeta().java_lang_Class_array, permittedSubclasses);
    }
    return getMeta().java_lang_Class.allocateReferenceArray(nClasses, (i) -> permittedSubclasses[i]);
}
Also used : Klass(com.oracle.truffle.espresso.impl.Klass) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) EspressoException(com.oracle.truffle.espresso.runtime.EspressoException) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) PermittedSubclassesAttribute(com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute) ObjectKlass(com.oracle.truffle.espresso.impl.ObjectKlass) NoSafepoint(com.oracle.truffle.espresso.jni.NoSafepoint) JavaType(com.oracle.truffle.espresso.substitutions.JavaType) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 9 with RuntimeConstantPool

use of com.oracle.truffle.espresso.classfile.RuntimeConstantPool in project graal by oracle.

the class BytecodeNode method putPoolConstant.

private void putPoolConstant(VirtualFrame frame, int top, char cpi, int opcode) {
    assert opcode == LDC || opcode == LDC_W || opcode == LDC2_W;
    RuntimeConstantPool pool = getConstantPool();
    PoolConstant constant = pool.at(cpi);
    if (constant instanceof IntegerConstant) {
        assert opcode == LDC || opcode == LDC_W;
        putInt(frame, top, ((IntegerConstant) constant).value());
    } else if (constant instanceof LongConstant) {
        assert opcode == LDC2_W;
        putLong(frame, top, ((LongConstant) constant).value());
    } else if (constant instanceof DoubleConstant) {
        assert opcode == LDC2_W;
        putDouble(frame, top, ((DoubleConstant) constant).value());
    } else if (constant instanceof FloatConstant) {
        assert opcode == LDC || opcode == LDC_W;
        putFloat(frame, top, ((FloatConstant) constant).value());
    } else if (constant instanceof StringConstant) {
        assert opcode == LDC || opcode == LDC_W;
        StaticObject internedString = pool.resolvedStringAt(cpi);
        putObject(frame, top, internedString);
    } else if (constant instanceof ClassConstant) {
        assert opcode == LDC || opcode == LDC_W;
        Klass klass = pool.resolvedKlassAt(getDeclaringKlass(), cpi);
        putObject(frame, top, klass.mirror());
    } else if (constant instanceof MethodHandleConstant) {
        assert opcode == LDC || opcode == LDC_W;
        StaticObject methodHandle = pool.resolvedMethodHandleAt(getDeclaringKlass(), cpi);
        putObject(frame, top, methodHandle);
    } else if (constant instanceof MethodTypeConstant) {
        assert opcode == LDC || opcode == LDC_W;
        StaticObject methodType = pool.resolvedMethodTypeAt(getDeclaringKlass(), cpi);
        putObject(frame, top, methodType);
    } else if (constant instanceof DynamicConstant) {
        DynamicConstant.Resolved dynamicConstant = pool.resolvedDynamicConstantAt(getDeclaringKlass(), cpi);
        dynamicConstant.putResolved(frame, top, this);
    } else {
        CompilerDirectives.transferToInterpreter();
        throw EspressoError.unimplemented(constant.toString());
    }
}
Also used : LongConstant(com.oracle.truffle.espresso.classfile.constantpool.LongConstant) DoubleConstant(com.oracle.truffle.espresso.classfile.constantpool.DoubleConstant) PoolConstant(com.oracle.truffle.espresso.classfile.constantpool.PoolConstant) FloatConstant(com.oracle.truffle.espresso.classfile.constantpool.FloatConstant) MethodHandleConstant(com.oracle.truffle.espresso.classfile.constantpool.MethodHandleConstant) IntegerConstant(com.oracle.truffle.espresso.classfile.constantpool.IntegerConstant) InvokeDynamicConstant(com.oracle.truffle.espresso.classfile.constantpool.InvokeDynamicConstant) DynamicConstant(com.oracle.truffle.espresso.classfile.constantpool.DynamicConstant) RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) Klass(com.oracle.truffle.espresso.impl.Klass) ArrayKlass(com.oracle.truffle.espresso.impl.ArrayKlass) StaticObject(com.oracle.truffle.espresso.runtime.StaticObject) StringConstant(com.oracle.truffle.espresso.classfile.constantpool.StringConstant) ClassConstant(com.oracle.truffle.espresso.classfile.constantpool.ClassConstant) MethodTypeConstant(com.oracle.truffle.espresso.classfile.constantpool.MethodTypeConstant)

Example 10 with RuntimeConstantPool

use of com.oracle.truffle.espresso.classfile.RuntimeConstantPool in project graal by oracle.

the class BytecodeNode method quickenInvokeDynamic.

private int quickenInvokeDynamic(final VirtualFrame frame, int top, int curBCI, int opcode) {
    CompilerDirectives.transferToInterpreterAndInvalidate();
    assert (Bytecodes.INVOKEDYNAMIC == opcode);
    RuntimeConstantPool pool = getConstantPool();
    BaseQuickNode quick = null;
    int indyIndex = -1;
    synchronized (this) {
        if (bs.currentVolatileBC(curBCI) == QUICK) {
            // Check if someone did the job for us. Defer the call until we are out of the lock.
            quick = nodes[readCPI(curBCI)];
        } else {
            // fetch indy under lock.
            indyIndex = readCPI(curBCI);
        }
    }
    if (quick != null) {
        // Do invocation outside of the lock.
        return quick.execute(frame) - Bytecodes.stackEffectOf(opcode);
    }
    // Resolution should happen outside of the bytecode patching lock.
    InvokeDynamicConstant.CallSiteLink link = pool.linkInvokeDynamic(getMethod().getDeclaringKlass(), indyIndex);
    // re-lock to check if someone did the job for us, since this was a heavy operation.
    synchronized (this) {
        if (bs.currentVolatileBC(curBCI) == QUICK) {
            // someone beat us to it, just trust him.
            quick = nodes[readCPI(curBCI)];
        } else {
            quick = injectQuick(curBCI, new InvokeDynamicCallSiteNode(link.getMemberName(), link.getUnboxedAppendix(), link.getParsedSignature(), getMeta(), top, curBCI), QUICK);
        }
    }
    return quick.execute(frame) - Bytecodes.stackEffectOf(opcode);
}
Also used : RuntimeConstantPool(com.oracle.truffle.espresso.classfile.RuntimeConstantPool) BaseQuickNode(com.oracle.truffle.espresso.nodes.quick.BaseQuickNode) InvokeDynamicConstant(com.oracle.truffle.espresso.classfile.constantpool.InvokeDynamicConstant) TruffleSafepoint(com.oracle.truffle.api.TruffleSafepoint) InvokeDynamicCallSiteNode(com.oracle.truffle.espresso.nodes.quick.invoke.InvokeDynamicCallSiteNode)

Aggregations

RuntimeConstantPool (com.oracle.truffle.espresso.classfile.RuntimeConstantPool)13 ArrayKlass (com.oracle.truffle.espresso.impl.ArrayKlass)6 Klass (com.oracle.truffle.espresso.impl.Klass)6 ObjectKlass (com.oracle.truffle.espresso.impl.ObjectKlass)5 NoSafepoint (com.oracle.truffle.espresso.jni.NoSafepoint)4 StaticObject (com.oracle.truffle.espresso.runtime.StaticObject)4 JavaType (com.oracle.truffle.espresso.substitutions.JavaType)4 InnerClassesAttribute (com.oracle.truffle.espresso.classfile.attributes.InnerClassesAttribute)3 Name (com.oracle.truffle.espresso.descriptors.Symbol.Name)3 Meta (com.oracle.truffle.espresso.meta.Meta)3 NestMembersAttribute (com.oracle.truffle.espresso.classfile.attributes.NestMembersAttribute)2 PermittedSubclassesAttribute (com.oracle.truffle.espresso.classfile.attributes.PermittedSubclassesAttribute)2 InvokeDynamicConstant (com.oracle.truffle.espresso.classfile.constantpool.InvokeDynamicConstant)2 EspressoException (com.oracle.truffle.espresso.runtime.EspressoException)2 ArrayList (java.util.ArrayList)2 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 TruffleSafepoint (com.oracle.truffle.api.TruffleSafepoint)1 CodeAttribute (com.oracle.truffle.espresso.classfile.attributes.CodeAttribute)1 EnclosingMethodAttribute (com.oracle.truffle.espresso.classfile.attributes.EnclosingMethodAttribute)1 NestHostAttribute (com.oracle.truffle.espresso.classfile.attributes.NestHostAttribute)1