CodeSampleX

Exemple

modernc.org/memory v1.5.0

Échantillon vérifié pour golang modernc.org/memory v1.5.0. Le contrat s'est exécuté sur go 1.26 · linux alpine/x64 · docker et a réussi.

sha256:9a97c7453795e851b8021d9aea29c31142cdeba19c69cb5f4d0063be3656d173

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:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-20

Cas

HOW
Objectif
verify pkg:golang/modernc.org/memory@v1.5.0
Paquets
Créé
2026-08-19T23:14:17Z

Contrat

  1. Allocator.Calloc allocates zero-initialized memory slices and Allocator.Free releases them
  2. Allocator.Malloc allocates uninitialized memory slices and Allocator.Realloc resizes slices preserving existing content
  3. UsableSize reports the actual usable capacity of allocated memory slices
  4. Allocator.UintptrCalloc and Allocator.UintptrFree manage memory allocations using uintptr addresses
  5. Allocator.Close releases OS resources allocated by the Allocator instance

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/memory@v1.5.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/modernc.org/memory@v1.5.0
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:1d6fe7c4d5894fd413381e248e6721f4d7cadb4f27f7f32dcb1722b6adfe52e2","contract":["Allocator.Calloc allocates zero-initialized memory slices and Allocator.Free releases them","Allocator.Malloc allocates uninitialized memory slices and Allocator.Realloc resizes slices preserving existing content","UsableSize reports the actual usable capacity of allocated memory slices","Allocator.UintptrCalloc and Allocator.UintptrFree manage memory allocations using uintptr addresses","Allocator.Close releases OS resources allocated by the Allocator instance"],"goal":"verify pkg:golang/modernc.org/memory@v1.5.0","kind":"HOW","packages":["pkg:golang/modernc.org/memory@v1.5.0"],"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/memory@v1.5.0"],"schemaVersion":1,"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require modernc.org/memory v1.5.0
go.sum
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
main.go
package main

import (
	"fmt"
	"modernc.org/memory"
)

// BufferPool manages memory buffers using a modernc.org/memory Allocator.
type BufferPool struct {
	allocator memory.Allocator
}

// NewBufferPool creates an initialized BufferPool.
func NewBufferPool() *BufferPool {
	return &BufferPool{}
}

// AllocateZeroed allocates a zero-initialized byte slice of the requested size.
func (p *BufferPool) AllocateZeroed(size int) ([]byte, error) {
	return p.allocator.Calloc(size)
}

// AllocateRaw allocates an uninitialized byte slice of the requested size.
func (p *BufferPool) AllocateRaw(size int) ([]byte, error) {
	return p.allocator.Malloc(size)
}

// Resize resizes an existing byte slice to a new size, preserving original contents.
func (p *BufferPool) Resize(b []byte, newSize int) ([]byte, error) {
	return p.allocator.Realloc(b, newSize)
}

// Release deallocates a previously allocated byte slice.
func (p *BufferPool) Release(b []byte) error {
	return p.allocator.Free(b)
}

// GetCapacity returns the actual usable capacity of an allocated byte slice.
func (p *BufferPool) GetCapacity(b []byte) int {
	if len(b) == 0 {
		return 0
	}
	return memory.UsableSize(&b[0])
}

// Close releases all OS resources backing the buffer pool.
func (p *BufferPool) Close() error {
	return p.allocator.Close()
}

func main() {
	pool := NewBufferPool()
	defer pool.Close()

	buf, err := pool.AllocateZeroed(64)
	if err != nil {
		fmt.Printf("Allocation failed: %v\n", err)
		return
	}

	copy(buf, []byte("CodeSampleX verification"))
	fmt.Printf("Allocated buffer with %d bytes, usable capacity %d\n", len(buf), pool.GetCapacity(buf))

	resized, err := pool.Resize(buf, 128)
	if err != nil {
		fmt.Printf("Reallocation failed: %v\n", err)
		return
	}

	fmt.Printf("Resized buffer to %d bytes: %s\n", len(resized), string(resized[:24]))

	if err := pool.Release(resized); err != nil {
		fmt.Printf("Release failed: %v\n", err)
		return
	}

	fmt.Println("Memory management completed successfully.")
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/modernc.org/memory@v1.5.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/modernc.org/memory@v1.5.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"modernc.org/memory"
)

func main() {
	// Assertion 1: Allocator.Calloc allocates zero-initialized memory slices and Allocator.Free releases them
	{
		var alloc memory.Allocator
		size := 128
		buf, err := alloc.Calloc(size)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Calloc(%d) returned error: %v\n", size, err)
			os.Exit(1)
		}
		if len(buf) != size {
			fmt.Fprintf(os.Stderr, "FAIL: Calloc(%d) returned slice of length %d\n", size, len(buf))
			os.Exit(1)
		}
		for i, b := range buf {
			if b != 0 {
				fmt.Fprintf(os.Stderr, "FAIL: Calloc(%d) byte at index %d is non-zero: %d\n", size, i, b)
				os.Exit(1)
			}
		}
		if err := alloc.Free(buf); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Free returned error: %v\n", err)
			os.Exit(1)
		}
		_ = alloc.Close()
	}

	// Assertion 2: Allocator.Malloc allocates uninitialized memory slices and Allocator.Realloc resizes slices preserving existing content
	{
		var alloc memory.Allocator
		initialSize := 32
		buf, err := alloc.Malloc(initialSize)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Malloc(%d) returned error: %v\n", initialSize, err)
			os.Exit(1)
		}
		if len(buf) != initialSize {
			fmt.Fprintf(os.Stderr, "FAIL: Malloc(%d) returned slice of length %d\n", initialSize, len(buf))
			os.Exit(1)
		}

		payload := []byte("sample payload data 12345")
		copy(buf, payload)

		expandedSize := 96
		resizedBuf, err := alloc.Realloc(buf, expandedSize)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Realloc to %d returned error: %v\n", expandedSize, err)
			os.Exit(1)
		}
		if len(resizedBuf) != expandedSize {
			fmt.Fprintf(os.Stderr, "FAIL: Realloc to %d returned slice of length %d\n", expandedSize, len(resizedBuf))
			os.Exit(1)
		}
		for i, b := range payload {
			if resizedBuf[i] != b {
				fmt.Fprintf(os.Stderr, "FAIL: Realloc corrupted data at index %d: expected %d, got %d\n", i, b, resizedBuf[i])
				os.Exit(1)
			}
		}

		if err := alloc.Free(resizedBuf); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Free after Realloc returned error: %v\n", err)
			os.Exit(1)
		}
		_ = alloc.Close()
	}

	// Assertion 3: UsableSize reports the actual usable capacity of allocated memory slices
	{
		var alloc memory.Allocator
		reqSize := 48
		buf, err := alloc.Calloc(reqSize)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Calloc(%d) error: %v\n", reqSize, err)
			os.Exit(1)
		}
		usable := memory.UsableSize(&buf[0])
		if usable < reqSize {
			fmt.Fprintf(os.Stderr, "FAIL: UsableSize(%d) returned %d, expected >= %d\n", reqSize, usable, reqSize)
			os.Exit(1)
		}
		if err := alloc.Free(buf); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Free error: %v\n", err)
			os.Exit(1)
		}
		_ = alloc.Close()
	}

	// Assertion 4: Allocator.UintptrCalloc and Allocator.UintptrFree manage memory allocations using uintptr addresses
	{
		var alloc memory.Allocator
		reqSize := 64
		ptr, err := alloc.UintptrCalloc(reqSize)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: UintptrCalloc(%d) error: %v\n", reqSize, err)
			os.Exit(1)
		}
		if ptr == 0 {
			fmt.Fprintf(os.Stderr, "FAIL: UintptrCalloc returned nil pointer (0)\n")
			os.Exit(1)
		}
		usable := memory.UintptrUsableSize(ptr)
		if usable < reqSize {
			fmt.Fprintf(os.Stderr, "FAIL: UintptrUsableSize returned %d, expected >= %d\n", usable, reqSize)
			os.Exit(1)
		}
		if err := alloc.UintptrFree(ptr); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: UintptrFree error: %v\n", err)
			os.Exit(1)
		}
		_ = alloc.Close()
	}

	// Assertion 5: Allocator.Close releases OS resources allocated by the Allocator instance
	{
		var alloc memory.Allocator
		buf, err := alloc.Calloc(1024)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Calloc before Close returned error: %v\n", err)
			os.Exit(1)
		}
		_ = alloc.Free(buf)
		if err := alloc.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: Close returned error: %v\n", err)
			os.Exit(1)
		}
	}

	fmt.Println("PASS: modernc.org/memory contract verified")
}

Seeder d'origine

anonyme