Skip to content
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

glfw: Illegal instruction in pollEvents() #75

Closed
alichraghi opened this issue Nov 15, 2021 · 5 comments
Closed

glfw: Illegal instruction in pollEvents() #75

alichraghi opened this issue Nov 15, 2021 · 5 comments
Labels
bug Something isn't working glfw validating-fix
Milestone

Comments

@alichraghi
Copy link
Contributor

Code
const std = @import("std");
const glfw = @import("glfw");
const mem = std.mem;

fn printKey(window: glfw.Window, key: glfw.Key, scancode: isize, action: glfw.Action, mods: glfw.Mods) void  {
    _ = window;
    _ = scancode;
    _ = action;
    _ = mods;
    std.debug.print("{s}\n", .{key});
}

pub fn main() !void {
    try glfw.init();
    defer glfw.terminate();

    try glfw.Window.hint(glfw.Window.Hint.context_version_major, 4);
    try glfw.Window.hint(glfw.Window.Hint.context_version_minor, 5);
    const window = try glfw.Window.create(640, 480, "Hello, mach-glfw!", null, null);
    defer window.destroy();
    try glfw.makeContextCurrent(window);
    window.setKeyCallback(printKey);

    while (!window.shouldClose()) {
        try window.swapBuffers();
        try glfw.pollEvents();
    }
}

while the above code running, if you press a keyboard key that will crash with this messsage:

Illegal instruction at address 0x26832c
lib/glfw/upstream/glfw/src/x11_window.c:0:0: 0x26832c in _glfwPlatformPollEvents (/home/ali/dev/kizi/lib/glfw/upstream/glfw/src/x11_window.c)
lib/glfw/upstream/glfw/src/window.c:1072:5: 0x274e74 in glfwPollEvents (/home/ali/dev/kizi/lib/glfw/upstream/glfw/src/window.c)
    _glfwPlatformPollEvents();
    ^
/home/ali/dev/kizi/lib/glfw/src/main.zig:189:21: 0x22b4fd in main (kizi)
    c.glfwPollEvents();
@alichraghi
Copy link
Contributor Author

i think this is because currently mach can't build for wayland

@slimsag
Copy link
Member

slimsag commented Nov 15, 2021

Hi @alichraghi - thanks for the bug report! A few things:

  1. I believe that @silversquirl's patch in glfw: patch more undefined behaviour #72 likely fixes this, please try again and let me know. I do not think it is caused by any mismatch of Wayland vs. X11 support but rather is undefined behavior being caught in the GLFW source code by Zig / UBSan.

  2. You can build for Wayland if you like today by changing your build.zig:

-glfw.link(b, exe, .{});
+glfw.link(b, exe, .{.linux_window_manager = .Wayland});

It should "just work", but there may be unknown issues present. I wouldn't advise doing this however as GLFW's Wayland backend is still very immature, in the next version of GLFW there will be support for choosing between X11 and Wayland backends at runtime and so we will begin building for both by default. glfw/glfw#1958

  1. If you continue to run into such illegal instruction crashes, you can workaround them by building with -Drelease-fast temporarily as that will have Zig disable UBSan - until we track down the source of the undefined behavior and patch it in GLFW source code.

@slimsag slimsag added bug Something isn't working validating-fix labels Nov 15, 2021
@slimsag slimsag changed the title Illegal instruction in pollEvents() glfw: Illegal instruction in pollEvents() Nov 15, 2021
@slimsag slimsag added the glfw label Nov 15, 2021
@alichraghi
Copy link
Contributor Author

thank you! #72 worked for X11. but adding .linux_window_manager = .Wayland cause build error. i believe that's because:

  • typing mistake in build.zig
- .Wayland => "_D_GLFW_WAYLAND",
+ .Wayland => "-D_GLFW_WAYLAND",

.Wayland => "_D_GLFW_WAYLAND",

  • there's a missing header file in lib/glfw/upstream/glfw/src called wayland-xdg-shell-client-protocol.h.

@slimsag
Copy link
Member

slimsag commented Nov 16, 2021

Ahh, okay. I will see if I can fix this soon (unless someone wants to try and sends PRs for it!)

You're right about the typo.

For wayland-xdg-shell, we will need to add the right apt packages to the fecth.sh script here so our SDK has the headers/libs so we can cross compile. https://github.com/hexops/sdk-linux-x86_64

@slimsag
Copy link
Member

slimsag commented Nov 16, 2021

Filed #76 and #77 to track these two issues. I will close this one since the original illegal instruction issue is fixed.

@slimsag slimsag closed this as completed Nov 16, 2021
@slimsag slimsag added this to the Mach 0.2 milestone Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working glfw validating-fix
Projects
None yet
Development

No branches or pull requests

2 participants