From 5e7126ab933835962924c5577fafba23426af81d Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Fri, 14 Feb 2025 21:03:20 -0600 Subject: [PATCH] take out the garbage --- build.zig | 2 ++ src/lib.zig | 30 ++++++++++++++++++++++++++ src/main.zig | 59 ++++++++++++++++------------------------------------ 3 files changed, 50 insertions(+), 41 deletions(-) create mode 100644 src/lib.zig diff --git a/build.zig b/build.zig index 245073c..063906e 100644 --- a/build.zig +++ b/build.zig @@ -14,6 +14,8 @@ pub fn build(b: *std.Build) void { .name = "bloat-buster", .root_module = exe_mod, }); + exe.linkLibC(); + exe.linkSystemLibrary("LLVM"); b.installArtifact(exe); diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000..da3d787 --- /dev/null +++ b/src/lib.zig @@ -0,0 +1,30 @@ +const builtin = @import("builtin"); +extern "c" fn _errno() *c_int; +extern "c" fn ptrace(c_int, c_int, usize, usize) c_long; +extern "c" fn IsDebuggerPresent() bool; +fn errno() Error { + return @enumFromInt(_errno().*); +} + +pub const Error = enum(c_int) { + SUCCESS = 0, + PERM = 1, +}; + +pub const os = struct { + pub extern "c" fn exit(c_int) noreturn; + pub fn is_being_debugged() bool { + var result = false; + switch (builtin.os) { + .linux => { + if (ptrace(0, 0, 0, 0) == -1) { + result = errno() == Error.PERM; + } + }, + .windows => IsDebuggerPresent(), + else => {}, + } + + return result; + } +}; diff --git a/src/main.zig b/src/main.zig index 225370d..00c3838 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,46 +1,23 @@ -//! By convention, main.zig is where your main function lives in the case that -//! you are building an executable. If you are making a library, the convention -//! is to delete this file and start with root.zig instead. - -pub fn main() !void { - // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); - - // stdout is for the actual output of your application, for example if you - // are implementing gzip, then only the compressed bytes should be sent to - // stdout, not any debugging messages. - const stdout_file = std.io.getStdOut().writer(); - var bw = std.io.bufferedWriter(stdout_file); - const stdout = bw.writer(); - - try stdout.print("Run `zig build test` to run the tests.\n", .{}); - - try bw.flush(); // Don't forget to flush! +const lib = @import("lib.zig"); +pub fn panic(message: []const u8, stack_trace: ?*anyopaque, return_address: ?usize) noreturn { + _ = return_address; + _ = message; + _ = stack_trace; + if (lib.os.is_being_debugged()) { + @trap(); + } else { + lib.os.exit(1); + } } -test "simple test" { - var list = std.ArrayList(i32).init(std.testing.allocator); - defer list.deinit(); // Try commenting this out and see if zig detects the memory leak! - try list.append(42); - try std.testing.expectEqual(@as(i32, 42), list.pop()); +pub fn main() callconv(.C) c_int { + return 0; } -test "use other module" { - try std.testing.expectEqual(@as(i32, 150), lib.add(100, 50)); +comptime { + if (!@import("builtin").is_test) { + @export(&main, @import("std").builtin.ExportOptions{ + .name = "main", + }); + } } - -test "fuzz example" { - const Context = struct { - fn testOne(context: @This(), input: []const u8) anyerror!void { - _ = context; - // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case! - try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input)); - } - }; - try std.testing.fuzz(Context{}, Context.testOne, .{}); -} - -const std = @import("std"); - -/// This imports the separate module containing `root.zig`. Take a look in `build.zig` for details. -const lib = @import("bloat-buster_lib");