Skip to content

[Clang] Always verify LLVM IR inputs #134396

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

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticFrontendKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ def err_ast_action_on_llvm_ir : Error<
"cannot apply AST actions to LLVM IR file '%0'">,
DefaultFatal;

def err_invalid_llvm_ir : Error<"invalid LLVM IR input: %0">;

def err_os_unsupport_riscv_fmv : Error<
"function multiversioning is currently only supported on Linux">;

Expand Down
12 changes: 11 additions & 1 deletion clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LLVMRemarkStreamer.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/LTO/LTOBackend.h"
#include "llvm/Linker/Linker.h"
Expand Down Expand Up @@ -1048,8 +1049,17 @@ CodeGenAction::loadModule(MemoryBufferRef MBRef) {

// Handle textual IR and bitcode file with one single module.
llvm::SMDiagnostic Err;
if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext))
if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext)) {
// For LLVM IR files, always verify the input and report the error in a way
// that does not ask people to report an issue for it.
std::string VerifierErr;
raw_string_ostream VerifierErrStream(VerifierErr);
if (llvm::verifyModule(*M, &VerifierErrStream)) {
CI.getDiagnostics().Report(diag::err_invalid_llvm_ir) << VerifierErr;
return {};
}
return M;
}

// If MBRef is a bitcode with multiple modules (e.g., -fsplit-lto-unit
// output), place the extra modules (actually only one, a regular LTO module)
Expand Down
12 changes: 12 additions & 0 deletions clang/test/CodeGen/invalid_llvm_ir.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; RUN: not %clang %s 2>&1 | FileCheck %s
; RUN: llvm-as -disable-verify < %s > %t.bc
; RUN: not %clang %t.bc 2>&1 | FileCheck %s

; CHECK: error: invalid LLVM IR input: PHINode should have one entry for each predecessor of its parent basic block!
; CHECK-NEXT: %phi = phi i32 [ 0, %entry ]

define void @test() {
entry:
%phi = phi i32 [ 0, %entry ]
ret void
}