Building Safe C
How it is built
- Base: Apple's swiftlang/llvm-project
nextbranch, which is LLVMmainplus Apple's patches, including-fbounds-safety. - Null safety: the nullability-safety fork, applied on top. Its own docs are in NULLSAFE.md.
- Safe C: the
-fsafe-cflag and the rules above.
cmake -G Ninja -S llvm -B build -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON \
-DLLVM_ENABLE_PROJECTS=clang -DCLANG_ENABLE_CIR=ON -DLLVM_TARGETS_TO_BUILD=AArch64
ninja -C build clang
build/bin/clang -fsafe-c -isysroot $(xcrun --show-sdk-path) example.c
What -fsafe-c turns on
One flag; no other flags are needed:
-fbounds-safety(Apple's bounds checks).- Pointers are non-null unless marked
_Nullable, checked by flow analysis. - Safe C's ownership rules, checked on ClangIR.
- Clang's thread-safety analysis (
-Wthread-safety), for_Guarded_by,_Requires,_Acquiresand_Releases. _FORTIFY_SOURCEoff: its checks are replaced by the bounds checks.- Every memory-safety finding is an error, not a warning, and
-wor-Wno-...cannot turn it off. Code inside__unsafe { }is exempt.
Prior work
Cyclone and Checked C made C dialects; Fil-C makes C safe at run time with a garbage collector. Safe C stays plain C, checked inside Clang: at compile time where it can be proven, with a trap where it cannot.