# HG changeset patch # User bpb # Date 1480713736 28800 # Fri Dec 02 13:22:16 2016 -0800 # Node ID a2f7d8a1d69ea7ca4bed66626d062d4f4ec44dd6 # Parent 150206269f3fd88cc27d6f9c456234bc1b520e0e 8168628: (fc) SIGBUS when extending file size to map it Summary: Synchronize file extension and subsequent map0(); on Linux use fallocate64() instead of ftruncate64(). Reviewed-by: rehn, simonis, alanb diff --git a/make/mapfiles/libnio/mapfile-linux b/make/mapfiles/libnio/mapfile-linux --- a/make/mapfiles/libnio/mapfile-linux +++ b/make/mapfiles/libnio/mapfile-linux @@ -73,6 +73,7 @@ Java_sun_nio_ch_FileDispatcherImpl_release0; Java_sun_nio_ch_FileDispatcherImpl_size0; Java_sun_nio_ch_FileDispatcherImpl_truncate0; + Java_sun_nio_ch_FileDispatcherImpl_allocate0; Java_sun_nio_ch_FileDispatcherImpl_write0; Java_sun_nio_ch_FileDispatcherImpl_writev0; Java_sun_nio_ch_FileKey_init; diff --git a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java --- a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2016, 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 @@ -898,57 +898,62 @@ if (!isOpen()) return null; - long filesize; - do { - filesize = nd.size(fd); - } while ((filesize == IOStatus.INTERRUPTED) && isOpen()); - if (!isOpen()) - return null; - - if (filesize < position + size) { // Extend file size - if (!writable) { - throw new IOException("Channel not open for writing " + - "- cannot extend file to required size"); - } - int rv; + long mapSize; + int pagePosition; + synchronized (positionLock) { + long filesize; do { - rv = nd.truncate(fd, position + size); - } while ((rv == IOStatus.INTERRUPTED) && isOpen()); + filesize = nd.size(fd); + } while ((filesize == IOStatus.INTERRUPTED) && isOpen()); if (!isOpen()) return null; - } - if (size == 0) { - addr = 0; - // a valid file descriptor is not required - FileDescriptor dummy = new FileDescriptor(); - if ((!writable) || (imode == MAP_RO)) - return Util.newMappedByteBufferR(0, 0, dummy, null); - else - return Util.newMappedByteBuffer(0, 0, dummy, null); - } - int pagePosition = (int)(position % allocationGranularity); - long mapPosition = position - pagePosition; - long mapSize = size + pagePosition; - try { - // If no exception was thrown from map0, the address is valid - addr = map0(imode, mapPosition, mapSize); - } catch (OutOfMemoryError x) { - // An OutOfMemoryError may indicate that we've exhausted memory - // so force gc and re-attempt map - System.gc(); + if (filesize < position + size) { // Extend file size + if (!writable) { + throw new IOException("Channel not open for writing " + + "- cannot extend file to required size"); + } + int rv; + do { + rv = nd.allocate(fd, position + size); + } while ((rv == IOStatus.INTERRUPTED) && isOpen()); + if (!isOpen()) + return null; + } + + if (size == 0) { + addr = 0; + // a valid file descriptor is not required + FileDescriptor dummy = new FileDescriptor(); + if ((!writable) || (imode == MAP_RO)) + return Util.newMappedByteBufferR(0, 0, dummy, null); + else + return Util.newMappedByteBuffer(0, 0, dummy, null); + } + + pagePosition = (int)(position % allocationGranularity); + long mapPosition = position - pagePosition; + mapSize = size + pagePosition; try { - Thread.sleep(100); - } catch (InterruptedException y) { - Thread.currentThread().interrupt(); + // If map0 did not throw an exception, the address is valid + addr = map0(imode, mapPosition, mapSize); + } catch (OutOfMemoryError x) { + // An OutOfMemoryError may indicate that we've exhausted + // memory so force gc and re-attempt map + System.gc(); + try { + Thread.sleep(100); + } catch (InterruptedException y) { + Thread.currentThread().interrupt(); + } + try { + addr = map0(imode, mapPosition, mapSize); + } catch (OutOfMemoryError y) { + // After a second OOME, fail + throw new IOException("Map failed", y); + } } - try { - addr = map0(imode, mapPosition, mapSize); - } catch (OutOfMemoryError y) { - // After a second OOME, fail - throw new IOException("Map failed", y); - } - } + } // synchronized // On Windows, and potentially other platforms, we need an open // file descriptor for some mapping operations. diff --git a/src/java.base/share/classes/sun/nio/ch/FileDispatcher.java b/src/java.base/share/classes/sun/nio/ch/FileDispatcher.java --- a/src/java.base/share/classes/sun/nio/ch/FileDispatcher.java +++ b/src/java.base/share/classes/sun/nio/ch/FileDispatcher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2016, 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 @@ -40,6 +40,8 @@ abstract int truncate(FileDescriptor fd, long size) throws IOException; + abstract int allocate(FileDescriptor fd, long size) throws IOException; + abstract long size(FileDescriptor fd) throws IOException; abstract int lock(FileDescriptor fd, boolean blocking, long pos, long size, diff --git a/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java b/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java --- a/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java +++ b/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2016, 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 @@ -76,6 +76,10 @@ return truncate0(fd, size); } + int allocate(FileDescriptor fd, long size) throws IOException { + return allocate0(fd, size); + } + long size(FileDescriptor fd) throws IOException { return size0(fd); } @@ -138,6 +142,9 @@ static native int truncate0(FileDescriptor fd, long size) throws IOException; + static native int allocate0(FileDescriptor fd, long size) + throws IOException; + static native long size0(FileDescriptor fd) throws IOException; static native int lock0(FileDescriptor fd, boolean blocking, long pos, diff --git a/src/java.base/unix/native/libjava/io_util_md.c b/src/java.base/unix/native/libjava/io_util_md.c --- a/src/java.base/unix/native/libjava/io_util_md.c +++ b/src/java.base/unix/native/libjava/io_util_md.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2016, 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 @@ -215,6 +215,20 @@ handleSetLength(FD fd, jlong length) { int result; +#if defined(__linux__) + /* + * On Linux, if the file size is being increased, then ftruncate64() + * will modify the metadata value of the size without actually allocating + * any blocks which can cause a SIGBUS error if the file is subsequently + * memory-mapped. + */ + struct stat64 sb; + + if (fstat64(fd, &sb) == 0 && length > sb.st_blocks*512) { + RESTARTABLE(fallocate64(fd, 0, 0, length), result); + return result; + } +#endif RESTARTABLE(ftruncate64(fd, length), result); return result; } diff --git a/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c b/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c --- a/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c +++ b/src/java.base/unix/native/libnio/ch/FileDispatcherImpl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2016, 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 @@ -186,6 +186,31 @@ "Truncation failed"); } +JNIEXPORT jint JNICALL +Java_sun_nio_ch_FileDispatcherImpl_allocate0(JNIEnv *env, jobject this, + jobject fdo, jlong size) +{ + jint fd = fdval(env, fdo); +#if defined(__linux__) + /* + * On Linux, if the file size is being increased, then ftruncate64() + * will modify the metadata value of the size without actually allocating + * any blocks which can cause a SIGBUS error if the file is subsequently + * memory-mapped. + */ + struct stat64 fbuf; + + if (fstat64(fd, &fbuf) == 0 && size > fbuf.st_blocks*512) { + return handle(env, + fallocate64(fd, 0, 0, size), + "Allocation failed"); + } +#endif + return handle(env, + ftruncate64(fd, size), + "Truncation failed"); +} + JNIEXPORT jlong JNICALL Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo) { diff --git a/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java b/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java --- a/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java +++ b/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2016, 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 @@ -84,6 +84,11 @@ return truncate0(fd, size); } + int allocate(FileDescriptor fd, long size) throws IOException { + // truncate0() works for extending and truncating file size + return truncate0(fd, size); + } + long size(FileDescriptor fd) throws IOException { return size0(fd); }