CodeSampleX

Beispiel

golang.org/x/sys v0.31.0: unix.O_CLOEXEC

Verifiziertes Beispiel für golang golang.org/x/sys v0.31.0: unix.O_CLOEXEC. Der Vertrag lief auf go 1.26 · linux alpine/x64 · docker und bestand.

sha256:29351663cb3f5cc15f11ac2e3d667c9ae3427a62a9a0ef0f277b2b61d80fde3e

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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-30

Fall

HOW
Ziel
verify golang.org/x/sys/unix.O_CLOEXEC in pkg:golang/golang.org/x/sys@v0.31.0
Pakete
Symbole
  • golang.org/x/sys/unix.O_CLOEXEC
Erstellt
2026-08-30T16:42:34Z

Contract

  1. unix.O_CLOEXEC is non-zero and opening a file with unix.O_CLOEXEC sets the FD_CLOEXEC flag
  2. Opening a file without unix.O_CLOEXEC does not set the FD_CLOEXEC flag initially
  3. unix.Pipe2 with unix.O_CLOEXEC creates read and write file descriptors with FD_CLOEXEC set
  4. unix.Openat with unix.O_CLOEXEC opens relative file descriptors with FD_CLOEXEC set

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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 golang.org/x/sys/unix.O_CLOEXEC in 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:
  - golang.org/x/sys/unix.O_CLOEXEC

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:21d52d174c3a939035944830a2cfaf7828a05d40d7382da9d810ebd7055f129f","contract":["unix.O_CLOEXEC is non-zero and opening a file with unix.O_CLOEXEC sets the FD_CLOEXEC flag","Opening a file without unix.O_CLOEXEC does not set the FD_CLOEXEC flag initially","unix.Pipe2 with unix.O_CLOEXEC creates read and write file descriptors with FD_CLOEXEC set","unix.Openat with unix.O_CLOEXEC opens relative file descriptors with FD_CLOEXEC set"],"goal":"verify golang.org/x/sys/unix.O_CLOEXEC in pkg:golang/golang.org/x/sys@v0.31.0","kind":"HOW","packages":["pkg:golang/golang.org/x/sys@v0.31.0"],"schemaVersion":1,"symbols":["golang.org/x/sys/unix.O_CLOEXEC"]},"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":["golang.org/x/sys/unix.O_CLOEXEC"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.23.0

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 (
	"fmt"
	"os"
	"path/filepath"

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

// OpenWithCLOEXEC opens or creates a file with O_CLOEXEC flag set.
func OpenWithCLOEXEC(path string, flags int, mode uint32) (int, error) {
	fd, err := unix.Open(path, flags|unix.O_CLOEXEC, mode)
	if err != nil {
		return -1, fmt.Errorf("unix.Open failed: %w", err)
	}
	return fd, nil
}

// IsCloseOnExec checks whether the FD_CLOEXEC flag is set on the given file descriptor.
func IsCloseOnExec(fd int) (bool, error) {
	flags, err := unix.FcntlInt(uintptr(fd), unix.F_GETFD, 0)
	if err != nil {
		return false, fmt.Errorf("unix.FcntlInt F_GETFD failed: %w", err)
	}
	return (flags & unix.FD_CLOEXEC) != 0, nil
}

// CreatePipeWithCLOEXEC creates a pipe with both ends having O_CLOEXEC set.
func CreatePipeWithCLOEXEC() ([2]int, error) {
	var p [2]int
	if err := unix.Pipe2(p[:], unix.O_CLOEXEC); err != nil {
		return p, fmt.Errorf("unix.Pipe2 failed: %w", err)
	}
	return p, nil
}

func main() {
	tmpDir, err := os.MkdirTemp("", "csx-cloexec-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error creating temp dir: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(tmpDir)

	filePath := filepath.Join(tmpDir, "example.txt")

	// Open file with O_CLOEXEC
	fd, err := OpenWithCLOEXEC(filePath, unix.O_RDWR|unix.O_CREAT|unix.O_TRUNC, 0600)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error opening file: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(fd)

	cloexec, err := IsCloseOnExec(fd)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error checking close-on-exec: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("File opened with O_CLOEXEC (fd: %d, FD_CLOEXEC: %v)\n", fd, cloexec)

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

	rCLOEXEC, _ := IsCloseOnExec(pipeFds[0])
	wCLOEXEC, _ := IsCloseOnExec(pipeFds[1])
	fmt.Printf("Pipe created with O_CLOEXEC (read fd: %d, cloexec: %v; write fd: %d, cloexec: %v)\n",
		pipeFds[0], rCLOEXEC, pipeFds[1], wCLOEXEC)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/sys/unix.O_CLOEXEC in pkg:golang/golang.org/x/sys@v0.31.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/sys@v0.31.0"
  ],
  "symbols": [
    "golang.org/x/sys/unix.O_CLOEXEC"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"path/filepath"

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

func main() {
	tmpDir, err := os.MkdirTemp("", "cloexec-contract-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: failed to create temp dir: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(tmpDir)

	// Assertion 1: unix.O_CLOEXEC is non-zero and opening a file with unix.O_CLOEXEC sets the FD_CLOEXEC flag
	if unix.O_CLOEXEC == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected unix.O_CLOEXEC to be non-zero\n")
		os.Exit(1)
	}

	testFile := filepath.Join(tmpDir, "cloexec.txt")
	fdWithCloexec, err := unix.Open(testFile, unix.O_RDWR|unix.O_CREAT|unix.O_TRUNC|unix.O_CLOEXEC, 0600)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Open with unix.O_CLOEXEC failed: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(fdWithCloexec)

	flags1, err := unix.FcntlInt(uintptr(fdWithCloexec), unix.F_GETFD, 0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.FcntlInt F_GETFD failed: %v\n", err)
		os.Exit(1)
	}
	if flags1&unix.FD_CLOEXEC == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected FD_CLOEXEC flag to be set when opened with unix.O_CLOEXEC\n")
		os.Exit(1)
	}

	// Assertion 2: Opening a file without unix.O_CLOEXEC does not set the FD_CLOEXEC flag initially
	noCloexecFile := filepath.Join(tmpDir, "nocloexec.txt")
	fdWithoutCloexec, err := unix.Open(noCloexecFile, unix.O_RDWR|unix.O_CREAT|unix.O_TRUNC, 0600)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Open without unix.O_CLOEXEC failed: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(fdWithoutCloexec)

	flags2, err := unix.FcntlInt(uintptr(fdWithoutCloexec), unix.F_GETFD, 0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.FcntlInt F_GETFD failed: %v\n", err)
		os.Exit(1)
	}
	if flags2&unix.FD_CLOEXEC != 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected FD_CLOEXEC flag NOT to be set when opened without unix.O_CLOEXEC\n")
		os.Exit(1)
	}

	// Assertion 3: unix.Pipe2 with unix.O_CLOEXEC creates read and write file descriptors with FD_CLOEXEC set
	var pipeFds [2]int
	if err := unix.Pipe2(pipeFds[:], unix.O_CLOEXEC); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Pipe2 with unix.O_CLOEXEC failed: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(pipeFds[0])
	defer unix.Close(pipeFds[1])

	readFlags, err := unix.FcntlInt(uintptr(pipeFds[0]), unix.F_GETFD, 0)
	if err != nil || readFlags&unix.FD_CLOEXEC == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected FD_CLOEXEC on pipe read fd: flags=%d, err=%v\n", readFlags, err)
		os.Exit(1)
	}

	writeFlags, err := unix.FcntlInt(uintptr(pipeFds[1]), unix.F_GETFD, 0)
	if err != nil || writeFlags&unix.FD_CLOEXEC == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected FD_CLOEXEC on pipe write fd: flags=%d, err=%v\n", writeFlags, err)
		os.Exit(1)
	}

	// Assertion 4: unix.Openat with unix.O_CLOEXEC opens relative file descriptors with FD_CLOEXEC set
	dirFd, err := unix.Open(tmpDir, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Open directory with unix.O_CLOEXEC failed: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(dirFd)

	atFd, err := unix.Openat(dirFd, "cloexec_at.txt", unix.O_RDWR|unix.O_CREAT|unix.O_TRUNC|unix.O_CLOEXEC, 0600)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: unix.Openat with unix.O_CLOEXEC failed: %v\n", err)
		os.Exit(1)
	}
	defer unix.Close(atFd)

	atFlags, err := unix.FcntlInt(uintptr(atFd), unix.F_GETFD, 0)
	if err != nil || atFlags&unix.FD_CLOEXEC == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected FD_CLOEXEC on openat fd: flags=%d, err=%v\n", atFlags, err)
		os.Exit(1)
	}

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

Ursprungs-Seeder

anonym