CodeSampleX

Exemple

allocator-api2 0.2.21: allocator_api2::alloc::Allocator

Échantillon vérifié pour cargo allocator-api2 0.2.21: allocator_api2::alloc::Allocator. Le contrat s'est exécuté sur rust 1 · linux alpine/x64 · docker et a…

sha256:c8607273cca682390e38f0d3b2ad5917abd1bb5552f20b1b0e5fb5a7cdbe2470

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 x64 cargo

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-11-05

Cas

HOW
Objectif
verify allocator_api2::alloc::Allocator in pkg:cargo/allocator-api2@0.2.21
Paquets
Symboles
  • allocator_api2::alloc::Allocator
Créé
2026-11-05T21:28:59Z

Contrat

  1. Allocator::allocate_zeroed(layout) returns NonNull<[u8]> of at least layout.size() zero-initialized bytes
  2. Allocator::grow and Allocator::shrink resize an allocated block while preserving existing prefix bytes
  3. Allocator::deallocate frees memory allocated or resized by the same allocator
  4. Allocator::by_ref borrows the allocator for use with Box::new_in and Vec::with_capacity_in

Fichiers

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/main.rs

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

Code source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "allocator-api2"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"

[[package]]
name = "sample-allocator-api2"
version = "0.1.0"
dependencies = [
 "allocator-api2",
]
Cargo.toml
[package]
name = "sample-allocator-api2"
version = "0.1.0"
edition = "2021"

[dependencies]
allocator-api2 = "=0.2.21"
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 allocator_api2::alloc::Allocator in pkg:cargo/allocator-api2@0.2.21
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/allocator-api2@0.2.21
Demonstrate these symbols/APIs:
  - allocator_api2::alloc::Allocator

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:8ac139117db5c76cbf3d73719a71d2c5734b65240f67f3dd00719a762713b8b7","contract":["Allocator::allocate_zeroed(layout) returns NonNull\u003c[u8]\u003e of at least layout.size() zero-initialized bytes","Allocator::grow and Allocator::shrink resize an allocated block while preserving existing prefix bytes","Allocator::deallocate frees memory allocated or resized by the same allocator","Allocator::by_ref borrows the allocator for use with Box::new_in and Vec::with_capacity_in"],"goal":"verify allocator_api2::alloc::Allocator in pkg:cargo/allocator-api2@0.2.21","kind":"HOW","packages":["pkg:cargo/allocator-api2@0.2.21"],"schemaVersion":1,"symbols":["allocator_api2::alloc::Allocator"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/allocator-api2@0.2.21"],"schemaVersion":1,"subject":"pkg:cargo/allocator-api2@0.2.21","symbols":["allocator_api2::alloc::Allocator"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify allocator_api2::alloc::Allocator in pkg:cargo/allocator-api2@0.2.21",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/allocator-api2@0.2.21"
  ],
  "symbols": [
    "allocator_api2::alloc::Allocator"
  ]
}
src/main.rs
use std::alloc::Layout;
use allocator_api2::alloc::{Allocator, Global};
use allocator_api2::boxed::Box;
use allocator_api2::vec::Vec;

fn allocate_and_fill<A: Allocator>(alloc: &A, count: usize, value: u8) -> Vec<u8, &A> {
    let mut vec = Vec::with_capacity_in(count, alloc.by_ref());
    vec.resize(count, value);
    vec
}

fn main() {
    let global = Global;

    // 1. Direct allocation and deallocation via the Allocator trait
    let layout = Layout::from_size_align(64, 8).expect("valid layout");
    let ptr = global.allocate_zeroed(layout).expect("allocation succeeds");
    assert_eq!(ptr.len(), 64);
    let slice: &[u8] = unsafe { ptr.as_ref() };
    assert!(slice.iter().all(|&b| b == 0));
    unsafe {
        global.deallocate(ptr.cast::<u8>(), layout);
    }

    // 2. Grow and shrink via the Allocator trait
    let initial_layout = Layout::from_size_align(16, 8).expect("valid layout");
    let grown_layout = Layout::from_size_align(32, 8).expect("valid layout");
    let shrunk_layout = Layout::from_size_align(8, 8).expect("valid layout");

    let block = global.allocate(initial_layout).expect("initial alloc");
    unsafe {
        std::ptr::write_bytes(block.cast::<u8>().as_ptr(), 0xAB, 16);
        let grown = global
            .grow(block.cast::<u8>(), initial_layout, grown_layout)
            .expect("grow succeeds");
        assert!(grown.len() >= 32);
        let grown_bytes = grown.as_ref();
        assert!(grown_bytes[..16].iter().all(|&b| b == 0xAB));

        let shrunk = global
            .shrink(grown.cast::<u8>(), grown_layout, shrunk_layout)
            .expect("shrink succeeds");
        assert!(shrunk.len() >= 8);
        assert!(shrunk.as_ref()[..8].iter().all(|&b| b == 0xAB));

        global.deallocate(shrunk.cast::<u8>(), shrunk_layout);
    }

    // 3. Using Allocator with Box and Vec via generic bound and by_ref
    let boxed = Box::new_in(42_u64, &global);
    assert_eq!(*boxed, 42);

    let vec = allocate_and_fill(&global, 4, 7);
    assert_eq!(vec.as_slice(), &[7, 7, 7, 7]);
}

Seeder d'origine

anonyme