================
@@ -1749,17 +1751,32 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const 
CXXNewExpr *E) {
       allocator->isReservedGlobalPlacementOperator())
     result = Builder.CreateLaunderInvariantGroup(result);
 
+  // Check the default alignment of the type and why. Users may incorrectly
+  // return misaligned memory from a replaced operator new without knowing
+  // about default alignment.
+  TypeCheckKind checkKind = CodeGenFunction::TCK_ConstructorCall;
+  CharUnits checkAlignment = result.getAlignment();
+  const TargetInfo &TI = getContext().getTargetInfo();
+  unsigned DefaultTargetAlignment = TI.getNewAlign() / TI.getCharWidth();
+  if (SanOpts.has(SanitizerKind::Alignment) &&
+      (DefaultTargetAlignment >
+       CGM.getContext().getTypeAlignInChars(allocType).getQuantity()) &&
+      !result.getAlignment().isOne() &&
----------------
gbMattN wrote:

Its an imperfect check, I've spent some time trying to find something better. 
Its all for the sake of this case in the clang test catch-undef-behaviour:
```
  // CHECK-LABEL: define{{.*}}throwing_new_zero_size
  void *throwing_new_zero_size() {
    // Nothing to check here.
    // CHECK-NOT: __ubsan_handle_type_mismatch
    return new (nothrow{}) char[0];
    // CHECK: ret
  }
}
```
afaik, a nothrow char array of size 0 is an exception to the undefined 
behaviour. All I added was the isOne check as it made the test pass, but since 
looking into the matter further its too broad an exception.

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

Reply via email to