mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
A VM's proxy runs as two processes, as design.md's "The proxy's broker"
says: devvm _proxy, the broker, which keeps what only the host should
do, and devvm _proxy-child, which reads the VM's network and can be
confined by the follow-ups.
- wire.go: the protocol over one socketpair (close-on-exec from the
start: SOCK_CLOEXEC on Linux, ForkLock elsewhere), length-prefixed JSON
frames of at most 2 MiB, unknown fields refused, bytes as base64, one
descriptor on a frame's first bytes (SCM_RIGHTS, MSG_CTRUNC breaks the
rules), wire types for every secret (redact.Secret at once).
- broker.go: newBroker, the start (handshake: version 1 and each side's
executable's SHA-256, within 4 s), the answers (value through lookups,
kept 30 s or 5 min, a failure 5 s, dropped when the bindings change;
file by number; link connected by the broker and passed as a
descriptor; token exchanged by the broker), each by something in its
own config, with caps 1/8/4/256; live: the lifeline closes the socket
and kills the child 6 s later, SIGTERM stops it within a second, a
child that ends ends the broker, a rule broken stops the child.
- child.go: the start (startOf), the child's server (newChildServer),
the broker as hostSide over the socket (brokerClient), runChildOn;
ready only after it reads the host's addresses (kept: the last good
read), roots and time zone loaded first.
- process.go: Main(ctx, ...) and ChildMain; the child spawned with
/proc/self/exe on Linux, PR_SET_PDEATHSIG from a locked thread
(daemon.Cmd.DieWithThread), descriptors 3 and 4, and TZ, SSL_CERT_*
and GODEBUG with updatemaxprocs=0 alone; run/proxy-child.pid and
run/proxy.confined ("none" until the follow-ups); KeepLogs in the
broker. ChildMain refuses to start unless descriptors 3 and 4 are a
unix socket and an IP one (checkInherited): run by hand, without
them, those numbers can be the Go runtime's own, which closing
crashed it (signal_recv: inconsistent state).
- capture.go: a captured token request goes to the broker
(captureToken, exchanger), so a login's real tokens never enter the
child; an error reply passes up to 1 MiB, longer gets 502.
- watched.go: the child stats each changing file and asks the broker
for it once it changed, keeping the stamp of what the broker read.
- rotate.go: proxy.log rotated by both through its descriptor, under a
lock of a description of its own; OpenLog opens a log never through a
link, one link, yours, replacing what a child left in its place.
- nixcache.go: the trim and its count through a safefile.Root, nar/
only as a folder, links deleted as themselves; reads never through a
link; the lock tried for 5 s (ErrNixLocked, nix_cache_locked).
- daemon: Cmd.Err, Files, Env, DieWithThread; Handle.Kill, Exit;
WritePID.
- netpolicy: Proxy.Stop also stops a child that outlived its broker
(run/proxy-child.pid, _proxy-child -for); proxy.log opened with
OpenLog; a refused launch keeps a running VM only while both run.
devvm network log and lock read the event log through safefile.
- vm, vmssh, vmlog: links.log and ports.log opened with OpenLog, read
through safefile, trimmed only as a regular file with one link.
- backend: the build proxy's logs in a logs/ folder of their own.
- cli: _proxy-child; _proxy waits for its child on a signal
(StopsItsOwn); the child reads none of devvm's folders (no
makePrivate); ui.Files.
- Tests: FuzzBrokerFrames, the frames' rules, the handshake and the
pair's end in synctest bubbles, the caps, the broker naming its own
alone, the client over a real socketpair with a passed descriptor,
ChildMain, TestInheritedSocket and proxy.txtar's child run by hand,
the frozen 0.1.0 configs starting a broker and child,
two writers rotating once, planted links, FIFOs and hard links; the
testscript runner stops _proxy-child too; e2e: broker and child, their
arguments, descriptors and environment, stop, SIGKILL either, planted
files in the logs folder and the Nix cache, build proxies gone.
- Review fixes: the lifeline closes the socket and starts the kill's
timer before logging; every log lock (proxy.log's, the event log's
rotation) is tried without blocking for 200 ms (flockWithin, which the
Nix lock shares), then written without or the rotation skipped; a
copied log's rotation streams at most twice its limit from a line's
start (copyFrom, copyAtomic); a reply too long to send goes as
too_long; kept token headers capped at 1 KiB; the child gives up on a
request whose context ended, dropping its late reply and holding its
slot until then; a write deadline on the start frame; errors reach the
child as codes (not_found, refused, busy, too_long, failed), their
text in the broker's log; FuzzFrameStreams, and FuzzBrokerFrames
writes from a goroutine; nix_cache_locked links to How it works.
- Docs: processes, network, overview, design (with threat rows for the
logs' locks and a log made huge), troubleshooting, the proxy's
README, structure, testing; CHANGELOG.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
383 lines
12 KiB
Go
383 lines
12 KiB
Go
package proxy
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func readLog(t *testing.T, path string) string {
|
|
t.Helper()
|
|
b, err := os.ReadFile(path) //nolint:gosec // the test's own file
|
|
if err != nil && !os.IsNotExist(err) {
|
|
t.Fatal(err)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func modeOf(t *testing.T, path string) os.FileMode {
|
|
t.Helper()
|
|
fi, err := os.Stat(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return fi.Mode().Perm()
|
|
}
|
|
|
|
// The event log: past the limit it's renamed to <log>.1, replacing the
|
|
// one there, and a new one starts; no line spans two files, and every
|
|
// file is the user's alone, whatever mode the log had.
|
|
func TestRotateRenames(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "vm.jsonl")
|
|
if err := os.WriteFile(path, []byte("old 0123456789\n"), 0o644); err != nil { //nolint:gosec // a log left open to others
|
|
t.Fatal(err)
|
|
}
|
|
r, err := openRotating(path, 30)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer r.Close()
|
|
if m := modeOf(t, path); m != 0o600 {
|
|
t.Errorf("opened: mode %v", m)
|
|
}
|
|
write := func(s string) {
|
|
t.Helper()
|
|
if n, err := r.Write([]byte(s)); err != nil || n != len(s) {
|
|
t.Fatalf("Write(%q) = %d, %v", s, n, err)
|
|
}
|
|
}
|
|
write("one 0123456789\n") // 30 bytes: at the limit, not past it
|
|
if got := readLog(t, path); got != "old 0123456789\none 0123456789\n" || readLog(t, path+".1") != "" {
|
|
t.Fatalf("at the limit: %q", got)
|
|
}
|
|
write("two\n")
|
|
if a, b := readLog(t, path+".1"), readLog(t, path); a != "old 0123456789\none 0123456789\n" || b != "two\n" {
|
|
t.Errorf("rotated: .1 %q, log %q", a, b)
|
|
}
|
|
write("three 0123456789 01234\n")
|
|
write("four\n")
|
|
if a, b := readLog(t, path+".1"), readLog(t, path); a != "two\nthree 0123456789 01234\n" || b != "four\n" {
|
|
t.Errorf("rotated again, the oldest dropped: .1 %q, log %q", a, b)
|
|
}
|
|
for _, p := range []string{path, path + ".1"} {
|
|
if m := modeOf(t, p); m != 0o600 {
|
|
t.Errorf("%s: mode %v", filepath.Base(p), m)
|
|
}
|
|
}
|
|
// A line longer than the limit goes whole into a new log.
|
|
long := strings.Repeat("x", 40) + "\n"
|
|
write(long)
|
|
if a, b := readLog(t, path+".1"), readLog(t, path); a != "four\n" || b != long {
|
|
t.Errorf("a long line: .1 %q, log %q", a, b)
|
|
}
|
|
// A log deleted while it's written starts again at its next line.
|
|
if err := os.Remove(path); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("five\n")
|
|
if got := readLog(t, path); got != "five\n" {
|
|
t.Errorf("deleted: log %q", got)
|
|
}
|
|
}
|
|
|
|
// The event log's other writer rotated it: the next line goes into the
|
|
// new log, whose size counts from its own, and the next rotation keeps
|
|
// the renamed one's lines.
|
|
func TestRotateFollowsTheOtherWriter(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "vm.jsonl")
|
|
r, err := openRotating(path, 30)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer r.Close()
|
|
write := func(s string) {
|
|
t.Helper()
|
|
if _, err := r.Write([]byte(s)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write("proxy 0123456789 01234\n") // 23 bytes
|
|
if err := os.Rename(path, path+".1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte("link\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("proxy two\n") // 15 bytes with the other writer's line
|
|
if a, b := readLog(t, path+".1"), readLog(t, path); a != "proxy 0123456789 01234\n" || b != "link\nproxy two\n" {
|
|
t.Errorf("rotated by the other writer: .1 %q, log %q", a, b)
|
|
}
|
|
// The other writer's line counts toward the limit too.
|
|
if err := os.WriteFile(path, []byte("link 0123456789 0123456789\n"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("three\n")
|
|
if a, b := readLog(t, path+".1"), readLog(t, path); a != "link 0123456789 0123456789\n" || b != "three\n" {
|
|
t.Errorf("rotated past the other writer's line: .1 %q, log %q", a, b)
|
|
}
|
|
}
|
|
|
|
// Writes from many connections at once during a rotation: each line
|
|
// whole, once, in one of the two files.
|
|
func TestRotateConcurrent(t *testing.T) {
|
|
t.Parallel()
|
|
const writers, each = 16, 50
|
|
line := func(w, i int) string { return fmt.Sprintf(`{"writer":%02d,"line":%03d,"pad":"0123456789"}`+"\n", w, i) }
|
|
total := int64(writers * each * len(line(0, 0)))
|
|
// One rotation: the first file takes most of the lines.
|
|
path := filepath.Join(t.TempDir(), "vm.jsonl")
|
|
r, err := openRotating(path, total*3/4)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var wg sync.WaitGroup
|
|
for w := range writers {
|
|
wg.Go(func() {
|
|
for i := range each {
|
|
if _, err := r.Write([]byte(line(w, i))); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
wg.Wait()
|
|
if err := r.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old, cur := readLog(t, path+".1"), readLog(t, path)
|
|
if old == "" || cur == "" {
|
|
t.Fatalf("no rotation: .1 %d bytes, log %d bytes", len(old), len(cur))
|
|
}
|
|
seen := map[string]int{}
|
|
for _, l := range strings.SplitAfter(old+cur, "\n") {
|
|
if l != "" {
|
|
seen[l]++
|
|
}
|
|
}
|
|
for w := range writers {
|
|
for i := range each {
|
|
if n := seen[line(w, i)]; n != 1 {
|
|
t.Errorf("%q: %d times", line(w, i), n)
|
|
}
|
|
delete(seen, line(w, i))
|
|
}
|
|
}
|
|
if len(seen) > 0 {
|
|
t.Errorf("lines split or not written: %v", seen)
|
|
}
|
|
}
|
|
|
|
// The proxy's own log is also its stderr, which devvm opened and which
|
|
// its last error goes to: it's copied to <log>.1 and emptied, so writes
|
|
// that bypass the rotation still reach the current log.
|
|
func TestRotateCopies(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "vm.proxy.log")
|
|
if err := os.WriteFile(path, []byte("started 0123\n"), 0o644); err != nil { //nolint:gosec // a log left open to others
|
|
t.Fatal(err)
|
|
}
|
|
stderr, err := os.OpenFile(path, os.O_APPEND|os.O_RDWR, 0o600) //nolint:gosec // the test's own file
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer stderr.Close()
|
|
r, err := copyRotating(stderr, path, stderr, 20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := r.Write([]byte("loaded\n")); err != nil { // 20 bytes: at the limit
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := r.Write([]byte("failed upstream\n")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := stderr.WriteString("devvm: the last error\n"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a, b := readLog(t, path+".1"), readLog(t, path); a != "started 0123\nloaded\n" || b != "failed upstream\ndevvm: the last error\n" {
|
|
t.Errorf(".1 %q, log %q", a, b)
|
|
}
|
|
for _, p := range []string{path, path + ".1"} {
|
|
if m := modeOf(t, p); m != 0o600 {
|
|
t.Errorf("%s: mode %v", filepath.Base(p), m)
|
|
}
|
|
}
|
|
// What reached the log as stderr counts toward its limit: the next
|
|
// line, which its own lines alone would leave under it, rotates it.
|
|
if _, err := r.Write([]byte("x\n")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a, b := readLog(t, path+".1"), readLog(t, path); a != "failed upstream\ndevvm: the last error\n" || b != "x\n" {
|
|
t.Errorf("after stderr: .1 %q, log %q", a, b)
|
|
}
|
|
}
|
|
|
|
// The broker and its child both write the proxy's log, through the descriptor devvm gave them:
|
|
// past the limit, they rotate it once between them, each line whole in one of the two files.
|
|
func TestRotateCopiesWithTwoWriters(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "proxy.log")
|
|
fd, err := OpenLog(path)
|
|
must(t, err)
|
|
defer fd.Close()
|
|
broker, err := copyRotating(fd, path, fd, 200)
|
|
must(t, err)
|
|
child, err := copyRotating(fd, path, fd, 200)
|
|
must(t, err)
|
|
defer broker.Close()
|
|
defer child.Close()
|
|
var wg sync.WaitGroup
|
|
for i, w := range []*rotating{broker, child} {
|
|
wg.Go(func() {
|
|
for j := range 10 {
|
|
_, _ = fmt.Fprintf(w, "writer %d line %02d\n", i, j)
|
|
}
|
|
})
|
|
}
|
|
wg.Wait()
|
|
lines := strings.Count(readLog(t, path+".1"), "\n") + strings.Count(readLog(t, path), "\n")
|
|
if lines != 20 || len(readLog(t, path)) > 200 {
|
|
t.Errorf("%d lines kept, the log %d bytes", lines, len(readLog(t, path)))
|
|
}
|
|
}
|
|
|
|
// A child that a VM took over can hold the log's lock for ever, from a description of its own: a
|
|
// line still goes in, within logLockWait or so, and the rotation is skipped rather than waited
|
|
// for; and the event log's rotation is skipped the same way.
|
|
func TestRotateLockHeld(t *testing.T) {
|
|
t.Parallel()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "proxy.log")
|
|
fd, err := OpenLog(path)
|
|
must(t, err)
|
|
defer fd.Close()
|
|
_, err = fd.WriteString(strings.Repeat("x", 30) + "\n")
|
|
must(t, err)
|
|
r, err := copyRotating(fd, path, fd, 20)
|
|
must(t, err)
|
|
defer r.Close()
|
|
child, err := os.Open(path) //nolint:gosec // the test's own file
|
|
must(t, err)
|
|
defer child.Close()
|
|
must(t, syscall.Flock(int(child.Fd()), syscall.LOCK_EX))
|
|
start := time.Now()
|
|
_, err = r.Write([]byte("a line\n"))
|
|
if took := time.Since(start); err != nil || took > 5*logLockWait {
|
|
t.Errorf("a line with the lock held: %v after %v", err, took)
|
|
}
|
|
if got := readLog(t, path); !strings.HasSuffix(got, "a line\n") || !strings.HasPrefix(got, "xxx") {
|
|
t.Errorf("the log: %q (the line in it, no rotation)", got)
|
|
}
|
|
if _, err := os.Stat(path + ".1"); !os.IsNotExist(err) {
|
|
t.Errorf("rotated under a held lock: %v", err)
|
|
}
|
|
// The event log.
|
|
events := filepath.Join(dir, "network.jsonl")
|
|
ev, err := openRotating(events, 20)
|
|
must(t, err)
|
|
defer ev.Close()
|
|
_, err = ev.Write([]byte(strings.Repeat("y", 30) + "\n"))
|
|
must(t, err)
|
|
held, err := os.Open(events) //nolint:gosec // the test's own file
|
|
must(t, err)
|
|
defer held.Close()
|
|
must(t, syscall.Flock(int(held.Fd()), syscall.LOCK_EX))
|
|
start = time.Now()
|
|
if _, err := ev.Write([]byte("z\n")); err != nil || time.Since(start) > 5*logLockWait {
|
|
t.Errorf("the event log with its lock held: %v after %v", err, time.Since(start))
|
|
}
|
|
}
|
|
|
|
// A copied log far past its limit, such as a sparse file a child made huge, is copied no further
|
|
// back than twice its limit, from a line's start, and emptied.
|
|
func TestRotateCopiesAHugeLog(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "proxy.log")
|
|
fd, err := OpenLog(path)
|
|
must(t, err)
|
|
defer fd.Close()
|
|
must(t, fd.Truncate(1<<40)) // a terabyte, sparse
|
|
_, err = fd.WriteString("\nthe end of a line\nthe last line\n")
|
|
must(t, err)
|
|
r, err := copyRotating(fd, path, fd, 20)
|
|
must(t, err)
|
|
defer r.Close()
|
|
_, err = r.Write([]byte("next\n"))
|
|
must(t, err)
|
|
if a, b := readLog(t, path+".1"), readLog(t, path); a != "the end of a line\nthe last line\n" || b != "next\n" {
|
|
t.Errorf(".1 %q, log %q", a, b)
|
|
}
|
|
}
|
|
|
|
// A copied log whose path isn't its file any more is locked through its descriptor, and still
|
|
// rotated once past its limit; a folder in a log's place is never taken, nor removed.
|
|
func TestRotateCopiesWithoutItsPath(t *testing.T) {
|
|
t.Parallel()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "proxy.log")
|
|
fd, err := OpenLog(path)
|
|
must(t, err)
|
|
defer fd.Close()
|
|
must(t, os.Rename(path, path+".moved"))
|
|
r, err := copyRotating(fd, path, fd, 20)
|
|
must(t, err)
|
|
defer r.Close()
|
|
if r.lock != nil {
|
|
t.Error("a lock on another file than the log's")
|
|
}
|
|
for _, l := range []string{"0123456789\n", "0123456789\n"} {
|
|
_, err := r.Write([]byte(l))
|
|
must(t, err)
|
|
}
|
|
if got := readLog(t, path+".moved"); got != "0123456789\n" {
|
|
t.Errorf("the log after its rotation: %q", got)
|
|
}
|
|
must(t, os.Mkdir(filepath.Join(dir, "folder.log"), 0o700))
|
|
must(t, os.WriteFile(filepath.Join(dir, "folder.log", "x"), nil, 0o600))
|
|
if _, err := OpenLog(filepath.Join(dir, "folder.log")); err == nil {
|
|
t.Error("a folder taken as a log")
|
|
}
|
|
if _, err := OpenEventLog(filepath.Join(dir, "folder.log")); err == nil {
|
|
t.Error("a folder taken as the event log")
|
|
}
|
|
}
|
|
|
|
// What a VM's proxy's child could leave in a log's place goes, its name alone, and a new log
|
|
// starts there: a link to a file of yours, which stays as it was, a FIFO, never waited on, and a
|
|
// hard link of another file.
|
|
func TestOpenLogReplacesWhatIsPlanted(t *testing.T) {
|
|
t.Parallel()
|
|
dir, mine := t.TempDir(), t.TempDir()
|
|
keep := filepath.Join(mine, "keep")
|
|
must(t, os.WriteFile(keep, []byte("mine"), 0o600))
|
|
plant := map[string]func(string) error{
|
|
"link": func(p string) error { return os.Symlink(keep, p) },
|
|
"fifo": func(p string) error { return syscall.Mkfifo(p, 0o600) },
|
|
"hard link": func(p string) error { return os.Link(keep, p) },
|
|
}
|
|
for name, fn := range plant {
|
|
p := filepath.Join(dir, strings.ReplaceAll(name, " ", "-")+".log")
|
|
must(t, fn(p))
|
|
f, err := OpenLog(p)
|
|
if err != nil {
|
|
t.Errorf("%s: %v", name, err)
|
|
continue
|
|
}
|
|
_, _ = f.WriteString("a line\n")
|
|
_ = f.Close()
|
|
if fi, err := os.Lstat(p); err != nil || !fi.Mode().IsRegular() || readLog(t, p) != "a line\n" {
|
|
t.Errorf("%s: not a log of its own: %v", name, err)
|
|
}
|
|
}
|
|
if readLog(t, keep) != "mine" {
|
|
t.Errorf("the planted links' target: %q", readLog(t, keep))
|
|
}
|
|
}
|