[llvm] ef7ca14 - [WebAssembly] Report error for inline assembly with unsupported opcodes
Bryan Chan via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 11 07:27:13 PDT 2023
Author: David Mo
Date: 2023-07-11T10:36:25-04:00
New Revision: ef7ca14fa5091fc1d0b1992384bff700a2dac47d
URL: https://github.com/llvm/llvm-project/commit/ef7ca14fa5091fc1d0b1992384bff700a2dac47d
DIFF: https://github.com/llvm/llvm-project/commit/ef7ca14fa5091fc1d0b1992384bff700a2dac47d.diff
LOG: [WebAssembly] Report error for inline assembly with unsupported opcodes
For inline WebAssembly, passing a numeric operand to global.get is
unsupported. This causes encodeInstruction to reach an llvm_unreachable
call, leading to undefined behaviors. This patch fixes the issue for
this invalid instruction encoding, making it report an error by adding
an MCContext field in class WebAssemblyMCCodeEmitter.
Reviewed By: sbc100, bryanpkc
Differential Revision: https://reviews.llvm.org/D154734
Added:
llvm/test/CodeGen/WebAssembly/inline-asm-failure.ll
Modified:
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
Removed:
################################################################################
diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
index 545e6fe5845361..b57fc40663b9c2 100644
--- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
+++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp
@@ -16,6 +16,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/MC/MCCodeEmitter.h"
+#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstrInfo.h"
@@ -25,6 +26,7 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/LEB128.h"
+#include "llvm/Support/SMLoc.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
@@ -37,7 +39,7 @@ STATISTIC(MCNumFixups, "Number of MC fixups created.");
namespace {
class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
const MCInstrInfo &MCII;
-
+ MCContext &Ctx;
// Implementation generated by tablegen.
uint64_t getBinaryCodeForInstr(const MCInst &MI,
SmallVectorImpl<MCFixup> &Fixups,
@@ -48,12 +50,14 @@ class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
const MCSubtargetInfo &STI) const override;
public:
- WebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) : MCII(MCII) {}
+ WebAssemblyMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
+ : MCII(MCII), Ctx{Ctx} {}
};
} // end anonymous namespace
-MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) {
- return new WebAssemblyMCCodeEmitter(MCII);
+MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
+ MCContext &Ctx) {
+ return new WebAssemblyMCCodeEmitter(MCII, Ctx);
}
void WebAssemblyMCCodeEmitter::encodeInstruction(
@@ -120,7 +124,9 @@ void WebAssemblyMCCodeEmitter::encodeInstruction(
support::endian::write<uint64_t>(OS, MO.getImm(), support::little);
break;
case WebAssembly::OPERAND_GLOBAL:
- llvm_unreachable("wasm globals should only be accessed symbolicly");
+ Ctx.reportError(
+ SMLoc(),
+ Twine("Wasm globals should only be accessed symbolically!"));
default:
encodeULEB128(uint64_t(MO.getImm()), OS);
}
diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp
index e49ada4b2119e5..e8f58a19d25e3b 100644
--- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp
+++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp
@@ -84,7 +84,7 @@ static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/,
static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
MCContext &Ctx) {
- return createWebAssemblyMCCodeEmitter(MCII);
+ return createWebAssemblyMCCodeEmitter(MCII, Ctx);
}
static MCAsmBackend *createAsmBackend(const Target & /*T*/,
diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
index 773223b7287404..fc33cebaa48a00 100644
--- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
+++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
@@ -16,6 +16,7 @@
#include "../WebAssemblySubtarget.h"
#include "llvm/BinaryFormat/Wasm.h"
+#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/Support/DataTypes.h"
#include <memory>
@@ -28,7 +29,8 @@ class MCInstrInfo;
class MCObjectTargetWriter;
class Triple;
-MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII);
+MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
+ MCContext &Ctx);
MCAsmBackend *createWebAssemblyAsmBackend(const Triple &TT);
diff --git a/llvm/test/CodeGen/WebAssembly/inline-asm-failure.ll b/llvm/test/CodeGen/WebAssembly/inline-asm-failure.ll
new file mode 100644
index 00000000000000..4311466f9449bd
--- /dev/null
+++ b/llvm/test/CodeGen/WebAssembly/inline-asm-failure.ll
@@ -0,0 +1,26 @@
+; RUN: not llc -O0 --mtriple=wasm32 -filetype=obj \
+; RUN: -o /dev/null 2>&1 <%s | FileCheck %s
+source_filename = "rust-issue-111471.c"
+target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
+target triple = "wasm32-unknown-unknown"
+
+ at __main_void = hidden alias i32 (), ptr @main
+
+; Function Attrs: noinline nounwind optnone
+define hidden void @get_global() #0 {
+entry:
+ call void asm sideeffect "global.get 0", ""() #1, !srcloc !0
+ ret void
+}
+
+; Function Attrs: noinline nounwind optnone
+define hidden i32 @main() #0 {
+entry:
+ ret i32 0
+}
+
+attributes #0 = { noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="generic" "target-features"="+mutable-globals,+sign-ext" }
+attributes #1 = { nounwind }
+
+!0 = !{i64 32}
+; CHECK: <unknown>:0: error: Wasm globals should only be accessed symbolically!
More information about the llvm-commits
mailing list