<commit_before>#include "build.h"
#include "xenonLib.h"  
#include "xenonLibNatives.h"
#include "xenonThread.h"
#include "xenonPlatform.h"
#include "xenonMemory.h"
#include "xenonBindings.h"

namespace xenon
{
	namespace lib
	{

		//---------------------------------------------------------------------------

		uint64 __fastcall Xbox_KeEnableFpuExceptions(uint64 ip, cpu::CpuRegs& regs)
		{
			const uint32 enabled = (const uint32)regs.R3;
			GLog.Log("KeEnableFpuExceptions(%d)", enabled);
			RETURN_ARG(0);
		}

		uint64 __fastcall Xbox_KeQueryPerformanceFrequency(uint64 ip, cpu::CpuRegs& regs)
		{
			__int64 ret = 0;
			QueryPerformanceFrequency((LARGE_INTEGER*)&ret);
			//ret *= 10;
			RETURN_ARG(ret); // english
		}

		uint64 __fastcall Xbox_XGetLanguage(uint64 ip, cpu::CpuRegs& regs)
		{
			GLog.Log("XGetLanguage: english returned");
			RETURN_ARG(1); // english
		}

		uint64 __fastcall Xbox_XGetAVPack(uint64 ip, cpu::CpuRegs& regs)
		{
			GLog.Log("XGetAVPack: returning USA/Canada");
			RETURN_ARG(0x010000);
		}

		uint64 __fastcall Xbox_XamLoaderTerminateTitle(uint64 ip, cpu::CpuRegs& regs)
		{
			GLog.Log("XamLoaderTerminateTitle: called");
			throw runtime::TerminateProcessException(ip, 0);
			RETURN();
		}

		uint64 __fastcall Xbox_XamShowMessageBoxUIEx(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_DEFAULT();
		}

		// BOOL XexCheckExecutablePrivilege(DWORD priviledge);
		uint64 __fastcall Xbox_XexCheckExecutablePrivilege(uint64 ip, cpu::CpuRegs& regs)
		{
			const uint32 mask = (const uint32)regs.R3;
			GLog.Log("Privledge check: 0x%08X", mask);
			RETURN_ARG(1);
		}

		uint64 __fastcall Xbox___C_specific_handler(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_DEFAULT();
		}

		uint64 __fastcall Xbox_HalReturnToFirmware(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_DEFAULT();
		}
		
		uint64 __fastcall Xbox_NtAllocateVirtualMemory(uint64 ip, cpu::CpuRegs& regs)
		{
			auto basePtr = Pointer<uint32>(regs.R3);
			auto base = (void*)(uint32)*basePtr;

			auto sizePtr = Pointer<uint32>(regs.R4);
			auto allocType = regs.R5;
			auto protect = regs.R6;

			const auto size = *sizePtr;

			// invalid type
			if (allocType & XMEM_RESERVE && (base != NULL))
			{
				GLog.Err("VMEM: Trying to reserve predefined memory region. Not supported.");
				regs.R3 = ((DWORD)0xC0000005L);
				*basePtr = 0;
				RETURN();
			}

			// determine aligned size
			uint32 pageAlignment = 4096;
			if (allocType & XMEM_LARGE_PAGES) pageAlignment = 64 << 10;
			else if (allocType & XMEM_16MB_PAGES) pageAlignment = 16 << 20;

			// align size
			uint32 alignedSize = (size + (pageAlignment - 1)) & ~(pageAlignment - 1);
			if (alignedSize != size)
			{
				GLog.Warn("VMEM: Aligned memory allocation %08X->%08X.", size, alignedSize);
			}

			// allocate memory from system
			void* allocated = GPlatform.GetMemory().VirtualAlloc(base, alignedSize, (uint32)allocType, (uint16_t)protect);
			if (!allocated)
			{
				GLog.Err("VMEM: Allocation failed.");
				regs.R3 = ((DWORD)0xC0000005L);
				*basePtr = 0;
				RETURN();
			}

			// save allocated memory & size
			*basePtr = (uint32)allocated;
			*sizePtr = (uint32)alignedSize;

			// allocation was OK
			regs.R3 = 0;
			RETURN();
		}

		uint64 __fastcall Xbox_KeBugCheckEx(uint64 ip, cpu::CpuRegs& regs)
		{
			GLog.Err("BugCheck: Encountered unrecoverable system exception: %d (%016LLXX, %016LLXX, %016LLXX, %016LLXX)",
				(uint32)(regs.R3), regs.R4, regs.R5, regs.R6, regs.R7);
			RETURN();
		}

		uint32 GCurrentProcessType = X_PROCTYPE_USER;

		uint64 __fastcall Xbox_KeGetCurrentProcessType(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_ARG(GCurrentProcessType); // ?
		}

		uint64 __fastcall Xbox_KeSetCurrentProcessType(uint64 ip, cpu::CpuRegs& regs)
		{
			const uint32 type = (const uint32)regs.R3;
			DEBUG_CHECK(type >= 0 && type <= 2);
			GCurrentProcessType = type;
			RETURN();
		}

		uint64 __fastcall Xbox_NtFreeVirtualMemory(uint64 ip, cpu::CpuRegs& regs)
		{
			auto basePtr = Pointer<uint32>(regs.R3);
			auto base = (void*)(uint32)*basePtr;

			auto sizePtr = Pointer<uint32>(regs.R4);
			auto size = sizePtr.IsValid() ? *sizePtr : 0;

			const auto freeType = regs.R5;
			const auto protect = regs.R6;

			if (!base)
				return X_STATUS_MEMORY_NOT_ALLOCATED;

			uint32 freedSize = 0;
			if (!GPlatform.GetMemory().VirtualFree(base, size, freeType, freedSize))
				return X_STATUS_UNSUCCESSFUL;

			if (sizePtr.IsValid())
				*sizePtr = freedSize;

			RETURN_ARG(0);
		}

		uint64 __fastcall Xbox_NtQueryVirtualMemory(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_DEFAULT();
		}<commit_msg>[launcher] Fixed bugs with memory allocation<commit_after>#include "build.h"
#include "xenonLib.h"  
#include "xenonLibNatives.h"
#include "xenonThread.h"
#include "xenonPlatform.h"
#include "xenonMemory.h"
#include "xenonBindings.h"

namespace xenon
{
	namespace lib
	{

		//---------------------------------------------------------------------------

		uint64 __fastcall Xbox_KeEnableFpuExceptions(uint64 ip, cpu::CpuRegs& regs)
		{
			const uint32 enabled = (const uint32)regs.R3;
			GLog.Log("KeEnableFpuExceptions(%d)", enabled);
			RETURN_ARG(0);
		}

		uint64 __fastcall Xbox_KeQueryPerformanceFrequency(uint64 ip, cpu::CpuRegs& regs)
		{
			__int64 ret = 0;
			QueryPerformanceFrequency((LARGE_INTEGER*)&ret);
			//ret *= 10;
			RETURN_ARG(ret); // english
		}

		uint64 __fastcall Xbox_XGetLanguage(uint64 ip, cpu::CpuRegs& regs)
		{
			GLog.Log("XGetLanguage: english returned");
			RETURN_ARG(1); // english
		}

		uint64 __fastcall Xbox_XGetAVPack(uint64 ip, cpu::CpuRegs& regs)
		{
			GLog.Log("XGetAVPack: returning USA/Canada");
			RETURN_ARG(0x010000);
		}

		uint64 __fastcall Xbox_XamLoaderTerminateTitle(uint64 ip, cpu::CpuRegs& regs)
		{
			GLog.Log("XamLoaderTerminateTitle: called");
			throw runtime::TerminateProcessException(ip, 0);
			RETURN();
		}

		uint64 __fastcall Xbox_XamShowMessageBoxUIEx(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_DEFAULT();
		}

		// BOOL XexCheckExecutablePrivilege(DWORD priviledge);
		uint64 __fastcall Xbox_XexCheckExecutablePrivilege(uint64 ip, cpu::CpuRegs& regs)
		{
			const uint32 mask = (const uint32)regs.R3;
			GLog.Log("Privledge check: 0x%08X", mask);
			RETURN_ARG(1);
		}

		uint64 __fastcall Xbox___C_specific_handler(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_DEFAULT();
		}

		uint64 __fastcall Xbox_HalReturnToFirmware(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_DEFAULT();
		}
		
		uint64 __fastcall Xbox_NtAllocateVirtualMemory(uint64 ip, cpu::CpuRegs& regs)
		{
			auto basePtr = Pointer<uint32>(regs.R3);
			auto base = (void*)(uint32)*basePtr;

			auto sizePtr = Pointer<uint32>(regs.R4);
			auto allocType = regs.R5;
			auto protect = regs.R6;

			const auto size = (uint32_t)*sizePtr;

			// invalid type
			if (allocType & XMEM_RESERVE && (base != NULL))
			{
				GLog.Err("VMEM: Trying to reserve predefined memory region. Not supported.");
				regs.R3 = ((DWORD)0xC0000005L);
				*basePtr = 0;
				RETURN();
			}

			// determine aligned size
			uint32 pageAlignment = 4096;
			if (allocType & XMEM_LARGE_PAGES) pageAlignment = 64 << 10;
			else if (allocType & XMEM_16MB_PAGES) pageAlignment = 16 << 20;

			// align size
			uint32 alignedSize = (size + (pageAlignment - 1)) & ~(pageAlignment - 1);
			if (alignedSize != size)
			{
				GLog.Warn("VMEM: Aligned memory allocation %08X->%08X.", size, alignedSize);
			}

			// allocate memory from system
			void* allocated = GPlatform.GetMemory().VirtualAlloc(base, alignedSize, (uint32)allocType, (uint16_t)protect);
			if (!allocated)
			{
				GLog.Err("VMEM: Allocation failed.");
				regs.R3 = ((DWORD)0xC0000005L);
				*basePtr = 0;
				RETURN();
			}

			// save allocated memory & size
			*basePtr = (uint32)allocated;
			*sizePtr = (uint32)alignedSize;

			// allocation was OK
			regs.R3 = 0;
			RETURN();
		}

		uint64 __fastcall Xbox_KeBugCheckEx(uint64 ip, cpu::CpuRegs& regs)
		{
			GLog.Err("BugCheck: Encountered unrecoverable system exception: %d (%016LLXX, %016LLXX, %016LLXX, %016LLXX)",
				(uint32)(regs.R3), regs.R4, regs.R5, regs.R6, regs.R7);
			RETURN();
		}

		uint32 GCurrentProcessType = X_PROCTYPE_USER;

		uint64 __fastcall Xbox_KeGetCurrentProcessType(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_ARG(GCurrentProcessType); // ?
		}

		uint64 __fastcall Xbox_KeSetCurrentProcessType(uint64 ip, cpu::CpuRegs& regs)
		{
			const uint32 type = (const uint32)regs.R3;
			DEBUG_CHECK(type >= 0 && type <= 2);
			GCurrentProcessType = type;
			RETURN();
		}

		uint64 __fastcall Xbox_NtFreeVirtualMemory(uint64 ip, cpu::CpuRegs& regs)
		{
			auto basePtr = Pointer<uint32>(regs.R3);
			auto base = (void*)(uint32)*basePtr;

			auto sizePtr = Pointer<uint32>(regs.R4);
			auto size = sizePtr.IsValid() ? (uint32_t)*sizePtr : 0;

			const auto freeType = regs.R5;
			const auto protect = regs.R6;

			if (!base)
				return X_STATUS_MEMORY_NOT_ALLOCATED;

			uint32 freedSize = 0;
			if (!GPlatform.GetMemory().VirtualFree(base, size, freeType, freedSize))
				return X_STATUS_UNSUCCESSFUL;

			if (sizePtr.IsValid())
				*sizePtr = freedSize;

			RETURN_ARG(0);
		}

		uint64 __fastcall Xbox_NtQueryVirtualMemory(uint64 ip, cpu::CpuRegs& regs)
		{
			RETURN_DEFAULT();
		}
{'fraction_non_alphanumeric': 0.14222125719248724, 'fraction_numerical': 0.04299207469330149, 'mean_word_length': 3.6761421319796956, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4542396', 'n_tokens_mistral': 4121, 'n_tokens_neox': 3360, 'n_words': 687}