|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.core.posix.heapdump; |
| 26 | + |
| 27 | +import java.io.FileDescriptor; |
| 28 | +import java.io.FileOutputStream; |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +import org.graalvm.nativeimage.ImageSingletons; |
| 32 | +import org.graalvm.nativeimage.Platform; |
| 33 | +import org.graalvm.nativeimage.Platforms; |
| 34 | +import org.graalvm.nativeimage.StackValue; |
| 35 | +import org.graalvm.nativeimage.c.type.CCharPointer; |
| 36 | +import org.graalvm.nativeimage.hosted.Feature; |
| 37 | +import org.graalvm.word.WordFactory; |
| 38 | + |
| 39 | +import com.oracle.svm.core.annotate.AutomaticFeature; |
| 40 | +import com.oracle.svm.core.heapdump.HeapDumpWriterImpl.AllocationFreeFileOutputStream; |
| 41 | +import com.oracle.svm.core.posix.PosixUtils; |
| 42 | +import com.oracle.svm.core.posix.headers.Unistd; |
| 43 | +import com.oracle.svm.core.util.VMError; |
| 44 | + |
| 45 | +/** |
| 46 | + * Posix implementation of allocation-free output stream created from FileOutputStream. |
| 47 | + * |
| 48 | + */ |
| 49 | +final class AllocationFreeFileOutputStreamPosix extends AllocationFreeFileOutputStream { |
| 50 | + |
| 51 | + /** |
| 52 | + * Pre-allocated exceptions, for throwing from code that must not allocate. |
| 53 | + */ |
| 54 | + private static final IOException preallocatedIOException = new IOException("Write failed."); |
| 55 | + private static final ArrayIndexOutOfBoundsException preallocatedArrayIndexOutOfBoundsException = new ArrayIndexOutOfBoundsException(); |
| 56 | + |
| 57 | + private FileOutputStream fos; |
| 58 | + private FileDescriptor fileDescriptor; |
| 59 | + |
| 60 | + AllocationFreeFileOutputStreamPosix() { |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + private AllocationFreeFileOutputStreamPosix(FileOutputStream fileOutputStream) throws IOException { |
| 65 | + fos = fileOutputStream; |
| 66 | + fileDescriptor = fos.getFD(); |
| 67 | + |
| 68 | + if (!(Platform.includedIn(Platform.LINUX.class) || Platform.includedIn(Platform.DARWIN.class))) { |
| 69 | + /* See GR-9725 */ |
| 70 | + throw VMError.unsupportedFeature("Heap dump writing currently contains Posix specific code"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public AllocationFreeFileOutputStream newStreamFor(FileOutputStream fileOutputStream) throws IOException { |
| 76 | + return new AllocationFreeFileOutputStreamPosix(fileOutputStream); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void write(int b) throws IOException { |
| 81 | + final CCharPointer buffer = StackValue.get(CCharPointer.class); |
| 82 | + buffer.write((byte) b); |
| 83 | + final boolean writeResult = PosixUtils.writeBytes(fileDescriptor, buffer, WordFactory.unsigned(1)); |
| 84 | + if (!writeResult) { |
| 85 | + throw preallocatedIOException; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void write(byte[] b, int off, int len) throws IOException { |
| 91 | + /* Sanity check the arguments. */ |
| 92 | + if ((b == null) || ((off < 0) || (len < 0) || ((b.length - off) < len))) { |
| 93 | + throw preallocatedArrayIndexOutOfBoundsException; |
| 94 | + } |
| 95 | + |
| 96 | + /* |
| 97 | + * Stack allocation needs an allocation size that is a compile time constant, so we split |
| 98 | + * the byte array up in multiple chunks and write them separately. |
| 99 | + */ |
| 100 | + final int chunkSize = 256; |
| 101 | + final CCharPointer bytes = StackValue.get(chunkSize); |
| 102 | + |
| 103 | + int chunkOffset = off; |
| 104 | + int inputLength = len; |
| 105 | + while (inputLength > 0) { |
| 106 | + int chunkLength = Math.min(inputLength, chunkSize); |
| 107 | + |
| 108 | + for (int i = 0; i < chunkLength; i++) { |
| 109 | + bytes.write(i, b[chunkOffset + i]); |
| 110 | + } |
| 111 | + |
| 112 | + if (!PosixUtils.writeBytes(fileDescriptor, bytes, WordFactory.unsigned(chunkLength))) { |
| 113 | + throw preallocatedIOException; |
| 114 | + } |
| 115 | + |
| 116 | + chunkOffset += chunkLength; |
| 117 | + inputLength -= chunkLength; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void close() throws IOException { |
| 123 | + fos.close(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void flush() throws IOException { |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Read the current position in a file descriptor. |
| 132 | + */ |
| 133 | + @Override |
| 134 | + protected long position() { |
| 135 | + int fd = PosixUtils.getFD(fileDescriptor); |
| 136 | + return Unistd.lseek(fd, WordFactory.zero(), Unistd.SEEK_CUR()).rawValue(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Set the current position in a file descriptor. |
| 141 | + */ |
| 142 | + @Override |
| 143 | + protected long position(long offset) { |
| 144 | + int fd = PosixUtils.getFD(fileDescriptor); |
| 145 | + return Unistd.lseek(fd, WordFactory.signed(offset), Unistd.SEEK_SET()).rawValue(); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/* |
| 150 | + * The limitation to Linux and Darwin is necessary because the implementation currently uses |
| 151 | + * posix-dependent low-level code. See GR-9725. |
| 152 | + */ |
| 153 | +@Platforms({Platform.LINUX.class, Platform.DARWIN.class}) |
| 154 | +@AutomaticFeature |
| 155 | +class AllocationFreeFileOutputStreamFeature implements Feature { |
| 156 | + |
| 157 | + @Override |
| 158 | + public void afterRegistration(Feature.AfterRegistrationAccess access) { |
| 159 | + ImageSingletons.add(AllocationFreeFileOutputStream.class, new AllocationFreeFileOutputStreamPosix()); |
| 160 | + } |
| 161 | +} |
0 commit comments