ziglang / zig

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

std.DynLib corrupts struct passed by value #19841

Open Pyrolistical opened 5 months ago

Pyrolistical commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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

xdBronch commented 5 months ago

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