Permalink
Browse files

[GR-10959] Add support for missing system properties.

PullRequest: graal/1885
  • Loading branch information...
olpaw committed Jul 26, 2018
2 parents 0697dc3 + c13fb95 commit 4529207aa55cd8a09c95c2be89f9723e55422f2f
@@ -101,11 +101,6 @@
import static com.oracle.svm.core.posix.headers.Unistd.unlink;
import static com.oracle.svm.core.posix.headers.Unistd.write;
import static com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFRelease;
import static com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFStringAppendCharacters;
import static com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFStringCreateMutable;
import static com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFStringGetCharacters;
import static com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFStringGetLength;
import static com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFStringNormalize;
import static com.oracle.svm.core.posix.headers.darwin.DarwinSendfile.sendfile;
import static com.oracle.svm.core.posix.headers.linux.LinuxSendfile.sendfile;
import static com.oracle.svm.core.posix.headers.linux.Mntent.getmntent_r;
@@ -122,7 +117,6 @@
import java.util.function.Predicate;
import org.graalvm.compiler.word.ObjectAccess;
import org.graalvm.nativeimage.PinnedObject;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.StackValue;
@@ -164,6 +158,7 @@
import com.oracle.svm.core.jdk.JDK8OrEarlier;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.os.IsDefined;
import com.oracle.svm.core.posix.darwin.DarwinCoreFoundationUtils;
import com.oracle.svm.core.posix.headers.Dirent;
import com.oracle.svm.core.posix.headers.Dirent.DIR;
import com.oracle.svm.core.posix.headers.Dirent.dirent;
@@ -194,7 +189,7 @@
import com.oracle.svm.core.posix.headers.Time.timeval;
import com.oracle.svm.core.posix.headers.Uio.iovec;
import com.oracle.svm.core.posix.headers.Unistd;
import com.oracle.svm.core.posix.headers.darwin.CoreFoundation.CFMutableStringRef;
import com.oracle.svm.core.posix.headers.darwin.CoreFoundation;
import com.oracle.svm.core.posix.headers.linux.Mntent;
import com.oracle.svm.core.posix.headers.linux.Mntent.mntent;
import com.oracle.svm.core.snippets.KnownIntrinsics;
@@ -2758,28 +2753,11 @@ static void initialize() {
@Substitute
private static char[] normalizepath(char[] path, int form) {
char[] result;
CFMutableStringRef csref = CFStringCreateMutable(WordFactory.nullPointer(), WordFactory.zero());
if (csref.isNull()) {
throw throwOutOfMemoryError("native heap");
}
try (PinnedObject pathPin = PinnedObject.create(path)) {
PointerBase chars = pathPin.addressOfArrayElement(0);
int len = path.length;
CFStringAppendCharacters(csref, chars, WordFactory.signed(len));
}
CFStringNormalize(csref, WordFactory.signed(form));
SignedWord len = CFStringGetLength(csref);
result = new char[(int) len.rawValue()];
try (PinnedObject resultPin = PinnedObject.create(result)) {
PointerBase resultChars = resultPin.addressOfArrayElement(0);
CFStringGetCharacters(csref, len, resultChars);
}
CoreFoundation.CFMutableStringRef csref = DarwinCoreFoundationUtils.toCFStringRef(String.valueOf(path));
CoreFoundation.CFStringNormalize(csref, WordFactory.signed(form));
String res = DarwinCoreFoundationUtils.fromCFStringRef(csref);
CFRelease(csref);
return result;
return res.toCharArray();
}
}
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2018, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.posix.darwin;
import org.graalvm.nativeimage.PinnedObject;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.word.PointerBase;
import org.graalvm.word.WordFactory;
import com.oracle.svm.core.posix.headers.darwin.CoreFoundation;
@Platforms(Platform.DARWIN.class)
public final class DarwinCoreFoundationUtils {
private DarwinCoreFoundationUtils() {
}
public static CoreFoundation.CFMutableStringRef toCFStringRef(String str) {
CoreFoundation.CFMutableStringRef stringRef = CoreFoundation.CFStringCreateMutable(WordFactory.nullPointer(), WordFactory.zero());
if (stringRef.isNull()) {
throw new OutOfMemoryError("native heap");
}
char[] charArray = str.toCharArray();
try (PinnedObject pathPin = PinnedObject.create(charArray)) {
PointerBase addressOfCharArray = pathPin.addressOfArrayElement(0);
CoreFoundation.CFStringAppendCharacters(stringRef, addressOfCharArray, WordFactory.signed(charArray.length));
}
return stringRef;
}
public static String fromCFStringRef(CoreFoundation.CFStringRef cfstr) {
int length = (int) CoreFoundation.CFStringGetLength(cfstr);
char[] chars = new char[length];
for (int i = 0; i < length; ++i) {
chars[i] = CoreFoundation.CFStringGetCharacterAtIndex(cfstr, i);
}
return String.valueOf(chars);
}
}
@@ -39,6 +39,7 @@
import com.oracle.svm.core.posix.PosixSystemPropertiesSupport;
import com.oracle.svm.core.posix.headers.Limits;
import com.oracle.svm.core.posix.headers.Unistd;
import com.oracle.svm.core.posix.headers.darwin.CoreFoundation;
@Platforms({Platform.DARWIN.class})
public class DarwinSystemPropertiesSupport extends PosixSystemPropertiesSupport {
@@ -59,6 +60,38 @@ protected String tmpdirValue() {
return "/var/tmp";
}
}
private static volatile String osVersionValue = null;
@Override
protected String osVersionValue() {
if (osVersionValue != null) {
return osVersionValue;
}
/* On OSX Java returns the ProductVersion instead of kernel release info. */
CoreFoundation.CFDictionaryRef dict = CoreFoundation._CFCopyServerVersionDictionary();
if (dict.isNull()) {
dict = CoreFoundation._CFCopySystemVersionDictionary();
}
if (dict.isNull()) {
return osVersionValue = "Unknown";
}
CoreFoundation.CFStringRef dictKeyRef = DarwinCoreFoundationUtils.toCFStringRef("MacOSXProductVersion");
CoreFoundation.CFStringRef dictValue = CoreFoundation.CFDictionaryGetValue(dict, dictKeyRef);
CoreFoundation.CFRelease(dictKeyRef);
if (dictValue.isNull()) {
dictKeyRef = DarwinCoreFoundationUtils.toCFStringRef("ProductVersion");
dictValue = CoreFoundation.CFDictionaryGetValue(dict, dictKeyRef);
CoreFoundation.CFRelease(dictKeyRef);
}
if (dictValue.isNull()) {
return osVersionValue = "Unknown";
}
osVersionValue = DarwinCoreFoundationUtils.fromCFStringRef(dictValue);
CoreFoundation.CFRelease(dictValue);
return osVersionValue;
}
}
@Platforms({Platform.DARWIN.class})
@@ -69,6 +69,7 @@
"<sys/time.h>",
"<sys/times.h>",
"<sys/uio.h>",
"<sys/utsname.h>",
"<sys/wait.h>",
"<termios.h>",
"<time.h>",
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2018, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.svm.core.posix.headers;
import org.graalvm.nativeimage.c.CContext;
import org.graalvm.nativeimage.c.function.CFunction;
import org.graalvm.nativeimage.c.struct.CFieldAddress;
import org.graalvm.nativeimage.c.struct.CStruct;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.word.PointerBase;
// Checkstyle: stop
/** Declarations of method from <sys/utsname.h>. */
@CContext(PosixDirectives.class)
public class Utsname {
@CStruct(addStructKeyword = true)
public interface utsname extends PointerBase {
@CFieldAddress
CCharPointer sysname();
@CFieldAddress
CCharPointer nodename();
@CFieldAddress
CCharPointer release();
@CFieldAddress
CCharPointer version();
@CFieldAddress
CCharPointer machine();
}
/**
* int uname(struct utsname *name);
*/
@CFunction
public static native int uname(utsname name);
}
@@ -74,18 +74,23 @@
/** Number of 16-bit Unicode characters in the string. */
@CFunction
public static native SignedWord CFStringGetLength(CFStringRef theString);
public static native long CFStringGetLength(CFStringRef theString);
/**
* Extracting the contents of the string. For obtaining multiple characters, calling
* CFStringGetCharacters() is more efficient than multiple calls to
* CFStringGetCharacterAtIndex(). If the length of the string is not known (so you can't use a
* fixed size buffer for CFStringGetCharacters()), another method is to use is
* CFStringGetCharacterFromInlineBuffer() (see further below).
*/
@CFunction
public static native void CFStringGetCharacters(CFStringRef theString, SignedWord range, PointerBase buffer);
public static native void CFRelease(PointerBase cf);
public interface CFDictionaryRef extends PointerBase {
}
@CFunction
public static native void CFRelease(PointerBase cf);
public static native CFDictionaryRef _CFCopyServerVersionDictionary();
@CFunction
public static native CFDictionaryRef _CFCopySystemVersionDictionary();
@CFunction
public static native CFStringRef CFDictionaryGetValue(CFDictionaryRef theDict, CFStringRef key);
@CFunction
public static native char CFStringGetCharacterAtIndex(CFStringRef theString, long idx);
}
@@ -28,11 +28,14 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.StackValue;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.jdk.SystemPropertiesSupport;
import com.oracle.svm.core.posix.PosixSystemPropertiesSupport;
import com.oracle.svm.core.posix.headers.Paths;
import com.oracle.svm.core.posix.headers.Utsname;
@Platforms({Platform.LINUX.class})
public class LinuxSystemPropertiesSupport extends PosixSystemPropertiesSupport {
@@ -41,6 +44,15 @@
protected String tmpdirValue() {
return Paths._PATH_VARTMP();
}
@Override
protected String osVersionValue() {
Utsname.utsname name = StackValue.get(Utsname.utsname.class);
if (Utsname.uname(name) >= 0) {
return CTypeConversion.toJavaString(name.release());
}
return "Unknown";
}
}
@Platforms({Platform.LINUX.class})
@@ -28,6 +28,7 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.jdk.SystemPropertiesSupport;
@@ -53,6 +54,11 @@ protected String userDirValue() {
protected String tmpdirValue() {
return "C:\\Temp";
}
@Override
protected String osVersionValue() {
return "Unknown";
}
}
@Platforms(Platform.WINDOWS.class)
@@ -46,6 +46,7 @@
/** System properties that are taken from the VM hosting the image generator. */
private static final String[] HOSTED_PROPERTIES = {
"java.version",
/*
* We do not support cross-compilation for now. Separator might also be cached
* in other classes, so changing them would be tricky.
@@ -74,10 +75,13 @@ protected SystemPropertiesSupport() {
lazyRuntimeValues = new HashMap<>();
lazyRuntimeValues.put("java.vm.name", () -> "Substrate VM");
lazyRuntimeValues.put("java.vendor", () -> "Oracle Corporation");
lazyRuntimeValues.put("java.vendor.url", () -> "https://www.graalvm.org/");
lazyRuntimeValues.put("user.name", this::userNameValue);
lazyRuntimeValues.put("user.home", this::userHomeValue);
lazyRuntimeValues.put("user.dir", this::userDirValue);
lazyRuntimeValues.put("java.io.tmpdir", this::tmpdirValue);
lazyRuntimeValues.put("os.version", this::osVersionValue);
lazyRuntimeValues.put(ImageInfo.PROPERTY_IMAGE_CODE_KEY, () -> ImageInfo.PROPERTY_IMAGE_CODE_VALUE_RUNTIME);
}
@@ -135,4 +139,6 @@ private void initializeLazyValue(String key) {
protected abstract String userDirValue();
protected abstract String tmpdirValue();
protected abstract String osVersionValue();
}

0 comments on commit 4529207

Please sign in to comment.