STREGAsGate / GateEngine

Swift Game Engine supporting Windows, Linux, macOS, HTML5, and more 🚂
Apache License 2.0
280 stars 6 forks source link

Crashing on Linux #46

Open andymule opened 2 months ago

andymule commented 2 months ago

I'm in WSL, but have no trouble running other graphics examples, like raylib stuff and xclock. I even made a test X11 app that calls __strlen_avx2 (which is crashing in your example), and that works. Any ideas? Lots of dumps attached

❯ swift run 3D_01_RotatingCube
Building for debugging...
[6/6] Linking 3D_01_RotatingCube
Build complete! (1.36s)
[GateEngine] Loaded static resource search paths: (GameDelegate search paths not included)
  "[MainBundle]/GateEngineDemos_2D_01_AnimatedSprite.resources",
  "[MainBundle]/",
  "[MainBundle]/GateEngine_GateEngine.resources",
[GateEngine] No Game State "SaveState.json" found. Creating new Game State.
[GateEngine] No Game State "GateEngine.json" found. Creating new Game State.
swift-runtime: unable to suspend thread 8033
swift-runtime: unable to suspend thread 8033

💣 Program crashed: Bad pointer dereference at 0x0000000000000000

Thread 0 "3D_01_RotatingC" crashed:

0 0x00007fe8b54467fd __strlen_avx2 + 29 in libc.so.6

Backtrace took 0.61s
❯ swift run 2D_Pong
Building for debugging...
[630/630] Linking 2D_Pong
Build complete! (15.23s)
[GateEngine] Loaded static resource search paths: (GameDelegate search paths not included)
  "[MainBundle]/GateEngineDemos_2D_01_AnimatedSprite.resources",
  "[MainBundle]/",
  "[MainBundle]/GateEngine_GateEngine.resources",
[GateEngine] No Game State "SaveState.json" found. Creating new Game State.
[GateEngine] No Game State "GateEngine.json" found. Creating new Game State.
swift-runtime: unable to suspend thread 7893
swift-runtime: unable to suspend thread 7893

💣 Program crashed: Bad pointer dereference at 0x0000000000000000

Thread 0 "2D_Pong" crashed:

0 0x00007ffb1d06f7fd __strlen_avx2 + 29 in libc.so.6

Backtrace took 0.61s
* thread STREGAsGate/GateEngineDemos#1, name = '2D_Pong', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
  * frame #0: 0x00007ffff695d7fd libc.so.6`__strlen_avx2 at strlen-avx2.S:74
    frame STREGAsGate/GateEngineDemos#1: 0x00007ffff7e7ff32 libX11.so.6`___lldb_unnamed_symbol1648 + 98
    frame STREGAsGate/GateEngineDemos#2: 0x00007ffff7e80df1 libX11.so.6`XrmGetStringDatabase + 65
    frame STREGAsGate/GateEngine#32: 0x00005555559af3d9 2D_Pong`X11Window.updateStoredMetaData(pixelSize=nil, self=0x0000555556cfd420) at X11Window.swift:30:32
    frame STREGAsGate/GateEngineDemos#4: 0x00005555559b04e8 2D_Pong`X11Window.init(window=0x0000555556cc6f40, self=0x0000555556cfd420) at X11Window.swift:90:9
    frame STREGAsGate/GateEngineDemos#5: 0x00005555559afe14 2D_Pong`X11Window.__allocating_init(window:) at X11Window.swift:0
    frame STREGAsGate/GateEngine#46: 0x0000555555a08250 2D_Pong`Window.windowBacking.getter(self=0x0000555556cc6f40) at
 Window.swift:51:58
    frame STREGAsGate/GateEngineDemos#7: 0x0000555555a08523 2D_Pong`Window.renderTargetBackend.getter(self=0x0000555556cc6f40) at Window.swift:54:9
    frame STREGAsGate/GateEngineDemos#8: 0x0000555555a0887a 2D_Pong`Window.renderTargetBackend.modify at <compiler-generated>:0
    frame STREGAsGate/GateEngineDemos#9: 0x0000555555a0acd9 2D_Pong`protocol witness for _RenderTargetProtocol.renderTargetBackend.modify in conformance Window at <compiler-generated>:0
    frame #10: 0x00005555559fbedd 2D_Pong`_RenderTargetProtocol.clearColor.setter(newValue=(red = 0, green = 0, blue = 0, alpha = 1), self=0x0000555556cc6f40) at RenderTarget.swift:164:44
    frame #11: 0x0000555555a09d3f 2D_Pong`Window.init(identifier="main", style=system, options=(rawValue = 8), self=0x0000555556cc6f40) at Window.swift:127:25
    frame #12: 0x0000555555a09bd2 2D_Pong`Window.__allocating_init(identifier:style:options:) at
 Window.swift:0
    frame #13: 0x0000555555a0d24c 2D_Pong`WindowManager.createWindow(identifier="main", style=system, options=(rawValue = 8), self=0x0000555557155590) at WindowManager.swift:56:30
    frame #14: 0x00005555557c0024 2D_Pong`GameDelegate.createMainWindow(game=0x0000555556cdae80, identifier="main", self=0x0000555556cc3e10) at GameDelegate.swift:78:39
    frame #15: 0x0000555555c92661 2D_Pong`protocol witness for GameDelegate.createMainWindow(game:identifier:) in conformance PongGameDelegate at <compiler-generated>:0
    frame #16: 0x00005555557bd79e 2D_Pong`Game.didFinishLaunching(self=0x0000555556cdae80) at Game.swift:62:34
    frame #17: 0x00007ffff763c54d libswift_Concurrency.so`swift::runJobInEstablishedExecutorContext(swift::Job*) + 349
    frame #18: 0x00007ffff763cd7c libswift_Concurrency.so`swift_job_run + 92
    frame #19: 0x00007ffff73cc8c8 libdispatch.so`_dispatch_main_queue_callback_4CF + 504
    frame #20: 0x00007ffff70ab728 libFoundation.so`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
    frame #21: 0x00007ffff70a74b9 libFoundation.so`__CFRunLoopRun + 1705
    frame #22: 0x00007ffff70a6b6e libFoundation.so`CFRunLoopRunSpecific + 510
    frame #23: 0x00007ffff6ffccf2 libFoundation.so`Foundation.RunLoop.run(until: Foundation.Date) -> () + 226
    frame #24: 0x00005555559ad105 2D_Pong`LinuxPlatform.main(self=0x0000555556cc3e30) at LinuxPlatform.swift:58:26
    frame #25: 0x00005555557c1863 2D_Pong`static GameDelegate.main(self=@thick _D_Pong.PongGameDelegate.Type) at GameDelegate.swift:154:30
    frame #26: 0x0000555555c924c3 2D_Pong`static PongGameDelegate.$main(self=@thick _D_Pong.PongGameDelegate.Type) at <compiler-generated>:0
    frame #27: 0x0000555555c9e128 2D_Pong`_D_Pong_main at Pong.swift:46:13
    frame #28: 0x00007ffff67e9d90 libc.so.6`__libc_start_call_main(main=(2D_Pong`_D_Pong_main at Pong.swift), argc=1, argv=0x00007fffffffdfa8) at libc_start_call_main.h:58:16
    frame #29: 0x00007ffff67e9e40 libc.so.6`__libc_start_main_impl(main=(2D_Pong`_D_Pong_main at Pong.swift), argc=1, argv=0x00007fffffffdfa8, init=<unavailable>, fini=<unavailable>, rtld_fini=<unavailable>, stack_end=0x00007fffffffdf98) at libc-start.c:392:3
    frame #30: 0x00005555555eb6f5 2D_Pong`_start + 37
(lldb)
andymule commented 2 months ago

The problem is either a missing or an empty XResources database. I can't find where the actual file is on ubuntu, but I can say that inserting ANYTHING into the database fixes these crashes. I just did this xrdb -merge <<< "MyApp.background: white"

I'll poke around the code and see if I can't add a default database write before the read as a permanent workaround. This seems more like a bug on X11 library on WSL (maybe all of Ubuntu? idk)

andymule commented 2 months ago

In X11Window.swift you just need this string to not be nil and everything runs fine.

I dont actually understand what's supposed to be happening here so I'll leave it to you to patch properly. Thanks for your work!

@preconcurrency @MainActor func updateStoredMetaData(pixelSize: Size2? = nil) {
        var resourceString: UnsafeMutablePointer<CChar>? = XResourceManagerString(xDisplay)
        XrmInitialize() /* Need to initialize the DB before calling Xrm* functions */

        if resourceString == nil {
            resourceString = UnsafeMutablePointer<CChar>.allocate(capacity: 1)
        }