Search in sources :

Example 1 with CLongPointer

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

the class JavavmExportJvm method NET_SetSockOpt.

/* @formatter:on */
/* Do not re-wrap commented-out code.  @formatter:off */
// 1266 /*
// 1267  * Wrapper for setsockopt system routine - performs any
// 1268  * necessary pre/post processing to deal with OS specific
// 1269  * issue :-
// 1270  *
// 1271  * On Solaris need to limit the suggested value for SO_SNDBUF
// 1272  * and SO_RCVBUF to the kernel configured limit
// 1273  *
// 1274  * For IP_TOS socket option need to mask off bits as this
// 1275  * aren't automatically masked by the kernel and results in
// 1276  * an error. In addition IP_TOS is a NOOP with IPv6 as it
// 1277  * should be setup as connection time.
// 1278  */
// 1279 int
// 1280 NET_SetSockOpt(int fd, int level, int  opt, const void *arg,
// 1281                int len)
// 1282 {
static int NET_SetSockOpt(int fd, int level, int opt, WordPointer arg, int len) {
    // 1283
    // 1284 #ifndef IPTOS_TOS_MASK
    // 1285 #define IPTOS_TOS_MASK 0x1e
    final int IPTOS_TOS_MASK = 0x1e;
    // 1286 #endif
    // 1287 #ifndef IPTOS_PREC_MASK
    // 1288 #define IPTOS_PREC_MASK 0xe0
    final int IPTOS_PREC_MASK = 0xe0;
    // 1289 #endif
    // 1290
    // 1291 #if defined(_ALLBSD_SOURCE)
    /* Hoist declarations out of "#if defined" block". */
    CIntPointer mib = null;
    CLongPointer rlen_Pointer = null;
    CIntPointer bufsize = null;
    /* maxsockbuf should be static. */
    /* Declaring maxsockbuf to be a CIntPointer because it is only used inside _ALLBSD_SOURCE. */
    CIntPointer maxsockbuf_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
    int maxsockbuf_size = SizeOf.get(CIntPointer.class);
    if (IsDefined._ALLBSD_SOURCE()) {
        // 1292 #if defined(KIPC_MAXSOCKBUF)
        if (IsDefined.sysctl_KIPC_MAXSOCKBUF()) {
            // 1293     int mib[3];
            mib = StackValue.get(3, SizeOf.get(CIntPointer.class));
            // 1294     size_t rlen;
            rlen_Pointer = StackValue.get(SizeOf.get(CLongPointer.class));
        }
        // 1295 #endif
        // 1296
        // 1297     int *bufsize;
        bufsize = StackValue.get(SizeOf.get(CIntPointer.class));
        // 1299 #ifdef __APPLE__
        if (IsDefined.__APPLE__()) {
            // 1300     static int maxsockbuf = -1;
            maxsockbuf_Pointer.write(-1);
            maxsockbuf_size = SizeOf.get(CIntPointer.class);
        // 1301 #else
        } else {
            // 1302     static long maxsockbuf = -1;
            VMError.unimplemented();
        }
    // 1303 #endif
    }
    // 1317     if (level == IPPROTO_IP && opt == IP_TOS) {
    if (level == NetinetIn.IPPROTO_IP() && opt == NetinetIn.IP_TOS()) {
        // 1318         int *iptos;
        CIntPointer iptos = StackValue.get(SizeOf.get(CIntPointer.class));
        // 1320 #if defined(AF_INET6) && (defined(__solaris__) || defined(MACOSX))
        if (IsDefined.socket_AF_INET6() && (IsDefined.__solaris__() || IsDefined.MACOSX())) {
            // 1321         if (ipv6_available()) {
            if (JavaNetNetUtil.ipv6_available()) {
                // 1322             return 0;
                return 0;
            }
        // 1324 #endif
        }
        // 1326 #if defined(AF_INET6) && defined(__linux__)
        if (IsDefined.socket_AF_INET6() && IsDefined.__linux__()) {
            VMError.unimplemented();
        // 1327         if (ipv6_available()) {
        // 1328             int optval = 1;
        // 1329             if (setsockopt(fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND,
        // 1330                            (void *)&optval, sizeof(optval)) < 0) {
        // 1331                 return -1;
        // 1332             }
        // 1332         }
        }
        // 1334 #endif
        // 1335
        // 1336         iptos = (int *)arg;
        iptos = (CIntPointer) arg;
        // 1337         *iptos &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
        iptos.write(iptos.read() & (IPTOS_TOS_MASK | IPTOS_PREC_MASK));
    }
    // 1344 #ifdef __solaris__
    if (IsDefined.__solaris__()) {
        VMError.unimplemented();
    // 1345     if (level == SOL_SOCKET) {
    // 1346         if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
    // 1347             int sotype=0, arglen;
    // 1348             int *bufsize, maxbuf;
    // 1349             int ret;
    // 1350
    // 1351             /* Attempt with the original size */
    // 1352             ret = setsockopt(fd, level, opt, arg, len);
    // 1353             if ((ret == 0) || (ret == -1 && errno != ENOBUFS))
    // 1354                 return ret;
    // 1355
    // 1356             /* Exceeded system limit so clamp and retry */
    // 1357
    // 1358             arglen = sizeof(sotype);
    // 1359             if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype,
    // 1360                            &arglen) < 0) {
    // 1361                 return -1;
    // 1362             }
    // 1363
    // 1364             /*
    // 1365              * We try to get tcp_maxbuf (and udp_max_buf) using
    // 1366              * an ioctl() that isn't available on all versions of Solaris.
    // 1367              * If that fails, we use the search algorithm in findMaxBuf()
    // 1368              */
    // 1369             if (!init_tcp_max_buf && sotype == SOCK_STREAM) {
    // 1370                 tcp_max_buf = net_getParam("/dev/tcp", "tcp_max_buf");
    // 1371                 if (tcp_max_buf == -1) {
    // 1372                     tcp_max_buf = findMaxBuf(fd, opt, SOCK_STREAM);
    // 1373                     if (tcp_max_buf == -1) {
    // 1374                         return -1;
    // 1375                     }
    // 1376                 }
    // 1377                 init_tcp_max_buf = 1;
    // 1378             } else if (!init_udp_max_buf && sotype == SOCK_DGRAM) {
    // 1379                 udp_max_buf = net_getParam("/dev/udp", "udp_max_buf");
    // 1380                 if (udp_max_buf == -1) {
    // 1381                     udp_max_buf = findMaxBuf(fd, opt, SOCK_DGRAM);
    // 1382                     if (udp_max_buf == -1) {
    // 1383                         return -1;
    // 1384                     }
    // 1385                 }
    // 1386                 init_udp_max_buf = 1;
    // 1387             }
    // 1388
    // 1389             maxbuf = (sotype == SOCK_STREAM) ? tcp_max_buf : udp_max_buf;
    // 1390             bufsize = (int *)arg;
    // 1391             if (*bufsize > maxbuf) {
    // 1392                 *bufsize = maxbuf;
    // 1393             }
    // 1394         }
    // 1395     }
    }
    // 1427 #ifdef __linux__
    if (IsDefined.__linux__()) {
        // 1428     if (level == SOL_SOCKET && opt == SO_RCVBUF) {
        if (level == Socket.SOL_SOCKET() && opt == Socket.SO_RCVBUF()) {
            // 1429         int *bufsize = (int *)arg;
            bufsize = (CIntPointer) arg;
            // 1430         if (*bufsize < 1024) {
            if (bufsize.read() < 1024) {
                // 1431             *bufsize = 1024;
                bufsize.write(1024);
            }
        }
    }
    // 1436 #if defined(_ALLBSD_SOURCE)
    if (IsDefined._ALLBSD_SOURCE()) {
        // 1442     if (level == SOL_SOCKET) {
        if (level == Socket.SOL_SOCKET()) {
            // 1443         if (opt == SO_SNDBUF || opt == SO_RCVBUF) {
            if (opt == Socket.SO_SNDBUF() || opt == Socket.SO_RCVBUF()) {
                // 1444 #ifdef KIPC_MAXSOCKBUF
                if (IsDefined.sysctl_KIPC_MAXSOCKBUF()) {
                    // 1445             if (maxsockbuf == -1) {
                    if (maxsockbuf_Pointer.read() == -1) {
                        // 1446                mib[0] = CTL_KERN;
                        mib.write(0, DarwinSysctl.CTL_KERN());
                        // 1447                mib[1] = KERN_IPC;
                        mib.write(1, DarwinSysctl.KERN_IPC());
                        // 1448                mib[2] = KIPC_MAXSOCKBUF;
                        mib.write(2, DarwinSysctl.KIPC_MAXSOCKBUF());
                        // 1449                rlen = sizeof(maxsockbuf);
                        rlen_Pointer.write(maxsockbuf_size);
                        // 1450                if (sysctl(mib, 3, &maxsockbuf, &rlen, NULL, 0) == -1)
                        if (Sysctl.sysctl(mib, 3, maxsockbuf_Pointer, (WordPointer) rlen_Pointer, WordFactory.nullPointer(), 0) == -1) {
                            // 1451                    maxsockbuf = 1024;
                            maxsockbuf_Pointer.write(1024);
                        }
                        // 1453 #if 1
                        if (true) {
                            // 1454                /* XXXBSD: This is a hack to workaround mb_max/mb_max_adj
                            // 1455                   problem.  It should be removed when kern.ipc.maxsockbuf
                            // 1456                   will be real value. */
                            // 1457                maxsockbuf = (maxsockbuf/5)*4;
                            maxsockbuf_Pointer.write((maxsockbuf_Pointer.read() / 5) * 4);
                        }
                    // 1458 #endif
                    }
                // 1460 #elif defined(__OpenBSD__)
                } else if (IsDefined.__OpenBSD__()) {
                    VMError.unimplemented();
                // 1461            maxsockbuf = SB_MAX;
                // 1462 #else
                } else {
                    VMError.unimplemented();
                // 1463            maxsockbuf = 64 * 1024;      /* XXX: NetBSD */
                }
                // 1464 #endif
                // 1465
                // 1466            bufsize = (int *)arg;
                bufsize = (CIntPointer) arg;
                // 1467            if (*bufsize > maxsockbuf) {
                if (bufsize.read() > maxsockbuf_Pointer.read()) {
                    // 1468                *bufsize = maxsockbuf;
                    bufsize.write(maxsockbuf_Pointer.read());
                }
                // 1471            if (opt == SO_RCVBUF && *bufsize < 1024) {
                if (opt == Socket.SO_RCVBUF() && bufsize.read() < 1024) {
                    // 1472                 *bufsize = 1024;
                    bufsize.write(1024);
                }
            // 1474
            }
        }
    }
    // 1479 #if defined(_ALLBSD_SOURCE) || defined(_AIX)
    if (IsDefined._AIX()) {
        VMError.unimplemented();
    // 1480     /*
    // 1481      * On Solaris, SO_REUSEADDR will allow multiple datagram
    // 1482      * sockets to bind to the same port. The network jck tests check
    // 1483      * for this "feature", so we need to emulate it by turning on
    // 1484      * SO_REUSEPORT as well for that combination.
    // 1485      */
    // 1486     if (level == SOL_SOCKET && opt == SO_REUSEADDR) {
    // 1487         int sotype;
    // 1488         socklen_t arglen;
    // 1489
    // 1490         arglen = sizeof(sotype);
    // 1491         if (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void *)&sotype, &arglen) < 0) {
    // 1492             return -1;
    // 1493         }
    // 1494
    // 1495         if (sotype == SOCK_DGRAM) {
    // 1496             setsockopt(fd, level, SO_REUSEPORT, arg, len);
    // 1497         }
    // 1498     }
    }
    // 1501     return setsockopt(fd, level, opt, arg, len);
    return Socket.setsockopt(fd, level, opt, arg, len);
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) CLongPointer(org.graalvm.nativeimage.c.type.CLongPointer)

Example 2 with CLongPointer

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

the class Target_com_oracle_truffle_nfi_impl_NFIContext method executePrimitive.

@Substitute
long executePrimitive(long cif, long functionPointer, byte[] primArgs, int patchCount, int[] patchOffsets, Object[] objArgs) {
    try (LocalNativeScope scope = TruffleNFISupport.createLocalScope(patchCount)) {
        NativeTruffleContext ctx = WordFactory.pointer(nativeContext);
        ffi_cif ffiCif = WordFactory.pointer(cif);
        CLongPointer retPtr = StackValue.get(8);
        ExecuteHelper.execute(ctx, ffiCif, retPtr, functionPointer, primArgs, patchCount, patchOffsets, objArgs, scope);
        return retPtr.read();
    }
}
Also used : LibFFI.ffi_cif(com.oracle.svm.truffle.nfi.libffi.LibFFI.ffi_cif) CLongPointer(org.graalvm.nativeimage.c.type.CLongPointer) NativeTruffleContext(com.oracle.svm.truffle.nfi.NativeAPI.NativeTruffleContext) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

CLongPointer (org.graalvm.nativeimage.c.type.CLongPointer)2 Substitute (com.oracle.svm.core.annotate.Substitute)1 NativeTruffleContext (com.oracle.svm.truffle.nfi.NativeAPI.NativeTruffleContext)1 LibFFI.ffi_cif (com.oracle.svm.truffle.nfi.libffi.LibFFI.ffi_cif)1 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)1 WordPointer (org.graalvm.nativeimage.c.type.WordPointer)1