Author: zebullax
Date: 2026-11-12T20:50:16-08:00
New Revision: d57c09d14615d9875c03e4e2fcb08ffbd6c46e0c

URL: 
https://github.com/llvm/llvm-project/commit/d57c09d14615d9875c03e4e2fcb08ffbd6c46e0c
DIFF: 
https://github.com/llvm/llvm-project/commit/d57c09d14615d9875c03e4e2fcb08ffbd6c46e0c.diff

LOG: [ubsan] Improve scoped enum diagnostic printer (#216460)

Noticed that ubsan diagnostic which print out enum underlying value,
default to `<unknown>` for scoped enum.

**Why**
The original code path in `EmitCheckTypeDescriptor` checks against
whether the type is integer via `isIntegerType(),
isSignedIntegerType()`. Per language, enum class types are never
considered integer types so that always fails and then the UBSan runtime
default prints `<unknown>` instead of the real value.

**Fix**
Simply extract the enum's underlying integer type up front and use that
for the int checks instead of the enum type itself.

**before**
`error: load of value <unknown>, which is not a valid value for type
'enum EBool'`

**after**
`error: load of value <5>, which is not a valid value for type 'enum
EBool'`


Added a simple codegen test on the make up of type info for enum (based
on the representation for known types versus unknown), emitted by
sanitizer

---------

Signed-off-by: acassagnes <[email protected]>

Added: 
    clang/test/CodeGen/ubsan-enum-type-descriptor.cpp

Modified: 
    clang/lib/CodeGen/CGExpr.cpp
    compiler-rt/test/ubsan/TestCases/Misc/enum.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index ae5f4e723f512..5075292d3d45a 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -4057,6 +4057,14 @@ llvm::Constant 
*CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
   uint16_t TypeInfo = 0;
   bool IsBitInt = false;
 
+  // isIntegerType() never holds for scoped enums, and getAs<BitIntType> can't
+  // see through the EnumType node to a __BitInt underlying type even for
+  // unscoped enums.
+  QualType BackupT = T;
+  if (const EnumType *ET = T->getAs<EnumType>())
+    if (ET->getDecl()->isComplete())
+      T = ET->getDecl()->getIntegerType();
+
   if (T->isIntegerType()) {
     TypeKind = TK_Integer;
     TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
@@ -4085,8 +4093,8 @@ llvm::Constant 
*CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
   // optionally an 'aka'.
   SmallString<32> Buffer;
   CGM.getDiags().ConvertArgToString(DiagnosticsEngine::ak_qualtype,
-                                    (intptr_t)T.getAsOpaquePtr(), StringRef(),
-                                    StringRef(), {}, Buffer, {});
+                                    (intptr_t)BackupT.getAsOpaquePtr(),
+                                    StringRef(), StringRef(), {}, Buffer, {});
 
   if (IsBitInt) {
     // The Structure is: 0 to end the string, 32 bit unsigned integer in target
@@ -4115,7 +4123,7 @@ llvm::Constant 
*CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
   CGM.getSanitizerMetadata()->disableSanitizerForGlobal(GV);
 
   // Remember the descriptor for this type.
-  CGM.setTypeDescriptorInMap(T, GV);
+  CGM.setTypeDescriptorInMap(BackupT, GV);
 
   return GV;
 }

diff  --git a/clang/test/CodeGen/ubsan-enum-type-descriptor.cpp 
b/clang/test/CodeGen/ubsan-enum-type-descriptor.cpp
new file mode 100644
index 0000000000000..459a9d7caf260
--- /dev/null
+++ b/clang/test/CodeGen/ubsan-enum-type-descriptor.cpp
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=enum -emit-llvm %s -o - 
| FileCheck %s
+
+// The UBSan type descriptor wraps the size and signedness of the checked type
+// so that the runtime can print the offending value. An enum does have the 
same
+// size and value representation as its underlying type, so the descriptor has
+// to unwrap up to the underlying type and not the enum: querying the enum type
+// itself leaves a scoped enum as TK_Unknown (i16 -1) with no width, and the
+// runtime then prints "<unknown>" instead of the value.
+
+// A scoped enum has a fixed underlying type, so it only reaches the range 
check
+// when that type has values outside the enumerator range, i.e. bool.
+enum class EBool : bool { a = 1 };
+
+// TypeKind is TK_Integer (0) and TypeInfo is (log2(8) << 1) => 6.
+// Name length includes the quotes (''), so 8
+// CHECK: @{{[0-9]+}} = private unnamed_addr constant { i16, i16, [8 x i8] } { 
i16 0, i16 6, [8 x i8] c"'EBool'\00" }
+
+enum EPlain { p = 1 };
+
+// TypeInfo is (log2(32) << 1) ==> 10.
+// CHECK: @{{[0-9]+}} = private unnamed_addr constant { i16, i16, [9 x i8] } { 
i16 0, i16 10, [9 x i8] c"'EPlain'\00" }
+
+// A negative enumerator gives the enum a signed underlying type, which sets 
the
+// low bit of TypeInfo.
+enum ENeg { n = -1 };
+
+// TypeInfo is ((log2(32) << 1) | 1) ==> 11.
+// CHECK: @{{[0-9]+}} = private unnamed_addr constant { i16, i16, [7 x i8] } { 
i16 0, i16 11, [7 x i8] c"'ENeg'\00" }
+
+// Descriptors are only emitted where a check is, so force a checked load of
+// each
+
+bool load_scoped(EBool *p) { return (bool)*p; }
+// CHECK: call void @__ubsan_handle_load_invalid_value_abort
+
+int load_unscoped(EPlain *p) { return (int)*p; }
+// CHECK: call void @__ubsan_handle_load_invalid_value_abort
+
+int load_signed(ENeg *p) { return (int)*p; }
+// CHECK: call void @__ubsan_handle_load_invalid_value_abort

diff  --git a/compiler-rt/test/ubsan/TestCases/Misc/enum.cpp 
b/compiler-rt/test/ubsan/TestCases/Misc/enum.cpp
index 2684029c2960b..b51347f07c050 100644
--- a/compiler-rt/test/ubsan/TestCases/Misc/enum.cpp
+++ b/compiler-rt/test/ubsan/TestCases/Misc/enum.cpp
@@ -25,7 +25,7 @@ int main(int argc, char **argv) {
   return ((int)e1 != -1) & ((int)e2 != -1) &
          // CHECK: error: load of value 4294967295, which is not a valid value 
for type 'E'
          ((int)e3 != -1) & ((int)e4 == 1) &
-         // CHECK: error: load of value <unknown>, which is not a valid value 
for type 'enum EBool'
+         // CHECK: error: load of value 255, which is not a valid value for 
type 'enum EBool'
          ((int)e5 == 2) & ((int)e6 == 1) &
          // CHECK: error: load of value 2, which is not a valid value for type 
'EEmpty'
          ((int)e7 == 2);


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to