From 0e3d3fe17574c2124a7312add307eda1acf6598b Mon Sep 17 00:00:00 2001 From: dulmarod Date: Fri, 20 Nov 2026 11:25:32 +0000 Subject: [PATCH] Revert "[java] Model FileChannel.open and Files.newByteChannel for Pulse" This reverts commit 66193a3b8cf38dd86b9f96d1b960bcd6d98e2021. --- infer/src/pulse/PulseModelsJava.ml | 32 ------------- .../codetoanalyze/java/pulse/FilesLeaks.java | 47 ------------------- .../tests/codetoanalyze/java/pulse/issues.exp | 4 -- 3 files changed, 83 deletions(-) diff --git a/infer/src/pulse/PulseModelsJava.ml b/infer/src/pulse/PulseModelsJava.ml index 1f8b7841cc..b1a89b1874 100644 --- a/infer/src/pulse/PulseModelsJava.ml +++ b/infer/src/pulse/PulseModelsJava.ml @@ -414,17 +414,6 @@ module Resource = struct let zip_codec_release = StringSet.of_list ["close"; "end"] - (* [SeekableByteChannel] reads, writes and repositions. None of them closes the channel, and the - ones below may throw [IOException]. Left unknown, any of them makes Pulse drop a channel - returned by [Files.newByteChannel] or [FileChannel.open]. [size], [transferTo] and - [transferFrom] are not included: channels from [getChannel] delegate to a stream, and modeling - those calls changes which delegated stream is still tracked in the existing tests. *) - let byte_channel_usage_modeled_throws_IOException = - StringSet.of_list ["position"; "read"; "truncate"; "write"] - - - let byte_channel_usage_modeled = StringSet.of_list ["isOpen"] - let use ~exn_class_name : model_no_non_disj = let exn = JavaClassName.from_string exn_class_name in fun model_data astate -> @@ -1078,27 +1067,6 @@ let matchers : matcher list = &:: "newBufferedWriter" <>--> Resource.allocate_returned ~exn_class_name:"java.io.IOException" "java.io.BufferedWriter" |> with_non_disj - (* [Files.newByteChannel] and [FileChannel.open] return a fresh channel without a modeled - constructor, so the channel was never tracked (same gap as the stream factories, #2110). *) - ; +map_context_tenv (PatternMatch.Java.implements "java.nio.file.Files") - &:: "newByteChannel" - <>--> Resource.allocate_returned ~exn_class_name:"java.io.IOException" - "java.nio.channels.SeekableByteChannel" - |> with_non_disj - ; +map_context_tenv (PatternMatch.Java.implements "java.nio.channels.FileChannel") - &:: "open" - <>--> Resource.allocate_returned ~exn_class_name:"java.io.IOException" - "java.nio.channels.FileChannel" - |> with_non_disj - ; +map_context_tenv (PatternMatch.Java.implements "java.nio.channels.SeekableByteChannel") - &::+ (fun _ proc_name_str -> - StringSet.mem proc_name_str Resource.byte_channel_usage_modeled_throws_IOException ) - <>$ any_arg - $+...$--> Resource.use ~exn_class_name:"java.io.IOException" - |> with_non_disj - ; +map_context_tenv (PatternMatch.Java.implements "java.nio.channels.SeekableByteChannel") - &::+ (fun _ proc_name_str -> StringSet.mem proc_name_str Resource.byte_channel_usage_modeled) - <>$ any_arg $+...$--> Basic.skip |> with_non_disj ; +map_context_tenv (PatternMatch.Java.implements "java.sql.DriverManager") &:: "getConnection" <>--> Resource.allocate_returned ~exn_class_name:"java.sql.SQLException" "java.sql.Connection" diff --git a/infer/tests/codetoanalyze/java/pulse/FilesLeaks.java b/infer/tests/codetoanalyze/java/pulse/FilesLeaks.java index 365eda9cb4..2c21f1bc7c 100644 --- a/infer/tests/codetoanalyze/java/pulse/FilesLeaks.java +++ b/infer/tests/codetoanalyze/java/pulse/FilesLeaks.java @@ -12,12 +12,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.channels.SeekableByteChannel; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.StandardOpenOption; // Streams returned by the java.nio.file.Files factories (issue #2110) public class FilesLeaks { @@ -78,47 +74,4 @@ public void newBufferedWriterTryWithResourcesOk(Path path) throws IOException { writer.write(1); } } - - // Channels from Files.newByteChannel and FileChannel.open are fresh resources. Left unknown, - // Pulse never allocates them, so not closing the channel is missed. - - public void newByteChannelNotClosedAfterReadBad(Path path) throws IOException { - SeekableByteChannel channel = Files.newByteChannel(path); - ByteBuffer buffer = ByteBuffer.allocate(8); - channel.read(buffer); - } - - public void newByteChannelClosedOk(Path path) throws IOException { - SeekableByteChannel channel = Files.newByteChannel(path); - try { - ByteBuffer buffer = ByteBuffer.allocate(8); - channel.read(buffer); - } finally { - channel.close(); - } - } - - public void newByteChannelTryWithResourcesOk(Path path) throws IOException { - try (SeekableByteChannel channel = Files.newByteChannel(path)) { - ByteBuffer buffer = ByteBuffer.allocate(8); - channel.read(buffer); - } - } - - public SeekableByteChannel newByteChannelReturnedOk(Path path) throws IOException { - return Files.newByteChannel(path); - } - - public void fileChannelOpenNotClosedAfterWriteBad(Path path) throws IOException { - FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE); - ByteBuffer buffer = ByteBuffer.allocate(8); - channel.write(buffer); - } - - public void fileChannelOpenTryWithResourcesOk(Path path) throws IOException { - try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ)) { - ByteBuffer buffer = ByteBuffer.allocate(8); - channel.read(buffer); - } - } } diff --git a/infer/tests/codetoanalyze/java/pulse/issues.exp b/infer/tests/codetoanalyze/java/pulse/issues.exp index 0ab903de9f..4018339afc 100644 --- a/infer/tests/codetoanalyze/java/pulse/issues.exp +++ b/infer/tests/codetoanalyze/java/pulse/issues.exp @@ -34,10 +34,6 @@ codetoanalyze/java/pulse/FilesLeaks.java, codetoanalyze.java.infer.FilesLeaks.ne codetoanalyze/java/pulse/FilesLeaks.java, codetoanalyze.java.infer.FilesLeaks.newBufferedReaderNotClosedAfterReadBad(java.nio.file.Path):void, 3, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.BufferedReader()` here,resource becomes unreachable here] codetoanalyze/java/pulse/FilesLeaks.java, codetoanalyze.java.infer.FilesLeaks.newBufferedWriterNotClosedAfterWriteBad(java.nio.file.Path):void, 2, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.BufferedWriter()` here,resource becomes unreachable here] codetoanalyze/java/pulse/FilesLeaks.java, codetoanalyze.java.infer.FilesLeaks.newBufferedWriterNotClosedAfterWriteBad(java.nio.file.Path):void, 3, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.BufferedWriter()` here,resource becomes unreachable here] -codetoanalyze/java/pulse/FilesLeaks.java, codetoanalyze.java.infer.FilesLeaks.newByteChannelNotClosedAfterReadBad(java.nio.file.Path):void, 3, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.nio.channels.SeekableByteChannel()` here,resource becomes unreachable here] -codetoanalyze/java/pulse/FilesLeaks.java, codetoanalyze.java.infer.FilesLeaks.newByteChannelNotClosedAfterReadBad(java.nio.file.Path):void, 4, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.nio.channels.SeekableByteChannel()` here,resource becomes unreachable here] -codetoanalyze/java/pulse/FilesLeaks.java, codetoanalyze.java.infer.FilesLeaks.fileChannelOpenNotClosedAfterWriteBad(java.nio.file.Path):void, 3, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.nio.channels.FileChannel()` here,resource becomes unreachable here] -codetoanalyze/java/pulse/FilesLeaks.java, codetoanalyze.java.infer.FilesLeaks.fileChannelOpenNotClosedAfterWriteBad(java.nio.file.Path):void, 4, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.nio.channels.FileChannel()` here,resource becomes unreachable here] codetoanalyze/java/pulse/FilterInputStreamLeaks.java, codetoanalyze.java.infer.FilterInputStreamLeaks.bufferedInputStreamNotClosedAfterReadBad():void, 9, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.BufferedInputStream()` here,resource becomes unreachable here] codetoanalyze/java/pulse/FilterInputStreamLeaks.java, codetoanalyze.java.infer.FilterInputStreamLeaks.checkedInputStreamNotClosedAfterReadBad():void, 9, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.util.zip.CheckedInputStream()` here,resource becomes unreachable here] codetoanalyze/java/pulse/FilterInputStreamLeaks.java, codetoanalyze.java.infer.FilterInputStreamLeaks.cipherInputStreamNotClosedAfterSkipBad():void, 9, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `javax.crypto.CipherInputStream()` here,resource becomes unreachable here]