[PATCH] D152023: [UBSan] Consider zero input to __builtin_clz/ctz to be undefined independent of the target.

Craig Topper via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 2 12:25:24 PDT 2023


craig.topper created this revision.
craig.topper added reviewers: efriedma, nikic, vsk.
Herald added a subscriber: StephenFan.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added a project: clang.

Previously we checked isCLZForZeroUndef and only added UBSan checks
if it returned true.

The builtin should be considered undefined for 0 regardless of
the target so that code using it is portable. The isCLZForZeroUndef
was only intended to disable optimizations in the middle end and
backend.

See https://discourse.llvm.org/t/should-ubsan-detect-0-input-to-builtin-clz-ctz-regardless-of-target/71060


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152023

Files:
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/test/CodeGen/ubsan-builtin-checks.c


Index: clang/test/CodeGen/ubsan-builtin-checks.c
===================================================================
--- clang/test/CodeGen/ubsan-builtin-checks.c
+++ clang/test/CodeGen/ubsan-builtin-checks.c
@@ -1,7 +1,8 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -w -emit-llvm -o - %s -fsanitize=builtin | FileCheck %s
-// RUN: %clang_cc1 -triple arm64-none-linux-gnu -w -emit-llvm -o - %s -fsanitize=builtin | FileCheck %s --check-prefix=NOT-UB
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -w -emit-llvm -o - %s -fsanitize=builtin | FileCheck %s --check-prefixes=CHECK,POISON
+// RUN: %clang_cc1 -triple arm64-none-linux-gnu -w -emit-llvm -o - %s -fsanitize=builtin | FileCheck %s --check-prefixes=CHECK,NOPOISON
 
-// NOT-UB-NOT: __ubsan_handle_invalid_builtin
+// A zero input to __bultin_ctz/clz is considered UB even if the target does not
+// want to optimize based on zero input being undefined.
 
 // CHECK: define{{.*}} void @check_ctz
 void check_ctz(int n) {
@@ -13,7 +14,8 @@
   // CHECK-NEXT: unreachable
   //
   // Continuation block:
-  // CHECK: call i32 @llvm.cttz.i32(i32 [[N]], i1 true)
+  // POISON: call i32 @llvm.cttz.i32(i32 [[N]], i1 true)
+  // NOPOISON: call i32 @llvm.cttz.i32(i32 [[N]], i1 false)
   __builtin_ctz(n);
 
   // CHECK: call void @__ubsan_handle_invalid_builtin
@@ -33,7 +35,8 @@
   // CHECK-NEXT: unreachable
   //
   // Continuation block:
-  // CHECK: call i32 @llvm.ctlz.i32(i32 [[N]], i1 true)
+  // POISON: call i32 @llvm.ctlz.i32(i32 [[N]], i1 true)
+  // NOPOISON: call i32 @llvm.ctlz.i32(i32 [[N]], i1 false)
   __builtin_clz(n);
 
   // CHECK: call void @__ubsan_handle_invalid_builtin
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -1741,7 +1741,7 @@
           && "Unsupported builtin check kind");
 
   Value *ArgValue = EmitScalarExpr(E);
-  if (!SanOpts.has(SanitizerKind::Builtin) || !getTarget().isCLZForZeroUndef())
+  if (!SanOpts.has(SanitizerKind::Builtin))
     return ArgValue;
 
   SanitizerScope SanScope(this);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152023.527932.patch
Type: text/x-patch
Size: 2131 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230602/3fb0b2b2/attachment-0001.bin>


More information about the cfe-commits mailing list