Search in sources :

Example 41 with CCharPointer

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

the class PosixUtils method writeSingle.

@SuppressWarnings("unused")
static void writeSingle(FileDescriptor fd, int b, boolean append) throws IOException {
    SignedWord n;
    int handle = Util_java_io_FileDescriptor.getFD(fd);
    if (handle == -1) {
        throw new IOException("Stream Closed");
    }
    CCharPointer bufPtr = StackValue.get(SizeOf.get(CCharPointer.class));
    bufPtr.write((byte) b);
    // the append parameter is disregarded
    n = write(handle, bufPtr, WordFactory.unsigned(1));
    if (n.equal(-1)) {
        throw new IOException(lastErrorString("Write error"));
    }
}
Also used : IOException(java.io.IOException) SignedWord(org.graalvm.word.SignedWord) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 42 with CCharPointer

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

the class JavaMainWrapper method getCRuntimeArgumentBlockLength.

/**
 * Argv is an array of C strings (i.e. array of pointers to characters). Each entry points to a
 * different C string corresponding to a program argument. The program argument strings
 * themselves are located in a contiguous block of memory followed by the environment variables
 * key-value strings:
 *
 * <pre>
 * &lt;argument_0&gt;\n&lt;argument_1&gt;\n...&lt;argument_n&gt;\n
 * &lt;env_key_value_1&gt;\n&lt;env_key_value_2&gt;\n...&lt;env_key_value_n&gt;\n
 * </pre>
 *
 * @return maximum length of C chars that can be stored in the program argument part of the
 *         contiguous memory block without writing into the environment variables part.
 */
public static long getCRuntimeArgumentBlockLength() {
    VMError.guarantee(argv.notEqual(WordFactory.zero()) && argc > 0, "Requires JavaMainWrapper.run(int, CCharPointerPointer) entry point!");
    CCharPointer firstArgPos = argv.read(0);
    if (argvLength.equal(WordFactory.zero())) {
        // Get char* to last program argument
        CCharPointer lastArgPos = argv.read(argc - 1);
        // Determine the length of the last program argument
        UnsignedWord lastArgLength = SubstrateUtil.strlen(lastArgPos);
        // Determine maximum C string length that can be stored in the program argument part
        argvLength = WordFactory.unsigned(lastArgPos.rawValue()).add(lastArgLength).subtract(WordFactory.unsigned(firstArgPos.rawValue()));
    }
    return argvLength.rawValue();
}
Also used : UnsignedWord(org.graalvm.word.UnsignedWord) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 43 with CCharPointer

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

the class JavaUtilZipSubstitutions method setDictionary.

// Inflater.c-Java_java_util_zip_Inflater_setDictionary(JNIEnv *env, jclass cls, jlong addr,
@Substitute
private static void setDictionary(long addr, byte[] b, int off, int len) {
    try (PinnedObject pinned = PinnedObject.create(b)) {
        CCharPointer bufPlusOff = pinned.addressOfArrayElement(off);
        int res = ZLib.inflateSetDictionary(WordFactory.pointer(addr), bufPlusOff, len);
        if (res == ZLib.Z_OK()) {
            return;
        } else if (res == ZLib.Z_STREAM_ERROR() || res == ZLib.Z_DATA_ERROR()) {
            throw new IllegalArgumentException();
        } else {
            throw new InternalError();
        }
    }
}
Also used : PinnedObject(org.graalvm.nativeimage.PinnedObject) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 44 with CCharPointer

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

the class PosixJavaIOSubstitutions method canonicalize0.

@Substitute
private String canonicalize0(String path) throws IOException {
    final int maxPathLen = MAXPATHLEN();
    if (path.length() > maxPathLen) {
        throw new IOException("Bad pathname");
    }
    CCharPointer resolved = StackValue.get(maxPathLen);
    try (CCharPointerHolder pathPin = CTypeConversion.toCString(path)) {
        CCharPointer pathPtr = pathPin.get();
        if (realpath(pathPtr, resolved).notEqual(WordFactory.zero())) {
            // that worked, so return it
            return PosixUtils.collapse(CTypeConversion.toJavaString(resolved));
        }
    }
    // something's bogus in the original path, so remove names from the end
    // until either some subpath works or we run out of names
    String resolvedPart = path;
    String unresolvedPart = "";
    CCharPointer r = WordFactory.nullPointer();
    int lastSep = resolvedPart.lastIndexOf('/');
    while (lastSep != -1) {
        // prepend the last part of the resolved part to the unresolved part
        unresolvedPart = resolvedPart.substring(lastSep) + unresolvedPart;
        // remove the last part from the resolved part
        resolvedPart = lastSep == 0 ? "" : resolvedPart.substring(0, lastSep);
        lastSep = resolvedPart.lastIndexOf('/');
        if (lastSep == -1) {
            break;
        }
        try (CCharPointerHolder pathPin = CTypeConversion.toCString(resolvedPart)) {
            CCharPointer resolvedPartPtr = pathPin.get();
            r = realpath(resolvedPartPtr, resolved);
        }
        int errno = errno();
        if (r.notEqual(WordFactory.zero())) {
            // the subpath has a canonical path
            break;
        } else if (errno == ENOENT() || errno == ENOTDIR() || errno == EACCES()) {
            // Other I/O problems cause an error return.
            continue;
        } else {
            throw new IOException(lastErrorString("Bad pathname"));
        }
    }
    if (r.notEqual(WordFactory.zero())) {
        // append unresolved subpath to resolved subpath
        String rs = CTypeConversion.toJavaString(r);
        if (rs.length() + 1 + unresolvedPart.length() > maxPathLen) {
            throw new IOException(lastErrorString("Bad pathname"));
        }
        return PosixUtils.collapse(rs + "/" + unresolvedPart);
    } else {
        // nothing resolved, so just return the original path
        return PosixUtils.collapse(path);
    }
}
Also used : IOException(java.io.IOException) PosixOSInterface.lastErrorString(com.oracle.svm.core.posix.PosixOSInterface.lastErrorString) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 45 with CCharPointer

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

the class PosixJavaIOSubstitutions method setLastModifiedTime.

@Substitute
public boolean setLastModifiedTime(File f, long time) {
    stat stat = StackValue.get(SizeOf.get(stat.class));
    try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
        CCharPointer pathPtr = pathPin.get();
        if (stat(pathPtr, stat) == 0) {
            timeval timeval = StackValue.get(2, SizeOf.get(timeval.class));
            // preserve access time
            timeval access = timeval.addressOf(0);
            access.set_tv_sec(stat.st_atime());
            access.set_tv_usec(0L);
            // change last-modified time
            timeval last = timeval.addressOf(1);
            last.set_tv_sec(time / 1000);
            last.set_tv_usec((time % 1000) * 1000);
            if (utimes(pathPtr, timeval) == 0) {
                return true;
            }
        }
    }
    return false;
}
Also used : Stat.stat(com.oracle.svm.core.posix.headers.Stat.stat) Stat.fstat(com.oracle.svm.core.posix.headers.Stat.fstat) Time.timeval(com.oracle.svm.core.posix.headers.Time.timeval) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Aggregations

CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)47 Substitute (com.oracle.svm.core.annotate.Substitute)18 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)15 PinnedObject (org.graalvm.nativeimage.PinnedObject)8 IOException (java.io.IOException)7 NetinetIn (com.oracle.svm.core.posix.headers.NetinetIn)6 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)6 SocketException (java.net.SocketException)5 SignedWord (org.graalvm.word.SignedWord)5 Inet6Address (java.net.Inet6Address)4 InetAddress (java.net.InetAddress)4 UnsignedWord (org.graalvm.word.UnsignedWord)4 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)3 Socket (com.oracle.svm.core.posix.headers.Socket)3 InterruptedIOException (java.io.InterruptedIOException)3 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)3 CCharPointerPointer (org.graalvm.nativeimage.c.type.CCharPointerPointer)3 PosixOSInterface.lastErrorString (com.oracle.svm.core.posix.PosixOSInterface.lastErrorString)2 DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)2 Dirent.dirent (com.oracle.svm.core.posix.headers.Dirent.dirent)2