Search in sources :

Example 36 with Substitute

use of com.oracle.svm.core.annotate.Substitute 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 37 with Substitute

use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.

the class LinuxSubstitutions method nanoTime.

@Substitute
@Uninterruptible(reason = "Does basic math after a simple system call")
private static long nanoTime() {
    timespec timespec = StackValue.get(SizeOf.get(timespec.class));
    if (clock_gettime(CLOCK_MONOTONIC(), timespec) == 0) {
        return timespec.tv_sec() * 1_000_000_000L + timespec.tv_nsec();
    } else {
        /* High precision time is not available, fall back to low precision. */
        timeval timeval = StackValue.get(SizeOf.get(timeval.class));
        timezone timezone = WordFactory.nullPointer();
        gettimeofday(timeval, timezone);
        return timeval.tv_sec() * 1_000_000_000L + timeval.tv_usec() * 1_000L;
    }
}
Also used : Time.timespec(com.oracle.svm.core.posix.headers.Time.timespec) Time.timezone(com.oracle.svm.core.posix.headers.Time.timezone) Time.timeval(com.oracle.svm.core.posix.headers.Time.timeval) Uninterruptible(com.oracle.svm.core.annotate.Uninterruptible) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 38 with Substitute

use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.

the class AnnotationSubstitutionProcessor method handleAnnotatedMethodInSubstitutionClass.

private void handleAnnotatedMethodInSubstitutionClass(Executable annotatedMethod, Class<?> originalClass) {
    Substitute substituteAnnotation = lookupAnnotation(annotatedMethod, Substitute.class);
    KeepOriginal keepOriginalAnnotation = lookupAnnotation(annotatedMethod, KeepOriginal.class);
    int numAnnotations = (substituteAnnotation != null ? 1 : 0) + (keepOriginalAnnotation != null ? 1 : 0);
    if (numAnnotations == 0) {
        /* Unannotated method in substitution class: a regular method, nothing to do. */
        return;
    }
    guarantee(numAnnotations == 1, "only one of @Substitute or @KeepOriginal can be used: %s", annotatedMethod);
    ResolvedJavaMethod annotated = metaAccess.lookupJavaMethod(annotatedMethod);
    ResolvedJavaMethod original = findOriginalMethod(annotatedMethod, originalClass);
    if (original == null) {
    /* Optional target that is not present, so nothing to do. */
    } else if (substituteAnnotation != null) {
        register(methodSubstitutions, annotated, original, annotated);
    } else if (keepOriginalAnnotation != null) {
        register(methodSubstitutions, annotated, original, original);
    }
}
Also used : KeepOriginal(com.oracle.svm.core.annotate.KeepOriginal) Substitute(com.oracle.svm.core.annotate.Substitute) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 39 with Substitute

use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.

the class JLineSubstitutions method setupSigCont.

@Substitute
private void setupSigCont() {
    SignalHandler signalHandler = new SignalHandler() {

        @Override
        public void handle(Signal arg0) {
            /* Original implementation calls this code using reflection. */
            try {
                terminal.init();
                drawLine();
                flush();
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    };
    Signal.handle(new Signal("CONT"), signalHandler);
}
Also used : Signal(sun.misc.Signal) SignalHandler(sun.misc.SignalHandler) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 40 with Substitute

use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.

the class AnnotationSubstitutionProcessor method handleClass.

private void handleClass(Class<?> annotatedClass) {
    guarantee(Modifier.isFinal(annotatedClass.getModifiers()) || annotatedClass.isInterface(), "Annotated class must be final: %s", annotatedClass);
    guarantee(annotatedClass.getSuperclass() == Object.class || annotatedClass.isInterface(), "Annotated class must inherit directly from Object: %s", annotatedClass);
    if (!NativeImageGenerator.includedIn(ImageSingletons.lookup(Platform.class), lookupAnnotation(annotatedClass, Platforms.class))) {
        return;
    }
    TargetClass targetClassAnnotation = lookupAnnotation(annotatedClass, TargetClass.class);
    Class<?> originalClass = findTargetClass(annotatedClass, targetClassAnnotation);
    if (originalClass == null) {
        return;
    }
    Delete deleteAnnotation = lookupAnnotation(annotatedClass, Delete.class);
    Substitute substituteAnnotation = lookupAnnotation(annotatedClass, Substitute.class);
    int numAnnotations = (deleteAnnotation != null ? 1 : 0) + (substituteAnnotation != null ? 1 : 0);
    guarantee(numAnnotations <= 1, "Only one of @Delete or @Substitute can be used: %s", annotatedClass);
    if (deleteAnnotation != null) {
        handleDeletedClass(originalClass, deleteAnnotation);
    } else if (substituteAnnotation != null) {
        handleSubstitutionClass(annotatedClass, originalClass);
    } else {
        handleAliasClass(annotatedClass, originalClass);
    }
}
Also used : Delete(com.oracle.svm.core.annotate.Delete) TargetClass(com.oracle.svm.core.annotate.TargetClass) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

Substitute (com.oracle.svm.core.annotate.Substitute)69 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)18 SocketException (java.net.SocketException)11 IOException (java.io.IOException)10 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)10 PinnedObject (org.graalvm.nativeimage.PinnedObject)9 Util_java_io_FileDescriptor (com.oracle.svm.core.posix.PosixOSInterface.Util_java_io_FileDescriptor)8 FileDescriptor (java.io.FileDescriptor)8 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)8 ServerSocket (java.net.ServerSocket)7 Socket (com.oracle.svm.core.posix.headers.Socket)6 NativeTruffleContext (com.oracle.svm.truffle.nfi.NativeAPI.NativeTruffleContext)6 SignedWord (org.graalvm.word.SignedWord)6 InterruptedIOException (java.io.InterruptedIOException)5 Time.timeval (com.oracle.svm.core.posix.headers.Time.timeval)4 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)3 Stat.fstat (com.oracle.svm.core.posix.headers.Stat.fstat)3 Stat.stat (com.oracle.svm.core.posix.headers.Stat.stat)3 Time.timezone (com.oracle.svm.core.posix.headers.Time.timezone)3 LibFFI.ffi_cif (com.oracle.svm.truffle.nfi.libffi.LibFFI.ffi_cif)3