Annotate alloc/free open/close functions with compiler attributes#13537
Annotate alloc/free open/close functions with compiler attributes#13537shindere merged 1 commit intoocaml:trunkfrom
Conversation
This helps the compiler optimize code, and static analysis by detecting potential mismatches in alloc/free pairs. - malloc > The malloc attribute indicates that the function acts like a system > memory allocation function, returning a pointer to allocated storage > disjoint from the storage for any other object accessible to the > caller. https://clang.llvm.org/docs/AttributeReference.html#malloc > 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 satisifies 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 warnings guarded by -fanalyzer respect allocation and > deallocation pairs marked with the malloc. https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute Note that the malloc attribute can only be applied to functions returning pointers. The OCaml value type is a typedef to an integer type, and the C compiler will refuse applying the attribute to a function returning an OCaml value. - nodiscard / warn_unused_result Prevent memory leaks by warning if the result of an allocation is ignored. https://clang.llvm.org/docs/AttributeReference.html#nodiscard-warn-unused-result - alloc_align > GCC uses this information to improve pointer alignment analysis. https://clang.llvm.org/docs/AttributeReference.html#alloc-align https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005falign-function-attribute - alloc_size > GCC uses this information to improve the results of > __builtin_object_size. https://clang.llvm.org/docs/AttributeReference.html#alloc-size https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute - returns_nonnull > lets the compiler optimize callers based on the knowledge that the > return value will never be null. > The analyzer considers the possibility that an allocation function > could fail and return null. […] If the allocator always returns > non-null, use __attribute__ ((returns_nonnull)) to suppress these > warnings. https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-returns_005fnonnull-function-attribute With GCC, restrict the attributes usage to GCC >= 14, as GCC 13 reports false positives on the runtime code.
bb3b7fa to
48264c3
Compare
|
I was confused by this PR description as received in my mailbox ("Didn't we already receive this PR? What is going on?"). Maybe a small remark at the top to say that it is a new iteration on PR number .... would help. What is the old PR number? |
|
My opinion doesn't change that I think we should merge this PR, modulo finding the right configuration gimmicks. @Octachron is running the PR through |
I wrote this text in the middle of the PR message, I'll move it on top.
I'd like to point out that there are other compiler attributes we may want to leverage, at the cost of some verbosity. I hope to revisit this idea if this PR proves itself useful and not too invasive. For functions:
For variables:
|
|
@gasche , |
This helps the compiler optimize code, and static analysis by detecting potential mismatches in alloc/free pairs.
With GCC, restrict the attributes usage to GCC >= 14, as GCC 13 reports false positives on the runtime code.
The only change from #13457 is to restrict this PR to GCC >= 14, or LLVM-based compilers. It was reverted in bce3bb7, as builds using GCC 13 would fail.
https://clang.llvm.org/docs/AttributeReference.html#malloc
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute
Note that the malloc attribute can only be applied to functions returning pointers. The OCaml value type is a typedef to an integer type, and the C compiler will refuse applying the attribute to a function returning an OCaml value.
Prevent memory leaks by warning if the result of an allocation is ignored.
https://clang.llvm.org/docs/AttributeReference.html#nodiscard-warn-unused-result
https://clang.llvm.org/docs/AttributeReference.html#alloc-align https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005falign-function-attribute
https://clang.llvm.org/docs/AttributeReference.html#alloc-size https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-returns_005fnonnull-function-attribute
Invoked on the following test program, GCC 14.2.0 reports:
Simply using
-Wallturns on more checks. The-fanalyzerwarning has both false positive and false negatives and I'm not turning it on on the codebase. I believe this code may help users writing C FFI code for OCaml. No warnings have been raised on the current trunk or on Lwt. Let me know if there are packages containing C code that could be tested.glibc has annotated functions in
malloc/malloc.h, but as we're keeping track of C heap allocations, the compiler cannot propagate the annotations to the call sites ofcaml_stat_*functions.I don't have insights on whether code gen has improved or worsened.
cc @gasche and @Octachron, who reviewed the previous version of this PR.