Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions guest/src/sandboxfs/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1122,16 +1122,30 @@ fn openRpcPort(path: []const u8) ?std.posix.fd_t {
const expected = std.fs.path.basename(path);
var attempts: usize = 0;
while (attempts < 50) : (attempts += 1) {
if (std.posix.open(path, .{ .ACCMODE = .RDWR, .CLOEXEC = true }, 0)) |fd| {
if (tryOpenRpcPath(path) catch null) |fd| {
return fd;
} else |_| {
if (openVirtioPortByName(expected)) |fd| return fd;
}
if (openVirtioPortByName(expected)) |fd| return fd;
std.posix.nanosleep(0, 100 * std.time.ns_per_ms);
}
return null;
}

fn tryOpenRpcPath(path: []const u8) !?std.posix.fd_t {
const fd = std.posix.open(path, .{ .ACCMODE = .RDWR, .NONBLOCK = true, .CLOEXEC = true }, 0) catch |err| switch (err) {
error.FileNotFound, error.NoDevice => return null,
else => return err,
};
errdefer std.posix.close(fd);

const original_flags = try std.posix.fcntl(fd, std.posix.F.GETFL, 0);
const nonblock_flag_u32: u32 = @bitCast(std.posix.O{ .NONBLOCK = true });
const nonblock_flag: usize = @intCast(nonblock_flag_u32);
_ = try std.posix.fcntl(fd, std.posix.F.SETFL, original_flags & ~nonblock_flag);

return fd;
}

fn openVirtioPortByName(expected: []const u8) ?std.posix.fd_t {
var dev_dir = std.fs.openDirAbsolute("/dev", .{ .iterate = true }) catch return null;
defer dev_dir.close();
Expand All @@ -1142,7 +1156,7 @@ fn openVirtioPortByName(expected: []const u8) ?std.posix.fd_t {
if (!std.mem.startsWith(u8, entry.name, "vport")) continue;
if (!virtioPortMatches(entry.name, expected)) continue;
const path = std.fmt.bufPrint(&path_buf, "/dev/{s}", .{entry.name}) catch continue;
return std.posix.open(path, .{ .ACCMODE = .RDWR, .CLOEXEC = true }, 0) catch continue;
return tryOpenRpcPath(path) catch continue;
}

return null;
Expand Down
1 change: 1 addition & 0 deletions host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"start": "node dist/bin/gondolin.js exec",
"dev": "node bin/gondolin.ts exec",
"test": "node --test test/*.test.ts",
"test:attach-reconnect": "node ./scripts/validate-vm-attach-reconnect.ts",
"test:backend-parity": "node --test test/backend-parity.test.ts",
"bash": "node bin/gondolin.ts bash",
"gondolin": "node bin/gondolin.ts",
Expand Down
195 changes: 195 additions & 0 deletions host/scripts/validate-vm-attach-reconnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import net from "node:net";
import path from "node:path";

import { VM } from "../src/vm/core.ts";
import { __test as serverOptionsTest } from "../src/sandbox/server-options.ts";
import { shouldSkipVmTests } from "../test/helpers/vm-fixture.ts";

function resolveHelperPath(): string {
return path.resolve(import.meta.dirname, "..", "test", "helpers", "vm-attach-helper.ts");
}

function waitForLine(child: ReturnType<typeof spawn>): Promise<string> {
return new Promise((resolve, reject) => {
let stdout = "";
let stderr = "";

const onStdout = (chunk: Buffer | string) => {
stdout += chunk.toString();
const newline = stdout.indexOf("\n");
if (newline === -1) return;

cleanup();
resolve(stdout.slice(0, newline));
};

const onStderr = (chunk: Buffer | string) => {
stderr += chunk.toString();
};

const onExit = (code: number | null, signal: NodeJS.Signals | null) => {
cleanup();
reject(
new Error(
`helper exited before announcing vm id (code=${String(code)} signal=${String(signal)} stderr=${stderr.trim()})`,
),
);
};

const cleanup = () => {
child.stdout?.off("data", onStdout);
child.stderr?.off("data", onStderr);
child.off("exit", onExit);
};

child.stdout?.on("data", onStdout);
child.stderr?.on("data", onStderr);
child.on("exit", onExit);
});
}

function waitForExit(child: ReturnType<typeof spawn>): Promise<void> {
if (child.exitCode !== null || child.signalCode !== null) {
return Promise.resolve();
}

return new Promise((resolve) => {
child.once("exit", () => resolve());
});
}

function disposeChildStreams(child: ReturnType<typeof spawn>): void {
try {
child.stdout?.destroy();
} catch {
// ignore
}
try {
child.stderr?.destroy();
} catch {
// ignore
}
}

function listenLocalServer(): Promise<{
close: () => Promise<void>;
port: number;
}> {
return new Promise((resolve, reject) => {
const server = net.createServer((socket) => {
socket.end(
"HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: 9\r\n" +
"Connection: close\r\n\r\n" +
"attach-ok",
);
});

server.once("error", reject);
server.listen(0, "127.0.0.1", () => {
const address = server.address();
if (!address || typeof address === "string") {
reject(new Error("failed to bind local test server"));
return;
}
resolve({
port: address.port,
close: async () =>
await new Promise<void>((done) => {
server.close(() => done());
}),
});
});
});
}

async function main(): Promise<void> {
if (shouldSkipVmTests()) {
throw new Error("hardware virtualization unavailable");
}

const qemuBinary = process.arch === "arm64" ? "qemu-system-aarch64" : "qemu-system-x86_64";
if (!(serverOptionsTest as any).qemuSupportsReconnect(qemuBinary)) {
throw new Error(`${qemuBinary} does not support reconnect-ms`);
}

const httpServer = await listenLocalServer();
let attachedVm: VM | null = null;
const helper = spawn(process.execPath, [resolveHelperPath(), String(httpServer.port)], {
cwd: path.resolve(import.meta.dirname, ".."),
stdio: ["ignore", "pipe", "pipe"],
});

try {
const line = await waitForLine(helper);
const announced = JSON.parse(line) as { id: string };
assert.ok(typeof announced.id === "string" && announced.id.length > 0);
console.log(`helper vm id: ${announced.id}`);

helper.kill("SIGKILL");
await waitForExit(helper);
disposeChildStreams(helper);

attachedVm = await VM.attach({
id: announced.id,
sandbox: {
console: "none",
dns: {
mode: "synthetic",
syntheticHostMapping: "per-host",
},
tcp: {
hosts: {
"local.test:8080": `127.0.0.1:${httpServer.port}`,
},
},
},
vfs: null,
});

await attachedVm.start();

const marker = await attachedVm.exec(["/bin/cat", "/tmp/reconnect-marker"]);
assert.equal(marker.exitCode, 0);
assert.equal(marker.stdout.trim(), "reconnect-ok");

const pidRead = await attachedVm.exec(["/bin/cat", "/tmp/reconnect-worker.pid"]);
assert.equal(pidRead.exitCode, 0);
const workerPid = pidRead.stdout.trim();
assert.match(workerPid, /^[0-9]+$/u);

const workerAlive = await attachedVm.exec([
"/bin/sh",
"-lc",
`kill -0 ${workerPid}`,
]);
assert.equal(workerAlive.exitCode, 0);

const networkProbe = await attachedVm.exec([
"/bin/sh",
"-lc",
"curl -fsS http://local.test:8080/ || wget -qO- http://local.test:8080/",
]);
assert.equal(networkProbe.exitCode, 0);
assert.equal(networkProbe.stdout.trim(), "attach-ok");

console.log("reconnect validation passed");
} finally {
if (attachedVm) {
await attachedVm.close().catch(() => undefined);
}
helper.kill("SIGKILL");
await waitForExit(helper);
disposeChildStreams(helper);
await httpServer.close();
}
}

main().catch((error) => {
const message = error instanceof Error ? error.stack ?? error.message : String(error);
console.error(message);
process.exit(1);
});
1 change: 1 addition & 0 deletions host/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export {
type VmFsWriteFileOptions,
type VmFsDeleteOptions,
} from "./vm/core.ts";
export { type VMAttachOptions } from "./vm/types.ts";
export { VmCheckpoint, type VmCheckpointData } from "./checkpoint.ts";
export { type ExecOptions, type ExecResult, type ExecProcess } from "./exec.ts";

Expand Down
Loading
Loading