CwlDemangle icon indicating copy to clipboard operation
CwlDemangle copied to clipboard

Sometimes crash when try to print description of a Swift Symbol

Open LiYanan2004 opened this issue 11 months ago • 4 comments

let symbol = try parseMangledSwiftSymbol("_$s7SwiftUI17_Rotation3DEffectV14animatableDataAA14AnimatablePairVySdAFy12CoreGraphics7CGFloatVAFyAiFyAiFyAFyA2IGAJGGGGGvpMV")
print(symbol.description) // Crash
Image

This symbol comes from SwiftUICore. If I try to demangle it from terminal, it prints:

$ swift demangle s7SwiftUI17_Rotation3DEffectV14animatableDataAA14AnimatablePairVySdAFy12CoreGraphics7CGFloatVAFyAiFyAiFyAFyA2IGAJGGGGGvpMV

property descriptor for SwiftUI._Rotation3DEffect.animatableData : SwiftUI.AnimatablePair<Swift.Double, SwiftUI.AnimatablePair<CoreGraphics.CGFloat, SwiftUI.AnimatablePair<CoreGraphics.CGFloat, SwiftUI.AnimatablePair<CoreGraphics.CGFloat, SwiftUI.AnimatablePair<SwiftUI.AnimatablePair<CoreGraphics.CGFloat, CoreGraphics.CGFloat>, SwiftUI.AnimatablePair<CoreGraphics.CGFloat, CoreGraphics.CGFloat>>>>

LiYanan2004 avatar Feb 08 '25 03:02 LiYanan2004

I'm not able to reproduce this. An EXC_BAD_ACCESS is not something that's usually possible in Swift. It looks like something has corrupted the stack. Possibly stack overflow or some weird compiler issue. But I would expect a stack overflow to be reproducible.

Is this just something that's happened due to an unclean build or some specific build configuration?

mattgallagher avatar Feb 09 '25 22:02 mattgallagher

@mattgallagher Sorry for the delay

I tried the code in main.swift, working as normal, but in my project, I may use it in async context.

The issue is reproducible when it switched to another thread.

Try this example:


Task.detached {
    let symbol = try parseMangledSwiftSymbol("_$s7SwiftUI17_Rotation3DEffectV14animatableDataAA14AnimatablePairVySdAFy12CoreGraphics7CGFloatVAFyAiFyAiFyAFyA2IGAJGGGGGvpMV")
    print(symbol.description) // Crash
}

RunLoop.main.run()
Image

LiYanan2004 avatar Feb 10 '25 16:02 LiYanan2004

The workaround for this issue is to operate SwiftSymbol processing only on main thread

Task { @MainActor in
    let symbol = try parseMangledSwiftSymbol("_$s7SwiftUI17_Rotation3DEffectV14animatableDataAA14AnimatablePairVySdAFy12CoreGraphics7CGFloatVAFyAiFyAiFyAFyA2IGAJGGGGGvpMV")
    print(symbol.description) // Works
}

That's why you can't reproduce it, I think

Anyway, that's good, at least we found workaround and the reason that causes the issue

LiYanan2004 avatar Feb 10 '25 16:02 LiYanan2004

Thanks. I can reproduce the crash, now. I'll see if I can understand what's going on and fix it properly.

mattgallagher avatar Feb 10 '25 22:02 mattgallagher

While this was easy to reproduce, it was a little annoying to work around (needed some tedious refactoring). However, it should now be fixed on master.

mattgallagher avatar Mar 31 '25 11:03 mattgallagher