diff --git a/build.zig b/build.zig index 191351c..bc3ee1f 100644 --- a/build.zig +++ b/build.zig @@ -55,7 +55,7 @@ fn create_vmlinux(b: *std.Build) *std.Build.Module { const stdout = run_exe.captureStdOut(); const vmlinux_h = b.addInstallFile(stdout, "vmlinux.h"); const zigify = b.addTranslateC(.{ - .root_source_file = .{.cwd_relative = b.getInstallPath(vmlinux_h.dir, vmlinux_h.dest_rel_path)}, + .root_source_file = .{ .cwd_relative = b.getInstallPath(vmlinux_h.dir, vmlinux_h.dest_rel_path) }, .target = target, .optimize = optimize, }); @@ -83,7 +83,8 @@ const Ctx = struct { pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); + // WA for pointer alignment assumption of libbpf + const optimize: std.builtin.OptimizeMode = .ReleaseFast; const libbpf = create_libbpf(b, target, optimize); diff --git a/src/hello.zig b/src/hello.zig index 7ed19c1..c83eb8c 100644 --- a/src/hello.zig +++ b/src/hello.zig @@ -10,9 +10,7 @@ pub fn main() !void { const allocator = arena.allocator(); - const obj_bytes = @embedFile("@bpf_prog"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@bpf_prog"); const obj = libbpf.bpf_object__open_mem(bytes.ptr, bytes.len, null); if (obj == null) { diff --git a/src/tests/array.zig b/src/tests/array.zig index a87e78a..0239c9b 100644 --- a/src/tests/array.zig +++ b/src/tests/array.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "array" { - const obj_bytes = @embedFile("@array"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@array"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/exit.zig b/src/tests/exit.zig index 8dfa1bc..e11ffa6 100644 --- a/src/tests/exit.zig +++ b/src/tests/exit.zig @@ -6,13 +6,11 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "exit" { - const obj_bytes = @embedFile("@exit"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@exit"); _ = libbpf.libbpf_set_print(root.dbg_printf); - const obj = libbpf.bpf_object__open_mem(bytes.ptr, bytes.len, null); + const obj = libbpf.bpf_object__open_mem(bytes, bytes.len, null); if (obj == null) { print("failed to open bpf object: {}\n", .{std.posix.errno(-1)}); return error.OPEN; diff --git a/src/tests/fentry.zig b/src/tests/fentry.zig index 9f87b0d..04506d2 100644 --- a/src/tests/fentry.zig +++ b/src/tests/fentry.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "fentry" { - const obj_bytes = @embedFile("@fentry"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@fentry"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/hash.zig b/src/tests/hash.zig index 8366e14..243e20b 100644 --- a/src/tests/hash.zig +++ b/src/tests/hash.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "hash" { - const obj_bytes = @embedFile("@hash"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@hash"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/iterator.zig b/src/tests/iterator.zig index ef3b459..1ac6747 100644 --- a/src/tests/iterator.zig +++ b/src/tests/iterator.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "iterator" { - const obj_bytes = @embedFile("@iterator"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@iterator"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/kmulprobe.zig b/src/tests/kmulprobe.zig index cc25450..941f684 100644 --- a/src/tests/kmulprobe.zig +++ b/src/tests/kmulprobe.zig @@ -9,9 +9,7 @@ const REGS = @import("bpf").Args.REGS; test "kmulprobe" { if (!root.btf_name_exist("bpf_kprobe_multi_link")) return error.SkipZigTest; - const obj_bytes = @embedFile("@kmulprobe"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@kmulprobe"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/kprobe.zig b/src/tests/kprobe.zig index 8b920fc..8024393 100644 --- a/src/tests/kprobe.zig +++ b/src/tests/kprobe.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "kprobe" { - const obj_bytes = @embedFile("@kprobe"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@kprobe"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/ksyscall.zig b/src/tests/ksyscall.zig index e3e267c..8da2a0a 100644 --- a/src/tests/ksyscall.zig +++ b/src/tests/ksyscall.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "ksyscall" { - const obj_bytes = @embedFile("@ksyscall"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@ksyscall"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/panic.zig b/src/tests/panic.zig index 78df939..3230e53 100644 --- a/src/tests/panic.zig +++ b/src/tests/panic.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "panic" { - const obj_bytes = @embedFile("@panic"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@panic"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/perf_event.zig b/src/tests/perf_event.zig index 3c87b4b..9566a73 100644 --- a/src/tests/perf_event.zig +++ b/src/tests/perf_event.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "perf_event" { - const obj_bytes = @embedFile("@perf_event"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@perf_event"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/ringbuf.zig b/src/tests/ringbuf.zig index e4969fc..11540bd 100644 --- a/src/tests/ringbuf.zig +++ b/src/tests/ringbuf.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "ringbuf" { - const obj_bytes = @embedFile("@ringbuf"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@ringbuf"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/trace_printk.zig b/src/tests/trace_printk.zig index b075b79..fa7a166 100644 --- a/src/tests/trace_printk.zig +++ b/src/tests/trace_printk.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "trace_printk" { - const obj_bytes = @embedFile("@trace_printk"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@trace_printk"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/tracepoint.zig b/src/tests/tracepoint.zig index 4d18f54..c00b1f7 100644 --- a/src/tests/tracepoint.zig +++ b/src/tests/tracepoint.zig @@ -6,9 +6,7 @@ const allocator = root.allocator; const libbpf = root.libbpf; test "tracepoint" { - const obj_bytes = @embedFile("@tracepoint"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@tracepoint"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/tests/xdp_ping.zig b/src/tests/xdp_ping.zig index 1f2316d..a645c64 100644 --- a/src/tests/xdp_ping.zig +++ b/src/tests/xdp_ping.zig @@ -10,9 +10,7 @@ const c = @cImport({ }); test "xdp_ping" { - const obj_bytes = @embedFile("@xdp_ping"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@xdp_ping"); _ = libbpf.libbpf_set_print(root.dbg_printf); diff --git a/src/trace.zig b/src/trace.zig index 8db5ff4..e4b6184 100644 --- a/src/trace.zig +++ b/src/trace.zig @@ -39,9 +39,7 @@ pub fn main() !void { defer process.argsFree(allocator, args); var arg_idx: usize = 1; // skip exe name - const obj_bytes = @embedFile("@bpf_prog"); - const bytes = try allocator.dupe(u8, obj_bytes); - defer allocator.free(bytes); + const bytes = @embedFile("@bpf_prog"); var seconds: ?usize = null; while (nextArg(args, &arg_idx)) |arg| {