Skip to content

Commit 79e4583

Browse files
committed
fix(nav3): Fail closed when the property-encoding probe can't confirm the encoder
The built-in SerialNameAndProperties mapper's compatibility probe treated only a LinkageError from the property encoder as conclusive; any other failure was inconclusive and property encoding proceeded anyway. An API-incompatible kotlinx.serialization version can fail the encoder without a LinkageError (for instance, a NullPointerException under R8), so the probe passed and the mapper went on to record wrong arguments. Now any failure of the encode probe disables property encoding and keeps names, since recording wrong arguments is worse than recording none.
1 parent a47b546 commit 79e4583

5 files changed

Lines changed: 79 additions & 66 deletions

File tree

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxBackStackEntryMapper.kt‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ internal class KotlinxBackStackEntryMapper(
7575
private val isEncodingIncompatible: Boolean =
7676
includeProperties &&
7777
run {
78-
val error =
79-
probe.encodingLinkageError { error ->
80-
diagnostics.inconclusiveProbe(ENCODING_CAPABILITY, error)
81-
} ?: return@run false
78+
val error = probe.encoderIncompatibility() ?: return@run false
8279
diagnostics.incompatibleEncoder(error)
8380
true
8481
}
@@ -275,7 +272,6 @@ internal class KotlinxBackStackEntryMapper(
275272
private class SerializerLookup(val serializer: KSerializer<*>?, val error: Throwable?)
276273

277274
private companion object {
278-
private const val ENCODING_CAPABILITY = "property encoding"
279275
private const val NAME_LOOKUP_CAPABILITY = "name lookups"
280276
private val UNRESOLVED = CachedType(serializer = null, name = null)
281277
}
@@ -334,7 +330,7 @@ internal class KotlinxMapperDiagnostics(private val logger: () -> ILogger) {
334330
* Logs [error] indicating that the app's kotlinx.serialization version is incompatible with the
335331
* properties encoder. Entries are recorded by name without arguments.
336332
*/
337-
fun incompatibleEncoder(error: LinkageError) {
333+
fun incompatibleEncoder(error: Throwable) {
338334
logger()
339335
.log(
340336
WARNING,

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxCompatibilityProbe.kt‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ import kotlinx.serialization.Serializable
2323
* `ProbeObject` don't execute any host app code, so a `LinkageError` while handling them means the
2424
* app's kotlinx.serialization version is incompatible.
2525
*
26-
* Note: Only a `LinkageError` is conclusive. Any other failure, or a probe type whose serializer
27-
* can't be found, leaves the check inconclusive: it's reported to the caller's `onInconclusive` and
28-
* otherwise ignored, as a real type that hits the same failure is handled and logged on its own.
26+
* Note: For the name lookup check ([nameLookupLinkageError]), only a `LinkageError` is conclusive.
27+
* Any other failure, or a probe type whose serializer can't be found, leaves it inconclusive: it's
28+
* reported to the caller's `onInconclusive` and otherwise ignored, as a real type that hits the
29+
* same failure is handled and logged on its own. The encoder check ([encoderIncompatibility])
30+
* instead treats any failure as conclusive, because a broken encoder can record wrong arguments
31+
* without throwing, and recording wrong arguments is worse than recording none.
2932
*/
3033
internal interface KotlinxCompatibilityProbe {
3134

@@ -42,14 +45,16 @@ internal interface KotlinxCompatibilityProbe {
4245
fun nameLookupLinkageError(onInconclusive: (Throwable?) -> Unit): LinkageError?
4346

4447
/**
45-
* Encodes [ProbeClass] using the same path as [KotlinxBackStackEntryMapper].
48+
* Encodes [ProbeClass] using the same path as [KotlinxBackStackEntryMapper] and checks that it
49+
* links and runs.
4650
*
47-
* Returns a [LinkageError] if the encoder can't link against the host app's kotlinx.serialization
48-
* version, or `null` if encoding succeeds or the check is inconclusive.
49-
*
50-
* Calls [onInconclusive] for each inconclusive result, passing the cause.
51+
* Returns the error that makes the properties encoder unusable, or `null` if encoding succeeds.
52+
* Unlike [nameLookupLinkageError], any failure is conclusive: an incompatible
53+
* kotlinx.serialization version can fail the encoder without a [LinkageError] (for instance, an
54+
* API-incompatible encoder can throw a [NullPointerException] under R8), and the caller disables
55+
* property encoding on any failure here rather than risk recording wrong arguments.
5156
*/
52-
fun encodingLinkageError(onInconclusive: (Throwable) -> Unit): LinkageError?
57+
fun encoderIncompatibility(): Throwable?
5358

5459
/** Probes the app's kotlinx.serialization version using [ProbeClass] and [ProbeObject]. */
5560
companion object : KotlinxCompatibilityProbe {
@@ -80,14 +85,22 @@ internal interface KotlinxCompatibilityProbe {
8085
return error
8186
}
8287

83-
override fun encodingLinkageError(onInconclusive: (Throwable) -> Unit): LinkageError? =
84-
probe(onInconclusive) {
88+
override fun encoderIncompatibility(): Throwable? =
89+
try {
8590
@Suppress("UNCHECKED_CAST")
8691
KotlinxPropertiesEncoder.encode(
8792
ProbeClass.serializer() as KSerializer<Any>,
8893
ProbeClass(),
8994
TreeArgumentWriter(ArgumentBudget()),
9095
)
96+
null
97+
} catch (@Suppress("TooGenericExceptionCaught") t: Throwable) {
98+
// Broad because an incompatible kotlinx.serialization version can fail the encoder in any
99+
// way, not only with a LinkageError. Any failure disables property encoding and keeps
100+
// names,
101+
// as recording wrong arguments would be worse than recording none.
102+
ExceptionUtils.rethrowIfFatal(t)
103+
t
91104
}
92105

93106
/**

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxBackStackEntryMapperProbeTest.kt‎

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,8 @@ class KotlinxBackStackEntryMapperProbeTest {
132132

133133
@Test
134134
fun `never checks the properties encoder when properties are excluded`() {
135-
// Neither an incompatible nor an inconclusive check is logged, as neither is run.
136-
val probe =
137-
FakeCompatibilityProbe(
138-
encodingError = NoSuchMethodError(),
139-
encodingInconclusiveError = IllegalStateException(),
140-
)
135+
// No incompatible check is logged, as the check isn't run when properties are excluded.
136+
val probe = FakeCompatibilityProbe(encoderError = NoSuchMethodError())
141137
val fixture = KotlinxMapperFixture.create(includeProperties = false, probe = probe)
142138
val mapper = fixture.mapper
143139

@@ -150,7 +146,7 @@ class KotlinxBackStackEntryMapperProbeTest {
150146

151147
@Test
152148
fun `keeps the name and records only the encoding failure when the properties encoder is incompatible`() {
153-
val probe = FakeCompatibilityProbe(encodingError = NoSuchMethodError())
149+
val probe = FakeCompatibilityProbe(encoderError = NoSuchMethodError())
154150
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
155151
val mapper = fixture.mapper
156152

@@ -166,7 +162,7 @@ class KotlinxBackStackEntryMapperProbeTest {
166162

167163
@Test
168164
fun `records no arguments for objects when the properties encoder is incompatible`() {
169-
val probe = FakeCompatibilityProbe(encodingError = NoSuchMethodError())
165+
val probe = FakeCompatibilityProbe(encoderError = NoSuchMethodError())
170166
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
171167
val mapper = fixture.mapper
172168

@@ -177,7 +173,7 @@ class KotlinxBackStackEntryMapperProbeTest {
177173

178174
@Test
179175
fun `doesn't encode properties when the properties encoder is incompatible`() {
180-
val probe = FakeCompatibilityProbe(encodingError = NoSuchMethodError())
176+
val probe = FakeCompatibilityProbe(encoderError = NoSuchMethodError())
181177
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
182178
val mapper = fixture.mapper
183179

@@ -190,7 +186,7 @@ class KotlinxBackStackEntryMapperProbeTest {
190186
@Test
191187
fun `logs a warning with the error and compatibility advice when created for an incompatible properties encoder`() {
192188
val error = NoSuchMethodError("encodeStructure")
193-
val probe = FakeCompatibilityProbe(encodingError = error)
189+
val probe = FakeCompatibilityProbe(encoderError = error)
194190
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
195191

196192
val message = argumentCaptor<String>()
@@ -206,7 +202,7 @@ class KotlinxBackStackEntryMapperProbeTest {
206202
@Test
207203
fun `logs nothing more for entries mapped when the properties encoder is incompatible`() {
208204
val error = NoSuchMethodError()
209-
val probe = FakeCompatibilityProbe(encodingError = error)
205+
val probe = FakeCompatibilityProbe(encoderError = error)
210206
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
211207
val mapper = fixture.mapper
212208

@@ -220,40 +216,59 @@ class KotlinxBackStackEntryMapperProbeTest {
220216
}
221217

222218
@Test
223-
fun `records encoded properties when the encoding check is inconclusive`() {
224-
val probe = FakeCompatibilityProbe(encodingInconclusiveError = IllegalStateException())
219+
fun `keeps the name and records only the encoding failure when the encoder fails without a LinkageError`() {
220+
val probe = FakeCompatibilityProbe(encoderError = NullPointerException())
225221
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
226222
val mapper = fixture.mapper
227223

228224
val mapped = mapper.map(Detail("1"))
229225

230-
assertThat(mapped).isEqualTo(SentryBackStackEntry("Detail", mapOf("id" to "1")))
226+
assertThat(mapped)
227+
.isEqualTo(SentryBackStackEntry("Detail", droppedBy(ArgumentDropReason.ENCODING_FAILED)))
231228
}
232229

233230
@Test
234-
fun `logs at debug level with the error when the encoding check is inconclusive`() {
235-
val error = IllegalStateException("check failed")
236-
val probe = FakeCompatibilityProbe(encodingInconclusiveError = error)
231+
fun `records no arguments for objects when the encoder fails without a LinkageError`() {
232+
val probe = FakeCompatibilityProbe(encoderError = NullPointerException())
233+
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
234+
val mapper = fixture.mapper
235+
236+
val mapped = mapper.map(Home)
237+
238+
assertThat(mapped).isEqualTo(SentryBackStackEntry("Home", arguments = null))
239+
}
240+
241+
@Test
242+
fun `doesn't encode properties when the encoder fails without a LinkageError`() {
243+
val probe = FakeCompatibilityProbe(encoderError = NullPointerException())
244+
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
245+
val mapper = fixture.mapper
246+
247+
mapper.mapAndReadArguments(Detail("1"))
248+
mapper.mapAndWriteArguments(Detail("2"))
237249

250+
assertThat(fixture.detailSerializer.serialized).isEmpty()
251+
}
252+
253+
@Test
254+
fun `logs a warning with the error and compatibility advice when the encoder fails without a LinkageError`() {
255+
val error = NullPointerException("encodeStructure")
256+
val probe = FakeCompatibilityProbe(encoderError = error)
238257
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
239258

240259
val message = argumentCaptor<String>()
241-
verify(fixture.logger, times(1))
242-
.log(
243-
eq(SentryLevel.DEBUG),
244-
eq(error),
245-
message.capture(),
246-
eq(ENCODING_CAPABILITY),
247-
eq(PROBE_THREW),
260+
verify(fixture.logger, times(1)).log(eq(SentryLevel.WARNING), message.capture(), eq(error))
261+
assertThat(message.firstValue)
262+
.startsWith(
263+
"Nav3 SerialNameAndProperties is incompatible with this app's kotlinx.serialization " +
264+
"version.",
248265
)
249-
assertThat(message.firstValue).startsWith(INCONCLUSIVE_PROBE)
266+
assertThat(message.firstValue).contains(INCOMPATIBLE_KOTLINX_ADVICE)
250267
}
251268

252269
private companion object {
253270
const val NAME_LOOKUP_CAPABILITY = "name lookups"
254271

255-
const val ENCODING_CAPABILITY = "property encoding"
256-
257272
const val INCONCLUSIVE_PROBE = "Nav3 couldn't verify kotlinx.serialization support for %s"
258273

259274
const val PROBE_THREW = "the compatibility probe threw (see the attached error)"

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxCompatibilityProbeTest.kt‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,15 @@ class KotlinxCompatibilityProbeTest {
2323
assertThat(inconclusive).isEmpty()
2424
}
2525

26-
// encodingLinkageError()
26+
// encoderIncompatibility()
2727

2828
@Test
29-
fun `encodingLinkageError() - returns null for a compatible kotlinx serialization version`() {
30-
val error = KotlinxCompatibilityProbe.encodingLinkageError {}
29+
fun `encoderIncompatibility() - returns null for a compatible kotlinx serialization version`() {
30+
val error = KotlinxCompatibilityProbe.encoderIncompatibility()
3131

3232
assertThat(error).isNull()
3333
}
3434

35-
@Test
36-
fun `encodingLinkageError() - finds a compatible kotlinx serialization version conclusively`() {
37-
val inconclusive = mutableListOf<Throwable>()
38-
39-
KotlinxCompatibilityProbe.encodingLinkageError { inconclusive += it }
40-
41-
assertThat(inconclusive).isEmpty()
42-
}
43-
4435
// Probe types
4536

4637
@Test
@@ -58,7 +49,7 @@ class KotlinxCompatibilityProbeTest {
5849

5950
@Test
6051
fun `the properties encoder encodes every property of the probe's class`() {
61-
// A probe type that failed to encode for another reason would make the check inconclusive.
52+
// A probe type that failed to encode for another reason would make the check fail closed.
6253
val encoded = encodeToMap(ProbeClass.serializer(), ProbeClass())
6354

6455
assertThat(encoded)

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxMapperFixture.kt‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,15 @@ internal class ShapeThrowingDetailSerializer(
7777

7878
/**
7979
* A [KotlinxCompatibilityProbe] that finds the app's kotlinx.serialization version compatible
80-
* unless [nameLookupError] or [encodingError] is set, so that mapper tests don't depend on the real
81-
* probe. Reports the name check inconclusive with [nameLookupInconclusive], if set, and the
82-
* encoding check inconclusive with [encodingInconclusiveError], if set. Counts the checks of the
83-
* properties encoder in [encodingChecks].
80+
* unless [nameLookupError] or [encoderError] is set, so that mapper tests don't depend on the real
81+
* probe. Reports the name check inconclusive with [nameLookupInconclusive], if set. Any
82+
* [encoderError], whether a [LinkageError] or not, makes the properties encoder unusable and
83+
* disables property encoding. Counts the checks of the properties encoder in [encodingChecks].
8484
*/
8585
internal class FakeCompatibilityProbe(
8686
private val nameLookupError: LinkageError? = null,
87-
private val encodingError: LinkageError? = null,
87+
private val encoderError: Throwable? = null,
8888
private val nameLookupInconclusive: Inconclusive? = null,
89-
private val encodingInconclusiveError: Throwable? = null,
9089
) : KotlinxCompatibilityProbe {
9190
var encodingChecks = 0
9291

@@ -98,10 +97,9 @@ internal class FakeCompatibilityProbe(
9897
return null
9998
}
10099

101-
override fun encodingLinkageError(onInconclusive: (Throwable) -> Unit): LinkageError? {
100+
override fun encoderIncompatibility(): Throwable? {
102101
encodingChecks++
103-
encodingInconclusiveError?.let(onInconclusive)
104-
return encodingError
102+
return encoderError
105103
}
106104

107105
/** Why the name check is inconclusive: [error], or `null` if a probe serializer wasn't found. */

0 commit comments

Comments
 (0)