diff --git a/llvm/lib/CodeGen/WasmEHPrepare.cpp b/llvm/lib/CodeGen/WasmEHPrepare.cpp --- a/llvm/lib/CodeGen/WasmEHPrepare.cpp +++ b/llvm/lib/CodeGen/WasmEHPrepare.cpp @@ -80,6 +80,7 @@ #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/WasmEHFuncInfo.h" +#include "llvm/IR/EHPersonalities.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/IntrinsicsWebAssembly.h" #include "llvm/InitializePasses.h" @@ -209,6 +210,12 @@ if (CatchPads.empty() && CleanupPads.empty()) return false; + if (!F.hasPersonalityFn() || + !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) { + report_fatal_error("Function '" + F.getName() + + "' does not have a correct Wasm personality function " + "'__gxx_wasm_personality_v0'"); + } assert(F.hasPersonalityFn() && "Personality function not found"); // __wasm_lpad_context global variable. diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp @@ -1291,6 +1291,7 @@ // end_try const auto *EHInfo = MF.getWasmEHFuncInfo(); + assert(EHInfo); SmallVector EHPadStack; // For EH pads that have catch unwind mismatches, a map of . diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyExceptionInfo.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyExceptionInfo.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyExceptionInfo.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyExceptionInfo.cpp @@ -121,6 +121,7 @@ // and A's unwind destination is B and B's is C. When we visit B before A, we // end up extracting C only out of B but not out of A. const auto *EHInfo = MF.getWasmEHFuncInfo(); + assert(EHInfo); SmallVector> UnwindWEVec; for (auto *DomNode : depth_first(&MDT)) { diff --git a/llvm/test/CodeGen/WebAssembly/wasm-eh-invalid-personality.ll b/llvm/test/CodeGen/WebAssembly/wasm-eh-invalid-personality.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/WebAssembly/wasm-eh-invalid-personality.ll @@ -0,0 +1,26 @@ +; RUN: not --crash llc < %s -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling 2>&1 | FileCheck %s + +target triple = "wasm32-unknown-unknown" + +; Tests if the compiler correctly errors out when a function having EH pads does +; not have a correct Wasm personality function. + +define void @test() personality ptr @invalid_personality { +; CHECK: LLVM ERROR: Function 'test' does not have a correct Wasm personality function '__gxx_wasm_personality_v0' +entry: + invoke void @foo() + to label %try.cont unwind label %catch.dispatch + +catch.dispatch: ; preds = %entry + %0 = catchswitch within none [label %catch.start] unwind to caller + +catch.start: ; preds = %catch.dispatch + %1 = catchpad within %0 [ptr null] + catchret from %1 to label %try.cont + +try.cont: ; preds = %catch, %entry + ret void +} + +declare void @foo() +declare i32 @invalid_personality(...)