From c7ee0b8bda8b32a800bc01e9151b364446a6e1b1 Mon Sep 17 00:00:00 2001 From: Mark Kettenis Date: Fri, 22 Apr 2022 01:55:58 -0400 Subject: [PATCH] [Clang] Fix the guaranteed alignment of memory returned by malloc/new on OpenBSD The guaranteed alignment is 16 bytes on OpenBSD. --- clang/lib/Basic/TargetInfo.cpp | 6 +++--- clang/test/Preprocessor/init-arm.c | 3 +++ clang/test/Preprocessor/init-ppc.c | 3 +++ clang/test/Preprocessor/init-x86.c | 3 +++ 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp index 801961dc3b56..ae7dbfe7eb77 100644 --- a/clang/lib/Basic/TargetInfo.cpp +++ b/clang/lib/Basic/TargetInfo.cpp @@ -69,11 +69,11 @@ TargetInfo::TargetInfo(const llvm::Triple &T) : Triple(T) { // From the glibc documentation, on GNU systems, malloc guarantees 16-byte // alignment on 64-bit systems and 8-byte alignment on 32-bit systems. See // https://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html. - // This alignment guarantee also applies to Windows and Android. On Darwin, - // the alignment is 16 bytes on both 64-bit and 32-bit systems. + // This alignment guarantee also applies to Windows and Android. On Darwin + // and OpenBSD, the alignment is 16 bytes on both 64-bit and 32-bit systems. if (T.isGNUEnvironment() || T.isWindowsMSVCEnvironment() || T.isAndroid()) NewAlign = Triple.isArch64Bit() ? 128 : Triple.isArch32Bit() ? 64 : 0; - else if (T.isOSDarwin()) + else if (T.isOSDarwin() || T.isOSOpenBSD()) NewAlign = 128; else NewAlign = 0; // Infer from basic type alignment. diff --git a/clang/test/Preprocessor/init-arm.c b/clang/test/Preprocessor/init-arm.c index 2d1503c18560..58ee6981bfb4 100644 --- a/clang/test/Preprocessor/init-arm.c +++ b/clang/test/Preprocessor/init-arm.c @@ -198,6 +198,9 @@ // RUN: %clang_cc1 -E -dM -triple=armv7-apple-ios7.0 -x c++ < /dev/null | FileCheck -match-full-lines -check-prefix ARM-DARWIN-CXX %s // ARM-DARWIN-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL +// RUN: %clang_cc1 -E -dM -triple=arm-unknown-openbsd -x c++ < /dev/null | FileCheck -match-full-lines -check-prefix ARM-OPENBSD-CXX %s +// ARM-OPENBSD-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL + // RUN: %clang_cc1 -dM -ffreestanding -triple arm-none-none -target-abi apcs-gnu -E /dev/null -o - | FileCheck -match-full-lines -check-prefix ARM-APCS-GNU %s // ARM-APCS-GNU: #define __INTPTR_TYPE__ int // ARM-APCS-GNU: #define __PTRDIFF_TYPE__ int diff --git a/clang/test/Preprocessor/init-ppc.c b/clang/test/Preprocessor/init-ppc.c index 45c8a5e53ad4..46db63c722bc 100644 --- a/clang/test/Preprocessor/init-ppc.c +++ b/clang/test/Preprocessor/init-ppc.c @@ -1171,3 +1171,6 @@ // PPC-DARWIN:#define __WINT_WIDTH__ 32 // PPC-DARWIN:#define __powerpc__ 1 // PPC-DARWIN:#define __ppc__ 1 + +// RUN: %clang_cc1 -E -dM -triple=powerpc-unknown-openbsd -x c++ < /dev/null | FileCheck -match-full-lines -check-prefix PPC-OPENBSD-CXX %s +// PPC-OPENBSD-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL diff --git a/clang/test/Preprocessor/init-x86.c b/clang/test/Preprocessor/init-x86.c index aa2e05ec807c..339941fcf3ee 100644 --- a/clang/test/Preprocessor/init-x86.c +++ b/clang/test/Preprocessor/init-x86.c @@ -1717,3 +1717,6 @@ // X86_64-NETBSD:#define __amd64__ 1 // X86_64-NETBSD:#define __x86_64 1 // X86_64-NETBSD:#define __x86_64__ 1 + +// RUN: %clang_cc1 -E -dM -triple=i386-unknown-openbsd -x c++ < /dev/null | FileCheck -match-full-lines -check-prefix I386-OPENBSD-CXX %s +// I386-OPENBSD-CXX: #define __STDCPP_DEFAULT_NEW_ALIGNMENT__ 16UL