Skip to content

Commit

Permalink
Provide replacement for CompilerCommandPlugin based ArgumentVector fu…
Browse files Browse the repository at this point in the history
…nctions
  • Loading branch information
olpaw committed Feb 26, 2020
1 parent d76d17d commit 6830a30
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,40 @@ public static void exec(Path executable, String... args) {
ImageSingletons.lookup(ProcessPropertiesSupport.class).exec(executable, args);
}

/**
* If the running image is an executable (not a shared library) the program name that is stored
* in the argument vector of the running process gets returned.
*
* @since 20.1
*/
public static String getArgumentVectorProgramName() {
return ImageSingletons.lookup(ProcessPropertiesSupport.class).getArgumentVectorProgramName();
}

/**
* If the running image is an executable (not a shared library) the program name that is stored
* in the argument vector of the running process gets replaced with the give name. If the size
* of the argument vector is too small for the given name it gets truncated so that the
* environment vector next to the argument vector does not get corrupted.
*
* @return true, if given name had to be truncated to fit in the argument vector
*
* @since 20.1
*/
public static boolean setArgumentVectorProgramName(String name) {
return ImageSingletons.lookup(ProcessPropertiesSupport.class).setArgumentVectorProgramName(name);
}

/**
* If the running image is an executable (not a shared library) the total size of the argument
* vector of the running process gets returned.
*
* @since 20.1
*/
public static int getArgumentVectorBlockSize() {
return ImageSingletons.lookup(ProcessPropertiesSupport.class).getArgumentVectorBlockSize();
}

private ProcessProperties() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,17 @@ public interface ProcessPropertiesSupport {
int waitForProcessExit(long processID);

void exec(Path executable, String[] args);

default int getArgumentVectorBlockSize() {
throw new UnsupportedOperationException();
}

default String getArgumentVectorProgramName() {
throw new UnsupportedOperationException();
}

@SuppressWarnings("unused")
default boolean setArgumentVectorProgramName(String name) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;
import org.graalvm.word.PointerBase;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.BaseProcessPropertiesSupport;
import com.oracle.svm.core.posix.headers.Dlfcn;
import com.oracle.svm.core.posix.headers.LibC;
import com.oracle.svm.core.posix.headers.Signal;
import com.oracle.svm.core.posix.headers.Stdlib;
import com.oracle.svm.core.posix.headers.Unistd;

public abstract class PosixProcessPropertiesSupport implements ProcessPropertiesSupport {
public abstract class PosixProcessPropertiesSupport extends BaseProcessPropertiesSupport {

@Override
public long getProcessID() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand All @@ -42,13 +43,14 @@
import org.graalvm.word.PointerBase;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.BaseProcessPropertiesSupport;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.core.windows.headers.Process;
import com.oracle.svm.core.windows.headers.WinBase;

@Platforms(Platform.WINDOWS.class)
public class WindowsProcessPropertiesSupport implements ProcessPropertiesSupport {
public class WindowsProcessPropertiesSupport extends BaseProcessPropertiesSupport {

@Override
public String getExecutableName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2020, 2020, 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;

import org.graalvm.nativeimage.impl.ProcessPropertiesSupport;

public abstract class BaseProcessPropertiesSupport implements ProcessPropertiesSupport {
@Override
public int getArgumentVectorBlockSize() {
return JavaMainWrapper.getCRuntimeArgumentBlockLength();
}

@Override
public String getArgumentVectorProgramName() {
return JavaMainWrapper.getCRuntimeArgument0();
}

@Override
public boolean setArgumentVectorProgramName(String name) {
return JavaMainWrapper.setCRuntimeArgument0(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static int run(int paramArgc, CCharPointerPointer paramArgv) {
* @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() {
public static int getCRuntimeArgumentBlockLength() {
VMError.guarantee(argv.notEqual(WordFactory.zero()) && argc > 0, "Requires JavaMainWrapper.run(int, CCharPointerPointer) entry point!");

CCharPointer firstArgPos = argv.read(0);
Expand All @@ -209,10 +209,12 @@ public static long getCRuntimeArgumentBlockLength() {
// 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();
return Math.toIntExact(argvLength.rawValue());
}

public static boolean setCRuntimeArgument0(String arg0) {
VMError.guarantee(argv.notEqual(WordFactory.zero()) && argc > 0, "Requires JavaMainWrapper.run(int, CCharPointerPointer) entry point!");

boolean arg0truncation = false;

try (CCharPointerHolder arg0Pin = CTypeConversion.toCString(arg0)) {
Expand All @@ -238,6 +240,8 @@ public static boolean setCRuntimeArgument0(String arg0) {
}

public static String getCRuntimeArgument0() {
VMError.guarantee(argv.notEqual(WordFactory.zero()) && argc > 0, "Requires JavaMainWrapper.run(int, CCharPointerPointer) entry point!");

return CTypeConversion.toJavaString(argv.read(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,11 @@ static void install() {
Signal.handle(new Signal("USR1"), new DumpHeapReport());
}

@SuppressWarnings("deprecation")
@NeverInline("Ensure ClassCastException gets caught")
private static void performHeapDump(FileOutputStream fileOutputStream) throws Exception {
Object[] args = new Object[]{"HeapDump.dumpHeap(FileOutputStream, Boolean)Boolean", fileOutputStream, Boolean.TRUE};
if (!((Boolean) Compiler.command(args))) {
throw new RuntimeException();
private static void performHeapDump(FileOutputStream fileOutputStream) {
try {
RuntimeSupport.getRuntimeSupport().dumpHeap(fileOutputStream, true);
} catch (IOException e) {
Log.log().string("HeapDump failed: ").string(e.getMessage()).newline();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2019, Oracle and/or its affiliates.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -35,6 +35,8 @@
import java.util.ArrayList;
import java.util.Arrays;

import org.graalvm.nativeimage.ProcessProperties;

import com.oracle.truffle.llvm.toolchain.launchers.common.Driver;

public final class BinUtil {
Expand Down Expand Up @@ -81,7 +83,7 @@ private static String getProcessName() {
String binPathName = System.getProperty("org.graalvm.launcher.executablename");

if (binPathName == null) {
binPathName = (String) java.lang.Compiler.command(new Object[]{"com.oracle.svm.core.JavaMainWrapper.getCRuntimeArgument0()String"});
binPathName = ProcessProperties.getArgumentVectorProgramName();

if (binPathName == null) {
return null;
Expand Down

0 comments on commit 6830a30

Please sign in to comment.