Repository navigation
fix!: bound a checked conversion by its run of objects - #83
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RFC 0055's adversarial round (G10, probe
t9_grow.c) found a falsepointer-out-of-boundsstop 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 avoid *and read the new element:Cause
reallocand the ownership rules are not involved:void *g = calloc(7, sizeof(struct b))stops the same way. The implicitds = gis 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
dsbe 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 whystruct b *ds = realloc(...)already worked.What changes
__weavec_rt_castnow returns auint64_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.__weavec.object(p, size, extent), and GuardSafe bounds the root byp + extent.void *parameter's) still cannot be indexed. A read past the grown block's new size still stops.WEAVEC_RT_ABIgoes 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/andruntime/(tests excluded) gain 96 and lose 40.Tests
test/Guards/cast-extent.c: in the fast, checked and-O0builds, element 6 of the grown block reads, and element 7 stops withpointer-out-of-boundsat the read ("0 bytes after the 112-byte bounds").test/Guards/ir-cast.c: the bound isp + phi(16, __weavec_rt_cast(...)).ir-cast.cppandreport-json.care updated for the new return type and ABI number.test/cases/sum/cast-grown.candcast-grown_ok.c: a grown block, anintarray 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 everycases-*suite andruntime.scripts/check-format.sh,check-spelling.pyandcheck-hygiene.pypass.13d6774d.Not run: Linux, clang-tidy.