Repository navigation
[java] resolve library classes in a capture that does not run javac - #2351
VladimirMakaev wants to merge 1 commit into
Conversation
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.
| [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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 longera release, and a stream closed through the reader that wraps it is reported as leaked.
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$1vs$lambda$0); the parent commit shows the same difference with this compiler.