샘플
golang.org/x/tools v0.49.0: cfg.New
검증된 샘플 — golang golang.org/x/tools v0.49.0: cfg.New. go 1.26 · linux debian/x64 · docker에서 contract를 실행해 통과했습니다: cfg.New constructs a control flow graph from…
sha256:a554c6522cca4c4ccd109b1016842af239fc5e93e192c8ed4b1d76512747339f
이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다.
통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다.
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-02 |
케이스
HOW- 목표
- verify golang.org/x/tools/go/cfg.New in pkg:golang/golang.org/x/tools@v0.49.0
- 심벌
-
- golang.org/x/tools/go/cfg.New
- 생성일
- 2026-09-02T19:58:30Z
컨트랙트
- cfg.New constructs a control flow graph from an AST block statement for sequential code
- cfg.New creates branching basic blocks with two successors for conditional if-else statements
- cfg.New marks entry-reachable control flow blocks as Live
- cfg.New formats the control flow graph into a readable representation via CFG.Format
파일
- PROMPT.md
- cfganalyzer.go
- csx.json
- go.mod
- go.sum
- spec.json
- test/contract.go
소스
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 golang.org/x/tools/go/cfg.New in pkg:golang/golang.org/x/tools@v0.49.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/golang.org/x/tools@v0.49.0
Demonstrate these symbols/APIs:
- golang.org/x/tools/go/cfg.New
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.
package cfganalyzer
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"golang.org/x/tools/go/cfg"
)
// BuildCFG constructs a control flow graph for the given function body
// using default returnability analysis.
func BuildCFG(body *ast.BlockStmt) *cfg.CFG {
return cfg.New(body, func(call *ast.CallExpr) bool {
return true
})
}
// AnalyzeFunction parses a Go source code string containing a function
// and returns the constructed CFG alongside the token.FileSet.
func AnalyzeFunction(src string) (*cfg.CFG, *token.FileSet, error) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "src.go", src, 0)
if err != nil {
return nil, nil, fmt.Errorf("parsing source failed: %w", err)
}
for _, decl := range file.Decls {
if fn, ok := decl.(*ast.FuncDecl); ok && fn.Body != nil {
return BuildCFG(fn.Body), fset, nil
}
}
return nil, nil, fmt.Errorf("no function declaration with body found")
}
// CountBranchBlocks returns the number of blocks in the CFG that have more than one successor.
func CountBranchBlocks(c *cfg.CFG) int {
if c == nil {
return 0
}
count := 0
for _, b := range c.Blocks {
if len(b.Succs) > 1 {
count++
}
}
return count
}
// ReachableBlocks returns all basic blocks that are marked as live (reachable from entry).
func ReachableBlocks(c *cfg.CFG) []*cfg.Block {
if c == nil {
return nil
}
var reachable []*cfg.Block
for _, b := range c.Blocks {
if b.Live {
reachable = append(reachable, b)
}
}
return reachable
}
{"case":{"caseId":"case:sha256:873001d06f4c2b21ca2a1548fcf6545853dc9bc36b621d92437ae43446a14179","contract":["cfg.New constructs a control flow graph from an AST block statement for sequential code","cfg.New creates branching basic blocks with two successors for conditional if-else statements","cfg.New marks entry-reachable control flow blocks as Live","cfg.New formats the control flow graph into a readable representation via CFG.Format"],"goal":"verify golang.org/x/tools/go/cfg.New in pkg:golang/golang.org/x/tools@v0.49.0","kind":"HOW","packages":["pkg:golang/golang.org/x/tools@v0.49.0"],"schemaVersion":1,"symbols":["golang.org/x/tools/go/cfg.New"]},"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/golang.org/x/tools@v0.49.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/tools@v0.49.0","symbols":["golang.org/x/tools/go/cfg.New"],"verifierAdapter":"golang@1"}
module example.com/cfg-sample
go 1.25.0
require golang.org/x/tools v0.49.0
golang.org/x/mod v0.39.0 h1:UF5zwQdCRRUpHfyPwr7d4UrGiVeldIsogtzWVnczL74=
golang.org/x/mod v0.39.0/go.mod h1:bvIbwjQ0HUFFf5AKukeeYQG4ZBUG9yxQbR9aEweIwYY=
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI=
golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo=
{
"schemaVersion": 1,
"goal": "verify golang.org/x/tools/go/cfg.New in pkg:golang/golang.org/x/tools@v0.49.0",
"kind": "HOW",
"packages": [
"pkg:golang/golang.org/x/tools@v0.49.0"
],
"symbols": [
"golang.org/x/tools/go/cfg.New"
]
}
package main
import (
"fmt"
"os"
"strings"
cfganalyzer "example.com/cfg-sample"
"golang.org/x/tools/go/cfg"
)
func main() {
// Assertion 1: cfg.New constructs a control flow graph from an AST block statement for sequential code
seqSrc := `package p
func linear() int {
a := 1
b := 2
return a + b
}`
c1, fset1, err := cfganalyzer.AnalyzeFunction(seqSrc)
if err != nil {
fmt.Fprintf(os.Stderr, "FAIL: failed to analyze sequential function: %v\n", err)
os.Exit(1)
}
if c1 == nil || len(c1.Blocks) == 0 {
fmt.Fprintf(os.Stderr, "FAIL: expected non-empty blocks for sequential function\n")
os.Exit(1)
}
if cfganalyzer.CountBranchBlocks(c1) != 0 {
fmt.Fprintf(os.Stderr, "FAIL: expected 0 branch blocks in linear code, got %d\n", cfganalyzer.CountBranchBlocks(c1))
os.Exit(1)
}
// Assertion 2: cfg.New creates branching basic blocks with two successors for conditional if-else statements
branchSrc := `package p
func branching(x int) int {
if x > 0 {
return 1
} else {
return -1
}
}`
c2, fset2, err := cfganalyzer.AnalyzeFunction(branchSrc)
if err != nil {
fmt.Fprintf(os.Stderr, "FAIL: failed to analyze branching function: %v\n", err)
os.Exit(1)
}
if c2 == nil {
fmt.Fprintf(os.Stderr, "FAIL: c2 is nil\n")
os.Exit(1)
}
branchCount := cfganalyzer.CountBranchBlocks(c2)
if branchCount != 1 {
fmt.Fprintf(os.Stderr, "FAIL: expected 1 branch block with multiple successors, got %d\n", branchCount)
os.Exit(1)
}
entryBlock := c2.Blocks[0]
if len(entryBlock.Succs) != 2 {
fmt.Fprintf(os.Stderr, "FAIL: expected entry block to have 2 successors, got %d\n", len(entryBlock.Succs))
os.Exit(1)
}
// Assertion 3: cfg.New marks entry-reachable control flow blocks as Live
liveBlocks := cfganalyzer.ReachableBlocks(c2)
if len(liveBlocks) == 0 {
fmt.Fprintf(os.Stderr, "FAIL: expected reachable blocks to be non-empty\n")
os.Exit(1)
}
for _, b := range liveBlocks {
if !b.Live {
fmt.Fprintf(os.Stderr, "FAIL: block %d should have Live=true\n", b.Index)
os.Exit(1)
}
}
// Assertion 4: cfg.New formats the control flow graph into a readable representation via CFG.Format
formatted := c2.Format(fset2)
if !strings.Contains(formatted, ".0:") || !strings.Contains(formatted, "succs:") {
fmt.Fprintf(os.Stderr, "FAIL: formatted CFG missing expected markers: %q\n", formatted)
os.Exit(1)
}
_ = fset1
// Assertion 5: Direct call with mayReturn hook verifies custom non-returning function handling
deadSrc := `package p
func panicCaller() {
panic("fatal error")
println("unreachable")
}`
c3, _, err := cfganalyzer.AnalyzeFunction(deadSrc)
if err != nil {
fmt.Fprintf(os.Stderr, "FAIL: failed to analyze panic caller: %v\n", err)
os.Exit(1)
}
if c3 == nil {
fmt.Fprintf(os.Stderr, "FAIL: c3 is nil\n")
os.Exit(1)
}
// Direct instantiation testing block kinds
var foundIfThen, foundIfElse bool
for _, b := range c2.Blocks {
if b.Kind == cfg.KindIfThen {
foundIfThen = true
}
if b.Kind == cfg.KindIfElse {
foundIfElse = true
}
}
if !foundIfThen || !foundIfElse {
fmt.Fprintf(os.Stderr, "FAIL: expected KindIfThen and KindIfElse in branching CFG\n")
os.Exit(1)
}
fmt.Println("PASS: cfg.New contract passed")
}
오리진 시더
익명