Skip to content

spirv: fix null file handle crash & improve flush err handling #24296

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nuIIpointerexception
Copy link

Add makeWritable() call before writeAll() in SPIR-V linker flush function.
SPIR-V linker was trying to write to uninitialized file handle.

Changes:

  • Add .spirv case to makeWritable() switch in link.zig
  • Call makeWritable() with proper error handling in SpirV.zig
  • OutOfMemory propagates up, other errors become LinkFailure

Testing:

  • SPIR-V shader compilation now works: zig build-obj shader.zig -target spirv64-vulkan
  • No more null pointer crashes during file writing
  • Proper error handling and file lifecycle management

Fixes #23883

Add makeWritable() call before writeAll() in SPIR-V linker flush function.
SPIR-V linker was trying to write to uninitialized file handle.

- Add .spirv case to makeWritable() switch in link.zig
- Call makeWritable() with proper error handling in SpirV.zig
- OutOfMemory propagates up, other errors become LinkFailure

Fixes ziglang#23883
@alexrp
Copy link
Member

alexrp commented Jun 29, 2025

cc @alichraghi

VoltEngine added 2 commits July 1, 2025 23:36
Add makeWritable() call before file operations to ensure file handle is initialized.
Implement ELF/COFF delegation pattern with flushInner() for consistent error handling
and architectural parity with other major linkers.

Prevents crash when SPIR-V linker attempts to write to uninitialized file.
@nuIIpointerexception
Copy link
Author

@alichraghi if you got time, mind looking at this?

@nuIIpointerexception nuIIpointerexception changed the title spirv: fix null file handle crash spirv: fix null file handle crash & improve flush err handling Jul 1, 2025
Comment on lines +192 to +196
return flushInner(self, arena, tid, sub_prog_node) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.LinkFailure => return error.LinkFailure,
else => |e| return diags.fail("SPIR-V flush failed: {s}", .{@errorName(e)}),
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just return LinkFailure for every other error:

     return flushInner(self, arena, tid, sub_prog_node) catch |err| switch (err) {
         error.OutOfMemory => return error.OutOfMemory,
-        error.LinkFailure => return error.LinkFailure,
-        else => |e| return diags.fail("SPIR-V flush failed: {s}", .{@errorName(e)}),
+        else => return error.LinkFailure,
     };

Comment on lines +260 to +261
try self.pwriteAll(std.mem.sliceAsBytes(linked_module), 0);
try self.setEndPos(linked_module.len * @sizeOf(Word));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't abstract them into mini functions. just use try:

- try self.pwriteAll(std.mem.sliceAsBytes(linked_module), 0);
- try self.setEndPos(linked_module.len * @sizeOf(Word));
+ try self.base.file.?.pwriteAll(std.mem.sliceAsBytes(linked_module), 0);
+ try self.base.file.?.setEndPos(linked_module.len * @sizeOf(Word));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error or crash when compiling vulkan spir-v shaders on windows
3 participants