CodeSampleX

示例

github.com/klauspost/cpuid/v2 v2.2.10: cpuid.CPU

已验证示例 — golang github.com/klauspost/cpuid/v2 v2.2.10: cpuid.CPU. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: cpuid.CPU is a global CPUInfo struct…

sha256:e392c980f485f75f0ab3bed34b8fbaa7278b5c503c85f74066bf9d2319ac9611

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
2
构建过它的签名密钥
1
声明的环境 go linux 24 · ubuntu · glibc 2.39 x64 go go go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-26
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 FAIL compile:SKIPPED · contract:FAIL · load:SKIPPED · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-07

案例

HOW
目标
verify cpuid.CPU in pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10
包
符号
  • cpuid.CPU
环境
go
创建时间
2026-08-26T04:47:18Z

契约

  1. cpuid.CPU is a global CPUInfo struct initialized at startup providing non-negative core counts and matching vendor information.
  2. cpuid.CPU.Supports returns true if all specified feature IDs are supported, and cpuid.CPU.AnyOf returns true if at least one feature ID is supported.
  3. Passing cpuid.UNKNOWN (-1) or an unrecognized feature ID directly to cpuid.CPU.Has causes an index out of range panic.
  4. Dynamic feature flag modification with Enable and Disable updates CPUInfo feature membership and alters subsequent Has, Supports, and AnyOf query results.

文件

  • NOTES.md
  • PROMPT.md
  • cpuid_test.go
  • csx.json
  • go.mod
  • go.sum
  • spec.json

下载源代码构件 (tar.gz)

源代码

NOTES.md
# klauspost/cpuid/v2 CPU Feature Detection and Query Semantics

## Prior Knowledge Search
A query to search known solutions for `github.com/klauspost/cpuid/v2` with `cpuid.CPU` returned `NO_SAFE_MATCH`.

## Pinned Release
- `github.com/klauspost/cpuid/v2`: `v2.2.10`

## Observed Behavior and API Contracts

### 1. Global `cpuid.CPU` Singleton Initialization
- `cpuid.CPU` is an exported global `CPUInfo` struct initialized automatically at runtime startup.
- It exposes hardware attributes such as `PhysicalCores`, `LogicalCores`, `Family`, `Model`, `Stepping`, `VendorID`, and `VendorString`.
- Core counts are non-negative, and `VendorString` matches the detected `VendorID`.

### 2. Feature Querying with `Supports`, `AnyOf`, and `FeatureSet`
- `cpuid.CPU.FeatureSet()` returns a slice of string names of all detected CPU features.
- `cpuid.CPU.Supports(ids ...FeatureID)` checks for conjunction: returns `true` only if all specified features are supported (vacuously `true` when passed zero arguments).
- `cpuid.CPU.AnyOf(ids ...FeatureID)` checks for disjunction: returns `true` if at least one of the specified features is supported (returns `false` when passed zero arguments).

### 3. Negative / Unknown Feature Panic Behavior
- `cpuid.ParseFeature` returns `cpuid.UNKNOWN` (`-1`) when given an unrecognized feature name.
- `cpuid.CPU.Has(id FeatureID)` directly indexes internal flag buckets without bounds-checking negative indices (`set[id/64]`).
- As a consequence, passing `cpuid.UNKNOWN` (`-1`) to `cpuid.CPU.Has` causes a runtime panic (`runtime error: index out of range [-1]`) rather than returning `false`. Callers must guard against `UNKNOWN` or negative feature IDs before invoking `Has`.

### 4. Dynamic Feature Flag Mutation via `Enable` and `Disable`
- `CPUInfo` instances (cloned or custom) support `Enable(ids ...FeatureID)` and `Disable(ids ...FeatureID)` to mutate enabled CPU feature flags.
- Enabling or disabling features immediately updates the output of `Has`, `Supports`, and `AnyOf` on that instance.
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify cpuid.CPU in pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10
Demonstrate these symbols/APIs:
  - cpuid.CPU

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
cpuid_test.go
package cpuidcheck

import (
	"testing"

	"github.com/klauspost/cpuid/v2"
)

func TestCPUSingletonAndProperties(t *testing.T) {
	// 1. cpuid.CPU provides CPU info detected at initialization
	if cpuid.CPU.PhysicalCores < 0 || cpuid.CPU.LogicalCores < 0 {
		t.Fatalf("expected non-negative core counts, got physical: %d, logical: %d",
			cpuid.CPU.PhysicalCores, cpuid.CPU.LogicalCores)
	}

	if cpuid.CPU.VendorString == "" && cpuid.CPU.VendorID != cpuid.VendorUnknown {
		t.Fatalf("expected VendorString to match VendorID")
	}
}

func TestCPUFeatureQueries(t *testing.T) {
	// 2. Querying features via Supports, AnyOf, and FeatureSet
	features := cpuid.CPU.FeatureSet()
	if len(features) > 0 {
		// First feature from FeatureSet should be reported as supported via Has
		firstFeatureID := cpuid.ParseFeature(features[0])
		if firstFeatureID != cpuid.UNKNOWN {
			if !cpuid.CPU.Has(firstFeatureID) {
				t.Fatalf("expected cpuid.CPU.Has(%s) to be true", features[0])
			}
		}
	}

	// Supports with empty slice returns true
	if !cpuid.CPU.Supports() {
		t.Fatalf("cpuid.CPU.Supports() with no arguments must return true")
	}

	// AnyOf with empty slice returns false
	if cpuid.CPU.AnyOf() {
		t.Fatalf("cpuid.CPU.AnyOf() with no arguments must return false")
	}
}

func TestCPUUnknownFeaturePanic(t *testing.T) {
	// 3. Querying cpuid.UNKNOWN (-1) or invalid feature ID directly with Has panics with index out of range
	defer func() {
		r := recover()
		if r == nil {
			t.Fatalf("expected panic when checking cpuid.CPU.Has(cpuid.UNKNOWN), but none occurred")
		}
	}()

	_ = cpuid.CPU.Has(cpuid.UNKNOWN)
}

func TestCPUFeatureMutation(t *testing.T) {
	// 4. Modifying features on a CPUInfo instance via Enable and Disable
	customCPU := cpuid.CPU

	// Enable a feature
	customCPU.Enable(cpuid.AVX512F)
	if !customCPU.Has(cpuid.AVX512F) {
		t.Fatalf("expected customCPU.Has(AVX512F) to be true after Enable")
	}

	// Disable the feature
	customCPU.Disable(cpuid.AVX512F)
	if customCPU.Has(cpuid.AVX512F) {
		t.Fatalf("expected customCPU.Has(AVX512F) to be false after Disable")
	}

	// Supports reflects multiple features
	customCPU.Enable(cpuid.AVX, cpuid.AVX2)
	if !customCPU.Supports(cpuid.AVX, cpuid.AVX2) {
		t.Fatalf("expected customCPU.Supports(AVX, AVX2) to be true")
	}

	customCPU.Disable(cpuid.AVX2)
	if customCPU.Supports(cpuid.AVX, cpuid.AVX2) {
		t.Fatalf("expected customCPU.Supports(AVX, AVX2) to be false after disabling AVX2")
	}
	if !customCPU.AnyOf(cpuid.AVX, cpuid.AVX2) {
		t.Fatalf("expected customCPU.AnyOf(AVX, AVX2) to be true when AVX is enabled")
	}
}
csx.json
{"case":{"caseId":"case:sha256:5c14fa714ed99771e0adc1c88e6531f4b8978d973785654a1e33fe07c6a78fbb","contract":["cpuid.CPU is a global CPUInfo struct initialized at startup providing non-negative core counts and matching vendor information.","cpuid.CPU.Supports returns true if all specified feature IDs are supported, and cpuid.CPU.AnyOf returns true if at least one feature ID is supported.","Passing cpuid.UNKNOWN (-1) or an unrecognized feature ID directly to cpuid.CPU.Has causes an index out of range panic.","Dynamic feature flag modification with Enable and Disable updates CPUInfo feature membership and alters subsequent Has, Supports, and AnyOf query results."],"goal":"verify cpuid.CPU in pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10","kind":"HOW","packages":["pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10"],"schemaVersion":1,"symbols":["cpuid.CPU"]},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","executionContext":"go","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10"],"schemaVersion":1,"subject":"pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10","symbols":["cpuid.CPU"],"verifierAdapter":"golang@1"}
go.mod
module example.com/cpuidcheck

go 1.26.6

require (
	github.com/klauspost/cpuid/v2 v2.2.10 // indirect
	golang.org/x/sys v0.30.0 // indirect
)
go.sum
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify cpuid.CPU in pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/klauspost/cpuid/v2@v2.2.10"
  ],
  "symbols": [
    "cpuid.CPU"
  ]
}

原始种子者

匿名