From 3fd05306fc0361df5fec1566c24f4ca10df6cd5e Mon Sep 17 00:00:00 2001 From: ayeteadoe Date: Mon, 16 Jun 2025 15:08:50 -0600 Subject: [PATCH] AK: Add abstraction for forcing empty base optimization on Windows Without this annotation, the MSVC ABI is reluctant to apply EBO even in cases that are basically guaranteed on the Itanium ABI by modern compilers. This fixes an UBSAN issue with Variant on Windows. --- AK/Platform.h | 7 +++++++ AK/Tuple.h | 4 ++-- AK/Variant.h | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/AK/Platform.h b/AK/Platform.h index f8fcaa3d6cb..e0e08b4331c 100644 --- a/AK/Platform.h +++ b/AK/Platform.h @@ -249,6 +249,13 @@ # define NO_UNIQUE_ADDRESS [[no_unique_address]] #endif +#if defined(AK_OS_WINDOWS) +// https://learn.microsoft.com/en-us/cpp/cpp/empty-bases?view=msvc-170 +# define AK_COMPACT_EMPTY_BASES __declspec(empty_bases) +#else +# define AK_COMPACT_EMPTY_BASES +#endif + // GCC doesn't have __has_feature but clang does #ifndef __has_feature # define __has_feature(...) 0 diff --git a/AK/Tuple.h b/AK/Tuple.h index df8a3d26fc4..2ae3c92d724 100644 --- a/AK/Tuple.h +++ b/AK/Tuple.h @@ -59,7 +59,7 @@ private: }; template -struct Tuple : Tuple { +struct AK_COMPACT_EMPTY_BASES Tuple : Tuple { template Tuple(FirstT&& first, RestT&&... rest) @@ -113,7 +113,7 @@ private: namespace AK { template -struct Tuple : Detail::Tuple { +struct AK_COMPACT_EMPTY_BASES Tuple : Detail::Tuple { using Types = TypeList; using Detail::Tuple::Tuple; using Indices = MakeIndexSequence; diff --git a/AK/Variant.h b/AK/Variant.h index a04a1daf902..a03d57c112f 100644 --- a/AK/Variant.h +++ b/AK/Variant.h @@ -478,7 +478,7 @@ private: } template - struct Visitor : Fs... { + struct AK_COMPACT_EMPTY_BASES Visitor : Fs... { using Types = TypeList; Visitor(Fs&&... args)