Skip to content

Modeled C: no clause for a caller-owned buffer and its length (zlib compress/uncompress, bzip2 BZ2_bzBuffToBuff*); a plain fn compress / lend renders a safe call over raw pointers with no bounds contract #1621

Closed

Description

@ehartford

Found by the ruling §66 example rewrite (zlib release UAT, build/release_uat_fixtures/zlib_main.w), left as is: zlib is the first library whose surface the facade language cannot express, so no zlib/bzip2/libcurl facade is written (§65: never half-model).

The surface. int compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) and uncompress (and bzip2's BZ2_bzBuffToBuffCompress/Decompress) take no resource: a caller-owned output buffer whose capacity is passed in *destLen and whose written length comes back through it, and a caller-owned input buffer with its length. The clause vocabulary (spec §16.2b, Parser.parse_facade_clause: from/init/preinit/drop/destroys/ok/borrows/independent/movable/lend/consumes[destroyed_by]/retains-by/returns borrow|static/preserves/of/rename/thread/callback_thread/callback consumes|param-userdata) has nothing that pairs a pointer parameter with a length parameter. §16.2b.8 covers foreign-owned buffers (a resource) and foreign strings only.

What lend does today (evidence). This facade checks clean and runs (stage1 of c18fdfe):

use c_import("zlib.h", link: "z")
c facade zlib:
    fn compress
        lend
    fn uncompress
        lend
    fn zlibVersion
        returns static CStr
fn main:
    var compressed: [u8; 256] = [0 as u8; 256]
    var compressed_len: uLongf = compressed.len() as uLongf
    let input = "with zlib roundtrip"
    let rc = compress(&raw mut compressed[0] as *mut Bytef, &raw mut compressed_len as *mut uLongf, input as *const Bytef, input.len() as uLong)
    print(f"rc={rc} {zlibVersion().unwrap().to_str().unwrap()}")   // rc=0 1.2.12

Without the facade the same call is refused (raw c_import function call requires unsafe context). With lend — which asserts only "not retained, consumed or destroyed" — the call becomes safe while the program still spells every raw pointer and cast, and the compiler holds no contract between dest and *destLen: a caller passing 1024 in compressed_len over a 256-byte array corrupts memory in safe code. That is the partial model §16.2b.3 forbids ("a partial model that could create unsafety is a compile error"), and the ceremony left in the program (&raw mut compressed[0] as *mut Bytef, &raw mut compressed_len as *mut uLongf, input as *const Bytef) is what a facade exists to remove.

Missing clause (proposal). A buffer contract on an fn item, e.g.

fn compress
    buffer param 0 len param 1 out      // dest: &mut [u8]; *destLen = capacity in, written out
    buffer param 2 len param 3          // source: &[u8]; sourceLen = its length

rendering compress(dest: &mut [u8], source: &[u8]) -> (c_int, i64) (status and the written length), with the length parameters computed by the compiler from the slices. Until it exists, lend on an fn whose parameters include an unmodeled raw pointer should not render a safe call (or should warn with provenance), so the gap is visible instead of silently safe.

Refs: ruling §65/§66; spec §16.2b.3, §16.2b.5, §16.2b.8; #1376 (user-programs-safe red on zlib/bzip2/libcurl).

Activity

  1. ehartford commented on Sep 24, 2026

    @ehartford
    ContributorAuthor

    Ruled (Eric 2026-09-24; §16.2b.8 "Buffers" / D64): buffer param P len param L (input []u8) and buffer param P capacity param L inout (writable []mut u8, capacity in, bounds-checked produced length out as the usize result; caller's slice unchanged; bytes only; presented on success per the status contract). An unpaired raw pointer parameter is not a buffer; lend/presentation on it is refused. Implementation in the next modeled-C batch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions