Skip to content

LLVMRustWriteOutputFile: PassManager leaked on error return paths #495

@SebTardif

Description

@SebTardif

Summary

In compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp, the function LLVMRustWriteOutputFile only calls LLVMDisposePassManager(PMR) on the success path. Both error-return paths leak the pass manager.

Location

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp, lines 478 and 489

Bug

extern "C" LLVMRustResult
LLVMRustWriteOutputFile(..., LLVMPassManagerRef PMR, ...) {
  ...
  if (ErrorInfo != "") {
    LLVMRustSetLastError(ErrorInfo.c_str());
    return LLVMRustResult::Failure;    // PMR leaked (line 478)
  }
  ...
    return LLVMRustResult::Failure;    // PMR leaked (line 489, also dead code per #493)
  ...
  LLVMDisposePassManager(PMR);         // only reached on success (line 502)
  return LLVMRustResult::Success;
}

Impact

In practice, line 478 fires when the primary output file cannot be opened, which typically causes compilation to abort shortly after. Line 489 is dead code (see issue #493). So this leak is mostly theoretical.

Fix

Call LLVMDisposePassManager(PMR) before each error return, or restructure to use RAII/goto cleanup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: code generationI-leakResource leak (fd, memory)P-lowLow impact: edge case or niche scenariobugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions