1 ======================================
2 Adoption Guide for ``-fbounds-safety``
3 ======================================
8 Where to get ``-fbounds-safety``
9 ================================
11 The open sourcing to llvm.org's ``llvm-project`` is still on going and the
12 feature is not available yet. In the mean time, the preview implementation is
14 `here <https://github.com/swiftlang/llvm-project/tree/stable/20240723>`_ in a
15 fork of ``llvm-project``. Please follow
16 `Building LLVM with CMake <https://llvm.org/docs/CMake.html>`_ to build the
22 Pass ``-fbounds-safety`` as a Clang compilation flag for the C file that you
23 want to adopt. We recommend adopting the model file by file, because adoption
24 requires some effort to add bounds annotations and fix compiler diagnostics.
26 Include ``ptrcheck.h``
27 ======================
29 ``ptrcheck.h`` is a Clang toolchain header to provide definition of the bounds
30 annotations such as ``__counted_by``, ``__counted_by_or_null``, ``__sized_by``,
31 etc. In the LLVM source tree, the header is located in
32 ``llvm-project/clang/lib/Headers/ptrcheck.h``.
35 Add bounds annotations on pointers as necessary
36 ===============================================
38 Annotate pointers on struct fields and function parameters if they are pointing
39 to an array of object, with appropriate bounds annotations. Please see
40 :doc:`BoundsSafety` to learn what kind of bounds annotations are available and
41 their semantics. Note that local pointer variables typically don't need bounds
42 annotations because they are implicitely a wide pointer (``__bidi_indexable``)
43 that automatically carries the bounds information.
45 Address compiler diagnostics
46 ============================
48 Once you pass ``-fbounds-safety`` to compiler a C file, you will see some new
49 compiler warnings and errors, which guide adoption of ``-fbounds-safety``.
50 Consider the following example:
56 void init_buf(int *p, int n) {
57 for (int i = 0; i < n; ++i)
58 p[i] = 0; // error: array subscript on single pointer 'p' must use a constant index of 0 to be in bounds
61 The parameter ``int *p`` doesn't have a bounds annotation, so the compiler will
62 complain about the code indexing into it (``p[i]``) as it assumes that ``p`` is
63 pointing to a single ``int`` object or null. To address the diagnostics, you
64 should add a bounds annotation on ``int *p`` so that the compiler can reason
65 about the safety of the array subscript. In the following example, ``p`` is now
66 ``int *__counted_by(n)``, so the compiler will allow the array subscript with
67 additional run-time checks as necessary.
73 void init_buf(int *__counted_by(n) p, int n) {
74 for (int i = 0; i < n; ++i)
75 p[i] = 0; // ok; `p` is now has a type with bounds annotation.
78 Run test suites to fix new run-time traps
79 =========================================
81 Adopting ``-fbounds-safety`` may cause your program to trap if it violates
82 bounds safety or it has incorrect adoption. Thus, it is necessary to perform
83 run-time testing of your program to gain confidence that it won't trap at
86 Repeat the process for each remaining file
87 ==========================================
89 Once you've done with adopting a single C file, please repeat the same process
90 for each remaining C file that you want to adopt.