From 31b45419ce33e36607c3f05be48e02b29a0769ac Mon Sep 17 00:00:00 2001 From: Shrutiya Date: Thu, 12 Nov 2026 20:32:26 +0000 Subject: [PATCH 1/3] atelet: record cpu_features from runsc cpu-features --bitmask runsc prints its feature set as a bitmask since gVisor release 20261026.0, in the numbering the cpu_features attribute already uses. Probe with --bitmask and encode the blocks directly, so a gVisor feature newer than the name tables no longer fails the probe. The recorded value is unchanged, so snapshots taken before this keep matching. An older runsc fails the probe permanently. ateworker-gvisor needs that release anyway, as the gvisor-default SandboxConfig notes. Signed-off-by: Shrutiya --- cmd/atelet/worker_sandbox_syncer.go | 2 +- cmd/atelet/worker_sandbox_syncer_test.go | 25 +++--- cmd/atelet/worker_sandbox_versions.go | 53 +++++++------ cmd/atelet/worker_sandbox_versions_test.go | 77 ++++++++++--------- internal/hardware/cpufeatures.go | 23 ++++++ internal/hardware/cpufeatures_test.go | 42 ++++++++++ internal/hardware/hardware.go | 7 +- .../ate-install/sandboxconfig-gvisor.yaml | 4 +- 8 files changed, 150 insertions(+), 83 deletions(-) diff --git a/cmd/atelet/worker_sandbox_syncer.go b/cmd/atelet/worker_sandbox_syncer.go index 771c6ccbc0..8a215ac781 100644 --- a/cmd/atelet/worker_sandbox_syncer.go +++ b/cmd/atelet/worker_sandbox_syncer.go @@ -138,7 +138,7 @@ type workerSandboxSyncer struct { // probeCPUIdentity is hardware.ProbeCPUIdentity. Overridable in tests. probeCPUIdentity func() (vendor, model string, err error) // runCPUFeatures is runRunscCPUFeatures. Overridable in tests. - runCPUFeatures func(ctx context.Context, runscPath string) ([]string, error) + runCPUFeatures func(ctx context.Context, runscPath string) (string, error) // mu guards hostCPU, cpuFeatures and verifiedBundles across the // syncWorkers in run: hostCPU is the probed host CPU identity, nil until // a probe succeeds; cpuFeatures is the encoded cpu_features of each diff --git a/cmd/atelet/worker_sandbox_syncer_test.go b/cmd/atelet/worker_sandbox_syncer_test.go index 8bd46bc28c..92f27835ce 100644 --- a/cmd/atelet/worker_sandbox_syncer_test.go +++ b/cmd/atelet/worker_sandbox_syncer_test.go @@ -19,7 +19,6 @@ import ( "errors" "os" "path/filepath" - "runtime" "slices" "strings" "testing" @@ -51,16 +50,12 @@ const ( testCPUModel = "6:143" ) -// testCPUFeatures are the first two CPU feature names for runtime.GOARCH, -// which encode to bit 0 | bit 1 = "3" on both amd64 and arm64. -func testCPUFeatures() []string { - switch runtime.GOARCH { - case "arm64": - return []string{"fp", "asimd"} - default: - return []string{"pni", "pclmulqdq"} - } -} +// testCPUFeaturesBlock is what the faked runsc prints for `cpu-features +// --bitmask`: bits 0 and 1, which encode to testCPUFeaturesMask. +const ( + testCPUFeaturesBlock = "0x00000003" + testCPUFeaturesMask = "3" +) // fakeAssetFetcher answers ensureSandboxAssets from fn. It creates dstDir as // the real fetcher does, but the paths it returns are not under it: the @@ -94,12 +89,12 @@ func newTestAssetFetcher() sandboxAssetFetcher { } // newScriptAssetFetcher returns a sandboxAssetFetcher that backs every asset -// with an executable script printing testCPUFeatures(), for a syncer that +// with an executable script printing testCPUFeaturesBlock, for a syncer that // runs the real runsc probe. func newScriptAssetFetcher(t *testing.T) sandboxAssetFetcher { t.Helper() script := filepath.Join(t.TempDir(), "runsc") - body := "#!/bin/sh\necho " + strings.Join(testCPUFeatures(), ",") + "\n" + body := "#!/bin/sh\necho " + testCPUFeaturesBlock + "\n" if err := os.WriteFile(script, []byte(body), 0o755); err != nil { t.Fatal(err) } @@ -145,7 +140,7 @@ func resolvedPoolOf(class atev1.SandboxClass, name, config string) *atev1.Worker // newTestSyncer serves objs from synced caches and keeps worker directories // under a temp dir. Its probes are faked: the host CPU is -// testCPUVendor/testCPUModel and every runsc reports testCPUFeatures(); +// testCPUVendor/testCPUModel and every runsc reports testCPUFeaturesMask; // TestRunRunscCPUFeatures covers the real probe. func newTestSyncer(t *testing.T, objs ...any) *workerSandboxSyncer { t.Helper() @@ -181,7 +176,7 @@ func newTestSyncer(t *testing.T, objs ...any) *workerSandboxSyncer { t.Cleanup(s.queue.ShutDown) s.scratchDir = t.TempDir() // buildFile stages assets there s.probeCPUIdentity = func() (string, string, error) { return testCPUVendor, testCPUModel, nil } - s.runCPUFeatures = func(context.Context, string) ([]string, error) { return testCPUFeatures(), nil } + s.runCPUFeatures = func(context.Context, string) (string, error) { return testCPUFeaturesMask, nil } // Tests that check the jitter set it. s.maxJitter = 0 return s diff --git a/cmd/atelet/worker_sandbox_versions.go b/cmd/atelet/worker_sandbox_versions.go index de59f1336e..5b139614a9 100644 --- a/cmd/atelet/worker_sandbox_versions.go +++ b/cmd/atelet/worker_sandbox_versions.go @@ -59,9 +59,9 @@ import ( // - the pod has no worker pool label // - the config's default version is missing or has no assets for this // node's architecture -// - the default version's runsc runs but fails, prints no features, or -// prints one this atelet does not know; the binary is content-addressed, -// so running it again gives the same answer +// - the default version's runsc runs but fails, prints nothing, or prints +// what does not encode; the binary is content-addressed, so running it +// again gives the same answer // // The same failures on a restorable version leave it out of the file instead. var errPermanent = errors.New("permanent failure") @@ -414,11 +414,11 @@ func (s *workerSandboxSyncer) ensureMicroVMAssets(ctx context.Context, uid strin } // gvisorCPUFeatures returns the encoded cpu_features of v's runsc binary. It -// stages the version like ensureMicroVMAssets, and runs ` -// cpu-features` the first time this atelet process sees the binary's digest, -// on a runsc linked into scratch for the probe alone, never on the one under -// the worker's directory. The memo is per digest, so a bundle that adds an -// asset beside a known runsc is staged for that asset without running runsc +// stages the version like ensureMicroVMAssets, and probes it with +// runRunscCPUFeatures the first time this atelet process sees the binary's +// digest, on a runsc linked into scratch for the probe alone, never on the one +// under the worker's directory. The memo is per digest, so a bundle that adds +// an asset beside a known runsc is staged for that asset without running runsc // again. func (s *workerSandboxSyncer) gvisorCPUFeatures(ctx context.Context, uid string, v hashedVersion) (string, error) { digest := v.rec.Assets[gvisorAssetName].SHA256 @@ -442,13 +442,10 @@ func (s *workerSandboxSyncer) gvisorCPUFeatures(ctx context.Context, uid string, } defer done() if !probed { - names, err := s.runCPUFeatures(ctx, runscPathFor(paths)) + mask, err = s.runCPUFeatures(ctx, runscPathFor(paths)) if err != nil { return "", err } - if mask, err = hardware.EncodeCPUFeatures(runtime.GOARCH, names); err != nil { - return "", fmt.Errorf("%w: %w", errPermanent, err) - } } } s.mu.Lock() @@ -477,14 +474,18 @@ const probeFetchTimeout = 10 * time.Minute // can shorten it. var cpuFeaturesTimeout = 30 * time.Second -// runRunscCPUFeatures executes ` cpu-features` and returns the -// comma-separated feature names it prints. The binary is content-addressed, -// so one that runs and fails, or prints nothing, would do so again and fails -// permanently; one that cannot be started, or is cut off by ctx, is retried. -func runRunscCPUFeatures(ctx context.Context, runscPath string) ([]string, error) { +// runRunscCPUFeatures executes ` cpu-features --bitmask` and +// returns the cpu_features value its output encodes to. The binary is +// content-addressed, so one that runs and fails, or prints nothing or what +// does not encode, would do so again and fails permanently; one that cannot +// be started, or is cut off by ctx, is retried. runsc has the flag from +// gVisor release 20261026.0, which ateworker-gvisor needs anyway (see +// manifests/ate-install/sandboxconfig-gvisor.yaml), so an older runsc fails +// here with "flag provided but not defined". +func runRunscCPUFeatures(ctx context.Context, runscPath string) (string, error) { ctx, cancel := context.WithTimeout(ctx, cpuFeaturesTimeout) defer cancel() - cmd := exec.CommandContext(ctx, runscPath, "cpu-features") + cmd := exec.CommandContext(ctx, runscPath, "cpu-features", "--bitmask") cmd.Env = []string{} // the probe needs nothing from atelet's environment out, err := cmd.Output() var exitErr *exec.ExitError @@ -493,20 +494,24 @@ func runRunscCPUFeatures(ctx context.Context, runscPath string) ([]string, error case ctx.Err() != nil: // Before the ExitError case: a process killed at the deadline exits // with one too. - return nil, fmt.Errorf("while running %s cpu-features: %w", runscPath, ctx.Err()) + return "", fmt.Errorf("while running %s cpu-features --bitmask: %w", runscPath, ctx.Err()) case errors.As(err, &exitErr): if stderr := strings.TrimSpace(string(exitErr.Stderr)); stderr != "" { - return nil, fmt.Errorf("%w: %s cpu-features: %w (%s)", errPermanent, runscPath, err, stderr) + return "", fmt.Errorf("%w: %s cpu-features --bitmask: %w (%s)", errPermanent, runscPath, err, stderr) } - return nil, fmt.Errorf("%w: %s cpu-features: %w", errPermanent, runscPath, err) + return "", fmt.Errorf("%w: %s cpu-features --bitmask: %w", errPermanent, runscPath, err) default: - return nil, fmt.Errorf("while starting %s cpu-features: %w", runscPath, err) + return "", fmt.Errorf("while starting %s cpu-features --bitmask: %w", runscPath, err) } printed := strings.TrimSpace(string(out)) if printed == "" { - return nil, fmt.Errorf("%w: %s cpu-features printed no features", errPermanent, runscPath) + return "", fmt.Errorf("%w: %s cpu-features --bitmask printed nothing", errPermanent, runscPath) + } + mask, err := hardware.EncodeCPUFeatureMask(printed) + if err != nil { + return "", fmt.Errorf("%w: %s cpu-features --bitmask: %w", errPermanent, runscPath, err) } - return strings.Split(printed, ","), nil + return mask, nil } // bundleHash is the sandbox_bundle_hash of a version: a sha256 over one diff --git a/cmd/atelet/worker_sandbox_versions_test.go b/cmd/atelet/worker_sandbox_versions_test.go index 735d080b74..da4a769b25 100644 --- a/cmd/atelet/worker_sandbox_versions_test.go +++ b/cmd/atelet/worker_sandbox_versions_test.go @@ -250,9 +250,9 @@ func TestWorkerSandboxResolverResolve(t *testing.T) { } // wantRuntime is the runtime buildFile builds from multiVersionConfig's -// version with the given name when runsc reports testCPUFeatures() ("3"). +// version with the given name when the probe reports testCPUFeaturesMask. func wantRuntime(name string) sandboxruntimes.Attributes { - return wantRuntimeWithFeatures(name, "3") + return wantRuntimeWithFeatures(name, testCPUFeaturesMask) } func wantRuntimeWithFeatures(name, cpuFeatures string) sandboxruntimes.Attributes { @@ -723,8 +723,8 @@ func TestSyncPodAssetCacheAndEviction(t *testing.T) { s.probeCPUIdentity = func() (string, string, error) { return "", "", errors.New("re-probed memoized host CPU identity") } - s.runCPUFeatures = func(context.Context, string) ([]string, error) { - return nil, errors.New("re-ran runsc cpu-features for a memoized digest") + s.runCPUFeatures = func(context.Context, string) (string, error) { + return "", errors.New("re-ran runsc cpu-features for a memoized digest") } if _, err := s.syncPod(ctx, workerPod("pod-a", poolName)); err != nil { t.Fatalf("syncPod(pod-a) after bucket recovery: %v", err) @@ -781,9 +781,9 @@ func TestSyncPodAssetCacheAndEviction(t *testing.T) { probed = append(probed, "host") return testCPUVendor, testCPUModel, nil } - s.runCPUFeatures = func(_ context.Context, runscPath string) ([]string, error) { + s.runCPUFeatures = func(_ context.Context, runscPath string) (string, error) { probed = append(probed, runscPath) - return testCPUFeatures(), nil + return testCPUFeaturesMask, nil } if _, err := s.syncPod(ctx, workerPod("pod-a", poolName)); err != nil { t.Fatalf("syncPod(pod-a) in a new process: %v", err) @@ -876,7 +876,7 @@ func TestSyncPodPinsDefaultRuntime(t *testing.T) { // A build that fails on the default's probe writes nothing and leaves // the previous default's directory as it was. s = newTestSyncer(t, pod, resolvedPool("pool", "gvisor-release"), multiVersionConfig()) - s.runCPUFeatures = func(context.Context, string) ([]string, error) { return nil, errors.New("runsc failed") } + s.runCPUFeatures = func(context.Context, string) (string, error) { return "", errors.New("runsc failed") } previous := s.defaultRuntimeDir("pod-a", "sha256:previous") if err := os.MkdirAll(previous, 0o700); err != nil { t.Fatal(err) @@ -896,8 +896,6 @@ func TestSyncPodPinsDefaultRuntime(t *testing.T) { } func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) { - feats := testCPUFeatures() - t.Run("a version with an earlier version's binaries in another bucket is neither fetched nor probed", func(t *testing.T) { // bundleHash leaves URLs out, so v3 is v1's runtime. The syncer must // see that from the spec alone, before touching v3's bucket. @@ -946,12 +944,12 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) { }, } execCalls := make(map[string]int) - s.runCPUFeatures = func(_ context.Context, runscPath string) ([]string, error) { + s.runCPUFeatures = func(_ context.Context, runscPath string) (string, error) { execCalls[runscPath]++ if runscPath == "/cache/sha-v2/runsc" { - return feats, nil // bits 0 and 1 -> "3" + return testCPUFeaturesMask, nil // bits 0 and 1 } - return feats[:1], nil // bit 0 only -> "1" + return "1", nil // bit 0 only } got, err := planAndBuild(s, workerPod("pod-a", "pool")) @@ -959,7 +957,7 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) { t.Fatalf("first planAndBuild() error = %v", err) } want := gvisorFile( - wantRuntimeWithFeatures("v2", "3"), + wantRuntimeWithFeatures("v2", testCPUFeaturesMask), wantRuntimeWithFeatures("v1", "1"), sandboxruntimes.Attributes{ hardware.AttrArchitecture: runtime.GOARCH, @@ -1008,8 +1006,8 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) { } s = newTestSyncer(t, resolvedPool("pool", "gvisor-release"), multiVersionConfig()) - s.runCPUFeatures = func(context.Context, string) ([]string, error) { - return nil, errors.New("runsc failed") + s.runCPUFeatures = func(context.Context, string) (string, error) { + return "", errors.New("runsc failed") } if _, err := planAndBuild(s, workerPod("pod-a", "pool")); err == nil || errors.Is(err, errPermanent) { t.Errorf("planAndBuild() on runCPUFeatures error = %v, want non-permanent error", err) @@ -1027,7 +1025,7 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) { return map[string]string{gvisorAssetName: "sha-v2"}, nil }, } - s.runCPUFeatures = func(context.Context, string) ([]string, error) { return feats, nil } + s.runCPUFeatures = func(context.Context, string) (string, error) { return testCPUFeaturesMask, nil } got, err := planAndBuild(s, workerPod("pod-a", "pool")) if !errors.Is(err, errIncomplete) || errors.Is(err, errPermanent) { t.Errorf("planAndBuild() on a restorable version's fetch error = %v, want errIncomplete and not errPermanent", err) @@ -1038,13 +1036,14 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) { } }) - t.Run("unknown CPU feature on default version is permanent, on non-default is skipped", func(t *testing.T) { + t.Run("permanent probe failure on default version fails the build, on non-default is skipped", func(t *testing.T) { + // What runRunscCPUFeatures returns for a runsc that prints what does + // not encode. + probeErr := fmt.Errorf("%w: runsc cpu-features --bitmask: not a bitmask", errPermanent) s := newTestSyncer(t, resolvedPool("pool", "gvisor-release"), multiVersionConfig()) - s.runCPUFeatures = func(context.Context, string) ([]string, error) { - return []string{"not_a_known_feature"}, nil - } + s.runCPUFeatures = func(context.Context, string) (string, error) { return "", probeErr } if _, err := planAndBuild(s, workerPod("pod-a", "pool")); !errors.Is(err, errPermanent) { - t.Errorf("planAndBuild() with unknown feature on default = %v, want errPermanent", err) + t.Errorf("planAndBuild() with the default's probe failing permanently = %v, want errPermanent", err) } s = newTestSyncer(t, resolvedPool("pool", "gvisor-release"), multiVersionConfig()) @@ -1053,15 +1052,15 @@ func TestGVisorCPUFeaturesMemoizationAndErrors(t *testing.T) { return map[string]string{gvisorAssetName: rec.Assets[gvisorAssetName].SHA256}, nil }, } - s.runCPUFeatures = func(_ context.Context, runscPath string) ([]string, error) { + s.runCPUFeatures = func(_ context.Context, runscPath string) (string, error) { if runscPath == "sha-v2" { - return feats, nil + return testCPUFeaturesMask, nil } - return []string{"future_unknown_feature"}, nil + return "", probeErr } got, err := planAndBuild(s, workerPod("pod-a", "pool")) if err != nil { - t.Fatalf("planAndBuild() with unknown feature on non-default error = %v", err) + t.Fatalf("planAndBuild() with a restorable version's probe failing permanently error = %v", err) } want := gvisorFile(wantRuntime("v2")) if diff := cmp.Diff(want, got); diff != "" { @@ -1081,24 +1080,24 @@ func TestRunRunscCPUFeatures(t *testing.T) { } return p } - - got, err := runRunscCPUFeatures(ctx, script("ok", "echo pni,avx2\n")) - if err != nil { - t.Fatalf("runRunscCPUFeatures() error = %v", err) - } - if diff := cmp.Diff([]string{"pni", "avx2"}, got); diff != "" { - t.Errorf("runRunscCPUFeatures() mismatch (-want +got):\n%s", diff) + got, err := runRunscCPUFeatures(ctx, script("bitmask", "echo "+testCPUFeaturesBlock+"\n")) + if err != nil || got != testCPUFeaturesMask { + t.Errorf("runRunscCPUFeatures() = (%q, %v), want (%q, nil)", got, err, testCPUFeaturesMask) } - // A binary that runs and fails, or prints nothing, will do so again: - // permanent, with its stderr in the error. - _, err = runRunscCPUFeatures(ctx, script("fail", "echo boom >&2\nexit 1\n")) - if !errors.Is(err, errPermanent) || !strings.Contains(err.Error(), "boom") { - t.Errorf("runRunscCPUFeatures() on a failing binary = %v, want permanent error quoting boom", err) + // A binary that runs and fails, prints nothing, or prints what does not + // encode will do so again: permanent, with its stderr in the error. A + // runsc from before --bitmask is the first kind. + _, err = runRunscCPUFeatures(ctx, script("old", "echo 'flag provided but not defined: -bitmask' >&2\nexit 128\n")) + if !errors.Is(err, errPermanent) || !strings.Contains(err.Error(), "not defined: -bitmask") { + t.Errorf("runRunscCPUFeatures() on a runsc without --bitmask = %v, want permanent error quoting its stderr", err) } if _, err := runRunscCPUFeatures(ctx, script("empty", "exit 0\n")); !errors.Is(err, errPermanent) { t.Errorf("runRunscCPUFeatures() on empty output = %v, want permanent error", err) } + if _, err := runRunscCPUFeatures(ctx, script("names", "echo fpu,sse2\n")); !errors.Is(err, errPermanent) { + t.Errorf("runRunscCPUFeatures() on names instead of blocks = %v, want permanent error", err) + } // A binary that cannot be started, or is cut off, is retried. if _, err := runRunscCPUFeatures(ctx, filepath.Join(dir, "missing")); err == nil || errors.Is(err, errPermanent) { @@ -1149,7 +1148,9 @@ func TestSyncPodAdvertisesWhatItWrote(t *testing.T) { s.mu.Lock() s.cpuFeatures = map[string]string{} s.mu.Unlock() - s.runCPUFeatures = func(context.Context, string) ([]string, error) { return []string{"not_a_known_feature"}, nil } + s.runCPUFeatures = func(context.Context, string) (string, error) { + return "", fmt.Errorf("%w: runsc cpu-features --bitmask: not a bitmask", errPermanent) + } if _, err := s.syncPod(ctx, pod); !errors.Is(err, errPermanent) { t.Fatalf("syncPod with the default's probe failing = %v, want errPermanent", err) } diff --git a/internal/hardware/cpufeatures.go b/internal/hardware/cpufeatures.go index d525c8a634..c6fb797c36 100644 --- a/internal/hardware/cpufeatures.go +++ b/internal/hardware/cpufeatures.go @@ -140,6 +140,29 @@ func parseFeatureMask(s string) (featureMask, bool) { return m, true } +// EncodeCPUFeatureMask encodes the blocks `runsc cpu-features --bitmask` +// prints as a cpu_features value: one "0x%08x" uint32 per 32 features, +// lowest first, as many as the binary's newest feature needs, so trailing +// zero blocks change nothing. More blocks than featureMask holds is an error. +func EncodeCPUFeatureMask(blocks string) (string, error) { + var m featureMask + for i, block := range strings.Split(strings.TrimSpace(blocks), ",") { + if i >= featureMaskWords*2 { + return "", fmt.Errorf("cpu_features bitmask %q has more than %d blocks", blocks, featureMaskWords*2) + } + digits, ok := strings.CutPrefix(block, "0x") + if !ok { + return "", fmt.Errorf("cpu_features bitmask block %q lacks the 0x prefix", block) + } + v, err := strconv.ParseUint(digits, 16, 32) + if err != nil { + return "", fmt.Errorf("cpu_features bitmask block %q: %w", block, err) + } + m[i/2] |= v << (32 * (i % 2)) + } + return m.String(), nil +} + // EncodeCPUFeatures encodes the feature names `runsc cpu-features` printed // for arch as a cpu_features value. Blank names are skipped; duplicates and // order do not matter. An unknown name is an error rather than a dropped diff --git a/internal/hardware/cpufeatures_test.go b/internal/hardware/cpufeatures_test.go index 5db4b05d91..6ea4d84a82 100644 --- a/internal/hardware/cpufeatures_test.go +++ b/internal/hardware/cpufeatures_test.go @@ -133,6 +133,48 @@ func TestEncodeDecodeCPUFeatures(t *testing.T) { }) } +// TestEncodeCPUFeatureMask pins the cpu_features value of real `runsc +// cpu-features --bitmask` output, which stored snapshots must keep matching, +// and checks the edges of the block format. +func TestEncodeCPUFeatureMask(t *testing.T) { + // runsc 20261102.0 on an Intel host: block 0 is CPUID.1:ECX (pni is bit + // 0), block 1 CPUID.1:EDX (fpu is bit 0, mask bit 32), and so on, with + // block 8 empty. Its names form, through the tables EncodeCPUFeatures + // maps them with, encodes to the same value. + const ( + blocks = "0xfefa3203,0x1f8bfbff,0x001c2ffb,0x00000000,0x00000001,0x00000121,0x2c100800,0xac000400,0x00000000" + want = "ac0004002c100800000001210000000100000000001c2ffb1f8bfbfffefa3203" + ) + if got, err := EncodeCPUFeatureMask(blocks); err != nil || got != want { + t.Errorf("EncodeCPUFeatureMask(%q) = (%q, %v), want (%q, nil)", blocks, got, err, want) + } + + for _, tc := range []struct{ blocks, want string }{ + {"0x00000000", "0"}, + {"0x00000003\n", "3"}, + {"0x00000003,0x00000000,0x00000000", "3"}, // trailing zero blocks + {"0x00000001,0x00000000,0x00000020", "20" + strings.Repeat("0", 15) + "1"}, // bits 0 and 69: a word boundary crossed + {strings.Repeat("0x00000000,", featureMaskWords*2-1) + "0x80000000", "8" + strings.Repeat("0", featureMaskWords*16-1)}, + } { + if got, err := EncodeCPUFeatureMask(tc.blocks); err != nil || got != tc.want { + t.Errorf("EncodeCPUFeatureMask(%q) = (%q, %v), want (%q, nil)", tc.blocks, got, err, tc.want) + } + } + for _, bad := range []string{ + "", + "3", + "0x", + "0xg", + "0x100000000", + "0x00000001,", + strings.Repeat("0x00000000,", featureMaskWords*2) + "0x00000001", + } { + if got, err := EncodeCPUFeatureMask(bad); err == nil { + t.Errorf("EncodeCPUFeatureMask(%q) = %q, want an error", bad, got) + } + } +} + // TestCPUFeatureTables checks structural invariants of each architecture's // feature table and pins anchor bits to the values of gVisor's Feature // constants (32*block + CPUID bit on amd64). A moved anchor means diff --git a/internal/hardware/hardware.go b/internal/hardware/hardware.go index 2c35548901..edce7444ec 100644 --- a/internal/hardware/hardware.go +++ b/internal/hardware/hardware.go @@ -34,10 +34,9 @@ const ( // sha256 over a SandboxConfig version's asset digests, computed by atelet. AttrSandboxBundleHash = "sandbox_bundle_hash" - // AttrCPUFeatures is the set of CPU features `runsc cpu-features` reports - // for a gVisor runtime, encoded by EncodeCPUFeatures as a hex bitmask over - // the architecture's feature table. A snapshot's set must be a subset of - // the worker's. + // AttrCPUFeatures is the set of CPU features `runsc cpu-features --bitmask` + // reports for a gVisor runtime, as the hex bitmask EncodeCPUFeatureMask + // returns. A snapshot's set must be a subset of the worker's. AttrCPUFeatures = "cpu_features" // AttrCPUVendor identifies the host CPU vendor (e.g. "GenuineIntel", diff --git a/manifests/ate-install/sandboxconfig-gvisor.yaml b/manifests/ate-install/sandboxconfig-gvisor.yaml index 09e4c21119..e75ff621c7 100644 --- a/manifests/ate-install/sandboxconfig-gvisor.yaml +++ b/manifests/ate-install/sandboxconfig-gvisor.yaml @@ -38,7 +38,9 @@ spec: # needs google/gvisor@b79256080dec, first in tagged release 20261019.0. # ateworker-gvisor also runs the sandbox with no root container # (google/gvisor#15551). This release's restore picks each container's init - # process correctly in such a sandbox (google/gvisor@29b97c66d945). + # process correctly in such a sandbox (google/gvisor@29b97c66d945). atelet + # records a version's cpu_features with `runsc cpu-features --bitmask`, also + # first in this release (google/gvisor@3fdb673dc0a3). - name: v6 state: Enabled assets: From ec2b3682d38f05ae6218ae365c32d4293e586899 Mon Sep 17 00:00:00 2001 From: Shrutiya Date: Thu, 12 Nov 2026 20:33:26 +0000 Subject: [PATCH 2/3] hardware: remove the CPU feature name tables atelet no longer maps runsc feature names to bits, so the tables, EncodeCPUFeatures and DecodeCPUFeatures have no callers outside their tests. Signed-off-by: Shrutiya --- internal/hardware/cpufeatures.go | 115 +---------- internal/hardware/cpufeatures_table.go | 244 ---------------------- internal/hardware/cpufeatures_test.go | 267 ++----------------------- internal/hardware/hardware_test.go | 27 +-- 4 files changed, 40 insertions(+), 613 deletions(-) delete mode 100644 internal/hardware/cpufeatures_table.go diff --git a/internal/hardware/cpufeatures.go b/internal/hardware/cpufeatures.go index c6fb797c36..352874f9e7 100644 --- a/internal/hardware/cpufeatures.go +++ b/internal/hardware/cpufeatures.go @@ -18,8 +18,6 @@ import ( "bufio" "fmt" "io" - "maps" - "math/bits" "os" "runtime" "slices" @@ -27,60 +25,19 @@ import ( "strings" ) -// cpuFeatureTables maps each supported architecture to its CPU feature table -// from cpufeatures_table.go, whose header says where the tables come from -// and what a bit means. -var cpuFeatureTables = map[string]featureTable{ - "amd64": newFeatureTable(amd64CPUFeatures), - "arm64": newFeatureTable(arm64CPUFeatures), -} - -// featureTable is one architecture's CPU feature names by bit, with the -// reverse lookup EncodeCPUFeatures uses and the set of bits that have a -// name, which DecodeCPUFeatures validates against. -type featureTable struct { - names map[int]string - bits []int // the keys of names, ascending - index map[string]int - defined featureMask -} - -// newFeatureTable indexes a table. A bit outside featureMask is a table -// error, so it is a panic rather than a dropped name. -func newFeatureTable(names map[int]string) featureTable { - t := featureTable{ - names: names, - bits: slices.Sorted(maps.Keys(names)), - index: make(map[string]int, len(names)), - } - for bit, name := range names { - if bit < 0 || bit >= featureMaskWords*64 { - panic(fmt.Sprintf("hardware: CPU feature %q has bit %d outside a %d-bit featureMask", name, bit, featureMaskWords*64)) - } - t.defined.set(bit) - t.index[name] = bit - } - return t -} - // featureMaskWords is the width of a cpu_features bitmask in 64-bit words. // gVisor's amd64 blocks 0-8 use bits up to 261, and 6 words leave room for // blocks through 11 within SchemaVersionV1. const featureMaskWords = 6 -// featureMask is a decoded cpu_features value: bit i set means the feature at -// bit i of the architecture's table is present. It is a fixed-size array so -// matching on the scheduler's hot path allocates nothing. +// featureMask is a decoded cpu_features value: bit i set means gVisor's +// Feature i is present. That number is fixed by the hardware on amd64, 32 * +// CPUID block + the feature's bit in that block's register, and declared by +// gVisor on arm64, so a bit never moves between gVisor versions and masks +// from different runsc binaries compare. It is a fixed-size array so matching +// on the scheduler's hot path allocates nothing. type featureMask [featureMaskWords]uint64 -func (m *featureMask) set(bit int) { - m[bit/64] |= 1 << (bit % 64) -} - -func (m featureMask) has(bit int) bool { - return m[bit/64]&(1<<(bit%64)) != 0 -} - // subsetOf reports whether every bit set in m is also set in w. func (m featureMask) subsetOf(w featureMask) bool { for i := range m { @@ -91,17 +48,6 @@ func (m featureMask) subsetOf(w featureMask) bool { return true } -// firstNotIn is the lowest bit set in m but not in w, or -1 if m is a -// subset of w. -func (m featureMask) firstNotIn(w featureMask) int { - for i := range m { - if extra := m[i] &^ w[i]; extra != 0 { - return i*64 + bits.TrailingZeros64(extra) - } - } - return -1 -} - // String is the canonical encoding: lowercase hex, no prefix, no leading // zeros, "0" for the empty mask. Equal sets encode to equal strings, which // is what proto equality on a recorded runtime compares. @@ -163,55 +109,6 @@ func EncodeCPUFeatureMask(blocks string) (string, error) { return m.String(), nil } -// EncodeCPUFeatures encodes the feature names `runsc cpu-features` printed -// for arch as a cpu_features value. Blank names are skipped; duplicates and -// order do not matter. An unknown name is an error rather than a dropped -// bit: a snapshot encoded without a feature its sandbox used would match -// workers that lack it. -func EncodeCPUFeatures(arch string, features []string) (string, error) { - table, ok := cpuFeatureTables[arch] - if !ok { - return "", fmt.Errorf("unsupported architecture %q for cpu_features", arch) - } - var m featureMask - for _, raw := range features { - name := strings.TrimSpace(raw) - if name == "" { - continue - } - bit, known := table.index[name] - if !known { - return "", fmt.Errorf("unknown %s CPU feature %q", arch, name) - } - m.set(bit) - } - return m.String(), nil -} - -// DecodeCPUFeatures returns the feature names a cpu_features value encodes -// for arch, in bit order. A set bit with no name is an error: the hardware -// reserves it, so no encoder of this schema could have produced the value. -func DecodeCPUFeatures(arch, value string) ([]string, error) { - table, ok := cpuFeatureTables[arch] - if !ok { - return nil, fmt.Errorf("unsupported architecture %q for cpu_features", arch) - } - m, ok := parseFeatureMask(value) - if !ok { - return nil, fmt.Errorf("invalid cpu_features value %q", value) - } - if bit := m.firstNotIn(table.defined); bit >= 0 { - return nil, fmt.Errorf("cpu_features value %q sets bit %d, which is not a known %s feature", value, bit, arch) - } - var out []string - for _, bit := range table.bits { - if m.has(bit) { - out = append(out, table.names[bit]) - } - } - return out, nil -} - // ProbeCPUIdentity reads /proc/cpuinfo for the host CPU's vendor and model: // the AttrCPUVendor and AttrCPUModel values of a microVM runtime. func ProbeCPUIdentity() (vendor, model string, err error) { diff --git a/internal/hardware/cpufeatures_table.go b/internal/hardware/cpufeatures_table.go deleted file mode 100644 index d698832600..0000000000 --- a/internal/hardware/cpufeatures_table.go +++ /dev/null @@ -1,244 +0,0 @@ -// Copyright 2026 The Agent Substrate Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package hardware - -// CPU feature tables for the cpu_features compatibility attribute. -// -// `runsc cpu-features` prints the features a gVisor sandbox exposes by name, -// and the control plane needs them as a bitmask it can compare by subset. -// Each table maps a bit to the name gVisor prints for it. A bit is the value -// of the gVisor Feature constant named in the entry's comment: on amd64, -// 32*block + the feature's bit in that block's CPUID register, which the -// hardware fixes; on arm64, the value gVisor declares. A bit therefore never -// moves between gVisor versions, and a bit with no entry is one the hardware -// reserves. -// -// The tables were produced once from gVisor's pkg/cpuid sources at -// gvisor.dev/gvisor@v0.0.0-20261014212059-ddc37c698141: for every constant of -// type Feature in features_amd64.go and features_arm64.go with an entry in -// that file's allFeatures map, the constant's value (evaluated with go/types) -// keyed to the entry's display name. To take features from a newer gVisor, -// add its new constants the same way; existing entries do not change. -// -// TODO: Remove the tables, EncodeCPUFeatures, and DecodeCPUFeatures once -// runsc can print its feature set as this bitmask itself, so atelet records -// the value directly instead of mapping names. The mask format and the -// subset matching stay. -var ( - amd64CPUFeatures = map[int]string{ - 0: "pni", // X86FeatureSSE3 (block 0, bit 0) - 1: "pclmulqdq", // X86FeaturePCLMULDQ (block 0, bit 1) - 2: "dtes64", // X86FeatureDTES64 (block 0, bit 2) - 3: "monitor", // X86FeatureMONITOR (block 0, bit 3) - 4: "ds_cpl", // X86FeatureDSCPL (block 0, bit 4) - 5: "vmx", // X86FeatureVMX (block 0, bit 5) - 6: "smx", // X86FeatureSMX (block 0, bit 6) - 7: "est", // X86FeatureEST (block 0, bit 7) - 8: "tm2", // X86FeatureTM2 (block 0, bit 8) - 9: "ssse3", // X86FeatureSSSE3 (block 0, bit 9) - 10: "cid", // X86FeatureCNXTID (block 0, bit 10) - 11: "sdbg", // X86FeatureSDBG (block 0, bit 11) - 12: "fma", // X86FeatureFMA (block 0, bit 12) - 13: "cx16", // X86FeatureCX16 (block 0, bit 13) - 14: "xtpr", // X86FeatureXTPR (block 0, bit 14) - 15: "pdcm", // X86FeaturePDCM (block 0, bit 15) - 17: "pcid", // X86FeaturePCID (block 0, bit 17) - 18: "dca", // X86FeatureDCA (block 0, bit 18) - 19: "sse4_1", // X86FeatureSSE4_1 (block 0, bit 19) - 20: "sse4_2", // X86FeatureSSE4_2 (block 0, bit 20) - 21: "x2apic", // X86FeatureX2APIC (block 0, bit 21) - 22: "movbe", // X86FeatureMOVBE (block 0, bit 22) - 23: "popcnt", // X86FeaturePOPCNT (block 0, bit 23) - 24: "tsc_deadline_timer", // X86FeatureTSCD (block 0, bit 24) - 25: "aes", // X86FeatureAES (block 0, bit 25) - 26: "xsave", // X86FeatureXSAVE (block 0, bit 26) - 27: "osxsave", // X86FeatureOSXSAVE (block 0, bit 27) - 28: "avx", // X86FeatureAVX (block 0, bit 28) - 29: "f16c", // X86FeatureF16C (block 0, bit 29) - 30: "rdrand", // X86FeatureRDRAND (block 0, bit 30) - 31: "hypervisor", // X86FeatureHypervisor (block 0, bit 31) - 32: "fpu", // X86FeatureFPU (block 1, bit 0) - 33: "vme", // X86FeatureVME (block 1, bit 1) - 34: "de", // X86FeatureDE (block 1, bit 2) - 35: "pse", // X86FeaturePSE (block 1, bit 3) - 36: "tsc", // X86FeatureTSC (block 1, bit 4) - 37: "msr", // X86FeatureMSR (block 1, bit 5) - 38: "pae", // X86FeaturePAE (block 1, bit 6) - 39: "mce", // X86FeatureMCE (block 1, bit 7) - 40: "cx8", // X86FeatureCX8 (block 1, bit 8) - 41: "apic", // X86FeatureAPIC (block 1, bit 9) - 43: "sep", // X86FeatureSEP (block 1, bit 11) - 44: "mtrr", // X86FeatureMTRR (block 1, bit 12) - 45: "pge", // X86FeaturePGE (block 1, bit 13) - 46: "mca", // X86FeatureMCA (block 1, bit 14) - 47: "cmov", // X86FeatureCMOV (block 1, bit 15) - 48: "pat", // X86FeaturePAT (block 1, bit 16) - 49: "pse36", // X86FeaturePSE36 (block 1, bit 17) - 50: "pn", // X86FeaturePSN (block 1, bit 18) - 51: "clflush", // X86FeatureCLFSH (block 1, bit 19) - 53: "dts", // X86FeatureDS (block 1, bit 21) - 54: "acpi", // X86FeatureACPI (block 1, bit 22) - 55: "mmx", // X86FeatureMMX (block 1, bit 23) - 56: "fxsr", // X86FeatureFXSR (block 1, bit 24) - 57: "sse", // X86FeatureSSE (block 1, bit 25) - 58: "sse2", // X86FeatureSSE2 (block 1, bit 26) - 59: "ss", // X86FeatureSS (block 1, bit 27) - 60: "ht", // X86FeatureHTT (block 1, bit 28) - 61: "tm", // X86FeatureTM (block 1, bit 29) - 62: "ia64", // X86FeatureIA64 (block 1, bit 30) - 63: "pbe", // X86FeaturePBE (block 1, bit 31) - 64: "fsgsbase", // X86FeatureFSGSBase (block 2, bit 0) - 65: "tsc_adjust", // X86FeatureTSC_ADJUST (block 2, bit 1) - 67: "bmi1", // X86FeatureBMI1 (block 2, bit 3) - 68: "hle", // X86FeatureHLE (block 2, bit 4) - 69: "avx2", // X86FeatureAVX2 (block 2, bit 5) - 70: "fdp_excptn_only", // X86FeatureFDP_EXCPTN_ONLY (block 2, bit 6) - 71: "smep", // X86FeatureSMEP (block 2, bit 7) - 72: "bmi2", // X86FeatureBMI2 (block 2, bit 8) - 73: "erms", // X86FeatureERMS (block 2, bit 9) - 74: "invpcid", // X86FeatureINVPCID (block 2, bit 10) - 75: "rtm", // X86FeatureRTM (block 2, bit 11) - 76: "cqm", // X86FeatureCQM (block 2, bit 12) - 77: "fpcsds", // X86FeatureFPCSDS (block 2, bit 13) - 78: "mpx", // X86FeatureMPX (block 2, bit 14) - 79: "rdt_a", // X86FeatureRDT (block 2, bit 15) - 80: "avx512f", // X86FeatureAVX512F (block 2, bit 16) - 81: "avx512dq", // X86FeatureAVX512DQ (block 2, bit 17) - 82: "rdseed", // X86FeatureRDSEED (block 2, bit 18) - 83: "adx", // X86FeatureADX (block 2, bit 19) - 84: "smap", // X86FeatureSMAP (block 2, bit 20) - 87: "clfushopt", // X86FeatureCLFLUSHOPT (block 2, bit 23) - 88: "clwb", // X86FeatureCLWB (block 2, bit 24) - 89: "ipt", // X86FeatureIPT (block 2, bit 25) - 90: "avx512pf", // X86FeatureAVX512PF (block 2, bit 26) - 91: "avx512er", // X86FeatureAVX512ER (block 2, bit 27) - 92: "avx512cd", // X86FeatureAVX512CD (block 2, bit 28) - 93: "sha_ni", // X86FeatureSHA (block 2, bit 29) - 94: "avx512bw", // X86FeatureAVX512BW (block 2, bit 30) - 95: "avx512vl", // X86FeatureAVX512VL (block 2, bit 31) - 96: "prefetchwt1", // X86FeaturePREFETCHWT1 (block 3, bit 0) - 97: "avx512vbmi", // X86FeatureAVX512VBMI (block 3, bit 1) - 98: "umip", // X86FeatureUMIP (block 3, bit 2) - 99: "pku", // X86FeaturePKU (block 3, bit 3) - 100: "ospke", // X86FeatureOSPKE (block 3, bit 4) - 101: "waitpkg", // X86FeatureWAITPKG (block 3, bit 5) - 102: "avx512_vbmi2", // X86FeatureAVX512_VBMI2 (block 3, bit 6) - 103: "cet_ss", // X86FeatureCET_SS (block 3, bit 7) - 104: "gfni", // X86FeatureGFNI (block 3, bit 8) - 105: "vaes", // X86FeatureVAES (block 3, bit 9) - 106: "vpclmulqdq", // X86FeatureVPCLMULQDQ (block 3, bit 10) - 107: "avx512_vnni", // X86FeatureAVX512_VNNI (block 3, bit 11) - 108: "avx512_bitalg", // X86FeatureAVX512_BITALG (block 3, bit 12) - 109: "tme", // X86FeatureTME (block 3, bit 13) - 110: "avx512_vpopcntdq", // X86FeatureAVX512_VPOPCNTDQ (block 3, bit 14) - 112: "la57", // X86FeatureLA57 (block 3, bit 16) - 118: "rdpid", // X86FeatureRDPID (block 3, bit 22) - 121: "cldemote", // X86FeatureCLDEMOTE (block 3, bit 25) - 123: "movdiri", // X86FeatureMOVDIRI (block 3, bit 27) - 124: "movdir64b", // X86FeatureMOVDIR64B (block 3, bit 28) - 128: "xsaveopt", // X86FeatureXSAVEOPT (block 4, bit 0) - 129: "xsavec", // X86FeatureXSAVEC (block 4, bit 1) - 130: "xgetbv1", // X86FeatureXGETBV1 (block 4, bit 2) - 131: "xsaves", // X86FeatureXSAVES (block 4, bit 3) - 160: "lahf_lm", // X86FeatureLAHF64 (block 5, bit 0) - 161: "cmp_legacy", // X86FeatureCMP_LEGACY (block 5, bit 1) - 162: "svm", // X86FeatureSVM (block 5, bit 2) - 163: "extapic", // X86FeatureEXTAPIC (block 5, bit 3) - 164: "cr8_legacy", // X86FeatureCR8_LEGACY (block 5, bit 4) - 165: "abm", // X86FeatureLZCNT (block 5, bit 5) - 166: "sse4a", // X86FeatureSSE4A (block 5, bit 6) - 167: "misalignsse", // X86FeatureMISALIGNSSE (block 5, bit 7) - 168: "3dnowprefetch", // X86FeaturePREFETCHW (block 5, bit 8) - 169: "osvw", // X86FeatureOSVW (block 5, bit 9) - 170: "ibs", // X86FeatureIBS (block 5, bit 10) - 171: "xop", // X86FeatureXOP (block 5, bit 11) - 172: "skinit", // X86FeatureSKINIT (block 5, bit 12) - 173: "wdt", // X86FeatureWDT (block 5, bit 13) - 175: "lwp", // X86FeatureLWP (block 5, bit 15) - 176: "fma4", // X86FeatureFMA4 (block 5, bit 16) - 177: "tce", // X86FeatureTCE (block 5, bit 17) - 181: "tbm", // X86FeatureTBM (block 5, bit 21) - 182: "topoext", // X86FeatureTOPOLOGY (block 5, bit 22) - 183: "perfctr_core", // X86FeaturePERFCTR_CORE (block 5, bit 23) - 184: "perfctr_nb", // X86FeaturePERFCTR_NB (block 5, bit 24) - 186: "bpext", // X86FeatureBPEXT (block 5, bit 26) - 187: "ptsc", // X86FeaturePERFCTR_TSC (block 5, bit 27) - 188: "perfctr_llc", // X86FeaturePERFCTR_LLC (block 5, bit 28) - 189: "mwaitx", // X86FeatureMWAITX (block 5, bit 29) - 190: "ad_mask_extn", // X86FeatureADMSKEXTN (block 5, bit 30) - 203: "syscall", // X86FeatureSYSCALL (block 6, bit 11) - 212: "nx", // X86FeatureNX (block 6, bit 20) - 214: "mmxext", // X86FeatureMMXEXT (block 6, bit 22) - 217: "fxsr_opt", // X86FeatureFXSR_OPT (block 6, bit 25) - 218: "pdpe1gb", // X86FeatureGBPAGES (block 6, bit 26) - 219: "rdtscp", // X86FeatureRDTSCP (block 6, bit 27) - 221: "lm", // X86FeatureLM (block 6, bit 29) - 222: "3dnowext", // X86Feature3DNOWEXT (block 6, bit 30) - 223: "3dnow", // X86Feature3DNOW (block 6, bit 31) - 226: "avx512_4vnniw", // X86FeatureAVX512_4VNNIW (block 7, bit 2) - 227: "avx512_4fmaps", // X86FeatureAVX512_4FMAPS (block 7, bit 3) - 228: "fsrm", // X86FeatureFSRM (block 7, bit 4) - 232: "avx512_vp2intersect", // X86FeatureAVX512_VP2INTERSECT (block 7, bit 8) - 233: "srbds_ctrl", // X86FeatureSRBDS_CTRL (block 7, bit 9) - 234: "md_clear", // X86FeatureMD_CLEAR (block 7, bit 10) - 235: "rtm_always_abort", // X86FeatureRTM_ALWAYS_ABORT (block 7, bit 11) - 237: "tsx_force_abort", // X86FeatureTSX_FORCE_ABORT (block 7, bit 13) - 238: "serialize", // X86FeatureSERIALIZE (block 7, bit 14) - 239: "hybrid_cpu", // X86FeatureHYBRID_CPU (block 7, bit 15) - 240: "tsxldtrk", // X86FeatureTSXLDTRK (block 7, bit 16) - 242: "pconfig", // X86FeaturePCONFIG (block 7, bit 18) - 243: "arch_lbr", // X86FeatureARCH_LBR (block 7, bit 19) - 244: "ibt", // X86FeatureIBT (block 7, bit 20) - 246: "amx_bf16", // X86FeatureAMX_BF16 (block 7, bit 22) - 247: "avx512_fp16", // X86FeatureAVX512_FP16 (block 7, bit 23) - 248: "amx_tile", // X86FeatureAMX_TILE (block 7, bit 24) - 249: "amx_int8", // X86FeatureAMX_INT8 (block 7, bit 25) - 250: "spec_ctrl", // X86FeatureSPEC_CTRL (block 7, bit 26) - 251: "intel_stibp", // X86FeatureINTEL_STIBP (block 7, bit 27) - 252: "flush_l1d", // X86FeatureFLUSH_L1D (block 7, bit 28) - 253: "arch_capabilities", // X86FeatureARCH_CAPABILITIES (block 7, bit 29) - 254: "core_capabilities", // X86FeatureCORE_CAPABILITIES (block 7, bit 30) - 255: "spec_ctrl_ssbd", // X86FeatureSPEC_CTRL_SSBD (block 7, bit 31) - 260: "avx_vnni", // X86FeatureAVX_VNNI (block 8, bit 4) - 261: "avx512_bf16", // X86FeatureAVX512_BF16 (block 8, bit 5) - } - - arm64CPUFeatures = map[int]string{ - 0: "fp", // ARM64FeatureFP - 1: "asimd", // ARM64FeatureASIMD - 2: "evtstrm", // ARM64FeatureEVTSTRM - 3: "aes", // ARM64FeatureAES - 4: "pmull", // ARM64FeaturePMULL - 5: "sha1", // ARM64FeatureSHA1 - 6: "sha2", // ARM64FeatureSHA2 - 7: "crc32", // ARM64FeatureCRC32 - 8: "atomics", // ARM64FeatureATOMICS - 9: "fphp", // ARM64FeatureFPHP - 10: "asimdhp", // ARM64FeatureASIMDHP - 11: "cpuid", // ARM64FeatureCPUID - 12: "asimdrdm", // ARM64FeatureASIMDRDM - 13: "jscvt", // ARM64FeatureJSCVT - 14: "fcma", // ARM64FeatureFCMA - 15: "lrcpc", // ARM64FeatureLRCPC - 16: "dcpop", // ARM64FeatureDCPOP - 17: "sha3", // ARM64FeatureSHA3 - 18: "sm3", // ARM64FeatureSM3 - 19: "sm4", // ARM64FeatureSM4 - 20: "asimddp", // ARM64FeatureASIMDDP - 21: "sha512", // ARM64FeatureSHA512 - 22: "sve", // ARM64FeatureSVE - 23: "asimdfhm", // ARM64FeatureASIMDFHM - } -) diff --git a/internal/hardware/cpufeatures_test.go b/internal/hardware/cpufeatures_test.go index 6ea4d84a82..2b67ba76c9 100644 --- a/internal/hardware/cpufeatures_test.go +++ b/internal/hardware/cpufeatures_test.go @@ -15,132 +15,18 @@ package hardware import ( - "slices" - "strconv" "strings" "testing" - - "github.com/google/go-cmp/cmp" ) -func TestEncodeDecodeCPUFeatures(t *testing.T) { - for _, arch := range []string{"amd64", "arm64"} { - t.Run(arch+"/all_features_roundtrip", func(t *testing.T) { - table := cpuFeatureTables[arch] - all := make([]string, 0, len(table.bits)) - for _, bit := range table.bits { - all = append(all, table.names[bit]) - } - mask, err := EncodeCPUFeatures(arch, all) - if err != nil { - t.Fatalf("EncodeCPUFeatures(%q, all) error: %v", arch, err) - } - got, err := DecodeCPUFeatures(arch, mask) - if err != nil { - t.Fatalf("DecodeCPUFeatures(%q, %q) error: %v", arch, mask, err) - } - if diff := cmp.Diff(all, got); diff != "" { - t.Errorf("round-trip mismatch (-want +got):\n%s", diff) - } - }) - - t.Run(arch+"/empty_roundtrip", func(t *testing.T) { - mask, err := EncodeCPUFeatures(arch, nil) - if err != nil { - t.Fatalf("EncodeCPUFeatures(%q, nil) error: %v", arch, err) - } - if mask != "0" { - t.Errorf("EncodeCPUFeatures(%q, nil) = %q, want \"0\"", arch, mask) - } - got, err := DecodeCPUFeatures(arch, mask) - if err != nil { - t.Fatalf("DecodeCPUFeatures(%q, %q) error: %v", arch, mask, err) - } - if len(got) != 0 { - t.Errorf("DecodeCPUFeatures(%q, \"0\") = %v, want empty", arch, got) - } - }) - } - - t.Run("duplicates_and_order_normalized", func(t *testing.T) { - m1, err := EncodeCPUFeatures("amd64", []string{"avx2", "pni", "avx2", ""}) - if err != nil { - t.Fatal(err) - } - m2, err := EncodeCPUFeatures("amd64", []string{"pni", "avx2"}) - if err != nil { - t.Fatal(err) - } - if m1 != m2 { - t.Errorf("EncodeCPUFeatures with duplicates/reordering = %q, want %q", m1, m2) - } - got, err := DecodeCPUFeatures("amd64", m1) - if err != nil { - t.Fatal(err) - } - if diff := cmp.Diff([]string{"pni", "avx2"}, got); diff != "" { - t.Errorf("decoded features mismatch (-want +got):\n%s", diff) - } - }) - - t.Run("errors", func(t *testing.T) { - if _, err := EncodeCPUFeatures("riscv64", []string{"pni"}); err == nil { - t.Error("EncodeCPUFeatures with unsupported arch succeeded") - } - if _, err := EncodeCPUFeatures("amd64", []string{"not_a_real_feature"}); err == nil { - t.Error("EncodeCPUFeatures with unknown feature succeeded") - } - if _, err := DecodeCPUFeatures("riscv64", "0"); err == nil { - t.Error("DecodeCPUFeatures with unsupported arch succeeded") - } - for _, bad := range []string{"", "0x1f", "1F", "07", "xyz", strings.Repeat("f", featureMaskWords*16+1)} { - if _, err := DecodeCPUFeatures("amd64", bad); err == nil { - t.Errorf("DecodeCPUFeatures(%q) succeeded, want a non-canonical value rejected", bad) - } - } - // Bit 16 is CPUID.1:ECX bit 16, which the hardware reserves, so it - // sits inside the table's range with no name; and one past the - // highest named bit has none either. Decoding must name the bit. - amd64 := cpuFeatureTables["amd64"] - lastBit := amd64.bits[len(amd64.bits)-1] - for _, bit := range []int{16, lastBit + 1} { - if _, named := amd64.names[bit]; named { - t.Fatalf("test assumes amd64 bit %d has no name, but it is %q", bit, amd64.names[bit]) - } - var m featureMask - m.set(0) - m.set(bit) - _, err := DecodeCPUFeatures("amd64", m.String()) - if err == nil || !strings.Contains(err.Error(), strconv.Itoa(bit)) { - t.Errorf("DecodeCPUFeatures with unnamed bit %d set = %v, want an error naming the bit", bit, err) - } - } - }) - - t.Run("canonical_hex", func(t *testing.T) { - // Bits 0, 1, 2 and 64: one word boundary crossed, low word zero-padded. - var m featureMask - for _, bit := range []int{0, 1, 2, 64} { - m.set(bit) - } - if got, want := m.String(), "1"+strings.Repeat("0", 15)+"7"; got != want { - t.Errorf("featureMask.String() = %q, want %q", got, want) - } - back, ok := parseFeatureMask(m.String()) - if !ok || back != m { - t.Errorf("parseFeatureMask(%q) = %v, %v; want the original mask", m.String(), back, ok) - } - }) -} - // TestEncodeCPUFeatureMask pins the cpu_features value of real `runsc // cpu-features --bitmask` output, which stored snapshots must keep matching, // and checks the edges of the block format. func TestEncodeCPUFeatureMask(t *testing.T) { // runsc 20261102.0 on an Intel host: block 0 is CPUID.1:ECX (pni is bit // 0), block 1 CPUID.1:EDX (fpu is bit 0, mask bit 32), and so on, with - // block 8 empty. Its names form, through the tables EncodeCPUFeatures - // maps them with, encodes to the same value. + // block 8 empty. The removed feature-name tables produced the same + // value for this host. const ( blocks = "0xfefa3203,0x1f8bfbff,0x001c2ffb,0x00000000,0x00000001,0x00000121,0x2c100800,0xac000400,0x00000000" want = "ac0004002c100800000001210000000100000000001c2ffb1f8bfbfffefa3203" @@ -175,74 +61,26 @@ func TestEncodeCPUFeatureMask(t *testing.T) { } } -// TestCPUFeatureTables checks structural invariants of each architecture's -// feature table and pins anchor bits to the values of gVisor's Feature -// constants (32*block + CPUID bit on amd64). A moved anchor means -// cpufeatures_table.go no longer follows the hardware's numbering and stored -// snapshots would decode differently than they were encoded. -func TestCPUFeatureTables(t *testing.T) { - for _, tc := range []struct { - arch, name string - bit int - }{ - {"amd64", "pni", 0}, - {"amd64", "pcid", 17}, // after the reserved bit 16 - {"amd64", "osxsave", 27}, // listed after bit 31 in gVisor's map - {"amd64", "fpu", 32}, // block 1, bit 0 - {"amd64", "avx2", 2*32 + 5}, // block 2 - {"amd64", "syscall", 6*32 + 11}, // sparse block 6 - {"amd64", "avx512_bf16", 8*32 + 5}, - {"arm64", "fp", 0}, - {"arm64", "asimdfhm", 23}, - } { - if got, ok := cpuFeatureTables[tc.arch].index[tc.name]; !ok || got != tc.bit { - t.Errorf("%s %q at bit %d (present=%v), want %d", tc.arch, tc.name, got, ok, tc.bit) - } +// TestParseFeatureMask checks that the canonical encoding round-trips and +// that nothing else parses: a worker or snapshot value in another spelling +// of the same set would compare unequal as a proto field. +func TestParseFeatureMask(t *testing.T) { + // Bits 0, 1, 2 and 64: one word boundary crossed, low word zero-padded. + m := featureMask{7, 1} + if got, want := m.String(), "1"+strings.Repeat("0", 15)+"7"; got != want { + t.Errorf("featureMask.String() = %q, want %q", got, want) } - for arch, table := range cpuFeatureTables { - t.Run(arch, func(t *testing.T) { - if len(table.bits) == 0 { - t.Fatalf("%s table is empty", arch) - } - if !slices.IsSorted(table.bits) || len(table.bits) != len(table.names) { - t.Errorf("%s table bits %v are not the sorted keys of its %d names", arch, table.bits, len(table.names)) - } - var defined featureMask - seen := make(map[string]int, len(table.names)) - for bit, name := range table.names { - defined.set(bit) // newFeatureTable panics at init on a bit outside the mask - if other, dup := seen[name]; dup { - t.Errorf("%s table lists %q at both %d and %d", arch, name, other, bit) - } - seen[name] = bit - if name != strings.TrimSpace(name) || name == "" { - t.Errorf("%s table bit %d is %q, want a trimmed non-empty name", arch, bit, name) - } - if got := table.index[name]; got != bit { - t.Errorf("%s index[%q] = %d, want %d", arch, name, got, bit) - } - } - if defined != table.defined { - t.Errorf("%s table's defined mask %v does not match its names", arch, table.defined) - } - for name, bit := range table.index { - if _, ok := table.names[bit]; !ok { - t.Errorf("%s index maps %q to bit %d, which has no name", arch, name, bit) - } - } - }) + for _, s := range []string{m.String(), "0", "f", featureMask{^uint64(0)}.String()} { + back, ok := parseFeatureMask(s) + if !ok || back.String() != s { + t.Errorf("parseFeatureMask(%q) = (%v, %v), want the mask that encodes to it", s, back, ok) + } } -} - -// A table entry the mask cannot hold is a panic at package init rather than a -// dropped name. -func TestNewFeatureTableRejectsBitOutsideMask(t *testing.T) { - defer func() { - if recover() == nil { - t.Error("newFeatureTable with a bit outside the mask did not panic") + for _, bad := range []string{"", "0x1f", "1F", "07", "xyz", strings.Repeat("f", featureMaskWords*16+1)} { + if _, ok := parseFeatureMask(bad); ok { + t.Errorf("parseFeatureMask(%q) succeeded, want a non-canonical value rejected", bad) } - }() - newFeatureTable(map[int]string{featureMaskWords * 64: "beyond"}) + } } func TestParseCPUInfo(t *testing.T) { @@ -306,70 +144,3 @@ CPU revision : 1 }) } } - -// FuzzCPUFeatures checks, from real `runsc cpu-features` output and whatever -// the fuzzer derives from it, that a feature list round-trips through -// EncodeCPUFeatures and DecodeCPUFeatures, that the encoding is canonical, -// and that subsetOf agrees with set inclusion on the names. -func FuzzCPUFeatures(f *testing.F) { - const ( - broadwellCSV = "fpu,vme,de,pse,tsc,msr,pae,mce,cx8,apic,sep,mtrr,pge,mca,cmov,pat,pse36,clflush,mmx,fxsr,sse,sse2,ss,ht,syscall,nx,pdpe1gb,rdtscp,lm,pni,pclmulqdq,ssse3,fma,cx16,pcid,sse4_1,sse4_2,x2apic,movbe,popcnt,aes,xsave,osxsave,avx,f16c,rdrand,hypervisor,lahf_lm,abm,3dnowprefetch,fsgsbase,tsc_adjust,bmi1,hle,avx2,smep,bmi2,erms,invpcid,rtm,rdseed,adx,smap,xsaveopt,md_clear,arch_capabilities" - sapphireRapidsCSV = "fpu,vme,de,pse,tsc,msr,pae,mce,cx8,apic,sep,mtrr,pge,mca,cmov,pat,pse36,clflush,mmx,fxsr,sse,sse2,ss,ht,syscall,nx,pdpe1gb,rdtscp,lm,pni,pclmulqdq,ssse3,fma,cx16,pcid,sse4_1,sse4_2,x2apic,movbe,popcnt,tsc_deadline_timer,aes,xsave,osxsave,avx,f16c,rdrand,hypervisor,lahf_lm,abm,3dnowprefetch,fsgsbase,tsc_adjust,bmi1,hle,avx2,smep,bmi2,erms,invpcid,rtm,avx512f,avx512dq,rdseed,adx,smap,clfushopt,clwb,avx512cd,sha_ni,avx512bw,avx512vl,xsaveopt,xsavec,xgetbv1,xsaves,avx512vbmi,umip,pku,ospke,waitpkg,avx512_vbmi2,gfni,vaes,vpclmulqdq,avx512_vnni,avx512_bitalg,avx512_vpopcntdq,la57,rdpid,cldemote,movdiri,movdir64b,fsrm,md_clear,serialize,tsxldtrk,amx_bf16,avx512_fp16,amx_tile,amx_int8,spec_ctrl,intel_stibp,flush_l1d,arch_capabilities,spec_ctrl_ssbd,avx_vnni,avx512_bf16" - amdMilanCSV = "fpu,vme,de,pse,tsc,msr,pae,mce,cx8,apic,sep,mtrr,pge,mca,cmov,pat,pse36,clflush,mmx,fxsr,sse,sse2,ht,syscall,nx,mmxext,fxsr_opt,pdpe1gb,rdtscp,lm,pni,pclmulqdq,ssse3,fma,cx16,pcid,sse4_1,sse4_2,x2apic,movbe,popcnt,aes,xsave,osxsave,avx,f16c,rdrand,hypervisor,lahf_lm,cmp_legacy,svm,cr8_legacy,abm,sse4a,misalignsse,3dnowprefetch,osvw,topoext,perfctr_core,fsgsbase,tsc_adjust,bmi1,avx2,smep,bmi2,erms,invpcid,rdseed,adx,smap,clfushopt,clwb,sha_ni,xsaveopt,xsavec,xgetbv1,xsaves,umip,pku,ospke,vaes,vpclmulqdq,rdpid,fsrm" - neoverseN1CSV = "fp,asimd,evtstrm,aes,pmull,sha1,sha2,crc32,atomics,fphp,asimdhp,cpuid,asimdrdm,lrcpc,dcpop,asimddp" - neoverseV2CSV = "fp,asimd,evtstrm,aes,pmull,sha1,sha2,crc32,atomics,fphp,asimdhp,cpuid,asimdrdm,jscvt,fcma,lrcpc,dcpop,sha3,sm3,sm4,asimddp,sha512,sve,asimdfhm" - ) - // A seed the tables cannot encode would only exercise the early return - // below, so a table change that drops one of these names fails here - // rather than quietly weakening the corpus. - for _, seed := range []struct{ arch, csv string }{ - {"amd64", broadwellCSV}, {"amd64", sapphireRapidsCSV}, {"amd64", amdMilanCSV}, - {"arm64", neoverseN1CSV}, {"arm64", neoverseV2CSV}, - } { - if _, err := EncodeCPUFeatures(seed.arch, strings.Split(seed.csv, ",")); err != nil { - f.Fatalf("seed does not encode: %v", err) - } - } - f.Add("amd64", broadwellCSV, sapphireRapidsCSV) - f.Add("amd64", sapphireRapidsCSV, broadwellCSV) - f.Add("amd64", broadwellCSV, amdMilanCSV) - f.Add("arm64", neoverseN1CSV, neoverseV2CSV) - f.Add("arm64", neoverseV2CSV, neoverseN1CSV) - // Linux-synthesized flags runsc never prints; encoding rejects them. - f.Add("amd64", "fpu,sse2,constant_tsc,rep_good,nopl", broadwellCSV) - - f.Fuzz(func(t *testing.T, arch, snapCSV, workerCSV string) { - snapRaw, workerRaw := strings.Split(snapCSV, ","), strings.Split(workerCSV, ",") - snapMask, snapErr := EncodeCPUFeatures(arch, snapRaw) - workerMask, workerErr := EncodeCPUFeatures(arch, workerRaw) - if snapErr != nil || workerErr != nil { - return - } - decoded, err := DecodeCPUFeatures(arch, snapMask) - if err != nil { - t.Fatalf("DecodeCPUFeatures(%q, %q) failed: %v", arch, snapMask, err) - } - if reencoded, err := EncodeCPUFeatures(arch, decoded); err != nil || reencoded != snapMask { - t.Fatalf("round-trip EncodeCPUFeatures(%q, %v) = (%q, %v), want %q", arch, decoded, reencoded, err, snapMask) - } - workerDecoded, err := DecodeCPUFeatures(arch, workerMask) - if err != nil { - t.Fatalf("DecodeCPUFeatures(%q, %q) failed: %v", arch, workerMask, err) - } - wantSubset := true - for _, feat := range decoded { - if !slices.Contains(workerDecoded, feat) { - wantSubset = false - break - } - } - mSnap, okSnap := parseFeatureMask(snapMask) - mWorker, okWorker := parseFeatureMask(workerMask) - if !okSnap || !okWorker || mSnap.String() != snapMask || mWorker.String() != workerMask { - t.Fatalf("non-canonical mask produced: snap=%q worker=%q", snapMask, workerMask) - } - if got := mSnap.subsetOf(mWorker); got != wantSubset { - t.Fatalf("subsetOf(%q, %q) = %v, want %v", snapMask, workerMask, got, wantSubset) - } - }) -} diff --git a/internal/hardware/hardware_test.go b/internal/hardware/hardware_test.go index aa834501ee..383ce308a6 100644 --- a/internal/hardware/hardware_test.go +++ b/internal/hardware/hardware_test.go @@ -106,19 +106,22 @@ func TestMatchesCompat(t *testing.T) { return &ateapi.AttributeEntry{Key: AttrCPUFeatures, Value: val} } - // Build >64-bit masks so the subset check spans mask words. - highSnap, err := EncodeCPUFeatures("amd64", []string{"pni", "avx2", "avx512_bf16"}) - if err != nil { - t.Fatal(err) - } - highWorkerSuperset, err := EncodeCPUFeatures("amd64", []string{"pni", "sse2", "avx2", "avx_vnni", "avx512_bf16"}) - if err != nil { - t.Fatal(err) - } - highWorkerMissingOne, err := EncodeCPUFeatures("amd64", []string{"pni", "sse2", "avx2", "avx_vnni"}) - if err != nil { - t.Fatal(err) + // Build >64-bit masks so the subset check spans mask words, from gVisor's + // blocks: 0 is CPUID.1:ECX (pni bit 0), 1 CPUID.1:EDX (sse2 bit 26), 2 + // CPUID.7:EBX (avx2 bit 5), 8 CPUID.7.1:EAX (avx_vnni bit 4, avx512_bf16 + // bit 5). + mask := func(blocks string) string { + t.Helper() + m, err := EncodeCPUFeatureMask(blocks) + if err != nil { + t.Fatal(err) + } + return m } + const blocks3to7 = ",0x00000000,0x00000000,0x00000000,0x00000000,0x00000000" + highSnap := mask("0x00000001,0x00000000,0x00000020" + blocks3to7 + ",0x00000020") // pni, avx2, avx512_bf16 + highWorkerSuperset := mask("0x00000001,0x04000000,0x00000020" + blocks3to7 + ",0x00000030") // and sse2, avx_vnni + highWorkerMissingOne := mask("0x00000001,0x04000000,0x00000020" + blocks3to7 + ",0x00000010") // without avx512_bf16 tests := []struct { name string From d643b73149c7abbfc362d5149a7b87354cd95371 Mon Sep 17 00:00:00 2001 From: Shrutiya Date: Fri, 20 Nov 2026 19:32:02 +0000 Subject: [PATCH 3/3] docs, hardware: document gVisor minimum and ignore high zero blocks Document in the API guide that gVisor SandboxConfigs require release 20261026.0 or newer. Older runsc binaries lack `runsc cpu-features --bitmask` and the `-no-root-container` restore fix. Describe how atelet handles an older release when a SandboxConfig lists one. Update EncodeCPUFeatureMask to ignore all-zero blocks past 12 blocks. `runsc` prints blocks up to its highest compiled feature even when those bits are zero on the host, so a future runsc with a 13th block would otherwise fail on existing CPUs. Non-zero bits past 12 blocks still fail. Update the cpuFeaturesTimeout comment in atelet to reference `runsc cpu-features --bitmask`. Signed-off-by: Shrutiya --- cmd/atelet/worker_sandbox_versions.go | 6 +++--- docs/reference/api-guide.md | 2 +- internal/hardware/cpufeatures.go | 12 ++++++++---- internal/hardware/cpufeatures_test.go | 1 + 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/atelet/worker_sandbox_versions.go b/cmd/atelet/worker_sandbox_versions.go index 5b139614a9..47a79e4c72 100644 --- a/cmd/atelet/worker_sandbox_versions.go +++ b/cmd/atelet/worker_sandbox_versions.go @@ -469,9 +469,9 @@ func (s *workerSandboxSyncer) gvisorCPUFeatures(ctx context.Context, uid string, // joins one still in flight. const probeFetchTimeout = 10 * time.Minute -// cpuFeaturesTimeout bounds one `runsc cpu-features` run, which takes -// milliseconds. A runsc that hangs would hold a sync worker. A var so tests -// can shorten it. +// cpuFeaturesTimeout bounds one `runsc cpu-features --bitmask` run, which +// takes milliseconds. A runsc that hangs would hold a sync worker. A var so +// tests can shorten it. var cpuFeaturesTimeout = 30 * time.Second // runRunscCPUFeatures executes ` cpu-features --bitmask` and diff --git a/docs/reference/api-guide.md b/docs/reference/api-guide.md index 35ee6bce4a..c3cf1f9e47 100644 --- a/docs/reference/api-guide.md +++ b/docs/reference/api-guide.md @@ -587,7 +587,7 @@ Each entry of `versions` (`SandboxVersionConfig`): | `state` | `string` | Optional. `Enabled` (default) or `Disabled`. The version named by `defaultVersion` cannot be `Disabled`. | | `assets` | `map[arch]map[name]AssetFile` | Content-addressed files atelet fetches, keyed by architecture (`amd64`, `arm64`) then asset name. gVisor expects a `gvisor` asset (the release's `gvisor.tar.zstd`), which atelet auto-extracts. A microVM backend expects several. Each `AssetFile` is a `{ url, sha256 }` pair; `url` is at most 2048 bytes. A `ValidatingAdmissionPolicy` enforces each class's required assets on every version. atelet holds no storage credentials: it fetches every URL through the node's object-store plugin (`AssetProvider`), with the plugin's credentials. Which schemes work depends on the plugin; the in-tree one serves only its storage backend's scheme, `gs://` on GCS and `s3://` on S3, as `/`, and rejects other schemes without reading them. A `gs://` URL the plugin cannot serve (it has no such object, rejects the URL, does not implement `AssetProvider`, is unreachable, or fails with an error it does not recognize, such as GCS refusing its credentials for a public bucket) is then read anonymously, which serves public buckets such as `gs://gvisor`; any other refusal by the plugin is final. The plugin returns an object only if its content matches `sha256`, and refuses any URL under a snapshot location (a snapshot location followed by `/atespaces/`), so assets must not be stored there. On GKE, substrate-gke's setup-gcp lets the plugin read only the asset bucket, `-assets`, so keep assets there. atelet verifies the sha256 again before using an asset. Its logs and errors show an asset URL as `scheme://host/path`. | -A cluster-wide gVisor `SandboxConfig` (`gvisor-default`, annotated as the class default) is installed with the platform, so gVisor `WorkerPool`s use it automatically without any extra setup. +A cluster-wide gVisor `SandboxConfig` (`gvisor-default`, annotated as the class default) is installed with the platform, so gVisor `WorkerPool`s use it automatically without any extra setup. Custom `gvisor` `SandboxConfig`s require gVisor release `20261026.0` or newer (for `runsc cpu-features --bitmask` and `-no-root-container` restore). On an older release, `atelet` fails the version's probe permanently: a non-default version is omitted from `restorableRuntimes` (actors on it cold-boot under `defaultVersion`, keeping their files), and an older `defaultVersion` fails runtime sync for the pool (new workers advertise no runtimes, and existing workers keep advertising their previous runtimes file). ### Class default diff --git a/internal/hardware/cpufeatures.go b/internal/hardware/cpufeatures.go index 352874f9e7..8297ce5872 100644 --- a/internal/hardware/cpufeatures.go +++ b/internal/hardware/cpufeatures.go @@ -89,13 +89,11 @@ func parseFeatureMask(s string) (featureMask, bool) { // EncodeCPUFeatureMask encodes the blocks `runsc cpu-features --bitmask` // prints as a cpu_features value: one "0x%08x" uint32 per 32 features, // lowest first, as many as the binary's newest feature needs, so trailing -// zero blocks change nothing. More blocks than featureMask holds is an error. +// zero blocks change nothing. A non-zero bit past what featureMask holds is an +// error. func EncodeCPUFeatureMask(blocks string) (string, error) { var m featureMask for i, block := range strings.Split(strings.TrimSpace(blocks), ",") { - if i >= featureMaskWords*2 { - return "", fmt.Errorf("cpu_features bitmask %q has more than %d blocks", blocks, featureMaskWords*2) - } digits, ok := strings.CutPrefix(block, "0x") if !ok { return "", fmt.Errorf("cpu_features bitmask block %q lacks the 0x prefix", block) @@ -104,6 +102,12 @@ func EncodeCPUFeatureMask(blocks string) (string, error) { if err != nil { return "", fmt.Errorf("cpu_features bitmask block %q: %w", block, err) } + if i >= featureMaskWords*2 { + if v != 0 { + return "", fmt.Errorf("cpu_features bitmask %q sets bits past %d blocks", blocks, featureMaskWords*2) + } + continue + } m[i/2] |= v << (32 * (i % 2)) } return m.String(), nil diff --git a/internal/hardware/cpufeatures_test.go b/internal/hardware/cpufeatures_test.go index 2b67ba76c9..5f08686609 100644 --- a/internal/hardware/cpufeatures_test.go +++ b/internal/hardware/cpufeatures_test.go @@ -41,6 +41,7 @@ func TestEncodeCPUFeatureMask(t *testing.T) { {"0x00000003,0x00000000,0x00000000", "3"}, // trailing zero blocks {"0x00000001,0x00000000,0x00000020", "20" + strings.Repeat("0", 15) + "1"}, // bits 0 and 69: a word boundary crossed {strings.Repeat("0x00000000,", featureMaskWords*2-1) + "0x80000000", "8" + strings.Repeat("0", featureMaskWords*16-1)}, + {strings.Repeat("0x00000000,", featureMaskWords*2) + "0x00000000", "0"}, // trailing zero block past featureMaskWords*2 blocks } { if got, err := EncodeCPUFeatureMask(tc.blocks); err != nil || got != tc.want { t.Errorf("EncodeCPUFeatureMask(%q) = (%q, %v), want (%q, nil)", tc.blocks, got, err, tc.want)