|
24 | 24 | /* @test |
25 | 25 | * @summary java.io.RandomAccessFileTest uses java.nio.file inside. |
26 | 26 | * @library testNio |
| 27 | + * @compile --enable-preview --source 27 RandomAccessFileTest.java |
27 | 28 | * @run junit/othervm |
28 | 29 | * -Djava.nio.file.spi.DefaultFileSystemProvider=testNio.ManglingFileSystemProvider |
29 | 30 | * -Djbr.java.io.use.nio=true |
30 | 31 | * --add-opens jdk.unsupported/com.sun.nio.file=ALL-UNNAMED |
31 | 32 | * --add-opens java.base/java.io=ALL-UNNAMED |
| 33 | + * --enable-native-access=ALL-UNNAMED |
| 34 | + * --enable-preview |
32 | 35 | * RandomAccessFileTest |
33 | 36 | */ |
34 | 37 |
|
|
45 | 48 | import java.io.EOFException; |
46 | 49 | import java.io.File; |
47 | 50 | import java.io.RandomAccessFile; |
| 51 | +import java.lang.foreign.*; |
| 52 | +import java.lang.invoke.MethodHandle; |
| 53 | +import java.lang.invoke.VarHandle; |
48 | 54 | import java.lang.reflect.Field; |
49 | 55 | import java.nio.channels.FileChannel; |
50 | 56 | import java.nio.channels.FileLock; |
|
55 | 61 | import java.util.Collections; |
56 | 62 | import java.util.EnumSet; |
57 | 63 | import java.util.Objects; |
| 64 | +import java.util.concurrent.atomic.AtomicBoolean; |
58 | 65 |
|
59 | 66 | import static org.junit.Assert.assertEquals; |
60 | 67 | import static org.junit.Assert.assertFalse; |
@@ -293,4 +300,122 @@ public void testNoShareDelete() throws Exception { |
293 | 300 | FileSystems.getDefault().provider().newFileChannel(file.toPath(), Collections.singleton(option)).close(); |
294 | 301 | } |
295 | 302 | } |
| 303 | + |
| 304 | + /** |
| 305 | + * JBR-9779 |
| 306 | + */ |
| 307 | + @Test |
| 308 | + public void testWindowsPipe() throws Throwable { |
| 309 | + Assume.assumeTrue("Windows-only test", System.getProperty("os.name").toLowerCase().startsWith("win")); |
| 310 | + |
| 311 | + // Creating a pipe. |
| 312 | + Linker linker = Linker.nativeLinker(); |
| 313 | + SymbolLookup loader = SymbolLookup.libraryLookup("kernel32", Arena.global()); |
| 314 | + |
| 315 | + StructLayout captureLayout = Linker.Option.captureStateLayout(); |
| 316 | + VarHandle GetLastError = captureLayout.varHandle(MemoryLayout.PathElement.groupElement("GetLastError")); |
| 317 | + |
| 318 | + MethodHandle CreateNamedPipeW = linker.downcallHandle( |
| 319 | + loader.find("CreateNamedPipeW").get(), |
| 320 | + FunctionDescriptor.of(ValueLayout.JAVA_LONG, |
| 321 | + ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, |
| 322 | + ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, |
| 323 | + ValueLayout.ADDRESS), |
| 324 | + Linker.Option.captureCallState("GetLastError") |
| 325 | + ); |
| 326 | + |
| 327 | + MethodHandle ConnectNamedPipe = linker.downcallHandle( |
| 328 | + loader.find("ConnectNamedPipe").get(), |
| 329 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.JAVA_LONG, ValueLayout.ADDRESS), |
| 330 | + Linker.Option.captureCallState("GetLastError") |
| 331 | + ); |
| 332 | + |
| 333 | + MethodHandle DisconnectNamedPipe = linker.downcallHandle( |
| 334 | + loader.find("DisconnectNamedPipe").get(), |
| 335 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.JAVA_LONG) |
| 336 | + ); |
| 337 | + |
| 338 | + MethodHandle PeekNamedPipe = linker.downcallHandle( |
| 339 | + loader.find("PeekNamedPipe").get(), |
| 340 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.JAVA_LONG, |
| 341 | + ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.ADDRESS, |
| 342 | + ValueLayout.ADDRESS, ValueLayout.ADDRESS) |
| 343 | + ); |
| 344 | + |
| 345 | + MethodHandle CloseHandle = linker.downcallHandle( |
| 346 | + loader.find("CloseHandle").get(), |
| 347 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.JAVA_LONG) |
| 348 | + ); |
| 349 | + |
| 350 | + String pipeName = "\\\\.\\pipe\\jbr-test-pipe-" + System.nanoTime(); |
| 351 | + Arena arena = Arena.ofAuto(); |
| 352 | + char[] nameChars = (pipeName + "\0").toCharArray(); // `char` on Windows is UTF-16, as WinAPI expects. |
| 353 | + MemorySegment pipeWinPath = arena.allocateFrom(ValueLayout.JAVA_CHAR, nameChars); |
| 354 | + MemorySegment capturedState = arena.allocate(captureLayout); |
| 355 | + |
| 356 | + final long INVALID_HANDLE_VALUE = -1L; |
| 357 | + |
| 358 | + long hPipe = (long) CreateNamedPipeW.invokeExact( |
| 359 | + capturedState, |
| 360 | + pipeWinPath, |
| 361 | + 0x00000003, // dwOpenMode = PIPE_ACCESS_DUPLEX |
| 362 | + 0x00000000, // dwPipeMode = PIPE_TYPE_BYTE |
| 363 | + 1, // nMaxInstances. Limit to 1 to force ERROR_PIPE_BUSY. |
| 364 | + 1024, // nOutBufferSize |
| 365 | + 1024, // nInBufferSize |
| 366 | + 0, // nDefaultTimeOut |
| 367 | + MemorySegment.NULL // lpSecurityAttributes |
| 368 | + ); |
| 369 | + |
| 370 | + if (hPipe == INVALID_HANDLE_VALUE) { |
| 371 | + int errorCode = (int) GetLastError.get(capturedState); |
| 372 | + throw new Exception("CreateNamedPipeW failed: " + errorCode); |
| 373 | + } |
| 374 | + |
| 375 | + AtomicBoolean keepRunning = new AtomicBoolean(true); |
| 376 | + Thread serverThread = new Thread(() -> { |
| 377 | + // This server accepts a connection and does nothing until the client disconnects explicitly. |
| 378 | + try { |
| 379 | + int i = 0; |
| 380 | + while (keepRunning.get()) { |
| 381 | + int connected = (int) ConnectNamedPipe.invokeExact(capturedState, hPipe, MemorySegment.NULL); |
| 382 | + if (connected == 0) { |
| 383 | + int errorCode = (int) GetLastError.get(capturedState); |
| 384 | + // https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499- |
| 385 | + if (errorCode == 6 && !keepRunning.get()) { // ERROR_INVALID_HANDLE |
| 386 | + break; |
| 387 | + } |
| 388 | + throw new Exception("ConnectNamedPipe failed: " + errorCode); |
| 389 | + } |
| 390 | + try { |
| 391 | + int peekResult; |
| 392 | + do { |
| 393 | + // Random timeout. The timeout must be big enough to reveal possible consequent |
| 394 | + // attempts to connect to the pipe. |
| 395 | + Thread.sleep(1000); |
| 396 | + |
| 397 | + // Check if the pipe is still connected by peeking at it. |
| 398 | + peekResult = (int) PeekNamedPipe.invokeExact(hPipe, MemorySegment.NULL, 0, |
| 399 | + MemorySegment.NULL, MemorySegment.NULL, MemorySegment.NULL); |
| 400 | + } |
| 401 | + while (keepRunning.get() && peekResult != 0); |
| 402 | + } finally { |
| 403 | + int disconnected = (int) DisconnectNamedPipe.invokeExact(hPipe); |
| 404 | + } |
| 405 | + } |
| 406 | + } catch (Throwable e) { |
| 407 | + e.printStackTrace(); |
| 408 | + } |
| 409 | + }); |
| 410 | + |
| 411 | + serverThread.setDaemon(true); |
| 412 | + serverThread.start(); |
| 413 | + |
| 414 | + try { |
| 415 | + new RandomAccessFile(pipeName, "rw").close(); |
| 416 | + } finally { |
| 417 | + keepRunning.set(false); |
| 418 | + int closed = (int) CloseHandle.invokeExact(hPipe); |
| 419 | + } |
| 420 | + } |
296 | 421 | } |
0 commit comments