CodeSampleX

示例

github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38: profile.Function

已验证示例 — golang github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38: profile.Function. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:45b9475261e81bf25533babdc421dfdbc4911d141130f8f3dfb8f713054f523e

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify github.com/google/pprof/profile.Function in pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38
包
符号
  • github.com/google/pprof/profile.Function
创建时间
2026-09-04T11:05:08Z

契约

  1. profile.Function stores symbol metadata including ID, Name, SystemName, Filename, and StartLine
  2. profile.Function binds to Location lines and passes Profile.CheckValid validation with unique non-zero IDs
  3. profile.Function with reserved ID 0 or duplicate IDs causes Profile.CheckValid to fail
  4. profile.Function preserves all metadata fields across protobuf serialization and deserialization
  5. profile.Function is cloned as an independent instance during Profile.Copy

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract.go

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

源代码

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 github.com/google/pprof/profile.Function in pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38
Demonstrate these symbols/APIs:
  - github.com/google/pprof/profile.Function

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.
csx.json
{"case":{"caseId":"case:sha256:504a34d7d106889a3dc470d824bda13adf518e0b870e61a42bae1722e609587e","contract":["profile.Function stores symbol metadata including ID, Name, SystemName, Filename, and StartLine","profile.Function binds to Location lines and passes Profile.CheckValid validation with unique non-zero IDs","profile.Function with reserved ID 0 or duplicate IDs causes Profile.CheckValid to fail","profile.Function preserves all metadata fields across protobuf serialization and deserialization","profile.Function is cloned as an independent instance during Profile.Copy"],"goal":"verify github.com/google/pprof/profile.Function in pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38","kind":"HOW","packages":["pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38"],"schemaVersion":1,"symbols":["github.com/google/pprof/profile.Function"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38"],"schemaVersion":1,"subject":"pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38","symbols":["github.com/google/pprof/profile.Function"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22

require github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38
go.sum
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
sample.go
package sample

import (
	"bytes"
	"fmt"

	"github.com/google/pprof/profile"
)

// BuildFunction instantiates a profile.Function with defined symbol metadata.
func BuildFunction(id uint64, name, systemName, filename string, startLine int64) *profile.Function {
	return &profile.Function{
		ID:         id,
		Name:       name,
		SystemName: systemName,
		Filename:   filename,
		StartLine:  startLine,
	}
}

// BuildProfile constructs a valid profile incorporating the given functions.
func BuildProfile(functions []*profile.Function) *profile.Profile {
	p := &profile.Profile{
		SampleType: []*profile.ValueType{
			{Type: "samples", Unit: "count"},
		},
		PeriodType: &profile.ValueType{Type: "cpu", Unit: "nanoseconds"},
		Period:     10000000,
		Function:   functions,
	}

	for i, fn := range functions {
		loc := &profile.Location{
			ID: uint64(i + 1),
			Line: []profile.Line{
				{
					Function: fn,
					Line:     fn.StartLine + 1,
				},
			},
		}
		p.Location = append(p.Location, loc)
		p.Sample = append(p.Sample, &profile.Sample{
			Location: []*profile.Location{loc},
			Value:    []int64{1},
		})
	}

	return p
}

// RoundTripProfile serializes a profile to protobuf and parses it back.
func RoundTripProfile(p *profile.Profile) (*profile.Profile, error) {
	var buf bytes.Buffer
	if err := p.Write(&buf); err != nil {
		return nil, fmt.Errorf("write profile: %w", err)
	}
	parsed, err := profile.Parse(&buf)
	if err != nil {
		return nil, fmt.Errorf("parse profile: %w", err)
	}
	return parsed, nil
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/google/pprof/profile.Function in pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/pprof@v0.0.0-20210407192527-94a9f03dee38"
  ],
  "symbols": [
    "github.com/google/pprof/profile.Function"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"github.com/google/pprof/profile"
	"sample"
)

func main() {
	// Assertion 1: profile.Function stores symbol metadata including ID, Name, SystemName, Filename, and StartLine
	fn1 := sample.BuildFunction(1, "processWork", "sample.processWork", "processor.go", 42)
	if fn1.ID != 1 || fn1.Name != "processWork" || fn1.SystemName != "sample.processWork" || fn1.Filename != "processor.go" || fn1.StartLine != 42 {
		fmt.Fprintf(os.Stderr, "FAIL: fn1 attributes mismatch: %+v\n", fn1)
		os.Exit(1)
	}

	// Assertion 2: profile.Function binds to Location lines and passes Profile.CheckValid validation with unique non-zero IDs
	fn2 := sample.BuildFunction(2, "dispatchWork", "sample.dispatchWork", "dispatcher.go", 88)
	p := sample.BuildProfile([]*profile.Function{fn1, fn2})
	if err := p.CheckValid(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: valid profile CheckValid returned error: %v\n", err)
		os.Exit(1)
	}
	if len(p.Function) != 2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 2 functions, got %d\n", len(p.Function))
		os.Exit(1)
	}

	// Assertion 3: profile.Function with reserved ID 0 or duplicate IDs causes Profile.CheckValid to fail
	badFnZero := sample.BuildFunction(0, "zeroID", "sample.zeroID", "zero.go", 10)
	pZero := sample.BuildProfile([]*profile.Function{badFnZero})
	if err := pZero.CheckValid(); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for Function with reserved ID 0, got nil\n")
		os.Exit(1)
	}

	fnDup1 := sample.BuildFunction(5, "fnA", "sample.fnA", "a.go", 10)
	fnDup2 := sample.BuildFunction(5, "fnB", "sample.fnB", "b.go", 20)
	pDup := sample.BuildProfile([]*profile.Function{fnDup1, fnDup2})
	if err := pDup.CheckValid(); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for duplicate Function IDs, got nil\n")
		os.Exit(1)
	}

	// Assertion 4: profile.Function preserves all metadata fields across protobuf serialization and deserialization
	roundTripped, err := sample.RoundTripProfile(p)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: serialization roundtrip failed: %v\n", err)
		os.Exit(1)
	}
	if len(roundTripped.Function) != len(p.Function) {
		fmt.Fprintf(os.Stderr, "FAIL: roundtripped functions count mismatch: %d != %d\n", len(roundTripped.Function), len(p.Function))
		os.Exit(1)
	}
	rtFn1 := roundTripped.Function[0]
	if rtFn1.ID != fn1.ID || rtFn1.Name != fn1.Name || rtFn1.SystemName != fn1.SystemName || rtFn1.Filename != fn1.Filename || rtFn1.StartLine != fn1.StartLine {
		fmt.Fprintf(os.Stderr, "FAIL: roundtripped Function[0] metadata altered: got %+v, want %+v\n", rtFn1, fn1)
		os.Exit(1)
	}

	// Assertion 5: profile.Function is cloned as an independent instance during Profile.Copy
	copied := p.Copy()
	if err := copied.CheckValid(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: copied profile CheckValid failed: %v\n", err)
		os.Exit(1)
	}
	if copied.Function[0] == p.Function[0] {
		fmt.Fprintf(os.Stderr, "FAIL: copied profile shares Function pointer with original\n")
		os.Exit(1)
	}
	copied.Function[0].Name = "mutatedName"
	if p.Function[0].Name == "mutatedName" {
		fmt.Fprintf(os.Stderr, "FAIL: mutating copied Function affected original Function\n")
		os.Exit(1)
	}

	fmt.Println("PASS: github.com/google/pprof/profile.Function contract verified")
}

原始种子者

匿名