From 8c0e7babd34af403aec365e79813cf41799cb8c4 Mon Sep 17 00:00:00 2001 From: David Senoner Date: Tue, 6 May 2025 11:31:51 +0200 Subject: [PATCH 1/5] Introduce common `bzero` libc implementation. --- lib/c/strings.zig | 18 ++++++++++++++++++ lib/libc/musl/src/string/bzero.c | 8 -------- src/musl.zig | 1 - src/wasi_libc.zig | 1 - 4 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 lib/c/strings.zig delete mode 100644 lib/libc/musl/src/string/bzero.c diff --git a/lib/c/strings.zig b/lib/c/strings.zig new file mode 100644 index 000000000000..b225b6caf3c3 --- /dev/null +++ b/lib/c/strings.zig @@ -0,0 +1,18 @@ +const std = @import("std"); +const common = @import("common.zig"); + +comptime { + @export(&bzero, .{ .name = "bzero", .linkage = common.linkage, .visibility = common.visibility }); +} + +fn bzero(s: *anyopaque, n: usize) void { + _ = std.zig.c_builtins.__builtin_memset(s, 0, n); +} + +test "bzero" { + var array: [10]u8 = [_]u8{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; + var a = std.mem.zeroes([array.len]u8); + a[9] = '0'; + bzero(&array[0], 9); + try std.testing.expect(std.mem.eql(u8, &array, &a)); +} diff --git a/lib/libc/musl/src/string/bzero.c b/lib/libc/musl/src/string/bzero.c deleted file mode 100644 index ba536b07e939..000000000000 --- a/lib/libc/musl/src/string/bzero.c +++ /dev/null @@ -1,8 +0,0 @@ -#define _BSD_SOURCE -#include -#include - -void bzero(void *s, size_t n) -{ - memset(s, 0, n); -} diff --git a/src/musl.zig b/src/musl.zig index 71b01aee34af..6903544fb32d 100644 --- a/src/musl.zig +++ b/src/musl.zig @@ -1840,7 +1840,6 @@ const src_files = [_][]const u8{ "musl/src/string/arm/__aeabi_memset.s", "musl/src/string/bcmp.c", "musl/src/string/bcopy.c", - "musl/src/string/bzero.c", "musl/src/string/explicit_bzero.c", "musl/src/string/i386/memset.s", "musl/src/string/index.c", diff --git a/src/wasi_libc.zig b/src/wasi_libc.zig index bf6e13e55633..abd9972aebe4 100644 --- a/src/wasi_libc.zig +++ b/src/wasi_libc.zig @@ -1044,7 +1044,6 @@ const libc_top_half_src_files = [_][]const u8{ "musl/src/stdlib/qsort_nr.c", "musl/src/string/bcmp.c", "musl/src/string/bcopy.c", - "musl/src/string/bzero.c", "musl/src/string/explicit_bzero.c", "musl/src/string/index.c", "musl/src/string/memccpy.c", From 8ec86e45a68c9e0c9b71f351fb0dcdc25e62aba1 Mon Sep 17 00:00:00 2001 From: David <45010661+kada49@users.noreply.github.com> Date: Tue, 6 May 2025 12:10:05 +0200 Subject: [PATCH 2/5] Update test name according to review Co-authored-by: Linus Groh --- lib/c/strings.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/c/strings.zig b/lib/c/strings.zig index b225b6caf3c3..266b3c6e5b55 100644 --- a/lib/c/strings.zig +++ b/lib/c/strings.zig @@ -9,7 +9,7 @@ fn bzero(s: *anyopaque, n: usize) void { _ = std.zig.c_builtins.__builtin_memset(s, 0, n); } -test "bzero" { +test bzero { var array: [10]u8 = [_]u8{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; var a = std.mem.zeroes([array.len]u8); a[9] = '0'; From 6b35c1abbaa2c7ed2d18b5f32b1effca7091d20b Mon Sep 17 00:00:00 2001 From: David Senoner Date: Tue, 6 May 2025 20:01:05 +0200 Subject: [PATCH 3/5] address code review - import common implementation when musl or wasi is included - don't use `c_builtins`, use `@memset` --- lib/c.zig | 1 + lib/c/strings.zig | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/c.zig b/lib/c.zig index 156f47157382..39684264ce28 100644 --- a/lib/c.zig +++ b/lib/c.zig @@ -17,6 +17,7 @@ comptime { if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) { // Files specific to musl and wasi-libc. _ = @import("c/string.zig"); + _ = @import("c/strings.zig"); } if (builtin.target.isMuslLibC()) { diff --git a/lib/c/strings.zig b/lib/c/strings.zig index 266b3c6e5b55..d492be958f2a 100644 --- a/lib/c/strings.zig +++ b/lib/c/strings.zig @@ -6,7 +6,8 @@ comptime { } fn bzero(s: *anyopaque, n: usize) void { - _ = std.zig.c_builtins.__builtin_memset(s, 0, n); + const s_cast = @as([*c]u8, @ptrCast(s)); + @memset(s_cast[0..n], 0); } test bzero { From ba42d86d459fd1ac415fdf896d26db387bfaaa27 Mon Sep 17 00:00:00 2001 From: David Senoner Date: Tue, 6 May 2025 20:30:27 +0200 Subject: [PATCH 4/5] bzero calling conv to .c --- lib/c/strings.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/c/strings.zig b/lib/c/strings.zig index d492be958f2a..7a44f51d1a54 100644 --- a/lib/c/strings.zig +++ b/lib/c/strings.zig @@ -5,7 +5,7 @@ comptime { @export(&bzero, .{ .name = "bzero", .linkage = common.linkage, .visibility = common.visibility }); } -fn bzero(s: *anyopaque, n: usize) void { +fn bzero(s: *anyopaque, n: usize) callconv(.c) void { const s_cast = @as([*c]u8, @ptrCast(s)); @memset(s_cast[0..n], 0); } From 31291303234451cd4aaa5d0f80302741d01c8bd2 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 7 May 2025 18:17:38 +0200 Subject: [PATCH 5/5] Apply review Co-authored-by: Veikka Tuominen --- lib/c/strings.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/c/strings.zig b/lib/c/strings.zig index 7a44f51d1a54..30e945e7b110 100644 --- a/lib/c/strings.zig +++ b/lib/c/strings.zig @@ -6,7 +6,7 @@ comptime { } fn bzero(s: *anyopaque, n: usize) callconv(.c) void { - const s_cast = @as([*c]u8, @ptrCast(s)); + const s_cast: [*]u8 = @ptrCast(s); @memset(s_cast[0..n], 0); }