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

LLVM-backend generates COFF with invalid-length strtab entries when dynamically linking zig cc-built C-library (SDL) #15238

Open
rohlem opened this issue Apr 10, 2023 · 5 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior os-windows
Milestone

Comments

@rohlem
Copy link
Contributor

rohlem commented Apr 10, 2023

Zig Version

0.11.0-dev.2477+2ee328995

Steps to Reproduce and Observed Behavior

I've not really been able to reduce this to a minimal example, but it's consistently happening to me on a project using some larger libraries (most notably SDL, all built with zig cc, though that shouldn't matter).
However, if I @panic right in main, making calls to these libraries statically unreachable, it stops occurring, so it seems to be linked to overall code size/complexity.

When printing a stack trace, like during a panic, they result in out-of-bounds slicing, which leads to a panicked-during-panic abort.
With debug printing I see that the length read in std.coff.Coff.getStrtab, + the offset, would lead to reading up to f.e. 1903106884 bytes, when the length of self.data is only 16543744 bytes.

I've now also submitted PR #15239, which works around the issue.
It still needs to be investigated where these sections come from, and whether they're actually invalid or just in some other format we would also be interested in supporting.

Expected Behavior

A stack trace instead of panicking.
With #15239 just skipping invalid-length sections, the stack trace still works, so the actually relevant sections don't seem corrupted or anything afaict.

@rohlem rohlem added the bug Observed behavior contradicts documented or intended behavior label Apr 10, 2023
@rohlem
Copy link
Contributor Author

rohlem commented Apr 10, 2023

If it's deemed helpful in investigating this, I can upload the Zig-generated .pdb file that (as far as I understand) should hold these invalid-length sections (currently 1.6 MiB zip-ed).

@kubkon
Copy link
Member

kubkon commented Apr 11, 2023

If it's deemed helpful in investigating this, I can upload the Zig-generated .pdb file that (as far as I understand) should hold these invalid-length sections (currently 1.6 MiB zip-ed).

It would be more helpful if you could provide a step-by-step repro instructions. Is that a possibility?

@rohlem
Copy link
Contributor Author

rohlem commented Apr 11, 2023

@kubkon Thanks for the quick response!
The issue appears in a large local project with several build.zig scripts that invoke git, CMake, etc. - not quite portable yet due to using some hardcoded absolute paths.
I think it'll be quicker for now to un-hook the nested build scripts, pre-build the dependencies, and reduce the build.zig to only contain the final executable build.
I'll try getting that done and upload it to a GitHub repo by either this evening (12h from now) or the next (36h)!

@rohlem
Copy link
Contributor Author

rohlem commented Apr 11, 2023

@kubkon Okay, a tiny reproduction is now available at https://github.com/rohlem/Zig-invalid-coff-section-length-repro - simply clone it and zig build run.
Turns out it is particularly coupled to SDL (which is linked via DLL) - the issue happens exactly when there are statically-reachable calls to it that aren't optimized out.
The SDL build (contained in the repo) comes from CMake and ninja running zig cc (also version 0.11.0-dev.2477+2ee328995 like in the original post).

@rohlem rohlem changed the title LLVM-backend generates COFF with invalid-length strtab entries for big project LLVM-backend generates COFF with invalid-length strtab entries when dynamically linking zig cc-built C-library (SDL) Apr 11, 2023
@kubkon
Copy link
Member

kubkon commented Apr 12, 2023

Nice, thanks so much for this! I'll have a look shortly!

@kubkon kubkon added this to the 0.11.0 milestone Apr 12, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.11.1 Jul 20, 2023
@andrewrk andrewrk added the backend-llvm The LLVM backend outputs an LLVM IR Module. label Jul 27, 2023
@andrewrk andrewrk modified the milestones: 0.11.1, 0.12.0, 0.13.0 Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior os-windows
Projects
None yet
Development

No branches or pull requests

3 participants