Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing support for GCC's attribute malloc (deallocator) and malloc (deallocator, ptr-index) #53152

Open
davidbolvansky opened this issue Jan 12, 2022 · 8 comments
Labels
clang:codegen clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@davidbolvansky
Copy link
Collaborator

davidbolvansky commented Jan 12, 2022

Clang currently only supports basic version - attribute((malloc)). (in LLVM as noalias)

@davidbolvansky
Copy link
Collaborator Author

cc @nikic as you presented similar topic recently: https://lists.llvm.org/pipermail/llvm-dev/2022-January/154538.html
cc @jdoerfert

@davidbolvansky
Copy link
Collaborator Author

and also maybe cc @nickdesaulniers as this was implemented in GCC mainly with linux kernel use cases in mind.

@davidbolvansky davidbolvansky changed the title Missing support forGCC's attribute malloc (deallocator) and malloc (deallocator, ptr-index) Missing support for GCC's attribute malloc (deallocator) and malloc (deallocator, ptr-index) Jan 12, 2022
@dilyanpalauzov
Copy link

With clang 14.0.0 this compiles:

#include <stdlib.h>

void fr(char* x) {
  free(x);
}

__attribute__((malloc)) char * al() {
  return (char*) malloc(10);
}

int main() {
  char* x = al();
  fr(x);
  return 0;
}

and this fails:

#include <stdlib.h>

void fr(char* x) {
  free(x);
}

__attribute__((malloc, malloc(fr, 1))) char * al() {
  return (char*) malloc(10);
}

int main() {
  char* x = al();
  fr(x);
  return 0;
}
a.c:7:25: error: 'malloc' attribute takes no arguments
 __attribute__((malloc, malloc(fr, 1))) char * al() {
                        ^
1 error generated.

GCC does accept the malloc (deallocator, ptr-index) form.

Please extend clang to accept the malloc (deallocator, ptr-index) __attribute__(()) form. It shall at least ignore it, rather than throwing an error for wrong syntax. This way the malloc (deallocator, ptr-index) form can always be used, irrespective of the compiler. Some compilers will handle it, other will ignore it.

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen and removed new issue labels May 9, 2022
@llvmbot
Copy link
Member

llvmbot commented May 9, 2022

@llvm/issue-subscribers-clang-codegen

@llvmbot
Copy link
Member

llvmbot commented May 9, 2022

@llvm/issue-subscribers-clang-frontend

@nickdesaulniers
Copy link
Member

this was implemented in GCC mainly with linux kernel use cases in mind.

@davidbolvansky do you have more context or a link to that discussion?

@alejandro-colomar
Copy link

alejandro-colomar commented Sep 28, 2022

@msebor could probably tell us more about motivating cases.

@davidbolvansky
Above this comment is a link to a pull request in shadow-utils, regarding the introduction of a new API called agetpass() and its deallocator called erase_pass(). It should provide an example of a use case for this feature.

We tried to use the attribute, and noticed that clang doesn't support it.

JordanL8 pushed a commit to MoonCollider/CppSharp that referenced this issue Aug 29, 2023
JordanL8 added a commit to MoonCollider/CppSharp that referenced this issue Sep 7, 2023
* Incorrect build instructions on Windows (mono#1701)

* Fix incorrect build instructions for Windows

* Add link to Visual Studio installer

* Refine building instructions for VS2019+

(cherry picked from commit c4a24b0)

* Misc changes (mono#1710)

* Minor code refactoring for re-use.

* Add Class.FindVariable helper method.

* Upgrade to .NET 6.

(cherry picked from commit db7949b)

* Add C++ WebAssembly ABI (mono#1711)

* Minor code refactorings.

* Fix debug assert issue with vtable methods.

* Add support for WebAssembly C++ ABI to parser and AST converter.

(cherry picked from commit 9b06e7b)

* Add initial Emscripten generator. (mono#1712)

(cherry picked from commit 117567d)

* Code cleanups and improvements (mono#1713)

* Code cleanups.

* Run GetterSetterToPropertyPass for more generators.

* Fixed compile warning when compiling parser bindings.

* Cleanup driver code.

* Remove dead 32-bit code.

* Reduce verbosity when Options.Quiet is set.

* Remove test compile-time warnings.

* Move .NET tests to tests/dotnet.

* Remove unused AST viewer code and premake-qt submodule.

* Move tests2/ contents to tests/.

(cherry picked from commit 4417dd9)

* Publish package to nuget.org when a version tag is created

(cherry picked from commit 5715df5)

* Allow passing a `-target-framework` option to `build.sh` (mono#1718)

Closes mono#1717.

(cherry picked from commit 8cf6e3f)

* Remove unused LLVM build flags.

(cherry picked from commit 84b7276)

* Update LLVM workflow to latest Action images.

(cherry picked from commit 3d32fc7)

* Update LLVM Windows workflow to latest Action images.

(cherry picked from commit f9f9e9c)

* Update action steps to latest to fix Node.js deprecation warnings.

(cherry picked from commit 7d6decd)

* Update LLVM build script for Visual Studio 2022.

(cherry picked from commit b8a16a4)

* Workaround Directory.Build.props getting picked up when building LLVM.

This is to workaround MSBuild CMake step from LLVM build in Windows
picking this up.

(cherry picked from commit 25e2c7c)

* Added variation of mono#1736

* Workaround parsing GNU system headers with GCC 11.0 version. (mono#1737)

Closes mono#1703.

Upstream bugs:

  llvm/llvm-project#51607
  llvm/llvm-project#53152

(cherry picked from commit 0272532)

* CppSharp.AST.Declaration - fix return QualifiedOriginalName, QualifiedLogicalOriginalName (mono#1740)

Fixed properties getters of QualifiedOriginalName, QualifiedLogicalOriginalName and methods GetQualifiedName(),  GatherNamespaces.

(cherry picked from commit 9923d79)

* CSharpExpressionPrinter: Wrap expression in parenthesis (mono#1741)

Wrap default parameter expressions in parentheses to ensure whole expressions are casted instead of only the first operand.

(cherry picked from commit 169f868)

* Update README.md

Remove Gitter since the link is 404 and we have not been using it anyway.

(cherry picked from commit 160efb4)

* CSharpExpressionPrinter: Recurse into operands (mono#1745)

* CSharpExpressionPrinter: Recurse into operands

Recursively call `VisitExpression` on the LHS and RHS of a binary
operator expression. This fixes the generation for complex default
parameters involving things other than two enumeration members.

* CSharpSources: Use `const` when possible

Generate `const` instead of `static` members when possible.
This allows generated members to be used when compile-time constants are
required, such as default parameters.

(cherry picked from commit 1ce9cb7)

* CSharp: More default parameter fixes (mono#1747)

- Expression generation for `ConstructorReference` now also recursively
calls `VisitExpression` for the argument if only one argument is
detected. This allows correct overload generation for functions taking
a variable as the default parameter value.
- Default parameters of pointer-to-enumeration types are now correctly
generated similar to primitive types.

(cherry picked from commit add3aba)

* Add type parameter to non-type template parameter decls. (mono#1749)

(cherry picked from commit bb31bd6)

* Update parser example to latest API.

(cherry picked from commit 8c1fa18)

* Fixed debugging printing when type printing delegate is not initialized.

(cherry picked from commit 9f3ce76)

* Array marshalling (mono#1748)

* Generator: Customization for const char[]

Allow the user to choose whether `const char[]` should be marshalled as
`string` or a normal `char` array in C#.

A new option `MarshalConstCharArrayAsString` is added, and is `true`
by default.

This helps in situations where the original C++ API distinguishes
between C-strings and char arrays using the two different notations.

* CSharpMarshal: Fix unknown length array marshal

For unknown length arrays, also run a conversion loop if the primitive
type encountered needs conversion (e.g. `char` to `sbyte`).

* CSharpTypePrinter: Fix for boolean arrays

(cherry picked from commit 357efec)

* Add qualified template name to GetCXXRecordDeclFromBaseType. (mono#1751)

(cherry picked from commit 626a362)

* SymbolResolver: Use filename when path cannot be found (mono#1752)

Uses bare filename to pass to `dlopen` when the full path cannot
be detected. This helps on systems where library paths are not the
same as $PATH.

(cherry picked from commit 3978fb3)

* CSharpSources: Dereference pointer variables (mono#1753)

Dereference pointers when generating getters for pointer variables.
Otherwise, the managed instance would point to the pointer itself
rather than the object at the native instance's address.

(cherry picked from commit ce3d04a)

* Use TypePrinter.IntPtrType in CSharpSources.cs code from previous commit.

(cherry picked from commit 3c31179)

---------

Co-authored-by: aybe <aybe@users.noreply.github.com>
Co-authored-by: João Matos <joao@tritao.eu>
Co-authored-by: josetr <37419832+josetr@users.noreply.github.com>
Co-authored-by: stachu99 <stachu99@users.noreply.github.com>
Co-authored-by: Trung Nguyen <57174311+trungnt2910@users.noreply.github.com>
aloisklink added a commit to aloisklink/llvm-project that referenced this issue Oct 7, 2023
Ignore the `[[malloc(x)]]` or `[[malloc(x, 1)]]` function attribute
syntax added in [GCC 11][1] and print a warning instead of an error.

Unlike `[[malloc]]` with no arguments (which is supported by Clang),
GCC uses the one or two argument form to specify a deallocator for
GCC's static analyzer.

Code currently compiled with `[[malloc(x)]]` or
`__attribute((malloc(x)))` fails with the following error:
`'malloc' attribute takes no arguments`.

[1]: https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;f=gcc/doc/extend.texi;h=dce6c58db87ebf7f4477bd3126228e73e4eeee97#patch6

Fixes: llvm#51607
Partial-Bug: llvm#53152
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

6 participants