use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class Util_jni method getLocalHostName.
@Substitute
// 354 Java_java_net_Inet4AddressImpl_getLocalHostName(JNIEnv *env, jobject this) {
@SuppressWarnings({ "static-method" })
public String getLocalHostName() {
// 355 char hostname[NI_MAXHOST+1];
CCharPointer hostname = StackValue.get(Netdb.NI_MAXHOST() + 1, SizeOf.get(CCharPointer.class));
// 357 hostname[0] = '\0';
hostname.write(0, (byte) '\0');
// 358 if (JVM_GetHostName(hostname, sizeof(hostname))) {
if (CTypeConversion.toBoolean(VmPrimsJVM.JVM_GetHostName(hostname, Netdb.NI_MAXHOST() + 1))) {
// 360 strcpy(hostname, "localhost");
try (CCharPointerHolder pin = CTypeConversion.toCString("localhost")) {
LibC.strcpy(hostname, pin.get());
}
} else {
// 362 struct addrinfo hints, *res;
Netdb.addrinfo hints = StackValue.get(SizeOf.get(Netdb.addrinfo.class));
Netdb.addrinfoPointer res = StackValue.get(SizeOf.get(Netdb.addrinfoPointer.class));
// 363 int error;
int error;
// 365 hostname[NI_MAXHOST] = '\0';
hostname.write(Netdb.NI_MAXHOST(), (byte) '\0');
// 366 memset(&hints, 0, sizeof(hints));
LibC.memset(hints, WordFactory.zero(), SizeOf.unsigned(Netdb.addrinfo.class));
// 367 hints.ai_flags = AI_CANONNAME;
hints.set_ai_flags(Netdb.AI_CANONNAME());
// 368 hints.ai_family = AF_INET;
hints.set_ai_family(Socket.AF_INET());
// 370 error = getaddrinfo(hostname, NULL, &hints, &res);
error = Netdb.getaddrinfo(hostname, WordFactory.nullPointer(), hints, res);
// 372 if (error == 0) {/* host is known to name service */
if (error == 0) {
// 373 getnameinfo(res->ai_addr,
// 374 res->ai_addrlen,
// 375 hostname,
// 376 NI_MAXHOST,
// 377 NULL,
// 378 0,
// 379 NI_NAMEREQD);
Netdb.getnameinfo(res.read().ai_addr(), res.read().ai_addrlen(), hostname, Netdb.NI_MAXHOST(), WordFactory.nullPointer(), 0, Netdb.NI_NAMEREQD());
// 381 /* if getnameinfo fails hostname is still the value
// 382 from gethostname */
// 384 freeaddrinfo(res);
Netdb.freeaddrinfo(res.read());
}
}
// 387 return (*env)->NewStringUTF(env, hostname);
return CTypeConversion.toJavaString(hostname);
}
use of com.oracle.svm.core.annotate.Substitute 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();
}
}
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class JavaUtilZipSubstitutions method deflateBytes.
// Deflater.c-Java_java_util_zip_Deflater_deflateBytes(JNIEnv *env, jobject this, jlong addr,
@SuppressWarnings("hiding")
@Substitute
private int deflateBytes(long addr, byte[] b, int off, int len, int flush) {
z_stream strm = WordFactory.pointer(addr);
try (PinnedObject pinned_in_buf = PinnedObject.create(this.buf);
PinnedObject pinned_out_buf = PinnedObject.create(b)) {
strm.set_next_in(pinned_in_buf.addressOfArrayElement(this.off));
strm.set_next_out(pinned_out_buf.addressOfArrayElement(off));
strm.set_avail_in(this.len);
strm.set_avail_out(len);
if (this.setParams) {
int res = ZLib.deflateParams(strm, level, strategy);
this.setParams = false;
if (res == ZLib.Z_OK()) {
return Util_java_util_zip_Deflater.update(this, len, strm);
} else if (res == ZLib.Z_BUF_ERROR()) {
return 0;
} else {
throw new InternalError();
}
} else {
int res = ZLib.deflate(strm, this.finish ? ZLib.Z_FINISH() : flush);
if (res == ZLib.Z_STREAM_END()) {
this.finished = true;
return Util_java_util_zip_Deflater.update(this, len, strm);
} else if (res == ZLib.Z_OK()) {
return Util_java_util_zip_Deflater.update(this, len, strm);
} else if (res == ZLib.Z_BUF_ERROR()) {
return 0;
} else {
throw new InternalError();
}
}
}
}
use of com.oracle.svm.core.annotate.Substitute 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);
}
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class PosixJavaIOSubstitutions method skip.
@Substitute
public long skip(long n) throws IOException {
SignedWord cur = WordFactory.zero();
SignedWord end = WordFactory.zero();
int handle = PosixUtils.getFDHandle(fd);
if ((cur = lseek(handle, WordFactory.zero(), SEEK_CUR())).equal(WordFactory.signed(-1))) {
throw new IOException(lastErrorString("Seek error"));
} else if ((end = lseek(handle, WordFactory.signed(n), SEEK_CUR())).equal(WordFactory.signed(-1))) {
throw new IOException(lastErrorString("Seek error"));
}
return end.subtract(cur).rawValue();
}
Aggregations