-
Notifications
You must be signed in to change notification settings - Fork 15.9k
[BoundsSafety][Doc] Add BoundsSafetyAdoptionGuide.rst #120674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,87 @@ | ||||||||
| ====================================== | ||||||||
| Adoption Guide for ``-fbounds-safety`` | ||||||||
| ====================================== | ||||||||
|
|
||||||||
| .. contents:: | ||||||||
| :local: | ||||||||
|
|
||||||||
| Where to get ``-fbounds-safety`` | ||||||||
| ================================ | ||||||||
|
|
||||||||
| The open sourcing to llvm.org's ``llvm-project`` is still on going and the | ||||||||
| feature is not available yet. In the mean time, the preview implementation is | ||||||||
| available | ||||||||
| `here <https://github.com/swiftlang/llvm-project/tree/stable/20240723>`_ in a | ||||||||
| fork of ``llvm-project``. Please follow | ||||||||
| `Building LLVM with CMake <https://llvm.org/docs/CMake.html>`_ to build the | ||||||||
| compiler. | ||||||||
|
|
||||||||
| Feature flag | ||||||||
| ============ | ||||||||
|
|
||||||||
| Pass ``-fbounds-safety`` as a Clang compilation flag for the C file that you | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| want to adopt. We recommend adopting the model file by file, because adoption | ||||||||
| requires some effort to add bounds annotations and fix compiler diagnostics. | ||||||||
|
|
||||||||
| Include ``ptrcheck.h`` | ||||||||
| ====================== | ||||||||
|
|
||||||||
| ``ptrcheck.h`` is a Clang toolchain header to provide definition of the bounds | ||||||||
| annotations such as ``__counted_by``, ``__counted_by_or_null``, ``__sized_by``, | ||||||||
| etc. In the LLVM source tree, the header is located in | ||||||||
| ``llvm-project/clang/lib/Headers/ptrcheck.h``. | ||||||||
|
|
||||||||
|
|
||||||||
| Add bounds annotations on pointers as necessary | ||||||||
| =============================================== | ||||||||
|
|
||||||||
| Annotate pointers on struct fields and function parameters if they are pointing | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: They might need to annotate globals and local variables too. |
||||||||
| to an array of object, with appropriate bounds annotations. Please see | ||||||||
| :doc:`BoundsSafety` to learn what kind of bounds annotations are available and | ||||||||
| their semantics. Note that local pointer variables typically don't need bounds | ||||||||
| annotations because they are implicitely a wide pointer (``__bidi_indexable``) | ||||||||
| that automatically carries the bounds information. | ||||||||
|
|
||||||||
| Address compiler diagnostics | ||||||||
| ============================ | ||||||||
|
|
||||||||
| Once you pass ``-fbounds-safety`` to compiler a C file, you will see some new | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| compiler warnings and errors, which will lead you to adopt the feature and | ||||||||
| to remove unsafeness in the code. Consider the following example: | ||||||||
|
||||||||
| compiler warnings and errors, which will lead you to adopt the feature and | |
| to remove unsafeness in the code. Consider the following example: | |
| compiler warnings and errors, which guide adoption of `-fbounds-safety`. Consider the following example: |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Maybe note that the compiler assumes it points to a 0 or 1 int objects by default?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Run unit tests to fix new run-time traps | |
| Run test suites to fix new run-time traps |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| bounds safety or it has incorrect adoption. | |
| bounds safety or it has incorrect adoption. Thus it is necessary to perform runtime testing of your program to gain confidence that it won't trap at runtime. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rapidsna I thought
-fbound-safetywas broken in open source? I thought people needed to do-Xclang -fbounds-safety.. or something like that?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No.
-fbounds-safetyis available as a driver option in the preview implementation (in the fork of llvm-project).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the purpose of this document is to instruct people on how to use the preview implementation.