CodeSampleX

Exemple

addr2line 0.25.1: find_location

Échantillon vérifié pour cargo addr2line 0.25.1: find_location. Le contrat s'est exécuté sur rust 1 · linux alpine/x64 · docker et a réussi.

sha256:30ccbbca2815f3c368eabaaa242841f14875211004b2fc651e4843ebda61412a

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-10-20

Cas

HOW
Objectif
verify find_location in pkg:cargo/addr2line@0.25.1
Paquets
Symboles
  • find_location
Créé
2026-10-20T00:22:53Z

Contrat

  1. Context::find_location returns Ok(None) when querying an unmapped address against an empty DWARF context
  2. Loader::find_location resolves the source file name and 1-based line number for a compiled ELF symbol SVMA
  3. Loader::find_location returns Ok(None) when querying an unmapped address (u32::MAX as u64)

Fichiers

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/main.rs
  • tests/contract.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 = "addr2line"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b"
dependencies = [
 "cpp_demangle",
 "fallible-iterator",
 "gimli",
 "memmap2",
 "object",
 "rustc-demangle",
 "smallvec",
 "typed-arena",
]

[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"

[[package]]
name = "cfg-if"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"

[[package]]
name = "cpp_demangle"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2bb79cb74d735044c972aae58ed0aaa9a837e85b01106a54c39e42e97f62253"
dependencies = [
 "cfg-if",
]

[[package]]
name = "crc32fast"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78"
dependencies = [
 "cfg-if",
]

[[package]]
name = "fallible-iterator"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"

[[package]]
name = "flate2"
version = "1.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb"
dependencies = [
 "crc32fast",
 "miniz_oxide",
 "zlib-rs",
]

[[package]]
name = "gimli"
version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
dependencies = [
 "fallible-iterator",
 "stable_deref_trait",
]

[[package]]
name = "libc"
version = "0.2.191"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"

[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"

[[package]]
name = "memmap2"
version = "0.9.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1219ed1b7f229ee7104d281dd01d6802fe28bb6e95d292942c4daacdeb798c0"
dependencies = [
 "libc",
]

[[package]]
name = "miniz_oxide"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c"
dependencies = [
 "adler2",
 "simd-adler32",
]

[[package]]
name = "object"
version = "0.37.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe"
dependencies = [
 "flate2",
 "memchr",
 "ruzstd",
]

[[package]]
name = "rustc-demangle"
version = "0.1.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb"

[[package]]
name = "ruzstd"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7c1c839d570d835527c9a5e4db7cb2198683a988cb9d7293fc8674e6bd58fc8"
dependencies = [
 "twox-hash",
]

[[package]]
name = "sample-addr2line-find-location"
version = "0.1.0"
dependencies = [
 "addr2line",
]

[[package]]
name = "simd-adler32"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"

[[package]]
name = "smallvec"
version = "1.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b"

[[package]]
name = "stable_deref_trait"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"

[[package]]
name = "twox-hash"
version = "2.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86a801b3cea342a06d468c8710662aa29e5e05e4f5c0d62f00bbb7f2ad7941c2"

[[package]]
name = "typed-arena"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a"

[[package]]
name = "zlib-rs"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112"
Cargo.toml
[package]
name = "sample-addr2line-find-location"
version = "0.1.0"
edition = "2021"

[dependencies]
addr2line = "=0.25.1"
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 find_location in pkg:cargo/addr2line@0.25.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/addr2line@0.25.1
Demonstrate these symbols/APIs:
  - find_location

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:b6615182a04bbad77f312253639e214673d7ae7d6468ba98909ded8299c09adb","contract":["Context::find_location returns Ok(None) when querying an unmapped address against an empty DWARF context","Loader::find_location resolves the source file name and 1-based line number for a compiled ELF symbol SVMA","Loader::find_location returns Ok(None) when querying an unmapped address (u32::MAX as u64)"],"goal":"verify find_location in pkg:cargo/addr2line@0.25.1","kind":"HOW","packages":["pkg:cargo/addr2line@0.25.1"],"schemaVersion":1,"symbols":["find_location"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/addr2line@0.25.1"],"schemaVersion":1,"subject":"pkg:cargo/addr2line@0.25.1","symbols":["find_location"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify find_location in pkg:cargo/addr2line@0.25.1",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/addr2line@0.25.1"
  ],
  "symbols": [
    "find_location"
  ]
}
src/main.rs
use addr2line::{gimli, Context, Loader};

#[no_mangle]
#[inline(never)]
pub extern "C" fn sample_target_probe(x: u64) -> u64 {
    std::hint::black_box(x + 42)
}

fn find_elf_symbol_svma(elf: &[u8], target_name: &str) -> Option<u64> {
    if elf.len() < 64 || &elf[0..4] != b"\x7fELF" || elf[4] != 2 || elf[5] != 1 {
        return None;
    }
    let e_shoff = u64::from_le_bytes(elf[40..48].try_into().ok()?) as usize;
    let e_shentsize = u16::from_le_bytes(elf[58..60].try_into().ok()?) as usize;
    let e_shnum = u16::from_le_bytes(elf[60..62].try_into().ok()?) as usize;

    for i in 0..e_shnum {
        let sh = e_shoff + i * e_shentsize;
        let sh_type = u32::from_le_bytes(elf[sh + 4..sh + 8].try_into().ok()?);
        if sh_type == 2 {
            let sym_off = u64::from_le_bytes(elf[sh + 24..sh + 32].try_into().ok()?) as usize;
            let sym_size = u64::from_le_bytes(elf[sh + 32..sh + 40].try_into().ok()?) as usize;
            let sh_link = u32::from_le_bytes(elf[sh + 40..sh + 44].try_into().ok()?) as usize;
            let sym_entsize = u64::from_le_bytes(elf[sh + 56..sh + 64].try_into().ok()?) as usize;

            let str_sh = e_shoff + sh_link * e_shentsize;
            let str_off = u64::from_le_bytes(elf[str_sh + 24..str_sh + 32].try_into().ok()?) as usize;
            let str_size = u64::from_le_bytes(elf[str_sh + 32..str_sh + 40].try_into().ok()?) as usize;
            let strtab = &elf[str_off..str_off + str_size];

            let count = sym_size / sym_entsize;
            for j in 0..count {
                let s = sym_off + j * sym_entsize;
                let st_name = u32::from_le_bytes(elf[s..s + 4].try_into().ok()?) as usize;
                let st_value = u64::from_le_bytes(elf[s + 8..s + 16].try_into().ok()?);
                if st_name < strtab.len() {
                    let end = strtab[st_name..].iter().position(|&b| b == 0).unwrap_or(0);
                    if let Ok(name) = std::str::from_utf8(&strtab[st_name..st_name + end]) {
                        if name == target_name && st_value != 0 {
                            return Some(st_value);
                        }
                    }
                }
            }
        }
    }
    None
}

fn main() {
    assert_eq!(sample_target_probe(8), 50);

    // 1. Verify Context::find_location on an empty in-memory DWARF image returns Ok(None)
    let empty_dwarf = gimli::Dwarf::load(|_id| -> Result<_, gimli::Error> {
        Ok(gimli::EndianSlice::new(&[][..], gimli::LittleEndian))
    })
    .expect("Dwarf::load failed");
    let empty_ctx = Context::from_dwarf(empty_dwarf).expect("Context::from_dwarf failed");
    assert!(empty_ctx
        .find_location(0x1000)
        .expect("empty find_location failed")
        .is_none());

    // 2. Verify Loader::find_location resolves source file and line number for a compiled symbol SVMA
    let exe_path = std::env::current_exe().expect("failed to get current_exe");
    let elf_bytes = std::fs::read(&exe_path).expect("failed to read current_exe");
    let probe_addr = find_elf_symbol_svma(&elf_bytes, "sample_target_probe")
        .expect("failed to find sample_target_probe in ELF symtab");

    let loader = Loader::new(&exe_path).expect("failed to load current_exe with Loader::new");

    let loc = loader
        .find_location(probe_addr)
        .expect("find_location returned Err")
        .expect("find_location returned None");
    let file = loc.file.expect("location missing file");
    assert!(file.ends_with("main.rs"), "unexpected file: {}", file);
    assert_eq!(loc.line, Some(5));

    assert!(loader
        .find_location(u32::MAX as u64)
        .expect("find_location(u32::MAX) returned Err")
        .is_none());
}
tests/contract.rs
use addr2line::{gimli, Context, Loader};

#[no_mangle]
#[inline(never)]
pub extern "C" fn sample_target_probe(x: u64) -> u64 {
    std::hint::black_box(x + 42)
}

fn find_elf_symbol_svma(elf: &[u8], target_name: &str) -> Option<u64> {
    if elf.len() < 64 || &elf[0..4] != b"\x7fELF" || elf[4] != 2 || elf[5] != 1 {
        return None;
    }
    let e_shoff = u64::from_le_bytes(elf[40..48].try_into().ok()?) as usize;
    let e_shentsize = u16::from_le_bytes(elf[58..60].try_into().ok()?) as usize;
    let e_shnum = u16::from_le_bytes(elf[60..62].try_into().ok()?) as usize;

    for i in 0..e_shnum {
        let sh = e_shoff + i * e_shentsize;
        let sh_type = u32::from_le_bytes(elf[sh + 4..sh + 8].try_into().ok()?);
        if sh_type == 2 {
            let sym_off = u64::from_le_bytes(elf[sh + 24..sh + 32].try_into().ok()?) as usize;
            let sym_size = u64::from_le_bytes(elf[sh + 32..sh + 40].try_into().ok()?) as usize;
            let sh_link = u32::from_le_bytes(elf[sh + 40..sh + 44].try_into().ok()?) as usize;
            let sym_entsize = u64::from_le_bytes(elf[sh + 56..sh + 64].try_into().ok()?) as usize;

            let str_sh = e_shoff + sh_link * e_shentsize;
            let str_off = u64::from_le_bytes(elf[str_sh + 24..str_sh + 32].try_into().ok()?) as usize;
            let str_size = u64::from_le_bytes(elf[str_sh + 32..str_sh + 40].try_into().ok()?) as usize;
            let strtab = &elf[str_off..str_off + str_size];

            let count = sym_size / sym_entsize;
            for j in 0..count {
                let s = sym_off + j * sym_entsize;
                let st_name = u32::from_le_bytes(elf[s..s + 4].try_into().ok()?) as usize;
                let st_value = u64::from_le_bytes(elf[s + 8..s + 16].try_into().ok()?);
                if st_name < strtab.len() {
                    let end = strtab[st_name..].iter().position(|&b| b == 0).unwrap_or(0);
                    if let Ok(name) = std::str::from_utf8(&strtab[st_name..st_name + end]) {
                        if name == target_name && st_value != 0 {
                            return Some(st_value);
                        }
                    }
                }
            }
        }
    }
    None
}

#[test]
fn contract_addr2line_find_location() {
    assert_eq!(sample_target_probe(8), 50);

    let empty_dwarf = gimli::Dwarf::load(|_id| -> Result<_, gimli::Error> {
        Ok(gimli::EndianSlice::new(&[][..], gimli::LittleEndian))
    })
    .expect("Dwarf::load failed");
    let empty_ctx = Context::from_dwarf(empty_dwarf).expect("Context::from_dwarf failed");
    assert!(empty_ctx
        .find_location(0x1000)
        .expect("empty find_location failed")
        .is_none());

    let exe_path = std::env::current_exe().expect("failed to get current_exe");
    let elf_bytes = std::fs::read(&exe_path).expect("failed to read current_exe");
    let probe_addr = find_elf_symbol_svma(&elf_bytes, "sample_target_probe")
        .expect("failed to find sample_target_probe in ELF symtab");

    let loader = Loader::new(&exe_path).expect("failed to load current_exe with Loader::new");

    let loc = loader
        .find_location(probe_addr)
        .expect("find_location returned Err")
        .expect("find_location returned None");
    let file = loc.file.expect("location missing file");
    assert!(file.ends_with("contract.rs"), "unexpected file: {}", file);
    assert_eq!(loc.line, Some(5));

    assert!(loader
        .find_location(u32::MAX as u64)
        .expect("find_location(u32::MAX) returned Err")
        .is_none());
}

Seeder d'origine

anonyme