Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .cursor/skills/adopt-c-bounds-safety/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
when_to_use: |
When working with, reading, reviewing, comparing, debugging or analyzing C code that has adopted -fbounds-safety or wants to adopt it. Key syntax to look for Bounds annotations (__counted_by, __counted_by_or_null, __sized_by, __sized_by_or_null, __ended_by, __single, __indexable, __bidi_indexable, __unsafe_indexable, __null_terminated, __terminated_by), its helper functions (e.g.: __unsafe_forge_bidi_indexable, __unsafe_forge_single, __null_terminated_to_indexable, __unsafe_null_terminated_to_indexable, __unsafe_null_terminated_from_indexable) or other macros (e.g. __ptrcheck_abi_assume_single) or includes of "ptrcheck.h".
effort: high
description: |
Guide for the C -fbounds-safety language extension. Covers the language model, pointer annotations, adopting bounds-safety in existing C code, compiler build settings and modes, and runtime debugging of bounds violations.
name: adopt-c-bounds-safety
---
## How to Use This Skill

When helping with `-fbounds-safety` adoption or code changes, ask clarifying questions about the user's codebase and goals before suggesting changes. For complex tasks involving multiple files or non-trivial annotation decisions, use plan mode to propose an approach before implementing.

# `-fbounds-safety` Language Extension

`-fbounds-safety` is a C language extension that prevents out-of-bounds memory access by enforcing bounds safety at the language level. It inserts automatic bounds checks at runtime, rejects unsafe pointer operations at compile time, and requires programmers to provide bounds annotations so the compiler can guarantee safety. Out-of-bounds accesses become deterministic traps instead of exploitable vulnerabilities.

## Detailed Documentation

### Required reading before adoption work

You MUST have fully read the following three documents (via the Read tool) at the start of an adoption task, and re-read them via the Read tool before any source-modifying step in the adoption workflow unless their content is verifiably fresh in your active context:

- [adoption-strategies.md](references/adoption-strategies.md) — the workflow for adopting `-fbounds-safety` in an existing C project (full and header-only modes).
- [language-overview.md](references/language-overview.md) — the language reference for `-fbounds-safety`: pointer kinds, annotations, and the rules that govern them.
- [common-patterns-and-pitfalls.md](references/common-patterns-and-pitfalls.md) — recipes and anti-patterns encountered during real-world adoption.

### Other references (read on demand)

For compiler flags, Xcode build settings, soft trap mode, and `ptrcheck.h` configuration, read [build-settings.md](references/build-settings.md).

For debugging bounds violations at runtime — trap behavior, LLDB commands, wide pointer inspection, watchpoints, crash log analysis, and soft trap debugging, read [runtime-debugging.md](references/runtime-debugging.md).

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Build Settings for `-fbounds-safety`

This document covers compiler flags, build system configuration, and related settings for enabling `-fbounds-safety`.

## Enabling `-fbounds-safety`

### Per-File Enablement (Recommended for Incremental Adoption)

Most projects adopt `-fbounds-safety` incrementally, enabling it one file at a time as a per-file build flag. See [adoption-strategies.md](adoption-strategies.md) for the adoption workflow.

### Project-Wide Enablement (After Adoption Is Complete)

Once adoption is complete across an entire target or project, you can enable `-fbounds-safety` globally. This is desirable because it controls enablement from a single location, making it easier to switch on or off.

**Xcode:** Add the custom build setting `ENABLE_C_BOUNDS_SAFETY=YES`. This applies `-fbounds-safety` only to C files — it will not bleed onto C++, Objective-C, or Objective-C++ files (unlike adding the flag to project-level C flags directly, which would).

**Other Build Systems:** Pass `-fbounds-safety` to Clang for each C source file.

No additional link-time libraries are required. Clients (including non-bounds-safe ones) should be oblivious to the change.

## Useful Flags

### `-ferror-limit=0`

Removes the limit on compiler errors. Useful during adoption to see all diagnostics at once rather than fixing errors one batch at a time.

### `-ffreestanding`

For projects without access to a `strlen` implementation. When converting `__null_terminated` pointers to indexable, `-fbounds-safety` may insert a `strlen` call. The `-ffreestanding` flag makes the compiler generate a character-counting loop instead.

### `-fbounds-safety-unique-traps`

Prevents trap merging in optimized builds. By default, the optimizer merges all traps in a function into one (to reduce code size), making it difficult to determine which specific bounds check failed. This flag preserves separate trap locations, making optimized-build debugging much easier.

### `-fbounds-safety-soft-traps=call-minimal`

Enables soft trap mode. Soft traps log violations instead of terminating the program — the compiler emits calls to `__bounds_safety_soft_trap` instead of trap instructions, allowing execution to continue after a bounds check failure. This is useful during adoption to discover multiple issues in a single run rather than fixing them one at a time. After all files compile and all traps are fixed use of soft trap mode **must be removed** to actually get the security benefit.

**Xcode:** Add the build setting `CLANG_BOUNDS_SAFETY_SOFT_TRAPS=call-minimal`. This enables soft trap mode for every source file that uses `ENABLE_C_BOUNDS_SAFETY`. For files where you manually pass `-fbounds-safety`, add the flag directly.

**Other build systems:** Pass `-fbounds-safety-soft-traps=call-minimal` to every source file that uses `-fbounds-safety`.

See [runtime-debugging.md](runtime-debugging.md) for more information on debugging with soft traps.
Loading
Loading