use of org.graalvm.nativeimage.c.type.CCharPointerPointer in project graal by oracle.
the class PosixJavaLangSubstitutions method forkAndExec.
/*
* NOTE: This implementation uses simple fork() and exec() calls. However, OpenJDK uses
* posix_spawn() on some platforms, specifically on Solaris to avoid swap exhaustion when memory
* is reserved conservatively for the fork'ed process. That implementation is more complex and
* requires a helper tool to cleanly launch the actual target executable.
*/
@Substitute
@SuppressWarnings({ "unused", "static-method" })
int forkAndExec(int mode, byte[] helperpath, byte[] file, byte[] argBlock, int argCount, byte[] envBlock, int envCount, byte[] dir, int[] fds, boolean redirectErrorStream) throws IOException {
int[] pipes = new int[8];
Arrays.fill(pipes, -1);
try (//
PinnedObject filePin = PinnedObject.create(file);
PinnedObject dirPin = PinnedObject.create(dir);
PinnedObject argBlockPin = PinnedObject.create(argBlock);
PinnedObject argvPin = PinnedObject.create(new CCharPointerPointer[argCount + 2]);
PinnedObject envBlockPin = PinnedObject.create(envBlock);
PinnedObject envpPin = PinnedObject.create(new CCharPointerPointer[envCount + 1]);
//
PinnedObject pipesPin = PinnedObject.create(pipes)) {
CCharPointerPointer argv = argvPin.addressOfArrayElement(0);
argv.write(0, filePin.addressOfArrayElement(0));
Java_lang_UNIXProcess_Supplement.gatherCStringPointers(argBlockPin.addressOfArrayElement(0), argBlock.length, argv.addressOf(1), argCount + 1);
CCharPointerPointer envp = WordFactory.nullPointer();
if (envBlock != null) {
envp = envpPin.addressOfArrayElement(0);
Java_lang_UNIXProcess_Supplement.gatherCStringPointers(envBlockPin.addressOfArrayElement(0), envBlock.length, envp.addressOf(0), envCount + 1);
}
CIntPointer[] stdioPipes = new CIntPointer[3];
for (int i = 0; i <= 2; i++) {
if (fds[i] == -1) {
stdioPipes[i] = pipesPin.addressOfArrayElement(2 * i);
if (Unistd.pipe(stdioPipes[i]) < 0) {
throw new IOException("pipe() failed");
}
}
}
CIntPointer failPipe = pipesPin.addressOfArrayElement(2 * stdioPipes.length);
if (Unistd.pipe(failPipe) < 0) {
throw new IOException("pipe() failed");
}
int[] childStdioFds = new int[3];
childStdioFds[0] = (fds[0] != -1) ? fds[0] : stdioPipes[0].read(0);
childStdioFds[1] = (fds[1] != -1) ? fds[1] : stdioPipes[1].read(1);
childStdioFds[2] = (fds[2] != -1) ? fds[2] : stdioPipes[2].read(1);
int childFailFd = failPipe.read(1);
CCharPointer filep = filePin.addressOfArrayElement(0);
CCharPointer dirp = (dir != null) ? dirPin.addressOfArrayElement(0) : WordFactory.nullPointer();
int childPid = Java_lang_UNIXProcess_Supplement.doForkAndExec(filep, dirp, argv, envp, childStdioFds, childFailFd);
if (childPid < 0) {
throw new IOException("fork() failed");
}
// If we are here, we are the parent.
// store fds of our pipe ends for caller, close unused fds (those of the child)
fds[0] = fds[1] = fds[2] = -1;
if (stdioPipes[0].isNonNull()) {
fds[0] = stdioPipes[0].read(1);
Unistd.close(stdioPipes[0].read(0));
}
if (stdioPipes[1].isNonNull()) {
fds[1] = stdioPipes[1].read(0);
Unistd.close(stdioPipes[1].read(1));
}
if (stdioPipes[2].isNonNull()) {
fds[2] = stdioPipes[2].read(0);
Unistd.close(stdioPipes[2].read(1));
}
Unistd.close(failPipe.read(1));
// read status from child
final int intSize = SizeOf.get(CIntPointer.class);
CIntPointer pErrno = StackValue.get(intSize);
SignedWord failBytes = Java_lang_UNIXProcess_Supplement.readEntirely(failPipe.read(0), pErrno, WordFactory.unsigned(intSize));
Unistd.close(failPipe.read(0));
if (failBytes.equal(0)) {
// success: pipe closed during exec()
return childPid;
} else if (failBytes.equal(SizeOf.get(CIntPointer.class))) {
int errbuflen = 256;
try (PinnedObject errbuf = PinnedObject.create(new byte[errbuflen])) {
CCharPointer detailCstr = Errno.strerror_r(pErrno.read(), errbuf.addressOfArrayElement(0), WordFactory.unsigned(errbuflen));
String detail = CTypeConversion.toJavaString(detailCstr);
throw new IOException("error=" + pErrno.read() + ", " + detail);
}
} else {
throw new IOException("unexpected data from child");
}
} catch (IOException e) {
// NOTE: not a finally statement because when successful, some pipes need to stay open
for (int fd : pipes) {
if (fd != -1) {
Unistd.close(fd);
}
}
throw e;
}
}
use of org.graalvm.nativeimage.c.type.CCharPointerPointer in project graal by oracle.
the class PosixJavaLangSubstitutions method environ.
// This code is derived from the C implementation of the JDK.
@Substitute
static byte[][] environ() {
CCharPointerPointer environ = LibCHelper.getEnviron();
int count = 0;
for (int i = 0; environ.read(i).isNonNull(); i++) {
/* Ignore corrupted environment variables */
if (LibC.strchr(environ.read(i), '=').isNonNull()) {
count++;
}
}
byte[][] result = new byte[count * 2][];
int j = 0;
for (int i = 0; environ.read(i).isNonNull(); i++) {
CCharPointer varBeg = environ.read(i);
CCharPointer varEnd = LibC.strchr(varBeg, '=');
/* Ignore corrupted environment variables */
if (varEnd.isNonNull()) {
CCharPointer valBeg = varEnd.addressOf(1);
int varLength = (int) PointerUtils.absoluteDifference(varEnd, varBeg).rawValue();
int valLength = (int) LibC.strlen(valBeg).rawValue();
byte[] var = new byte[varLength];
SubstrateUtil.wrapAsByteBuffer(varBeg, varLength).get(var);
result[2 * j] = var;
byte[] val = new byte[valLength];
SubstrateUtil.wrapAsByteBuffer(valBeg, valLength).get(val);
result[2 * j + 1] = val;
j++;
}
}
assert j == count;
return result;
}
use of org.graalvm.nativeimage.c.type.CCharPointerPointer in project graal by oracle.
the class PosixJavaLangSubstitutions method uninterruptibleForkAndExec.
@Uninterruptible(reason = "fragile state after fork()")
private static int uninterruptibleForkAndExec(CCharPointer file, CCharPointer dir, CCharPointerPointer argv, CCharPointerPointer envp, int[] stdioFds, int initialFailFd, PointerBase buffer, int buflen, CCharPointer procFdsPath, CCharPointer searchPaths, CCharPointer searchPathSeparator) {
int childPid;
childPid = UnistdNoTransitions.fork();
if (childPid != 0) {
return childPid;
}
// If we are here, we are the child process.
int failFd = initialFailFd;
try {
// In case of an error, we "return" to end up in the finally block below and notify the
// parent process of the failure.
final int gotoFinally = -1;
if (Java_lang_UNIXProcess_Supplement.dup2(stdioFds[0], 0) < 0) {
return gotoFinally;
}
if (Java_lang_UNIXProcess_Supplement.dup2(stdioFds[1], 1) < 0) {
return gotoFinally;
}
if (Java_lang_UNIXProcess_Supplement.dup2(stdioFds[2], 2) < 0) {
return gotoFinally;
}
if (Java_lang_UNIXProcess_Supplement.dup2(failFd, 3) < 0) {
return gotoFinally;
}
failFd = 3;
// FD_CLOEXEC: close fail pipe on exec() to indicate success to parent
if (Fcntl.fcntl_no_transition(failFd, Fcntl.F_SETFD(), Fcntl.FD_CLOEXEC()) < 0) {
return gotoFinally;
}
/*
* opendir() below allocates a file descriptor. We close failFd+1 so it should become
* the descriptor allocated to opendir() and we can avoid closing it together with the
* other descriptors.
*/
final int maxFd = failFd + 1;
if (UnistdNoTransitions.close(maxFd) < 0) {
return gotoFinally;
}
if (procFdsPath.isNull()) {
// We have no procfs, resort to close file descriptors by trial and error
int maxOpenFds = (int) UnistdNoTransitions.sysconf(Unistd._SC_OPEN_MAX());
for (int fd = maxFd + 1; fd < maxOpenFds; fd++) {
if (UnistdNoTransitions.close(fd) != 0 && Errno.errno() != Errno.EBADF()) {
return gotoFinally;
}
}
} else {
DIR fddir = Dirent.opendir_no_transition(procFdsPath);
if (fddir.isNull()) {
return gotoFinally;
}
dirent dirent = WordFactory.pointer(buffer.rawValue());
direntPointer direntptr = StackValue.get(SizeOf.get(direntPointer.class));
int status;
while ((status = Dirent.readdir_r_no_transition(fddir, dirent, direntptr)) == 0 && direntptr.read().isNonNull()) {
CCharPointerPointer endptr = StackValue.get(SizeOf.get(CCharPointerPointer.class));
long fd = LibC.strtol(dirent.d_name(), endptr, 10);
if (fd > maxFd && endptr.read().isNonNull() && endptr.read().read() == '\0') {
UnistdNoTransitions.close((int) fd);
}
}
if (status != 0) {
// readdir_r() does not set errno
Errno.set_errno(status);
return gotoFinally;
}
Dirent.closedir_no_transition(fddir);
}
if (dir.isNonNull()) {
if (UnistdNoTransitions.chdir(dir) < 0) {
return gotoFinally;
}
}
CCharPointerPointer actualEnvp = envp;
if (actualEnvp.isNull()) {
actualEnvp = LibCHelper.getEnviron();
}
if (LibC.strchr(file, '/').isNonNull()) {
UnistdNoTransitions.execve(argv.read(0), argv, actualEnvp);
} else {
// Scan PATH for the file to execute. We cannot use execvpe()
// because it is a GNU extension that is not universally available.
final int fileStrlen = (int) LibC.strlen(file).rawValue();
int stickyErrno = 0;
final CCharPointerPointer saveptr = StackValue.get(SizeOf.get(CCharPointerPointer.class));
saveptr.write(WordFactory.nullPointer());
CCharPointer searchDir = LibC.strtok_r(searchPaths, searchPathSeparator, saveptr);
while (searchDir.isNonNull()) {
CCharPointer bufptr = WordFactory.pointer(buffer.rawValue());
int len0 = (int) LibC.strlen(searchDir).rawValue();
if (len0 + fileStrlen + 2 > buflen) {
Errno.set_errno(Errno.ENAMETOOLONG());
continue;
}
if (len0 > 0) {
LibC.strcpy(bufptr, searchDir);
if (bufptr.read(len0 - 1) != '/') {
bufptr.write(len0, (byte) '/');
len0++;
}
}
LibC.strcpy(bufptr.addressOf(len0), file);
UnistdNoTransitions.execve(bufptr, argv, actualEnvp);
int e = Errno.errno();
if (e == Errno.EACCES()) {
// as exec(): report EACCES unless we succeed later
stickyErrno = e;
} else if (e == Errno.ENOENT() || e == Errno.ENOTDIR() || e == Errno.ELOOP() || e == Errno.ESTALE() || e == Errno.ENODEV() || e == Errno.ETIMEDOUT()) {
// ignore
} else {
stickyErrno = e;
// bad
break;
}
searchDir = LibC.strtok_r(WordFactory.nullPointer(), searchPathSeparator, saveptr);
}
if (stickyErrno != 0) {
Errno.set_errno(stickyErrno);
}
}
// If we are here, exec certainly failed.
} catch (Throwable t) {
Errno.set_errno(Integer.MIN_VALUE);
} finally {
try {
// notify parent of failure
final int intSize = SizeOf.get(CIntPointer.class);
CIntPointer pErrno = StackValue.get(intSize);
pErrno.write(Errno.errno());
Java_lang_UNIXProcess_Supplement.writeEntirely(failFd, pErrno, WordFactory.unsigned(intSize));
UnistdNoTransitions.close(failFd);
} finally {
UnistdNoTransitions._exit(-1);
}
}
throw VMError.shouldNotReachHere();
}
use of org.graalvm.nativeimage.c.type.CCharPointerPointer in project graal by oracle.
the class NativeImageGeneratorRunner method buildImage.
@SuppressWarnings("try")
private int buildImage(String[] arguments, String[] classpath, ClassLoader classLoader) {
if (!verifyValidJavaVersionAndPlatform()) {
return -1;
}
Timer totalTimer = new Timer("[total]", false);
ForkJoinPool analysisExecutor = null;
ForkJoinPool compilationExecutor = null;
try (StopTimer ignored = totalTimer.start()) {
ImageClassLoader imageClassLoader;
Timer classlistTimer = new Timer("classlist", false);
try (StopTimer ignored1 = classlistTimer.start()) {
imageClassLoader = ImageClassLoader.create(defaultPlatform(), classpath, classLoader);
}
HostedOptionParser optionParser = new HostedOptionParser(imageClassLoader);
String[] remainingArgs = optionParser.parse(arguments);
if (remainingArgs.length > 0) {
throw UserError.abort("Unknown options: " + Arrays.toString(remainingArgs));
}
/*
* We do not have the VMConfiguration and the HostedOptionValues set up yet, so we need
* to pass the OptionValues explicitly when accessing options.
*/
OptionValues parsedHostedOptions = new OptionValues(optionParser.getHostedValues());
DebugContext debug = DebugContext.create(parsedHostedOptions, new GraalDebugHandlersFactory(GraalAccess.getOriginalSnippetReflection()));
String imageName = NativeImageOptions.Name.getValue(parsedHostedOptions);
if (imageName.length() == 0) {
throw UserError.abort("No output file name specified. " + "Use '" + SubstrateOptionsParser.commandArgument(NativeImageOptions.Name, "<output-file>") + "'.");
}
// print the time here to avoid interactions with flags processing
classlistTimer.print();
Map<Method, CEntryPointData> entryPoints = new HashMap<>();
Method mainEntryPoint = null;
JavaMainSupport javaMainSupport = null;
AbstractBootImage.NativeImageKind k = AbstractBootImage.NativeImageKind.valueOf(NativeImageOptions.Kind.getValue(parsedHostedOptions));
if (k == AbstractBootImage.NativeImageKind.EXECUTABLE) {
String className = NativeImageOptions.Class.getValue(parsedHostedOptions);
if (className == null || className.length() == 0) {
throw UserError.abort("Must specify main entry point class when building " + AbstractBootImage.NativeImageKind.EXECUTABLE + " native image. " + "Use '" + SubstrateOptionsParser.commandArgument(NativeImageOptions.Class, "<fully-qualified-class-name>") + "'.");
}
Class<?> mainClass;
try {
mainClass = Class.forName(className, false, classLoader);
} catch (ClassNotFoundException ex) {
throw UserError.abort("Main entry point class '" + className + "' not found.");
}
String mainEntryPointName = NativeImageOptions.Method.getValue(parsedHostedOptions);
if (mainEntryPointName == null || mainEntryPointName.length() == 0) {
throw UserError.abort("Must specify main entry point method when building " + AbstractBootImage.NativeImageKind.EXECUTABLE + " native image. " + "Use '" + SubstrateOptionsParser.commandArgument(NativeImageOptions.Method, "<method-name>") + "'.");
}
try {
/* First look for an main method with the C-level signature for arguments. */
mainEntryPoint = mainClass.getDeclaredMethod(mainEntryPointName, int.class, CCharPointerPointer.class);
} catch (NoSuchMethodException ignored2) {
try {
/*
* If no C-level main method was found, look for a Java-level main method
* and use our wrapper to invoke it.
*/
Method javaMainMethod = mainClass.getDeclaredMethod(mainEntryPointName, String[].class);
javaMainMethod.setAccessible(true);
if (javaMainMethod.getReturnType() != void.class) {
throw UserError.abort("Java main method must have return type void. Change the return type of method '" + mainClass.getName() + "." + mainEntryPointName + "(String[])'.");
}
final int mainMethodModifiers = javaMainMethod.getModifiers();
if (!Modifier.isPublic(mainMethodModifiers)) {
throw UserError.abort("Method '" + mainClass.getName() + "." + mainEntryPointName + "(String[])' is not accessible. Please make it 'public'.");
}
javaMainSupport = new JavaMainSupport(javaMainMethod);
mainEntryPoint = JavaMainWrapper.class.getDeclaredMethod("run", int.class, CCharPointerPointer.class);
} catch (NoSuchMethodException ex) {
throw UserError.abort("Method '" + mainClass.getName() + "." + mainEntryPointName + "' is declared as the main entry point but it can not be found. " + "Make sure that class '" + mainClass.getName() + "' is on the classpath and that method '" + mainEntryPointName + "(String[])' exists in that class.");
}
}
CEntryPoint annotation = mainEntryPoint.getAnnotation(CEntryPoint.class);
if (annotation == null) {
throw UserError.abort("Entry point must have the '@" + CEntryPoint.class.getSimpleName() + "' annotation");
}
entryPoints.put(mainEntryPoint, CEntryPointData.create(mainEntryPoint));
Class<?>[] pt = mainEntryPoint.getParameterTypes();
if (pt.length != 2 || pt[0] != int.class || pt[1] != CCharPointerPointer.class || mainEntryPoint.getReturnType() != int.class) {
throw UserError.abort("Main entry point must have signature 'int main(int argc, CCharPointerPointer argv)'.");
}
}
int maxConcurrentThreads = NativeImageOptions.getMaximumNumberOfConcurrentThreads(parsedHostedOptions);
analysisExecutor = Inflation.createExecutor(debug, NativeImageOptions.getMaximumNumberOfAnalysisThreads(parsedHostedOptions));
compilationExecutor = Inflation.createExecutor(debug, maxConcurrentThreads);
generator = new NativeImageGenerator(imageClassLoader, optionParser);
generator.run(entryPoints, mainEntryPoint, javaMainSupport, imageName, k, SubstitutionProcessor.IDENTITY, analysisExecutor, compilationExecutor, optionParser.getRuntimeOptionNames());
} catch (InterruptImageBuilding e) {
if (analysisExecutor != null) {
analysisExecutor.shutdownNow();
}
if (compilationExecutor != null) {
compilationExecutor.shutdownNow();
}
e.getReason().ifPresent(NativeImageGeneratorRunner::info);
return 0;
} catch (UserException e) {
e.getMessages().iterator().forEachRemaining(NativeImageGeneratorRunner::reportUserError);
return -1;
} catch (AnalysisError e) {
NativeImageGeneratorRunner.reportUserError(e.getMessage());
return -1;
} catch (ParallelExecutionException pee) {
boolean hasUserError = false;
for (Throwable exception : pee.getExceptions()) {
if (exception instanceof UserException) {
((UserException) exception).getMessages().iterator().forEachRemaining(NativeImageGeneratorRunner::reportUserError);
hasUserError = true;
} else if (exception instanceof AnalysisError) {
NativeImageGeneratorRunner.reportUserError(exception.getMessage());
hasUserError = true;
}
}
if (hasUserError) {
return -1;
}
if (pee.getExceptions().size() > 1) {
System.err.println(pee.getExceptions().size() + " fatal errors detected:");
}
for (Throwable exception : pee.getExceptions()) {
NativeImageGeneratorRunner.reportFatalError(exception);
}
return -1;
} catch (Throwable e) {
NativeImageGeneratorRunner.reportFatalError(e);
return -1;
}
totalTimer.print();
return 0;
}
use of org.graalvm.nativeimage.c.type.CCharPointerPointer in project graal by oracle.
the class NativeClosure method call.
private Object call(WordPointer argPointers, ByteBuffer retBuffer) {
Target_com_oracle_truffle_nfi_impl_LibFFIType[] argTypes = signature.getArgTypes();
int length = argTypes.length - skippedArgCount;
if (retBuffer != null) {
length++;
}
int argIdx = 0;
Object[] args = new Object[length];
for (int i = 0; i < argTypes.length; i++) {
Object type = argTypes[i];
if (Target_com_oracle_truffle_nfi_impl_LibFFIType_StringType.class.isInstance(type)) {
CCharPointerPointer argPtr = argPointers.read(i);
args[argIdx++] = TruffleNFISupport.utf8ToJavaString(argPtr.read());
} else if (Target_com_oracle_truffle_nfi_impl_LibFFIType_ObjectType.class.isInstance(type)) {
WordPointer argPtr = argPointers.read(i);
args[argIdx++] = ImageSingletons.lookup(TruffleNFISupport.class).resolveHandle(argPtr.read());
} else if (Target_com_oracle_truffle_nfi_impl_LibFFIType_EnvType.class.isInstance(type)) {
// skip
} else {
WordPointer argPtr = argPointers.read(i);
args[argIdx++] = SubstrateUtil.wrapAsByteBuffer(argPtr, argTypes[i].size);
}
}
if (retBuffer != null) {
args[argIdx] = retBuffer;
}
return callTarget.call(args);
}
Aggregations