From 045b721e95dd163a346c36c2ec8760ab78c3bc84 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 23 Nov 2026 17:15:50 -0300 Subject: [PATCH 1/3] lab: the traceable kernel is a vmlinux entered through PVH, as the release's (#697) Since v20261123.07 the machine boots a release's vmlinux through PVH, and the traceable kernel the ledger boots was published only as a bzImage, so every release ledger since measured a decompressor the release no longer runs. release traceable-kernel now publishes the traceable vmlinux, which build verified as the release's, and no bzImage. traceable:TAG fetches whichever of the two the release published, and a tree places a vmlinux that randomizes itself as kernel/vmlinux with no bzImage beside it. The tree's kernel part is the one OpenRelease boots, and its manifest says how the machine enters it. The ledger names the window before the kernel's clock by that entry. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/parts.yml | 2 +- cmd/spin-machine-dev/lab.go | 2 +- cmd/spin-machine-dev/release.go | 16 +++-- cmd/spin-machine-dev/verbs_test.go | 8 +-- internal/lab/compat.go | 2 +- internal/lab/lane_test.go | 8 ++- internal/lab/ledger.go | 16 +++-- internal/lab/ledger_test.go | 5 ++ internal/lab/ledgerprobe.go | 11 +++- internal/lab/sources.go | 101 +++++++++++++++++++++-------- internal/lab/tree.go | 42 +++++++++--- internal/lab/tree_test.go | 89 +++++++++++++++++++------ internal/release/fetch.go | 12 ++++ internal/release/pack.go | 15 +++-- internal/release/release_test.go | 12 ++-- 15 files changed, 253 insertions(+), 88 deletions(-) diff --git a/.github/workflows/parts.yml b/.github/workflows/parts.yml index e8a22f2e..196b3269 100644 --- a/.github/workflows/parts.yml +++ b/.github/workflows/parts.yml @@ -246,7 +246,7 @@ jobs: # before it is released and not after, when the release has no traceable kernel and no # ledger (#558). Its own cache tag, written by main as the release kernel's is, keeps # release.yml's job warm. ~10 min more cold (parts run 45071066129); the version only names - # the bzImage, which nothing here publishes. + # the vmlinux, which nothing here publishes. - name: Build the traceable kernel env: CACHE_EXPORT: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} diff --git a/cmd/spin-machine-dev/lab.go b/cmd/spin-machine-dev/lab.go index 7f1a84df..0f0c0548 100644 --- a/cmd/spin-machine-dev/lab.go +++ b/cmd/spin-machine-dev/lab.go @@ -49,7 +49,7 @@ func probeNames(probes map[string]lab.Probe) string { // treeFlags are a tree spec's flags, the same for lab tree and inside a --tree NAME=SPEC. func treeFlags(fs *pflag.FlagSet, s *lab.TreeSpec) { fs.StringVar(&s.From, "from", "release", "what the tree starts from: release, release:vYYYYMMDD.NN, or a directory laid out as one (_output)") - fs.StringVar(&s.Kernel, "kernel", "", "the kernel instead: run:N (a parts.yml run), traceable:TAG (the lab-only kernel published beside release TAG, lockdown forced to none) or a bzImage") + fs.StringVar(&s.Kernel, "kernel", "", "the kernel instead: run:N (a parts.yml run), traceable:TAG (the lab-only kernel published beside release TAG, lockdown forced to none), or a vmlinux that randomizes itself or a bzImage") fs.StringVar(&s.QEMU, "qemu", "", "QEMU and its firmware instead: run:N (a parts.yml run) or a directory with bin/ and qemu/") fs.StringVar(&s.SpinMachine, "spin-machine", "", "here: built from this checkout; release: built from the commit the --from release was made from, the machine it defined; commit:SHA: built from that commit; left out, the --from tree's own when it has one, else this checkout's (a release ships none)") fs.StringVar(&s.Append, "append", "", "added to the kernel command line of every boot") diff --git a/cmd/spin-machine-dev/release.go b/cmd/spin-machine-dev/release.go index 27061e9a..d1386823 100644 --- a/cmd/spin-machine-dev/release.go +++ b/cmd/spin-machine-dev/release.go @@ -160,9 +160,11 @@ func releaseTraceableKernelCmd() *cobra.Command { Long: `Build the release's kernel (as build kernel does, into _output) and a second one from the same source and config with lockdown forced to none instead of confidentiality, into _output/traceable, then hold the second's config to the first's: that one option swapped and every other as the -release has it. Its bzImage is put in _output/release as the release's traceable kernel, to be -signed and published beside the release for the lab's ledger, which reads a scheduler trace that -lockdown takes away (kernel/README.md). It enters no tarball and nothing spin boots.`, +release has it. Its vmlinux, which build verified as the release's (both Xen notes and a filled +.data.reloc), is put in _output/release as the release's traceable kernel, to be signed and +published beside the release for the lab's ledger, which reads a scheduler trace that lockdown +takes away (kernel/README.md), and boots it through PVH as the release boots its own (#697). It +enters no tarball and nothing spin boots.`, Args: cobra.NoArgs, RunE: func(c *cobra.Command, _ []string) error { w, err := open() @@ -188,11 +190,11 @@ lockdown takes away (kernel/README.md). It enters no tarball and nothing spin bo if err := build.TraceableConfig(filepath.Join(o.Out, "kernel", "kernel-config"), filepath.Join(t.Out, "kernel", "kernel-config")); err != nil { return couldNot(err) } - to := filepath.Join(w.out, "release", release.TraceableKernel(version)) + to := filepath.Join(w.out, "release", release.TraceableVmlinux(version)) if err := os.MkdirAll(filepath.Dir(to), 0o755); err != nil { return couldNot(fmt.Errorf("making %s: %w", filepath.Dir(to), err)) } - if err := copyFile(filepath.Join(t.Out, "kernel", "bzImage"), to); err != nil { + if err := copyFile(filepath.Join(t.Out, "kernel", "vmlinux"), to); err != nil { return couldNot(err) } say(c, "✓ %s", to) @@ -419,7 +421,7 @@ func sign(ctx context.Context, c *cobra.Command, dir, predicate, identity string "--certificate-identity", identity, "--certificate-oidc-issuer", release.OIDCIssuer) } var files []string - for _, g := range []string{"*.tar.gz", "*.tar.gz.sha256", "machine.env", "*.cdx.json", release.TraceableKernel("*")} { + for _, g := range []string{"*.tar.gz", "*.tar.gz.sha256", "machine.env", "*.cdx.json", release.TraceableVmlinux("*")} { found, _ := filepath.Glob(filepath.Join(dir, g)) files = append(files, found...) } @@ -451,7 +453,7 @@ func sign(ctx context.Context, c *cobra.Command, dir, predicate, identity string } for _, g := range []struct{ require, except []string }{ {[]string{"provenance"}, nil}, - {[]string{"cdx"}, []string{"*.sha256", "machine.env", "*.cdx.json", release.TraceableKernel("*")}}, + {[]string{"cdx"}, []string{"*.sha256", "machine.env", "*.cdx.json", release.TraceableVmlinux("*")}}, } { missing, err := attestgate.Files(dir, g.require, g.except) if err != nil { diff --git a/cmd/spin-machine-dev/verbs_test.go b/cmd/spin-machine-dev/verbs_test.go index 71b69d56..dd05331a 100644 --- a/cmd/spin-machine-dev/verbs_test.go +++ b/cmd/spin-machine-dev/verbs_test.go @@ -229,7 +229,7 @@ func printfEscaped(b []byte) string { func TestTheTraceableKernelIsTheReleaseKernelLessLockdown(t *testing.T) { dir := checkout(t) put(t, filepath.Join(dir, "kernel/Dockerfile"), "FROM x", 0o644) - published := filepath.Join(dir, "_output/release", release.TraceableKernel("v1")) + published := filepath.Join(dir, "_output/release", release.TraceableVmlinux("v1")) calls := fakes(t, map[string]string{"docker": kernelDocker("")}) if _, stderr, status := verb(t, "release", "traceable-kernel", "--version", "v1"); status != 0 { t.Fatalf("exit %d: %s", status, stderr) @@ -238,10 +238,10 @@ func TestTheTraceableKernelIsTheReleaseKernelLessLockdown(t *testing.T) { if !strings.Contains(got, "KERNEL_LOCKDOWN=confidentiality") || !strings.Contains(got, "KERNEL_LOCKDOWN=none --build-arg KERNEL_NPROC=8 --output type=local,dest="+filepath.Join(dir, "_output/traceable")+" ") { t.Errorf("docker was not asked for both kernels:\n%s", got) } - if read(t, published) != read(t, filepath.Join(dir, "_output/traceable/kernel/bzImage")) { + if read(t, published) != read(t, filepath.Join(dir, "_output/traceable/kernel/vmlinux")) { t.Error("the traceable kernel was not put beside the release") } - if _, err := os.Stat(filepath.Join(dir, "_output/kernel/bzImage")); err != nil { + if _, err := os.Stat(filepath.Join(dir, "_output/kernel/vmlinux")); err != nil { t.Errorf("the release kernel was not built: %v", err) } _ = os.Remove(published) @@ -698,7 +698,7 @@ func TestProvenanceIsRefusedOutsideAWorkflow(t *testing.T) { func TestSignChecksEveryBundleAndThatNothingIsLeftUnattested(t *testing.T) { dir := checkout(t) rel := filepath.Join(dir, "_output/release") - for _, f := range []string{"r.tar.gz", "r.tar.gz.sha256", "machine.env", "r.cdx.json", release.TraceableKernel("v1")} { + for _, f := range []string{"r.tar.gz", "r.tar.gz.sha256", "machine.env", "r.cdx.json", release.TraceableVmlinux("v1")} { put(t, filepath.Join(rel, f), f, 0o644) } // cosign writes each bundle it is asked for. diff --git a/internal/lab/compat.go b/internal/lab/compat.go index 958af639..009600e9 100644 --- a/internal/lab/compat.go +++ b/internal/lab/compat.go @@ -57,7 +57,7 @@ func Compat(here string, tags []string, src Sources, resume func(previous, previ if err != nil { return nil, err } - if !fileExists(filepath.Join(dir, partFiles["kernel"])) { + if !fileExists(filepath.Join(dir, "kernel", "bzImage")) { rows = append(rows, CompatRow{Release: tag, NoBzImage: true}) break } diff --git a/internal/lab/lane_test.go b/internal/lab/lane_test.go index 57bb5eeb..ca606e13 100644 --- a/internal/lab/lane_test.go +++ b/internal/lab/lane_test.go @@ -9,6 +9,8 @@ import ( "strconv" "strings" "testing" + + "github.com/spin-stack/machine/machine" ) // TestEveryProbeMeasuresATree is the VM lane: every probe once over a tree made from a built @@ -135,12 +137,12 @@ func TestTheLabTalksToGitHub(t *testing.T) { } // A release's traceable kernel, its signature checked: from the first release built with one. if tag := os.Getenv("SPIN_LAB_TRACEABLE"); tag != "" { - bz, err := src.traceable(tag) + k, err := src.traceable(tag) if err != nil { t.Fatal(err) } - if err := isBzImage(bz); err != nil { - t.Fatal(err) + if machine.PVH(k) != nil && isBzImage(k) != nil { + t.Fatalf("%s is neither a vmlinux the machine enters through PVH nor a bzImage", k) } } if id := os.Getenv("SPIN_LAB_RUN"); id != "" { diff --git a/internal/lab/ledger.go b/internal/lab/ledger.go index aa323d9d..2deac29d 100644 --- a/internal/lab/ledger.go +++ b/internal/lab/ledger.go @@ -32,7 +32,7 @@ type Ledger struct { FirstLogin float64 `json:"first_login"` // KernelZero is where the kernel's clock read 0 on the host's, and KernelZeroError half the // width of the narrowest bracket the trips gave it: before it is QEMU, the firmware and the - // kernel's decompression, which no clock in the guest sees. + // kernel's entry (a bzImage's decompression, or PVH's), which no clock in the guest sees. KernelZero float64 `json:"kernel_zero"` KernelZeroError float64 `json:"kernel_zero_error"` // QEMUExec is where spin-machine exec'd QEMU, and QEMUExecError half the bracket the host's @@ -42,9 +42,13 @@ type Ledger struct { // Running is where the machine went to running and its vCPU ran the guest's first instruction: // KernelZero less the kvm-clock the kernel read as it made kvm-clock the scheduler's clock, which // QEMU zeroed at that moment (schedOffset). From QEMUExec to Running is QEMU's start and the held - // start's monitor; from Running to KernelZero, the firmware, the bzImage's decompression and the - // kernel's setup until its clock runs. + // start's monitor; from Running to KernelZero, the firmware, the kernel's entry and its setup + // until its clock runs. Running float64 `json:"running"` + // PVH is whether the machine entered a vmlinux through PVH, with no decompressor (#645), and not + // a bzImage: what the time from Running to KernelZero holds. A ledger from before #697 has + // none, and booted a bzImage. + PVH bool `json:"pvh,omitempty"` // LoginEnd is the login's command's exit on the kernel's clock: where the trace's account ends. LoginEnd float64 `json:"login_end"` // Residue is FirstLogin less KernelZero and LoginEnd: what the host waited for after the @@ -685,7 +689,11 @@ func (l Ledger) Text(title string) string { fmt.Fprintf(&b, "%8s %8s %8.1f before the kernel's clock (±%.2f)\n", "", "0.0", l.KernelZero, l.KernelZeroError) fmt.Fprintf(&b, "%26.1f spin-machine, to its exec of QEMU (±%.2f)\n", l.QEMUExec, l.QEMUExecError) fmt.Fprintf(&b, "%26.1f QEMU's start and the held start, to the guest's first instruction\n", l.Running-l.QEMUExec) - fmt.Fprintf(&b, "%26.1f the firmware, the decompression and the kernel's setup, to the kernel's clock\n", l.KernelZero-l.Running) + entry := "the firmware, the decompression and the kernel's setup, to the kernel's clock" + if l.PVH { + entry = "the firmware, the kernel's PVH entry and its setup, to the kernel's clock" + } + fmt.Fprintf(&b, "%26.1f %s\n", l.KernelZero-l.Running, entry) for _, w := range l.Windows { fmt.Fprintf(&b, "%8.1f %8.1f %8.1f %s\n", w.Start, w.End, w.End-w.Start, w.Name) var rest float64 diff --git a/internal/lab/ledger_test.go b/internal/lab/ledger_test.go index 40c858af..34eda172 100644 --- a/internal/lab/ledger_test.go +++ b/internal/lab/ledger_test.go @@ -527,6 +527,11 @@ func TestTheLedgersTextShowsWhatMatters(t *testing.T) { if strings.Contains(text, "quiet") || strings.Contains(text, " d 9.6") || strings.Count(text, "others under") != 1 { t.Errorf("the table shows what it should leave out:\n%s", text) } + // A boot that entered a vmlinux through PVH ran no decompressor, and the window says so. + l.PVH = true + if pvh := l.Text("t"); !strings.Contains(pvh, " 20.0 the firmware, the kernel's PVH entry and its setup, to the kernel's clock\n") || strings.Contains(pvh, "decompression") { + t.Errorf("a PVH boot's window before the kernel's clock:\n%s", pvh) + } bare := Ledger{Windows: l.Windows[:1]}.Text("t") if strings.Contains(bare, "owners from") || strings.Contains(bare, "in the journal") || strings.Contains(bare, "lost events") { t.Errorf("a ledger of one window, no marks and no breaks:\n%s", bare) diff --git a/internal/lab/ledgerprobe.go b/internal/lab/ledgerprobe.go index 61fe6dcd..5e080043 100644 --- a/internal/lab/ledgerprobe.go +++ b/internal/lab/ledgerprobe.go @@ -16,6 +16,8 @@ import ( "time" xssh "golang.org/x/crypto/ssh" + + "github.com/spin-stack/machine/machine" ) // ledgerName is the edit a tree carries to be accounted for, and the probe that accounts for it. @@ -132,11 +134,11 @@ func (ledgerProbe) Needs(o Options) []Need { func (ledgerProbe) Metrics() []Metric { return []Metric{ {"first_login", "ms", "the first login, /usr/bin/true, from the moment before QEMU is exec'd, with the trace running"}, - {"kernel_zero", "ms", "where the kernel's clock read 0, from the same moment: QEMU, the firmware and the kernel's decompression"}, + {"kernel_zero", "ms", "where the kernel's clock read 0, from the same moment: QEMU, the firmware and the kernel's entry"}, {"kernel_zero_error", "ms", "kernel_zero's uncertainty, half its narrowest bracket"}, {"before_qemu", "ms", "kernel_zero's first part: spin-machine, from the same moment to its exec of QEMU"}, {"qemu_start", "ms", "kernel_zero's second part: QEMU's start and the held start, from its exec to the guest's first instruction"}, - {"guest_to_zero", "ms", "kernel_zero's third part: the firmware, the bzImage's decompression and the kernel's setup, to the kernel's clock"}, + {"guest_to_zero", "ms", "kernel_zero's third part: the firmware, the kernel's entry (through PVH, or a bzImage's decompression) and its setup, to the kernel's clock"}, {"qemu_exec_error", "ms", "before_qemu's uncertainty, half the bracket the host's polling put QEMU's exec in"}, {"pid1", "ms", "PID 1's exec of init, on the kernel's clock"}, {"handoff", "ms", "spin's handoff as login=spin stands in for it, from its exec as PID 1 to its exec of systemd; 0 on the lane"}, @@ -165,6 +167,10 @@ func (ledgerProbe) Measure(_ context.Context, m *Measurement) error { if m.Keep == "" { return fmt.Errorf("the ledger probe keeps its ledgers, and the experiment names no directory to keep them in") } + kernel, err := m.Tree.Kernel() + if err != nil { + return err + } overlay, err := m.Overlay("overlay.qcow2", m.Key.Edits()...) if err != nil { return err @@ -244,6 +250,7 @@ func (ledgerProbe) Measure(_ context.Context, m *Measurement) error { if err != nil { return Miss("no ledger, %d events dropped by the buffer: %v", dropped, err) } + l.PVH = machine.PVH(kernel) == nil b, err := json.MarshalIndent(l, "", " ") if err != nil { return fmt.Errorf("writing the ledger: %w", err) diff --git a/internal/lab/sources.go b/internal/lab/sources.go index c13a465f..5e130ecd 100644 --- a/internal/lab/sources.go +++ b/internal/lab/sources.go @@ -11,11 +11,13 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "github.com/spin-stack/go-tools/versions" "github.com/spin-stack/machine/internal/release" + "github.com/spin-stack/machine/machine" ) // from is the tree a spec starts from and how a manifest names it. @@ -114,15 +116,27 @@ func one(dir, suffix string) (string, error) { return found[0], nil } -// traceable is release tag's traceable kernel (release.TraceableKernel), downloaded and its -// signature checked the first time it is asked for, in Cache/traceable/TAG. +// traceable is release tag's traceable kernel, downloaded and its signature checked the first time +// it is asked for, in Cache/traceable/TAG: the vmlinux a release publishes from #697 on +// (release.TraceableVmlinux), or the bzImage one published before it (release.TraceableBzImage). +// A release publishes one of the two, so whichever the cache holds is the release's. // // mutate-lane: TestTheLabTalksToGitHub, with SPIN_LAB_TRACEABLE set to a release that has one. func (s Sources) traceable(tag string) (string, error) { dir := filepath.Join(s.Cache, "traceable", tag) - bz := filepath.Join(dir, release.TraceableKernel(tag)) - if fileExists(bz) { - return bz, nil + names := []string{release.TraceableVmlinux(tag), release.TraceableBzImage(tag)} + for _, n := range names { + if p := filepath.Join(dir, n); fileExists(p) { + return p, nil + } + } + published, err := release.Assets(context.Background(), tag) + if err != nil { + return "", err + } + i := slices.IndexFunc(names, func(n string) bool { return slices.Contains(published, n) }) + if i < 0 { + return "", fmt.Errorf("release %s publishes no traceable kernel, neither %s nor %s (published beside releases from #536 on)", tag, names[0], names[1]) } pins, err := versions.Load(filepath.Join(s.Checkout, versions.File)) if err != nil { @@ -134,36 +148,61 @@ func (s Sources) traceable(tag string) (string, error) { if err := os.MkdirAll(dir+".part", 0o755); err != nil { return "", fmt.Errorf("making %s.part: %w", dir, err) } - if _, err := release.Signed(context.Background(), pins, tag, dir+".part", release.TraceableKernel(tag)); err != nil { - return "", fmt.Errorf("release %s's traceable kernel (published beside releases from #536 on): %w", tag, err) + if _, err := release.Signed(context.Background(), pins, tag, dir+".part", names[i]); err != nil { + return "", fmt.Errorf("release %s's traceable kernel: %w", tag, err) } if err := os.Rename(dir+".part", dir); err != nil { return "", fmt.Errorf("keeping release %s's traceable kernel: %w", tag, err) } - return bz, nil + return filepath.Join(dir, names[i]), nil } -// placeKernel puts the kernel what names into the tree at dir and says where it came from. +// placeKernel puts the kernel what names into the tree at dir and says where it came from and how +// the machine enters it. // -// A bzImage, always, which is what the machine boots. A Kernel run from before 2026-11-19 -// uploaded none, and is refused by name. +// A vmlinux that randomizes itself (machine.PVH) is entered through PVH, as a release's is from +// v20261123.07 on (#645): it is put at kernel/vmlinux and the tree's bzImage is removed, which +// would be another kernel. Otherwise the kernel is a bzImage, entered through its decompressor, +// with the vmlinux beside it when there is one, for what reads symbols. A run's artifact or a +// path given may hold both, and the vmlinux is what the machine boots then (machine.OpenRelease). +// A Kernel run from before 2026-11-19 built neither, and is refused by name. func (s Sources) placeKernel(what, dir string) (string, error) { - bz, source := what, what - if tag, ok := strings.CutPrefix(what, "traceable:"); ok { - var err error - if bz, err = s.traceable(tag); err != nil { + var bz, vmlinux string + source := what + switch { + case strings.HasPrefix(what, "traceable:"): + tag := strings.TrimPrefix(what, "traceable:") + k, err := s.traceable(tag) + if err != nil { return "", err } - source = "release " + tag + "'s traceable kernel" - } else if id, ok := strings.CutPrefix(what, "run:"); ok { + bz, vmlinux, source = k, k, "release "+tag+"'s traceable kernel" + case strings.HasPrefix(what, "run:"): + id := strings.TrimPrefix(what, "run:") d, err := s.run(id, "vmlinux-*") if err != nil { return "", err } - if bz, err = one(d, "bzImage"); err != nil { - return "", fmt.Errorf("kernel run %s: %w (a run from before 2026-11-19 built no bzImage)", id, err) + vmlinux, _ = one(d, "vmlinux") + if bz, err = one(d, "bzImage"); err != nil && machine.PVH(vmlinux) != nil { + return "", fmt.Errorf("kernel run %s: %w, and no vmlinux the machine enters through PVH (a run from before 2026-11-19 built neither)", id, err) } source = "run " + id + default: + bz, vmlinux = what, filepath.Join(filepath.Dir(what), "vmlinux") + if machine.PVH(what) == nil { + vmlinux = what + } + } + tree := filepath.Join(dir, "kernel", "vmlinux") + if machine.PVH(vmlinux) == nil { + if err := replace(vmlinux, tree, 0o644); err != nil { + return "", err + } + if err := removeIfThere(filepath.Join(dir, "kernel", "bzImage")); err != nil { + return "", err + } + return source + ", a vmlinux entered through PVH", nil } if err := isBzImage(bz); err != nil { return "", err @@ -171,18 +210,24 @@ func (s Sources) placeKernel(what, dir string) (string, error) { if err := replace(bz, filepath.Join(dir, "kernel", "bzImage"), 0o644); err != nil { return "", err } - // The vmlinux beside it when there is one, for what reads symbols; without one the release's - // would be left describing another kernel. - vmlinux := filepath.Join(dir, "kernel", "vmlinux") - if err := os.Remove(vmlinux); err != nil && !errors.Is(err, fs.ErrNotExist) { - return "", fmt.Errorf("removing %s: %w", vmlinux, err) + // Without a vmlinux of its own, the release's would be left describing another kernel. + if err := removeIfThere(tree); err != nil { + return "", err } - if v := filepath.Join(filepath.Dir(bz), "vmlinux"); fileExists(v) { - if err := replace(v, vmlinux, 0o644); err != nil { + if fileExists(vmlinux) && vmlinux != bz { + if err := replace(vmlinux, tree, 0o644); err != nil { return "", err } } - return source, nil + return source + ", a bzImage entered through its decompressor", nil +} + +// removeIfThere removes path, and is content when there is nothing to remove. +func removeIfThere(path string) error { + if err := os.Remove(path); err != nil && !errors.Is(err, fs.ErrNotExist) { + return fmt.Errorf("removing %s: %w", path, err) + } + return nil } // placeQEMU puts QEMU and the firmware beside it - a QEMU run's artifact, or a directory holding @@ -382,7 +427,7 @@ func isBzImage(path string) error { defer func() { _ = f.Close() }() magic := make([]byte, 4) if _, err := f.ReadAt(magic, 0x202); err != nil || string(magic) != "HdrS" { - return fmt.Errorf("%s is not a bzImage (no HdrS at 0x202): the machine boots a bzImage, and nothing enters a vmlinux", path) + return fmt.Errorf("%s is not a bzImage (no HdrS at 0x202), nor a vmlinux the machine enters through PVH: the machine boots one of the two", path) } return nil } diff --git a/internal/lab/tree.go b/internal/lab/tree.go index 261c0791..a539f484 100644 --- a/internal/lab/tree.go +++ b/internal/lab/tree.go @@ -73,17 +73,31 @@ type NamedEdit struct { Edit Edit `json:"edit"` } -// partFiles are the parts of a tree, and the file under it each is checked by. +// partFiles are the parts of a tree, and the file under it each is checked by. The kernel's is the +// one the tree boots, which machine.OpenRelease picks (partFile). var partFiles = map[string]string{ "qemu": "bin/qemu-system-x86_64", "firmware": "qemu/" + machine.DefaultBIOS, - "kernel": "kernel/bzImage", + "kernel": "kernel/vmlinux or kernel/bzImage", "image": "image/rootfs.qcow2", "spin-machine": "bin/spin-machine", } // partFile is the file under dir that part name is checked by. -func partFile(dir, name string) string { return filepath.Join(dir, partFiles[name]) } +func partFile(dir, name string) (string, error) { + if name != "kernel" { + return filepath.Join(dir, partFiles[name]), nil + } + r, err := machine.OpenRelease(dir) + if err != nil { + return "", err + } + s, err := r.Spec("") + if err != nil { + return "", err + } + return s.Kernel, nil +} const manifestName = "manifest.json" @@ -118,7 +132,10 @@ func (t *Tree) check() error { if !ok || p.Source == "" { return fmt.Errorf("tree %s: the manifest does not say where its %s came from", t.Manifest.Name, name) } - file := partFile(t.Dir, name) + file, err := partFile(t.Dir, name) + if err != nil { + return fmt.Errorf("tree %s: %w", t.Manifest.Name, err) + } if _, err := os.Stat(file); err != nil { return fmt.Errorf("tree %s has no %s: %w", t.Manifest.Name, name, err) } @@ -130,7 +147,7 @@ func (t *Tree) check() error { return err } if sum != p.SHA256 { - return fmt.Errorf("tree %s: %s is %s, and its manifest says %s", t.Manifest.Name, partFiles[name], sum, p.SHA256) + return fmt.Errorf("tree %s: %s is %s, and its manifest says %s", t.Manifest.Name, file, sum, p.SHA256) } } return nil @@ -145,6 +162,9 @@ func (t *Tree) SpinMachine() string { return filepath.Join(t.Dir, "bin", "spin-m // QEMUImg is the tree's qemu-img, which makes its overlays. func (t *Tree) QEMUImg() string { return filepath.Join(t.Dir, "bin", "qemu-img") } +// Kernel is the kernel every boot of the tree boots: its vmlinux entered through PVH, or its bzImage. +func (t *Tree) Kernel() (string, error) { return partFile(t.Dir, "kernel") } + // Rootfs is the base image every overlay of the tree is backed by. It is never opened for writing. func (t *Tree) Rootfs() string { return filepath.Join(t.Dir, "image", "rootfs.qcow2") } @@ -161,9 +181,9 @@ type TreeSpec struct { // a directory laid out as a release (_output after `spin-machine-dev build`). From string // Kernel and QEMU replace those parts: "run:N" for a parts.yml run (or a Kernel or QEMU workflow run from before it), or a path - // (a bzImage; a directory holding bin/qemu-system-x86_64 and qemu/). Kernel is also - // "traceable:vYYYYMMDD.NN", the kernel published beside that release with lockdown forced to - // none, which the ledger reads a trace from (release.TraceableKernel). + // (a vmlinux that randomizes itself or a bzImage; a directory holding bin/qemu-system-x86_64 and + // qemu/). Kernel is also "traceable:vYYYYMMDD.NN", the kernel published beside that release with + // lockdown forced to none, which the ledger reads a trace from (Sources.traceable). Kernel, QEMU string // SpinMachine is "here" for this checkout's, built; "release" for the one built from the // commit the From tree's machine.env names, which is the machine that release defined: spin @@ -300,7 +320,11 @@ func MakeTree(dir string, spec TreeSpec, src Sources) (*Tree, error) { } for _, name := range []string{"kernel", "qemu", "spin-machine"} { p := m.Parts[name] - if p.SHA256, err = fileSHA256(partFile(dir, name)); err != nil { + file, err := partFile(dir, name) + if err != nil { + return nil, fmt.Errorf("tree %s: %w", spec.Name, err) + } + if p.SHA256, err = fileSHA256(file); err != nil { return nil, err } m.Parts[name] = p diff --git a/internal/lab/tree_test.go b/internal/lab/tree_test.go index f32cc586..f53b2918 100644 --- a/internal/lab/tree_test.go +++ b/internal/lab/tree_test.go @@ -14,6 +14,7 @@ import ( "strings" "testing" + "github.com/spin-stack/machine/internal/elftest" "github.com/spin-stack/machine/internal/release" ) @@ -35,6 +36,7 @@ func fakeRelease(t *testing.T, kernel string) string { "bin/spin-machine": []byte("spin-machine"), "qemu/qboot.bin": []byte("qboot"), "qemu/linuxboot_dma.bin": []byte("rom"), + "qemu/pvh.bin": []byte("pvh rom"), "image/rootfs.qcow2": []byte("base"), "kernel/vmlinux": []byte("elf"), } @@ -144,7 +146,7 @@ func TestAPartPutIntoATreeIsACopyAndTheBaseIsUntouched(t *testing.T) { if fi, err := os.Lstat(filepath.Join(tr.Dir, "kernel/bzImage")); err != nil || fi.Mode()&os.ModeSymlink != 0 { t.Error("a part put into the tree is still a link") } - if tr.Manifest.Parts["kernel"].Source != newKernel || tr.Manifest.Parts["qemu"].Source != newQEMU || tr.Manifest.Parts["firmware"].Source != newQEMU { + if tr.Manifest.Parts["kernel"].Source != newKernel+", a bzImage entered through its decompressor" || tr.Manifest.Parts["qemu"].Source != newQEMU || tr.Manifest.Parts["firmware"].Source != newQEMU { t.Errorf("the manifest's sources: %+v", tr.Manifest.Parts) } sum, _ := fileSHA256(newKernel) @@ -372,7 +374,7 @@ func TestAKernelOrQEMUFromARunIsTheRunsBuild(t *testing.T) { if err != nil { t.Fatal(err) } - if tr.Manifest.Parts["kernel"].Source != "run 11" || tr.Manifest.Parts["qemu"].Source != "run 22" || + if tr.Manifest.Parts["kernel"].Source != "run 11, a bzImage entered through its decompressor" || tr.Manifest.Parts["qemu"].Source != "run 22" || !strings.HasSuffix(read(t, filepath.Join(tr.Dir, "kernel/bzImage")), "run kernel") || read(t, filepath.Join(tr.Dir, "qemu/qboot.bin")) != "run qboot" { t.Errorf("the run's parts: %+v", tr.Manifest.Parts) @@ -400,30 +402,60 @@ func TestAKernelOrQEMUFromARunIsTheRunsBuild(t *testing.T) { } } +// A kernel that randomizes itself is entered through PVH: put at kernel/vmlinux with the tree's +// bzImage gone, which would be another kernel, and the manifest says so. A run since the +// bzImage was dropped built only that. +func TestAVmlinuxThatRandomizesItselfIsBootedThroughPVH(t *testing.T) { + base := fakeRelease(t, "release kernel") + cache := t.TempDir() + s := Sources{Checkout: checkoutRoot(t), Cache: cache, Log: io.Discard} + write(t, filepath.Join(cache, "runs/11/vmlinux/vmlinux-abc/vmlinux"), pvhKernel()) + given := filepath.Join(t.TempDir(), "vmlinux") + write(t, given, pvhKernel()) + for what, source := range map[string]string{"run:11": "run 11", given: given} { + tr, err := MakeTree(filepath.Join(t.TempDir(), "p"), TreeSpec{Name: "p", From: base, Kernel: what}, s) + if err != nil { + t.Fatal(err) + } + booted, err := tr.Kernel() + if err != nil || booted != filepath.Join(tr.Dir, "kernel/vmlinux") || read(t, booted) != string(pvhKernel()) { + t.Errorf("%s: the tree boots %q, %v", what, booted, err) + } + if fileExists(filepath.Join(tr.Dir, "kernel/bzImage")) { + t.Errorf("%s: the release's bzImage was left beside the kernel put in", what) + } + sum, _ := fileSHA256(given) + if p := tr.Manifest.Parts["kernel"]; p.Source != source+", a vmlinux entered through PVH" || p.SHA256 != sum { + t.Errorf("%s: the manifest's kernel: %+v", what, p) + } + } +} + +func pvhKernel() []byte { return elftest.Kernel([]uint32{18, 19}, []byte{1, 0, 0, 0}) } + // A release's traceable kernel is downloaded beside the release once its signature is release.yml's, -// kept in the cache, and booted from there; one that is not signed is not kept. +// kept in the cache, and booted from there; one that is not signed is not kept. A release publishes +// its vmlinux from #697 on, entered through PVH as the release's own, and before it a bzImage. func TestATraceableKernelIsTheSignedOneBesideTheRelease(t *testing.T) { base := fakeRelease(t, "release kernel") cache, pub, bin := t.TempDir(), t.TempDir(), t.TempDir() - name := release.TraceableKernel("v1") - write(t, filepath.Join(pub, name), bzImage("traceable kernel")) - write(t, filepath.Join(pub, name+".sigstore.json"), []byte("{}")) - gh := `d=""; while [ $# -gt 0 ]; do case "$1" in -D) d=$2;; -p) [ -f "` + pub + `/$2" ] && cp "` + pub + `/$2" "$d/";; esac; shift; done` + old, now := release.TraceableBzImage("v1"), release.TraceableVmlinux("v2") + write(t, filepath.Join(pub, "v1", old), bzImage("traceable kernel")) + write(t, filepath.Join(pub, "v1", old+".sigstore.json"), []byte("{}")) + write(t, filepath.Join(pub, "v2", now), pvhKernel()) + write(t, filepath.Join(pub, "v2", now+".sigstore.json"), []byte("{}")) + write(t, filepath.Join(pub, "v3", "nothing"), nil) + gh := `tag=$3; [ "$2" = view ] && exec ls "` + pub + `/$tag" +d=""; while [ $# -gt 0 ]; do case "$1" in -D) d=$2;; -p) [ -f "` + pub + `/$tag/$2" ] && cp "` + pub + `/$tag/$2" "$d/";; esac; shift; done` write(t, filepath.Join(bin, "gh"), []byte("#!/bin/sh\n"+gh+"\n")) write(t, filepath.Join(bin, "go"), []byte("#!/bin/sh\nexit 1\n")) - if err := os.Chmod(filepath.Join(bin, "gh"), 0o755); err != nil { - t.Fatal(err) - } - if err := os.Chmod(filepath.Join(bin, "go"), 0o755); err != nil { - t.Fatal(err) - } t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH")) s := Sources{Checkout: checkoutRoot(t), Cache: cache, Log: io.Discard} spec := TreeSpec{Name: "t", From: base, Kernel: "traceable:v1"} if _, err := MakeTree(filepath.Join(t.TempDir(), "t"), spec, s); err == nil || !strings.Contains(err.Error(), "signature") { t.Errorf("an unsigned traceable kernel: %v", err) } - if fileExists(filepath.Join(cache, "traceable", "v1", name)) { + if fileExists(filepath.Join(cache, "traceable", "v1", old)) { t.Error("an unsigned traceable kernel was kept") } write(t, filepath.Join(bin, "go"), []byte("#!/bin/sh\nexit 0\n")) @@ -431,14 +463,33 @@ func TestATraceableKernelIsTheSignedOneBesideTheRelease(t *testing.T) { if err != nil { t.Fatal(err) } - if tr.Manifest.Parts["kernel"].Source != "release v1's traceable kernel" || !strings.HasSuffix(read(t, filepath.Join(tr.Dir, "kernel/bzImage")), "traceable kernel") { - t.Errorf("the tree's kernel: %+v", tr.Manifest.Parts["kernel"]) + if tr.Manifest.Parts["kernel"].Source != "release v1's traceable kernel, a bzImage entered through its decompressor" || + !strings.HasSuffix(read(t, filepath.Join(tr.Dir, "kernel/bzImage")), "traceable kernel") || fileExists(filepath.Join(tr.Dir, "kernel/vmlinux")) { + t.Errorf("v1's tree's kernel: %+v", tr.Manifest.Parts["kernel"]) } - if err := os.Remove(filepath.Join(pub, name)); err != nil { + + spec.Kernel = "traceable:v2" + tr, err = MakeTree(filepath.Join(t.TempDir(), "t"), spec, s) + if err != nil { t.Fatal(err) } - if _, err := MakeTree(filepath.Join(t.TempDir(), "t"), spec, s); err != nil { - t.Errorf("a traceable kernel in the cache was asked of GitHub again: %v", err) + if booted, _ := tr.Kernel(); tr.Manifest.Parts["kernel"].Source != "release v2's traceable kernel, a vmlinux entered through PVH" || + booted != filepath.Join(tr.Dir, "kernel/vmlinux") || read(t, booted) != string(pvhKernel()) { + t.Errorf("v2's tree boots %s: %+v", booted, tr.Manifest.Parts["kernel"]) + } + + spec.Kernel = "traceable:v3" + if _, err := MakeTree(filepath.Join(t.TempDir(), "t"), spec, s); err == nil || !strings.Contains(err.Error(), "publishes no traceable kernel") { + t.Errorf("a release with none: %v", err) + } + if err := os.RemoveAll(pub); err != nil { + t.Fatal(err) + } + for _, tag := range []string{"v1", "v2"} { + spec.Kernel = "traceable:" + tag + if _, err := MakeTree(filepath.Join(t.TempDir(), "t"), spec, s); err != nil { + t.Errorf("%s's traceable kernel in the cache was asked of GitHub again: %v", tag, err) + } } } diff --git a/internal/release/fetch.go b/internal/release/fetch.go index d67da6f2..afb1303a 100644 --- a/internal/release/fetch.go +++ b/internal/release/fetch.go @@ -70,6 +70,18 @@ func Signed(ctx context.Context, pins *versions.Versions, tag, dir, name string) return path, verify(ctx, pins, path) } +// Assets are the names of the files published with release tag. +func Assets(ctx context.Context, tag string) ([]string, error) { + var stderr bytes.Buffer + cmd := exec.CommandContext(ctx, "gh", "release", "view", tag, "-R", Repo, "--json", "assets", "-q", ".assets[].name") + cmd.Stderr = &stderr + out, err := cmd.Output() + if err != nil { + return nil, fmt.Errorf("listing release %s's files: %w: %s", tag, err, bytes.TrimSpace(stderr.Bytes())) + } + return strings.Fields(string(out)), nil +} + // download puts the files names of release tag in dir. func download(ctx context.Context, tag, dir string, names ...string) error { args := []string{"release", "download", tag, "-R", Repo, "-D", dir} diff --git a/internal/release/pack.go b/internal/release/pack.go index 5834a1a9..41472c83 100644 --- a/internal/release/pack.go +++ b/internal/release/pack.go @@ -30,11 +30,16 @@ import ( // Name is the release's name, which its tarball and directory carry. func Name(version string) string { return "spin-machine-" + version + "-linux-" + build.Arch } -// TraceableKernel is the name the lab's kernel of release version is published under beside the -// release: its kernel built from the same source and config with lockdown forced to none, so the -// ledger can read a scheduler trace (kernel/README.md). Never in the tarball nor anything spin -// boots, and named lab-only for whoever reads the release's files. -func TraceableKernel(version string) string { return Name(version) + ".lab-only-traceable.bzImage" } +// TraceableVmlinux is the name the lab's kernel of release version is published under beside the +// release: its vmlinux built from the same source and config with lockdown forced to none, so the +// ledger can read a scheduler trace (kernel/README.md), and entered through PVH as the release's is. +// Never in the tarball nor anything spin boots, and named lab-only for whoever reads the release's +// files. +func TraceableVmlinux(version string) string { return Name(version) + ".lab-only-traceable.vmlinux" } + +// TraceableBzImage is the name the traceable kernel was published under until #697: a bzImage, +// which releases up to v20261123.07 boot, and the lab still fetches for a ledger of one of them. +func TraceableBzImage(version string) string { return Name(version) + ".lab-only-traceable.bzImage" } // Options is what a release is packed from. type Options struct { diff --git a/internal/release/release_test.go b/internal/release/release_test.go index d2621e9c..8698a7c0 100644 --- a/internal/release/release_test.go +++ b/internal/release/release_test.go @@ -367,10 +367,14 @@ func TestAVersionIsGivenOrTheNextOfToday(t *testing.T) { } } -// The traceable kernel's name is what the lab of a later commit asks an earlier release for, and -// says lab-only to whoever reads the release's files. +// The traceable kernel's names are what the lab of a later commit asks an earlier release for, and +// say lab-only to whoever reads the release's files: the vmlinux a release publishes from #697 on, +// and the bzImage the releases before it did. func TestTheTraceableKernelIsPublishedUnderAName(t *testing.T) { - if got, want := TraceableKernel("v20261123.01"), "spin-machine-v20261123.01-linux-x86_64.lab-only-traceable.bzImage"; got != want { - t.Errorf("TraceableKernel = %q, want %q", got, want) + if got, want := TraceableVmlinux("v20261123.08"), "spin-machine-v20261123.08-linux-x86_64.lab-only-traceable.vmlinux"; got != want { + t.Errorf("TraceableVmlinux = %q, want %q", got, want) + } + if got, want := TraceableBzImage("v20261123.01"), "spin-machine-v20261123.01-linux-x86_64.lab-only-traceable.bzImage"; got != want { + t.Errorf("TraceableBzImage = %q, want %q", got, want) } } From 8f69d5c20499f565bee78ad69512a9b33c6767ae Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 23 Nov 2026 17:16:44 -0300 Subject: [PATCH 2/3] docs: the ledger boots the way the release boots (#697) Co-Authored-By: Claude Opus 5.5 --- docs/measuring.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/measuring.md b/docs/measuring.md index dc14176f..0ded254c 100644 --- a/docs/measuring.md +++ b/docs/measuring.md @@ -230,6 +230,14 @@ published beside release TAG, lab-only: the release's source and config with the release kernel's (#536). Releases before the first one built with it have none. `releases measure` takes one ledger of each release on it, beside its boot chart, and its record keeps it as `docs/releases/TAG-ledger.txt`. + +**It boots the way the release boots.** A release from #697 on publishes its traceable kernel as +a vmlinux that randomizes itself, verified as the release's is, and the tree boots it through +PVH as the release boots its own. Up to v20261123.07 it was a bzImage, and `traceable:TAG` still +fetches that for those releases. So the ledgers of v20261123.07 and of every release after it +until #697 booted through a decompressor their release no longer ran, and their window before the +kernel's clock reads ~20 ms long. The tree's manifest names the kernel and how the machine +enters it, and the ledger names that window by it. `--append` takes `lab.LedgerAppend` as it stands in internal/lab/ledgerprobe.go: the scheduler's tracepoints from the kernel's start, on CLOCK_MONOTONIC, into a 2 MiB buffer that drops what does not fit rather than the boot. @@ -282,7 +290,8 @@ trace lost events. A switch away from a task the CPU was not running counts as a So the span before the kernel's clock is three parts, which add up to `kernel_zero` by construction: `before_qemu`, spin-machine to its exec of QEMU; `qemu_start`, QEMU's start and the held start's monitor to the guest's first instruction; and `guest_to_zero`, the firmware, the -bzImage's decompression and the kernel's setup until its clock runs. A sched offset that puts the +kernel's entry (through PVH, or a bzImage's decompression) and the kernel's setup until its clock +runs. A sched offset that puts the machine's running before QEMU's exec refuses the ledger, as a journal without the line does. `kernel_zero` read 97 to 114 ms in six boots, the first the longest (lab 45110630795, bench, spin's shape, v20261123.04, 2026-11-23): From 08d1e87029045c7c8981841f9c54333b27d3d0b2 Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Mon, 23 Nov 2026 17:26:12 -0300 Subject: [PATCH 3/3] test: a release's assets, and a listing that fails (#697) Co-Authored-By: Claude Opus 5.5 --- internal/lab/tree_test.go | 4 ++++ internal/release/fetch_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/internal/lab/tree_test.go b/internal/lab/tree_test.go index f53b2918..834e11e6 100644 --- a/internal/lab/tree_test.go +++ b/internal/lab/tree_test.go @@ -478,6 +478,10 @@ d=""; while [ $# -gt 0 ]; do case "$1" in -D) d=$2;; -p) [ -f "` + pub + `/$tag/ t.Errorf("v2's tree boots %s: %+v", booted, tr.Manifest.Parts["kernel"]) } + spec.Kernel = "traceable:v4" + if _, err := MakeTree(filepath.Join(t.TempDir(), "t"), spec, s); err == nil || !strings.Contains(err.Error(), "listing release v4's files") { + t.Errorf("a release GitHub has not: %v", err) + } spec.Kernel = "traceable:v3" if _, err := MakeTree(filepath.Join(t.TempDir(), "t"), spec, s); err == nil || !strings.Contains(err.Error(), "publishes no traceable kernel") { t.Errorf("a release with none: %v", err) diff --git a/internal/release/fetch_test.go b/internal/release/fetch_test.go index 7d0c4515..0e8035ed 100644 --- a/internal/release/fetch_test.go +++ b/internal/release/fetch_test.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "slices" "strings" "testing" @@ -164,6 +165,22 @@ func TestASignedFileIsTheOneAskedForOnceItsSignatureIsTheReleases(t *testing.T) } } +// A release's files are the names GitHub lists for it, and a listing that fails says which release. +func TestAReleasesAssetsAreTheNamesItPublished(t *testing.T) { + calls := github(t, "", "echo a.vmlinux; echo a.vmlinux.sigstore.json", "") + got, err := Assets(context.Background(), "v1") + if err != nil || !slices.Equal(got, []string{"a.vmlinux", "a.vmlinux.sigstore.json"}) { + t.Errorf("Assets = %q, %v", got, err) + } + if asked := read(t, calls); asked != "gh release view v1 -R spin-stack/machine --json assets -q .assets[].name\n" { + t.Errorf("asked %s", asked) + } + github(t, "", "echo no such release >&2; exit 1", "") + if _, err := Assets(context.Background(), "v9"); err == nil || !strings.Contains(err.Error(), "listing release v9's files") || !strings.Contains(err.Error(), "no such release") { + t.Errorf("Assets with gh failing: %v", err) + } +} + // A release's kernel is its signed tarball's: the version its manifest names and the sources list // it carries, in a dir emptied first. A release from before either is refused by what it lacks. func TestAReleasesKernelIsItsSignedTarballs(t *testing.T) {