diff options
| author | Alex Rønne Petersen <alex@alexrp.com> | 2025-04-14 18:40:47 +0200 |
|---|---|---|
| committer | Matthew Lugg <mlugg@mlugg.co.uk> | 2025-04-27 23:54:54 +0100 |
| commit | 5ed8bd5c85e34d200a504418ebd2fc3d902460ac (patch) | |
| tree | 5315274c455873fadde58eb09c572324128e3dff | |
| parent | e7b46363aea6e42c206cb7b1bd8c9b47a83d9045 (diff) | |
| download | zig-5ed8bd5c85e34d200a504418ebd2fc3d902460ac.tar.gz zig-5ed8bd5c85e34d200a504418ebd2fc3d902460ac.zip | |
Sema: Fix some ptr alignment checks to handle a potential ISA tag bit.
Closes #23570.
| -rw-r--r-- | src/Sema.zig | 19 | ||||
| -rw-r--r-- | test/behavior/align.zig | 10 |
2 files changed, 26 insertions, 3 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 38b6a7f3bc..5c084cd4b6 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -22614,8 +22614,16 @@ fn ptrFromIntVal( const addr = try operand_val.toUnsignedIntSema(pt); if (!ptr_ty.isAllowzeroPtr(zcu) and addr == 0) return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{ptr_ty.fmt(pt)}); - if (addr != 0 and ptr_align != .none and !ptr_align.check(addr)) - return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{ptr_ty.fmt(pt)}); + if (addr != 0 and ptr_align != .none) { + const masked_addr = if (ptr_ty.childType(zcu).fnPtrMaskOrNull(zcu)) |mask| + addr & mask + else + addr; + + if (!ptr_align.check(masked_addr)) { + return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{ptr_ty.fmt(pt)}); + } + } return switch (ptr_ty.zigTypeTag(zcu)) { .optional => Value.fromInterned(try pt.intern(.{ .opt = .{ @@ -23131,7 +23139,12 @@ fn ptrCastFull( if (dest_align.compare(.gt, src_align)) { if (try ptr_val.getUnsignedIntSema(pt)) |addr| { - if (!dest_align.check(addr)) { + const masked_addr = if (Type.fromInterned(dest_info.child).fnPtrMaskOrNull(zcu)) |mask| + addr & mask + else + addr; + + if (!dest_align.check(masked_addr)) { return sema.fail(block, operand_src, "pointer address 0x{X} is not aligned to {d} bytes", .{ addr, dest_align.toByteUnits().?, diff --git a/test/behavior/align.zig b/test/behavior/align.zig index 4d5a36d877..6ed63ea8ca 100644 --- a/test/behavior/align.zig +++ b/test/behavior/align.zig @@ -596,3 +596,13 @@ test "function pointer @intFromPtr/@ptrFromInt roundtrip" { try std.testing.expectEqual(nothing_ptr, nothing_ptr2); } + +test "function pointer align mask" { + if (!(builtin.cpu.arch.isArm() or builtin.cpu.arch.isMIPS())) return error.SkipZigTest; + + const a: *const fn () callconv(.c) void = @ptrFromInt(0x20202021); + _ = &a; + + const b: *align(16) const fn () callconv(.c) void = @alignCast(a); + _ = &b; +} |
