Sema: add more type checks to @mem{cpy,set}

Closes #15634
Co-authored-by: Dima Afanasyev <dimaafanasev@example.com>
This commit is contained in:
Veikka Tuominen 2023-05-14 15:02:11 +03:00
parent ab5a72f6ca
commit 2703db3b40
2 changed files with 60 additions and 22 deletions

View File

@ -3416,19 +3416,12 @@ fn indexablePtrLenOrNone(
sema: *Sema,
block: *Block,
src: LazySrcLoc,
object: Air.Inst.Ref,
operand: Air.Inst.Ref,
) CompileError!Air.Inst.Ref {
const object_ty = sema.typeOf(object);
const indexable_ty = t: {
const ptr_size = object_ty.ptrSizeOrNull() orelse break :t object_ty;
break :t switch (ptr_size) {
.Many => return .none,
.One => object_ty.childType(),
else => object_ty,
};
};
try checkIndexable(sema, block, src, indexable_ty);
return sema.fieldVal(block, src, object, "len", src);
const operand_ty = sema.typeOf(operand);
try checkMemOperand(sema, block, src, operand_ty);
if (operand_ty.ptrSize() == .Many) return .none;
return sema.fieldVal(block, src, operand, "len", src);
}
fn zirAllocExtended(
@ -22080,19 +22073,25 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
const src_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
const dest_ptr = try sema.resolveInst(extra.lhs);
const src_ptr = try sema.resolveInst(extra.rhs);
const dest_ty = sema.typeOf(dest_ptr);
const src_ty = sema.typeOf(src_ptr);
const dest_len = try indexablePtrLenOrNone(sema, block, dest_src, dest_ptr);
const src_len = try indexablePtrLenOrNone(sema, block, src_src, src_ptr);
const target = sema.mod.getTarget();
if (dest_ty.isConstPtr()) {
return sema.fail(block, dest_src, "cannot memcpy to constant pointer", .{});
}
if (dest_len == .none and src_len == .none) {
const msg = msg: {
const msg = try sema.errMsg(block, src, "unknown @memcpy length", .{});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, dest_src, msg, "destination type '{}' provides no length", .{
sema.typeOf(dest_ptr).fmt(sema.mod),
dest_ty.fmt(sema.mod),
});
try sema.errNote(block, src_src, msg, "source type '{}' provides no length", .{
sema.typeOf(src_ptr).fmt(sema.mod),
src_ty.fmt(sema.mod),
});
break :msg msg;
};
@ -22180,9 +22179,6 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
} else break :rs src_src;
} else dest_src;
const dest_ty = sema.typeOf(dest_ptr);
const src_ty = sema.typeOf(src_ptr);
// If in-memory coercion is not allowed, explode this memcpy call into a
// for loop that copies element-wise.
// Likewise if this is an iterable rather than a pointer, do the same
@ -22274,7 +22270,11 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
const dest_ptr = try sema.resolveInst(extra.lhs);
const uncoerced_elem = try sema.resolveInst(extra.rhs);
const dest_ptr_ty = sema.typeOf(dest_ptr);
try checkIndexable(sema, block, dest_src, dest_ptr_ty);
try checkMemOperand(sema, block, dest_src, dest_ptr_ty);
if (dest_ptr_ty.isConstPtr()) {
return sema.fail(block, dest_src, "cannot memset constant pointer", .{});
}
const dest_elem_ty = dest_ptr_ty.elemType2();
const target = sema.mod.getTarget();
@ -31102,6 +31102,27 @@ fn checkIndexable(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void {
}
}
fn checkMemOperand(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void {
if (ty.zigTypeTag() == .Pointer) {
switch (ty.ptrSize()) {
.Slice, .Many, .C => return,
.One => {
const elem_ty = ty.childType();
if (elem_ty.zigTypeTag() == .Array) return;
// TODO https://github.com/ziglang/zig/issues/15479
// if (elem_ty.isTuple()) return;
},
}
}
const msg = msg: {
const msg = try sema.errMsg(block, src, "type '{}' is not an indexable pointer", .{ty.fmt(sema.mod)});
errdefer msg.destroy(sema.gpa);
try sema.errNote(block, src, msg, "operand must be a slice, a many pointer or a pointer to an array", .{});
break :msg msg;
};
return sema.failWithOwnedErrorMsg(msg);
}
fn resolveUnionLayout(sema: *Sema, ty: Type) CompileError!void {
const resolved_ty = try sema.resolveTypeFields(ty);
const union_obj = resolved_ty.cast(Type.Payload.Union).?.data;

View File

@ -19,6 +19,19 @@ pub export fn non_matching_lengths() void {
var buf2: [6]u8 = .{ 1, 2, 3, 4, 5, 6 };
@memcpy(&buf2, &buf1);
}
pub export fn memset_const_dest_ptr() void {
const buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
@memset(&buf, 1);
}
pub export fn memcpy_const_dest_ptr() void {
const buf1: [5]u8 = .{ 1, 2, 3, 4, 5 };
var buf2: [5]u8 = .{ 1, 2, 3, 4, 5 };
@memcpy(&buf1, &buf2);
}
pub export fn memset_array() void {
var buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
@memcpy(buf, 1);
}
// error
// backend=stage2
@ -27,10 +40,14 @@ pub export fn non_matching_lengths() void {
// :5:5: error: unknown @memcpy length
// :5:18: note: destination type '[*]u8' provides no length
// :5:24: note: source type '[*]align(4) const u8' provides no length
// :10:13: error: type 'u8' does not support indexing
// :10:13: note: operand must be an array, slice, tuple, or vector
// :15:13: error: type '*u8' does not support indexing
// :15:13: note: operand must be an array, slice, tuple, or vector
// :10:13: error: type '*u8' is not an indexable pointer
// :10:13: note: operand must be a slice, a many pointer or a pointer to an array
// :15:13: error: type '*u8' is not an indexable pointer
// :15:13: note: operand must be a slice, a many pointer or a pointer to an array
// :20:5: error: non-matching @memcpy lengths
// :20:13: note: length 6 here
// :20:20: note: length 5 here
// :24:13: error: cannot memset constant pointer
// :29:13: error: cannot memcpy to constant pointer
// :33:13: error: type '[5]u8' is not an indexable pointer
// :33:13: note: operand must be a slice, a many pointer or a pointer to an array