1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:37:36 +00:00

Everywhere: Fix some alignment issues

When creating uninitialized storage for variables, we need to make sure
that the alignment is correct. Fixes a KUBSAN failure when running
kernels compiled with Clang.

In `Syscalls/socket.cpp`, we can simply use local variables, as
`sockaddr_un` is a POD type.

Along with moving the `alignas` specifier to the correct member,
`AK::Optional`'s internal buffer has been made non-zeroed by default.
GCC emitted bogus uninitialized memory access warnings, so we now use
`__builtin_launder` to tell the compiler that we know what we are doing.
This might disable some optimizations, but judging by how GCC failed to
notice that the memory's initialization is dependent on `m_has_value`,
I'm not sure that's a bad thing.
This commit is contained in:
Daniel Bertalan 2021-07-01 11:29:28 +02:00 committed by Ali Mohammad Pur
parent 45a82b2a5b
commit b9f30c6f2a
5 changed files with 17 additions and 17 deletions

View file

@ -14,7 +14,7 @@
namespace AK {
template<typename T>
class alignas(T) [[nodiscard]] Optional {
class [[nodiscard]] Optional {
public:
using ValueType = T;
@ -132,13 +132,13 @@ public:
[[nodiscard]] ALWAYS_INLINE T& value()
{
VERIFY(m_has_value);
return *reinterpret_cast<T*>(&m_storage);
return *__builtin_launder(reinterpret_cast<T*>(&m_storage));
}
[[nodiscard]] ALWAYS_INLINE const T& value() const
{
VERIFY(m_has_value);
return *reinterpret_cast<const T*>(&m_storage);
return *__builtin_launder(reinterpret_cast<const T*>(&m_storage));
}
[[nodiscard]] T release_value()
@ -164,7 +164,7 @@ public:
ALWAYS_INLINE T* operator->() { return &value(); }
private:
u8 m_storage[sizeof(T)] { 0 };
alignas(T) u8 m_storage[sizeof(T)];
bool m_has_value { false };
};