ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.58k stars 2.45k forks source link

Missing stacktrace for Segmentation Fault inside OpenGL #16474

Open IntegratedQuantum opened 1 year ago

IntegratedQuantum commented 1 year ago

Zig Version

0.11.0-dev.4170+61d5b7c95 (after #15823)

Steps to Reproduce and Observed Behavior

This can be reproduced by producing a segmentation fault inside OpenGL (this reproducible only works on linux, and I could only test it with my amd drivers, so it may not be reproducible everywhere. If requested I can try to find a better reproducible that works on more platforms):

// zig run test.zig -lc -lglfw -lGL
const std = @import("std");

pub const c = @cImport ({
    @cInclude("GLFW/glfw3.h");
    @cDefine("GL_GLEXT_PROTOTYPES", {});
    @cInclude("GL/glx.h");
});

pub fn main() !void {
    if(c.glfwInit() == 0) {
        return error.GLFWFailed;
    }
    c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 4);
    c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 3);

    var window = c.glfwCreateWindow(10, 10, "Cubyz", null, null) orelse return error.GLFWFailed;
    c.glfwMakeContextCurrent(window);
    const glGenVertexArrays = @as(c.PFNGLGENVERTEXARRAYSPROC, @ptrCast(c.glXGetProcAddress("glGenVertexArrays"))).?;
    const glBindVertexArray = @as(c.PFNGLBINDVERTEXARRAYPROC, @ptrCast(c.glXGetProcAddress("glBindVertexArray"))).?;

    var vao: c_uint = undefined;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    c.glDrawElements(c.GL_TRIANGLES, 4, c.GL_UNSIGNED_INT, null);
}

Running this gives a Segmentation fault, with no meaningful stacktrace:

$ ~/zig/stage3/bin/zig run test.zig -lc -lglfw -lGL
Segmentation fault at address 0x0
???:?:?: 0x2c9b40 in ??? (???)
Aborted (core dumped)

Expected Behavior

gdb does manage to get the stacktrace so in principle it should be possible to unwind the stack here:

(gdb) bt
#0  0x00000000002c9b40 in memcpy ()
#1  0x00007ffff638c158 in ?? ()
   from /opt/amdgpu/lib/x86_64-linux-gnu/dri/radeonsi_dri.so
#2  0x00007ffff6386be5 in ?? ()
   from /opt/amdgpu/lib/x86_64-linux-gnu/dri/radeonsi_dri.so
#3  0x00007ffff5eba642 in ?? ()
   from /opt/amdgpu/lib/x86_64-linux-gnu/dri/radeonsi_dri.so
#4  0x0000000000212782 in test.main () at test.zig:71
#5  0x0000000000212e13 in start.callMain ()
    at /home/mint/zig/stage3/lib/zig/std/start.zig:608
#6  start.initEventLoopAndCallMain ()
    at /home/mint/zig/stage3/lib/zig/std/start.zig:542
#7  start.callMainWithArgs ()
    at /home/mint/zig/stage3/lib/zig/std/start.zig:492
#8  main (c_argc=1, c_argv=0x7fffffffddc8, c_envp=0x7fffffffddd8)
    at /home/mint/zig/stage3/lib/zig/std/start.zig:507
kcbanner commented 1 year ago

Thanks for the repro, I can reproduce this under WSL so I'll see what's going on here!