Search in sources :

Example 6 with PinnedObject

use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.

the class Target_com_oracle_truffle_nfi_impl_NFIContext method executeNative.

@Substitute
void executeNative(long cif, long functionPointer, byte[] primArgs, int patchCount, int[] patchOffsets, Object[] objArgs, byte[] ret) {
    try (LocalNativeScope scope = TruffleNFISupport.createLocalScope(patchCount);
        PinnedObject retBuffer = PinnedObject.create(ret)) {
        NativeTruffleContext ctx = WordFactory.pointer(nativeContext);
        ffi_cif ffiCif = WordFactory.pointer(cif);
        ExecuteHelper.execute(ctx, ffiCif, retBuffer.addressOfArrayElement(0), functionPointer, primArgs, patchCount, patchOffsets, objArgs, scope);
    }
}
Also used : LibFFI.ffi_cif(com.oracle.svm.truffle.nfi.libffi.LibFFI.ffi_cif) PinnedObject(org.graalvm.nativeimage.PinnedObject) NativeTruffleContext(com.oracle.svm.truffle.nfi.NativeAPI.NativeTruffleContext) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 7 with PinnedObject

use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.

the class LocalNativeScope method pinArray.

@TruffleBoundary
PointerBase pinArray(Object arr) {
    PinnedObject ret = PinnedObject.create(arr);
    pinned[pinCount++] = ret;
    return ret.addressOfArrayElement(0);
}
Also used : PinnedObject(org.graalvm.nativeimage.PinnedObject) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)

Example 8 with PinnedObject

use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.

the class PosixUtils method readBytes.

static int readBytes(byte[] b, int off, int len, FileDescriptor fd) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if (PosixUtils.outOfBounds(off, len, b)) {
        throw new IndexOutOfBoundsException();
    }
    if (len == 0) {
        return 0;
    }
    SignedWord nread;
    CCharPointer buf = LibC.malloc(WordFactory.unsigned(len));
    try {
        if (buf.equal(WordFactory.zero())) {
            throw new OutOfMemoryError();
        }
        int handle = getFDHandle(fd);
        nread = read(handle, buf, WordFactory.unsigned(len));
        if (nread.greaterThan(0)) {
            /*
                 * We do not read directly into the (pinned) result array because read can block,
                 * and that could lead to object pinned for an unexpectedly long time.
                 */
            try (PinnedObject pin = PinnedObject.create(b)) {
                LibC.memcpy(pin.addressOfArrayElement(off), buf, (UnsignedWord) nread);
            }
        } else if (nread.equal(-1)) {
            throw new IOException(lastErrorString("Read error"));
        } else {
            // EOF
            nread = WordFactory.signed(-1);
        }
    } finally {
        LibC.free(buf);
    }
    return (int) nread.rawValue();
}
Also used : PinnedObject(org.graalvm.nativeimage.PinnedObject) IOException(java.io.IOException) SignedWord(org.graalvm.word.SignedWord) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 9 with PinnedObject

use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.

the class DarwinExecutableName method apply.

/**
 * This implementation of {@link CompilerCommandPlugin#apply(Object[])} does not use the
 * arguments, and returns a String, possibly null.
 */
@Override
public Object apply(Object[] args) {
    /* Find out how long the executable path is. */
    final CIntPointer sizePointer = StackValue.get(SizeOf.get(CIntPointer.class));
    sizePointer.write(0);
    if (DarwinDyld._NSGetExecutablePath(WordFactory.nullPointer(), sizePointer) != -1) {
        VMError.shouldNotReachHere("DarwinExecutableName.getExecutableName: Executable path length is 0?");
    }
    /* Allocate a correctly-sized buffer and ask again. */
    final byte[] byteBuffer = new byte[sizePointer.read()];
    try (PinnedObject pinnedBuffer = PinnedObject.create(byteBuffer)) {
        final CCharPointer bufferPointer = pinnedBuffer.addressOfArrayElement(0);
        if (DarwinDyld._NSGetExecutablePath(bufferPointer, sizePointer) == -1) {
            /* Failure to find executable path. */
            return null;
        }
        final String executableString = CTypeConversion.toJavaString(bufferPointer);
        final String result = realpath(executableString);
        return result;
    }
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) PinnedObject(org.graalvm.nativeimage.PinnedObject) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 10 with PinnedObject

use of org.graalvm.nativeimage.PinnedObject in project graal by oracle.

the class Util_jni method getHostByAddr.

@Substitute
@SuppressWarnings({ "static-method" })
public String getHostByAddr(byte[] addrArray) throws UnknownHostException {
    String ret = null;
    CCharPointer host = StackValue.get(Netdb.NI_MAXHOST() + 1);
    int error = 0;
    int len = 0;
    CCharPointer caddr = StackValue.get(16);
    NetinetIn.sockaddr_in him4 = StackValue.get(SizeOf.get(NetinetIn.sockaddr_in.class));
    NetinetIn.sockaddr_in6 him6 = StackValue.get(SizeOf.get(NetinetIn.sockaddr_in6.class));
    Socket.sockaddr sa;
    if (addrArray.length == 4) {
        /*
             * For IPv4 addresses construct a sockaddr_in structure.
             */
        int addr = 0;
        addr |= ((addrArray[0] << 24) & 0xff000000);
        addr |= ((addrArray[1] << 16) & 0xff0000);
        addr |= ((addrArray[2] << 8) & 0xff00);
        addr |= ((addrArray[3] << 0) & 0xff);
        LibC.memset(him4, WordFactory.signed(0), SizeOf.unsigned(NetinetIn.sockaddr_in.class));
        him4.sin_addr().set_s_addr(NetinetIn.htonl(addr));
        him4.set_sin_family(Socket.AF_INET());
        sa = (Socket.sockaddr) him4;
        len = SizeOf.get(NetinetIn.sockaddr_in.class);
    } else {
        /*
             * For IPv6 address construct a sockaddr_in6 structure.
             */
        try (PinnedObject pinnedAddrArray = PinnedObject.create(addrArray)) {
            CCharPointer addrArray0 = pinnedAddrArray.addressOfArrayElement(0);
            LibC.memcpy(caddr, addrArray0, WordFactory.unsigned(16));
        }
        LibC.memset(him6, WordFactory.signed(0), SizeOf.unsigned(NetinetIn.sockaddr_in6.class));
        LibC.memcpy(him6.sin6_addr(), caddr, SizeOf.unsigned(NetinetIn.in6_addr.class));
        him6.set_sin6_family(Socket.AF_INET6());
        sa = (Socket.sockaddr) him6;
        len = SizeOf.get(NetinetIn.sockaddr_in6.class);
    }
    error = Netdb.getnameinfo(sa, len, host, Netdb.NI_MAXHOST(), WordFactory.nullPointer(), 0, Netdb.NI_NAMEREQD());
    if (error == 0) {
        ret = CTypeConversion.toJavaString(host);
    }
    if (ret == null) {
        throw new UnknownHostException();
    }
    return ret;
}
Also used : UnknownHostException(java.net.UnknownHostException) PinnedObject(org.graalvm.nativeimage.PinnedObject) NetinetIn(com.oracle.svm.core.posix.headers.NetinetIn) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) ServerSocket(java.net.ServerSocket) Socket(com.oracle.svm.core.posix.headers.Socket) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

PinnedObject (org.graalvm.nativeimage.PinnedObject)12 Substitute (com.oracle.svm.core.annotate.Substitute)7 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)7 ZLib.z_stream (com.oracle.svm.core.posix.headers.ZLib.z_stream)2 IOException (java.io.IOException)2 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)2 SignedWord (org.graalvm.word.SignedWord)2 NoAllocationVerifier (com.oracle.svm.core.heap.NoAllocationVerifier)1 NetinetIn (com.oracle.svm.core.posix.headers.NetinetIn)1 Pwd.passwd (com.oracle.svm.core.posix.headers.Pwd.passwd)1 Pwd.passwdPointer (com.oracle.svm.core.posix.headers.Pwd.passwdPointer)1 Socket (com.oracle.svm.core.posix.headers.Socket)1 NativeTruffleContext (com.oracle.svm.truffle.nfi.NativeAPI.NativeTruffleContext)1 LibFFI.ffi_cif (com.oracle.svm.truffle.nfi.libffi.LibFFI.ffi_cif)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 ServerSocket (java.net.ServerSocket)1 UnknownHostException (java.net.UnknownHostException)1 DataFormatException (java.util.zip.DataFormatException)1 Word (org.graalvm.compiler.word.Word)1 Platform (org.graalvm.nativeimage.Platform)1