r297750 - [ubsan] Use the nicer nullability diagnostic handlers

Vedant Kumar via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 14 09:48:29 PDT 2017


Author: vedantk
Date: Tue Mar 14 11:48:29 2017
New Revision: 297750

URL: http://llvm.org/viewvc/llvm-project?rev=297750&view=rev
Log:
[ubsan] Use the nicer nullability diagnostic handlers

This is a follow-up to r297700 (Add a nullability sanitizer).

It addresses some FIXME's re: using nullability-specific diagnostic
handlers from compiler-rt, now that the necessary handlers exist.

check-ubsan test updates to follow.

Modified:
    cfe/trunk/lib/CodeGen/CGCall.cpp
    cfe/trunk/lib/CodeGen/CGDecl.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h
    cfe/trunk/test/CodeGenObjC/ubsan-nullability.m

Modified: cfe/trunk/lib/CodeGen/CGCall.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCall.cpp?rev=297750&r1=297749&r2=297750&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGCall.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCall.cpp Tue Mar 14 11:48:29 2017
@@ -2938,18 +2938,20 @@ void CodeGenFunction::EmitReturnValueChe
   // Prefer the returns_nonnull attribute if it's present.
   SourceLocation AttrLoc;
   SanitizerMask CheckKind;
+  SanitizerHandler Handler;
   if (RetNNAttr) {
     assert(!requiresReturnValueNullabilityCheck() &&
            "Cannot check nullability and the nonnull attribute");
     AttrLoc = RetNNAttr->getLocation();
     CheckKind = SanitizerKind::ReturnsNonnullAttribute;
+    Handler = SanitizerHandler::NonnullReturn;
   } else {
-    // FIXME: The runtime shouldn't refer to the 'returns_nonnull' attribute.
     if (auto *DD = dyn_cast<DeclaratorDecl>(CurCodeDecl))
       if (auto *TSI = DD->getTypeSourceInfo())
         if (auto FTL = TSI->getTypeLoc().castAs<FunctionTypeLoc>())
           AttrLoc = FTL.getReturnLoc().findNullabilityLoc();
     CheckKind = SanitizerKind::NullabilityReturn;
+    Handler = SanitizerHandler::NullabilityReturn;
   }
 
   SanitizerScope SanScope(this);
@@ -2971,8 +2973,7 @@ void CodeGenFunction::EmitReturnValueChe
   llvm::Constant *StaticData[] = {
       EmitCheckSourceLocation(EndLoc), EmitCheckSourceLocation(AttrLoc),
   };
-  EmitCheck(std::make_pair(Cond, CheckKind), SanitizerHandler::NonnullReturn,
-            StaticData, None);
+  EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, None);
 
   if (requiresReturnValueNullabilityCheck())
     EmitBlock(NoCheck);
@@ -3314,12 +3315,15 @@ void CodeGenFunction::EmitNonNullArgChec
 
   SourceLocation AttrLoc;
   SanitizerMask CheckKind;
+  SanitizerHandler Handler;
   if (NNAttr) {
     AttrLoc = NNAttr->getLocation();
     CheckKind = SanitizerKind::NonnullAttribute;
+    Handler = SanitizerHandler::NonnullArg;
   } else {
     AttrLoc = PVD->getTypeSourceInfo()->getTypeLoc().findNullabilityLoc();
     CheckKind = SanitizerKind::NullabilityArg;
+    Handler = SanitizerHandler::NullabilityArg;
   }
 
   SanitizerScope SanScope(this);
@@ -3331,8 +3335,7 @@ void CodeGenFunction::EmitNonNullArgChec
       EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),
       llvm::ConstantInt::get(Int32Ty, ArgNo + 1),
   };
-  EmitCheck(std::make_pair(Cond, CheckKind), SanitizerHandler::NonnullArg,
-            StaticData, None);
+  EmitCheck(std::make_pair(Cond, CheckKind), Handler, StaticData, None);
 }
 
 void CodeGenFunction::EmitCallArgs(

Modified: cfe/trunk/lib/CodeGen/CGDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDecl.cpp?rev=297750&r1=297749&r2=297750&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDecl.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDecl.cpp Tue Mar 14 11:48:29 2017
@@ -685,11 +685,10 @@ void CodeGenFunction::EmitNullabilityChe
   // hand side must be nonnull.
   SanitizerScope SanScope(this);
   llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
-  // FIXME: The runtime shouldn't refer to a 'reference'.
   llvm::Constant *StaticData[] = {
       EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(LHS.getType()),
-      llvm::ConstantInt::get(Int8Ty, 1),
-      llvm::ConstantInt::get(Int8Ty, TCK_ReferenceBinding)};
+      llvm::ConstantInt::get(Int8Ty, 0), //< The LogAlignment info is unused.
+      llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
   EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}},
             SanitizerHandler::TypeMismatch, StaticData, RHS);
 }

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=297750&r1=297749&r2=297750&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Tue Mar 14 11:48:29 2017
@@ -115,6 +115,8 @@ enum TypeEvaluationKind {
   SANITIZER_CHECK(MissingReturn, missing_return, 0)                            \
   SANITIZER_CHECK(MulOverflow, mul_overflow, 0)                                \
   SANITIZER_CHECK(NegateOverflow, negate_overflow, 0)                          \
+  SANITIZER_CHECK(NullabilityArg, nullability_arg, 0)                          \
+  SANITIZER_CHECK(NullabilityReturn, nullability_return, 0)                    \
   SANITIZER_CHECK(NonnullArg, nonnull_arg, 0)                                  \
   SANITIZER_CHECK(NonnullReturn, nonnull_return, 0)                            \
   SANITIZER_CHECK(OutOfBounds, out_of_bounds, 0)                               \
@@ -2290,7 +2292,9 @@ public:
     TCK_Upcast,
     /// Checking the operand of a cast to a virtual base object. Must be an
     /// object within its lifetime.
-    TCK_UpcastToVirtualBase
+    TCK_UpcastToVirtualBase,
+    /// Checking the value assigned to a _Nonnull pointer. Must not be null.
+    TCK_NonnullAssign
   };
 
   /// \brief Whether any type-checking sanitizers are enabled. If \c false,

Modified: cfe/trunk/test/CodeGenObjC/ubsan-nullability.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/ubsan-nullability.m?rev=297750&r1=297749&r2=297750&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenObjC/ubsan-nullability.m (original)
+++ cfe/trunk/test/CodeGenObjC/ubsan-nullability.m Tue Mar 14 11:48:29 2017
@@ -20,7 +20,7 @@ int *_Nonnull nonnull_retval1(int *p) {
   // CHECK: [[NULL]]:
   // CHECK: [[ICMP:%.*]] = icmp ne i32* {{.*}}, null, !nosanitize
   // CHECK-NEXT: br i1 [[ICMP]], {{.*}}, !nosanitize
-  // CHECK: call void @__ubsan_handle_nonnull_return{{.*}}[[NONNULL_RV_LOC1]]
+  // CHECK: call void @__ubsan_handle_nullability_return{{.*}}[[NONNULL_RV_LOC1]]
   return p;
   // CHECK: [[NONULL]]:
   // CHECK-NEXT: ret i32*
@@ -34,7 +34,7 @@ void nonnull_arg(int *_Nonnull p) {}
 void call_func_with_nonnull_arg(int *_Nonnull p) {
   // CHECK: [[ICMP:%.*]] = icmp ne i32* {{.*}}, null, !nosanitize
   // CHECK-NEXT: br i1 [[ICMP]], {{.*}}, !nosanitize
-  // CHECK: call void @__ubsan_handle_nonnull_arg{{.*}}[[NONNULL_ARG_LOC]]
+  // CHECK: call void @__ubsan_handle_nullability_arg{{.*}}[[NONNULL_ARG_LOC]]
   nonnull_arg(p);
 }
 
@@ -108,7 +108,7 @@ int *_Nonnull nonnull_retval2(int *_Nonn
   // CHECK: [[NULL]]:
   // CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32* {{.*}}, null, !nosanitize
   // CHECK-NEXT: br i1 [[ICMP]], {{.*}}, !nosanitize
-  // CHECK: call void @__ubsan_handle_nonnull_return{{.*}}[[NONNULL_RV_LOC2]]
+  // CHECK: call void @__ubsan_handle_nullability_return{{.*}}[[NONNULL_RV_LOC2]]
   return arg1;
   // CHECK: [[NONULL]]:
   // CHECK-NEXT: ret i32*
@@ -129,7 +129,7 @@ int *_Nonnull nonnull_retval2(int *_Nonn
   // CHECK: [[NULL]]:
   // CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32* {{.*}}, null, !nosanitize
   // CHECK-NEXT: br i1 [[ICMP]], {{.*}}, !nosanitize
-  // CHECK: call void @__ubsan_handle_nonnull_return{{.*}}
+  // CHECK: call void @__ubsan_handle_nullability_return{{.*}}
   return arg1;
   // CHECK: [[NONULL]]:
   // CHECK-NEXT: ret i32*
@@ -143,7 +143,7 @@ int *_Nonnull nonnull_retval2(int *_Nonn
   // CHECK: [[NULL]]:
   // CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32* {{.*}}, null, !nosanitize
   // CHECK-NEXT: br i1 [[ICMP]], {{.*}}, !nosanitize
-  // CHECK: call void @__ubsan_handle_nonnull_return{{.*}}
+  // CHECK: call void @__ubsan_handle_nullability_return{{.*}}
   return arg1;
   // CHECK: [[NONULL]]:
   // CHECK-NEXT: ret i32*
@@ -154,13 +154,13 @@ int *_Nonnull nonnull_retval2(int *_Nonn
 void call_A(A *a, int *p) {
   // CHECK: [[ICMP:%.*]] = icmp ne i32* [[P1:%.*]], null, !nosanitize
   // CHECK-NEXT: br i1 [[ICMP]], {{.*}}, !nosanitize
-  // CHECK: call void @__ubsan_handle_nonnull_arg{{.*}} !nosanitize
+  // CHECK: call void @__ubsan_handle_nullability_arg{{.*}} !nosanitize
   // CHECK: call i32* {{.*}} @objc_msgSend to i32* {{.*}}({{.*}}, i32* [[P1]])
   [a objc_method: p];
 
   // CHECK: [[ICMP:%.*]] = icmp ne i32* [[P2:%.*]], null, !nosanitize
   // CHECK-NEXT: br i1 [[ICMP]], {{.*}}, !nosanitize
-  // CHECK: call void @__ubsan_handle_nonnull_arg{{.*}} !nosanitize
+  // CHECK: call void @__ubsan_handle_nullability_arg{{.*}} !nosanitize
   // CHECK: call i32* {{.*}} @objc_msgSend to i32* {{.*}}({{.*}}, i32* [[P2]])
   [A objc_clsmethod: p];
 }




More information about the cfe-commits mailing list