Skip to content

Commit

Permalink
[GR-12479] NFI support object type with native-null semantics.
Browse files Browse the repository at this point in the history
PullRequest: graal/3685
  • Loading branch information
mukel committed Aug 22, 2019
2 parents a32ef18 + a283c13 commit 7b6657b
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static void initializeSimpleTypes(Target_com_oracle_truffle_nfi_impl_NFIContext

initializeNativeSimpleType(context, NativeSimpleType.STRING, ffi_type_pointer.get());
initializeNativeSimpleType(context, NativeSimpleType.OBJECT, ffi_type_pointer.get());
initializeNativeSimpleType(context, NativeSimpleType.NULLABLE, ffi_type_pointer.get());
}

static void initializeContext(NativeTruffleContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ private Object call(WordPointer argPointers, ByteBuffer retBuffer) {
} else if (Target_com_oracle_truffle_nfi_impl_LibFFIType_ObjectType.class.isInstance(type)) {
WordPointer argPtr = argPointers.read(i);
args[argIdx++] = ImageSingletons.lookup(TruffleNFISupport.class).resolveHandle(argPtr.read());
} else if (Target_com_oracle_truffle_nfi_impl_LibFFIType_NullableType.class.isInstance(type)) {
WordPointer argPtr = argPointers.read(i);
args[argIdx++] = ImageSingletons.lookup(TruffleNFISupport.class).resolveHandle(argPtr.read());
} else if (Target_com_oracle_truffle_nfi_impl_LibFFIType_EnvType.class.isInstance(type)) {
// skip
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class NativeObjectReplacer implements Function<Object, Object> {
private final IdentityHashMap<Class<?>, Object> disallowedClasses;

NativeObjectReplacer(DuringSetupAccess access) {
disallowedClasses = new IdentityHashMap<>(15);
disallowedClasses = new IdentityHashMap<>(16);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.ClosureNativePointer"), Boolean.FALSE);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.ClosureNativePointer$NativeDestructor"), Boolean.FALSE);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.LibFFILibrary"), Boolean.FALSE);
Expand All @@ -47,6 +47,7 @@ public final class NativeObjectReplacer implements Function<Object, Object> {
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.LibFFIType$ArrayType"), Boolean.FALSE);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.LibFFIType$ClosureType"), Boolean.FALSE);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.LibFFIType$EnvType"), Boolean.FALSE);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.LibFFIType$NullableType"), Boolean.FALSE);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.LibFFIType$ObjectType"), Boolean.FALSE);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.LibFFIType$SimpleType"), Boolean.FALSE);
disallowedClasses.put(access.findClassByName("com.oracle.truffle.nfi.impl.LibFFIType$StringType"), Boolean.FALSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ final class Target_com_oracle_truffle_nfi_impl_LibFFIType_StringType {
final class Target_com_oracle_truffle_nfi_impl_LibFFIType_ObjectType {
}

@TargetClass(className = "com.oracle.truffle.nfi.impl.LibFFIType", innerClass = "NullableType", onlyWith = TruffleNFIFeature.IsEnabled.class)
final class Target_com_oracle_truffle_nfi_impl_LibFFIType_NullableType {
}

@TargetClass(className = "com.oracle.truffle.nfi.impl.LibFFIType", innerClass = "EnvType", onlyWith = TruffleNFIFeature.IsEnabled.class)
final class Target_com_oracle_truffle_nfi_impl_LibFFIType_EnvType {
}
2 changes: 2 additions & 0 deletions truffle/src/com.oracle.truffle.nfi.native/src/closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ jobject prepare_closure(JNIEnv *env, jlong context, jobject signature, jobject c
data->argTypes[i] = ARG_STRING;
} else if ((*env)->IsInstanceOf(env, argType, ctx->LibFFIType_ObjectType)) {
data->argTypes[i] = ARG_OBJECT;
} else if ((*env)->IsInstanceOf(env, argType, ctx->LibFFIType_NullableType)) {
data->argTypes[i] = ARG_OBJECT;
} else if ((*env)->IsInstanceOf(env, argType, ctx->LibFFIType_EnvType)) {
data->argTypes[i] = ARG_SKIP;
data->skippedArgCount++;
Expand Down
1 change: 1 addition & 0 deletions truffle/src/com.oracle.truffle.nfi.native/src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct __TruffleContextInternal {
jfieldID LibFFIType_type;
jclass LibFFIType_EnvType;
jclass LibFFIType_ObjectType;
jclass LibFFIType_NullableType;
jclass LibFFIType_StringType;

jclass NativeString;
Expand Down
3 changes: 3 additions & 0 deletions truffle/src/com.oracle.truffle.nfi.native/src/jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ JNIEXPORT jlong JNICALL Java_com_oracle_truffle_nfi_impl_NFIContext_initializeNa
ret->LibFFIType_type = (*env)->GetFieldID(env, LibFFIType, "type", "J");
ret->LibFFIType_EnvType = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/oracle/truffle/nfi/impl/LibFFIType$EnvType"));
ret->LibFFIType_ObjectType = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/oracle/truffle/nfi/impl/LibFFIType$ObjectType"));
ret->LibFFIType_NullableType = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/oracle/truffle/nfi/impl/LibFFIType$NullableType"));
ret->LibFFIType_StringType = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/oracle/truffle/nfi/impl/LibFFIType$StringType"));

ret->NativeString = (jclass) (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/oracle/truffle/nfi/impl/NativeString"));
Expand Down Expand Up @@ -132,6 +133,7 @@ JNIEXPORT jlong JNICALL Java_com_oracle_truffle_nfi_impl_NFIContext_initializeNa

cacheFFIType(env, NativeSimpleType, context, initializeSimpleType, "STRING", &ffi_type_pointer);
cacheFFIType(env, NativeSimpleType, context, initializeSimpleType, "OBJECT", &ffi_type_pointer);
cacheFFIType(env, NativeSimpleType, context, initializeSimpleType, "NULLABLE", &ffi_type_pointer);

#if !defined(_WIN32)
initializeFlag(env, NFIContext, context, "RTLD_GLOBAL", RTLD_GLOBAL);
Expand All @@ -152,6 +154,7 @@ JNIEXPORT void JNICALL Java_com_oracle_truffle_nfi_impl_NFIContext_disposeNative

(*env)->DeleteGlobalRef(env, ctx->LibFFIType_EnvType);
(*env)->DeleteGlobalRef(env, ctx->LibFFIType_ObjectType);
(*env)->DeleteGlobalRef(env, ctx->LibFFIType_NullableType);
(*env)->DeleteGlobalRef(env, ctx->LibFFIType_StringType);

(*env)->DeleteGlobalRef(env, ctx->NativeString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ public enum NativeSimpleType {
DOUBLE,
POINTER,
STRING,
OBJECT;
OBJECT,
NULLABLE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.nfi.test;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.nfi.spi.types.NativeSimpleType;
import com.oracle.truffle.nfi.test.interop.NullObject;
import com.oracle.truffle.nfi.test.interop.TestCallback;
import com.oracle.truffle.tck.TruffleRunner;
import com.oracle.truffle.tck.TruffleRunner.Inject;

@RunWith(TruffleRunner.class)
public class NullableNFITest extends NFITest {

public class TestNullableArgNode extends SendExecuteNode {

public TestNullableArgNode() {
super("null_arg", String.format("(%s) : string", NativeSimpleType.NULLABLE));
}
}

@Test
public void testNullableArgNull(@Inject(TestNullableArgNode.class) CallTarget callTarget) throws UnsupportedMessageException {
Object ret = callTarget.call(new NullObject());
checkResult(ret, "null");
}

@Test
public void testNullableArgString(@Inject(TestNullableArgNode.class) CallTarget callTarget) throws UnsupportedMessageException {
Object ret = callTarget.call("a string");
checkResult(ret, "non-null");
}

@Test
public void testNullableArgClosure(@Inject(TestNullableArgNode.class) CallTarget callTarget) throws UnsupportedMessageException {
Object ret = callTarget.call(NULL_RET_CALLBACK);
checkResult(ret, "non-null");
}

public class TestNullableCallbackRetNode extends SendExecuteNode {

public TestNullableCallbackRetNode() {
super("callback_null_ret", String.format("(():%s) : string", NativeSimpleType.NULLABLE));
}
}

private static final TruffleObject NULL_RET_CALLBACK = new TestCallback(0, (args) -> {
return new NullObject();
});

@Test
public void testNullableCallbackRetNull(@Inject(TestNullableCallbackRetNode.class) CallTarget callTarget) throws UnsupportedMessageException {
Object ret = callTarget.call(NULL_RET_CALLBACK);
checkResult(ret, "null");
}

private static final TruffleObject STRING_RET_CALLBACK = new TestCallback(0, (args) -> {
return "a string";
});

@Test
public void testNullableCallbackRetString(@Inject(TestNullableCallbackRetNode.class) CallTarget callTarget) throws UnsupportedMessageException {
Object ret = callTarget.call(STRING_RET_CALLBACK);
checkResult(ret, "non-null");
}

private static final TruffleObject CLOSURE_RET_CALLBACK = new TestCallback(0, (args) -> {
return new TestCallback(0, (ignored) -> 0);
});

@Test
public void testNullableCallbackRetClosure(@Inject(TestNullableCallbackRetNode.class) CallTarget callTarget) throws UnsupportedMessageException {
Object ret = callTarget.call(CLOSURE_RET_CALLBACK);
checkResult(ret, "non-null");
}

private static void checkResult(Object ret, String expected) throws UnsupportedMessageException {
Assert.assertThat("return value", ret, is(instanceOf(TruffleObject.class)));
TruffleObject obj = (TruffleObject) ret;
Assert.assertTrue("isString", UNCACHED_INTEROP.isString(obj));
Assert.assertEquals("return value", expected, UNCACHED_INTEROP.asString(obj));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ public VariableArgCountFormatRoot() {
new FormatSpec("x (nil)", "([sint8], uint64, string, ...string, pointer) : sint32", "%s %p", new BoxedPrimitive("x"), new NullObject()),
new FormatSpec("x (nil) 42.00", "([sint8], uint64, string, ...string, pointer, double) : sint32", "%s %p %f", new BoxedPrimitive("x"), new NullObject(), 42),
new FormatSpec("x (nil) 42.00 42", "([sint8], uint64, string, ...string, pointer, double, sint64) : sint32", "%s %p %f %d", new BoxedPrimitive("x"), new NullObject(), 42,
42));
42),
new FormatSpec("x (nil)", "([sint8], uint64, string, ...string, nullable) : sint32", "%s %p", new BoxedPrimitive("x"), new NullObject()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ private LibFFIClosure(NFIContext context, LibFFISignature signature, Object exec
// shortcut for simple object return values
CallTarget executeCallTarget = Truffle.getRuntime().createCallTarget(new ObjectRetClosureRootNode(signature, executable));
this.nativePointer = context.allocateClosureObjectRet(signature, executeCallTarget);
} else if (retType instanceof LibFFIType.NullableType) {
// shortcut for simple object return values
CallTarget executeCallTarget = Truffle.getRuntime().createCallTarget(new NullableRetClosureRootNode(signature, executable));
this.nativePointer = context.allocateClosureObjectRet(signature, executeCallTarget);
} else if (retType instanceof LibFFIType.StringType) {
// shortcut for simple string return values
CallTarget executeCallTarget = Truffle.getRuntime().createCallTarget(new StringRetClosureRootNode(signature, executable));
Expand Down Expand Up @@ -227,6 +231,27 @@ public Object execute(VirtualFrame frame) {
}
}

private static final class NullableRetClosureRootNode extends RootNode {

@Child private CallClosureNode callClosure;
@Child private InteropLibrary interopLibrary;

private NullableRetClosureRootNode(LibFFISignature signature, Object receiver) {
super(null);
callClosure = new CallClosureNode(signature, receiver);
interopLibrary = InteropLibrary.getFactory().createDispatched(4);
}

@Override
public Object execute(VirtualFrame frame) {
Object ret = callClosure.execute(frame.getArguments());
if (interopLibrary.isNull(ret)) {
return null;
}
return ret;
}
}

static final class RetStringBuffer extends NativeArgumentBuffer {

Object ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ static LibFFIType createSimpleType(NFIContext ctx, NativeSimpleType simpleType,
return new StringType(ctx, size, alignment, ffiType);
case OBJECT:
return new ObjectType(ctx, size, alignment, ffiType);
case NULLABLE:
return new NullableType(ctx, size, alignment, ffiType);
default:
throw new AssertionError(simpleType.name());
}
Expand Down Expand Up @@ -121,12 +123,16 @@ abstract static class BasicType extends LibFFIType {
final NFIContext ctx;
final NativeSimpleType simpleType;

BasicType(NFIContext ctx, NativeSimpleType simpleType, int size, int alignment, int objectCount, long ffiType) {
super(size, alignment, objectCount, ffiType, Direction.BOTH, false);
BasicType(NFIContext ctx, NativeSimpleType simpleType, int size, int alignment, int objectCount, long ffiType, Direction direction) {
super(size, alignment, objectCount, ffiType, direction, false);
this.ctx = ctx;
this.simpleType = simpleType;
}

BasicType(NFIContext ctx, NativeSimpleType simpleType, int size, int alignment, int objectCount, long ffiType) {
this(ctx, simpleType, size, alignment, objectCount, ffiType, Direction.BOTH);
}

@ExportMessage
boolean accepts(@Shared("cachedType") @Cached("this.simpleType") NativeSimpleType cachedType) {
return cachedType == simpleType;
Expand All @@ -135,7 +141,8 @@ boolean accepts(@Shared("cachedType") @Cached("this.simpleType") NativeSimpleTyp
@ExportMessage
void serialize(NativeArgumentBuffer buffer, Object value,
@Shared("cachedType") @Cached("this.simpleType") NativeSimpleType cachedType,
@CachedLibrary(limit = "3") SerializeArgumentLibrary serialize) throws UnsupportedTypeException {
@CachedLibrary(limit = "3") SerializeArgumentLibrary serialize,
@CachedLibrary(limit = "1") InteropLibrary interop) throws UnsupportedTypeException {
buffer.align(alignment);
switch (cachedType) {
case UINT8:
Expand Down Expand Up @@ -177,6 +184,13 @@ void serialize(NativeArgumentBuffer buffer, Object value,
case OBJECT:
buffer.putObject(TypeTag.OBJECT, value, size);
break;
case NULLABLE:
if (interop.isNull(value)) {
buffer.putPointer(0L, size);
} else {
buffer.putObject(TypeTag.OBJECT, value, size);
}
break;
case VOID:
default:
CompilerDirectives.transferToInterpreter();
Expand Down Expand Up @@ -215,13 +229,15 @@ Object deserialize(NativeArgumentBuffer buffer,
return NativePointer.create(language, buffer.getPointer(size));
case STRING:
return new NativeString(buffer.getPointer(size));
case NULLABLE:
case OBJECT:
Object ret = buffer.getObject(size);
if (ret == null) {
return NativePointer.create(language, 0);
} else {
return ret;
}

default:
CompilerDirectives.transferToInterpreter();
throw new AssertionError(simpleType.name());
Expand Down Expand Up @@ -352,6 +368,18 @@ public ClosureArgumentNode createClosureArgumentNode() {
}
}

static final class NullableType extends BasicType {

NullableType(NFIContext ctx, int size, int alignment, long ffiType) {
super(ctx, NativeSimpleType.NULLABLE, size, alignment, 1, ffiType, Direction.JAVA_TO_NATIVE_ONLY);
}

@Override
public ClosureArgumentNode createClosureArgumentNode() {
return new ObjectClosureArgumentNode();
}
}

abstract static class BasePointerType extends LibFFIType {

protected BasePointerType(LibFFIType pointerType, Direction direction, boolean injectedArgument) {
Expand Down

0 comments on commit 7b6657b

Please sign in to comment.