https://github.com/ABWI-Y updated https://github.com/llvm/llvm-project/pull/213539
>From 6009fa1475e8076c67d459fbcd9edceca152f148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E7=BA=AA=E5=85=83?= <[email protected]> Date: Sun, 2 Aug 2026 20:47:13 +0800 Subject: [PATCH] [X86][AsmParser] Fix compiler crash on division by zero in MS inline asm This fixes issue #213415. If a user writes something like '1 / 0' or '1 % 0' in assembly, the compiler will now show a normal error message instead of crashing completely. Fixes #213415 --- clang/test/Parser/ms-inline-asm-div-by-zero.c | 6 +++++ .../lib/Target/X86/AsmParser/X86AsmParser.cpp | 25 +++++++++++++++++-- llvm/test/MC/X86/intel-expr-div-by-zero.s | 7 ++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 clang/test/Parser/ms-inline-asm-div-by-zero.c create mode 100644 llvm/test/MC/X86/intel-expr-div-by-zero.s diff --git a/clang/test/Parser/ms-inline-asm-div-by-zero.c b/clang/test/Parser/ms-inline-asm-div-by-zero.c new file mode 100644 index 0000000000000..350435c4d5c22 --- /dev/null +++ b/clang/test/Parser/ms-inline-asm-div-by-zero.c @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fms-extensions -fasm-blocks -fsyntax-only -verify %s + +void foo() { + __asm { return 1 / 0; } // expected-error {{division by zero in assembly expression}} + __asm { return 1 % 0; } // expected-error {{modulo by zero in assembly expression}} +} diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp index 54edcba34a7e9..5046e58942fd2 100644 --- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp +++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp @@ -890,6 +890,29 @@ class X86AsmParser : public MCTargetAsmParser { default: State = IES_ERROR; break; + case IES_DIVIDE: + case IES_MOD: { + auto HasError = [&ErrMsg](IntelExprState IES, int64_t TmpInt) { + if (TmpInt != 0) + return false; + switch (IES) { + case IES_DIVIDE: + ErrMsg = "division by zero in assembly expression"; + break; + case IES_MOD: + ErrMsg = "modulo by zero in assembly expression"; + break; + default: + llvm_unreachable("unreachable"); + } + return true; + }; + if (HasError(State, TmpInt)) { + State = IES_ERROR; + return true; + } + [[fallthrough]]; + } case IES_PLUS: case IES_MINUS: case IES_NOT: @@ -904,8 +927,6 @@ class X86AsmParser : public MCTargetAsmParser { case IES_GE: case IES_LSHIFT: case IES_RSHIFT: - case IES_DIVIDE: - case IES_MOD: case IES_MULTIPLY: case IES_LPAREN: case IES_INIT: diff --git a/llvm/test/MC/X86/intel-expr-div-by-zero.s b/llvm/test/MC/X86/intel-expr-div-by-zero.s new file mode 100644 index 0000000000000..2dae4e9967976 --- /dev/null +++ b/llvm/test/MC/X86/intel-expr-div-by-zero.s @@ -0,0 +1,7 @@ +# RUN: not llvm-mc -triple x86_64-unknown-unknown -x86-asm-syntax=intel %s 2>&1 | FileCheck %s + +# CHECK: error: division by zero +mov eax, 1 / 0 + +# CHECK: error: modulo by zero +mov eax, 1 % 0 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
