zig icon indicating copy to clipboard operation
zig copied to clipboard

std.DynLib corrupts struct passed by value

Open Pyrolistical opened this issue 1 year ago • 4 comments

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 avatar May 03 '24 01:05 Pyrolistical

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.

Pyrolistical avatar May 03 '24 01:05 Pyrolistical

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

xdBronch avatar May 03 '24 01:05 xdBronch

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

Pyrolistical avatar May 03 '24 01:05 Pyrolistical

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

xdBronch avatar May 03 '24 01:05 xdBronch