Adopting Safe C
Same code, any compiler
Safe C does not lock you in. Every annotation comes from one header, safec.h:
- With the Safe C compiler and
-fsafe-c, the annotations are checked and the safety rules are enforced. - With GCC or any Clang (Xcode's, a stock one),
safec.hdefines every annotation as nothing, or as the plain GNU C it stands for. The same source compiles as ordinary C, without the checks._Takeneeds GNU C (a statement expression): it does not build on MSVC.
safec.h is a single file with no dependencies. The Safe C compiler ships it; for other
compilers, copy clang/lib/Headers/safec.h into your project
(Apache 2.0 with LLVM exceptions, like the rest of LLVM). -fsafe-c
includes it automatically.
Adopting Safe C, one file at a time
Nobody annotates a whole codebase at once. Safe C is adopted per file, and the code builds and ships at every step.
The unit is a file.
-fsafe-cis a per-file flag. Safe files and plain C files link together unchanged: same ABI.Four levels per file:
Level What it means What you get 0. Plain C untouched builds as before 1. Annotated interface the header has _Nullable,__counted_by, ...every safe caller is checked against it 2. Safe, with __unsafe-fsafe-c, the hard spots wrapped in__unsafeeverything outside those blocks is checked 3. Fully checked -fsafe-c, no__unsafeor_Trustedevery rule is checked Ownership can be left off for a file that is not ready:
-mllvm -safe-c-ownership=false.Calling code that is not safe yet. Put
#pragma clang safe_c legacyat the top of a header that is not ready. Safe C then treats it like a system header: its code is not checked and its declarations are trusted. The trust boundary is one searchable line per header, not one__unsafeper call, and-Rsafe-c-unsafelists every legacy header in a build. The pragma is an error in the file being compiled (that file is always checked), and outside Safe C it does nothing.#pragma clang system_headerin a user file is rejected, so this is the only way to declare one. Not yet done: requiring legacy declarations' pointers to be checked before use (today they are trusted as declared).Order: code that handles untrusted input first (parsers, decoders, protocol handlers), where most memory-safety CVEs are; leaf utilities next; the core last.
Tooling (planned): an annotator that proposes annotations from inference and compiler fix-its, each one checked by the compiler; a report of each file's level and
__unsafecount; and a CI ratchet, so a file that reaches level 2 never goes back.
Why a fork and not a set of flags
Even where Clang has a feature, a set of flags does not give a guarantee:
- Pieces are missing. Bounds-carrying pointers are only in Apple's fork. Flow-sensitive
null checking, ownership and
__unsafeare only in this one. - Most of it is warnings. Any of them can be turned off with
-Wno-...or a pragma. Under-fsafe-cthey are errors that no flag or pragma can lower. - Flags can be dropped. One missing flag in one build file silently removes a check.
-fsafe-crejects the flags that would weaken it (-fno-bounds-safetyand so on). - The defaults are wrong. Upstream, an unannotated pointer means "unknown".
Under
-fsafe-cit means non-null and exactly one element, so forgetting an annotation is a compile error, not a hole. The exception is code you choose to trust: system headers,-isystemdirectories and headers marked legacy. - The escape hatch must be visible. The only way out is
__unsafe { }in the source, where a reviewer can see and search for it.
Standards and portability
The C standard committee is unlikely to adopt any of this soon, and it does not need to.
Compiler extensions are how C has always moved: GNU C, __attribute__, Apple's blocks,
Microsoft's SAL. Safe C code stays portable through safec.h (see Same code, any
compiler).
The code is still C. It compiles with the normal C ABI and links with any C library.
Plain C libraries
A C library nobody checked is not safe to call, and Safe C does not try to make it so. Put its header in a -isystem directory and call it from a small
function marked __unsafe, which is responsible for what it passes.