From 82c31046edfa7548702c16cfc051f19f985566ca Mon Sep 17 00:00:00 2001 From: leveldb Team Date: Wed, 8 Jan 2025 18:54:39 +0000 Subject: [PATCH 1/2] Fix C++23 compilation errors in leveldb Remove usages of std::aligned_storage, which is deprecated. More details about the replacement in https://crbug.com/388068052. PiperOrigin-RevId: 713346733 --- util/env_posix.cc | 9 ++++++--- util/env_windows.cc | 9 ++++++--- util/no_destructor.h | 10 +++++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/util/env_posix.cc b/util/env_posix.cc index fac41be6ce..ca70ff67f2 100644 --- a/util/env_posix.cc +++ b/util/env_posix.cc @@ -854,7 +854,11 @@ class SingletonEnv { #endif // !defined(NDEBUG) static_assert(sizeof(env_storage_) >= sizeof(EnvType), "env_storage_ will not fit the Env"); - static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType), + static_assert(std::is_standard_layout_v>); + static_assert( + offsetof(SingletonEnv, env_storage_) % alignof(EnvType) == 0, + "env_storage_ does not meet the Env's alignment needs"); + static_assert(alignof(SingletonEnv) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); new (&env_storage_) EnvType(); } @@ -872,8 +876,7 @@ class SingletonEnv { } private: - typename std::aligned_storage::type - env_storage_; + alignas(EnvType) char env_storage_[sizeof(EnvType)]; #if !defined(NDEBUG) static std::atomic env_initialized_; #endif // !defined(NDEBUG) diff --git a/util/env_windows.cc b/util/env_windows.cc index aafcdcc3be..2137a9f154 100644 --- a/util/env_windows.cc +++ b/util/env_windows.cc @@ -802,7 +802,11 @@ class SingletonEnv { #endif // !defined(NDEBUG) static_assert(sizeof(env_storage_) >= sizeof(EnvType), "env_storage_ will not fit the Env"); - static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType), + static_assert(std::is_standard_layout_v>); + static_assert( + offsetof(SingletonEnv, env_storage_) % alignof(EnvType) == 0, + "env_storage_ does not meet the Env's alignment needs"); + static_assert(alignof(SingletonEnv) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); new (&env_storage_) EnvType(); } @@ -820,8 +824,7 @@ class SingletonEnv { } private: - typename std::aligned_storage::type - env_storage_; + alignas(EnvType) char env_storage_[sizeof(EnvType)]; #if !defined(NDEBUG) static std::atomic env_initialized_; #endif // !defined(NDEBUG) diff --git a/util/no_destructor.h b/util/no_destructor.h index a0d3b8703d..08ce6a40a9 100644 --- a/util/no_destructor.h +++ b/util/no_destructor.h @@ -5,6 +5,7 @@ #ifndef STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ #define STORAGE_LEVELDB_UTIL_NO_DESTRUCTOR_H_ +#include #include #include @@ -20,8 +21,12 @@ class NoDestructor { explicit NoDestructor(ConstructorArgTypes&&... constructor_args) { static_assert(sizeof(instance_storage_) >= sizeof(InstanceType), "instance_storage_ is not large enough to hold the instance"); + static_assert(std::is_standard_layout_v>); static_assert( - alignof(decltype(instance_storage_)) >= alignof(InstanceType), + offsetof(NoDestructor, instance_storage_) % alignof(InstanceType) == 0, + "instance_storage_ does not meet the instance's alignment requirement"); + static_assert( + alignof(NoDestructor) % alignof(InstanceType) == 0, "instance_storage_ does not meet the instance's alignment requirement"); new (&instance_storage_) InstanceType(std::forward(constructor_args)...); @@ -37,8 +42,7 @@ class NoDestructor { } private: - typename std::aligned_storage::type instance_storage_; + alignas(InstanceType) char instance_storage_[sizeof(InstanceType)]; }; } // namespace leveldb From 183e79a495d4e19c539cf1d64498106972c0ee6b Mon Sep 17 00:00:00 2001 From: leveldb Team Date: Tue, 21 Jan 2025 16:19:03 +0000 Subject: [PATCH 2/2] Fix speculatively some "placement new" issues in leveldb cl/713346733 changed the type of some variables to pointers, but didn't adjust the placement new statements. From pkasting@: "I suspect your code is wrong and will crash. An array is a pointer, so taking its address also gives a pointer, which is why it compiles; but the value of that pointer is different. You're no longer providing the address of the storage, but rather the address of the array pointer." PiperOrigin-RevId: 717926210 --- util/env_posix.cc | 2 +- util/env_windows.cc | 2 +- util/no_destructor.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/util/env_posix.cc b/util/env_posix.cc index ca70ff67f2..86571059ba 100644 --- a/util/env_posix.cc +++ b/util/env_posix.cc @@ -860,7 +860,7 @@ class SingletonEnv { "env_storage_ does not meet the Env's alignment needs"); static_assert(alignof(SingletonEnv) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); - new (&env_storage_) EnvType(); + new (env_storage_) EnvType(); } ~SingletonEnv() = default; diff --git a/util/env_windows.cc b/util/env_windows.cc index 2137a9f154..0a48c3fb52 100644 --- a/util/env_windows.cc +++ b/util/env_windows.cc @@ -808,7 +808,7 @@ class SingletonEnv { "env_storage_ does not meet the Env's alignment needs"); static_assert(alignof(SingletonEnv) % alignof(EnvType) == 0, "env_storage_ does not meet the Env's alignment needs"); - new (&env_storage_) EnvType(); + new (env_storage_) EnvType(); } ~SingletonEnv() = default; diff --git a/util/no_destructor.h b/util/no_destructor.h index 08ce6a40a9..c28a107313 100644 --- a/util/no_destructor.h +++ b/util/no_destructor.h @@ -28,7 +28,7 @@ class NoDestructor { static_assert( alignof(NoDestructor) % alignof(InstanceType) == 0, "instance_storage_ does not meet the instance's alignment requirement"); - new (&instance_storage_) + new (instance_storage_) InstanceType(std::forward(constructor_args)...); }