2022-08-10 11:12:36 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
File: AuProcessSectionView.Unix.hpp
|
2022-08-10 11:12:36 +00:00
|
|
|
Date: 2022-08-10
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuProcessSectionView.Unix.hpp"
|
2022-08-10 11:12:36 +00:00
|
|
|
#include "Process.hpp"
|
|
|
|
#include <Source/IO/FS/FileStream.Unix.hpp>
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuProcessSectionFileMapView.Unix.hpp"
|
2022-12-16 00:41:01 +00:00
|
|
|
#include <Source/IO/IPC/AuIPCHandle.hpp>
|
|
|
|
#include <Source/IO/IPC/AuIPCMemory.Unix.hpp>
|
2022-08-10 11:12:36 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
namespace Aurora::Process
|
|
|
|
{
|
2022-12-17 20:14:19 +00:00
|
|
|
AuUInt ProcessSectionView::GetStart()
|
|
|
|
{
|
|
|
|
return AuNumericLimits<AuUInt>::min();
|
|
|
|
}
|
|
|
|
|
|
|
|
AuUInt ProcessSectionView::GetEnd()
|
|
|
|
{
|
|
|
|
return AuNumericLimits<AuUInt>::max();
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::Allocate(AuUInt uLength)
|
2022-08-10 11:12:36 +00:00
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
if (!uLength)
|
2022-08-14 23:41:43 +00:00
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
SysPushErrorArg("invalid uLength");
|
2022-08-14 23:41:43 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
auto map = ::mmap(nullptr, uLength, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
|
2022-08-10 11:12:36 +00:00
|
|
|
if (map == MAP_FAILED)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto newObject = AuMakeShared<ProcessSectionFileMapView>(AuUInt(map),
|
2022-12-16 00:41:01 +00:00
|
|
|
uLength,
|
2022-08-10 11:12:36 +00:00
|
|
|
false);
|
|
|
|
if (!newObject)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
2022-12-16 00:41:01 +00:00
|
|
|
::munmap(map, uLength);
|
2022-08-10 11:12:36 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapFileByPath(const AuString &str,
|
2022-12-16 00:41:01 +00:00
|
|
|
AuUInt64 uOffset,
|
|
|
|
AuUInt uLength,
|
2022-08-10 11:12:36 +00:00
|
|
|
AuFS::EFileOpenMode mode,
|
|
|
|
AuFS::EFileAdvisoryLockLevel sectionLock)
|
|
|
|
{
|
|
|
|
auto file = AuFS::OpenShared(str, mode, AuFS::EFileAdvisoryLockLevel::eNoSafety);
|
2022-12-16 00:41:01 +00:00
|
|
|
return file ? this->MapFileByObject(file, uOffset, uLength, mode, sectionLock) : AuSPtr<IProcessSectionMapView> {};
|
2022-08-10 11:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapFileByObject(const AuSPtr<AuFS::IFileStream> &stream,
|
2022-12-16 00:41:01 +00:00
|
|
|
AuUInt64 uOffset,
|
|
|
|
AuUInt uLength,
|
2022-08-10 11:12:36 +00:00
|
|
|
AuFS::EFileOpenMode mode,
|
|
|
|
AuFS::EFileAdvisoryLockLevel processLockLevel)
|
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
return this->MapFileByObjectEx(0, stream, uOffset, uLength, mode, processLockLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapIPCMemory(const AuString &handleString,
|
|
|
|
AuUInt64 uOffset,
|
|
|
|
AuUInt uLength,
|
|
|
|
AuFS::EFileOpenMode mode)
|
|
|
|
{
|
|
|
|
return this->MapIPCMemoryEx(0, handleString, uOffset, uLength, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::AllocateEx(AuUInt uLength, AuUInt uOffset)
|
|
|
|
{
|
|
|
|
PageTable table {};
|
|
|
|
table.NX = true;
|
|
|
|
table.readable = true;
|
|
|
|
table.writable = true;
|
|
|
|
return this->AllocateEx2(uLength, uOffset, table);
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::AllocateEx2(AuUInt uLength, AuUInt uOffset, PageTable permissions)
|
|
|
|
{
|
|
|
|
if (!uLength)
|
2022-08-10 11:12:36 +00:00
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
SysPushErrorArg("invalid uLength");
|
2022-08-10 11:12:36 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
AuUInt32 uProtFlags {};
|
|
|
|
|
|
|
|
if (!permissions.NX)
|
2022-08-14 23:41:43 +00:00
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
uProtFlags |= PROT_EXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (permissions.writable)
|
|
|
|
{
|
|
|
|
uProtFlags |= PROT_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (permissions.readable)
|
|
|
|
{
|
|
|
|
uProtFlags |= PROT_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto map = ::mmap((void *)uOffset, uLength, uProtFlags, MAP_ANONYMOUS | MAP_PRIVATE | (uOffset ? MAP_FIXED : 0), 0, 0);
|
|
|
|
if (map == MAP_FAILED)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
2022-08-14 23:41:43 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
auto newObject = AuMakeShared<ProcessSectionFileMapView>(AuUInt(map),
|
|
|
|
uLength,
|
|
|
|
false);
|
|
|
|
if (!newObject)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
::munmap(map, uLength);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapFileByPathEx(AuUInt viewOffset,
|
|
|
|
const AuString &str,
|
|
|
|
AuUInt64 uOffset,
|
|
|
|
AuUInt uLength,
|
|
|
|
Aurora::IO::FS::EFileOpenMode mode,
|
|
|
|
Aurora::IO::FS::EFileAdvisoryLockLevel processLockLevel)
|
|
|
|
{
|
|
|
|
auto file = AuFS::OpenShared(str, mode, AuFS::EFileAdvisoryLockLevel::eNoSafety);
|
|
|
|
return file ? this->MapFileByObjectEx(viewOffset, file, uOffset, uLength, mode, processLockLevel) : AuSPtr<IProcessSectionMapView> {};
|
|
|
|
}
|
|
|
|
|
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapFileByObjectEx(AuUInt viewOffset,
|
|
|
|
const AuSPtr<Aurora::IO::FS::IFileStream> &pStream,
|
|
|
|
AuUInt64 uOffset,
|
|
|
|
AuUInt uLength,
|
|
|
|
Aurora::IO::FS::EFileOpenMode mode,
|
|
|
|
Aurora::IO::FS::EFileAdvisoryLockLevel processLockLevel)
|
|
|
|
{
|
|
|
|
if (!pStream)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uLength)
|
|
|
|
{
|
|
|
|
SysPushErrorArg("invalid uLength");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ok = AuStaticCast<AuFS::PosixFileStream>(pStream);
|
2022-08-10 11:12:36 +00:00
|
|
|
int fd = ::dup(ok->GetHandle());
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO (Reece): lock garbage
|
|
|
|
|
|
|
|
int prot {};
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case AuFS::EFileOpenMode::eRead:
|
|
|
|
{
|
|
|
|
prot = PROT_READ;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AuFS::EFileOpenMode::eWrite:
|
|
|
|
case AuFS::EFileOpenMode::eReadWrite:
|
|
|
|
{
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
SysPushErrorGeneric();
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
auto map = ::mmap((void *)viewOffset, uLength, prot, MAP_SHARED | (viewOffset ? MAP_FIXED : 0), fd, uOffset);
|
2022-08-10 11:12:36 +00:00
|
|
|
if (map == MAP_FAILED)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto newObject = AuMakeShared<ProcessSectionFileMapView>(AuUInt(map),
|
2022-12-16 00:41:01 +00:00
|
|
|
uLength,
|
2022-08-10 11:12:36 +00:00
|
|
|
true,
|
|
|
|
fd);
|
|
|
|
if (!newObject)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
::close(fd);
|
2022-12-16 00:41:01 +00:00
|
|
|
::munmap(map, uLength);
|
2022-08-10 11:12:36 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObject;
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
AuSPtr<IProcessSectionMapView> ProcessSectionView::MapIPCMemoryEx(AuUInt viewOffset,
|
|
|
|
const AuString &handleString,
|
|
|
|
AuUInt64 uOffset,
|
|
|
|
AuUInt uLength,
|
|
|
|
Aurora::IO::FS::EFileOpenMode mode)
|
2022-08-10 11:12:36 +00:00
|
|
|
{
|
|
|
|
AuIPC::IPCHandle handle;
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
if (!uLength)
|
2022-08-14 23:41:43 +00:00
|
|
|
{
|
2022-12-16 00:41:01 +00:00
|
|
|
SysPushErrorArg("invalid uLength");
|
2022-08-14 23:41:43 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-10 11:12:36 +00:00
|
|
|
if (!handle.FromString(handleString))
|
|
|
|
{
|
|
|
|
SysPushErrorParseError("Invalid handle: {}", handleString);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto val = handle.GetToken(AuIPC::EIPCHandleType::eIPCMemory, 0);
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
SysPushErrorParseError("Invalid handle: {}", handleString);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-11 11:28:20 +00:00
|
|
|
auto actualLength = val->token.word;
|
|
|
|
auto path = AuIPC::GetServerPath(val->token);
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
if (actualLength < uOffset + uLength)
|
2022-08-11 11:28:20 +00:00
|
|
|
{
|
|
|
|
SysPushErrorIO("Out of range");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-10 11:12:36 +00:00
|
|
|
int fd = ::shm_open(path.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-08-11 11:28:20 +00:00
|
|
|
// TODO (Reece): lock garbage ( ??? )
|
|
|
|
|
2022-08-10 11:12:36 +00:00
|
|
|
int prot {};
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case AuFS::EFileOpenMode::eRead:
|
|
|
|
{
|
|
|
|
prot = PROT_READ;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AuFS::EFileOpenMode::eWrite:
|
|
|
|
case AuFS::EFileOpenMode::eReadWrite:
|
|
|
|
{
|
|
|
|
prot = PROT_READ | PROT_WRITE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
SysPushErrorGeneric();
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
auto map = ::mmap((void *)viewOffset, uLength, prot, MAP_SHARED | (viewOffset ? MAP_FIXED : 0), fd, uOffset);
|
2022-08-10 11:12:36 +00:00
|
|
|
if (map == MAP_FAILED)
|
|
|
|
{
|
|
|
|
SysPushErrorIO();
|
|
|
|
::close(fd);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto newObject = AuMakeShared<ProcessSectionFileMapView>(AuUInt(map),
|
2022-12-16 00:41:01 +00:00
|
|
|
uLength,
|
2022-08-10 11:12:36 +00:00
|
|
|
true,
|
|
|
|
fd);
|
|
|
|
if (!newObject)
|
|
|
|
{
|
|
|
|
SysPushErrorMem();
|
|
|
|
::close(fd);
|
|
|
|
::munmap(map, val->token.word);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObject;
|
|
|
|
}
|
|
|
|
|
2022-12-16 00:41:01 +00:00
|
|
|
AuList<AuPair<AuUInt, AuUInt>> ProcessSectionView::GetAllocations()
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(this->spinlock);
|
|
|
|
return allocations;
|
|
|
|
}
|
|
|
|
|
2022-08-10 11:12:36 +00:00
|
|
|
AUKN_SYM AuSPtr<IProcessSectionView> GetGlobalProcessSpace()
|
|
|
|
{
|
|
|
|
static ProcessSectionView gSingleton;
|
|
|
|
return AuUnsafeRaiiToShared(&gSingleton);
|
|
|
|
}
|
|
|
|
}
|