Re: Add malloc attribute to memory allocation functions

From: "Tristan Partin" <tristan(at)partin(dot)io>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Michael Paquier" <michael(at)paquier(dot)xyz>
Cc: "Peter Eisentraut" <peter(at)eisentraut(dot)org>, "pgsql-hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add malloc attribute to memory allocation functions
Date: 2026-11-16 22:02:05
Message-ID: DMWZ2YWWN43C.18ABG5KXSGVI0@partin.io
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue Jul 7, 2026 at 11:57 PM UTC, Tristan Partin wrote:
> On Tue Jul 7, 2026 at 6:54 PM CDT, Tom Lane wrote:
>> Michael Paquier <michael(at)paquier(dot)xyz> writes:
>>> Additional question. Does this help with requirements like the one
>>> listed in fe-exec.c for PQfreemem() under WIN32?
>>
>> Yeah, I was wondering about that point too. It would be really nice
>> to have automated checks for that. Maybe the right thing to do here
>> (to start anyway) is to put in targeted annotations that address
>> specific pain points like that one.
>>
>> One issue that'd have to be dealt with is that that would involve
>> putting annotations into the public header file libpq-fe.h. We'd need
>> to be sure we do not break things for applications using compilers
>> other than what we built libpq with. That seems reasonably easy to do
>> with some macro trickery, but it's a point to keep in mind.
>
> Ok, I think the path forward is two-fold:
>
> 1. Add the annotations to specific pain points as Tom suggested
> (PGfreemem(), frontend memory allocators)
> 2. Add the annotations to the backend allocators knowing that the patch
> may not be committed

After talking to Peter off-list, I've come up with a new strategy by
splitting up __attribute__((malloc)) and
__attribute__((malloc(deallocator, arg))).

For those that are unaware, these two attributes, though named the same,
have two separate meanings:

malloc:

> Attribute malloc indicates that a function is malloc-like, i.e., that
> the pointer P returned by the function cannot alias any other pointer
> valid when the function returns, and moreover no pointers to valid
> objects occur in any storage addressed by P. In addition, GCC predicts
> that a function with the attribute returns non-null in most cases.

malloc(deallocator, arg):

> Associating a function with a deallocator helps detect calls to
> mismatched allocation and deallocation functions and diagnose them
> under the control of options such as -Wmismatched-dealloc. It also
> makes it possible to diagnose attempts to deallocate objects that were
> not allocated dynamically, by -Wfree-nonheap-object. To indicate that
> an allocation function both satisfies the nonaliasing property and has
> a deallocator associated with it, both the plain form of the attribute
> and the one with the deallocator argument must be used. The same
> function can be both an allocator and a deallocator. Since inlining
> one of the associated functions but not the other could result in
> apparent mismatches, this form of attribute malloc is not accepted on
> inline functions. For the same reason, using the attribute prevents
> both the allocation and deallocation functions from being expanded
> inline.

To summarize the second a bit, getting the extra analysis is not free.
There are performance implications with the potential lack of inlining.
I think adding the deallocator is important, but probably only in debug
builds.

Attached are patches which add pg_attribute_malloc and apply it to
various memory allocating functions.

I have the patches prepared for what I am calling
pg_attribute_dealloc(...), which is defined to be
__attribute__((malloc(deallocator, arg))). I'll hold off on inundating
the list with those until this plain version is reviewed and committed.

--
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

Attachment Content-Type Size
v2-0001-Add-pg_attribute_malloc.patch text/x-patch 1.7 KB
v2-0002-Mark-some-memory-allocation-functions-with-pg_att.patch text/x-patch 15.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bharath Rupireddy 2026-11-16 22:02:57 Re: Parallel heap vacuum
Previous Message surya poondla 2026-11-16 21:52:46 Re: WAL_LOG CREATE DATABASE strategy broken for non-standard page layouts