Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.41k stars 620 forks source link

Segmentation fault when deserialising on iOS #1914

Open kierans opened 2 years ago

kierans commented 2 years ago

Describe the bug

When attempting to deserialise some JSON to objects, a segmentation fault occurs when running on iOS (via the iosX64Test target)

To Reproduce

See https://github.com/kierans/mpp-serialisation for a sample project which reproduces the issue.

Expected behavior

Environment

nomisRev commented 2 years ago

I am running into this issue for all other native targets as well.

Very strange though, when I move the same code to macosX64Main for example, then I don't run into this issue. I was testing it through main as recommended in this ticket. https://youtrack.jetbrains.com/issue/KT-51217

Set up targets in my project:

  linuxX64()
  mingwX64()
  iosArm32()
  iosArm64()
  iosSimulatorArm64()
  iosX64()
  macosArm64()
  macosX64()
  tvosArm64()
  tvosSimulatorArm64()
  tvosX64()
  watchosArm32()
  watchosArm64()
  watchosSimulatorArm64()
  watchosX64()
  watchosX86()

EDIT: I wanted to switch to kotlin-test to make sure Kotest was not to blame, but could not find a straight forward guide explaining how to test native code using kotlin-test. I am happy to rewrite my tests to kotlin-test if someone can link direction to how to set kotlin-test up for native targets.

kierans commented 2 years ago

Thanks @nomisRev. Did you just move the commonMain code to macosX64Main or everything (test code included)?

If you want to do a PR on my sample project with your setup that would be helpful so I can try to keep digging into it.

nomisRev commented 2 years ago

Hey @kierans, I only moved my test code to macosX64Main, and then build the binary and ran it. When I did that, then it didn't fail with the segment error.

That is what makes me think there is a chance Kotest is interfering. My test was nothing special, just a simple @Serializable class that tests the isomorphism of KSerializer<A>.

@Serializable data class City(val streets: List<Street>)
@Serializable data class Street(val name: String)

val city = City(listOf(Street("1"), Street("2"), Street("3")))
val json = Json.parseToJsonElement(City.serializer(), city)
val reverse = Json.decodeFromJsonElement(City.serializer(), json)
kierans commented 2 years ago

@i-walker @sksamuel Is this something you guys might be able to help with?

sksamuel commented 2 years ago

@kierans I don't use a mac so won't be able to reproduce ios only errors, but I notice the Kotest versions are quite behind. You're using 5.0.x, I would try 5.2.3. Long shot...

But looking at that linked YT ticket it does seem to be just something related to test and the ios target in general.

kierans commented 2 years ago

You're using 5.0.x, I would try 5.2.3. Long shot...

@sksamuel Updated and tried again. No dice.

i-walker commented 2 years ago

Hey @kierans, I only moved my test code to macosX64Main, and then build the binary and ran it. When I did that, then it didn't fail with the segment error.

the suggestion from @nomisRev on your project @kierans works for me. But solely upgrading kotest didn't

kierans commented 2 years ago

Thanks @i-walker. However moving test code into a main folder doesn't seem like a great solution to me. 😄

I'm not sure how to continue to debug this.

nomisRev commented 2 years ago

However moving test code into a main folder doesn't seem like a great solution to me.

It's definitely not a solution 😅 It just shows that the segment error doesn't occur in main, but only in test. This is why I was trying to compare kotlin-test vs Kotest, just to try a 3rd environment.