Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions infer/src/pulse/PulseModelsJava.ml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,36 @@ module Resource = struct
PulseOperations.write_id ret_id channel astate


(* [Socket.getInputStream] and [Socket.getOutputStream] share the socket's lifetime: closing the
stream closes the socket, and closing the socket closes the stream. Allocate the returned
stream as a resource and point [delegated_release] both ways, so either [close] releases both.
The stream stays reachable while the socket does, so the leak is reported when the socket
becomes unreachable. *)
let socket_stream ~stream_class_name socket : model_no_non_disj =
fun ({callee_procname; path; location; ret= ret_id, _} as model_data) astate ->
let allocator = Attribute.JavaResource (JavaClassName.from_string stream_class_name) in
let desc = Procname.to_simplified_string ~withclass:true callee_procname in
let alloc_state =
let<**> astate =
Basic.alloc_not_null ~desc allocator None ~initialize:true model_data astate
in
match PulseOperations.read_id ret_id astate with
| None ->
Basic.ok_continue astate
| Some stream ->
let<*> astate =
write_field path PulseOperations.ModeledField.delegated_release socket location stream
astate
in
let<+> astate =
write_field path PulseOperations.ModeledField.delegated_release stream location socket
astate
in
astate
in
alloc_state


(* Common java.sql.Connection calls that do not close the connection. Left unknown, any of them
makes Pulse stop tracking the connection (#2111). *)
let jdbc_connection_usage_modeled =
Expand Down Expand Up @@ -880,9 +910,13 @@ let matchers : matcher list =
&::+ (fun _ proc_name_str -> StringSet.mem proc_name_str Resource.zip_codec_release)
<>$ capt_arg_payload $--> Resource.release_no_exception |> with_non_disj
; +map_context_tenv (PatternMatch.Java.implements "java.net.Socket")
&:: "getOutputStream" <>$ any_arg $--> Basic.skip |> with_non_disj
&:: "getOutputStream" <>$ capt_arg_payload
$--> Resource.socket_stream ~stream_class_name:"java.io.OutputStream"
|> with_non_disj
; +map_context_tenv (PatternMatch.Java.implements "java.net.Socket")
&:: "getInputStream" <>$ any_arg $--> Basic.skip |> with_non_disj
&:: "getInputStream" <>$ capt_arg_payload
$--> Resource.socket_stream ~stream_class_name:"java.io.InputStream"
|> with_non_disj
; +map_context_tenv (PatternMatch.Java.implements "java.net.ServerSocket")
&:: "accept" <>$ any_arg $--> Basic.skip |> with_non_disj
; +map_context_tenv
Expand Down
71 changes: 63 additions & 8 deletions infer/tests/codetoanalyze/java/pulse/ResourceLeaks.java
Original file line number Diff line number Diff line change
Expand Up @@ -628,37 +628,92 @@ public void socketClosedAfterGetOutputStreamWriteOk() throws IOException {
}

// Socket InputStream tests
//
// Former FN_socketInputStreamNotClosedBad: getInputStream() was skipped, so the returned stream
// was not a tracked resource. The socket is allocated here so it dies with the stream; a
// caller-owned socket stays reachable, and closing that socket releases the stream.

public int FN_socketInputStreamNotClosedBad(Socket socket) throws IOException {
public int socketInputStreamNotClosedBad() throws IOException {
Socket socket = new Socket();
InputStream stream = socket.getInputStream();
return stream.read();
}

public void socketInputStreamClosedOk() throws IOException {
Socket socket = new Socket();
InputStream stream = socket.getInputStream();
try {
InputStream stream = socket.getInputStream();
stream.close();
} catch (Exception e) {
} finally {
socket.close();
}
}

public void socketInputStreamClosedByStreamOk() throws IOException {
Socket socket = new Socket();
InputStream stream = socket.getInputStream();
stream.close();
}

public void socketInputStreamClosedBySocketOk() throws IOException {
Socket socket = new Socket();
try {
InputStream stream = socket.getInputStream();
ignore(stream);
} finally {
socket.close();
}
}

public int socketInputStreamTryWithResourcesOk() throws IOException {
try (Socket socket = new Socket()) {
InputStream stream = socket.getInputStream();
return stream.read();
}
socket.close();
}

// Socket OutputStream tests
//
// Former FN_socketOutputStreamNotClosedBad: getOutputStream() was skipped, so the returned
// stream was not a tracked resource.

public void FN_socketOutputStreamNotClosedBad(Socket socket) throws IOException {
public void socketOutputStreamNotClosedBad() throws IOException {
Socket socket = new Socket();
OutputStream stream = socket.getOutputStream();
stream.write(10);
}

public void socketOutputStreamClosedOk() throws IOException {
Socket socket = new Socket();
OutputStream stream = socket.getOutputStream();
try {
OutputStream stream = socket.getOutputStream();
stream.close();
} catch (Exception e) {
} finally {
socket.close();
}
}

public void socketOutputStreamClosedByStreamOk() throws IOException {
Socket socket = new Socket();
OutputStream stream = socket.getOutputStream();
stream.close();
}

public void socketOutputStreamClosedBySocketOk() throws IOException {
Socket socket = new Socket();
try {
OutputStream stream = socket.getOutputStream();
ignore(stream);
} finally {
socket.close();
}
}

public void socketOutputStreamTryWithResourcesOk() throws IOException {
try (Socket socket = new Socket()) {
OutputStream stream = socket.getOutputStream();
stream.write(10);
}
socket.close();
}

// ServerSocket tests
Expand Down
6 changes: 5 additions & 1 deletion infer/tests/codetoanalyze/java/pulse/issues.exp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLe
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.jarOutputStreamLeakBad():void, 8, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.FileOutputStream()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.nestedJarOutputStreamBad():void, 3, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.FileOutputStream()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.socketNotClosedBad():void, 1, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.net.Socket()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.socketNotClosedAfterGetOutputStreamWriteBad():void, 2, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.net.Socket()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.socketNotClosedAfterGetOutputStreamWriteBad():void, 2, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.OutputStream()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.socketNotClosedAfterGetOutputStreamWriteBad():void, 3, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.OutputStream()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.socketInputStreamNotClosedBad():int, 3, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.InputStream()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.socketOutputStreamNotClosedBad():void, 3, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.OutputStream()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.socketOutputStreamNotClosedBad():void, 4, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.OutputStream()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.serverSocketNotClosedBad():void, 1, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.net.ServerSocket()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.serverSocketNotClosedAfterAcceptBad():void, 2, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.net.ServerSocket()` here,resource becomes unreachable here]
codetoanalyze/java/pulse/ResourceLeaks.java, codetoanalyze.java.infer.ResourceLeaks.FN_serverSocketNotClosedBad():void, 6, PULSE_RESOURCE_LEAK, no_bucket, ERROR, [allocation part of the trace starts here,allocated by constructor `java.io.PrintWriter()` here,resource becomes unreachable here]
Expand Down
Loading