Index: include/clang/Basic/AlignedExceptionObject.h =================================================================== --- /dev/null +++ include/clang/Basic/AlignedExceptionObject.h @@ -0,0 +1,44 @@ +//===--- AlignedExceptionObject.h - Aligned exception object ----*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Defines a function that returns the minimum OS versions that guarantee +/// exception objects are double-word aligned. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_BASIC_ALIGNED_ALLOCATION_H +#define LLVM_CLANG_BASIC_ALIGNED_ALLOCATION_H + +#include "llvm/ADT/Triple.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/VersionTuple.h" + +namespace clang { + +inline llvm::VersionTuple +alignedExceptionObjectMinVersion(llvm::Triple::OSType OS) { + switch (OS) { + default: + break; + case llvm::Triple::Darwin: + case llvm::Triple::MacOSX: // Earliest supporting version is 10.14. + return llvm::VersionTuple(10U, 14U); + case llvm::Triple::IOS: + case llvm::Triple::TvOS: // Earliest supporting version is 12.0.0. + return llvm::VersionTuple(12U); + case llvm::Triple::WatchOS: // Earliest supporting version is 5.0.0. + return llvm::VersionTuple(5U); + } + + llvm_unreachable("Unexpected OS"); +} + +} // end namespace clang + +#endif // LLVM_CLANG_BASIC_ALIGNED_ALLOCATION_H Index: include/clang/Basic/DiagnosticGroups.td =================================================================== --- include/clang/Basic/DiagnosticGroups.td +++ include/clang/Basic/DiagnosticGroups.td @@ -408,6 +408,7 @@ def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">; def ObjCBoxing : DiagGroup<"objc-boxing">; def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">; +def UnderalignedExcpObj : DiagGroup<"underaligned-exception-object">; def DeprecatedObjCIsaUsage : DiagGroup<"deprecated-objc-isa-usage">; def ExplicitInitializeCall : DiagGroup<"explicit-initialize-call">; def Packed : DiagGroup<"packed">; Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -6564,6 +6564,12 @@ "cannot throw object of incomplete type %0">; def err_throw_incomplete_ptr : Error< "cannot throw pointer to object of incomplete type %0">; +def warn_throw_underaligned_obj : Warning< + "underaligned exception object thrown">, + InGroup; +def note_throw_underaligned_obj : Note< + "The alignment of %0 (%1-bytes) is larger than the guaranteed alignment of " + "the memory Itanium C++ runtime returns (%2-bytes)">; def err_return_in_constructor_handler : Error< "return in the catch of a function try block of a constructor is illegal">; def warn_cdtor_function_try_handler_mem_expr : Warning< Index: include/clang/Basic/TargetInfo.h =================================================================== --- include/clang/Basic/TargetInfo.h +++ include/clang/Basic/TargetInfo.h @@ -637,6 +637,21 @@ /// types for the given target. unsigned getSimdDefaultAlign() const { return SimdDefaultAlign; } + /// Return the alignment (in bits) of the thrown exception object. This is + /// only meaningful for targets that allocate C++ exceptions in a system + /// runtime, such as those using the Itanium C++ ABI. + virtual unsigned getExnObjectAlignment() const { + // Itanium says that an _Unwind_Exception has to be "double-word" + // aligned (and thus the end of it is also so-aligned), meaning 16 + // bytes. Of course, that was written for the actual Itanium, + // which is a 64-bit platform. Classically, the ABI doesn't really + // specify the alignment on other platforms, but in practice + // libUnwind declares the struct with __attribute__((aligned)), so + // we assume that alignment here. (It's generally 16 bytes, but + // some targets overwrite it.) + return getDefaultAlignForAttributeAligned(); + } + /// Return the size of intmax_t and uintmax_t for this target, in bits. unsigned getIntMaxTWidth() const { return getTypeWidth(IntMaxType); Index: lib/Basic/Targets/OSTargets.h =================================================================== --- lib/Basic/Targets/OSTargets.h +++ lib/Basic/Targets/OSTargets.h @@ -13,6 +13,7 @@ #define LLVM_CLANG_LIB_BASIC_TARGETS_OSTARGETS_H #include "Targets.h" +#include "clang/Basic/AlignedExceptionObject.h" #include "llvm/MC/MCSectionMachO.h" namespace clang { @@ -133,6 +134,19 @@ /// attribute on declarations that can be dynamically replaced. bool hasProtectedVisibility() const override { return false; } + unsigned getExnObjectAlignment() const override { + // Older versions of libc++abi guarantee an alignment of only 8-bytes for + // exception objects because of a bug in __cxa_exception that was + // eventually fixed in r319123. + const llvm::Triple &T = this->getTriple(); + llvm::VersionTuple MinVT = alignedExceptionObjectMinVersion(T.getOS()); + unsigned Major, Minor, Micro; + T.getOSVersion(Major, Minor, Micro); + if (llvm::VersionTuple(Major, Minor, Micro) < MinVT) + return 64; + return OSTargetInfo::getExnObjectAlignment(); + } + TargetInfo::IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final { // Darwin uses `long long` for `int_least64_t` and `int_fast64_t`. Index: lib/CodeGen/ItaniumCXXABI.cpp =================================================================== --- lib/CodeGen/ItaniumCXXABI.cpp +++ lib/CodeGen/ItaniumCXXABI.cpp @@ -154,17 +154,9 @@ Address Ptr, QualType ElementType, const CXXDestructorDecl *Dtor) override; - /// Itanium says that an _Unwind_Exception has to be "double-word" - /// aligned (and thus the end of it is also so-aligned), meaning 16 - /// bytes. Of course, that was written for the actual Itanium, - /// which is a 64-bit platform. Classically, the ABI doesn't really - /// specify the alignment on other platforms, but in practice - /// libUnwind declares the struct with __attribute__((aligned)), so - /// we assume that alignment here. (It's generally 16 bytes, but - /// some targets overwrite it.) CharUnits getAlignmentOfExnObject() { - auto align = CGM.getContext().getTargetDefaultAlignForAttributeAligned(); - return CGM.getContext().toCharUnitsFromBits(align); + unsigned Align = CGM.getContext().getTargetInfo().getExnObjectAlignment(); + return CGM.getContext().toCharUnitsFromBits(Align); } void emitRethrow(CodeGenFunction &CGF, bool isNoReturn) override; Index: lib/Sema/SemaExprCXX.cpp =================================================================== --- lib/Sema/SemaExprCXX.cpp +++ lib/Sema/SemaExprCXX.cpp @@ -941,6 +941,20 @@ } } + // Issue a warning if the type of the thrown object requires an alignment that + // is larger than the minimum alignment the libc++abi runtime guarantees. + if (Context.getTargetInfo().getTriple().isOSDarwin()) { + CharUnits TypeAlign = Context.getTypeAlignInChars(Ty); + CharUnits ExnObjAlign = Context.toCharUnitsFromBits( + Context.getTargetInfo().getExnObjectAlignment()); + if (ExnObjAlign < TypeAlignn) { + Diag(ThrowLoc, diag::warn_throw_underaligned_obj); + Diag(ThrowLoc, diag::note_throw_underaligned_obj) + << Ty << (unsigned)TypeAlign.getQuantity() + << (unsigned)ExnObjAlign.getQuantity(); + } + } + return false; } Index: test/CodeGenCXX/eh.cpp =================================================================== --- test/CodeGenCXX/eh.cpp +++ test/CodeGenCXX/eh.cpp @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -triple x86_64-apple-darwin -std=c++11 -emit-llvm %s -o - \ -// RUN: | FileCheck %s +// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -triple x86_64-apple-macosx10.13.99 -std=c++11 -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=UNALIGNED %s +// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -triple x86_64-apple-macosx10.14 -std=c++11 -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=ALIGNED %s struct test1_D { double d; @@ -13,7 +13,8 @@ // CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8) // CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]] // CHECK-NEXT: [[EXN2:%.*]] = bitcast [[DSTAR]] [[EXN]] to i8* -// CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 [[EXN2]], i8* align 8 bitcast ([[DSTAR]] @d1 to i8*), i64 8, i1 false) +// UNALIGNED-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 [[EXN2]], i8* align 8 bitcast ([[DSTAR]] @d1 to i8*), i64 8, i1 false) +// ALIGNED-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 [[EXN2]], i8* align 8 bitcast ([[DSTAR]] @d1 to i8*), i64 8, i1 false) // CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({ i8*, i8* }* @_ZTI7test1_D to i8*), i8* null) [[NR:#[0-9]+]] // CHECK-NEXT: unreachable @@ -466,7 +467,8 @@ // CHECK: [[T0:%.*]] = call i8* @__cxa_allocate_exception(i64 16) // CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to %"class.test17::DerivedException"* // CHECK-NEXT: [[T2:%.*]] = bitcast %"class.test17::DerivedException"* [[T1]] to i8* - // CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 16 [[T2]], i8 0, i64 16, i1 false) + // UNALIGNED-NEXT: call void @llvm.memset.p0i8.i64(i8* align 8 [[T2]], i8 0, i64 16, i1 false) + // ALIGNED-NEXT: call void @llvm.memset.p0i8.i64(i8* align 16 [[T2]], i8 0, i64 16, i1 false) } } Index: test/SemaCXX/warn-overaligned-type-thrown.cpp =================================================================== --- /dev/null +++ test/SemaCXX/warn-overaligned-type-thrown.cpp @@ -0,0 +1,53 @@ +// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13.99 -verify -fsyntax-only -std=c++11 -fcxx-exceptions -fexceptions -DUNDERALIGNED %s +// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14 -verify -fsyntax-only -std=c++11 -fcxx-exceptions -fexceptions %s +// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14 -verify -fsyntax-only -std=c++11 -fcxx-exceptions -fexceptions -Wno-underaligned-exception-object -DNODIAG %s + +struct S0 { + S0(); + int m; +}; + +struct Overaligned1 { + Overaligned1(); + int __attribute__((aligned(16))) m; +}; + +struct __attribute__((aligned(16))) Overaligned2 { + Overaligned2(); + int m; +}; + +struct Overaligned3 { + Overaligned3(); + int __attribute__((aligned(64))) m; +}; + +void test0() { + throw S0(); +} + +void test1() { + throw Overaligned1(); +} + +void test2() { + throw Overaligned2(); +} + +void test3() { + throw Overaligned3(); +} + +#if defined(NODIAG) +// expected-no-diagnostics +#elif defined(UNDERALIGNED) +// expected-warning@-14 {{underaligned exception object thrown}} +// expected-note@-15 {{(16-bytes) is larger than the guaranteed alignment of the memory Itanium C++ runtime returns (8-bytes)}} +// expected-warning@-12 {{underaligned exception object thrown}} +// expected-note@-13 {{(16-bytes) is larger than the guaranteed alignment of the memory Itanium C++ runtime returns (8-bytes)}} +// expected-warning@-10 {{underaligned exception object thrown}} +// expected-note@-11 {{(64-bytes) is larger than the guaranteed alignment of the memory Itanium C++ runtime returns (8-bytes)}} +#else +// expected-warning@-13 {{underaligned exception object thrown}} +// expected-note@-14 {{(64-bytes) is larger than the guaranteed alignment of the memory Itanium C++ runtime returns (16-bytes)}} +#endif