Skip to content

[pulse] do not continue past the Kotlin intrinsics that always throw - #2340

Merged
dulmarod merged 1 commit into
meta:mainfrom
VladimirMakaev:pr/pulse-kotlin-intrinsics-1126
Dec 2, 2026
Merged

dulmarod merged 1 commit into
meta:mainfrom
VladimirMakaev:pr/pulse-kotlin-intrinsics-1126

Conversation

@VladimirMakaev

Copy link
Copy Markdown
Contributor

kotlin.jvm.internal.Intrinsics has a family of static methods that never return, which
kotlinc emits for the language's own checks. They are library bytecode, so with no model the
call is unknown, it returns, and the path carries on. Reading a lateinit property is the
costly case: it compiles to "load the field; if it is non-null jump over; otherwise call
throwUninitializedPropertyAccessException; push null", so a path continuing past the thrower
reaches that pushed null, and every such read was reported as a null dereference.

class C {
  lateinit var r: Repo
  fun read() = r.use()   // reported NULLPTR_DEREFERENCE on the compiler's own null
}

Only the throwers whose failure the analysis cannot reason about are modelled. The
checkNotNull family is deliberately left alone, even though it has the same contract as
Guava's Preconditions.checkNotNull, which is modelled right above it as pruning its argument
non-null. Pruning makes the null branch unsatisfiable, so Pulse drops the disjunct rather than
reporting it, and the two members of the family that matter are not assertions the programmer
made: kotlinc emits checkNotNull for an implicit platform-type coercion as well as for x!!,
and emits checkNotNullParameter at the head of every function with a non-null formal. Both
carry Java-to-Kotlin null flows. Modelling them costs, in this suite alone,
Kotlin.passNullFromJavaWhenDisallowedBad and all six BasicJavaKotlinBoundary NULL_ARGUMENT
findings. throwNpe/throwJavaNpe are the pre-1.4 compilation of the same two constructs and
are left out for the same reason.

Limitations: a lateinit property that genuinely is never assigned is now not reported either.
A !! whose value Pulse can see is null is still reported at the dereference past the check
rather than at the assertion; separating that from the platform-type coercion would need source
information the bytecode does not carry, since kotlinc compiles both to the same instruction.

Test plan

New codetoanalyze/kotlin/pulse/Lateinit.kt: the lateinit read is silent, while !! on a
null local, !! on a null field and passing null to a non-null parameter still report.

@meta-cla meta-cla Bot added the CLA Signed label Nov 28, 2026
@dulmarod

Copy link
Copy Markdown
Contributor

I think we should trim the comment.

`kotlin.jvm.internal.Intrinsics` has a family of static methods that never return, which
kotlinc emits for the language's own checks. They are library bytecode, so with no model the
call is unknown, it returns, and the path carries on. Reading a `lateinit` property is the
costly case: it compiles to "load the field; if it is non-null jump over; otherwise call
`throwUninitializedPropertyAccessException`; push null", so a path continuing past the thrower
reaches that pushed null, and every such read was reported as a null dereference.

    class C {
      lateinit var r: Repo
      fun read() = r.use()   // reported NULLPTR_DEREFERENCE on the compiler's own null
    }

Only the throwers whose failure the analysis cannot reason about are modelled. The
`checkNotNull` family is deliberately left alone, even though it has the same contract as
Guava's `Preconditions.checkNotNull`, which is modelled right above it as pruning its argument
non-null. Pruning makes the null branch unsatisfiable, so Pulse drops the disjunct rather than
reporting it, and the two members of the family that matter are not assertions the programmer
made: kotlinc emits `checkNotNull` for an implicit platform-type coercion as well as for `x!!`,
and emits `checkNotNullParameter` at the head of every function with a non-null formal. Both
carry Java-to-Kotlin null flows. Modelling them costs, in this suite alone,
`Kotlin.passNullFromJavaWhenDisallowedBad` and all six `BasicJavaKotlinBoundary` NULL_ARGUMENT
findings. `throwNpe`/`throwJavaNpe` are the pre-1.4 compilation of the same two constructs and
are left out for the same reason.

Limitations: a `lateinit` property that genuinely is never assigned is now not reported either.
A `!!` whose value Pulse can see is null is still reported at the dereference past the check
rather than at the assertion; separating that from the platform-type coercion would need source
information the bytecode does not carry, since kotlinc compiles both to the same instruction.

## Test plan
New `codetoanalyze/kotlin/pulse/Lateinit.kt`: the `lateinit` read is silent, while `!!` on a
null local, `!!` on a null field and passing null to a non-null parameter still report.
@VladimirMakaev
VladimirMakaev force-pushed the pr/pulse-kotlin-intrinsics-1126 branch from 38ad901 to a9b4ca6 Compare December 1, 2026 22:47
@VladimirMakaev

Copy link
Copy Markdown
Contributor Author

Trimmed the Intrinsics comment to two lines; the models and test expectations are unchanged.

@dulmarod
dulmarod merged commit ba60e48 into meta:main Dec 2, 2026
8 checks passed
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