Seg fault using vulkan-zig with dynamic library as loader

I created an issue in vulkan-zig as well as I am not quite sure where to go for help, here.

Here is a copy of the issue description (with some links redacted due to limit):

Hello, I have been working through trying to set up a small application that will run a basic SPIR-V compute shader with the vulkan bindings here. Unfortunately, I am running into a segmentation fault when trying to invoke the vulkan functions.

The current program attempts to load the vulkan dynamic library and use this as the loader (copied from how machdoes it). It appears to be able to locate all the symbols fine and setup up BaseDispatch correctly. But when I try to invoke any of these functions, I run into the below segfault.

I run into the same issue if I first retrieve vkGetInstanceProcAddr and then attempt to use this as my loader.

I am sure I am just doing something silly, but not sure where to ask. If you want to close this and point me in a direction of where to ask I am equally happy. Thanks!

Additional context:

zig version
0.15.0-dev.286+ddcf6fcdf

Current program output with zig build run:

Loading with vkb: // invoking vk.BaseWrapper.load

vk.BaseWrapperWithCustomDispatch(vk.BaseDispatch){
  .dispatch = vk.BaseDispatch{
    .vkCreateInstance = fn (*const vk.InstanceCreateInfo, ?*const vk.AllocationCallbacks, *vk.Instance) callconv(.c) vk.Result@7f81fd012e00, 
    .vkGetInstanceProcAddr = fn (vk.Instance, [*:0]const u8) callconv(.c) ?*const fn () callconv(.c) void@7f81fd005660, 
    .vkEnumerateInstanceVersion = fn (*u32) callconv(.c) vk.Result@7f81fd010920,
    .vkEnumerateInstanceLayerProperties = fn (*u32, ?[*]vk.LayerProperties) callconv(.c) vk.Result@7f81fd010600, 
    .vkEnumerateInstanceExtensionProperties = fn (?[*:0]const u8, *u32, ?[*]vk.ExtensionProperties) callconv(.c) vk.Result@7f81fd0102d0
  }
}

// Then trying to invoke vkb.createInstance
Segmentation fault at address 0x0
???:?:?: 0x0 in ??? (???)
run
└─ run zbox failure

In order to dynamically load system shared libraries you need to make a dynamic executable linked against libc. You also shouldn’t link vulkan as a system library if you go the dynamic loading dlopen approach (I’m not entirely sure why the linker doesn’t complain about that line to be honest).

    // ...
    const exe_mod = b.createModule(.{
        // ...
++      .link_libc = true,
    });
    // ...
--  exe.linkSystemLibrary("vulkan");
    // ...
3 Likes

Awesome, that did the trick. Thanks!