r350779 - In nothrow new-expressions, null-check the result if we're going to

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 9 16:03:29 PST 2019


Author: rsmith
Date: Wed Jan  9 16:03:29 2019
New Revision: 350779

URL: http://llvm.org/viewvc/llvm-project?rev=350779&view=rev
Log:
In nothrow new-expressions, null-check the result if we're going to
apply sanitizers to it.

This avoids a sanitizer false positive that we are initializing a null
pointer.

Modified:
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp
    cfe/trunk/test/CodeGenCXX/catch-undef-behavior.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=350779&r1=350778&r2=350779&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Wed Jan  9 16:03:29 2019
@@ -1656,9 +1656,10 @@ llvm::Value *CodeGenFunction::EmitCXXNew
   // Emit a null check on the allocation result if the allocation
   // function is allowed to return null (because it has a non-throwing
   // exception spec or is the reserved placement new) and we have an
-  // interesting initializer.
+  // interesting initializer will be running sanitizers on the initialization.
   bool nullCheck = E->shouldNullCheckAllocation() &&
-                   (!allocType.isPODType(getContext()) || E->hasInitializer());
+                   (!allocType.isPODType(getContext()) || E->hasInitializer() ||
+                    sanitizePerformTypeCheck());
 
   llvm::BasicBlock *nullCheckBB = nullptr;
   llvm::BasicBlock *contBB = nullptr;

Modified: cfe/trunk/test/CodeGenCXX/catch-undef-behavior.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/catch-undef-behavior.cpp?rev=350779&r1=350778&r2=350779&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/catch-undef-behavior.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/catch-undef-behavior.cpp Wed Jan  9 16:03:29 2019
@@ -520,6 +520,49 @@ void upcast_to_vbase() {
 }
 }
 
+struct nothrow {};
+void *operator new[](__SIZE_TYPE__, nothrow) noexcept;
+
+namespace NothrowNew {
+  struct X { X(); };
+
+  // CHECK-LABEL: define{{.*}}nothrow_new_trivial
+  void *nothrow_new_trivial() {
+    // CHECK: %[[is_null:.*]] = icmp eq i8*{{.*}}, null
+    // CHECK: br i1 %[[is_null]], label %[[null:.*]], label %[[nonnull:.*]]
+
+    // CHECK: [[nonnull]]:
+    // CHECK: llvm.objectsize
+    // CHECK: br i1
+    //
+    // CHECK: call {{.*}}__ubsan_handle_type_mismatch
+    //
+    // CHECK: [[null]]:
+    // CHECK-NOT: {{ }}br{{ }}
+    // CHECK: ret
+    return new (nothrow{}) char[123456];
+  }
+
+  // CHECK-LABEL: define{{.*}}nothrow_new_nontrivial
+  void *nothrow_new_nontrivial() {
+    // CHECK: %[[is_null:.*]] = icmp eq i8*{{.*}}, null
+    // CHECK: br i1 %[[is_null]], label %[[null:.*]], label %[[nonnull:.*]]
+
+    // CHECK: [[nonnull]]:
+    // CHECK: llvm.objectsize
+    // CHECK: br i1
+    //
+    // CHECK: call {{.*}}__ubsan_handle_type_mismatch
+    //
+    // CHECK: call {{.*}}_ZN10NothrowNew1XC1Ev
+    //
+    // CHECK: [[null]]:
+    // CHECK-NOT: {{ }}br{{ }}
+    // CHECK: ret
+    return new (nothrow{}) X[123456];
+  }
+}
+
 struct ThisAlign {
   void this_align_lambda();
   void this_align_lambda_2();




More information about the cfe-commits mailing list