From 333fed96b7ccd3ee4f5f097445408dde34d330db Mon Sep 17 00:00:00 2001 From: amlal Date: Thu, 21 Mar 2024 00:38:52 +0100 Subject: Kernel: See below. - Fix kernel heap, made it better. - Fix System.Core, made it better. Signed-off-by: amlal --- Private/Source/KernelCheck.cxx | 4 ++-- Private/Source/KernelHeap.cxx | 49 ++++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 25 deletions(-) (limited to 'Private/Source') diff --git a/Private/Source/KernelCheck.cxx b/Private/Source/KernelCheck.cxx index 30c76050..0fb81d47 100644 --- a/Private/Source/KernelCheck.cxx +++ b/Private/Source/KernelCheck.cxx @@ -32,12 +32,12 @@ void ke_stop(const HCore::Int &id) { } case RUNTIME_CHECK_ACPI: { kcout << "*** CAUSE: RUNTIME_CHECK_ACPI *** \r\n"; - kcout << "*** WHAT: ACPI DEFECT. *** \r\n"; + kcout << "*** WHAT: ACPI ERROR, UNSTABLE STATE. *** \r\n"; break; } case RUNTIME_CHECK_POINTER: { kcout << "*** CAUSE: RUNTIME_CHECK_POINTER *** \r\n"; - kcout << "*** WHAT: BAD POINTER. *** \r\n"; + kcout << "*** WHAT: HEAP ERROR, UNSTABLE STATE. *** \r\n"; break; } case RUNTIME_CHECK_BAD_BEHAVIOR: { diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index 438df005..f5cb01da 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -6,8 +6,9 @@ #include #include -#include #include +#include +#include //! @file KernelHeap.cxx //! @brief Kernel allocator. @@ -16,14 +17,13 @@ namespace HCore { STATIC SizeT kHeapCount = 0UL; -STATIC Ref kHeapLastWrapper; STATIC PageManager kHeapPageManager; namespace Detail { /// @brief Kernel heap information block. /// Located before the address bytes. /// | HIB | ADDRESS | -struct HeapInformationBlock final { +struct PACKED HeapInformationBlock final { UInt16 hMagic; Boolean hPresent; Int32 hCRC32; @@ -43,9 +43,6 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { if (sz == 0) ++sz; auto wrapper = kHeapPageManager.Request(rw, user, false); - kHeapLastWrapper = wrapper; - - kcout << "HCoreKrnl.exe: Populating HIB...\r\n"; Detail::HeapInformationBlockPtr heapInfo = reinterpret_cast( @@ -58,8 +55,6 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { ++kHeapCount; - kcout << "HCoreKrnl.exe: Return address...\r\n"; - return reinterpret_cast(wrapper.VirtualAddress() + sizeof(Detail::HeapInformationBlock)); } @@ -68,23 +63,31 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { /// @param ptr the pointer. /// @return Int32 ke_delete_ke_heap(VoidPtr ptr) { - if (ptr) { - Detail::HeapInformationBlockPtr virtualAddress = - reinterpret_cast(ptr) - - sizeof(Detail::HeapInformationBlock); - - if (kHeapLastWrapper && virtualAddress->hMagic == kHeapMagic && - virtualAddress->hAddress == kHeapLastWrapper.Leak().VirtualAddress()) { - virtualAddress->hSizeAddress = 0UL; - virtualAddress->hPresent = false; + if (kHeapCount < 1) return -kErrorInternal; + + Detail::HeapInformationBlockPtr virtualAddress = + reinterpret_cast(ptr - + sizeof(Detail::HeapInformationBlock)); + + if (virtualAddress && virtualAddress->hMagic == kHeapMagic) { + if (virtualAddress->hCRC32 != 0) { + if (virtualAddress->hCRC32 != + ke_calculate_crc32((Char *)virtualAddress->hAddress, + virtualAddress->hSizeAddress)) + ke_stop(RUNTIME_CHECK_POINTER); + } - --kHeapCount; + virtualAddress->hSizeAddress = 0UL; + virtualAddress->hPresent = false; + virtualAddress->hAddress = 0; + virtualAddress->hCRC32 = 0; + virtualAddress->hMagic = 0; - return true; - } + --kHeapCount; + return 0; } - return -1; + return -kErrorInternal; } /// @brief Check if pointer is a valid kernel pointer. @@ -95,8 +98,8 @@ Boolean ke_is_valid_ptr(VoidPtr ptr) { if (ptr) { Detail::HeapInformationBlockPtr virtualAddress = - reinterpret_cast(ptr) - - sizeof(Detail::HeapInformationBlock); + reinterpret_cast(ptr - + sizeof(Detail::HeapInformationBlock)); if (virtualAddress->hPresent && virtualAddress->hMagic == kHeapMagic) { return true; -- cgit v1.2.3