Skip to content

[java] resolve library classes in a capture that does not run javac - #2351

Open
VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:cut-java-libclasses
Open

VladimirMakaev wants to merge 1 commit into
meta:mainfrom
VladimirMakaev:cut-java-libclasses

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

The Java frontend learns which library classes to translate, and where to find them, from the
verbose output of javac. A capture that does not run javac, such as a Kotlin one, had neither, so
it recorded every library class in the type environment as a placeholder with no supertypes, and
a placeholder already there was never replaced once a later capture could resolve the class. A
model that matches on a supertype then stops matching, with no diagnostic: close() is no longer
a release, and a stream closed through the reader that wraps it is reported as leaked.

// Kotlin, with only a Kotlin capture: reported a resource leak on the FileInputStream that
// bufferedReader() wraps, even though use{} closes the reader, which closes the stream
fun firstLine(path: String): String? = File(path).bufferedReader().use { it.readLine() }

The Kotlin capture now passes on the classpath it was given, and a placeholder is replaced as
soon as some capture can resolve the class.

Limitations: a class that is on neither the compiler's classpath nor the compiler's output is
still a placeholder, so models matching on its supertypes still do not fire.

Test plan

With Kotlin 2.4.21 and JDK 21, the Kotlin resources suite (including the new resource-leak tests) and the Java pulse suite pass. The Kotlin pulse suite differs from its expectations only in two lambda names in TAINT_ERROR traces ($lambda$1 vs $lambda$0); the parent commit shows the same difference with this compiler.

The Java frontend learns which library classes to translate, and where to find them, from the
verbose output of javac. A capture that does not run javac, such as a Kotlin one, had neither, so
it recorded every library class in the type environment as a placeholder with no supertypes, and
a placeholder already there was never replaced once a later capture could resolve the class. A
model that matches on a supertype then stops matching, with no diagnostic: `close()` is no longer
a release, and a stream closed through the reader that wraps it is reported as leaked.

    // Kotlin, with only a Kotlin capture: reported a resource leak on the FileInputStream that
    // bufferedReader() wraps, even though use{} closes the reader, which closes the stream
    fun firstLine(path: String): String? = File(path).bufferedReader().use { it.readLine() }

The Kotlin capture now passes on the classpath it was given, and a placeholder is replaced as
soon as some capture can resolve the class.

Limitations: a class that is on neither the compiler's classpath nor the compiler's output is
still a placeholder, so models matching on its supertypes still do not fire.

## Test plan
New Kotlin resource-leak tests, which only report correctly with a usable type environment; the
Java and Kotlin pulse suites.
@meta-cla meta-cla Bot added the CLA Signed label Dec 2, 2026
@VladimirMakaev
VladimirMakaev marked this pull request as ready for review December 2, 2026 11:25
[compiler_classpath] is what the compiler itself was given: without it a capture that does
not go through javac's verbose output, such as a Kotlin one, cannot resolve any library
class, and records it in the type environment as a supertype-less dummy instead. *)
split Config.bootclasspath @ split Config.classpath @ split compiler_classpath

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a bug in this code (before these changes) where we are missing the bootclasspath in some cases. This could explain why the issue in UnknownCode.kt is not reported. I will have a look later today.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right: FromArguments omits the JDK platform by default—nothing in the integrations supplies --bootclasspath (infer/src/base/Config.ml:850, infer/src/java/jClasspath.ml:242), and read_modules is applied only on the javac verbose-output path (jClasspath.ml:141).

I tried a minimal local change adding the JDK platform default and module expansion on the argument path: AbstractStringBuilder gained its real JDK fields, but the Kotlin pulse, Kotlin resources and Java pulse report outputs were byte-identical with Kotlin 2.4.21/JDK 21. UnknownCode.kt gained no issue and FN_callPropagateFootprintBad and FN_callPropagateThenPropagateFootprintBad stayed unreported, so that change did not recover the missing finding.

I can send the classpath fix separately.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants