https://github.com/keepyixiao created https://github.com/llvm/llvm-project/pull/230903
The CodeGen cast fails when the interrupt handler's first parameter is a typedef of a pointer type. Use castAs<PointerType>() to handle typedef types correctly. Add a regression test for typedef pointer parameters. Fixes https://github.com/llvm/llvm-project/issues/204756 >From c5d429d0ba277f1121cabc9455ee2a7de308e47a Mon Sep 17 00:00:00 2001 From: nudt_yixiao <[email protected]> Date: Sun, 11 Oct 2026 12:35:24 +0800 Subject: [PATCH] [Clang] Fix crash with typedef pointer in x86 interrupt handlers The CodeGen cast fails when the interrupt handler's first parameter is a typedef of a pointer type. Use castAs<PointerType>() to handle typedef types correctly. Add a regression test for typedef pointer parameters. --- clang/lib/CodeGen/Targets/X86.cpp | 2 +- clang/test/CodeGen/attr-x86-interrupt.c | 6 ++++++ clang/test/Sema/attr-x86-interrupt.c | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/Targets/X86.cpp b/clang/lib/CodeGen/Targets/X86.cpp index c0947c481d2b0..f55ea6f0e662b 100644 --- a/clang/lib/CodeGen/Targets/X86.cpp +++ b/clang/lib/CodeGen/Targets/X86.cpp @@ -1220,7 +1220,7 @@ static void addX86InterruptAttrs(const FunctionDecl *FD, llvm::GlobalValue *GV, if (FD->getNumParams() == 0) return; - auto PtrTy = cast<PointerType>(FD->getParamDecl(0)->getType()); + const auto *PtrTy = FD->getParamDecl(0)->getType()->castAs<PointerType>(); llvm::Type *ByValTy = CGM.getTypes().ConvertType(PtrTy->getPointeeType()); llvm::Attribute NewAttr = llvm::Attribute::getWithByValType( Fn->getContext(), ByValTy); diff --git a/clang/test/CodeGen/attr-x86-interrupt.c b/clang/test/CodeGen/attr-x86-interrupt.c index 107a8fd316bcc..416c4ea9003b9 100644 --- a/clang/test/CodeGen/attr-x86-interrupt.c +++ b/clang/test/CodeGen/attr-x86-interrupt.c @@ -3,6 +3,7 @@ // RUN: %clang_cc1 -triple x86_64-pc-win32 %s -emit-llvm -o - | FileCheck %s --check-prefix=X86_64_WIN // RUN: %clang_cc1 -triple i386-pc-win32 %s -emit-llvm -o - | FileCheck %s --check-prefix=X86_WIN // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnux32 %s -emit-llvm -o - | FileCheck %s --check-prefix=X86_64_LINUX +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -DTEST_TYPEDEF %s -emit-llvm -o - | FileCheck %s --check-prefix=TYPEDEF #ifdef __x86_64__ typedef __UINT64_TYPE__ uword; @@ -12,6 +13,11 @@ typedef __UINT32_TYPE__ uword; __attribute__((interrupt)) void foo7(int *a, uword b) {} __attribute__((interrupt)) void foo8(int *a) {} +#ifdef TEST_TYPEDEF +typedef int *int_ptr; +__attribute__((interrupt)) void foo_typedef(int_ptr a) {} +// TYPEDEF: define{{.*}} x86_intrcc void @foo_typedef(ptr noundef byval(i32) %{{.+}}) +#endif // X86_64_LINUX: @llvm.compiler.used = appending global [2 x ptr] [ptr @foo7, ptr @foo8], section "llvm.metadata" // X86_64_LINUX: define{{.*}} x86_intrcc void @foo7(ptr noundef byval(i32) %{{.+}}, i64 noundef %{{.+}}) // X86_64_LINUX: define{{.*}} x86_intrcc void @foo8(ptr noundef byval(i32) %{{.+}}) diff --git a/clang/test/Sema/attr-x86-interrupt.c b/clang/test/Sema/attr-x86-interrupt.c index 84c60227135fe..ea05e873ffeaa 100644 --- a/clang/test/Sema/attr-x86-interrupt.c +++ b/clang/test/Sema/attr-x86-interrupt.c @@ -40,6 +40,9 @@ __attribute__((interrupt)) void foo6(float *a, int b) {} __attribute__((interrupt)) void foo7(int *a, unsigned b) {} __attribute__((interrupt)) void foo8(int *a) {} +typedef *a; // expected-error {{type specifier missing}} +__attribute__((interrupt)) void b(a) {} // expected-warning {{omitting the parameter name}} + void g(void (*fp)(int *)); int main(int argc, char **argv) { void *ptr = (void *)&foo7; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
