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

int128 as underlying enum type leaks memory #78160

Closed
tbaederr opened this issue Jan 15, 2024 · 4 comments · Fixed by #78311
Closed

int128 as underlying enum type leaks memory #78160

tbaederr opened this issue Jan 15, 2024 · 4 comments · Fixed by #78311
Assignees
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@tbaederr
Copy link
Contributor

Code:

enum A : __int128 {
  a,
  b
};

Leak reported by asan:

Direct leak of 32 byte(s) in 2 object(s) allocated from:
    #0 0x3e3301 in operator new[](unsigned long) (/home/tbaeder/code/llvm-project/build/bin/clang-18+0x3e3301) (BuildId: f60b6049740dff9abb50a1499d5e41906fc6ed51)
    #1 0x7f7bdcfbfdfe in getMemory(unsigned int) /home/tbaeder/code/llvm-project/llvm/lib/Support/APInt.cpp:45:10
    #2 0x7f7bdcfbfbf3 in llvm::APInt::initSlowCase(llvm::APInt const&) /home/tbaeder/code/llvm-project/llvm/lib/Support/APInt.cpp:86:12
    #3 0x7f7c20dcb581 in llvm::APInt::APInt(llvm::APInt const&) /home/tbaeder/code/llvm-project/llvm/include/llvm/ADT/APInt.h:158:7
    #4 0x7f7c2176a5c6 in llvm::APSInt::APSInt(llvm::APSInt const&) /home/tbaeder/code/llvm-project/llvm/include/llvm/ADT/APSInt.h:23:21
    #5 0x7f7c22be7fc6 in clang::EnumConstantDecl::EnumConstantDecl(clang::DeclContext*, clang::SourceLocation, clang::IdentifierInfo*, clang::QualType, clang::Expr*, llvm::APSInt const&) /home/tbaeder/code/llvm-project/clang/include/clang/AST/Decl.h:3262:62
    #6 0x7f7c22bc2833 in clang::EnumConstantDecl::Create(clang::ASTContext&, clang::EnumDecl*, clang::SourceLocation, clang::IdentifierInfo*, clang::QualType, clang::Expr*, llvm::APSInt const&) /home/tbaeder/code/llvm-project/clang/lib/AST/Decl.cpp:5376:22
    #7 0x7f7c27b0048e in clang::Sema::CheckEnumConstant(clang::EnumDecl*, clang::EnumConstantDecl*, clang::SourceLocation, clang::IdentifierInfo*, clang::Expr*) /home/tbaeder/code/llvm-project/clang/lib/Sema/SemaDecl.cpp:19743:10
    #8 0x7f7c27b02e75 in clang::Sema::ActOnEnumConstant(clang::Scope*, clang::Decl*, clang::Decl*, clang::SourceLocation, clang::IdentifierInfo*, clang::ParsedAttributesView const&, clang::SourceLocation, clang::Expr*) /home/tbaeder/code/llvm-project/clang/lib/Sema/SemaDecl.cpp:19808:5
    #9 0x7f7c21265efd in clang::Parser::ParseEnumBody(clang::SourceLocation, clang::Decl*) /home/tbaeder/code/llvm-project/clang/lib/Parse/ParseDecl.cpp:5301:35
    #10 0x7f7c2125905b in clang::Parser::ParseEnumSpecifier(clang::SourceLocation, clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext) /home/tbaeder/code/llvm-project/clang/lib/Parse/ParseDecl.cpp:5220:5
    #11 0x7f7c2124bc69 in clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*, clang::ImplicitTypenameContext) /home/tbaeder/code/llvm-project/clang/lib/Parse/ParseDecl.cpp:4413:7
    #12 0x7f7c212a0904 in clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&, clang::Parser::ParsedTemplateInfo const&, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*) /home/tbaeder/code/llvm-project/clang/include/clang/Parse/Parser.h:2452:12
    #13 0x7f7c2171d673 in clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:1143:3
    #14 0x7f7c2171c018 in clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:1261:12
    #15 0x7f7c21719d32 in clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:1065:14
    #16 0x7f7c21710c3e in clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:755:12
    #17 0x7f7c2170e1fd in clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:602:26
    #18 0x7f7c211a4372 in clang::ParseAST(clang::Sema&, bool, bool) /home/tbaeder/code/llvm-project/clang/lib/Parse/ParseAST.cpp:162:25
    #19 0x7f7c33140d83 in clang::ASTFrontendAction::ExecuteAction() /home/tbaeder/code/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1183:3
    #20 0x7f7c2ed59c12 in clang::CodeGenAction::ExecuteAction() /home/tbaeder/code/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:1153:30
    #21 0x7f7c3313efbf in clang::FrontendAction::Execute() /home/tbaeder/code/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1069:8
    #22 0x7f7c32c5c476 in clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /home/tbaeder/code/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1057:33
[...]
@tbaederr tbaederr added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Jan 15, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 15, 2024

@llvm/issue-subscribers-clang-frontend

Author: Timm Baeder (tbaederr)

Code:
enum A : __int128 {
  a,
  b
};

Leak reported by asan:

Direct leak of 32 byte(s) in 2 object(s) allocated from:
    #<!-- -->0 0x3e3301 in operator new[](unsigned long) (/home/tbaeder/code/llvm-project/build/bin/clang-18+0x3e3301) (BuildId: f60b6049740dff9abb50a1499d5e41906fc6ed51)
    #<!-- -->1 0x7f7bdcfbfdfe in getMemory(unsigned int) /home/tbaeder/code/llvm-project/llvm/lib/Support/APInt.cpp:45:10
    #<!-- -->2 0x7f7bdcfbfbf3 in llvm::APInt::initSlowCase(llvm::APInt const&amp;) /home/tbaeder/code/llvm-project/llvm/lib/Support/APInt.cpp:86:12
    #<!-- -->3 0x7f7c20dcb581 in llvm::APInt::APInt(llvm::APInt const&amp;) /home/tbaeder/code/llvm-project/llvm/include/llvm/ADT/APInt.h:158:7
    #<!-- -->4 0x7f7c2176a5c6 in llvm::APSInt::APSInt(llvm::APSInt const&amp;) /home/tbaeder/code/llvm-project/llvm/include/llvm/ADT/APSInt.h:23:21
    #<!-- -->5 0x7f7c22be7fc6 in clang::EnumConstantDecl::EnumConstantDecl(clang::DeclContext*, clang::SourceLocation, clang::IdentifierInfo*, clang::QualType, clang::Expr*, llvm::APSInt const&amp;) /home/tbaeder/code/llvm-project/clang/include/clang/AST/Decl.h:3262:62
    #<!-- -->6 0x7f7c22bc2833 in clang::EnumConstantDecl::Create(clang::ASTContext&amp;, clang::EnumDecl*, clang::SourceLocation, clang::IdentifierInfo*, clang::QualType, clang::Expr*, llvm::APSInt const&amp;) /home/tbaeder/code/llvm-project/clang/lib/AST/Decl.cpp:5376:22
    #<!-- -->7 0x7f7c27b0048e in clang::Sema::CheckEnumConstant(clang::EnumDecl*, clang::EnumConstantDecl*, clang::SourceLocation, clang::IdentifierInfo*, clang::Expr*) /home/tbaeder/code/llvm-project/clang/lib/Sema/SemaDecl.cpp:19743:10
    #<!-- -->8 0x7f7c27b02e75 in clang::Sema::ActOnEnumConstant(clang::Scope*, clang::Decl*, clang::Decl*, clang::SourceLocation, clang::IdentifierInfo*, clang::ParsedAttributesView const&amp;, clang::SourceLocation, clang::Expr*) /home/tbaeder/code/llvm-project/clang/lib/Sema/SemaDecl.cpp:19808:5
    #<!-- -->9 0x7f7c21265efd in clang::Parser::ParseEnumBody(clang::SourceLocation, clang::Decl*) /home/tbaeder/code/llvm-project/clang/lib/Parse/ParseDecl.cpp:5301:35
    #<!-- -->10 0x7f7c2125905b in clang::Parser::ParseEnumSpecifier(clang::SourceLocation, clang::DeclSpec&amp;, clang::Parser::ParsedTemplateInfo const&amp;, clang::AccessSpecifier, clang::Parser::DeclSpecContext) /home/tbaeder/code/llvm-project/clang/lib/Parse/ParseDecl.cpp:5220:5
    #<!-- -->11 0x7f7c2124bc69 in clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&amp;, clang::Parser::ParsedTemplateInfo const&amp;, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*, clang::ImplicitTypenameContext) /home/tbaeder/code/llvm-project/clang/lib/Parse/ParseDecl.cpp:4413:7
    #<!-- -->12 0x7f7c212a0904 in clang::Parser::ParseDeclarationSpecifiers(clang::DeclSpec&amp;, clang::Parser::ParsedTemplateInfo const&amp;, clang::AccessSpecifier, clang::Parser::DeclSpecContext, clang::Parser::LateParsedAttrList*) /home/tbaeder/code/llvm-project/clang/include/clang/Parse/Parser.h:2452:12
    #<!-- -->13 0x7f7c2171d673 in clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::ParsingDeclSpec&amp;, clang::AccessSpecifier) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:1143:3
    #<!-- -->14 0x7f7c2171c018 in clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::ParsingDeclSpec*, clang::AccessSpecifier) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:1261:12
    #<!-- -->15 0x7f7c21719d32 in clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::ParsingDeclSpec*) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:1065:14
    #<!-- -->16 0x7f7c21710c3e in clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&lt;clang::DeclGroupRef&gt;&amp;, clang::Sema::ModuleImportState&amp;) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:755:12
    #<!-- -->17 0x7f7c2170e1fd in clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr&lt;clang::DeclGroupRef&gt;&amp;, clang::Sema::ModuleImportState&amp;) /home/tbaeder/code/llvm-project/clang/lib/Parse/Parser.cpp:602:26
    #<!-- -->18 0x7f7c211a4372 in clang::ParseAST(clang::Sema&amp;, bool, bool) /home/tbaeder/code/llvm-project/clang/lib/Parse/ParseAST.cpp:162:25
    #<!-- -->19 0x7f7c33140d83 in clang::ASTFrontendAction::ExecuteAction() /home/tbaeder/code/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1183:3
    #<!-- -->20 0x7f7c2ed59c12 in clang::CodeGenAction::ExecuteAction() /home/tbaeder/code/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:1153:30
    #<!-- -->21 0x7f7c3313efbf in clang::FrontendAction::Execute() /home/tbaeder/code/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1069:8
    #<!-- -->22 0x7f7c32c5c476 in clang::CompilerInstance::ExecuteAction(clang::FrontendAction&amp;) /home/tbaeder/code/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1057:33
[...]

@shafik
Copy link
Collaborator

shafik commented Jan 16, 2024

I can not reproduce: https://godbolt.org/z/o9P3xMEsW

Please provide more details.

@topperc
Copy link
Collaborator

topperc commented Jan 16, 2024

EnumConstantDecl is allocated in the ASTContext's allocator. Destructors are never called for objects allocated in the ASTContext allocator. If the APInt in EnumConstantDecl is larger than 64 bits, the APInt constructor will allocate memory on the heap. This memory never gets destroyed.

Perhaps the APIntStorage class that is used to for a similar issue in IntegerLiteral can be extracted and used for EnumContantDecl as well?

@topperc
Copy link
Collaborator

topperc commented Jan 16, 2024

I can not reproduce: https://godbolt.org/z/o9P3xMEsW

Please provide more details.

The leak is in the compiler, not the program. asan won't find it unless you build the compiler itself with asan.

@topperc topperc self-assigned this Jan 16, 2024
topperc added a commit to topperc/llvm-project that referenced this issue Jan 16, 2024
EnumConstantDecl is allocated by the ASTContext allocator so the
destructor is never called.

This patch takes a similar approach to IntegerLiteral by using
APIntStorage to allocate large APSInts using the ASTContext
allocator as well.

The downside is that an additional heap allocation and copy of the
data needs to be made when calling getInitValue if the APSInt is
large.

Fixes llvm#78160.
topperc added a commit that referenced this issue Jan 16, 2024
EnumConstantDecl is allocated by the ASTContext allocator so the
destructor is never called.

This patch takes a similar approach to IntegerLiteral by using
APIntStorage to allocate large APSInts using the ASTContext allocator as
well.

The downside is that an additional heap allocation and copy of the data
needs to be made when calling getInitValue if the APSInt is large.

Fixes #78160.
topperc added a commit that referenced this issue Jan 16, 2024
…cl. (#78311)"

With lldb build fix.

Original message:

EnumConstantDecl is allocated by the ASTContext allocator so the
destructor is never called.

This patch takes a similar approach to IntegerLiteral by using
APIntStorage to allocate large APSInts using the ASTContext allocator as
well.

The downside is that an additional heap allocation and copy of the data
needs to be made when calling getInitValue if the APSInt is large.

Fixes #78160.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this issue Jan 28, 2024
…78311)

EnumConstantDecl is allocated by the ASTContext allocator so the
destructor is never called.

This patch takes a similar approach to IntegerLiteral by using
APIntStorage to allocate large APSInts using the ASTContext allocator as
well.

The downside is that an additional heap allocation and copy of the data
needs to be made when calling getInitValue if the APSInt is large.

Fixes llvm#78160.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this issue Jan 28, 2024
…cl. (llvm#78311)"

With lldb build fix.

Original message:

EnumConstantDecl is allocated by the ASTContext allocator so the
destructor is never called.

This patch takes a similar approach to IntegerLiteral by using
APIntStorage to allocate large APSInts using the ASTContext allocator as
well.

The downside is that an additional heap allocation and copy of the data
needs to be made when calling getInitValue if the APSInt is large.

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

Successfully merging a pull request may close this issue.

4 participants