belkiss created this revision.
belkiss requested review of this revision.
Herald added projects: clang, Sanitizers.
Herald added subscribers: Sanitizers, cfe-commits.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D116861
Files:
clang/lib/CodeGen/CGExprCXX.cpp
compiler-rt/test/ubsan/TestCases/TypeCheck/global-new-alignment.cpp
Index: compiler-rt/test/ubsan/TestCases/TypeCheck/global-new-alignment.cpp
===================================================================
--- /dev/null
+++ compiler-rt/test/ubsan/TestCases/TypeCheck/global-new-alignment.cpp
@@ -0,0 +1,32 @@
+// RUN: %clangxx -fsanitize=alignment %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not="runtime error"
-allow-empty
+
+#include <cassert>
+#include <cstddef>
+#include <cstdlib>
+
+void *operator new(std::size_t count) {
+ constexpr const size_t offset = 8;
+
+ // allocate a bit more so we can safely offset it
+ void *ptr = std::malloc(count + offset);
+
+ // verify malloc returned 16 bytes aligned mem
+ static_assert(__STDCPP_DEFAULT_NEW_ALIGNMENT__ == 16,
+ "Global new doesn't return 16 bytes aligned memory!");
+ assert(((std::ptrdiff_t)ptr & (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1)) == 0);
+
+ return (char *)ptr + offset;
+}
+
+struct Param {
+ void *_cookie1;
+ void *_cookie2;
+};
+
+static_assert(alignof(Param) == 8, "Param struct alignment must be 8 bytes!");
+
+int main() {
+ // CHECK-NOT: runtime error: constructor call on misaligned address
[[PTR:0x[0-9a-f]*]] for type 'Param', which requires 16 byte alignment
+ Param *p = new Param;
+}
Index: clang/lib/CodeGen/CGExprCXX.cpp
===================================================================
--- clang/lib/CodeGen/CGExprCXX.cpp
+++ clang/lib/CodeGen/CGExprCXX.cpp
@@ -1731,7 +1731,7 @@
SkippedChecks.set(SanitizerKind::Null, nullCheck);
EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall,
E->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(),
- result.getPointer(), allocType, result.getAlignment(),
+ result.getPointer(), allocType, allocAlign,
SkippedChecks, numElements);
EmitNewInitializer(*this, E, allocType, elementTy, result, numElements,
Index: compiler-rt/test/ubsan/TestCases/TypeCheck/global-new-alignment.cpp
===================================================================
--- /dev/null
+++ compiler-rt/test/ubsan/TestCases/TypeCheck/global-new-alignment.cpp
@@ -0,0 +1,32 @@
+// RUN: %clangxx -fsanitize=alignment %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s --implicit-check-not="runtime error" -allow-empty
+
+#include <cassert>
+#include <cstddef>
+#include <cstdlib>
+
+void *operator new(std::size_t count) {
+ constexpr const size_t offset = 8;
+
+ // allocate a bit more so we can safely offset it
+ void *ptr = std::malloc(count + offset);
+
+ // verify malloc returned 16 bytes aligned mem
+ static_assert(__STDCPP_DEFAULT_NEW_ALIGNMENT__ == 16,
+ "Global new doesn't return 16 bytes aligned memory!");
+ assert(((std::ptrdiff_t)ptr & (__STDCPP_DEFAULT_NEW_ALIGNMENT__ - 1)) == 0);
+
+ return (char *)ptr + offset;
+}
+
+struct Param {
+ void *_cookie1;
+ void *_cookie2;
+};
+
+static_assert(alignof(Param) == 8, "Param struct alignment must be 8 bytes!");
+
+int main() {
+ // CHECK-NOT: runtime error: constructor call on misaligned address [[PTR:0x[0-9a-f]*]] for type 'Param', which requires 16 byte alignment
+ Param *p = new Param;
+}
Index: clang/lib/CodeGen/CGExprCXX.cpp
===================================================================
--- clang/lib/CodeGen/CGExprCXX.cpp
+++ clang/lib/CodeGen/CGExprCXX.cpp
@@ -1731,7 +1731,7 @@
SkippedChecks.set(SanitizerKind::Null, nullCheck);
EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall,
E->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(),
- result.getPointer(), allocType, result.getAlignment(),
+ result.getPointer(), allocType, allocAlign,
SkippedChecks, numElements);
EmitNewInitializer(*this, E, allocType, elementTy, result, numElements,
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits