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

std.DynLib corrupts struct passed by value #19841

Open
Pyrolistical opened this issue May 3, 2024 · 4 comments · May be fixed by #19842
Open

std.DynLib corrupts struct passed by value #19841

Pyrolistical opened this issue May 3, 2024 · 4 comments · May be fixed by #19842
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@Pyrolistical
Copy link
Contributor

Pyrolistical commented May 3, 2024

Zig Version

0.13.0-dev.46+3648d7df1

Steps to Reproduce and Observed Behavior

Env:
Windows 11
Version 10.0.22631 Build 22631

Repo: https://github.com/Pyrolistical/dynlib-struct-pass-by-value-repo
Run zig build run

Outputs:

main main.SomeStruct{ .x = 11 }
viaStructDynLib root.SomeStruct{ .x = 147 }
viaStruct root.SomeStruct{ .x = 11 }

Expected Behavior

Expected DynLib to be the same as extern:

main main.SomeStruct{ .x = 11 }
viaStructDynLib root.SomeStruct{ .x = 11 }
viaStruct root.SomeStruct{ .x = 11 }
@Pyrolistical Pyrolistical added the bug Observed behavior contradicts documented or intended behavior label May 3, 2024
@Pyrolistical
Copy link
Contributor Author

Passing the struct as pointer works, and replacing the struct with a u8 also works. So it has something to do with copy by value.

@xdBronch
Copy link
Contributor

xdBronch commented May 3, 2024

im getting some (unrelated) weird build errors from this but i believe the problem is this line
https://github.com/Pyrolistical/dynlib-struct-pass-by-value-repo/blob/ace0ec3aff2613a43b84b0b12e26c21cec2bbc07/src/main.zig#L18
you need to add callconv(.C). this should probably be checked in the DynLib functions

@Pyrolistical
Copy link
Contributor Author

Pyrolistical commented May 3, 2024

@xdBronch yep! that is issue. but can DynLib assert something for function pointers missing callconv? maybe assert callconv is not the default .Unspecified?

@xdBronch
Copy link
Contributor

xdBronch commented May 3, 2024

that might work, i wouldve asserted that its specifically the C callconv idk what happens with any of the others

Pyrolistical added a commit to Pyrolistical/zig that referenced this issue May 3, 2024
@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label May 3, 2024
@Vexu Vexu added this to the 0.13.0 milestone May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior standard library This issue involves writing Zig code for the standard library.
Projects
None yet
3 participants