zig icon indicating copy to clipboard operation
zig copied to clipboard

Dynamic library symbol lookup fails on types with higher alignment

Open DiskPoppy opened this issue 2 years ago • 1 comments

Zig Version

0.10.0-dev.4418+99c3578f6

Steps to Reproduce

Use lookup() with a pointer to type that has larger alignment than 1. For example: lib.lookup(*usize, "foo"). Tested with macOS.

Expected Behavior

For it to compile.

Actual Behavior

/Users/me/zig/lib/zig/std/dynamic_library.zig:385:20: error: cast increases pointer alignment
            return @ptrCast(T, symbol);
                   ^~~~~~~~~~~~~~~~~~~
/Users/me/zig/lib/zig/std/dynamic_library.zig:385:32: note: '*anyopaque' has alignment '1'
            return @ptrCast(T, symbol);
                               ^~~~~~
/Users/me/zig/lib/zig/std/dynamic_library.zig:385:29: note: '*usize' has alignment '8'
            return @ptrCast(T, symbol);
                            ^

DiskPoppy avatar Oct 16 '22 21:10 DiskPoppy

The workaround is to have the pointer that is the argument to lookup be align(1) and then optionally @alignCast it.

DiskPoppy avatar Oct 17 '22 15:10 DiskPoppy