kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Segmentation fault when deserialising on iOS

Open kierans opened this issue 3 years ago • 9 comments
trafficstars

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

  • Kotlin version: 1.6.20
  • Library version: 1.3.2
  • Kotlin platforms: JVM, iOS
  • Gradle version: 7.1.1
  • IDE version (if bug is related to the IDE): IntelliJ IDEA 2021.3.3 (Ultimate Edition)
  • Other relevant context: macOS 11.6.3 Xcode 12.5.1 Build version 12E507

kierans avatar Apr 27 '22 01:04 kierans

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.

nomisRev avatar Apr 30 '22 12:04 nomisRev

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.

kierans avatar May 01 '22 02:05 kierans

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)

nomisRev avatar May 01 '22 08:05 nomisRev

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

kierans avatar May 02 '22 11:05 kierans

@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.

sksamuel avatar May 02 '22 13:05 sksamuel

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

@sksamuel Updated and tried again. No dice.

kierans avatar May 02 '22 23:05 kierans

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

i-walker avatar May 02 '22 23:05 i-walker

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.

kierans avatar May 02 '22 23:05 kierans

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.

nomisRev avatar May 04 '22 06:05 nomisRev