From 3033b78abc6ff3dad05994fa51bcd92bf3dae542 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 30 Apr 2024 11:58:58 +0200 Subject: AppMain.cxx: Add canary file, which prevents data corruption. TODO: this file must be protected. Signed-off-by: Amlal El Mahrouss --- Private/Source/AppMain.cxx | 46 +++++++++++++++---------------------- Private/Source/FS/NewFS.cxx | 16 +++++++------ Private/Source/ProcessScheduler.cxx | 12 +++++++--- 3 files changed, 36 insertions(+), 38 deletions(-) (limited to 'Private/Source') diff --git a/Private/Source/AppMain.cxx b/Private/Source/AppMain.cxx index d9c02843..ac1cb1ac 100644 --- a/Private/Source/AppMain.cxx +++ b/Private/Source/AppMain.cxx @@ -16,6 +16,7 @@ #include #include #include +#include /// @file Main microkernel entrypoint. @@ -25,51 +26,40 @@ EXTERN_C NewOS::Void AppMain(NewOS::Void) { NewOS::FilesystemManagerInterface::Mount(newFS); + constexpr auto cDataSz = 512; + NewOS::UInt8 theData[cDataSz] = { "\x48\xC7\xC0\x00\x00\x00\x00\xC3\xC1" }; + if (newFS->GetImpl()) { NewCatalog* textCatalog = nullptr; - if (!newFS->GetImpl()->GetCatalog("/EditableText")) { - constexpr auto cDataSz = 512; - NewOS::Char theData[cDataSz] = { - "About NewKernel...\rNewKernel is the System behind " - "NewOS.\rFeaturing modern common features, yet innovative.\r"}; + if (!newFS->GetImpl()->GetCatalog("/System/.NEWFS_SANITIZER")) { NewFork theFork{0}; - NewOS::rt_copy_memory((NewOS::VoidPtr) "EditableText", + NewOS::rt_copy_memory((NewOS::VoidPtr) "RawExecutable", (NewOS::VoidPtr)theFork.Name, - NewOS::rt_string_len("EditableText")); + NewOS::rt_string_len("RawExecutable")); theFork.Kind = NewOS::kNewFSDataForkKind; theFork.DataSize = kNewFSForkSize; - textCatalog = newFS->GetImpl()->CreateCatalog("/EditableText"); + newFS->GetImpl()->CreateCatalog("/System/", 0, kNewFSCatalogKindDir); + textCatalog = newFS->GetImpl()->CreateCatalog("/System/.NEWFS_SANITIZER"); newFS->GetImpl()->CreateFork(textCatalog, theFork); - newFS->GetImpl()->WriteCatalog(textCatalog, theData, cDataSz, "EditableText"); - - NewFork theForkPreview{0}; - - theForkPreview.Kind = NewOS::kNewFSDataForkKind; - theForkPreview.DataSize = kNewFSForkSize; - - NewOS::rt_copy_memory((NewOS::VoidPtr) "EditableTextPreview", - (NewOS::VoidPtr)theForkPreview.Name, - NewOS::rt_string_len("EditableTextPreview")); - - NewOS::Char theDataPreview[cDataSz] = { "NewKernel Info:\r\tNewKernel!" }; - newFS->GetImpl()->CreateFork(textCatalog, theForkPreview); - - newFS->GetImpl()->WriteCatalog(textCatalog, theDataPreview, cDataSz, "EditableTextPreview"); - } else { - NewOS::kcout << "Catalog already exists.\r"; + newFS->GetImpl()->WriteCatalog(textCatalog, theData, cDataSz, "RawExecutable"); } - char* buf = nullptr; + NewOS::UInt8* buf = nullptr; - buf = (NewOS::Char*)newFS->GetImpl()->ReadCatalog(newFS->GetImpl()->GetCatalog("/EditableText"), 512, "EditableTextPreview"); + buf = (NewOS::UInt8*)newFS->GetImpl()->ReadCatalog(newFS->GetImpl()->GetCatalog("/System/.NEWFS_SANITIZER"), 512, "RawExecutable"); - NewOS::kcout << buf << NewOS::endl; + for (NewOS::SizeT index = 0UL; index < cDataSz; ++index) { + if (buf[index] != theData[index]) { + NewOS::kcout << "Diff-Detected: " << NewOS::hex_number(buf[index]) << NewOS::endl; + NewOS::ke_stop(RUNTIME_CHECK_BAD_BEHAVIOR); + } + } } while (NewOS::ProcessScheduler::Shared().Leak().Run() > 0) diff --git a/Private/Source/FS/NewFS.cxx b/Private/Source/FS/NewFS.cxx index c9df501d..0057af20 100644 --- a/Private/Source/FS/NewFS.cxx +++ b/Private/Source/FS/NewFS.cxx @@ -66,15 +66,16 @@ _Output NewFork* NewFSParser::CreateFork(_Input NewCatalog* catalog, prevFork = cpyFork; } else { - break; - } - } + /// This is a check that we have, in order to link the previous fork entry. + if (lba >= kNewFSCatalogStartAddress) { + prevFork.NextSibling = lba; + /// write to disk. + drv->fOutput(&drv->fPacket); + } - if (lba >= kNewFSCatalogStartAddress) { - prevFork.NextSibling = lba; - - drv->fOutput(&drv->fPacket); + break; + } } constexpr auto cForkPadding = 4; @@ -90,6 +91,7 @@ _Output NewFork* NewFSParser::CreateFork(_Input NewCatalog* catalog, drv->fOutput(&drv->fPacket); + /// log what we have now. kcout << "New OS: Wrote fork data at: " << hex_number(theFork.DataOffset) << endl; kcout << "New OS: Wrote fork at: " << hex_number(lba) << endl; diff --git a/Private/Source/ProcessScheduler.cxx b/Private/Source/ProcessScheduler.cxx index 3cd5f885..f2d12e64 100644 --- a/Private/Source/ProcessScheduler.cxx +++ b/Private/Source/ProcessScheduler.cxx @@ -13,6 +13,7 @@ #include #include #include +#include ///! bugs = 0 @@ -48,7 +49,12 @@ void ProcessHeader::Wake(const bool should_wakeup) { /***********************************************************************************/ VoidPtr ProcessHeader::New(const SizeT &sz) { - if (this->FreeMemory < 1) return nullptr; + if (this->FreeMemory < 1) { + DbgLastError() = kErrorHeapOutOfMemory; + this->Crash(); /// out of memory. + + return nullptr; + } if (this->HeapCursor) { VoidPtr ptr = this->HeapCursor; @@ -66,7 +72,7 @@ VoidPtr ProcessHeader::New(const SizeT &sz) { /***********************************************************************************/ /* @brief checks if runtime pointer is in region. */ -bool rt_in_pool_region(VoidPtr pool_ptr, VoidPtr pool, const SizeT &sz) { +bool rt_is_in_pool(VoidPtr pool_ptr, VoidPtr pool, const SizeT &sz) { UIntPtr *_pool_ptr = (UIntPtr *)pool_ptr; UIntPtr *_pool = (UIntPtr *)pool; @@ -86,7 +92,7 @@ Boolean ProcessHeader::Delete(VoidPtr ptr, const SizeT &sz) { // also check for the amount of allocations we've done so far. if (this->UsedMemory < 1) return false; - if (rt_in_pool_region(ptr, this->HeapCursor, this->UsedMemory)) { + if (rt_is_in_pool(ptr, this->HeapCursor, this->UsedMemory)) { this->HeapCursor = (VoidPtr)((UIntPtr)this->HeapCursor - (sizeof(sz))); rt_zero_memory(ptr, sz); -- cgit v1.2.3