Add counted_by attribute

Started by Tristan Partin12 days ago3 messageshackers
Jump to latest
#1Tristan Partin
tristan@partin.io

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)

Attachments:

v1-0001-Add-pg_attribute_counted_by.patchtext/x-patch; charset=utf-8; name=v1-0001-Add-pg_attribute_counted_by.patchDownload+12-1
v1-0002-Make-use-of-counted_by-attribute.patchtext/x-patch; charset=utf-8; name=v1-0002-Make-use-of-counted_by-attribute.patchDownload+2-3
#2Japin Li
japinli@hotmail.com
In reply to: Tristan Partin (#1)
Re: Add counted_by attribute

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.

#3Peter Eisentraut
peter_e@gmx.net
In reply to: Tristan Partin (#1)
Re: Add counted_by attribute

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.