use of com.oracle.truffle.espresso.descriptors.Symbol.Signature in project graal by oracle.
the class JniEnv method GetMethodID.
/**
* <h3>jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig);</h3>
* <p>
* Returns the method ID for an instance (nonstatic) method of a class or interface. The method
* may be defined in one of the clazz’s superclasses and inherited by clazz. The method is
* determined by its name and signature.
* <p>
* GetMethodID() causes an uninitialized class to be initialized.
* <p>
* To obtain the method ID of a constructor, supply <init> as the method name and void (V) as
* the return type.
*
* @param clazz a Java class object.
* @param namePtr the method name in a 0-terminated modified UTF-8 string.
* @param signaturePtr the method signature in 0-terminated modified UTF-8 string.
* @return a method ID, or NULL if the specified method cannot be found.
* @throws NoSuchMethodError if the specified method cannot be found.
* @throws ExceptionInInitializerError if the class initializer fails due to an exception.
* @throws OutOfMemoryError if the system runs out of memory.
*/
@JniImpl
@Handle(Method.class)
public long GetMethodID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject signaturePtr) {
String name = NativeUtils.interopPointerToString(namePtr);
String signature = NativeUtils.interopPointerToString(signaturePtr);
assert name != null && signature != null;
Method method = null;
Symbol<Name> methodName = getNames().lookup(name);
if (methodName != null) {
Symbol<Signature> methodSignature = getSignatures().lookupValidSignature(signature);
if (methodSignature != null) {
Klass klass = clazz.getMirrorKlass();
klass.safeInitialize();
// Lookup only if name and type are known symbols.
method = klass.lookupMethod(methodName, methodSignature, klass);
}
}
if (method == null || method.isStatic()) {
Meta meta = getMeta();
throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
}
return methodIds.handlify(method);
}
use of com.oracle.truffle.espresso.descriptors.Symbol.Signature in project graal by oracle.
the class JniEnv method RegisterNative.
// endregion DirectBuffers
// region Register/Unregister natives
@JniImpl
@TruffleBoundary
public int RegisterNative(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject methodNamePtr, @Pointer TruffleObject methodSignaturePtr, @Pointer TruffleObject closure) {
String methodName = NativeUtils.interopPointerToString(methodNamePtr);
String methodSignature = NativeUtils.interopPointerToString(methodSignaturePtr);
assert methodName != null && methodSignature != null;
Symbol<Name> name = getNames().lookup(methodName);
Symbol<Signature> signature = getSignatures().lookupValidSignature(methodSignature);
Meta meta = getMeta();
if (name == null || signature == null) {
setPendingException(Meta.initException(meta.java_lang_NoSuchMethodError));
return JNI_ERR;
}
Method targetMethod = clazz.getMirrorKlass().lookupDeclaredMethod(name, signature);
if (targetMethod != null && targetMethod.isNative()) {
targetMethod.unregisterNative();
getSubstitutions().removeRuntimeSubstitution(targetMethod);
} else {
setPendingException(Meta.initException(meta.java_lang_NoSuchMethodError));
return JNI_ERR;
}
Substitutions.EspressoRootNodeFactory factory = null;
// Lookup known VM methods to shortcut native boudaries.
factory = lookupKnownVmMethods(closure, targetMethod);
if (factory == null) {
NativeSignature ns = Method.buildJniNativeSignature(targetMethod.getParsedSignature());
final TruffleObject boundNative = getNativeAccess().bindSymbol(closure, ns);
factory = createJniRootNodeFactory(() -> new NativeMethodNode(boundNative, targetMethod.getMethodVersion()), targetMethod);
}
Symbol<Type> classType = clazz.getMirrorKlass().getType();
getSubstitutions().registerRuntimeSubstitution(classType, name, signature, factory, true);
return JNI_OK;
}
use of com.oracle.truffle.espresso.descriptors.Symbol.Signature in project graal by oracle.
the class MethodVerifier method verifyInvokeInterface.
private void verifyInvokeInterface(int bci, OperandStack stack) {
// Check padding.
verifyGuarantee(code.readUByte(bci + 4) == 0, "4th byte after INVOKEINTERFACE must be 0.");
// Check CP validity
MethodRefConstant mrc = getMethodRefConstant(bci);
// Checks versioning
Symbol<Name> calledMethodName = mrc.getName(pool);
// Check guest is not invoking <clinit>
verifyGuarantee(!isClassInit(calledMethodName), "Invocation of class initializer!");
// Only INVOKESPECIAL can call <init>
verifyGuarantee(!isInstanceInit(calledMethodName), "Invocation of instance initializer with opcode other than INVOKESPECIAL");
Symbol<Signature> calledMethodSignature = mrc.getSignature(pool);
Operand[] parsedSig = getOperandSig(calledMethodSignature);
// Check signature is well formed.
assert parsedSig.length > 0 : "Method ref with no return value !";
// Pop arguments
// Check signature conforms with count argument
int count = code.readUByte(bci + 3);
verifyGuarantee(count > 0, "Invalid count argument for INVOKEINTERFACE: " + count);
// Has a receiver.
int descCount = 1;
for (int i = parsedSig.length - 2; i >= 0; i--) {
descCount++;
if (isType2(parsedSig[i])) {
descCount++;
}
stack.pop(parsedSig[i]);
}
verifyGuarantee(count == descCount, "Inconsistent redundant argument count for INVOKEINTERFACE.");
assert Validation.validClassNameEntry(mrc.getHolderKlassName(pool));
Symbol<Type> methodHolder = getTypes().fromName(mrc.getHolderKlassName(pool));
Operand methodHolderOp = kindToOperand(methodHolder);
checkInit(stack.popRef(methodHolderOp));
Operand returnOp = parsedSig[parsedSig.length - 1];
if (!(returnOp == Void)) {
stack.push(returnOp);
}
}
use of com.oracle.truffle.espresso.descriptors.Symbol.Signature in project graal by oracle.
the class Substitutions method registerStaticSubstitution.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void registerStaticSubstitution(JavaSubstitution.Factory substitutorFactory) {
List<Symbol<Type>> parameterTypes = new ArrayList<>();
for (int i = substitutorFactory.hasReceiver() ? 1 : 0; i < substitutorFactory.parameterTypes().length; i++) {
String type = substitutorFactory.parameterTypes()[i];
parameterTypes.add(StaticSymbols.putType(type));
}
Symbol<Type> returnType = StaticSymbols.putType(substitutorFactory.returnType());
Symbol<Signature> signature = StaticSymbols.putSignature(returnType, parameterTypes.toArray(Symbol.EMPTY_ARRAY));
EspressoRootNodeFactory factory = new EspressoRootNodeFactory() {
@Override
public EspressoRootNode createNodeIfValid(Method methodToSubstitute, boolean forceValid) {
if (!substitutorFactory.isValidFor(methodToSubstitute.getJavaVersion())) {
return null;
}
StaticObject classLoader = methodToSubstitute.getDeclaringKlass().getDefiningClassLoader();
if (forceValid || ClassRegistry.loaderIsBootOrPlatform(classLoader, methodToSubstitute.getMeta())) {
return EspressoRootNode.create(null, new IntrinsicSubstitutorNode(substitutorFactory, methodToSubstitute));
}
getLogger().warning(new Supplier<String>() {
@Override
public String get() {
StaticObject givenLoader = methodToSubstitute.getDeclaringKlass().getDefiningClassLoader();
return "Static substitution for " + methodToSubstitute + " does not apply.\n" + "\tExpected class loader: Boot (null) or platform class loader\n" + "\tGiven class loader: " + EspressoInterop.toDisplayString(givenLoader, false) + "\n";
}
});
return null;
}
};
String[] classNames = substitutorFactory.substitutionClassNames();
String[] methodNames = substitutorFactory.getMethodNames();
for (int i = 0; i < classNames.length; i++) {
assert classNames[i].startsWith("Target_");
Symbol<Type> classType = StaticSymbols.putType("L" + classNames[i].substring("Target_".length()).replace('_', '/') + ";");
Symbol<Name> methodName = StaticSymbols.putName(methodNames[i]);
registerStaticSubstitution(classType, methodName, signature, factory, true);
}
}
use of com.oracle.truffle.espresso.descriptors.Symbol.Signature in project graal by oracle.
the class JniEnv method GetStaticMethodID.
/**
* <h3>jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const char
* *sig);</h3>
* <p>
* Returns the method ID for a static method of a class. The method is specified by its name and
* signature.
* <p>
* GetStaticMethodID() causes an uninitialized class to be initialized.
*
* @param clazz a Java class object.
* @param namePtr the static method name in a 0-terminated modified UTF-8 string.
* @param signaturePtr the method signature in a 0-terminated modified UTF-8 string.
* @return a method ID, or NULL if the operation fails.
* @throws NoSuchMethodError if the specified static method cannot be found. *
* @throws ExceptionInInitializerError if the class initializer fails due to an exception.
* @throws OutOfMemoryError if the system runs out of memory.
*/
@JniImpl
@Handle(Method.class)
public long GetStaticMethodID(@JavaType(Class.class) StaticObject clazz, @Pointer TruffleObject namePtr, @Pointer TruffleObject signaturePtr) {
String name = NativeUtils.interopPointerToString(namePtr);
String signature = NativeUtils.interopPointerToString(signaturePtr);
assert name != null && signature != null;
Method method = null;
Symbol<Name> methodName = getNames().lookup(name);
if (methodName != null) {
Symbol<Signature> methodSignature = getSignatures().lookupValidSignature(signature);
if (methodSignature != null) {
// Throw a NoSuchMethodError exception if we have an instance of a
// primitive java.lang.Class
Klass klass = clazz.getMirrorKlass();
if (klass.isPrimitive()) {
Meta meta = getMeta();
throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
}
klass.safeInitialize();
// Lookup only if name and type are known symbols.
if (Name._clinit_.equals(methodName)) {
// Never search superclasses for static initializers.
method = klass.lookupDeclaredMethod(methodName, methodSignature);
} else {
method = klass.lookupMethod(methodName, methodSignature);
}
}
}
if (method == null || !method.isStatic()) {
Meta meta = getMeta();
throw meta.throwExceptionWithMessage(meta.java_lang_NoSuchMethodError, name);
}
return methodIds.handlify(method);
}
Aggregations