Skip to content

Commit 194ce2d

Browse files
committed
[Clang] Always verify LLVM IR inputs
We get a lot of issues that basically boil down to "I passed malformed LLVM IR to clang and it crashed". Clang does not perform IR verification by default in (non-assertion-enabled) release builds, and that's sensible for IR that Clang itself produces, which is expected to always be valid. However, if people pass in their own handwritten IR, we should report if it is malformed, instead of crashing. We should also report it in a way that does not produce a crash trace and ask for a bug report, as currently happens in assertions-enabled builds. I've only added the verification for textual IR inputs. I don't want to force verification for bitcode inputs, as these would affect typical LTO scenarios, and are usually coming from Clang itself.
1 parent 7d4ea77 commit 194ce2d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ def err_ast_action_on_llvm_ir : Error<
379379
"cannot apply AST actions to LLVM IR file '%0'">,
380380
DefaultFatal;
381381

382+
def err_invalid_llvm_ir : Error<"invalid LLVM IR input: %0">;
383+
382384
def err_os_unsupport_riscv_fmv : Error<
383385
"function multiversioning is currently only supported on Linux">;
384386

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "llvm/IR/LLVMContext.h"
4040
#include "llvm/IR/LLVMRemarkStreamer.h"
4141
#include "llvm/IR/Module.h"
42+
#include "llvm/IR/Verifier.h"
4243
#include "llvm/IRReader/IRReader.h"
4344
#include "llvm/LTO/LTOBackend.h"
4445
#include "llvm/Linker/Linker.h"
@@ -1048,8 +1049,20 @@ CodeGenAction::loadModule(MemoryBufferRef MBRef) {
10481049

10491050
// Handle textual IR and bitcode file with one single module.
10501051
llvm::SMDiagnostic Err;
1051-
if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext))
1052+
if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext)) {
1053+
// For textual LLVM IR files, always verify the input and report the error
1054+
// in a way that does not ask people to report an issue for it.
1055+
if (!llvm::isBitcode((const unsigned char *)MBRef.getBufferStart(),
1056+
(const unsigned char *)MBRef.getBufferEnd())) {
1057+
std::string VerifierErr;
1058+
raw_string_ostream VerifierErrStream(VerifierErr);
1059+
if (llvm::verifyModule(*M, &VerifierErrStream)) {
1060+
CI.getDiagnostics().Report(diag::err_invalid_llvm_ir) << VerifierErr;
1061+
return {};
1062+
}
1063+
}
10521064
return M;
1065+
}
10531066

10541067
// If MBRef is a bitcode with multiple modules (e.g., -fsplit-lto-unit
10551068
// output), place the extra modules (actually only one, a regular LTO module)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; RUN: not %clang %s 2>&1 | FileCheck %s
2+
3+
; CHECK: error: invalid LLVM IR input: PHINode should have one entry for each predecessor of its parent basic block!
4+
; CHECK-NEXT: %phi = phi i32 [ 0, %entry ]
5+
6+
define void @test() {
7+
entry:
8+
%phi = phi i32 [ 0, %entry ]
9+
ret void
10+
}

0 commit comments

Comments
 (0)