Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve readability of proto decode exception message #2768

Merged
merged 6 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,29 @@ import kotlinx.serialization.modules.*
import kotlinx.serialization.protobuf.*

internal typealias ProtoDesc = Long
internal const val VARINT = 0
internal const val i64 = 1
internal const val SIZE_DELIMITED = 2
internal const val i32 = 5

internal enum class ProtoWireType(val typeId: Int) {
xiaozhikang0916 marked this conversation as resolved.
Show resolved Hide resolved
INVALID(-1),
VARINT(0),
i64(1),
SIZE_DELIMITED(2),
i32(5),
;

companion object {
fun fromTypeId(typeId: Int): ProtoWireType {
xiaozhikang0916 marked this conversation as resolved.
Show resolved Hide resolved
return ProtoWireType.entries.find { it.typeId == typeId } ?: INVALID
}
}

fun wireIntWithTag(tag: Int): Int {
return ((tag shl 3) or typeId)
}

override fun toString(): String {
return "${this.name}($typeId)"
}
xiaozhikang0916 marked this conversation as resolved.
Show resolved Hide resolved
}

internal const val ID_HOLDER_ONE_OF = -2

Expand Down Expand Up @@ -104,7 +123,7 @@ internal fun extractProtoId(descriptor: SerialDescriptor, index: Int, zeroBasedD
return result
}

internal class ProtobufDecodingException(message: String) : SerializationException(message)
internal class ProtobufDecodingException(message: String, e: Throwable? = null) : SerializationException(message, e)

internal expect fun Int.reverseBytes(): Int
internal expect fun Long.reverseBytes(): Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,41 +122,53 @@ internal open class ProtobufDecoder(
}

override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
return when (descriptor.kind) {
StructureKind.LIST -> {
val tag = currentTagOrDefault
return if (this.descriptor.kind == StructureKind.LIST && tag != MISSING_TAG && this.descriptor != descriptor) {
val reader = makeDelimited(reader, tag)
// repeated decoder expects the first tag to be read already
reader.readTag()
// all elements always have id = 1
RepeatedDecoder(proto, reader, ProtoDesc(1, ProtoIntegerType.DEFAULT), descriptor)

} else if (reader.currentType == SIZE_DELIMITED && descriptor.getElementDescriptor(0).isPackable) {
val sliceReader = ProtobufReader(reader.objectInput())
PackedArrayDecoder(proto, sliceReader, descriptor)

} else {
RepeatedDecoder(proto, reader, tag, descriptor)
return try {
when (descriptor.kind) {
StructureKind.LIST -> {
val tag = currentTagOrDefault
return if (this.descriptor.kind == StructureKind.LIST && tag != MISSING_TAG && this.descriptor != descriptor) {
val reader = makeDelimited(reader, tag)
// repeated decoder expects the first tag to be read already
reader.readTag()
// all elements always have id = 1
RepeatedDecoder(proto, reader, ProtoDesc(1, ProtoIntegerType.DEFAULT), descriptor)

} else if (reader.currentType == ProtoWireType.SIZE_DELIMITED && descriptor.getElementDescriptor(0).isPackable) {
val sliceReader = ProtobufReader(reader.objectInput())
PackedArrayDecoder(proto, sliceReader, descriptor)

} else {
RepeatedDecoder(proto, reader, tag, descriptor)
}
}
}
StructureKind.CLASS, StructureKind.OBJECT, is PolymorphicKind -> {
val tag = currentTagOrDefault
// Do not create redundant copy
if (tag == MISSING_TAG && this.descriptor == descriptor) return this
if (tag.isOneOf) {
// If a tag is annotated as oneof
// [tag.protoId] here is overwritten with index-based default id in
// [kotlinx.serialization.protobuf.internal.HelpersKt.extractParameters]
// and restored the real id from index2IdMap, set by [decodeElementIndex]
val rawIndex = tag.protoId - 1
val restoredTag = index2IdMap?.get(rawIndex)?.let { tag.overrideId(it) } ?: tag
return OneOfPolymorphicReader(proto, reader, restoredTag, descriptor)

StructureKind.CLASS, StructureKind.OBJECT, is PolymorphicKind -> {
val tag = currentTagOrDefault
// Do not create redundant copy
if (tag == MISSING_TAG && this.descriptor == descriptor) return this
if (tag.isOneOf) {
// If a tag is annotated as oneof
// [tag.protoId] here is overwritten with index-based default id in
// [kotlinx.serialization.protobuf.internal.HelpersKt.extractParameters]
// and restored the real id from index2IdMap, set by [decodeElementIndex]
val rawIndex = tag.protoId - 1
val restoredTag = index2IdMap?.get(rawIndex)?.let { tag.overrideId(it) } ?: tag
return OneOfPolymorphicReader(proto, reader, restoredTag, descriptor)
}
return ProtobufDecoder(proto, makeDelimited(reader, tag), descriptor)
}
return ProtobufDecoder(proto, makeDelimited(reader, tag), descriptor)

StructureKind.MAP -> MapEntryReader(
proto,
makeDelimitedForced(reader, currentTagOrDefault),
currentTagOrDefault,
descriptor
)

else -> throw SerializationException("Primitives are not supported at top-level")
}
StructureKind.MAP -> MapEntryReader(proto, makeDelimitedForced(reader, currentTagOrDefault), currentTagOrDefault, descriptor)
else -> throw SerializationException("Primitives are not supported at top-level")
} catch (e: ProtobufDecodingException) {
throw ProtobufDecodingException("Fail to begin structure for ${descriptor.serialName} in ${this.descriptor.serialName} at proto number ${currentTagOrDefault.protoId}", e)
}
}

Expand All @@ -173,41 +185,53 @@ internal open class ProtobufDecoder(
override fun decodeTaggedByte(tag: ProtoDesc): Byte = decodeTaggedInt(tag).toByte()
override fun decodeTaggedShort(tag: ProtoDesc): Short = decodeTaggedInt(tag).toShort()
override fun decodeTaggedInt(tag: ProtoDesc): Int {
return if (tag == MISSING_TAG) {
reader.readInt32NoTag()
} else {
reader.readInt(tag.integerType)
try {
xiaozhikang0916 marked this conversation as resolved.
Show resolved Hide resolved
return if (tag == MISSING_TAG) {
reader.readInt32NoTag()
} else {
reader.readInt(tag.integerType)
}
} catch (e: ProtobufDecodingException) {
rethrowException(tag, e)
}
}
override fun decodeTaggedLong(tag: ProtoDesc): Long {
return if (tag == MISSING_TAG) {
reader.readLongNoTag()
} else {
reader.readLong(tag.integerType)
return decodeOrThrow(tag) {
if (tag == MISSING_TAG) {
reader.readLongNoTag()
} else {
reader.readLong(tag.integerType)
}
}
}

override fun decodeTaggedFloat(tag: ProtoDesc): Float {
return if (tag == MISSING_TAG) {
reader.readFloatNoTag()
} else {
reader.readFloat()
return decodeOrThrow(tag) {
if (tag == MISSING_TAG) {
reader.readFloatNoTag()
} else {
reader.readFloat()
}
}
}
override fun decodeTaggedDouble(tag: ProtoDesc): Double {
return if (tag == MISSING_TAG) {
reader.readDoubleNoTag()
} else {
reader.readDouble()
return decodeOrThrow(tag) {
if (tag == MISSING_TAG) {
reader.readDoubleNoTag()
} else {
reader.readDouble()
}
}
}
override fun decodeTaggedChar(tag: ProtoDesc): Char = decodeTaggedInt(tag).toChar()

override fun decodeTaggedString(tag: ProtoDesc): String {
return if (tag == MISSING_TAG) {
reader.readStringNoTag()
} else {
reader.readString()
return decodeOrThrow(tag) {
if (tag == MISSING_TAG) {
reader.readStringNoTag()
} else {
reader.readString()
}
}
}

Expand All @@ -218,22 +242,49 @@ internal open class ProtobufDecoder(
override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T = decodeSerializableValue(deserializer, null)

@Suppress("UNCHECKED_CAST")
override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>, previousValue: T?): T = when {
deserializer is MapLikeSerializer<*, *, *, *> -> {
deserializeMap(deserializer as DeserializationStrategy<T>, previousValue)
override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>, previousValue: T?): T = try {
when {
deserializer is MapLikeSerializer<*, *, *, *> -> {
deserializeMap(deserializer as DeserializationStrategy<T>, previousValue)
}

deserializer.descriptor == ByteArraySerializer().descriptor -> deserializeByteArray(previousValue as ByteArray?) as T
deserializer is AbstractCollectionSerializer<*, *, *> ->
(deserializer as AbstractCollectionSerializer<*, T, *>).merge(this, previousValue)

else -> deserializer.deserialize(this)
}
} catch (e: ProtobufDecodingException) {
val currentTag = currentTagOrDefault
val msg = if (descriptor != deserializer.descriptor) {
// Decoding child element
if (descriptor.kind == StructureKind.LIST && deserializer.descriptor.kind != StructureKind.MAP) {
// Decoding repeated field
"Error while decoding index ${currentTag.protoId - 1} in repeated field of ${deserializer.descriptor.serialName}"
} else if (descriptor.kind == StructureKind.MAP) {
// Decoding map field
val index = (currentTag.protoId - 1) / 2
val field = if ((currentTag.protoId - 1) % 2 == 0) { "key" } else "value"
"Error while decoding $field of index $index in map field of ${deserializer.descriptor.serialName}"
} else {
// Decoding common class
"Error while decoding ${deserializer.descriptor.serialName} at proto number ${currentTag.protoId} of ${descriptor.serialName}"
}
} else {
// Decoding self
"Error while decoding ${descriptor.serialName}"
}
deserializer.descriptor == ByteArraySerializer().descriptor -> deserializeByteArray(previousValue as ByteArray?) as T
deserializer is AbstractCollectionSerializer<*, *, *> ->
(deserializer as AbstractCollectionSerializer<*, T, *>).merge(this, previousValue)
else -> deserializer.deserialize(this)
throw ProtobufDecodingException(msg, e)
}

private fun deserializeByteArray(previousValue: ByteArray?): ByteArray {
val tag = currentTagOrDefault
val array = if (tag == MISSING_TAG) {
reader.readByteArrayNoTag()
} else {
reader.readByteArray()
val array = decodeOrThrow(tag) {
if (tag == MISSING_TAG) {
reader.readByteArrayNoTag()
} else {
reader.readByteArray()
}
}
return if (previousValue == null) array else previousValue + array
}
Expand All @@ -252,29 +303,33 @@ internal open class ProtobufDecoder(
override fun SerialDescriptor.getTag(index: Int) = extractParameters(index)

override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
while (true) {
val protoId = reader.readTag()
if (protoId == -1) { // EOF
return elementMarker.nextUnmarkedIndex()
}
val index = getIndexByNum(protoId)
if (index == -1) { // not found
reader.skipElement()
} else {
if (descriptor.extractParameters(index).isOneOf) {
/**
* While decoding message with one-of field,
* the proto id read from wire data cannot be easily found
* in the properties of this type,
* So the index of this one-of property and the id read from the wire
* are saved in this map, then restored in [beginStructure]
* and passed to [OneOfPolymorphicReader] to get the actual deserializer.
*/
index2IdMap?.put(index, protoId)
try {
while (true) {
val protoId = reader.readTag()
if (protoId == -1) { // EOF
return elementMarker.nextUnmarkedIndex()
}
val index = getIndexByNum(protoId)
if (index == -1) { // not found
reader.skipElement()
} else {
if (descriptor.extractParameters(index).isOneOf) {
/**
* While decoding message with one-of field,
* the proto id read from wire data cannot be easily found
* in the properties of this type,
* So the index of this one-of property and the id read from the wire
* are saved in this map, then restored in [beginStructure]
* and passed to [OneOfPolymorphicReader] to get the actual deserializer.
*/
index2IdMap?.put(index, protoId)
}
elementMarker.mark(index)
return index
}
elementMarker.mark(index)
return index
}
} catch (e: ProtobufDecodingException) {
throw ProtobufDecodingException("Fail to get element index for ${descriptor.serialName} in ${this.descriptor.serialName}", e)
}
}

Expand All @@ -296,6 +351,19 @@ internal open class ProtobufDecoder(
}
return false
}

private inline fun <T> decodeOrThrow(tag: ProtoDesc, crossinline action: (tag: ProtoDesc) -> T): T {
xiaozhikang0916 marked this conversation as resolved.
Show resolved Hide resolved
try {
return action(tag)
} catch (e: ProtobufDecodingException) {
rethrowException(tag, e)
}
}

@Suppress("NOTHING_TO_INLINE")
private inline fun rethrowException(tag: ProtoDesc, e: ProtobufDecodingException): Nothing {
throw ProtobufDecodingException("Error while decoding proto number ${tag.protoId} of ${descriptor.serialName}", e)
}
}

private class RepeatedDecoder(
Expand Down
Loading