CodeSampleX

Exemple

golang.org/x/sys v0.31.0: unix.Eventfd, unix.Pipe2, unix.Sysinfo

Échantillon vérifié pour golang golang.org/x/sys v0.31.0: unix.Eventfd, unix.Pipe2, unix.Sysinfo. Le contrat s'est exécuté sur go 1.26 · linux alpine/x64 …

sha256:de25fc2ddaccd391333f5ce63c4e1c26c012950282d2a3ea08ade8ee2ebb77c6

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 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-27

Cas

HOW
Objectif
verify pkg:golang/golang.org/x/sys@v0.31.0
Paquets
Symboles
  • unix.Eventfd
  • unix.Pipe2
  • unix.Sysinfo
Créé
2026-08-27T12:15:18Z

Contrat

  1. unix.Sysinfo returns system statistics including positive uptime, totalram, and process count without error
  2. unix.Eventfd creates a non-blocking event file descriptor that records initial counter value and accumulates writes
  3. unix.Pipe2 creates a non-blocking pipe pair with close-on-exec enabled for inter-process communication

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/golang.org/x/sys@v0.31.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/sys@v0.31.0
Demonstrate these symbols/APIs:
  - unix.Eventfd
  - unix.Pipe2
  - unix.Sysinfo

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:202826a7158e540ce94ce7eb594b88c300f16f3bd96e41f13986c4f332c8132e","contract":["unix.Sysinfo returns system statistics including positive uptime, totalram, and process count without error","unix.Eventfd creates a non-blocking event file descriptor that records initial counter value and accumulates writes","unix.Pipe2 creates a non-blocking pipe pair with close-on-exec enabled for inter-process communication"],"goal":"verify pkg:golang/golang.org/x/sys@v0.31.0","kind":"HOW","packages":["pkg:golang/golang.org/x/sys@v0.31.0"],"schemaVersion":1,"symbols":["unix.Eventfd","unix.Pipe2","unix.Sysinfo"]},"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/sys@v0.31.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/sys@v0.31.0","symbols":["unix.Eventfd","unix.Pipe2","unix.Sysinfo"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/sys v0.31.0
go.sum
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
main.go
package main

import (
	"encoding/binary"
	"fmt"
	"os"

	"golang.org/x/sys/unix"
)

// GetSystemStatistics retrieves system info using unix.Sysinfo.
func GetSystemStatistics() (*unix.Sysinfo_t, error) {
	var info unix.Sysinfo_t
	if err := unix.Sysinfo(&info); err != nil {
		return nil, fmt.Errorf("sysinfo failed: %w", err)
	}
	return &info, nil
}

// CreateEventNotification creates an eventfd descriptor initialized with a counter value.
func CreateEventNotification(initval uint) (int, error) {
	fd, err := unix.Eventfd(initval, unix.EFD_CLOEXEC|unix.EFD_NONBLOCK)
	if err != nil {
		return -1, fmt.Errorf("eventfd failed: %w", err)
	}
	return fd, nil
}

// CreateNonblockingPipe creates a pipe with nonblocking and close-on-exec flags.
func CreateNonblockingPipe() ([2]int, error) {
	var fds [2]int
	if err := unix.Pipe2(fds[:], unix.O_CLOEXEC|unix.O_NONBLOCK); err != nil {
		return fds, fmt.Errorf("pipe2 failed: %w", err)
	}
	return fds, nil
}

func main() {
	info, err := GetSystemStatistics()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error getting system info: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("System Uptime: %d seconds, Total RAM: %d bytes, Procs: %d\n", info.Uptime, info.Totalram, info.Procs)

	efd, err := CreateEventNotification(1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error creating eventfd: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(efd)

	var writeVal uint64 = 4
	var buf [8]byte
	binary.LittleEndian.PutUint64(buf[:], writeVal)
	if _, err := unix.Write(efd, buf[:]); err != nil {
		fmt.Fprintf(os.Stderr, "Error writing to eventfd: %v\n", err)
		os.Exit(1)
	}

	pipeFds, err := CreateNonblockingPipe()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error creating pipe2: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(pipeFds[0])
	defer unix.Close(pipeFds[1])

	msg := []byte("csx-sys-pipe-payload")
	if _, err := unix.Write(pipeFds[1], msg); err != nil {
		fmt.Fprintf(os.Stderr, "Error writing to pipe: %v\n", err)
		os.Exit(1)
	}
	readBuf := make([]byte, len(msg))
	if _, err := unix.Read(pipeFds[0], readBuf); err != nil {
		fmt.Fprintf(os.Stderr, "Error reading from pipe: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Pipe communicated message: %s\n", string(readBuf))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/sys@v0.31.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/sys@v0.31.0"
  ],
  "symbols": [
    "unix.Eventfd",
    "unix.Pipe2",
    "unix.Sysinfo"
  ]
}
test/contract.go
package main

import (
	"encoding/binary"
	"fmt"
	"os"

	"golang.org/x/sys/unix"
)

func main() {
	// Assertion 1: unix.Sysinfo returns system statistics including positive uptime, totalram, and process count without error
	var info unix.Sysinfo_t
	if err := unix.Sysinfo(&info); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Sysinfo failed: %v\n", err)
		os.Exit(1)
	}
	if info.Uptime <= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Sysinfo returned non-positive uptime: %d\n", info.Uptime)
		os.Exit(1)
	}
	if info.Totalram == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Sysinfo returned zero totalram\n")
		os.Exit(1)
	}
	if info.Procs == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Sysinfo returned zero process count\n")
		os.Exit(1)
	}

	// Assertion 2: unix.Eventfd creates a non-blocking event file descriptor that records initial counter value and accumulates writes
	efd, err := unix.Eventfd(10, unix.EFD_CLOEXEC|unix.EFD_NONBLOCK)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Eventfd failed: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(efd)
	if efd < 0 {
		fmt.Fprintf(os.Stderr, "FAIL: invalid eventfd descriptor: %d\n", efd)
		os.Exit(1)
	}

	var incVal uint64 = 25
	var writeBuf [8]byte
	binary.LittleEndian.PutUint64(writeBuf[:], incVal)
	wn, err := unix.Write(efd, writeBuf[:])
	if err != nil || wn != 8 {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Write on eventfd failed: wn=%d, err=%v\n", wn, err)
		os.Exit(1)
	}

	var readBuf [8]byte
	rn, err := unix.Read(efd, readBuf[:])
	if err != nil || rn != 8 {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Read on eventfd failed: rn=%d, err=%v\n", rn, err)
		os.Exit(1)
	}
	val := binary.LittleEndian.Uint64(readBuf[:])
	expectedVal := uint64(35) // 10 initial + 25 written
	if val != expectedVal {
		fmt.Fprintf(os.Stderr, "FAIL: eventfd read value mismatch: got %d, expected %d\n", val, expectedVal)
		os.Exit(1)
	}

	// Assertion 3: unix.Pipe2 creates a non-blocking pipe pair with close-on-exec enabled for inter-process communication
	var pFds [2]int
	if err := unix.Pipe2(pFds[:], unix.O_CLOEXEC|unix.O_NONBLOCK); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Pipe2 failed: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(pFds[0])
	defer unix.Close(pFds[1])
	if pFds[0] < 0 || pFds[1] < 0 {
		fmt.Fprintf(os.Stderr, "FAIL: invalid pipe descriptors: read=%d, write=%d\n", pFds[0], pFds[1])
		os.Exit(1)
	}

	pipePayload := []byte("csx-sys-pipe2-contract-payload")
	pwn, err := unix.Write(pFds[1], pipePayload)
	if err != nil || pwn != len(pipePayload) {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Write on pipe failed: pwn=%d, err=%v\n", pwn, err)
		os.Exit(1)
	}

	pipeReadBuf := make([]byte, len(pipePayload))
	prn, err := unix.Read(pFds[0], pipeReadBuf)
	if err != nil || prn != len(pipePayload) {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Read on pipe failed: prn=%d, err=%v\n", prn, err)
		os.Exit(1)
	}
	if string(pipeReadBuf) != string(pipePayload) {
		fmt.Fprintf(os.Stderr, "FAIL: pipe payload mismatch: got %q, expected %q\n", string(pipeReadBuf), string(pipePayload))
		os.Exit(1)
	}

	fmt.Println("PASS: golang.org/x/sys contract passed")
}

Seeder d'origine

anonyme