-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[BoundsSafety][NFC] Specify taking address of a variable referred to by '__counted_by' is forbidden #106147
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
base: main
Are you sure you want to change the base?
[BoundsSafety][NFC] Specify taking address of a variable referred to by '__counted_by' is forbidden #106147
Conversation
@llvm/pr-subscribers-clang Author: Yeoul Na (rapidsna) Changes
As shown in the example below, with -fbounds-safety the compiler ensures that the
Consequently,
This PR is to explicitly specify this restriction and avoid future conflicts. Full diff: https://github.com/llvm/llvm-project/pull/106147.diff 1 Files Affected:
diff --git a/clang/docs/BoundsSafety.rst b/clang/docs/BoundsSafety.rst
index 8fd655663edb00..e4ddd3c62db65d 100644
--- a/clang/docs/BoundsSafety.rst
+++ b/clang/docs/BoundsSafety.rst
@@ -759,7 +759,24 @@ relationship must hold even after any of these related variables are updated. To
this end, the model requires that assignments to ``buf`` and ``count`` must be
side by side, with no side effects between them. This prevents ``buf`` and
``count`` from temporarily falling out of sync due to updates happening at a
-distance.
+distance. In addition, taking address of ``count`` is not allowed in order to
+prevent the programmers from updating the ``count`` through the pointer, which
+will evade the necessary checks to make ``count`` and ``buf`` in sync.
+
+.. code-block:: c
+
+ struct counted_buf {
+ int *__counted_by(count) buf;
+ size_t count;
+ };
+
+ void foo(struct counted_buf *p) {
+ int *pointer_to_count = &p->count; // error: variable referred to by
+ // '__counted_by' cannot be pointed to by any other variable; exception is
+ // when the pointer is passed as a compatible argument to a function.
+ *pointer_to_count = SIZE_MAX; // Without reporting the error above, the
+ // compiler cannot prevent count from getting an invalid value.
+ }
The example below shows a function ``alloc_buf`` that initializes a struct that
members that use the ``__counted_by`` annotation. The compiler allows these
|
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.
Overall looks good, just a small clarification
void foo(struct counted_buf *p) { | ||
int *pointer_to_count = &p->count; // error: variable referred to by | ||
// '__counted_by' cannot be pointed to by any other variable; exception is | ||
// when the pointer is passed as a compatible argument to a function. |
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.
I think we should define what a compatible argument is
-fbounds-safety
doesn't allow taking address of a variable referred to by__counted_by
, in order to prevent code from using the pointer to update the variable without necessary checks to keep the invariant of__counted_by
.As shown in the example below, with -fbounds-safety the compiler ensures that the
__counted_by
pointer/or array has at least as many as elements that the attribute indicates, by requiring the count and the buf are always updated side by side and emitting run-time checks to ensure the new values are valid.Consequently,
-fbounds-safety
prevents taking address of a variable referred to by__counted_by
, because otherwise, the compiler cannot check the updates through the pointer pointing to the count:This PR is to explicitly specify this restriction and avoid future conflicts.