New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig code segfaults where equivalent C code is fine #1218

Closed
andrewrk opened this Issue Jul 11, 2018 · 1 comment

Comments

Projects
None yet
1 participant
@andrewrk
Member

andrewrk commented Jul 11, 2018

compile with gcc -o test test.c -lvulkan, and it runs and exits 0.

typedef enum VkStructureType {
    APPLICATION_INFO = 0,
    INSTANCE_CREATE_INFO = 1,
} VkStructureType;

typedef unsigned int VkFlags;
typedef VkFlags VkInstanceCreateFlags;

typedef struct VkInstanceCreateInfo {
	VkStructureType sType;
	const void* pNext;
	VkInstanceCreateFlags flags;
	const void* pApplicationInfo;
	unsigned int enabledLayerCount;
	const void* ppEnabledLayerNames;
	unsigned int enabledExtensionCount;
	const char** ppEnabledExtensionNames;
} VkInstanceCreateInfo;

typedef enum VkResult {
    SUCCESS = 0,
} VkResult;

typedef struct VkInstance_T* VkInstance;

VkResult vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const void* pAllocator, VkInstance* pInstance);

//#include <stdio.h>

int main(int argc, char** argv) {

	const char* name = "VK_KHR_surface";
	
    VkInstanceCreateInfo info = {
        .sType = INSTANCE_CREATE_INFO,
        .pNext = 0,
        .flags = 0,
        .pApplicationInfo = 0,
        .enabledLayerCount = 0,
        .ppEnabledLayerNames = 0,
        .enabledExtensionCount = 1,
        .ppEnabledExtensionNames = &name,
    };

	VkInstance instance;
    VkResult re = vkCreateInstance(&info, 0, &instance);

}

compile with zig build-exe test.zig -lvulkan -lc and it segfaults at runtime:

const VkStructureType = extern enum {
    APPLICATION_INFO = 0,
    INSTANCE_CREATE_INFO = 1,
};

const VkFlags = u32;
const VkInstanceCreateFlags = VkFlags;

const VkInstanceCreateInfo = extern struct {
    sType: VkStructureType,
    pNext: ?*const void,
    flags: VkInstanceCreateFlags,
    pApplicationInfo: ?*const c_void,
    enabledLayerCount: u32,
    ppEnabledLayerNames: ?*const c_void,
    enabledExtensionCount: u32,
    ppEnabledExtensionNames: ?*const[*]const u8,
};

const VkResult = extern enum {
    SUCCESS = 0,
};

const struct_VkInstance_T = @OpaqueType();
const VkInstance = ?*struct_VkInstance_T;


extern fn vkCreateInstance(pCreateInfo: *const VkInstanceCreateInfo, pAllocator: ?*const c_void, pInstance: *VkInstance) VkResult;

pub fn main() void {
    const name = c"VK_KHR_surface";

    const info = VkInstanceCreateInfo {
        .sType = VkStructureType.INSTANCE_CREATE_INFO,
        .pNext = null,
        .flags = 0,
        .pApplicationInfo = null,
        .enabledLayerCount = 0,
        .ppEnabledLayerNames = null,
        .enabledExtensionCount = 1,
        .ppEnabledExtensionNames = &name,
    };


    var i: VkInstance = undefined;
    const re = vkCreateInstance(&info, null, &i);
}

@andrewrk andrewrk added the bug label Jul 11, 2018

@andrewrk andrewrk added this to the 0.3.0 milestone Jul 11, 2018

@andrewrk

This comment has been minimized.

Member

andrewrk commented Jul 11, 2018

The problem is this:

    pNext: ?*const void,

instead of

    pNext: ?*const c_void,

This should be a compile error to have that type in an extern struct.

@andrewrk andrewrk closed this in 3f30897 Jul 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment