CodeSampleX

샘플

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

검증된 샘플 — golang github.com/klauspost/cpuid/v2 v2.2.10: cpuid.CPU. go 1.26 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다: cpuid.CPU is a global CPUInfo…

sha256:e392c980f485f75f0ab3bed34b8fbaa7278b5c503c85f74066bf9d2319ac9611

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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"
  ]
}

오리진 시더

익명