CodeSampleX

Exemple

modernc.org/cc/v4 v4.29.1

Échantillon vérifié pour golang modernc.org/cc/v4 v4.29.1. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et a réussi.

sha256:2bd5dde55f02fe08fae3eb646cbd53db625809d48ea35ae805c78caf242aab5f

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré linux 24 · ubuntu · glibc 2.39 x64 go

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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-16

Cas

HOW
Objectif
verify pkg:golang/modernc.org/cc/v4@v4.29.1
Paquets
Créé
2026-09-16T01:40:18Z

Contrat

  1. cc.NewABI creates target ABI with defined primitive type sizes for linux amd64
  2. cc.Translate parses and type checks C source code into an AST
  3. cc.Translate populates file scope with function declarators capturing return type and parameters
  4. cc.Translate populates file scope with struct definitions calculating member offsets and struct size
  5. cc.Translate with EvalAllMacros enabled registers constant object-like and function-like macros
  6. cc.Translate returns an error when encountering invalid C syntax

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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 pkg:golang/modernc.org/cc/v4@v4.29.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/modernc.org/cc/v4@v4.29.1

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:812f47260af1bc4d7baf9ad37c8a9eb24fc6b7ad72d010ce060ed322fca5347f","contract":["cc.NewABI creates target ABI with defined primitive type sizes for linux amd64","cc.Translate parses and type checks C source code into an AST","cc.Translate populates file scope with function declarators capturing return type and parameters","cc.Translate populates file scope with struct definitions calculating member offsets and struct size","cc.Translate with EvalAllMacros enabled registers constant object-like and function-like macros","cc.Translate returns an error when encountering invalid C syntax"],"goal":"verify pkg:golang/modernc.org/cc/v4@v4.29.1","kind":"HOW","packages":["pkg:golang/modernc.org/cc/v4@v4.29.1"],"schemaVersion":1},"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/modernc.org/cc/v4@v4.29.1"],"schemaVersion":1,"subject":"pkg:golang/modernc.org/cc/v4@v4.29.1","verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require (
	github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
	modernc.org/cc/v4 v4.29.1 // indirect
	modernc.org/mathutil v1.7.1 // indirect
	modernc.org/opt v0.2.0 // indirect
	modernc.org/sortutil v1.2.1 // indirect
	modernc.org/strutil v1.2.1 // indirect
	modernc.org/token v1.1.0 // indirect
)
go.sum
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
modernc.org/cc/v4 v4.29.1 h1:MKgdCV3WykTSPqpVrnxdEDS0HEd2FHpKZDzxzU5LyeI=
modernc.org/cc/v4 v4.29.1/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg=
modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
main.go
package main

import (
	"fmt"
	"log"

	"modernc.org/cc/v4"
)

// ParseAndAnalyze translates C code into an AST and inspects declared functions and structs.
func ParseAndAnalyze(cSource string) (*cc.AST, error) {
	abi, err := cc.NewABI("linux", "amd64")
	if err != nil {
		return nil, fmt.Errorf("failed to create ABI: %w", err)
	}

	cfg := &cc.Config{
		ABI:           abi,
		EvalAllMacros: true,
	}

	builtinHeader := `
int __predefined_declarator;
typedef unsigned long size_t;
typedef long ptrdiff_t;
typedef int wchar_t;
`
	sources := []cc.Source{
		{Name: "<builtin>", Value: builtinHeader},
		{Name: "input.c", Value: cSource},
	}

	return cc.Translate(cfg, sources)
}

func main() {
	cCode := `
#define BUFFER_CAPACITY 512
#define MAX(a, b) ((a) > (b) ? (a) : (b))

struct Point {
    int x;
    int y;
};

int calculate_distance_sq(struct Point *p1, struct Point *p2) {
    int dx = p2->x - p1->x;
    int dy = p2->y - p1->y;
    return dx * dx + dy * dy;
}
`

	ast, err := ParseAndAnalyze(cCode)
	if err != nil {
		log.Fatalf("ParseAndAnalyze failed: %v", err)
	}

	fmt.Printf("AST translated successfully with %d file-scope symbols\n", len(ast.Scope.Nodes))

	if nodes, ok := ast.Scope.Nodes["calculate_distance_sq"]; ok && len(nodes) > 0 {
		if decl, ok := nodes[0].(*cc.Declarator); ok {
			fmt.Printf("Function: %s, Return Type: %s\n", decl.Name(), decl.Type())
		}
	}

	if nodes, ok := ast.Scope.Nodes["Point"]; ok && len(nodes) > 0 {
		if spec, ok := nodes[0].(*cc.StructOrUnionSpecifier); ok {
			if st, ok := spec.Type().(*cc.StructType); ok {
				fmt.Printf("Struct Point: size=%d bytes, fields=%d\n", st.Size(), st.NumFields())
			}
		}
	}

	if macro, ok := ast.Macros["BUFFER_CAPACITY"]; ok {
		fmt.Printf("Macro BUFFER_CAPACITY defined: %v\n", macro.Value != nil)
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/modernc.org/cc/v4@v4.29.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/modernc.org/cc/v4@v4.29.1"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"modernc.org/cc/v4"
)

func main() {
	// Assertion 1: cc.NewABI creates target ABI with defined primitive type sizes for linux amd64
	abi, err := cc.NewABI("linux", "amd64")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Assertion 1 failed: cc.NewABI error: %v\n", err)
		os.Exit(1)
	}
	if abi.Types[cc.Int].Size != 4 {
		fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected sizeof(int)=4, got %d\n", abi.Types[cc.Int].Size)
		os.Exit(1)
	}
	if abi.Types[cc.Long].Size != 8 {
		fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected sizeof(long)=8, got %d\n", abi.Types[cc.Long].Size)
		os.Exit(1)
	}
	if abi.Types[cc.Double].Size != 8 {
		fmt.Fprintf(os.Stderr, "Assertion 1 failed: expected sizeof(double)=8, got %d\n", abi.Types[cc.Double].Size)
		os.Exit(1)
	}

	cfg := &cc.Config{
		ABI:           abi,
		EvalAllMacros: true,
	}

	builtin := `
int __predefined_declarator;
typedef unsigned long size_t;
typedef long ptrdiff_t;
typedef int wchar_t;
`

	validC := `
#define BUFFER_SIZE 2048
#define CLAMP(v, min, max) ((v) < (min) ? (min) : ((v) > (max) ? (max) : (v)))

struct Vector3D {
    int x;
    int y;
    int z;
};

int dot_product(struct Vector3D *a, struct Vector3D *b) {
    return a->x * b->x + a->y * b->y + a->z * b->z;
}
`

	sources := []cc.Source{
		{Name: "<builtin>", Value: builtin},
		{Name: "sample.c", Value: validC},
	}

	// Assertion 2: cc.Translate parses and type checks C source code into an AST
	ast, err := cc.Translate(cfg, sources)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Assertion 2 failed: cc.Translate returned error: %v\n", err)
		os.Exit(1)
	}
	if ast == nil || ast.Scope == nil {
		fmt.Fprintf(os.Stderr, "Assertion 2 failed: expected non-nil AST and scope\n")
		os.Exit(1)
	}

	// Assertion 3: cc.Translate populates file scope with function declarators capturing return type and parameters
	dotNodes := ast.Scope.Nodes["dot_product"]
	if len(dotNodes) == 0 {
		fmt.Fprintf(os.Stderr, "Assertion 3 failed: dot_product not found in scope\n")
		os.Exit(1)
	}
	decl, ok := dotNodes[0].(*cc.Declarator)
	if !ok {
		fmt.Fprintf(os.Stderr, "Assertion 3 failed: expected *cc.Declarator, got %T\n", dotNodes[0])
		os.Exit(1)
	}
	if decl.Name() != "dot_product" {
		fmt.Fprintf(os.Stderr, "Assertion 3 failed: expected function name 'dot_product', got %q\n", decl.Name())
		os.Exit(1)
	}
	fnType, ok := decl.Type().(*cc.FunctionType)
	if !ok {
		fmt.Fprintf(os.Stderr, "Assertion 3 failed: expected *cc.FunctionType, got %T\n", decl.Type())
		os.Exit(1)
	}
	if fnType.Result().Kind() != cc.Int {
		fmt.Fprintf(os.Stderr, "Assertion 3 failed: expected return type int, got %v\n", fnType.Result())
		os.Exit(1)
	}
	params := fnType.Parameters()
	if len(params) != 2 {
		fmt.Fprintf(os.Stderr, "Assertion 3 failed: expected 2 parameters, got %d\n", len(params))
		os.Exit(1)
	}
	if params[0].Name() != "a" || params[1].Name() != "b" {
		fmt.Fprintf(os.Stderr, "Assertion 3 failed: unexpected parameter names: %s, %s\n", params[0].Name(), params[1].Name())
		os.Exit(1)
	}

	// Assertion 4: cc.Translate populates file scope with struct definitions calculating member offsets and struct size
	vecNodes := ast.Scope.Nodes["Vector3D"]
	if len(vecNodes) == 0 {
		fmt.Fprintf(os.Stderr, "Assertion 4 failed: Vector3D not found in scope\n")
		os.Exit(1)
	}
	spec, ok := vecNodes[0].(*cc.StructOrUnionSpecifier)
	if !ok {
		fmt.Fprintf(os.Stderr, "Assertion 4 failed: expected *cc.StructOrUnionSpecifier, got %T\n", vecNodes[0])
		os.Exit(1)
	}
	st, ok := spec.Type().(*cc.StructType)
	if !ok {
		fmt.Fprintf(os.Stderr, "Assertion 4 failed: expected *cc.StructType, got %T\n", spec.Type())
		os.Exit(1)
	}
	if st.NumFields() != 3 {
		fmt.Fprintf(os.Stderr, "Assertion 4 failed: expected 3 fields in Vector3D, got %d\n", st.NumFields())
		os.Exit(1)
	}
	if st.Size() != 12 {
		fmt.Fprintf(os.Stderr, "Assertion 4 failed: expected struct size 12, got %d\n", st.Size())
		os.Exit(1)
	}
	f0 := st.FieldByIndex(0)
	f1 := st.FieldByIndex(1)
	f2 := st.FieldByIndex(2)
	if f0.Name() != "x" || f0.Offset() != 0 || f1.Name() != "y" || f1.Offset() != 4 || f2.Name() != "z" || f2.Offset() != 8 {
		fmt.Fprintf(os.Stderr, "Assertion 4 failed: field offsets mismatch: (%s:%d, %s:%d, %s:%d)\n",
			f0.Name(), f0.Offset(), f1.Name(), f1.Offset(), f2.Name(), f2.Offset())
		os.Exit(1)
	}

	// Assertion 5: cc.Translate with EvalAllMacros enabled registers constant object-like and function-like macros
	bufMacro, ok := ast.Macros["BUFFER_SIZE"]
	if !ok || bufMacro == nil {
		fmt.Fprintf(os.Stderr, "Assertion 5 failed: BUFFER_SIZE macro not found\n")
		os.Exit(1)
	}
	if bufMacro.IsFnLike {
		fmt.Fprintf(os.Stderr, "Assertion 5 failed: BUFFER_SIZE should not be function-like\n")
		os.Exit(1)
	}
	clampMacro, ok := ast.Macros["CLAMP"]
	if !ok || clampMacro == nil {
		fmt.Fprintf(os.Stderr, "Assertion 5 failed: CLAMP macro not found\n")
		os.Exit(1)
	}
	if !clampMacro.IsFnLike {
		fmt.Fprintf(os.Stderr, "Assertion 5 failed: CLAMP should be function-like\n")
		os.Exit(1)
	}

	// Assertion 6: cc.Translate returns an error when encountering invalid C syntax
	invalidSources := []cc.Source{
		{Name: "<builtin>", Value: builtin},
		{Name: "bad.c", Value: "int invalid syntax here {"},
	}
	_, errInvalid := cc.Translate(cfg, invalidSources)
	if errInvalid == nil {
		fmt.Fprintf(os.Stderr, "Assertion 6 failed: expected error on invalid syntax, got nil\n")
		os.Exit(1)
	}

	fmt.Println("All contract assertions passed successfully.")
}

Seeder d'origine

anonyme