Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deacresed Heap Crashes (Reopen PR) #2531

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
17a6d57
Fix Heap and Other bugs
TheFocusMan Nov 7, 2022
77f8e83
Merge branch 'master' of https://github.com/TheFocusMan/Cosmos
TheFocusMan Nov 7, 2022
584acf6
Merge branch 'CosmosOS:master' into master
TheFocusMan Nov 24, 2022
b7ecf8d
Fix Native Memory Blocking compiler
TheFocusMan Nov 24, 2022
274b052
Fix Heap Crashes
TheFocusMan Nov 24, 2022
e78024a
Merge branch 'master' into master
MishaTy Nov 25, 2022
d033292
Heap Large Free check
TheFocusMan Nov 27, 2022
5ac63ed
Fix Heap Bug
TheFocusMan Nov 27, 2022
0ad103f
Merge branch 'master' into master
TheFocusMan Nov 27, 2022
c73fe33
Merge branch 'master' into master
TheFocusMan Nov 28, 2022
212e2e1
Documenting missing
TheFocusMan Nov 28, 2022
a431357
Merge branch 'master' into master
TheFocusMan Nov 29, 2022
e8a630e
Made The debugger more friendly by adding assert and option to ignore…
TheFocusMan Nov 29, 2022
e7f8624
Merge branch 'master' of https://github.com/TheFocusMan/Cosmos
TheFocusMan Nov 29, 2022
78048cd
Fix syntax
TheFocusMan Nov 29, 2022
9e0c07b
Merge branch 'master' into master
valentinbreiz Dec 15, 2022
bf7c84f
Merge branch 'master' into master
quajak Dec 16, 2022
4385190
Merge branch 'master' into master
TheFocusMan Dec 20, 2022
4e68f2b
VMware host upgrade
TheFocusMan Dec 21, 2022
361bfdb
Restore Changes
TheFocusMan Dec 21, 2022
98b5f11
Merge branch 'master' of https://github.com/TheFocusMan/Cosmos
TheFocusMan Dec 21, 2022
17769bc
Merge branch 'master' into master
TheFocusMan Dec 29, 2022
06d0c3b
Merge branch 'master' into master
TheFocusMan Dec 30, 2022
649a78b
Merge branch 'CosmosOS:master' into master
TheFocusMan Jan 2, 2023
ff91d46
Fix File Copy Function
TheFocusMan Jan 4, 2023
749e9ba
Merge branch 'master' of https://github.com/TheFocusMan/Cosmos
TheFocusMan Jan 4, 2023
d933d21
Fix
TheFocusMan Jan 4, 2023
3ec07a9
Merge branch 'CosmosOS:master' into master
TheFocusMan Jan 16, 2023
9d3a8c0
Merge branch 'master' into master
MishaTy Jan 23, 2023
62198c7
Merge branch 'master' into master
TheFocusMan Jan 24, 2023
2cc6ffc
Merge branch 'master' into master
TheFocusMan Jan 30, 2023
5631212
Merge branch 'master' into master
TheFocusMan Feb 8, 2023
8d6c8ba
Merge branch 'master' into master
TheFocusMan Mar 2, 2023
f34025c
Merge branch 'master' into master
TheFocusMan Mar 23, 2023
47988d7
Merge branch 'master' into master
TheFocusMan Mar 26, 2023
ead53bc
Merge branch 'master' into master
TheFocusMan Apr 6, 2023
bd17e98
.
TheFocusMan May 28, 2023
6208e17
.
TheFocusMan May 28, 2023
c1ff1cd
.
TheFocusMan May 28, 2023
fcd940b
.
TheFocusMan May 28, 2023
e36fff9
.
TheFocusMan May 28, 2023
5fda64c
Merge branch 'master' into master
TheFocusMan Jun 5, 2023
bf2c1cd
Merge branch 'master' into master
TheFocusMan Jun 19, 2023
597bf32
.
TheFocusMan Jun 27, 2023
1fb1909
.
TheFocusMan Jun 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Globalization;
using System.Reflection;
using Cosmos.IL2CPU;

namespace Cosmos.Core
{
Expand Down
10 changes: 8 additions & 2 deletions source/Cosmos.Core/Memory/HeapLarge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void Init()
var xPtr = (uint*)RAT.AllocPages(aType, xPages);
if (xPtr == null)
{
Debugger.SendKernelPanic(0x67); // out of pages
while (true) { }
Debugger.DoFail(0x67); // out of pages

}
xPtr[0] = xPages * RAT.PageSize - PrefixBytes; // Allocated data size
xPtr[1] = aSize; // Actual data size
Expand All @@ -52,7 +52,13 @@ public static void Init()
/// <exception cref="Exception">Thrown if page type is not found.</exception>
public static void Free(void* aPtr)
{
var heapObject = (uint*)aPtr;
var xPageIdx = RAT.GetFirstRATIndex(aPtr);
if (heapObject[-4] == 0 && xPageIdx == 0)
{
// The object is not allocated
return;
}
RAT.Free(xPageIdx);
}
}
Expand Down
55 changes: 38 additions & 17 deletions source/Cosmos.Core/Memory/HeapSmall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ private static void AddRootSMTBlock(SMTPage* aPage, uint aSize)
// we cant later add a block with a size smaller than an earlier block. That would break the algorithm
Debugger.DoSendNumber(aSize);
Debugger.DoSendNumber(ptr->Size);
Debugger.SendKernelPanic(0x83);
while (true) { }
Debugger.DoFail(0x83);
}

if (ptr->Size == 0)
Expand Down Expand Up @@ -367,8 +366,7 @@ static void CreatePage(SMTPage* aPage, uint aItemSize)
smtBlock = NextFreeBlock();
if (smtBlock == null)
{
Debugger.SendKernelPanic(0x93);
while (true) { };
Debugger.DoFail(0x93);
}
}

Expand All @@ -389,29 +387,51 @@ static void CreatePage(SMTPage* aPage, uint aItemSize)
smtBlock->PagePtr = xPtr;
}



/// <summary>
/// Get the first block for this size, which has space left to allocate to And get the root block
/// </summary>
/// <param name="aSize">The size</param>
/// <param name="block">the Output block</param>
/// <returns>The parent of the block</returns>
private static RootSMTBlock* GetFirstWithSpaceAndParent(uint aSize, out SMTBlock* block)
{
var page = SMT;
RootSMTBlock* rootblock = null;
do
{
rootblock = GetFirstBlock(page, aSize);
block = GetFirstWithSpace(aSize, rootblock);

page = page->Next;
} while (rootblock == null && page != null);
return rootblock;
}

/// <summary>
/// Alloc memory block, of a given size.
/// </summary>
/// <param name="aSize">A size of block to alloc, in bytes.</param>
/// <returns>Byte pointer to the start of the block.</returns>
public static byte* Alloc(ushort aSize)
{
var pageBlock = GetFirstWithSpace(aSize);
var smtblock = GetFirstWithSpaceAndParent(aSize, out SMTBlock* pageBlock);
if (pageBlock == null) // This happens when the page is full and we need to allocate a new page for this size
{
CreatePage(GetLastPage(), GetRoundedSize(aSize));
CreatePage(GetLastPage(), smtblock->Size);
pageBlock = GetFirstWithSpace(aSize);
if (pageBlock == null)
{
//this means that we cant allocate another page
Debugger.SendKernelPanic(0x121);
Debugger.DoAssert(0x121);
}
}

//now find position in the block
ushort* page = (ushort*)pageBlock->PagePtr;
uint elementSize = GetRoundedSize(aSize) + PrefixItemBytes;
uint positions = RAT.PageSize / elementSize;
var page = (ushort*)pageBlock->PagePtr;
var elementSize = smtblock->Size + PrefixItemBytes;
var positions = RAT.PageSize / elementSize;
for (int i = 0; i < positions; i++)
{
if (page[i * elementSize / 2] == 0)
Expand All @@ -434,8 +454,8 @@ static void CreatePage(SMTPage* aPage, uint aItemSize)
// if we get here, RAM is corrupted, since we know we had a space but it turns out we didnt
Debugger.DoSendNumber((uint)pageBlock);
Debugger.DoSendNumber(aSize);
Debugger.SendKernelPanic(0x122);
while (true) { }
Debugger.DoFail(0x122);
return null;
}

/// <summary>
Expand All @@ -449,9 +469,10 @@ public static void Free(void* aPtr)
if (size == 0)
{
// double free, this object has already been freed
Debugger.DoBochsBreak();
//Debugger.DoBochsBreak();
Debugger.DoSendNumber((uint)heapObject);
Debugger.SendKernelPanic(0x99);
Debugger.DoAssert(true, 0x99, true);
return;
}

var allocated = (uint*)aPtr;
Expand Down Expand Up @@ -483,7 +504,7 @@ public static void Free(void* aPtr)
{
blockPtr = blockPtr->NextBlock;
}
if(blockPtr->PagePtr == allocatedOnPage)
if (blockPtr != null && blockPtr->PagePtr == allocatedOnPage)
{
break;
}
Expand All @@ -495,8 +516,7 @@ public static void Free(void* aPtr)
// this shouldnt happen
Debugger.DoSendNumber((uint)aPtr);
Debugger.DoSendNumber((uint)SMT);
Debugger.SendKernelPanic(0x98);
while (true) { }
Debugger.DoFail(0x98);
}
blockPtr->SpacesLeft++;
}
Expand Down Expand Up @@ -559,4 +579,5 @@ private static int GetAllocatedObjectCount(SMTPage* aPage, uint aSize)

#endregion
}

}
5 changes: 3 additions & 2 deletions source/Cosmos.Core/Memory/RAT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ public static void Init(byte* aStartPtr, uint aSize)

if (aSize % PageSize != 0)
{
Debugger.DoSendNumber(aSize % PageSize);
Debugger.SendKernelPanic(11);

Debugger.DoSendNumber((aSize % PageSize));
Debugger.DoAssert(11);
throw new Exception("RAM size must be page aligned.");
}

Expand Down
38 changes: 13 additions & 25 deletions source/Cosmos.Core/VTablesImpl.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cosmos.Debug.Kernel;

namespace Cosmos.Core
Expand Down Expand Up @@ -158,8 +154,7 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId)
EnableDebug = true;
DebugHex("Type", aType);
DebugHex("MethodId", aMethodId);
Debugger.SendKernelPanic(KernelPanics.VMT_TypeIdInvalid);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_TypeIdInvalid);
}
var xCurrentType = aType;
do
Expand All @@ -172,15 +167,13 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId)
{
EnableDebug = true;
DebugHex("MethodIndexes is null for type", aType);
Debugger.SendKernelPanic(KernelPanics.VMT_MethodIndexesNull);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_MethodIndexesNull);
}
if (xCurrentTypeInfo.MethodAddresses == null)
{
EnableDebug = true;
DebugHex("MethodAddresses is null for type", aType);
Debugger.SendKernelPanic(KernelPanics.VMT_MethodAddressesNull);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_MethodAddressesNull);
}

for (int i = 0; i < xCurrentTypeInfo.MethodIndexes.Length; i++)
Expand All @@ -198,9 +191,8 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId)
DebugHex("MethodCount", xCurrentTypeInfo.MethodCount);
DebugHex("MethodAddresses.Length", (uint)xCurrentTypeInfo.MethodAddresses.Length);
Debug("Method found, but address is invalid!");
Debugger.SendKernelPanic(KernelPanics.VMT_MethodFoundButAddressInvalid);
while (true)
;
Debugger.DoFail(KernelPanics.VMT_MethodFoundButAddressInvalid);

}
Debug("Found.");
return xResult;
Expand All @@ -220,8 +212,7 @@ public static uint GetMethodAddressForType(uint aType, uint aMethodId)
DebugHex("MethodId", aMethodId);
Debug("Not FOUND!");

Debugger.SendKernelPanic(KernelPanics.VMT_MethodNotFound);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_MethodNotFound);
throw new Exception("Cannot find virtual method!");
}

Expand Down Expand Up @@ -254,8 +245,8 @@ public static uint GetDeclaringTypeOfMethodForType(uint aType, uint aMethodId)
DebugHex("MethodId", aMethodId);
Debug("Not FOUND Declaring TYPE!");
Debugger.DoBochsBreak();
Debugger.SendKernelPanic(KernelPanics.VMT_MethodNotFound);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_MethodNotFound);
return 0;
}

public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceMethodId)
Expand All @@ -265,8 +256,7 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM
EnableDebug = true;
DebugHex("Type", aType);
DebugHex("InterfaceMethodId", aInterfaceMethodId);
Debugger.SendKernelPanic(KernelPanics.VMT_TypeIdInvalid);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_TypeIdInvalid);
}

var xTypeInfo = mTypes[aType];
Expand All @@ -275,16 +265,14 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM
{
EnableDebug = true;
DebugHex("InterfaceMethodIndexes is null for type", aType);
Debugger.SendKernelPanic(KernelPanics.VMT_MethodIndexesNull);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_MethodIndexesNull);
}

if (xTypeInfo.TargetMethodIndexes == null)
{
EnableDebug = true;
DebugHex("TargetMethodIndexes is null for type", aType);
Debugger.SendKernelPanic(KernelPanics.VMT_MethodAddressesNull);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_MethodAddressesNull);
}

for (int i = 0; i < xTypeInfo.InterfaceMethodIndexes.Length; i++)
Expand All @@ -302,8 +290,8 @@ public static uint GetMethodAddressForInterfaceType(uint aType, uint aInterfaceM
DebugHex("InterfaceMethodId", aInterfaceMethodId);
Debug("Not FOUND!");

Debugger.SendKernelPanic(KernelPanics.VMT_MethodNotFound);
while (true) ;
Debugger.DoFail(KernelPanics.VMT_MethodNotFound);
return 0;
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions source/Cosmos.Core_Plugs/System/ArrayImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static unsafe object GetValue(Array aThis, params int[] aIndices)
}

[PlugMethod(Signature = "System_Void__System_Array_SetValue_System_Object__System_Int32_")]
public static unsafe void SetValue([ObjectPointerAccess] uint* aThis, uint aValue, int aIndex)
public static unsafe void SetValue([ObjectPointerAccess] uint* aThis, [ObjectPointerAccess] uint* aValue, int aIndex)
{
aThis = (uint*) aThis[0];
aThis += 3;
Expand All @@ -128,16 +128,16 @@ public static unsafe void SetValue([ObjectPointerAccess] uint* aThis, uint aValu
switch (xElementSize)
{
case 1:
*(byte*) aThis = (byte) aValue;
*(byte*) aThis = (byte) *aValue;
return;
case 2:
*(ushort*) aThis = (ushort) aValue;
*(ushort*) aThis = (ushort) *aValue;
return;
case 3:
*(uint*) aThis = (uint) aValue;
*(uint*) aThis = (uint) *aValue;
return;
case 4:
*(uint*) aThis = (uint) aValue;
*(uint*) aThis = (uint) *aValue;
return;
}
throw new NotSupportedException("SetValue not supported in this situation!");
Expand Down