[clang] b4a4988 - [HIP] Fix ubsan function checks applied to kernel functions (#188872)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 9 08:16:08 PDT 2026
Author: Joseph Huber
Date: 2026-04-09T10:16:03-05:00
New Revision: b4a49883ce7a896eeb1fae88ca5abe0746d01168
URL: https://github.com/llvm/llvm-project/commit/b4a49883ce7a896eeb1fae88ca5abe0746d01168
DIFF: https://github.com/llvm/llvm-project/commit/b4a49883ce7a896eeb1fae88ca5abe0746d01168.diff
LOG: [HIP] Fix ubsan function checks applied to kernel functions (#188872)
Summary:
The 'function' check requires inserting eight bytes of magic before each
function. The HIP runtime expects and enforces 256 byte alignment. When
the instrumentation inserts the eight bytes this is done after the
alignment, which means that the HIP runtime then points the PC to an
invalid instruction by truncating the address to 256 byte alignment. The
OpenMP runtime doesn't do this.
The purpose of this function is to handle indirect calls, and it's
impossible to indirectly call a kernel anyway, so we should just
suppress this in this case. The only other solution would be to add the
alignment back before we emit the label, but that would be meaningless
because it'd just replace the magic bytes with zeroes.
Added:
clang/test/CodeGen/AMDGPU/sanitizer.c
Modified:
clang/lib/CodeGen/CodeGenFunction.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 77c6866bbefa6..b920266b59808 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -1033,9 +1033,11 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
}
// If we are checking function types, emit a function type signature as
- // prologue data.
+ // prologue data. Kernel functions have strict alignment requirements and
+ // cannot be call indirectly so we do not instrument them.
if (FD && SanOpts.has(SanitizerKind::Function) &&
- !FD->getType()->isCFIUncheckedCalleeFunctionType()) {
+ !FD->getType()->isCFIUncheckedCalleeFunctionType() &&
+ llvm::isCallableCC(Fn->getCallingConv())) {
if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) {
llvm::LLVMContext &Ctx = Fn->getContext();
llvm::MDBuilder MDB(Ctx);
diff --git a/clang/test/CodeGen/AMDGPU/sanitizer.c b/clang/test/CodeGen/AMDGPU/sanitizer.c
new file mode 100644
index 0000000000000..3b41e4dd8babe
--- /dev/null
+++ b/clang/test/CodeGen/AMDGPU/sanitizer.c
@@ -0,0 +1,20 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6
+// RUN: %clang_cc1 -cc1 -triple amdgcn-amd-amdhsa -emit-llvm -fsanitize=function %s -o - | FileCheck %s
+
+// CHECK-LABEL: define dso_local void @function(
+// CHECK-SAME: ) #[[ATTR0:[0-9]+]] !func_sanitize [[META2:![0-9]+]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: ret void
+//
+void function() {}
+
+// CHECK-LABEL: define dso_local amdgpu_kernel void @kernel(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: call void @function() #[[ATTR1:[0-9]+]]
+// CHECK-NEXT: ret void
+//
+[[clang::device_kernel]] void kernel() { function(); }
+//.
+// CHECK: [[META2]] = !{i32 -1056584962, i32 1717976574}
+//.
More information about the cfe-commits
mailing list