Bug 87736 - New attributes to mark custom alloc/free function pair
Summary: New attributes to mark custom alloc/free function pair
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 8.2.0
: P3 enhancement
Target Milestone: 11.0
Assignee: Martin Sebor
URL:
Keywords:
Depends on: 87731
Blocks:
  Show dependency treegraph
 
Reported: 2018-10-24 16:38 UTC by Daniel Fruzynski
Modified: 2023-12-30 20:56 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-10-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Fruzynski 2018-10-24 16:38:13 UTC
This is split from Bug 87731, as recommended by Jonathan Wakely:

Valgrind provides set of macros which allows it to track custom alloc/free functions. It would be nice if you add new attributes which could be attached to custom alloc and free functions, so gcc could check pairing for them too. I think of something like this:

__attribute__((malloc("MyAllocType")))
void* MyAlloc(size_t);

__attribute__((free("MyAllocType")))
void MyFree(void*);
Comment 1 jsm-csl@polyomino.org.uk 2018-10-24 17:01:50 UTC
Note that you can have a custom malloc function (e.g. xmalloc) that pairs 
with standard free, so it should be possible to indicate that.
Comment 2 Martin Sebor 2018-10-24 17:06:46 UTC
Something like this is probably necessary for a robust implementation of pr87731.  It won't do much in GCC on its own, though it could be used by third party static analyzers (that suggests that we might want to consider adopting one or more existing forms of annotations already recognized by such tools).

This is also a general mechanism, not one specific to C++, so changing Component to middle-end.
Comment 3 Martin Sebor 2019-11-08 02:28:39 UTC
The solution I'm working on for pr91582 adds tracking of dynamically allocated objects to the strlen pass (including alloca, VLAs, and user-defined allocation functions declared with attribute alloc_size).  A natural extension of the project is to detect accesses to deallocated objects.  A new function attribute such as "free" to parallel attributes malloc and alloca_size will be required to detect such deallocation by user-defined functions (the attribute will need to apply to specific function arguments to make it possible to use it with functions taking two more pointers).  I expect to submit my patch for pr91582 in time for GCC 10 but the new attribute will most likely have to wait until GCC 11.
Comment 4 Martin Sebor 2020-07-09 00:07:17 UTC
See also pr94527 for a related request.  I'm working on a solution for GCC 11 (fingers crossed).
Comment 5 Martin Sebor 2020-12-03 22:47:00 UTC
Implemented GCC 11 in r11-5732.