diff options
author | Martin Sebor <msebor@redhat.com> | 2020-12-03 15:41:25 -0700 |
---|---|---|
committer | Martin Sebor <msebor@redhat.com> | 2020-12-03 15:43:32 -0700 |
commit | dce6c58db87ebf7f4477bd3126228e73e4eeee97 (patch) | |
tree | f89f18c53c2f16c2d39a1951c21f99cdd4773c99 /gcc/doc/extend.texi | |
parent | a3f7a6957a674caf95c4aefa618be51092022e87 (diff) | |
download | gcc-dce6c58db87ebf7f4477bd3126228e73e4eeee97.zip gcc-dce6c58db87ebf7f4477bd3126228e73e4eeee97.tar.gz gcc-dce6c58db87ebf7f4477bd3126228e73e4eeee97.tar.bz2 |
Add support for detecting mismatched allocation/deallocation calls.
PR c++/90629 - Support for -Wmismatched-new-delete
PR middle-end/94527 - Add an __attribute__ that marks a function as freeing an object
gcc/ChangeLog:
PR c++/90629
PR middle-end/94527
* builtins.c (access_ref::access_ref): Initialize new member.
(compute_objsize): Use access_ref::deref. Handle simple pointer
assignment.
(expand_builtin): Remove handling of the free built-in.
(call_dealloc_argno): Same.
(find_assignment_location): New function.
(fndecl_alloc_p): Same.
(gimple_call_alloc_p): Same.
(call_dealloc_p): Same.
(matching_alloc_calls_p): Same.
(warn_dealloc_offset): Same.
(maybe_emit_free_warning): Same.
* builtins.h (struct access_ref): Declare new member.
(maybe_emit_free_warning): Make extern. Make use of access_ref.
Handle -Wmismatched-new-delete.
* calls.c (initialize_argument_information): Call
maybe_emit_free_warning.
* doc/extend.texi (attribute malloc): Update.
* doc/invoke.texi (-Wfree-nonheap-object): Expand documentation.
(-Wmismatched-new-delete): Document new option.
(-Wmismatched-dealloc): Document new option.
gcc/c-family/ChangeLog:
PR c++/90629
PR middle-end/94527
* c-attribs.c (handle_dealloc_attribute): New function.
(handle_malloc_attribute): Handle argument forms of attribute.
* c.opt (-Wmismatched-dealloc): New option.
(-Wmismatched-new-delete): New option.
gcc/testsuite/ChangeLog:
PR c++/90629
PR middle-end/94527
* g++.dg/asan/asan_test.cc: Fix a bug.
* g++.dg/warn/delete-array-1.C: Add expected warning.
* g++.old-deja/g++.other/delete2.C: Add expected warning.
* g++.dg/warn/Wfree-nonheap-object-2.C: New test.
* g++.dg/warn/Wfree-nonheap-object.C: New test.
* g++.dg/warn/Wmismatched-new-delete.C: New test.
* g++.dg/warn/Wmismatched-dealloc-2.C: New test.
* g++.dg/warn/Wmismatched-dealloc.C: New test.
* gcc.dg/Wmismatched-dealloc.c: New test.
* gcc.dg/analyzer/malloc-1.c: Prune out expected warning.
* gcc.dg/attr-malloc.c: New test.
* gcc.dg/free-1.c: Adjust text of expected warning.
* gcc.dg/free-2.c: Same.
* gcc.dg/torture/pr71816.c: Prune out expected warning.
* gcc.dg/tree-ssa/pr19831-2.c: Add an expected warning.
* gcc.dg/Wfree-nonheap-object-2.c: New test.
* gcc.dg/Wfree-nonheap-object-3.c: New test.
* gcc.dg/Wfree-nonheap-object.c: New test.
libstdc++-v3/ChangeLog:
* testsuite/ext/vstring/modifiers/clear/56166.cc: Suppress a false
positive warning.
Diffstat (limited to 'gcc/doc/extend.texi')
-rw-r--r-- | gcc/doc/extend.texi | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 195bb21..4357615 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -3233,20 +3233,63 @@ this reason the attribute is not allowed on types to annotate indirect calls. @item malloc +@item malloc (@var{deallocator}) +@item malloc (@var{deallocator}, @var{ptr-index}) @cindex @code{malloc} function attribute @cindex functions that behave like malloc -This tells the compiler that a function is @code{malloc}-like, i.e., -that the pointer @var{P} returned by the function cannot alias any +Attribute @code{malloc} indicates that a function is @code{malloc}-like, +i.e., that the pointer @var{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 @var{P}. -Using this attribute can improve optimization. Compiler predicts -that a function with the attribute returns non-null in most cases. -Functions like -@code{malloc} and @code{calloc} have this property because they return -a pointer to uninitialized or zeroed-out storage. However, functions -like @code{realloc} do not have this property, as they can return a -pointer to storage containing pointers. +Independently, the form of the attribute with one or two arguments +associates @code{deallocator} as a suitable deallocation function for +pointers returned from the @code{malloc}-like function. @var{ptr-index} +denotes the positional argument to which when the pointer is passed in +calls to @code{deallocator} has the effect of deallocating it. + +Using the attribute with no arguments is designed to improve optimization. +The compiler predicts that a function with the attribute returns non-null +in most cases. Functions like @code{malloc} and @code{calloc} have this +property because they return a pointer to uninitialized or zeroed-out +storage. However, functions like @code{realloc} do not have this property, +as they may return pointers to storage containing pointers to existing +objects. + +Associating a function with a @var{deallocator} helps detect calls to +mismatched allocation and deallocation functions and diagnose them +under the control of options such as @option{-Wmismatched-dealloc}. +To indicate that an allocation function both satisifies the nonaliasing +property and has a deallocator associated with it, both the plain form +of the attribute and the one with the @var{deallocator} argument must +be used. + +For example, besides stating that the functions return pointers that do +not alias any others, the following declarations make the @code{fclose} +and @code{frepen} functions suitable deallocators for pointers returned +from all the functions that return them, and the @code{pclose} function +as the only other suitable deallocator besides @code{freopen} for pointers +returned from @code{popen}. The deallocator functions must declared +before they can be referenced in the attribute. + +@smallexample +int fclose (FILE*); +FILE* freopen (const char*, const char*, FILE*); +int pclose (FILE*); + +__attribute__ ((malloc, malloc (fclose), malloc (freopen, 3))) + FILE* fdopen (int); +__attribute__ ((malloc, malloc (fclose), malloc (freopen, 3))) + FILE* fopen (const char*, const char*); +__attribute__ ((malloc, malloc (fclose), malloc (freopen, 3))) + FILE* fmemopen(void *, size_t, const char *); +__attribute__ ((malloc, malloc (fclose), malloc (freopen, 3))) + FILE* freopen (const char*, const char*, FILE*); +__attribute__ ((malloc, malloc (pclose), malloc (freopen, 3))) + FILE* popen (const char*, const char*); +__attribute__ ((malloc, malloc (fclose), malloc (freopen, 3))) + FILE* tmpfile (void); +@end smallexample @item no_icf @cindex @code{no_icf} function attribute |