forked from oracle/graal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GR-28138] Intrinsic for retrieving native function pointer.
PullRequest: graal/8003
- Loading branch information
Showing
7 changed files
with
275 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
.../src/com/oracle/truffle/llvm/runtime/nodes/intrinsics/handles/GraalVMResolveFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. | ||
* | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, are | ||
* permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this list of | ||
* conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of | ||
* conditions and the following disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to | ||
* endorse or promote products derived from this software without specific prior written | ||
* permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS | ||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
* OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.oracle.truffle.llvm.runtime.nodes.intrinsics.handles; | ||
|
||
import com.oracle.truffle.api.dsl.CachedContext; | ||
import com.oracle.truffle.api.dsl.Fallback; | ||
import com.oracle.truffle.api.dsl.NodeChild; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.llvm.runtime.LLVMContext; | ||
import com.oracle.truffle.llvm.runtime.LLVMFunctionDescriptor; | ||
import com.oracle.truffle.llvm.runtime.LLVMLanguage; | ||
import com.oracle.truffle.llvm.runtime.except.LLVMPolyglotException; | ||
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode; | ||
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.llvm.LLVMIntrinsic; | ||
import com.oracle.truffle.llvm.runtime.pointer.LLVMManagedPointer; | ||
import com.oracle.truffle.llvm.runtime.pointer.LLVMNativePointer; | ||
import com.oracle.truffle.llvm.runtime.pointer.LLVMPointer; | ||
|
||
@NodeChild(type = LLVMExpressionNode.class) | ||
public abstract class GraalVMResolveFunction extends LLVMIntrinsic { | ||
|
||
@Specialization | ||
protected Object doNativeResolve(LLVMNativePointer pointer, | ||
@CachedContext(LLVMLanguage.class) LLVMContext context) { | ||
return LLVMManagedPointer.create(context.getFunctionDescriptor(pointer)); | ||
} | ||
|
||
@Specialization(guards = "pointsToFunctionDescriptor(pointer)") | ||
protected Object doManagedResolve(LLVMManagedPointer pointer) { | ||
return pointer; | ||
} | ||
|
||
@Specialization(guards = "pointsToLong(pointer)") | ||
protected Object doNativePointerResolve(LLVMPointer pointer, | ||
@CachedContext(LLVMLanguage.class) LLVMContext context) { | ||
LLVMManagedPointer object = LLVMManagedPointer.cast(pointer); | ||
Object pointerValue = object.getObject(); | ||
LLVMNativePointer nativePointer = LLVMNativePointer.create((long) pointerValue); | ||
return LLVMManagedPointer.create(context.getFunctionDescriptor(nativePointer)); | ||
} | ||
|
||
@Fallback | ||
protected Object doError(Object pointer) { | ||
throw new LLVMPolyglotException(this, "Cannot resolve pointer %s to a function.", pointer); | ||
} | ||
|
||
protected boolean pointsToFunctionDescriptor(LLVMManagedPointer pointer) { | ||
return pointer.getObject() instanceof LLVMFunctionDescriptor; | ||
} | ||
|
||
protected boolean pointsToLong(LLVMPointer pointer) { | ||
if (LLVMManagedPointer.isInstance(pointer)) { | ||
return LLVMManagedPointer.cast(pointer).getObject() instanceof Long; | ||
} | ||
return false; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
sulong/tests/com.oracle.truffle.llvm.tests.interop.native/interop/createResolveFunction.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. | ||
* | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, are | ||
* permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this list of | ||
* conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of | ||
* conditions and the following disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to | ||
* endorse or promote products derived from this software without specific prior written | ||
* permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS | ||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
* OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
#include <graalvm/llvm/handles.h> | ||
|
||
int fortytwo() { | ||
return 42; | ||
} | ||
|
||
int max(int a, int b) { | ||
if (a > b) { | ||
return a; | ||
} | ||
return b; | ||
} | ||
|
||
void *test_native_fortytwo_function() { | ||
return resolve_function(fortytwo); | ||
} | ||
|
||
void *test_resolve_function(void *function) { | ||
return resolve_function(function); | ||
} |
119 changes: 119 additions & 0 deletions
119
...cle.truffle.llvm.tests/src/com/oracle/truffle/llvm/tests/interop/ResolveFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2021, Oracle and/or its affiliates. | ||
* | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, are | ||
* permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this list of | ||
* conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of | ||
* conditions and the following disclaimer in the documentation and/or other materials provided | ||
* with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to | ||
* endorse or promote products derived from this software without specific prior written | ||
* permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS | ||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
* OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
package com.oracle.truffle.llvm.tests.interop; | ||
|
||
import com.oracle.truffle.api.CallTarget; | ||
import com.oracle.truffle.api.interop.InteropException; | ||
import com.oracle.truffle.api.interop.InteropLibrary; | ||
import com.oracle.truffle.llvm.runtime.LLVMFunctionDescriptor; | ||
import com.oracle.truffle.llvm.runtime.pointer.LLVMManagedPointer; | ||
import com.oracle.truffle.tck.TruffleRunner; | ||
import com.oracle.truffle.tck.TruffleRunner.Inject; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(TruffleRunner.class) | ||
public class ResolveFunctionTest extends InteropTestBase { | ||
|
||
private static Object testLibrary; | ||
private static Object fortyTwoFunction; | ||
private static Object maxFunction; | ||
|
||
@BeforeClass | ||
public static void loadTestBitcode() { | ||
testLibrary = loadTestBitcodeInternal("createResolveFunction.c"); | ||
try { | ||
fortyTwoFunction = InteropLibrary.getFactory().getUncached().readMember(testLibrary, "fortytwo"); | ||
maxFunction = InteropLibrary.getFactory().getUncached().readMember(testLibrary, "max"); | ||
} catch (InteropException ex) { | ||
throw new AssertionError(ex); | ||
} | ||
} | ||
|
||
public static class FortyTwoFunctionNode extends SulongTestNode { | ||
public FortyTwoFunctionNode() { | ||
super(testLibrary, "test_native_fortytwo_function"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testFunctionFortyTwo(@Inject(FortyTwoFunctionNode.class) CallTarget function) throws InteropException { | ||
Object ret = function.call(); | ||
Assert.assertTrue(LLVMManagedPointer.isInstance(ret)); | ||
LLVMFunctionDescriptor retFunction = (LLVMFunctionDescriptor) LLVMManagedPointer.cast(ret).getObject(); | ||
String name = retFunction.getLLVMFunction().getName(); | ||
Assert.assertEquals("fortytwo", name); | ||
Assert.assertEquals(42, InteropLibrary.getUncached().execute(retFunction)); | ||
} | ||
|
||
public class ResolveFunctionNode extends SulongTestNode { | ||
public ResolveFunctionNode() { | ||
super(testLibrary, "test_resolve_function"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testResolveFunctionFortytwo(@Inject(ResolveFunctionNode.class) CallTarget function) throws InteropException { | ||
Object ret = function.call(fortyTwoFunction); | ||
Assert.assertTrue(LLVMManagedPointer.isInstance(ret)); | ||
LLVMFunctionDescriptor retFunction = (LLVMFunctionDescriptor) LLVMManagedPointer.cast(ret).getObject(); | ||
Assert.assertEquals(42, InteropLibrary.getUncached().execute(retFunction)); | ||
} | ||
|
||
@Test | ||
public void testResolveNativeFunctionFortytwo(@Inject(ResolveFunctionNode.class) CallTarget function) throws InteropException { | ||
InteropLibrary.getUncached().toNative(fortyTwoFunction); | ||
long pointer = InteropLibrary.getUncached().asPointer(fortyTwoFunction); | ||
Object ret = function.call(pointer); | ||
Assert.assertTrue(LLVMManagedPointer.isInstance(ret)); | ||
LLVMFunctionDescriptor retFunction = (LLVMFunctionDescriptor) LLVMManagedPointer.cast(ret).getObject(); | ||
Assert.assertEquals(42, InteropLibrary.getUncached().execute(retFunction)); | ||
} | ||
|
||
@Test | ||
public void testResolveNativeFunctionMax(@Inject(ResolveFunctionNode.class) CallTarget function) throws InteropException { | ||
InteropLibrary.getUncached().toNative(maxFunction); | ||
long pointer = InteropLibrary.getUncached().asPointer(maxFunction); | ||
Object ret = function.call(pointer); | ||
Assert.assertTrue(LLVMManagedPointer.isInstance(ret)); | ||
LLVMFunctionDescriptor retFunction = (LLVMFunctionDescriptor) LLVMManagedPointer.cast(ret).getObject(); | ||
Assert.assertEquals(42, InteropLibrary.getUncached().execute(retFunction, 1, 42)); | ||
} | ||
|
||
@Test | ||
public void testResolveFunctionMax(@Inject(ResolveFunctionNode.class) CallTarget function) throws InteropException { | ||
Object ret = function.call(maxFunction); | ||
Assert.assertTrue(LLVMManagedPointer.isInstance(ret)); | ||
LLVMFunctionDescriptor retFunction = (LLVMFunctionDescriptor) LLVMManagedPointer.cast(ret).getObject(); | ||
Assert.assertEquals(42, InteropLibrary.getUncached().execute(retFunction, 1, 42)); | ||
} | ||
} |