Search in sources :

Example 6 with RVMType

use of org.jikesrvm.classloader.RVMType in project JikesRVM by JikesRVM.

the class JNIFunctions method RegisterNatives.

/**
 * RegisterNatives: registers implementation of native methods
 * @param env A JREF index for the JNI environment object
 * @param classJREF a JREF index for the class to register native methods in
 * @param methodsAddress the address of an array of native methods to be registered
 * @param nmethods the number of native methods in the array
 * @return 0 is successful -1 if failed
 * @throws NoSuchMethodError if a specified method cannot be found or is not native
 */
private static int RegisterNatives(JNIEnvironment env, int classJREF, Address methodsAddress, int nmethods) {
    if (traceJNI)
        VM.sysWriteln("JNI called: RegisterNatives");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        // get the target class
        Class<?> jcls = (Class<?>) env.getJNIRef(classJREF);
        RVMType type = java.lang.JikesRVMSupport.getTypeForClass(jcls);
        if (!type.isClassType()) {
            env.recordException(new NoSuchMethodError());
            return 0;
        }
        RVMClass klass = type.asClass();
        if (!klass.isInitialized()) {
            RuntimeEntrypoints.initializeClassForDynamicLink(klass);
        }
        // Create list of methods and verify them to avoid partial success
        NativeMethod[] methods = new NativeMethod[nmethods];
        AddressArray symbols = AddressArray.create(nmethods);
        Address curMethod = methodsAddress;
        for (int i = 0; i < nmethods; i++) {
            String methodString = JNIGenericHelpers.createStringFromC(curMethod.loadAddress());
            Atom methodName = Atom.findOrCreateAsciiAtom(methodString);
            String sigString = JNIGenericHelpers.createStringFromC(curMethod.loadAddress(Offset.fromIntSignExtend(BYTES_IN_ADDRESS)));
            Atom sigName = Atom.findOrCreateAsciiAtom(sigString);
            // Find the target method
            RVMMethod meth = klass.findDeclaredMethod(methodName, sigName);
            if (meth == null || !meth.isNative()) {
                env.recordException(new NoSuchMethodError(klass + ": " + methodName + " " + sigName));
                return -1;
            }
            methods[i] = (NativeMethod) meth;
            symbols.set(i, curMethod.loadAddress(Offset.fromIntSignExtend(BYTES_IN_ADDRESS * 2)));
            curMethod = curMethod.plus(3 * BYTES_IN_ADDRESS);
        }
        // Register methods
        for (int i = 0; i < nmethods; i++) {
            methods[i].registerNativeSymbol(symbols.get(i));
        }
        return 0;
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return -1;
    }
}
Also used : AddressArray(org.vmmagic.unboxed.AddressArray) Address(org.vmmagic.unboxed.Address) RVMType(org.jikesrvm.classloader.RVMType) Atom(org.jikesrvm.classloader.Atom) RVMClass(org.jikesrvm.classloader.RVMClass) RVMMethod(org.jikesrvm.classloader.RVMMethod) NativeMethod(org.jikesrvm.classloader.NativeMethod) RVMClass(org.jikesrvm.classloader.RVMClass)

Example 7 with RVMType

use of org.jikesrvm.classloader.RVMType in project JikesRVM by JikesRVM.

the class JNIFunctions method GetStaticMethodID.

/**
 * GetStaticMethodID:  return the method ID for invocation later
 * @param env A JREF index for the JNI environment object
 * @param classJREF a JREF index for the class object
 * @param methodNameAddress a raw address to a null-terminated string in C for the method name
 * @param methodSigAddress a raw address to a null-terminated string in C for (TODO: document me)
 * @return a method ID or null if it fails
 * @throws NoSuchMethodError if the method is not found
 * @throws ExceptionInInitializerError if the initializer fails
 * @throws OutOfMemoryError if the system runs out of memory
 */
private static int GetStaticMethodID(JNIEnvironment env, int classJREF, Address methodNameAddress, Address methodSigAddress) {
    if (traceJNI)
        VM.sysWriteln("JNI called: GetStaticMethodID");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        // obtain the names as String from the native space
        String methodString = JNIGenericHelpers.createStringFromC(methodNameAddress);
        Atom methodName = Atom.findOrCreateAsciiAtom(methodString);
        String sigString = JNIGenericHelpers.createStringFromC(methodSigAddress);
        Atom sigName = Atom.findOrCreateAsciiAtom(sigString);
        // get the target class
        Class<?> jcls = (Class<?>) env.getJNIRef(classJREF);
        RVMType type = java.lang.JikesRVMSupport.getTypeForClass(jcls);
        if (!type.isClassType()) {
            env.recordException(new NoSuchMethodError());
            return 0;
        }
        RVMClass klass = type.asClass();
        if (!klass.isInitialized()) {
            RuntimeEntrypoints.initializeClassForDynamicLink(klass);
        }
        // Find the target method
        RVMMethod meth = klass.findStaticMethod(methodName, sigName);
        if (meth == null) {
            env.recordException(new NoSuchMethodError());
            return 0;
        }
        if (traceJNI)
            VM.sysWriteln("got method " + meth);
        return meth.getId();
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return 0;
    }
}
Also used : RVMMethod(org.jikesrvm.classloader.RVMMethod) RVMType(org.jikesrvm.classloader.RVMType) RVMClass(org.jikesrvm.classloader.RVMClass) Atom(org.jikesrvm.classloader.Atom) RVMClass(org.jikesrvm.classloader.RVMClass)

Example 8 with RVMType

use of org.jikesrvm.classloader.RVMType in project JikesRVM by JikesRVM.

the class JNIFunctions method GetMethodID.

/**
 * GetMethodID:  get the virtual method ID given the name and the signature
 * @param env A JREF index for the JNI environment object
 * @param classJREF a JREF index for the class object
 * @param methodNameAddress a raw address to a null-terminated string in C for the method name
 * @param methodSigAddress a raw address to a null-terminated string in C for the method signature
 * @return id of a MethodReference
 * @throws NoSuchMethodError if the method cannot be found
 * @throws ExceptionInInitializerError if the class or interface static initializer fails
 * @throws OutOfMemoryError if the system runs out of memory
 */
private static int GetMethodID(JNIEnvironment env, int classJREF, Address methodNameAddress, Address methodSigAddress) {
    if (traceJNI)
        VM.sysWriteln("JNI called: GetMethodID");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        // obtain the names as String from the native space
        String methodString = JNIGenericHelpers.createStringFromC(methodNameAddress);
        Atom methodName = Atom.findOrCreateAsciiAtom(methodString);
        String sigString = JNIGenericHelpers.createStringFromC(methodSigAddress);
        Atom sigName = Atom.findOrCreateAsciiAtom(sigString);
        // get the target class
        Class<?> jcls = (Class<?>) env.getJNIRef(classJREF);
        RVMType type = java.lang.JikesRVMSupport.getTypeForClass(jcls);
        if (!type.isClassType()) {
            env.recordException(new NoSuchMethodError());
            return 0;
        }
        RVMClass klass = type.asClass();
        if (!klass.isInitialized()) {
            RuntimeEntrypoints.initializeClassForDynamicLink(klass);
        }
        // Find the target method
        final RVMMethod meth;
        if (methodString.equals("<init>")) {
            meth = klass.findInitializerMethod(sigName);
        } else {
            meth = klass.findVirtualMethod(methodName, sigName);
        }
        if (meth == null) {
            env.recordException(new NoSuchMethodError(klass + ": " + methodName + " " + sigName));
            return 0;
        }
        if (traceJNI)
            VM.sysWriteln("got method " + meth);
        return meth.getId();
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return 0;
    }
}
Also used : RVMMethod(org.jikesrvm.classloader.RVMMethod) RVMType(org.jikesrvm.classloader.RVMType) RVMClass(org.jikesrvm.classloader.RVMClass) Atom(org.jikesrvm.classloader.Atom) RVMClass(org.jikesrvm.classloader.RVMClass)

Example 9 with RVMType

use of org.jikesrvm.classloader.RVMType in project JikesRVM by JikesRVM.

the class JNIFunctions method UnregisterNatives.

/**
 * UnregisterNatives: unregisters native methods
 * @param env A JREF index for the JNI environment object
 * @param classJREF a JREF index for the class to register native methods in
 * @return 0 is successful -1 if failed
 */
private static int UnregisterNatives(JNIEnvironment env, int classJREF) {
    if (traceJNI)
        VM.sysWriteln("JNI called: UnregisterNatives");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        // get the target class
        Class<?> jcls = (Class<?>) env.getJNIRef(classJREF);
        RVMType type = java.lang.JikesRVMSupport.getTypeForClass(jcls);
        if (!type.isClassType()) {
            env.recordException(new NoClassDefFoundError());
            return -1;
        }
        RVMClass klass = type.asClass();
        if (!klass.isInitialized()) {
            return 0;
        }
        klass.unregisterNativeMethods();
        return 0;
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return -1;
    }
}
Also used : RVMType(org.jikesrvm.classloader.RVMType) RVMClass(org.jikesrvm.classloader.RVMClass) RVMClass(org.jikesrvm.classloader.RVMClass)

Example 10 with RVMType

use of org.jikesrvm.classloader.RVMType in project JikesRVM by JikesRVM.

the class DebugUtil method dumpRef.

@Uninterruptible
public static void dumpRef(ObjectReference ref) {
    VM.sysWrite("REF=");
    if (ref.isNull()) {
        VM.sysWriteln("NULL");
        VM.sysWriteln();
        return;
    }
    VM.sysWrite(ref);
    if (!mappedVMRef(ref)) {
        VM.sysWriteln(" (REF OUTSIDE OF HEAP OR NOT MAPPED)");
        return;
    }
    ObjectModel.dumpHeader(ref);
    ObjectReference tib = ObjectReference.fromObject(ObjectModel.getTIB(ref));
    if (!MemoryManager.mightBeTIB(tib)) {
        VM.sysWriteln(" (INVALID TIB: CLASS NOT ACCESSIBLE)");
        return;
    }
    RVMType type = Magic.getObjectType(ref.toObject());
    ObjectReference itype = ObjectReference.fromObject(type);
    VM.sysWrite(" TYPE=");
    VM.sysWrite(itype);
    if (!validType(itype)) {
        VM.sysWriteln(" (INVALID TYPE: CLASS NOT ACCESSIBLE)");
        return;
    }
    VM.sysWrite(" CLASS=");
    VM.sysWrite(type.getDescriptor());
    VM.sysWriteln();
}
Also used : ObjectReference(org.vmmagic.unboxed.ObjectReference) RVMType(org.jikesrvm.classloader.RVMType) Uninterruptible(org.vmmagic.pragma.Uninterruptible)

Aggregations

RVMType (org.jikesrvm.classloader.RVMType)77 RVMClass (org.jikesrvm.classloader.RVMClass)23 TypeReference (org.jikesrvm.classloader.TypeReference)18 Address (org.vmmagic.unboxed.Address)16 RVMMethod (org.jikesrvm.classloader.RVMMethod)15 RegisterOperand (org.jikesrvm.compilers.opt.ir.operand.RegisterOperand)15 TIB (org.jikesrvm.objectmodel.TIB)13 Entrypoint (org.vmmagic.pragma.Entrypoint)13 IntConstantOperand (org.jikesrvm.compilers.opt.ir.operand.IntConstantOperand)12 Operand (org.jikesrvm.compilers.opt.ir.operand.Operand)12 RVMArray (org.jikesrvm.classloader.RVMArray)11 ConditionOperand (org.jikesrvm.compilers.opt.ir.operand.ConditionOperand)11 MethodOperand (org.jikesrvm.compilers.opt.ir.operand.MethodOperand)11 BranchProfileOperand (org.jikesrvm.compilers.opt.ir.operand.BranchProfileOperand)10 NullConstantOperand (org.jikesrvm.compilers.opt.ir.operand.NullConstantOperand)10 TrapCodeOperand (org.jikesrvm.compilers.opt.ir.operand.TrapCodeOperand)10 TrueGuardOperand (org.jikesrvm.compilers.opt.ir.operand.TrueGuardOperand)10 Instruction (org.jikesrvm.compilers.opt.ir.Instruction)9 TypeOperand (org.jikesrvm.compilers.opt.ir.operand.TypeOperand)9 Offset (org.vmmagic.unboxed.Offset)8