Search in sources :

Example 6 with CCharPointerHolder

use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.

the class PosixJavaLangSubstitutions method doForkAndExec.

@SuppressWarnings("try")
static int doForkAndExec(CCharPointer file, CCharPointer dir, CCharPointerPointer argv, CCharPointerPointer envp, int[] stdioFds, int failFd) {
    final int buflen = SizeOf.get(dirent.class) + Limits.PATH_MAX() + 1;
    final boolean haveProcFs = Platform.includedIn(Platform.LINUX.class);
    try (// only this thread will exist in the child and garbage collection is not possible.
    PinnedObject bufferPin = PinnedObject.create(new byte[buflen]);
        CCharPointerHolder procFdsPath = haveProcFs ? CTypeConversion.toCString("/proc/self/fd/") : null;
        CCharPointerHolder searchPaths = CTypeConversion.toCString(System.getenv("PATH"));
        CCharPointerHolder searchPathSeparator = CTypeConversion.toCString(":");
        NoAllocationVerifier v = NoAllocationVerifier.factory("fragile state after fork()")) {
        CCharPointer procFdsPathPtr = (procFdsPath != null) ? procFdsPath.get() : WordFactory.nullPointer();
        return uninterruptibleForkAndExec(file, dir, argv, envp, stdioFds, failFd, bufferPin.addressOfArrayElement(0), buflen, procFdsPathPtr, searchPaths.get(), searchPathSeparator.get());
    }
}
Also used : Platform(org.graalvm.nativeimage.Platform) PinnedObject(org.graalvm.nativeimage.PinnedObject) NoAllocationVerifier(com.oracle.svm.core.heap.NoAllocationVerifier) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 7 with CCharPointerHolder

use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.

the class PolyglotNativeAPI method handleThrowable.

private static PolyglotStatus handleThrowable(Throwable t) {
    PolyglotStatus errorCode = t instanceof PolyglotNativeAPIError ? ((PolyglotNativeAPIError) t).getCode() : polyglot_generic_failure;
    String message = t.getMessage();
    PolyglotExtendedErrorInfo unmanagedErrorInfo = UnmanagedMemory.malloc(SizeOf.get(PolyglotExtendedErrorInfo.class));
    unmanagedErrorInfo.setErrorCode(errorCode.getCValue());
    CCharPointerHolder holder = CTypeConversion.toCString(message);
    CCharPointer value = holder.get();
    unmanagedErrorInfo.setErrorMessage(value);
    errorInfo.set(new ErrorInfoHolder(unmanagedErrorInfo, holder));
    return errorCode;
}
Also used : PolyglotExtendedErrorInfo(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotExtendedErrorInfo) PolyglotStatus(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotStatus) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 8 with CCharPointerHolder

use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.

the class Util_jni method lookupAllHostAddr.

/* @formatter:on */
// Do not re-wrap long lines and comments: @formatter:off
@Substitute
@SuppressWarnings({ "static-method" })
public InetAddress[] lookupAllHostAddr(String host) throws UnknownHostException, SocketException, InterruptedException {
    CCharPointer hostname;
    InetAddress[] ret = null;
    int retLen = 0;
    int error = 0;
    Netdb.addrinfo hints = StackValue.get(SizeOf.get(Netdb.addrinfo.class));
    Netdb.addrinfo res = WordFactory.nullPointer();
    Netdb.addrinfoPointer resPtr = StackValue.get(SizeOf.get(Netdb.addrinfoPointer.class));
    Netdb.addrinfo resNew = WordFactory.nullPointer();
    if (host == null) {
        throw new NullPointerException("host is null");
    }
    try (CCharPointerHolder hostPin = CTypeConversion.toCString(host)) {
        // "hostname" is pinned through the end of the method.
        hostname = hostPin.get();
        // #ifdef MACOSX
        if (IsDefined.MACOSX()) {
            /*
                 * If we're looking up the local machine, attempt to get the address from
                 * getifaddrs. This ensures we get an IPv6 address for the local machine.
                 */
            ret = Util_java_net_Inet6AddressImpl.lookupIfLocalhost(hostname, true);
            if (ret != null) {
                return ret;
            }
        }
        // #endif MACOSX
        /* Try once, with our static buffer. */
        LibC.memset(hints, WordFactory.signed(0), SizeOf.unsigned(Netdb.addrinfo.class));
        hints.set_ai_flags(Netdb.AI_CANONNAME());
        hints.set_ai_family(Socket.AF_UNSPEC());
        // #endif
        try {
            // res needs cleanup at "cleanupAndReturn".
            error = Netdb.getaddrinfo(hostname, WordFactory.nullPointer(), hints, resPtr);
            if (error != 0) {
                throw new UnknownHostException(host);
            } else {
                res = resPtr.read();
                int i = 0;
                int inetCount = 0;
                int inet6Count = 0;
                int inetIndex;
                int inet6Index;
                Netdb.addrinfo itr;
                Netdb.addrinfo last = WordFactory.nullPointer();
                Netdb.addrinfo iterator = res;
                while (iterator.isNonNull()) {
                    boolean skip = false;
                    itr = resNew;
                    while (itr.isNonNull()) {
                        if ((iterator.ai_family() == itr.ai_family()) && (iterator.ai_addrlen() == itr.ai_addrlen())) {
                            if (itr.ai_family() == Socket.AF_INET()) {
                                NetinetIn.sockaddr_in addr1;
                                NetinetIn.sockaddr_in addr2;
                                addr1 = (NetinetIn.sockaddr_in) iterator.ai_addr();
                                addr2 = (NetinetIn.sockaddr_in) itr.ai_addr();
                                if (addr1.sin_addr().s_addr() == addr2.sin_addr().s_addr()) {
                                    skip = true;
                                    break;
                                }
                            } else {
                                NetinetIn.sockaddr_in6 addr1;
                                NetinetIn.sockaddr_in6 addr2;
                                addr1 = (NetinetIn.sockaddr_in6) iterator.ai_addr();
                                addr2 = (NetinetIn.sockaddr_in6) itr.ai_addr();
                                int t;
                                for (t = 0; t < 16; t++) {
                                    if (addr1.sin6_addr().s6_addr().read(t) != addr2.sin6_addr().s6_addr().read(t)) {
                                        break;
                                    }
                                }
                                if (t < 16) {
                                    itr = itr.ai_next();
                                    continue;
                                } else {
                                    skip = true;
                                    break;
                                }
                            }
                        } else if ((iterator.ai_family() != Socket.AF_INET()) && (iterator.ai_family() != Socket.AF_INET6())) {
                            /* we can't handle other family types */
                            skip = true;
                            break;
                        }
                        itr = itr.ai_next();
                    }
                    if (!skip) {
                        Netdb.addrinfo next = LibC.malloc(SizeOf.unsigned(Netdb.addrinfo.class));
                        if (next.isNull()) {
                            throw new OutOfMemoryError("malloc failed");
                        }
                        LibC.memcpy(next, iterator, SizeOf.unsigned(Netdb.addrinfo.class));
                        next.set_ai_next(WordFactory.nullPointer());
                        if (resNew.isNull()) {
                            resNew = next;
                        } else {
                            last.set_ai_next(next);
                        }
                        last = next;
                        i++;
                        if (iterator.ai_family() == Socket.AF_INET()) {
                            inetCount++;
                        } else if (iterator.ai_family() == Socket.AF_INET6()) {
                            inet6Count++;
                        }
                    }
                    iterator = iterator.ai_next();
                }
                retLen = i;
                iterator = resNew;
                ret = new InetAddress[retLen];
                if (Target_java_net_InetAddress.preferIPv6Address) {
                    /* AF_INET addresses will be offset by inet6Count */
                    inetIndex = inet6Count;
                    inet6Index = 0;
                } else {
                    /* AF_INET6 addresses will be offset by inetCount */
                    inetIndex = 0;
                    inet6Index = inetCount;
                }
                while (iterator.isNonNull()) {
                    int ret1;
                    if (iterator.ai_family() == Socket.AF_INET()) {
                        Inet4Address iaObj = Util_java_net_Inet4Address.new_Inet4Address();
                        JavaNetNetUtil.setInetAddress_addr(iaObj, NetinetIn.ntohl(((NetinetIn.sockaddr_in) iterator.ai_addr()).sin_addr().s_addr()));
                        JavaNetNetUtil.setInetAddress_hostName(iaObj, host);
                        ret[inetIndex] = iaObj;
                        inetIndex++;
                    } else if (iterator.ai_family() == Socket.AF_INET6()) {
                        // 455 jint scope = 0;
                        int scope = 0;
                        // 457 jobject iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
                        Inet6Address iaObj = Util_java_net_Inet6Address.new_Inet6Address();
                        // 458 if (IS_NULL(iaObj)) {
                        if (iaObj == null) {
                            // 459 ret = NULL;
                            ret = null;
                            // 460 goto cleanupAndReturn;
                            return ret;
                        }
                        // 462 ret1 = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)iterator->ai_addr)->sin6_addr));
                        ret1 = JavaNetNetUtil.setInet6Address_ipaddress(iaObj, ((NetinetIn.sockaddr_in6) iterator.ai_addr()).sin6_addr().s6_addr());
                        // 463 if (!ret1) {
                        if (!CTypeConversion.toBoolean(ret1)) {
                            // 464 ret = NULL;
                            ret = null;
                            // 465 goto cleanupAndReturn;
                            return ret;
                        }
                        // 468 scope = ((struct sockaddr_in6*)iterator->ai_addr)->sin6_scope_id;
                        scope = ((NetinetIn.sockaddr_in6) iterator.ai_addr()).sin6_scope_id();
                        // 469 if (scope != 0) { /* zero is default value, no need to set */
                        if (scope != 0) {
                            /* zero is default value, no need to set */
                            // 470 setInet6Address_scopeid(env, iaObj, scope);
                            JavaNetNetUtil.setInet6Address_scopeid(iaObj, scope);
                        }
                        // 472 setInetAddress_hostName(env, iaObj, host);
                        JavaNetNetUtil.setInetAddress_hostName(iaObj, host);
                        // 473 (*env)->SetObjectArrayElement(env, ret, inet6Index, iaObj);
                        ret[inet6Index] = iaObj;
                        // 474 inet6Index++;
                        inet6Index++;
                    }
                    iterator = iterator.ai_next();
                }
            }
        } finally {
            /* cleanupAndReturn: */
            Netdb.addrinfo iterator;
            Netdb.addrinfo tmp;
            iterator = resNew;
            while (iterator.isNonNull()) {
                tmp = iterator;
                iterator = iterator.ai_next();
                LibC.free(tmp);
            }
            Netdb.freeaddrinfo(res);
        }
    // JNU_ReleaseStringPlatformChars(env, host, hostname)
    // happens when I exit the CCharPointerHolder region.
    }
    return ret;
}
Also used : Netdb(com.oracle.svm.core.posix.headers.Netdb) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) Inet6Address(java.net.Inet6Address) NetinetIn(com.oracle.svm.core.posix.headers.NetinetIn) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) InetAddress(java.net.InetAddress) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 9 with CCharPointerHolder

use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.

the class PosixUtils method fileOpen.

static void fileOpen(String path, FileDescriptor fd, int flags) throws FileNotFoundException {
    try (CCharPointerHolder pathPin = CTypeConversion.toCString(removeTrailingSlashes(path))) {
        CCharPointer pathPtr = pathPin.get();
        int handle = open(pathPtr, flags, 0666);
        if (handle >= 0) {
            Util_java_io_FileDescriptor.setFD(fd, handle);
        } else {
            throw new FileNotFoundException(path);
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 10 with CCharPointerHolder

use of org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder in project graal by oracle.

the class PosixUtils method setLocale.

public static String setLocale(int category, String locale) {
    if (locale == null) {
        CCharPointer cstrResult = Locale.setlocale(category, WordFactory.nullPointer());
        return CTypeConversion.toJavaString(cstrResult);
    }
    try (CCharPointerHolder localePin = CTypeConversion.toCString(locale)) {
        CCharPointer cstrLocale = localePin.get();
        CCharPointer cstrResult = Locale.setlocale(category, cstrLocale);
        return CTypeConversion.toJavaString(cstrResult);
    }
}
Also used : CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Aggregations

CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)15 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)15 Substitute (com.oracle.svm.core.annotate.Substitute)8 IOException (java.io.IOException)3 PosixOSInterface.lastErrorString (com.oracle.svm.core.posix.PosixOSInterface.lastErrorString)2 Netdb (com.oracle.svm.core.posix.headers.Netdb)2 Stat.fstat (com.oracle.svm.core.posix.headers.Stat.fstat)2 Stat.stat (com.oracle.svm.core.posix.headers.Stat.stat)2 NoAllocationVerifier (com.oracle.svm.core.heap.NoAllocationVerifier)1 DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)1 Dirent.dirent (com.oracle.svm.core.posix.headers.Dirent.dirent)1 Dirent.direntPointer (com.oracle.svm.core.posix.headers.Dirent.direntPointer)1 ENOTDIR (com.oracle.svm.core.posix.headers.Errno.ENOTDIR)1 NetinetIn (com.oracle.svm.core.posix.headers.NetinetIn)1 S_IFDIR (com.oracle.svm.core.posix.headers.Stat.S_IFDIR)1 Statvfs.statvfs (com.oracle.svm.core.posix.headers.Statvfs.statvfs)1 Time.timeval (com.oracle.svm.core.posix.headers.Time.timeval)1 FileNotFoundException (java.io.FileNotFoundException)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1