Skip to content

Commit 4ba61a2

Browse files
authored
Merge pull request #15704 from Vexu/fix-memcpyset
`@mem{cpy,set}` fixes
2 parents b677b36 + 0bc5e7b commit 4ba61a2

File tree

3 files changed

+108
-24
lines changed

3 files changed

+108
-24
lines changed

src/Sema.zig

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3416,19 +3416,12 @@ fn indexablePtrLenOrNone(
34163416
sema: *Sema,
34173417
block: *Block,
34183418
src: LazySrcLoc,
3419-
object: Air.Inst.Ref,
3419+
operand: Air.Inst.Ref,
34203420
) CompileError!Air.Inst.Ref {
3421-
const object_ty = sema.typeOf(object);
3422-
const indexable_ty = t: {
3423-
const ptr_size = object_ty.ptrSizeOrNull() orelse break :t object_ty;
3424-
break :t switch (ptr_size) {
3425-
.Many => return .none,
3426-
.One => object_ty.childType(),
3427-
else => object_ty,
3428-
};
3429-
};
3430-
try checkIndexable(sema, block, src, indexable_ty);
3431-
return sema.fieldVal(block, src, object, "len", src);
3421+
const operand_ty = sema.typeOf(operand);
3422+
try checkMemOperand(sema, block, src, operand_ty);
3423+
if (operand_ty.ptrSize() == .Many) return .none;
3424+
return sema.fieldVal(block, src, operand, "len", src);
34323425
}
34333426

34343427
fn zirAllocExtended(
@@ -22080,19 +22073,25 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2208022073
const src_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
2208122074
const dest_ptr = try sema.resolveInst(extra.lhs);
2208222075
const src_ptr = try sema.resolveInst(extra.rhs);
22076+
const dest_ty = sema.typeOf(dest_ptr);
22077+
const src_ty = sema.typeOf(src_ptr);
2208322078
const dest_len = try indexablePtrLenOrNone(sema, block, dest_src, dest_ptr);
2208422079
const src_len = try indexablePtrLenOrNone(sema, block, src_src, src_ptr);
2208522080
const target = sema.mod.getTarget();
2208622081

22082+
if (dest_ty.isConstPtr()) {
22083+
return sema.fail(block, dest_src, "cannot memcpy to constant pointer", .{});
22084+
}
22085+
2208722086
if (dest_len == .none and src_len == .none) {
2208822087
const msg = msg: {
2208922088
const msg = try sema.errMsg(block, src, "unknown @memcpy length", .{});
2209022089
errdefer msg.destroy(sema.gpa);
2209122090
try sema.errNote(block, dest_src, msg, "destination type '{}' provides no length", .{
22092-
sema.typeOf(dest_ptr).fmt(sema.mod),
22091+
dest_ty.fmt(sema.mod),
2209322092
});
2209422093
try sema.errNote(block, src_src, msg, "source type '{}' provides no length", .{
22095-
sema.typeOf(src_ptr).fmt(sema.mod),
22094+
src_ty.fmt(sema.mod),
2209622095
});
2209722096
break :msg msg;
2209822097
};
@@ -22130,6 +22129,14 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2213022129
const ok = try block.addBinOp(.cmp_eq, dest_len, src_len);
2213122130
try sema.addSafetyCheck(block, ok, .memcpy_len_mismatch);
2213222131
}
22132+
} else if (dest_len != .none) {
22133+
if (try sema.resolveDefinedValue(block, dest_src, dest_len)) |dest_len_val| {
22134+
len_val = dest_len_val;
22135+
}
22136+
} else if (src_len != .none) {
22137+
if (try sema.resolveDefinedValue(block, src_src, src_len)) |src_len_val| {
22138+
len_val = src_len_val;
22139+
}
2213322140
}
2213422141

2213522142
const runtime_src = if (try sema.resolveDefinedValue(block, dest_src, dest_ptr)) |dest_ptr_val| rs: {
@@ -22139,7 +22146,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2213922146
const len = try sema.usizeCast(block, dest_src, len_u64);
2214022147
for (0..len) |i| {
2214122148
const elem_index = try sema.addIntUnsigned(Type.usize, i);
22142-
const dest_elem_ptr = try sema.elemPtr(
22149+
const dest_elem_ptr = try sema.elemPtrOneLayerOnly(
2214322150
block,
2214422151
src,
2214522152
dest_ptr,
@@ -22148,7 +22155,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2214822155
true, // init
2214922156
false, // oob_safety
2215022157
);
22151-
const src_elem_ptr = try sema.elemPtr(
22158+
const src_elem_ptr = try sema.elemPtrOneLayerOnly(
2215222159
block,
2215322160
src,
2215422161
src_ptr,
@@ -22172,9 +22179,6 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2217222179
} else break :rs src_src;
2217322180
} else dest_src;
2217422181

22175-
const dest_ty = sema.typeOf(dest_ptr);
22176-
const src_ty = sema.typeOf(src_ptr);
22177-
2217822182
// If in-memory coercion is not allowed, explode this memcpy call into a
2217922183
// for loop that copies element-wise.
2218022184
// Likewise if this is an iterable rather than a pointer, do the same
@@ -22213,6 +22217,10 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2221322217
if (new_src_ptr_ty.isSlice()) {
2221422218
new_src_ptr = try sema.analyzeSlicePtr(block, src_src, new_src_ptr, new_src_ptr_ty);
2221522219
}
22220+
} else if (dest_len == .none and len_val == null) {
22221+
// Change the dest to a slice, since its type must have the length.
22222+
const dest_ptr_ptr = try sema.analyzeRef(block, dest_src, new_dest_ptr);
22223+
new_dest_ptr = try sema.analyzeSlice(block, dest_src, dest_ptr_ptr, .zero, src_len, .none, .unneeded, dest_src, dest_src, dest_src, false);
2221622224
}
2221722225

2221822226
try sema.requireRuntimeBlock(block, src, runtime_src);
@@ -22262,7 +22270,11 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void
2226222270
const dest_ptr = try sema.resolveInst(extra.lhs);
2226322271
const uncoerced_elem = try sema.resolveInst(extra.rhs);
2226422272
const dest_ptr_ty = sema.typeOf(dest_ptr);
22265-
try checkIndexable(sema, block, dest_src, dest_ptr_ty);
22273+
try checkMemOperand(sema, block, dest_src, dest_ptr_ty);
22274+
22275+
if (dest_ptr_ty.isConstPtr()) {
22276+
return sema.fail(block, dest_src, "cannot memset constant pointer", .{});
22277+
}
2226622278

2226722279
const dest_elem_ty = dest_ptr_ty.elemType2();
2226822280
const target = sema.mod.getTarget();
@@ -31100,6 +31112,27 @@ fn checkIndexable(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void {
3110031112
}
3110131113
}
3110231114

31115+
fn checkMemOperand(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void {
31116+
if (ty.zigTypeTag() == .Pointer) {
31117+
switch (ty.ptrSize()) {
31118+
.Slice, .Many, .C => return,
31119+
.One => {
31120+
const elem_ty = ty.childType();
31121+
if (elem_ty.zigTypeTag() == .Array) return;
31122+
// TODO https://github.com/ziglang/zig/issues/15479
31123+
// if (elem_ty.isTuple()) return;
31124+
},
31125+
}
31126+
}
31127+
const msg = msg: {
31128+
const msg = try sema.errMsg(block, src, "type '{}' is not an indexable pointer", .{ty.fmt(sema.mod)});
31129+
errdefer msg.destroy(sema.gpa);
31130+
try sema.errNote(block, src, msg, "operand must be a slice, a many pointer or a pointer to an array", .{});
31131+
break :msg msg;
31132+
};
31133+
return sema.failWithOwnedErrorMsg(msg);
31134+
}
31135+
3110331136
fn resolveUnionLayout(sema: *Sema, ty: Type) CompileError!void {
3110431137
const resolved_ty = try sema.resolveTypeFields(ty);
3110531138
const union_obj = resolved_ty.cast(Type.Payload.Union).?.data;

test/behavior/memcpy.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,37 @@ fn testMemcpyBothSinglePtrArrayOneIsNullTerminated() !void {
4444
try expect(buf[98] == 'l');
4545
try expect(buf[99] == 'o');
4646
}
47+
48+
test "@memcpy dest many pointer" {
49+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
50+
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
51+
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
52+
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
53+
54+
try testMemcpyDestManyPtr();
55+
try comptime testMemcpyDestManyPtr();
56+
}
57+
58+
fn testMemcpyDestManyPtr() !void {
59+
var str = "hello".*;
60+
var buf: [5]u8 = undefined;
61+
@memcpy(@ptrCast([*]u8, &buf), @ptrCast([*]const u8, &str)[0..5]);
62+
try expect(buf[0] == 'h');
63+
try expect(buf[1] == 'e');
64+
try expect(buf[2] == 'l');
65+
try expect(buf[3] == 'l');
66+
try expect(buf[4] == 'o');
67+
}
68+
69+
comptime {
70+
const S = struct {
71+
buffer: [8]u8 = undefined,
72+
fn set(self: *@This(), items: []const u8) void {
73+
@memcpy(self.buffer[0..items.len], items);
74+
}
75+
};
76+
77+
var s = S{};
78+
s.set("hello");
79+
if (!std.mem.eql(u8, s.buffer[0..5], "hello")) @compileError("bad");
80+
}

test/cases/compile_errors/incorrect_type_to_memset_memcpy.zig

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ pub export fn non_matching_lengths() void {
1919
var buf2: [6]u8 = .{ 1, 2, 3, 4, 5, 6 };
2020
@memcpy(&buf2, &buf1);
2121
}
22+
pub export fn memset_const_dest_ptr() void {
23+
const buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
24+
@memset(&buf, 1);
25+
}
26+
pub export fn memcpy_const_dest_ptr() void {
27+
const buf1: [5]u8 = .{ 1, 2, 3, 4, 5 };
28+
var buf2: [5]u8 = .{ 1, 2, 3, 4, 5 };
29+
@memcpy(&buf1, &buf2);
30+
}
31+
pub export fn memset_array() void {
32+
var buf: [5]u8 = .{ 1, 2, 3, 4, 5 };
33+
@memcpy(buf, 1);
34+
}
2235

2336
// error
2437
// backend=stage2
@@ -27,10 +40,14 @@ pub export fn non_matching_lengths() void {
2740
// :5:5: error: unknown @memcpy length
2841
// :5:18: note: destination type '[*]u8' provides no length
2942
// :5:24: note: source type '[*]align(4) const u8' provides no length
30-
// :10:13: error: type 'u8' does not support indexing
31-
// :10:13: note: operand must be an array, slice, tuple, or vector
32-
// :15:13: error: type '*u8' does not support indexing
33-
// :15:13: note: operand must be an array, slice, tuple, or vector
43+
// :10:13: error: type '*u8' is not an indexable pointer
44+
// :10:13: note: operand must be a slice, a many pointer or a pointer to an array
45+
// :15:13: error: type '*u8' is not an indexable pointer
46+
// :15:13: note: operand must be a slice, a many pointer or a pointer to an array
3447
// :20:5: error: non-matching @memcpy lengths
3548
// :20:13: note: length 6 here
3649
// :20:20: note: length 5 here
50+
// :24:13: error: cannot memset constant pointer
51+
// :29:13: error: cannot memcpy to constant pointer
52+
// :33:13: error: type '[5]u8' is not an indexable pointer
53+
// :33:13: note: operand must be a slice, a many pointer or a pointer to an array

0 commit comments

Comments
 (0)