bkaradzic / bgfx

Cross-platform, graphics API agnostic, "Bring Your Own Engine/Framework" style rendering library.
https://bkaradzic.github.io/bgfx/overview.html
BSD 2-Clause "Simplified" License
15.05k stars 1.94k forks source link

Examples crash on startup on Ubuntu 22.04 #3274

Closed goossens closed 7 months ago

goossens commented 7 months ago

Describe the bug

After commit 2ad67a4, I can no longer start my application nor the examples on Ubuntu 22.04 using Vulkan. Both crash with a core dump caused by a "free(): invalid pointer" error. See console output and stack trace below. If I go back to the commit before 2ad67a4, everything starts up just fine.

To Reproduce

  1. Compile BGFX as documented
  2. Start the examples

Expected behavior

Screenshots

Additional context

Last console output:

2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b02650a0 vkInvalidateMappedMemoryRanges 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b02f9f30 vkBindBufferMemory 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b02fd5e0 vkBindImageMemory 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b3877a80 vkCreateSwapchainKHR 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b027d4d0 vkDestroySwapchainKHR 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b027d670 vkGetSwapchainImagesKHR 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b027d690 vkAcquireNextImageKHR 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b027e260 vkQueuePresentKHR 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b3859b10 vkSetDebugUtilsObjectNameEXT 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b3853370 vkCmdBeginDebugUtilsLabelEXT 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b3853450 vkCmdEndDebugUtilsLabelEXT 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b3853530 vkCmdInsertDebugUtilsLabelEXT 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b031f0c0 vkCmdDrawIndirectCountKHR 2024-04-01 08-35-21.333 [debug] renderer_vk.cpp (1860): BGFX 0x7e89b031f1d0 vkCmdDrawIndexedIndirectCountKHR 2024-04-01 08-35-21.344 [debug] renderer_vk.cpp (7358): BGFX findSurfaceFormat: Surface format RGBA8 not found! Defaulting to BGRA8. free(): invalid pointer Aborted (core dumped)

Backtrace from my application (but examples show the same)

0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=127717710960192) at ./nptl/pthread_kill.c:4$

1 __pthread_kill_internal (signo=6, threadid=127717710960192) at ./nptl/pthread_kill.c:78

2 __GI___pthread_kill (threadid=127717710960192, signo=signo@entry=6) at ./nptl/pthread_kill.c:89

3 0x00007428a2242476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26

4 0x00007428a22287f3 in __GI_abort () at ./stdlib/abort.c:79

5 0x00007428a2289676 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7428a23dbb77 "%s\n")

at ../sysdeps/posix/libc_fatal.c:155

6 0x00007428a22a0cfc in malloc_printerr (str=str@entry=0x7428a23d9744 "free(): invalid pointer") at ./malloc/malloc.c:5664

7 0x00007428a22a2a44 in _int_free (av=, p=, have_lock=0) at ./malloc/malloc.c:4439

8 0x00007428a22a5453 in __GI___libc_free (mem=) at ./malloc/malloc.c:3391

9 0x00005d880b17008b in bgfx::AllocatorStub::realloc (this=0x74289c000b70, _ptr=0x742898caf4b0, _size=0, _align=0,

_file=0x5d880b8a4f98 "vkFree", _line=0) at /home/goossens/ot/debug/_deps/bgfx-src/src/bgfx.cpp:198

10 0x00005d880b177119 in bgfx::vk::freeFunction (_userData=0x74289c000b70, _memory=0x742898caf4b0)

at /home/goossens/ot/debug/_deps/bgfx-src/src/renderer_vk.cpp:575

11 0x0000742891b10f69 in ?? () from /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so

12 0x0000742891b182b3 in ?? () from /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so

13 0x0000742891b2333e in ?? () from /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so

14 0x0000742891b0f489 in ?? () from /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so

15 0x00005d880b1783f3 in bgfx::vk::setImageMemoryBarrier (_commandBuffer=0x742894601540, _image=..., _aspectMask=6,

_oldLayout=VK_IMAGE_LAYOUT_UNDEFINED, _newLayout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, _baseMipLevel=0,
_levelCount=4294967295, _baseArrayLayer=0, _layerCount=4294967295)
at /home/goossens/ot/debug/_deps/bgfx-src/src/renderer_vk.cpp:1088

16 0x00005d880b180e11 in bgfx::vk::TextureVK::setImageMemoryBarrier (this=0x7428a01c59e0, _commandBuffer=0x742894601540,

_newImageLayout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, _singleMsaaImage=false)
at /home/goossens/ot/debug/_deps/bgfx-src/src/renderer_vk.cpp:6393

17 0x00005d880b17dda9 in bgfx::vk::TextureVK::create (this=0x7428a01c59e0, _commandBuffer=0x742894601540, _width=1290,

_height=704, _flags=618475290624, _format=VK_FORMAT_D32_SFLOAT_S8_UINT)
at /home/goossens/ot/debug/_deps/bgfx-src/src/renderer_vk.cpp:5717

18 0x00005d880b182e6a in bgfx::vk::SwapChainVK::createAttachments (this=0x7428a01c5700, _commandBuffer=0x742894601540)

at /home/goossens/ot/debug/_deps/bgfx-src/src/renderer_vk.cpp:7125

19 0x00005d880b18157c in bgfx::vk::SwapChainVK::create (this=0x7428a01c5700, _commandBuffer=0x742894601540, _nwh=0x140000b,

_resolution=..., _depthFormat=bgfx::TextureFormat::Count) at /home/goossens/ot/debug/_deps/bgfx-src/src/renderer_vk.cpp:6568

20 0x00005d880b18417a in bgfx::vk::FrameBufferVK::create (this=0x7428a01c5660, _denseIdx=65535, _nwh=0x140000b, _width=1280,

_height=704, _format=bgfx::TextureFormat::RGBA8, _depthFormat=bgfx::TextureFormat::Count)
bkaradzic commented 7 months ago

I'm not getting that crash. Try changing line 575 in renderer_vk.cpp to:

allocator->realloc(_memory, 0, kMinAlignment, "vkFree", 0);

bkaradzic commented 7 months ago

This should fix, but since I couldn't reproduce it here, I would need you to verify it.

goossens commented 7 months ago

Unfortunately, that didn't solve the problem. I did some more digging and here is what I found:

I still don't know what causes it but hopefully, it gives you some clues. Here's my output from running examplesDebug in gdb (the alloc statements at the top are from a poor mans's debugging attempt):

alloc: 48 alloc: 64 alloc: 0 free(): invalid pointer

Thread 3 "examplesDebug" received signal SIGABRT, Aborted. [Switching to Thread 0x7fffd7da2640 (LWP 25612)] __pthread_kill_implementation (no_tid=0, signo=6, threadid=140736814786112) at ./nptl/pthread_kill.c:44 44 ./nptl/pthread_kill.c: No such file or directory. (gdb) bt

0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=140736814786112) at ./nptl/pthread_kill.c:44

1 __pthread_kill_internal (signo=6, threadid=140736814786112) at ./nptl/pthread_kill.c:78

2 __GI___pthread_kill (threadid=140736814786112, signo=signo@entry=6) at ./nptl/pthread_kill.c:89

3 0x00007ffff7842476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26

4 0x00007ffff78287f3 in __GI_abort () at ./stdlib/abort.c:79

5 0x00007ffff7889676 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff79dbb77 "%s\n")

at ../sysdeps/posix/libc_fatal.c:155

6 0x00007ffff78a0cfc in malloc_printerr (str=str@entry=0x7ffff79d9744 "free(): invalid pointer") at ./malloc/malloc.c:5664

7 0x00007ffff78a2a44 in _int_free (av=, p=, have_lock=0) at ./malloc/malloc.c:4439

8 0x00007ffff78a5453 in __GI___libc_free (mem=) at ./malloc/malloc.c:3391

9 0x0000555555777c8a in bgfx::AllocatorStub::realloc (this=0x7fffdc001680, _ptr=0x7fffd7da0560, _size=0, _align=0,

_file=0x555555a83b03 "vkFree", _line=0) at ../../../src/bgfx.cpp:198

10 0x000055555557abe9 in bx::free (_allocator=0x7fffdc001680, _ptr=0x7fffd7da0560, _align=0, _location=...)

at ../../../../bx/include/bx/inline/allocator.inl:42

11 0x000055555557add8 in bx::alignedFree (_allocator=0x7fffdc001680, _ptr=0x7fffd7da0560, _align=16, _location=...)

at ../../../../bx/include/bx/inline/allocator.inl:67

12 0x0000555555777cc1 in bgfx::AllocatorStub::realloc (this=0x7fffdc001680, _ptr=0x7fffd7da0560, _size=0, _align=16,

_file=0x555555a83b03 "vkFree", _line=0) at ../../../src/bgfx.cpp:202

13 0x00005555557f0529 in bgfx::vk::freeFunction (_userData=0x7fffdc001680, _memory=0x7fffd7da0560)

at ../../../src/renderer_vk.cpp:578

14 0x00007fffd4b10f69 in ?? () from /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so

15 0x00007fffd4b182b3 in ?? () from /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so

16 0x00007fffd4b2333e in ?? () from /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so

17 0x00007fffd4b0f489 in ?? () from /usr/lib/x86_64-linux-gnu/libvulkan_lvp.so

18 0x00005555557f1881 in bgfx::vk::setImageMemoryBarrier (_commandBuffer=0x7fffd0615cb0, _image=..., _aspectMask=6,

_oldLayout=VK_IMAGE_LAYOUT_UNDEFINED, _newLayout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, _baseMipLevel=0, 
_levelCount=4294967295, _baseArrayLayer=0, _layerCount=4294967295) at ../../../src/renderer_vk.cpp:1091

19 0x00005555557fa034 in bgfx::vk::TextureVK::setImageMemoryBarrier (this=0x7fffd73789e0, _commandBuffer=0x7fffd0615cb0,

_newImageLayout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, _singleMsaaImage=false)
at ../../../src/renderer_vk.cpp:6396
bkaradzic commented 7 months ago

Grab latest.

goossens commented 7 months ago

That's it, you fixed it. Both the examples and my app now start just fine. Thanks for the quick help!!!

bkaradzic commented 7 months ago

What's your GPU/driver? I want to see to reproduce it locally to understand what's going on.

goossens commented 7 months ago

Well, this is going to shock you.

My main development machine is a 2018 Mac Mini (x86 with 16 GB of memory and embedded Intel UHD Graphics 630 1536 MB). Initially, I was using OpenGL to have some sort of cross platform compatibility. When I moved away from OpenGL, I did not fully want to commit to Metal and this is how I found your library. As I’m fully retired, I have no commercial ambitions. If I can get things running in Metal on MacOS, in Vulkan on Linux and in Direct3D on Windows, I’ll have all the brain exercises I could ever want to keep me mentally healthy.

So here is my typical workflow:

Obviously, the Virtual Machines aren’t speed devils specially given that they share a poor UHD Graphics 630 GPU. It does however work. My logic is that when I move to better (and dedicated) hardware, things can only improve.

To prove that I’m certifiably insane, I’m using CMake as my build environment. Once I got that figured out, it works well and the script for BGFX is only 50 lines (excluding BIMG and BX).

To answer the driver question, on Ubuntu 22.04, I get:

lshw -c video

*-display
description: VGA compatible controller product: SVGA II Adapter vendor: VMware physical id: f bus info: pci@0000:00:0f.0 logical name: /dev/fb0 version: 00 width: 32 bits clock: 33MHz capabilities: vga_controller bus_master cap_list rom fb configuration: depth=32 driver=vmwgfx latency=64 resolution=1280,800 resources: irq:16 ioport:2140(size=16) memory:f0000000-f7ffffff memory:fb800000-fbffffff memory:c0000-dffff

modinfo vmwgfx

filename: /lib/modules/6.5.0-26-generic/kernel/drivers/gpu/drm/vmwgfx/vmwgfx.ko version: 2.20.0.0 license: GPL and additional rights description: Standalone drm driver for the VMware SVGA device author: VMware Inc. and others import_ns: DMA_BUF srcversion: B41144FE71C1D39FBA6E54A