fork test

This commit is contained in:
2025-12-15 15:59:04 +01:00
parent 0a282259e3
commit 85a07116af
3 changed files with 42 additions and 4 deletions

View File

@@ -356,6 +356,25 @@ test "echo" {
try testHelper(&.{ "echo", "Hello", "There" }, "Hello There\n");
}
test "nolibc_nopie_fork" {
try testHelper(
&.{ flicker_path, getTestExePath("nolibc_nopie_fork") },
"Child: I'm alive!\nParent: Child died.\n",
);
}
test "nolibc_pie_fork" {
try testHelper(
&.{ flicker_path, getTestExePath("nolibc_pie_fork") },
"Child: I'm alive!\nParent: Child died.\n",
);
}
test "libc_pie_fork" {
try testHelper(
&.{ flicker_path, getTestExePath("libc_pie_fork") },
"Child: I'm alive!\nParent: Child died.\n",
);
}
fn testPrintArgs(comptime name: []const u8) !void {
const exe_path = getTestExePath(name);
const loader_argv: []const []const u8 = &.{ flicker_path, exe_path, "foo", "bar", "baz hi" };

View File

@@ -63,10 +63,6 @@ export fn syscall_handler(regs: *UserRegs) callconv(.c) void {
std.debug.print("back in `syscall_handler`\n", .{});
return;
},
.fork, .vfork => {
// fork/vfork duplicate the stack (or share it until exec), so the return path via
// syscall_entry works fine.
},
.rt_sigreturn => {
@panic("sigreturn is not supported yet");
},

23
src/test/fork.zig Normal file
View File

@@ -0,0 +1,23 @@
const std = @import("std");
const linux = std.os.linux;
pub fn main() !void {
const ret = linux.syscall0(.fork);
const pid: i32 = @intCast(ret);
if (pid == 0) {
// --- Child ---
const msg = "Child: I'm alive!\n";
_ = linux.syscall3(.write, 1, @intFromPtr(msg.ptr), msg.len);
linux.exit(0);
} else if (pid > 0) {
// --- Parent ---
var status: u32 = 0;
_ = linux.syscall4(.wait4, @intCast(pid), @intFromPtr(&status), 0, 0);
const msg = "Parent: Child died.\n";
_ = linux.syscall3(.write, 1, @intFromPtr(msg.ptr), msg.len);
} else {
const msg = "Fork failed!\n";
_ = linux.syscall3(.write, 1, @intFromPtr(msg.ptr), msg.len);
}
}