[+] IProcessSectionMapView

[+] IProcessSectionView (downgraded bc NT)
This commit is contained in:
Reece Wilson 2022-08-10 10:50:49 +01:00
parent 64cb7404ba
commit eda8d4255d
13 changed files with 489 additions and 7 deletions

View File

@ -0,0 +1,24 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: IProcessSectionView.hpp
Date: 2022-08-09
Author: Reece
***/
#pragma once
namespace Aurora::Process
{
struct IProcessSectionMapView
{
virtual void Unmap() = 0;
virtual bool Flush(AuUInt offset, AuUInt length) = 0;
virtual AuUInt GetBaseAddress() = 0;
virtual AuUInt GetAddress(AuUInt offset) = 0;
virtual AuUInt8 *GetBasePointer() = 0;
virtual AuUInt8 *GetPointer(AuUInt offset) = 0;
};
}

View File

@ -0,0 +1,37 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: IProcessSectionView.hpp
Date: 2022-08-09
Author: Reece
***/
#pragma once
namespace Aurora::Process
{
struct IProcessSectionMapView;
struct IProcessSectionView
{
virtual AuSPtr<IProcessSectionMapView> Allocate(AuUInt length) = 0;
virtual AuSPtr<IProcessSectionMapView> MapFileByPath(const AuString &str,
AuUInt64 offset,
AuUInt length,
Aurora::IO::FS::EFileOpenMode mode,
Aurora::IO::FS::EFileAdvisoryLockLevel processLockLevel) = 0;
virtual AuSPtr<IProcessSectionMapView> MapFileByObject(const AuSPtr<Aurora::IO::FS::IFileStream> &stream,
AuUInt64 offset,
AuUInt length,
Aurora::IO::FS::EFileOpenMode mode,
Aurora::IO::FS::EFileAdvisoryLockLevel processLockLevel) = 0;
virtual AuSPtr<IProcessSectionMapView> MapIPCMemory(const AuString &handle,
AuUInt64 offset,
Aurora::IO::FS::EFileOpenMode mode) = 0;
};
AUKN_SYM AuSPtr<IProcessSectionView> GetGlobalProcessSpace();
//AUKN_SHARED_API(ReservedAddressSpace, IProcessSectionView, AuUInt); < Windows SUCKS
}

View File

@ -9,6 +9,8 @@
#include "Paths.hpp"
#include "ProcessMap.hpp"
#include "IProcessSectionMapView.hpp"
#include "IProcessSectionView.hpp"
namespace Aurora::Process
{

View File

@ -26,7 +26,7 @@
#if defined(AURORA_PLATFORM_WIN32)
#include "Extensions/Win32/DarkTheme.hpp"
#endif
#include "Process/ProcessMap.hpp"
#include "Process/Process.hpp"
#include "Exit/Exit.hpp"
#include "CmdLine/CmdLine.hpp"
#include "Grug/Grug.hpp"
@ -46,7 +46,7 @@ static void Init()
Aurora::RNG::Init();
Aurora::Threading::InitSleep();
Aurora::Process::InitProcessMap();
Aurora::Process::InitProcess();
Aurora::SWInfo::InitSwInfo();
Aurora::Threading::Threads::InitThreading();
Aurora::Exit::InitExit();

View File

@ -212,6 +212,11 @@ namespace Aurora::IO::FS
this->bShouldDelete = true;
}
HANDLE WinFileStream::GetHandle()
{
return this->handle_;
}
AUKN_SYM IFileStream *OpenNew(const AuString &path, EFileOpenMode openMode, EFileAdvisoryLockLevel lock)
{
try

View File

@ -11,9 +11,8 @@
namespace Aurora::IO::FS
{
class WinFileStream : public IFileStream
struct WinFileStream : IFileStream
{
public:
~WinFileStream();
void Init(HANDLE handle, const AuString &path);
@ -28,6 +27,7 @@ namespace Aurora::IO::FS
void WriteEoS() override;
void MakeTemporary() override;
HANDLE GetHandle();
private:
bool bShouldDelete {};

View File

@ -297,6 +297,11 @@ namespace Aurora::IO::FS
this->bMadeTemporary = true;
}
int PosixFileStream::GetHandle()
{
return this->handle_;
}
static IFileStream *OpenNew(const AuString &path, EFileOpenMode openMode, EFileAdvisoryLockLevel lock)
{
auto pathex = NormalizePathRet(path);

View File

@ -16,9 +16,8 @@ namespace Aurora::IO::FS
bool PosixRead(int fd, void *buf, AuUInt32 count, AuUInt32 *pRead);
bool PosixWrite(int fd, const void *buf, AuUInt32 count, AuUInt32 *pWritten);
class PosixFileStream : public IFileStream
struct PosixFileStream : IFileStream
{
public:
~PosixFileStream();
bool Init(int handle, const AuString &path);
@ -33,6 +32,7 @@ namespace Aurora::IO::FS
void WriteEoS() override;
void MakeTemporary() override;
int GetHandle();
private:
int handle_ = -1;

View File

@ -21,6 +21,10 @@
#include <wintrust.h>
#endif
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include "ProcessSectionView.NT.hpp"
#endif
#include <Source/IO/FS/FS.hpp>
#include <Source/IO/FS/Resources.hpp>
@ -559,6 +563,9 @@ namespace Aurora::Process
// TODO (reece): test if self is signed -> xref kIsMainSigned
InitProcessMap();
#if defined(AURORA_IS_MODERNNT_DERIVED)
#endif
}
void DeinitProcess()

View File

@ -0,0 +1,66 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ProcessSectionFileMapView.NT.cpp
Date: 2022-08-09
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "ProcessSectionFileMapView.NT.hpp"
#include "ProcessSectionView.NT.hpp"
namespace Aurora::Process
{
ProcessSectionFileMapView::~ProcessSectionFileMapView()
{
Unmap();
}
ProcessSectionFileMapView::ProcessSectionFileMapView(AuUInt uAddress, HANDLE hSection) :
uAddress(uAddress),
hFileSection(hSection)
{
}
void ProcessSectionFileMapView::Unmap()
{
if (AuExchange(this->bIsDead_, true))
{
return;
}
if (this->uAddress)
{
::UnmapViewOfFile((PVOID)this->uAddress);
this->uAddress = 0;
}
AuWin32CloseHandle(this->hFileSection);
}
bool ProcessSectionFileMapView::Flush(AuUInt offset, AuUInt length)
{
return ::FlushViewOfFile(this->GetPointer(offset), length);
}
AuUInt ProcessSectionFileMapView::GetBaseAddress()
{
return this->uAddress;
}
AuUInt ProcessSectionFileMapView::GetAddress(AuUInt offset)
{
return this->uAddress ? this->uAddress + offset : 0;
}
AuUInt8 *ProcessSectionFileMapView::GetBasePointer()
{
return AuReinterpretCast<AuUInt8 *>(this->uAddress);
}
AuUInt8 *ProcessSectionFileMapView::GetPointer(AuUInt offset)
{
return this->uAddress ? AuReinterpretCast<AuUInt8 *>(this->uAddress) + offset : 0;
}
}

View File

@ -0,0 +1,35 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ProcessSectionFileMapView.NT.hpp
Date: 2022-08-09
Author: Reece
***/
#pragma once
namespace Aurora::Process
{
struct ProcessSectionView;
struct ProcessSectionFileMapView : IProcessSectionMapView
{
virtual ~ProcessSectionFileMapView();
ProcessSectionFileMapView(AuUInt uAddress, HANDLE hFileSection);
virtual void Unmap() override;
virtual bool Flush(AuUInt offset, AuUInt length) override;
AuUInt GetBaseAddress() override;
AuUInt GetAddress(AuUInt offset) override;
AuUInt8 *GetBasePointer() override;
AuUInt8 *GetPointer(AuUInt offset) override;
private:
HANDLE hFileSection {};
AuUInt uAddress {};
bool bIsDead_ {};
};
}

View File

@ -0,0 +1,268 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ProcessSectionView.hpp
Date: 2022-08-09
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "ProcessSectionView.NT.hpp"
#include "Process.hpp"
#include <Source/IO/FS/FileStream.NT.hpp>
#include "ProcessSectionFileMapView.NT.hpp"
#include <Source/IO/IPC/IPCHandle.hpp>
#include <Windows.h>
namespace Aurora::Process
{
AuSPtr<IProcessSectionMapView> ProcessSectionView::Allocate(AuUInt length)
{
HANDLE hFileMap;
hFileMap = ::CreateFileMappingA(INVALID_HANDLE_VALUE,
nullptr,
PAGE_READWRITE,
#if defined(AURORA_IS_64BIT)
AuBitsToHigher(length),
AuBitsToLower(length),
#else
0,
length,
#endif
nullptr);
if ((hFileMap == INVALID_HANDLE_VALUE) ||
(!hFileMap))
{
SysPushErrorIO("Couldn't create file map");
return {};
}
auto map = ::MapViewOfFile(hFileMap,
SECTION_MAP_READ | SECTION_MAP_WRITE,
0,
0,
length);
if (!map)
{
SysPushErrorIO("Couldn't create allocation of section");
AuWin32CloseHandle(hFileMap);
return {};
}
auto newObject = AuMakeShared<ProcessSectionFileMapView>(AuUInt(map), hFileMap);
if (!newObject)
{
SysPushErrorMem();
AuWin32CloseHandle(hFileMap);
return {};
}
return newObject;
}
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapFileByPath(const AuString &str,
AuUInt64 offset,
AuUInt length,
AuFS::EFileOpenMode mode,
AuFS::EFileAdvisoryLockLevel sectionLock)
{
auto file = AuFS::OpenShared(str, mode, AuFS::EFileAdvisoryLockLevel::eNoSafety);
return file ? this->MapFileByObject(file, offset, length, mode, sectionLock) : AuSPtr<IProcessSectionMapView> {};
}
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapFileByObject(const AuSPtr<AuFS::IFileStream> &stream,
AuUInt64 offset,
AuUInt length,
AuFS::EFileOpenMode mode,
AuFS::EFileAdvisoryLockLevel processLockLevel)
{
HANDLE hFileMap;
ULONG desiredAccess {}, pageAttributes {};
if (!stream)
{
return {};
}
auto ok = AuStaticCast<AuFS::WinFileStream>(stream);
if (processLockLevel != AuFS::EFileAdvisoryLockLevel::eNoSafety)
{
DWORD dwFlags {};
OVERLAPPED overlapped {};
if (processLockLevel == AuFS::EFileAdvisoryLockLevel::eBlockReadWrite)
{
dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
}
dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
overlapped.Offset = AuBitsToLower(offset);
overlapped.OffsetHigh = AuBitsToHigher(offset);
if (!::LockFileEx(ok->GetHandle(),
dwFlags,
0,
AuBitsToLower(length),
AuBitsToHigher(length),
&overlapped))
{
SysPushErrorIO("No Lock");
return {};
}
}
switch (mode)
{
case AuFS::EFileOpenMode::eRead:
{
desiredAccess = SECTION_MAP_READ;
pageAttributes = PAGE_READONLY;
break;
}
case AuFS::EFileOpenMode::eWrite:
case AuFS::EFileOpenMode::eReadWrite:
{
desiredAccess = SECTION_MAP_READ | SECTION_MAP_WRITE;
pageAttributes = PAGE_READWRITE;
break;
}
default:
SysPushErrorGeneric();
return {};
};
hFileMap = ::CreateFileMappingA(ok->GetHandle(),
nullptr,
pageAttributes,
#if defined(AURORA_IS_64BIT)
AuBitsToHigher(length),
AuBitsToLower(length),
#else
0,
length,
#endif
nullptr);
if ((hFileMap == INVALID_HANDLE_VALUE) ||
(!hFileMap))
{
SysPushErrorIO("Couldn't create file map. Is the requeted access mode too high for the given object?");
return {};
}
auto map = ::MapViewOfFile(hFileMap,
desiredAccess,
AuBitsToHigher(offset),
AuBitsToLower(offset),
length);
if (!map)
{
SysPushErrorIO("Couldn't create map of section");
AuWin32CloseHandle(hFileMap);
return {};
}
auto newObject = AuMakeShared<ProcessSectionFileMapView>(AuUInt(map), hFileMap);
if (!newObject)
{
SysPushErrorMem();
AuWin32CloseHandle(hFileMap);
::UnmapViewOfFile(map);
return {};
}
return newObject;
}
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapIPCMemory(const AuString &handleString,
AuUInt64 offset,
AuFS::EFileOpenMode mode)
{
AuIPC::IPCHandle handle;
HANDLE hFileMap;
ULONG desiredAccess {}, pageAttributes {};
if (!handle.FromString(handleString))
{
SysPushErrorParseError("{}", handleString);
return {};
}
auto token = handle.GetToken(AuIPC::EIPCHandleType::eIPCMemory, 0);
if (!token)
{
SysPushErrorParseError();
return {};
}
auto length = token->token.word;
auto path = token->token.ToNTPath();
switch (mode)
{
case AuFS::EFileOpenMode::eRead:
{
desiredAccess = SECTION_MAP_READ;
pageAttributes = PAGE_READONLY;
break;
}
case AuFS::EFileOpenMode::eWrite:
case AuFS::EFileOpenMode::eReadWrite:
{
desiredAccess = SECTION_MAP_READ | SECTION_MAP_WRITE;
pageAttributes = PAGE_READWRITE;
break;
}
default:
SysPushErrorGeneric();
return {};
};
hFileMap = ::CreateFileMappingA(INVALID_HANDLE_VALUE,
nullptr,
pageAttributes,
#if defined(AURORA_IS_64BIT)
AuBitsToHigher(length),
AuBitsToLower(length),
#else
0,
length,
#endif
path.c_str());
if ((hFileMap == INVALID_HANDLE_VALUE) ||
(!hFileMap))
{
SysPushErrorIO("Couldn't create IPC map (handle: {})", handleString);
return {};
}
auto map = ::MapViewOfFile(hFileMap,
desiredAccess,
AuBitsToHigher(offset),
AuBitsToLower(offset),
length);
if (!map)
{
SysPushErrorIO("Couldn't create map of IPC section (handle: {})", handleString);
AuWin32CloseHandle(hFileMap);
return {};
}
auto newObject = AuMakeShared<ProcessSectionFileMapView>(AuUInt(map), hFileMap);
if (!newObject)
{
SysPushErrorMem();
AuWin32CloseHandle(hFileMap);
::UnmapViewOfFile(map);
return {};
}
return newObject;
}
AUKN_SYM AuSPtr<IProcessSectionView> GetGlobalProcessSpace()
{
static ProcessSectionView gSingleton;
return AuUnsafeRaiiToShared(&gSingleton);
}
}

View File

@ -0,0 +1,33 @@
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ProcessSectionView.hpp
Date: 2022-08-09
Author: Reece
***/
#pragma once
namespace Aurora::Process
{
struct ProcessSectionView : IProcessSectionView
{
AuSPtr<IProcessSectionMapView> Allocate(AuUInt length) override;
AuSPtr<IProcessSectionMapView> MapFileByPath(const AuString &str,
AuUInt64 offset,
AuUInt length,
AuFS::EFileOpenMode mode,
AuFS::EFileAdvisoryLockLevel sectionLock) override;
AuSPtr<IProcessSectionMapView> MapFileByObject(const AuSPtr<Aurora::IO::FS::IFileStream> &stream,
AuUInt64 offset,
AuUInt length,
Aurora::IO::FS::EFileOpenMode mode,
AuFS::EFileAdvisoryLockLevel processLockLevel) override;
AuSPtr<IProcessSectionMapView> MapIPCMemory(const AuString &handle,
AuUInt64 offset,
AuFS::EFileOpenMode mode) override;
};
}