Skip to content

Bytecode Interpreter: Fix IntegralAP/Floating memory leaks #139012

Open
@tbaederr

Description

@tbaederr

When creating new IntegralAP (or also Floating) values, they allocate heap memory, which might not get cleaned up when we interrupt interpretation and the integral stays on the stack. We never call the destructor.

This code:

int ctz48 = __builtin_ctzg((unsigned __int128)0);

produces a memory leak:

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x0000003e2c41 in operator new[](unsigned long) (/home/tbaeder/code/llvm-project/build/bin/clang-21+0x3e2c41) (BuildId: 616e3f3e5634af4d767394e4e2f35f78358eaebd)
    #1 0x7f05a3cdb67e in getMemory(unsigned int) /home/tbaeder/code/llvm-project/llvm/lib/Support/APInt.cpp:43:10
    #2 0x7f05a3cdb78f in llvm::APInt::initSlowCase(llvm::APInt const&) /home/tbaeder/code/llvm-project/llvm/lib/Support/APInt.cpp:87:12
    #3 0x7f05dd9c27ea in llvm::APInt::APInt(llvm::APInt const&) /home/tbaeder/code/llvm-project/llvm/include/llvm/ADT/APInt.h:180:7
    #4 0x7f05e007a03c in clang::interp::IntegralAP<false>::IntegralAP(llvm::APInt) /home/tbaeder/code/llvm-project/clang/lib/AST/ByteCode/IntegralAP.h:62:25
    #5 0x7f05e04e4eb7 in clang::interp::IntegralAP<false> clang::interp::IntegralAP<false>::from<32u, true>(clang::interp::Integral<32u, true>, unsigned int) /home/tbaeder/code/llvm-project/clang/lib/AST/ByteCode/IntegralAP.h:115:12
    #6 0x7f05e032b3fc in bool clang::interp::CastAP<(clang::interp::PrimType)4, clang::interp::Integral<32u, true>>(clang::interp::InterpState&, clang::interp::CodePtr, unsigned int) /home/tbaeder/code/llvm-project/clang/lib/AST/ByteCode/Interp.h:2266:7
    #7 0x7f05e02679de in clang::interp::EvalEmitter::emitCastAPSint32(unsigned int, clang::interp::SourceInfo const&) /home/tbaeder/code/llvm-project/build/tools/clang/lib/AST/Opcodes.inc:6657:10

I think we should allocate the memory ourselves, like APNumericStorage does.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:bytecodeIssues for the clang bytecode constexpr interpreterclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions