aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CodeGenAction.cpp
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2025-04-07 15:18:47 +0800
committerGitHub <noreply@github.com>2025-04-07 09:18:47 +0200
commit87a4215ed154e867683b10c8d7fe1dbc79d81abb (patch)
tree00c5551ce9c1e12a5225db885183dae55fdd57bf /clang/lib/CodeGen/CodeGenAction.cpp
parentf280d60c9839120618da353ab71004be33c4fa53 (diff)
downloadllvm-87a4215ed154e867683b10c8d7fe1dbc79d81abb.zip
llvm-87a4215ed154e867683b10c8d7fe1dbc79d81abb.tar.gz
llvm-87a4215ed154e867683b10c8d7fe1dbc79d81abb.tar.bz2
[Clang] Always verify LLVM IR inputs (#134396)
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. This aligns the behavior with how opt/llc work.
Diffstat (limited to 'clang/lib/CodeGen/CodeGenAction.cpp')
-rw-r--r--clang/lib/CodeGen/CodeGenAction.cpp12
1 files changed, 11 insertions, 1 deletions
diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp
index 4321efd..1f5eb42 100644
--- a/clang/lib/CodeGen/CodeGenAction.cpp
+++ b/clang/lib/CodeGen/CodeGenAction.cpp
@@ -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"
@@ -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)