From 191a9459af2c4ded4e8e824f530870bb724e93d2 Mon Sep 17 00:00:00 2001 From: Ivan Chikish Date: Sat, 15 Jul 2023 19:05:14 +0300 Subject: [PATCH] [orbis-kernel] Improve kdelete safety --- orbis-kernel/include/orbis/KernelAllocator.hpp | 9 ++++++++- orbis-kernel/include/orbis/evf.hpp | 2 +- orbis-kernel/include/orbis/file.hpp | 2 +- orbis-kernel/include/orbis/thread/Process.hpp | 2 +- orbis-kernel/include/orbis/utils/LinkedNode.hpp | 2 +- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/orbis-kernel/include/orbis/KernelAllocator.hpp b/orbis-kernel/include/orbis/KernelAllocator.hpp index edf2aa9a..bc6a641c 100644 --- a/orbis-kernel/include/orbis/KernelAllocator.hpp +++ b/orbis-kernel/include/orbis/KernelAllocator.hpp @@ -54,9 +54,16 @@ template T *knew(Args &&...args) { return res; } +// clang-format off template void kdelete(T *ptr) { + auto total_size = sizeof(T); + if constexpr (requires(T *t) { t->_total_size = sizeof(T); }) + total_size = ptr->_total_size; + else + static_assert(std::is_final_v, "Uncertain type size"); ptr->~T(); - utils::kfree(ptr, sizeof(T)); + utils::kfree(ptr, total_size); } +// clang-format on } // namespace orbis diff --git a/orbis-kernel/include/orbis/evf.hpp b/orbis-kernel/include/orbis/evf.hpp index f128b5aa..5f88c124 100644 --- a/orbis-kernel/include/orbis/evf.hpp +++ b/orbis-kernel/include/orbis/evf.hpp @@ -21,7 +21,7 @@ enum { kEvfWaitModeClearPat = 0x20, }; -struct EventFlag { +struct EventFlag final { char name[32]; bool isDeleted = false; diff --git a/orbis-kernel/include/orbis/file.hpp b/orbis-kernel/include/orbis/file.hpp index 98a0780a..b58cf042 100644 --- a/orbis-kernel/include/orbis/file.hpp +++ b/orbis-kernel/include/orbis/file.hpp @@ -40,7 +40,7 @@ struct FileOps { Thread *thread) = nullptr; }; -struct File { +struct File final { FileOps *ops; Ref device; std::atomic refs{0}; diff --git a/orbis-kernel/include/orbis/thread/Process.hpp b/orbis-kernel/include/orbis/thread/Process.hpp index 4882734f..e3947b36 100644 --- a/orbis-kernel/include/orbis/thread/Process.hpp +++ b/orbis-kernel/include/orbis/thread/Process.hpp @@ -22,7 +22,7 @@ struct NamedObjInfo { uint16_t ty; }; -struct Process { +struct Process final { KernelContext *context = nullptr; pid_t pid = -1; sysentvec *sysent = nullptr; diff --git a/orbis-kernel/include/orbis/utils/LinkedNode.hpp b/orbis-kernel/include/orbis/utils/LinkedNode.hpp index 84f55174..f2c005f4 100644 --- a/orbis-kernel/include/orbis/utils/LinkedNode.hpp +++ b/orbis-kernel/include/orbis/utils/LinkedNode.hpp @@ -2,7 +2,7 @@ namespace orbis { inline namespace utils { -template struct LinkedNode { +template struct LinkedNode final { T object; LinkedNode *next = nullptr; LinkedNode *prev = nullptr;