Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8261160: Add a deserialization JFR event
Reviewed-by: phh
Backport-of: 3dc6f52
  • Loading branch information
GoeLin committed Jun 21, 2022
1 parent 661236d commit 765c5b4
Show file tree
Hide file tree
Showing 13 changed files with 846 additions and 19 deletions.
41 changes: 29 additions & 12 deletions src/java.base/share/classes/java/io/ObjectInputStream.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2021, 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
Expand Down Expand Up @@ -40,6 +40,7 @@
import java.util.Objects;

import jdk.internal.misc.SharedSecrets;
import jdk.internal.event.DeserializationEvent;
import jdk.internal.misc.Unsafe;
import sun.reflect.misc.ReflectUtil;
import sun.security.action.GetBooleanAction;
Expand Down Expand Up @@ -1314,21 +1315,25 @@ public final void setObjectInputFilter(ObjectInputFilter filter) {
}

/**
* Invoke the serialization filter if non-null.
* Invokes the serialization filter if non-null.
*
* If the filter rejects or an exception is thrown, throws InvalidClassException.
*
* Logs and/or commits a {@code DeserializationEvent}, if configured.
*
* @param clazz the class; may be null
* @param arrayLength the array length requested; use {@code -1} if not creating an array
* @throws InvalidClassException if it rejected by the filter or
* a {@link RuntimeException} is thrown
*/
private void filterCheck(Class<?> clazz, int arrayLength)
throws InvalidClassException {
// Info about the stream is not available if overridden by subclass, return 0
long bytesRead = (bin == null) ? 0 : bin.getBytesRead();
RuntimeException ex = null;
ObjectInputFilter.Status status = null;

if (serialFilter != null) {
RuntimeException ex = null;
ObjectInputFilter.Status status;
// Info about the stream is not available if overridden by subclass, return 0
long bytesRead = (bin == null) ? 0 : bin.getBytesRead();
try {
status = serialFilter.checkInput(new FilterValues(clazz, arrayLength,
totalObjectRefs, depth, bytesRead));
Expand All @@ -1346,12 +1351,24 @@ private void filterCheck(Class<?> clazz, int arrayLength)
status, clazz, arrayLength, totalObjectRefs, depth, bytesRead,
Objects.toString(ex, "n/a"));
}
if (status == null ||
status == ObjectInputFilter.Status.REJECTED) {
InvalidClassException ice = new InvalidClassException("filter status: " + status);
ice.initCause(ex);
throw ice;
}
}
DeserializationEvent event = new DeserializationEvent();
if (event.shouldCommit()) {
event.filterConfigured = serialFilter != null;
event.filterStatus = status != null ? status.name() : null;
event.type = clazz;
event.arrayLength = arrayLength;
event.objectReferences = totalObjectRefs;
event.depth = depth;
event.bytesRead = bytesRead;
event.exceptionType = ex != null ? ex.getClass() : null;
event.exceptionMessage = ex != null ? ex.getMessage() : null;
event.commit();
}
if (serialFilter != null && (status == null || status == ObjectInputFilter.Status.REJECTED)) {
InvalidClassException ice = new InvalidClassException("filter status: " + status);
ice.initCause(ex);
throw ice;
}
}

Expand Down
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2021, 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;

/**
* Event details relating to deserialization.
*/

public final class DeserializationEvent extends Event {
public boolean filterConfigured;
public String filterStatus;
public Class<?> type;
public int arrayLength;
public long objectReferences;
public long depth;
public long bytesRead;
public Class<?> exceptionType;
public String exceptionMessage;
}
67 changes: 67 additions & 0 deletions src/jdk.jfr/share/classes/jdk/jfr/events/DeserializationEvent.java
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2021, 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;

@Category({"Java Development Kit", "Serialization"})
@Label("Deserialization")
@Name("jdk.Deserialization")
@Description("Results of deserialiation and ObjectInputFilter checks")
@MirrorEvent(className = "jdk.internal.event.DeserializationEvent")
public final class DeserializationEvent extends AbstractJDKEvent {

@Label("Filter Configured")
public boolean filterConfigured;

@Label("Filter Status")
public String filterStatus;

@Label ("Type")
public Class<?> type;

@Label ("Array Length")
public int arrayLength;

@Label ("Object References")
public long objectReferences;

@Label ("Depth")
public long depth;

@Label ("Bytes Read")
public long bytesRead;

@Label ("Exception Type")
public Class<?> exceptionType;

@Label ("Exception Message")
public String exceptionMessage;
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, 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
Expand Down Expand Up @@ -37,6 +37,7 @@
import jdk.jfr.events.FileForceEvent;
import jdk.jfr.events.FileReadEvent;
import jdk.jfr.events.FileWriteEvent;
import jdk.jfr.events.DeserializationEvent;
import jdk.jfr.events.SecurityPropertyModificationEvent;
import jdk.jfr.events.SocketReadEvent;
import jdk.jfr.events.SocketWriteEvent;
Expand All @@ -54,6 +55,7 @@
public final class JDKEvents {

private static final Class<?>[] mirrorEventClasses = {
DeserializationEvent.class,
SecurityPropertyModificationEvent.class,
TLSHandshakeEvent.class,
X509CertificateEvent.class,
Expand All @@ -71,6 +73,7 @@ public final class JDKEvents {
ErrorThrownEvent.class,
ActiveSettingEvent.class,
ActiveRecordingEvent.class,
jdk.internal.event.DeserializationEvent.class,
jdk.internal.event.SecurityPropertyModificationEvent.class,
jdk.internal.event.TLSHandshakeEvent.class,
jdk.internal.event.X509CertificateEvent.class,
Expand Down
5 changes: 5 additions & 0 deletions src/jdk.jfr/share/conf/jfr/default.jfc
Expand Up @@ -603,6 +603,11 @@
<setting name="threshold" control="socket-io-threshold">20 ms</setting>
</event>

<event name="jdk.Deserialization">
<setting name="enabled">false</setting>
<setting name="stackTrace">true</setting>
</event>

<event name="jdk.SecurityPropertyModification">
<setting name="enabled">false</setting>
<setting name="stackTrace">true</setting>
Expand Down
5 changes: 5 additions & 0 deletions src/jdk.jfr/share/conf/jfr/profile.jfc
Expand Up @@ -603,6 +603,11 @@
<setting name="threshold" control="socket-io-threshold">10 ms</setting>
</event>

<event name="jdk.Deserialization">
<setting name="enabled">false</setting>
<setting name="stackTrace">true</setting>
</event>

<event name="jdk.SecurityPropertyModification">
<setting name="enabled">false</setting>
<setting name="stackTrace">true</setting>
Expand Down
14 changes: 13 additions & 1 deletion test/jdk/java/io/Serializable/serialFilter/GlobalFilterTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, 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
Expand Down Expand Up @@ -53,6 +53,18 @@
*
* @summary Test Global Filters
*/

/* @test
* @bug 8261160
* @summary Add a deserialization JFR event
* @build GlobalFilterTest SerialFilterTest
* @requires vm.hasJFR
* @run testng/othervm/policy=security.policy
* -XX:StartFlightRecording=name=DeserializationEvent,dumponexit=true
* -Djava.security.properties=${test.src}/java.security-extra1
* -Djava.security.debug=properties GlobalFilterTest
*/

@Test
public class GlobalFilterTest {
private static final String serialPropName = "jdk.serialFilter";
Expand Down

1 comment on commit 765c5b4

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.