aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/kapi
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-09-03 16:47:00 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-09-03 16:47:00 +0200
commit5668a7c45f23820862e26fe018b69df46cf5ebe8 (patch)
treee91230d6e9582d7c1c8735dad1a2c558a8f9e7a9 /arch/x86_64/kapi
parent60ad0c9a8da2cf265b1825cb555cebe9c96de069 (diff)
downloadkernel-5668a7c45f23820862e26fe018b69df46cf5ebe8.tar.xz
kernel-5668a7c45f23820862e26fe018b69df46cf5ebe8.zip
fs, devices: fix undefined behavior in registration
Diffstat (limited to 'arch/x86_64/kapi')
-rw-r--r--arch/x86_64/kapi/devices.cpp17
1 files changed, 13 insertions, 4 deletions
diff --git a/arch/x86_64/kapi/devices.cpp b/arch/x86_64/kapi/devices.cpp
index 39dbc010..b2e9cba5 100644
--- a/arch/x86_64/kapi/devices.cpp
+++ b/arch/x86_64/kapi/devices.cpp
@@ -9,6 +9,7 @@
#include <kstd/memory.hpp>
#include <kstd/print.hpp>
+#include <new>
#include <ranges>
#include <span>
#include <utility>
@@ -24,16 +25,24 @@ namespace kapi::devices
extern "C"
{
// We need to suppress clang-tidy linting warnings here, since these symbols are generated by the linker and we
- // cannot choose their names, unless we wanted to extend the linker script needlessly.
+ // cannot choose their names, unless we want to write a bht linker script for no reason. Additionally, these symbols
+ // need to be arrays, because otherwise any pointer arithmetic on them, beyond trivial cases of adding 0 or 1, is
+ // undefined behavior by the rules of the C++ abstract machine. To ensure the compiler does not try to perform
+ // constant folding, the pointers, these arrays of unknown bound decay to, need to be laundered before dereferencing
+ // them.
+
// NOLINTBEGIN(readability-identifier-naming)
- extern kstd::observer_ptr<kapi::devices::driver_descriptor> const __start_platform_drivers;
- extern kstd::observer_ptr<kapi::devices::driver_descriptor> const __stop_platform_drivers;
+ extern kstd::observer_ptr<kapi::devices::driver_descriptor> const __start_platform_drivers[];
+ extern kstd::observer_ptr<kapi::devices::driver_descriptor> const __stop_platform_drivers[];
// NOLINTEND(readability-identifier-naming)
}
auto init_platform_drivers() -> void
{
- auto descriptors = std::span{&__start_platform_drivers, &__stop_platform_drivers} | //
+ auto clean_start = std::launder(__start_platform_drivers);
+ auto clean_stop = std::launder(__stop_platform_drivers);
+
+ auto descriptors = std::span{clean_start, clean_stop} | //
std::views::filter([](auto p) { return p != nullptr; });
for (auto driver : descriptors)