dotnet / LLVMSharp

LLVM bindings for .NET Standard written in C# using ClangSharp
MIT License
838 stars 95 forks source link

Silent crash when outputting as object file, but works to output IR files and use llc manually #182

Closed PaddiM8 closed 2 years ago

PaddiM8 commented 2 years ago

Calling LLVM functions sometimes causes the program to crash abruptly with no feedback whatsoever (exit code 0 even). Now, this usually happens when something is being done wrongly, but right now I'm experiencing a crash like this at LLVM.TargetMachineEmitToFile caused by some code elsewhere, but outputting LLVM IR and then compiling that using llc works fine.

I have found that the following code in my project is causing it to crash:

var objectIdPtr = LLVM.BuildStructGEP(
    _builder,
    GetMethodObjectInstance(checkedFunction.LlvmValue!.Value),
    0,
    "".ToCString()
);
var objectId = LLVM.BuildLoad(
    _builder,
    objectIdPtr,
    "objectId".ToCString()
);

where GetMethodObjectInstance simply does LLVM.GetParam(method, 0). When outputting as LLVM IR, I find no issues with it, and neither does llc. Yet, LLVM.TargetMachineEmitToFile causes it to crash, and I am getting no feedback. This can't be expected behaviour, right?