diff --git a/src/java.base/share/classes/sun/nio/fs/NativeBuffer.java b/src/java.base/share/classes/sun/nio/fs/NativeBuffer.java index 9500a737af7..dc0c8e4d8c9 100644 --- a/src/java.base/share/classes/sun/nio/fs/NativeBuffer.java +++ b/src/java.base/share/classes/sun/nio/fs/NativeBuffer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2026, 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 @@ -25,9 +25,7 @@ package sun.nio.fs; -import java.lang.ref.Cleaner.Cleanable; import jdk.internal.misc.Unsafe; -import jdk.internal.ref.CleanerFactory; /** * A light-weight buffer in native memory. @@ -38,27 +36,17 @@ class NativeBuffer implements AutoCloseable { private final long address; private final int size; - private final Cleanable cleanable; // optional "owner" to avoid copying // (only safe for use by thread-local caches) private Object owner; - private static class Deallocator implements Runnable { - private final long address; - Deallocator(long address) { - this.address = address; - } - public void run() { - unsafe.freeMemory(address); - } - } + // owner thread ID + private long ownerTid; NativeBuffer(int size) { this.address = unsafe.allocateMemory(size); this.size = size; - this.cleanable = CleanerFactory.cleaner() - .register(this, new Deallocator(address)); } @Override @@ -79,16 +67,26 @@ class NativeBuffer implements AutoCloseable { } void free() { - cleanable.clean(); + unsafe.freeMemory(address); } // not synchronized; only safe for use by thread-local caches void setOwner(Object owner) { + Thread thread = Thread.currentThread(); + assert !thread.isVirtual(); + assert ownerTid == 0 || ownerTid == thread.threadId(); this.owner = owner; + this.ownerTid = (owner != null) ? thread.threadId() : 0; } // not synchronized; only safe for use by thread-local caches Object owner() { - return owner; + long tid = Thread.currentThread().threadId(); + assert ownerTid == 0 || ownerTid == tid; + if (ownerTid == tid) { + return owner; + } else { + return null; + } } } diff --git a/src/java.base/share/classes/sun/nio/fs/NativeBuffers.java b/src/java.base/share/classes/sun/nio/fs/NativeBuffers.java index 9b93cce9114..750da8c1a3d 100644 --- a/src/java.base/share/classes/sun/nio/fs/NativeBuffers.java +++ b/src/java.base/share/classes/sun/nio/fs/NativeBuffers.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2026, 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 @@ -92,7 +92,8 @@ class NativeBuffers { static NativeBuffer getNativeBuffer(int size) { NativeBuffer buffer = getNativeBufferFromCache(size); if (buffer != null) { - buffer.setOwner(null); + if (!Thread.currentThread().isVirtual()) + buffer.setOwner(null); return buffer; } else { return allocNativeBuffer(size); diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java index 77dc1851478..2d72aeb2ee9 100644 --- a/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java +++ b/src/java.base/unix/classes/sun/nio/fs/UnixNativeDispatcher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2026, 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 @@ -47,7 +47,8 @@ class UnixNativeDispatcher { return buffer; } NativeBuffers.copyCStringToNativeBuffer(cstr, buffer); - buffer.setOwner(path); + if (!Thread.currentThread().isVirtual()) + buffer.setOwner(path); return buffer; } diff --git a/src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java b/src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java index 8e351b7e0ef..ba1b7b1aa9f 100644 --- a/src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java +++ b/src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2026, 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 @@ -1085,7 +1085,8 @@ class WindowsNativeDispatcher { unsafe.copyMemory(chars, Unsafe.ARRAY_CHAR_BASE_OFFSET, null, buffer.address(), (long)stringLengthInBytes); unsafe.putChar(buffer.address() + stringLengthInBytes, (char)0); - buffer.setOwner(s); + if (!Thread.currentThread().isVirtual()) + buffer.setOwner(s); return buffer; }