Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions substratevm/mx.substratevm/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
"java.compiler",
"jdk.jfr",
"jdk.management",
"jdk.management.jfr",
"jdk.zipfs",
],
"requiresConcealed" : {
"java.base" : [
Expand Down Expand Up @@ -283,9 +283,6 @@
"jdk.management": [
"com.sun.management.internal"
],
"jdk.management.agent": [
"jdk.internal.agent",
],
"jdk.jfr": [
"jdk.jfr.events",
"jdk.jfr.internal",
Expand Down Expand Up @@ -1327,8 +1324,6 @@
"requires": [
"java.management",
"jdk.management",
"jdk.management.agent",
"jdk.management.jfr",
],
"uses" : [
"org.graalvm.nativeimage.Platform",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.graalvm.nativeimage.Platform.WINDOWS;
import org.graalvm.nativeimage.Platforms;

import com.oracle.svm.core.jdk.management.ManagementAgentModule;
import com.oracle.svm.core.option.APIOption;
import com.oracle.svm.core.option.HostedOptionKey;
import com.oracle.svm.core.option.LocatableMultiOptionValue;
Expand Down Expand Up @@ -121,7 +122,7 @@ public static boolean hasJvmstatSupport() {

@Fold
public static boolean hasJmxServerSupport() {
return hasAllOrKeywordMonitoringSupport(MONITORING_JMXSERVER_NAME) && !Platform.includedIn(WINDOWS.class);
return hasAllOrKeywordMonitoringSupport(MONITORING_JMXSERVER_NAME) && !Platform.includedIn(WINDOWS.class) && ManagementAgentModule.isPresent();
}

@Fold
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
*/
package com.oracle.svm.core.jdk;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Currency;
import java.util.NavigableMap;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.LogManager;
import java.util.function.BooleanSupplier;

import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.annotate.Alias;
Expand All @@ -41,6 +42,7 @@
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;

/*
* Lazily initialized cache fields of collection classes need to be reset. They are not needed in
Expand Down Expand Up @@ -208,30 +210,29 @@ final class Target_java_util_Currency {
* so that during runtime the first time the log handler is accessed the equivalent shutdown hook is
* added.
*/
@TargetClass(value = LogManager.class)
@TargetClass(className = "java.util.logging.LogManager", onlyWith = JavaLoggingModule.IsPresent.class)
final class Target_java_util_logging_LogManager {

@Inject @RecomputeFieldValue(kind = Kind.NewInstance, declClass = AtomicBoolean.class) private AtomicBoolean addedShutdownHook = new AtomicBoolean();

@Alias static LogManager manager;
@Alias static Target_java_util_logging_LogManager manager;

@Alias
native void ensureLogManagerInitialized();

@Substitute
public static LogManager getLogManager() {
public static Target_java_util_logging_LogManager getLogManager() {
/* First performing logic originally in getLogManager. */
if (manager == null) {
return manager;
}
Target_java_util_logging_LogManager managerAlias = SubstrateUtil.cast(manager, Target_java_util_logging_LogManager.class);
managerAlias.ensureLogManagerInitialized();
manager.ensureLogManagerInitialized();

/* Logic for adding shutdown hook. */
if (!managerAlias.addedShutdownHook.getAndSet(true)) {
if (!manager.addedShutdownHook.getAndSet(true)) {
/* Add a shutdown hook to close the global handlers. */
try {
Runtime.getRuntime().addShutdownHook(SubstrateUtil.cast(new Target_java_util_logging_LogManager_Cleaner(managerAlias), Thread.class));
Runtime.getRuntime().addShutdownHook(SubstrateUtil.cast(new Target_java_util_logging_LogManager_Cleaner(manager), Thread.class));
} catch (IllegalStateException e) {
/* If the VM is already shutting down, we do not need to register shutdownHook. */
}
Expand All @@ -241,7 +242,7 @@ public static LogManager getLogManager() {
}
}

@TargetClass(value = LogManager.class, innerClass = "Cleaner")
@TargetClass(className = "java.util.logging.LogManager", innerClass = "Cleaner", onlyWith = JavaLoggingModule.IsPresent.class)
final class Target_java_util_logging_LogManager_Cleaner {

@Alias
Expand All @@ -251,6 +252,48 @@ final class Target_java_util_logging_LogManager_Cleaner {
}
}

class JavaLoggingModule {

private static final Object logManager;
private static final Method logManagerGetProperty;

static {
var javaLoggingModule = ModuleLayer.boot().findModule("java.logging");
if (javaLoggingModule.isPresent() && JavaLoggingModule.class.getModule().canRead(javaLoggingModule.get())) {
var logManagerClass = ReflectionUtil.lookupClass(false, "java.util.logging.LogManager");
var logManagerGetLogManagerMethod = ReflectionUtil.lookupMethod(logManagerClass, "getLogManager");
logManagerGetProperty = ReflectionUtil.lookupMethod(logManagerClass, "getProperty", String.class);
try {
logManager = logManagerGetLogManagerMethod.invoke(null);
} catch (ReflectiveOperationException e) {
throw VMError.shouldNotReachHere("Unable to reflectively invoke java.util.logging.LogManager.getLogManager()", e);
}
} else {
logManager = null;
logManagerGetProperty = null;
}
}

static String logManagerGetProperty(String name) {
try {
return (String) logManagerGetProperty.invoke(logManager, name);
} catch (ReflectiveOperationException e) {
throw VMError.shouldNotReachHere("Unable to reflectively invoke java.util.logging.LogManager.getProperty(String)", e);
}
}

private static boolean isPresent() {
return logManager != null;
}

static class IsPresent implements BooleanSupplier {
@Override
public boolean getAsBoolean() {
return isPresent();
}
}
}

/** Dummy class to have a class with the file's name. */
public final class JavaUtilSubstitutions {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
package com.oracle.svm.core.jdk;

import java.util.logging.LogManager;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.InjectAccessors;
import com.oracle.svm.core.annotate.TargetClass;
Expand All @@ -36,13 +34,13 @@ class FormatAccessors {

// format string for printing the log record
private static String getLoggingProperty(String name) {
return LogManager.getLogManager().getProperty(name);
return JavaLoggingModule.logManagerGetProperty(name);
}

private static String format = null;

@SuppressWarnings("unused")
public static String getFormat(java.util.logging.SimpleFormatter parent) {
public static String getFormat(Object parent) {
if (format == null) {
/*
* If multiple threads are doing the initialization at the same time it is not a problem
Expand All @@ -55,12 +53,12 @@ public static String getFormat(java.util.logging.SimpleFormatter parent) {
}

@SuppressWarnings("unused")
public static void setFormat(java.util.logging.SimpleFormatter parent, String f) {
public static void setFormat(Object parent, String f) {
format = f;
}
}

@TargetClass(value = java.util.logging.SimpleFormatter.class)
@TargetClass(className = "java.util.logging.SimpleFormatter", onlyWith = JavaLoggingModule.IsPresent.class)
public final class Target_java_util_logging_SimpleFormatter {

@Alias @InjectAccessors(FormatAccessors.class)//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

package com.oracle.svm.core.jdk;

import java.util.function.BooleanSupplier;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.Substitute;
Expand All @@ -37,8 +39,8 @@
* which is not needed as it only implements the native maxObjectInspectionAge() method, which in
* turn is {@link Target_sun_rmi_transport_GC#maxObjectInspectionAge substituted in here}.
*/
@TargetClass(className = "sun.rmi.transport.GC")
final class Target_sun_rmi_transport_GC {
@TargetClass(className = "sun.rmi.transport.GC", onlyWith = JavaRMIModuleAvailable.class)
public final class Target_sun_rmi_transport_GC {
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset)//
private static Thread daemon = null;

Expand All @@ -47,3 +49,21 @@ public static long maxObjectInspectionAge() {
return Heap.getHeap().getMillisSinceLastWholeHeapExamined();
}
}

class JavaRMIModuleAvailable implements BooleanSupplier {

private static final boolean hasModule;

static {
var module = ModuleLayer.boot().findModule("java.rmi");
if (module.isPresent()) {
JavaRMIModuleAvailable.class.getModule().addReads(module.get());
}
hasModule = module.isPresent();
}

@Override
public boolean getAsBoolean() {
return hasModule;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2023, 2023, 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.jdk.management;

import java.lang.reflect.Method;
import java.util.Optional;
import java.util.function.BooleanSupplier;

import com.oracle.svm.core.util.VMError;
import com.oracle.svm.util.ReflectionUtil;

public class ManagementAgentModule {

private static final Method agentStartAgent;
private static final Method agentError;

static final String CONFIG_FILE_ACCESS_DENIED;
static final String CONFIG_FILE_CLOSE_FAILED;
static final String CONFIG_FILE_NOT_FOUND;
static final String CONFIG_FILE_OPEN_FAILED;

static {
Optional<Module> agentModule = ModuleLayer.boot().findModule("jdk.management.agent");
if (agentModule.isPresent()) {
ManagementAgentModule.class.getModule().addReads(agentModule.get());
var agentClass = ReflectionUtil.lookupClass(false, "jdk.internal.agent.Agent");
agentStartAgent = ReflectionUtil.lookupMethod(agentClass, "startAgent");
agentError = ReflectionUtil.lookupMethod(agentClass, "error", String.class, String.class);
var agentConfigurationErrorClass = ReflectionUtil.lookupClass(false, "jdk.internal.agent.AgentConfigurationError");
CONFIG_FILE_ACCESS_DENIED = ReflectionUtil.readStaticField(agentConfigurationErrorClass, "CONFIG_FILE_ACCESS_DENIED");
CONFIG_FILE_CLOSE_FAILED = ReflectionUtil.readStaticField(agentConfigurationErrorClass, "CONFIG_FILE_CLOSE_FAILED");
CONFIG_FILE_NOT_FOUND = ReflectionUtil.readStaticField(agentConfigurationErrorClass, "CONFIG_FILE_NOT_FOUND");
CONFIG_FILE_OPEN_FAILED = ReflectionUtil.readStaticField(agentConfigurationErrorClass, "CONFIG_FILE_OPEN_FAILED");
} else {
agentStartAgent = null;
agentError = null;
CONFIG_FILE_ACCESS_DENIED = null;
CONFIG_FILE_CLOSE_FAILED = null;
CONFIG_FILE_NOT_FOUND = null;
CONFIG_FILE_OPEN_FAILED = null;
}
}

public static boolean isPresent() {
return agentStartAgent != null;
}

static class IsPresent implements BooleanSupplier {
@Override
public boolean getAsBoolean() {
return isPresent();
}
}

static void agentError(String key, String message) {
try {
agentError.invoke(null, key, message);
} catch (ReflectiveOperationException e) {
throw VMError.shouldNotReachHere("Unable to reflectively invoke jdk.internal.agent.Agent.error(String, String)", e);
}
}

static void agentStartAgent() {
try {
agentStartAgent.invoke(null);
} catch (ReflectiveOperationException e) {
throw VMError.shouldNotReachHere("Unable to reflectively invoke jdk.internal.agent.Agent.startAgent()", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ManagementAgentStartupHook implements com.oracle.svm.core.jdk.Runti
@Override
public void execute(boolean isFirstIsolate) {
try {
jdk.internal.agent.Agent.startAgent();
ManagementAgentModule.agentStartAgent();
} catch (Exception e) {
throw VMError.shouldNotReachHere("ManagementFeature start-up hook failed: " + e);
}
Expand Down
Loading