Skip to content

fix!: bound a checked conversion by its run of objects - #83

Merged
owenthcarey merged 6 commits into
mainfrom
fix/checked-conversion-extent
Dec 4, 2026
Merged

owenthcarey merged 6 commits into
mainfrom
fix/checked-conversion-extent

Conversation

@owenthcarey

Copy link
Copy Markdown
Contributor

RFC 0055's adversarial round (G10, probe t9_grow.c) found a false pointer-out-of-bounds stop in Safe C. It happens on main too and in every build (fast, -fweavec-safe-checks=full, -O0). A safe function grew a block through a void * and read the new element:

struct b *WEAVEC_OWNED bs = calloc(6, sizeof *bs);
void *WEAVEC_OWNED g = realloc(bs, 7 * sizeof *bs);
struct b *WEAVEC_OWNED ds = g;
ds[6].n;   // weavec: ... 80 bytes after the 16-byte bounds of the pointer

Cause

realloc and the ownership rules are not involved: void *g = calloc(7, sizeof(struct b)) stops the same way. The implicit ds = g is one of RFC 0054's checked conversions (§2.1 (a)). What a checked conversion gave, __weavec.object(p, sizeof(T)), was bounded by GuardSafe as one object of the target's size (amendment 2).

The checker lets ds be indexed, because its value is an allocation (index-single). So the rules accepted the program and the bound then stopped it. A conversion straight from an allocation call is not checked, which is why struct b *ds = realloc(...) already worked.

What changes

  • Runtime. __weavec_rt_cast now returns a uint64_t: the bytes from the pointer that the record of what holds it shows to be objects of the target's type, one after another.
    • The rest of a heap block, global or recorded local that is an array of the type (its size a multiple of the type's), from an element's start.
    • The remaining elements of an array member of that type.
    • Whole elements of a plain scalar over a plain object.
    • A block with no type that takes the conversion's type (amendment 14) is an array of it when its size is a multiple of the type's.
    • Everything else is one object: a member that is not an array, a header with data after it, memory with no record, a null pointer, or a polymorphic downcast (which reads a vtable, not a record).
  • Passes. GuardTypes passes the result as the marker's new operand, __weavec.object(p, size, extent), and GuardSafe bounds the root by p + extent.
  • Unchanged. The fast mode still believes only the first object, and accesses at constant offsets inside it still need no check. A pointer the checker sees as one object (a void * parameter's) still cannot be indexed. A read past the grown block's new size still stops.
  • ABI. WEAVEC_RT_ABI goes from 49 to 50, because the entry point's signature changed. PR 82 also raises it to 50, so whichever of the two merges second must make it 51.

RFC 0054 amendment 22 records this. §2.1 already says a conversion gives "the bound its declaration states", and a local's bound is its value's (RFC 0042), so I treated it as a correction of implementation amendment 2 rather than a new decision. If that counts as changing a decision under docs/rfcs/README.md, it needs a superseding RFC instead. The rules page (pointer-cast) and the Safe C guide say what the bound is.

Code lines: lib/ and runtime/ (tests excluded) gain 96 and lose 40.

Tests

  • test/Guards/cast-extent.c: in the fast, checked and -O0 builds, element 6 of the grown block reads, and element 7 stops with pointer-out-of-bounds at the read ("0 bytes after the 112-byte bounds").
  • test/Guards/ir-cast.c: the bound is p + phi(16, __weavec_rt_cast(...)). ir-cast.cpp and report-json.c are updated for the new return type and ABI number.
  • test/cases/sum/cast-grown.c and cast-grown_ok.c: a grown block, an int array and a struct that only begins the block's type. Past the end stops; in bounds runs clean.
  • rt_test type-at-run: what the runtime returns for each case.

Verification

darwin-arm64, Homebrew LLVM, dev build:

  • lit build/dev/test: 217 of 217 pass.
  • ctest --preset dev: 56 of 56 pass, including every cases-* suite and runtime.
  • scripts/check-format.sh, check-spelling.py and check-hygiene.py pass.
  • Before the fix, the probe and the three variants above stopped on main at 13d6774d.

Not run: Linux, clang-tidy.

A safe C function that grew a block through a `void *` stopped falsely
with `pointer-out-of-bounds`, in the fast, checked and -O0 builds:

    struct b *bs = calloc(6, sizeof *bs);
    void *g = realloc(bs, 7 * sizeof *bs);
    struct b *ds = g;
    ds[6].n;   /* "80 bytes after the 16-byte bounds" */

`realloc` was not the cause; `void *g = calloc(7, ...)` stopped the same
way. The implicit conversion of `g` to `struct b *` is one of RFC 0054's
checked conversions, and what it gave was bounded as one object of the
target's size (amendment 2). The checker lets the pointer be indexed,
because its value is an allocation (`index-single`), so the rules accepted
a program the bound then stopped. A conversion straight from an
allocation call is not checked, which is why `struct b *ds = realloc(...)`
already worked.

`__weavec_rt_cast` now returns the bytes from the pointer that the
record of what holds it shows to be objects of the target's type, one
after another: to the end of a block, global or recorded local that is an
array of the type, to the last element of an array member, whole elements
of a plain scalar over a plain object, and one object otherwise (a member,
a header with data after it, memory with no record, a polymorphic
downcast). GuardTypes passes it to `__weavec.object(p, size, extent)`, and
GuardSafe bounds the root by it. The fast mode still believes only the
first object, and accesses inside it still need no check. A read past the
grown block's new size still stops.

The entry point's signature changed, so WEAVEC_RT_ABI is 50 (report-json.c
pins it).

RFC 0054 amendment 22. Tests: test/Guards/cast-extent.c (fast, full and
-O0; element 6 reads, element 7 stops), ir-cast.c, cases
test/cases/sum/cast-grown*.c, rt_test type-at-run.
clang-tidy's misc-const-correctness flagged the CallInst pointer.
A record nested in each element of an array member is flattened with the
element's stride, so `objectHolds` counted the gaps between its copies as
more objects of it: converting a block of `struct row { struct cell
cells[3]; }` with `struct cell { struct b c; long y; }` to `struct b *`
gave 48 bytes, and `c[1]` read `cells[0].y` (or, beside a pointer member,
overwrote it). The run of an array member is now counted only when its
stride is the target's size; otherwise the conversion gives one object.

RFC 0054 amendment 22. Tests: rt_test type-at-run (a row of cells), the
`nested` input of test/cases/sum/cast-grown.c and its clean twin.
A `long *` converted from a `void *` to a block of `struct tally { long
xs[4]; char *p; }` stopped falsely at `xs[1]`: the layout recorded no part
for a scalar array member, and over an object that holds pointers a plain
scalar was one element. The layout now records an array member of an
arithmetic or enum type as a part, and `objectHolds` reads the parts before
falling back to one scalar, so the run reaches the member's last element
(amendment 22). A read past it into the pointer still stops.

Tests: rt_test type-at-run (a tally gives 32 and 16), the `scalars` input
of test/cases/sum/cast-grown.c and its clean twin.
RFC 0055 (#82) took ABI 50, which this branch had also used for
__weavec_rt_cast's new signature. RFC 0054 amendment 22.
@owenthcarey owenthcarey changed the title fix!: bound a checked conversion by the objects the runtime finds fix!: bound a checked conversion by its run of objects Dec 1, 2026
@owenthcarey
owenthcarey merged commit 1f47ae9 into main Dec 4, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant