Skip to content

Commit

Permalink
build with ReleaseFast unconditionally to workaround pointer alignmen…
Browse files Browse the repository at this point in the history
…t issue

If building with Debug, we will encounter illegal instruction exception due to
https://github.com/ziglang/zig/wiki/FAQ#why-do-i-get-illegal-instruction-when-using-with-zig-cc-to-build-c-code
So let's workaround this issue right now.

Close issue #2

Signed-off-by: Tw <[email protected]>
  • Loading branch information
tw4452852 committed May 31, 2024
1 parent c1b7203 commit 523f145
Show file tree
Hide file tree
Showing 17 changed files with 20 additions and 51 deletions.
5 changes: 3 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/hello.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions src/tests/array.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 2 additions & 4 deletions src/tests/exit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/tests/fentry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/iterator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/kmulprobe.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/kprobe.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/ksyscall.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/panic.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/perf_event.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/ringbuf.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/trace_printk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/tracepoint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/tests/xdp_ping.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/trace.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down

0 comments on commit 523f145

Please sign in to comment.