Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JDK-8348099] Adapt JDK-8344332: (bf) Migrate DirectByteBuffer to use java.lang.ref.Cleaner #10562

Open
wants to merge 2 commits into
base: galahad
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion common.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"COMMENT.jdks": "When adding or removing JDKs keep in sync with JDKs in ci/common.jsonnet",
"jdks": {
"galahad-jdk": {"name": "jpg-jdk", "version": "25", "build_id": "jdk-25+6-477", "platformspecific": true, "extrabundles": ["static-libs"]},
"galahad-jdk": {"name": "jpg-jdk", "version": "25", "build_id": "jdk-25+6-XXX", "platformspecific": true, "extrabundles": ["static-libs"]},

"oraclejdk17": {"name": "jpg-jdk", "version": "17.0.7", "build_id": "jdk-17.0.7+8", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-17": {"name": "labsjdk", "version": "ce-17.0.7+4-jvmci-23.1-b02", "platformspecific": true },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
Expand All @@ -25,6 +25,7 @@
package com.oracle.objectfile;

import java.io.IOException;
import java.lang.ref.Cleaner.Cleanable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -1320,9 +1321,19 @@ public final void write(FileChannel outputChannel) {
writeBuffer(sortedObjectFileElements, buffer);
} finally {
// unmap immediately
((DirectBuffer) buffer).cleaner().clean();
if (Runtime.version().feature() == 21) {
// ((DirectBuffer) buffer).cleaner().clean();
Object cleaner = DirectBuffer.class.getMethod("cleaner").invoke(buffer);
cleaner.getClass().getMethod("clean").invoke(cleaner);
} else {
// ((DirectBuffer) buffer).cleanable().clean();
// We use reflection here because DirectBuffer.cleanable does not exist in JDK21.
// This can be replaced when JDK21 support is dropped.
Cleanable cleanable = (Cleanable) DirectBuffer.class.getMethod("cleanable").invoke(buffer);
cleanable.clean();
}
}
} catch (IOException e) {
} catch (IOException | ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2025, 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 com.oracle.svm.core.heap;

import com.oracle.svm.core.NeverInline;
import com.oracle.svm.core.annotate.AnnotateOriginal;
import com.oracle.svm.core.annotate.TargetClass;

@TargetClass(className = "java.lang.ref.Cleaner", innerClass="Cleanable")
public final class Target_java_lang_ref_Cleaner_Cleanable {
@AnnotateOriginal
@NeverInline("Ensure that every exception can be caught, including implicit exceptions.")
native void clean();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 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
Expand Down Expand Up @@ -29,6 +29,7 @@
import java.lang.reflect.Field;
import java.util.function.BooleanSupplier;

import com.oracle.svm.core.jdk.JDK21OrEarlier;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.FieldValueTransformer;
Expand Down Expand Up @@ -156,6 +157,7 @@ boolean refersTo0(Object obj) {

/** May be used by {@code JavaLangRefAccess} via {@code SharedSecrets}. */
@Substitute
@TargetElement(onlyWith = JDK21OrEarlier.class)
static boolean waitForReferenceProcessing() throws InterruptedException {
return ReferenceInternals.waitForReferenceProcessing();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2025, 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 com.oracle.svm.core.heap;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.jdk.JDKLatest;

@TargetClass(className = "java.nio.BufferCleaner", onlyWith = JDKLatest.class)
public final class Target_java_nio_BufferCleaner {

@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
static Target_java_lang_ref_Cleaner CLEANER;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
Expand Down Expand Up @@ -27,7 +27,6 @@
import java.lang.ref.Cleaner;
import java.lang.ref.ReferenceQueue;

import jdk.graal.compiler.serviceprovider.JavaVersionUtil;
import org.graalvm.nativeimage.hosted.FieldValueTransformer;

import com.oracle.svm.core.NeverInline;
Expand All @@ -42,6 +41,7 @@
import com.oracle.svm.core.thread.VMThreads;
import com.oracle.svm.util.ReflectionUtil;

import jdk.graal.compiler.serviceprovider.JavaVersionUtil;
import jdk.internal.misc.InnocuousThread;

@TargetClass(className = "jdk.internal.ref.Cleaner")
Expand Down Expand Up @@ -69,13 +69,6 @@ final class Target_java_lang_ref_Cleaner {
public Target_jdk_internal_ref_CleanerImpl impl;
}

@TargetClass(className = "java.lang.ref.Cleaner$Cleanable")
final class Target_java_lang_ref_Cleaner_Cleanable {
@AnnotateOriginal
@NeverInline("Ensure that every exception can be caught, including implicit exceptions.")
native void clean();
}

@TargetClass(className = "jdk.internal.ref.CleanerImpl")
final class Target_jdk_internal_ref_CleanerImpl {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 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
Expand Down Expand Up @@ -29,6 +29,8 @@
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.annotate.TargetElement;
import com.oracle.svm.core.heap.Target_java_lang_ref_Cleaner_Cleanable;
import com.oracle.svm.core.heap.Target_jdk_internal_ref_Cleaner;
import com.oracle.svm.core.util.BasedOnJDKFile;
import com.oracle.svm.core.util.VMError;
Expand All @@ -49,8 +51,13 @@ public final class Target_java_nio_DirectByteBuffer {
* registered for the buffer by resetting the field {@link #cleaner}.
*/
@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
@TargetElement(onlyWith = JDK21OrEarlier.class) //
Target_jdk_internal_ref_Cleaner cleaner;

@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset) //
@TargetElement(onlyWith = JDKLatest.class) //
Target_java_lang_ref_Cleaner_Cleanable cleanable;

@Alias
@SuppressWarnings("unused")
Target_java_nio_DirectByteBuffer(long addr, long cap) {
Expand Down
Loading