Search in sources :

Example 1 with Substitute

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

the class JavaUtilZipSubstitutions method update.

// CRC32.c-Java_java_util_zip_CRC32_update(JNIEnv *env, jclass cls, jint crc, jint b)
@Substitute
private static int update(int crc, int b) {
    // Only the low eight bits of the argument b should be used
    CCharPointer bytes = StackValue.get(SizeOf.get(CCharPointer.class));
    bytes.write((byte) b);
    return (int) ZLib.crc32(WordFactory.unsigned(crc), bytes, 1).rawValue();
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 2 with Substitute

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

the class Target_com_sun_security_auth_module_UnixSystem method getUnixInfo.

@Substitute
void getUnixInfo() {
    int realUid = Unistd.getuid();
    Word pwsize = WordFactory.signed(Unistd.sysconf(Unistd._SC_GETPW_R_SIZE_MAX()));
    if (pwsize.lessThan(0)) {
        // indicates no definitive bound: try different sizes
        pwsize = WordFactory.signed(1024);
    }
    CCharPointer pwbuf = LibC.malloc(pwsize);
    try {
        passwd pwent = StackValue.get(SizeOf.get(passwd.class));
        passwdPointer p = StackValue.get(SizeOf.get(passwdPointer.class));
        int result;
        do {
            if (pwbuf.isNull()) {
                throw new OutOfMemoryError("Native heap");
            }
            p.write(WordFactory.nullPointer());
            result = Pwd.getpwuid_r(realUid, pwent, pwbuf, pwsize, p);
            if (result == Errno.ERANGE()) {
                pwsize = pwsize.add(pwsize);
                pwbuf = LibC.realloc(pwbuf, pwsize);
            } else if (result < 0 && result != Errno.EINTR()) {
                throw new RuntimeException("getpwuid_r error: " + Errno.errno());
            }
        } while (result != 0);
        this.uid = pwent.pw_uid();
        this.gid = pwent.pw_gid();
        this.username = CTypeConversion.toJavaString(pwent.pw_name());
    } finally {
        LibC.free(pwbuf);
    }
    int ngroups = Unistd.getgroups(0, WordFactory.nullPointer());
    int[] groupIds = new int[ngroups];
    try (PinnedObject pinnedGroupIds = PinnedObject.create(groupIds)) {
        ngroups = Unistd.getgroups(groupIds.length, pinnedGroupIds.addressOfArrayElement(0));
    }
    this.groups = new long[ngroups];
    for (int i = 0; i < this.groups.length; i++) {
        this.groups[i] = groupIds[i];
    }
}
Also used : Word(org.graalvm.compiler.word.Word) Pwd.passwd(com.oracle.svm.core.posix.headers.Pwd.passwd) PinnedObject(org.graalvm.nativeimage.PinnedObject) Pwd.passwdPointer(com.oracle.svm.core.posix.headers.Pwd.passwdPointer) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 3 with Substitute

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

the class PosixJavaIOSubstitutions method getSpace.

@Substitute
public long getSpace(File f, int t) {
    statvfs statvfs = StackValue.get(SizeOf.get(statvfs.class));
    LibC.memset(statvfs, WordFactory.zero(), WordFactory.unsigned(SizeOf.get(statvfs.class)));
    try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
        CCharPointer pathPtr = pathPin.get();
        if (statvfs(pathPtr, statvfs) == 0) {
            final long frsize = statvfs.f_frsize();
            if (t == Target_java_io_FileSystem.SPACE_TOTAL) {
                return frsize * statvfs.f_blocks();
            } else if (t == Target_java_io_FileSystem.SPACE_FREE) {
                return frsize * statvfs.f_bfree();
            } else if (t == Target_java_io_FileSystem.SPACE_USABLE) {
                return frsize * statvfs.f_bavail();
            } else {
                throw VMError.shouldNotReachHere("illegal space mode");
            }
        }
    }
    return 0L;
}
Also used : Statvfs.statvfs(com.oracle.svm.core.posix.headers.Statvfs.statvfs) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 4 with Substitute

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

the class PosixJavaIOSubstitutions method createFileExclusively.

@Substitute
public boolean createFileExclusively(String path) throws IOException {
    int fd;
    if (path.equals("/")) {
        return false;
    } else {
        try (CCharPointerHolder pathPin = CTypeConversion.toCString(PosixUtils.removeTrailingSlashes(path))) {
            CCharPointer pathPtr = pathPin.get();
            fd = open(pathPtr, O_RDWR() | O_CREAT(), 0666);
        }
    }
    if (fd < 0) {
        if (fd != EEXIST()) {
            throw new IOException(lastErrorString(path));
        }
    } else {
        close(fd);
        return true;
    }
    return false;
}
Also used : IOException(java.io.IOException) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 5 with Substitute

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

the class PosixJavaIOSubstitutions method available.

@Substitute
public int available() throws IOException {
    int handle = PosixUtils.getFDHandle(fd);
    SignedWord ret = WordFactory.zero();
    boolean av = false;
    stat stat = StackValue.get(SizeOf.get(stat.class));
    if (fstat(handle, stat) >= 0) {
        int mode = stat.st_mode();
        if (Util_java_io_FileInputStream.isChr(mode) || Util_java_io_FileInputStream.isFifo(mode) || Util_java_io_FileInputStream.isSock(mode)) {
            CIntPointer np = StackValue.get(SizeOf.get(CIntPointer.class));
            if (ioctl(handle, FIONREAD(), np) >= 0) {
                ret = WordFactory.signed(np.read());
                av = true;
            }
        }
    }
    if (!av) {
        SignedWord cur;
        SignedWord end;
        if ((cur = lseek(handle, WordFactory.zero(), SEEK_CUR())).equal(WordFactory.signed(-1))) {
            av = false;
        } else if ((end = lseek(handle, WordFactory.zero(), SEEK_END())).equal(WordFactory.signed(-1))) {
            av = false;
        } else if (lseek(handle, cur, SEEK_SET()).equal(WordFactory.signed(-1))) {
            av = false;
        } else {
            ret = end.subtract(cur);
            av = true;
        }
    }
    if (av) {
        long r = ret.rawValue();
        if (r > Integer.MAX_VALUE) {
            r = Integer.MAX_VALUE;
        }
        return (int) r;
    }
    throw new IOException(lastErrorString(""));
}
Also used : Stat.stat(com.oracle.svm.core.posix.headers.Stat.stat) Stat.fstat(com.oracle.svm.core.posix.headers.Stat.fstat) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) IOException(java.io.IOException) SignedWord(org.graalvm.word.SignedWord) 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