authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-04-17 12:02:57+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-17 12:02:57+02:00
log1fb0b5a04429f06d75e22223765fa8723318489e
tree20cc14e5e2d44827497c0afbbe15d9344852ac16
parent72dcad6f020363237bdcd05b3f26f7b67fa9f162
parent956caf6eba6d5082f646dff501db0008fb523a1b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15308 from kcbanner/dynlib_lookup_alignment

DynLib.lookup: cast the pointer to the correct alignment

2 files changed, 3 insertions(+), 8 deletions(-)

lib/std/dynamic_library.zig+2-2
...@@ -348,7 +348,7 @@ pub const WindowsDynLib = struct {...@@ -348,7 +348,7 @@ pub const WindowsDynLib = struct {
348348
349 pub fn lookup(self: *WindowsDynLib, comptime T: type, name: [:0]const u8) ?T {349 pub fn lookup(self: *WindowsDynLib, comptime T: type, name: [:0]const u8) ?T {
350 if (windows.kernel32.GetProcAddress(self.dll, name.ptr)) |addr| {350 if (windows.kernel32.GetProcAddress(self.dll, name.ptr)) |addr| {
351 return @ptrCast(T, addr);351 return @ptrCast(T, @alignCast(@alignOf(@typeInfo(T).Pointer.child), addr));
352 } else {352 } else {
353 return null;353 return null;
354 }354 }
...@@ -382,7 +382,7 @@ pub const DlDynlib = struct {...@@ -382,7 +382,7 @@ pub const DlDynlib = struct {
382 // dlsym (and other dl-functions) secretly take shadow parameter - return address on stack382 // dlsym (and other dl-functions) secretly take shadow parameter - return address on stack
383 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826383 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66826
384 if (@call(.never_tail, system.dlsym, .{ self.handle, name.ptr })) |symbol| {384 if (@call(.never_tail, system.dlsym, .{ self.handle, name.ptr })) |symbol| {
385 return @ptrCast(T, symbol);385 return @ptrCast(T, @alignCast(@alignOf(@typeInfo(T).Pointer.child), symbol));
386 } else {386 } else {
387 return null;387 return null;
388 }388 }
test/standalone/load_dynamic_library/build.zig+1-6
...@@ -8,12 +8,7 @@ pub fn build(b: *std.Build) void {...@@ -8,12 +8,7 @@ pub fn build(b: *std.Build) void {
8 const optimize: std.builtin.OptimizeMode = .Debug;8 const optimize: std.builtin.OptimizeMode = .Debug;
9 const target: std.zig.CrossTarget = .{};9 const target: std.zig.CrossTarget = .{};
1010
11 const ok = (builtin.os.tag != .wasi and11 if (builtin.os.tag == .wasi) return;
12 // https://github.com/ziglang/zig/issues/13550
13 (builtin.os.tag != .macos or builtin.cpu.arch != .aarch64) and
14 // https://github.com/ziglang/zig/issues/13686
15 (builtin.os.tag != .windows or builtin.cpu.arch != .aarch64));
16 if (!ok) return;
1712
18 const lib = b.addSharedLibrary(.{13 const lib = b.addSharedLibrary(.{
19 .name = "add",14 .name = "add",