[BoundsSafety] Tracking issue: upstreaming -fbounds-safety
This issue tracks the upstreaming of -fbounds-safety to Clang. Bounds annotations are upstreamed first, so that projects can start annotating their code; the full -fbounds-safety model follows. Until the model is complete, it is guarded by the experimental -cc1 flag -fexperimental-bounds-safety, which is not exposed through the driver.
Design: https://clang.llvm.org/docs/BoundsSafety.html
Preview implementation: https://github.com/swiftlang/llvm-project/tree/stable/21.x
Open issues and PRs: label clang:bounds-safety
Bounds annotations
| Feature | Status |
|---|---|
__counted_by, __sized_by, __counted_by_or_null, __sized_by_or_null on struct fields |
Available, with limits: the count must be a single identifier, and the annotations are handled as declaration attributes rather than type attributes |
Use of the annotations by -fsanitize=array-bounds and _FORTIFY_SOURCE=3 (__builtin_dynamic_object_size) |
Available |
| Bounds annotations as type attributes | In progress (draft: #233582) |
| Late parsing of bounds annotations as type attributes, for struct fields | NFC groundwork merged (#192799, #223267, #224556, #224558, #227774, #224559); in review (#239050); remaining work in progress (draft: #233583); tracked in #166411 |
__counted_by on function parameters, where the count refers to an earlier parameter |
In progress (drafts: #234941, #234950, #234963, #234981), contributed by Mohammed Ashraf as part of Google Summer of Code |
Late parsing of __counted_by on function parameters, where the count refers to a later parameter |
Planned: to be split out of #230538 (#167365) |
__counted_by on function return types and variables |
Planned: to be split out of #230538 (#167365) |
__single |
Planned (#238474); refactoring in progress downstream (swiftlang/llvm-project#13651, swiftlang/llvm-project#13846, swiftlang/llvm-project#13925), contributed by Anupam Kumar |
__ended_by, __null_terminated, __unsafe_indexable |
Planned (__ended_by: #238476, __null_terminated: #230685, __unsafe_indexable: #238475) |
| Complex count expressions | Planned (#141827, #229345) |
Full -fbounds-safety model
| Feature | Status |
|---|---|
Wide pointers (__bidi_indexable, __indexable) and bounds checks |
Planned (#238469) |
| New AST expressions for bounds checks | Planned (RFC needed; #238470) |
| Cast and operation checks on annotated pointers | Planned (#238471) |
| Implicit (default) bounds annotations | Planned (#238472) |
| Checks on assignments to a pointer and its count in the same scope | Planned (RFC needed; #238473) |
Roadmap
- Early 2027: bounds annotations are ready for open-source projects to adopt.
- After that: the full
-fbounds-safetymodel.
How to contribute
Contributions are welcome. Please coordinate with us before starting, by email to [email protected] or in the #fbounds-safety channel on the LLVM Discord. Open issues are tagged clang:bounds-safety. Patches for low-priority work that does not directly unblock upstreaming can still slow upstreaming down, because each one takes time to review and to reconcile with the downstream implementation.
The full -fbounds-safety model lives in the swiftlang/llvm-project fork, which is downstream of llvm/llvm-project and merges from it automatically. An upstream change in this area can therefore cause conflicts downstream, so upstream patches must also work with the full model downstream:
- Before merging a patch into
llvm/llvm-project, cherry-pick it to thenextbranch ofswiftlang/llvm-projectand check that it works with the full-fbounds-safetymodel. Trivial updates may not need this. - For more complex changes, the downstream may need to put the upstream behavior behind a flag, and then migrate to it gradually.
Assisted-by: Sonnet 5.5