Add counted_by attribute
The counted_by[0]https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-counted_005fby compiler attribute is fairly new. It was added in GCC
15 and Clang 18. It has been used fairly extensively in the Linux
kernel[0]https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-counted_005fby.
To summarize the benefits of the attribute:
- Runtime bounds checking with -DFORTIFY_SOURCE=3 and -fsanitize-bounds
- Accurate reporting of __builtin_dynamic_object_size()
While we don't use __builtin_dynamic_object_size(), I think the runtime
bounds checking improvements are easily worth the little bit of effort
to add the attribute in various locations and review the code. I think
it will improve things for buildfarm animals using ASan due to expanded
coverage.
Adding this attribute to the codebase was previously proposed back in
2024[1]https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux, but the thread never got any traction. I figured that I would
try again, but bring some patches this time. I figured that I would
start small, and then after people agree that it has value, I can look
into adding the attribute in many more places.
[0]: https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-counted_005fby
[1]: https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux
[2]: /messages/by-id/ME3P282MB3166210CDE36BD485B703BF6B6C82@ME3P282MB3166.AUSP282.PROD.OUTLOOK.COM
--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)
On Wed, 29 Jul 2026 at 22:07, "Tristan Partin" <tristan@partin.io> wrote:
The counted_by[0] compiler attribute is fairly new. It was added in GCC
15 and Clang 18. It has been used fairly extensively in the Linux
kernel[0].To summarize the benefits of the attribute:
- Runtime bounds checking with -DFORTIFY_SOURCE=3 and -fsanitize-bounds
- Accurate reporting of __builtin_dynamic_object_size()While we don't use __builtin_dynamic_object_size(), I think the runtime
bounds checking improvements are easily worth the little bit of effort
to add the attribute in various locations and review the code. I think
it will improve things for buildfarm animals using ASan due to expanded
coverage.Adding this attribute to the codebase was previously proposed back in
2024[1], but the thread never got any traction. I figured that I would
try again, but bring some patches this time. I figured that I would
start small, and then after people agree that it has value, I can look
into adding the attribute in many more places.[0]: https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-counted_005fby
[1]: https://people.kernel.org/gustavoars/how-to-use-the-new-counted_by-attribute-in-c-and-linux
[2]: /messages/by-id/ME3P282MB3166210CDE36BD485B703BF6B6C82@ME3P282MB3166.AUSP282.PROD.OUTLOOK.COM
+1 for this idea.
--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)
--
Regards,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.
On 30.07.26 00:07, Tristan Partin wrote:
The counted_by[0] compiler attribute is fairly new. It was added in GCC
15 and Clang 18. It has been used fairly extensively in the Linux
kernel[0].To summarize the benefits of the attribute:
- Runtime bounds checking with -DFORTIFY_SOURCE=3 and -fsanitize-bounds
- Accurate reporting of __builtin_dynamic_object_size()While we don't use __builtin_dynamic_object_size(), I think the runtime
bounds checking improvements are easily worth the little bit of effort
to add the attribute in various locations and review the code. I think
it will improve things for buildfarm animals using ASan due to expanded
coverage.
I think this is a good idea.
Maybe you could produce an intentionally broken piece of code that would
illustrate what kinds of reports one might hope to get from this.