Skip to content
Closed
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
30 changes: 22 additions & 8 deletions src/java.base/share/classes/java/security/SecureClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.function.Function;
import jdk.internal.misc.CDS;

import jdk.internal.event.ClassFileDefineEvent;

/**
* This class extends {@code ClassLoader} with additional support for defining
* classes with an associated code source and permissions.
Expand Down Expand Up @@ -138,11 +140,16 @@ protected SecureClassLoader(String name, ClassLoader parent) {
* a different set of certificates than this class, or if
* the class name begins with "java.".
*/
protected final Class<?> defineClass(String name,
byte[] b, int off, int len,
CodeSource cs)
{
return defineClass(name, b, off, len, getProtectionDomain(cs));
protected final Class<?> defineClass(String name, byte[] b, int off, int len, CodeSource cs) {
final var cls = defineClass(name, b, off, len, getProtectionDomain(cs));

final var evt = new ClassFileDefineEvent();

evt.definedClass = cls;
evt.path = cs.getLocation().toExternalForm();
evt.commit();

return cls;
}

/**
Expand Down Expand Up @@ -171,10 +178,17 @@ protected final Class<?> defineClass(String name,
*
* @since 1.5
*/
protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
CodeSource cs)
protected final Class<?> defineClass(String name, java.nio.ByteBuffer b, CodeSource cs)
{
return defineClass(name, b, getProtectionDomain(cs));
final var cls = defineClass(name, b, getProtectionDomain(cs));

final var evt = new ClassFileDefineEvent();

evt.definedClass = cls;
evt.path = cs.getLocation().toExternalForm();
evt.commit();

return cls;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2025, 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 jdk.internal.event;

import java.net.URL;

/**
* Event recording that a Class has been loaded and defined by a ClassLoader.
*/
public final class ClassFileDefineEvent extends Event {
public Class<?> definedClass;
public String path;
}
45 changes: 45 additions & 0 deletions src/jdk.jfr/share/classes/jdk/jfr/events/ClassFileDefineEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025, 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 jdk.jfr.events;

import jdk.jfr.Category;
import jdk.jfr.Description;
import jdk.jfr.Label;
import jdk.jfr.Name;
import jdk.jfr.internal.MirrorEvent;
import jdk.jfr.internal.Type;

@Category("Java Virtual Machine, Class Loading")
@Label("ClassFile Define")
@Name("jdk.ClassFileDefine")
public final class ClassFileDefineEvent extends MirrorEvent {
@Label("Defined Class Name")
public Class<?> definedClass;

@Label("Path")
@Description("The path/url location of the classfile loaded")
public String path;
}
2 changes: 2 additions & 0 deletions src/jdk.jfr/share/classes/jdk/jfr/internal/JDKEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import jdk.jfr.Event;
import jdk.jfr.events.ActiveRecordingEvent;
import jdk.jfr.events.ActiveSettingEvent;
import jdk.jfr.events.ClassFileDefineEvent;
import jdk.jfr.events.ContainerCPUThrottlingEvent;
import jdk.jfr.events.ContainerCPUUsageEvent;
import jdk.jfr.events.ContainerConfigurationEvent;
Expand All @@ -56,6 +57,7 @@ public final class JDKEvents {
ActiveRecordingEvent.class,
// jdk.internal.event.* classes need their mirror
// event class to be listed in the MirrorEvents class.
jdk.internal.event.ClassFileDefineEvent.class,
jdk.internal.event.DeserializationEvent.class,
jdk.internal.event.ErrorThrownEvent.class,
jdk.internal.event.ExceptionStatisticsEvent.class,
Expand Down
2 changes: 2 additions & 0 deletions src/jdk.jfr/share/classes/jdk/jfr/internal/MirrorEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import jdk.jfr.events.ClassFileDefineEvent;
import jdk.jfr.events.DeserializationEvent;
import jdk.jfr.events.ErrorThrownEvent;
import jdk.jfr.events.ExceptionStatisticsEvent;
Expand Down Expand Up @@ -57,6 +58,7 @@ final class MirrorEvents {

// Add mirror event mapping here. See MirrorEvent class for details.
static {
register("jdk.internal.event.ClassFileDefineEvent", ClassFileDefineEvent.class);
register("jdk.internal.event.DeserializationEvent", DeserializationEvent.class);
register("jdk.internal.event.FileForceEvent", FileForceEvent.class);
register("jdk.internal.event.FileReadEvent", FileReadEvent.class);
Expand Down
4 changes: 4 additions & 0 deletions src/jdk.jfr/share/conf/jfr/default.jfc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@
<setting name="stackTrace">true</setting>
</event>

<event name="jdk.ClassFileDefine">
<setting name="enabled" control="class-loading">false</setting>
</event>

<event name="jdk.RedefineClasses">
<setting name="enabled">true</setting>
<setting name="stackTrace">true</setting>
Expand Down