[*] POSIX: AuProcesses mustn't allocate (sorta) while in a COW address space

This commit is contained in:
Reece Wilson 2024-08-05 22:13:02 +01:00
parent 57c5515173
commit a7f2042051
2 changed files with 85 additions and 26 deletions

View File

@ -437,16 +437,48 @@ namespace Aurora::Processes
InitProcessStdHandles(this->startup_.fwdErr, this->pipeStdErr_, AuIO::EStandardStream::eErrorStream, this->startup_.handleErrorStream);
InitProcessStdHandles(this->startup_.fwdIn, this->pipeStdIn_, AuIO::EStandardStream::eInputStream, this->startup_.handleInputStream);
this->loopSource_ = AuMakeShared<ProcessAliveLoopSource>();
if (!this->loopSource_)
if (!AuFS::FileExists(this->startup_.process))
{
SysPushErrorIO("No Process Module / Missing File");
return false;
}
this->loopSource_ = AuMakeShared<ProcessAliveLoopSource>();
if (!this->loopSource_)
{
SysPushErrorMemory();
return false;
}
this->pSemaphore_ = AuLoop::NewLSSemaphoreSlow(0);
if (!this->pSemaphore_)
{
SysPushErrorIO("No sync");
return false;
}
{
auto semaphoreFd = AuLoop::DbgLoopSourceToReadFd(this->pSemaphore_);
AuLogDbg("FD semaphore: {}", semaphoreFd);
int iFlags = fcntl(semaphoreFd, F_GETFD, 0);
if (iFlags < 0)
{
SysPushErrorIO("fcntl error");
return false;
}
iFlags &= ~FD_CLOEXEC;
if (fcntl(semaphoreFd, F_SETFD, iFlags) < 0)
{
AuLogDbg("failed to update? {}", errno);
return false;
}
}
this->finished_ = AuThreadPrimitives::EventShared(false, false, true);
if (!this->finished_)
{
SysPushErrorIO("No sync");
return false;
}
@ -561,6 +593,16 @@ namespace Aurora::Processes
{
PosixDoForkHooks();
static const auto kForkMemSize = 4 * 1024 * 1024;
auto pHeapMemory = SysAllocateLarge(kForkMemSize);
if (!pHeapMemory)
{
return;
}
AuMemoryViewWrite heapView { pHeapMemory, kForkMemSize };
AuMemory::RequestHeapOfRegion heapObject(heapView);
{
::dup2(this->pipeStdIn_[0], STDIN_FILENO);
::close(this->pipeStdIn_[0]);
@ -590,25 +632,9 @@ namespace Aurora::Processes
if (!this->startup_.bInheritEnvironmentVariables)
{
try
{
AuList<AuString> keys;
for (const auto &[key, value] : AuProcess::EnvironmentGetAll())
{
keys.push_back(key);
}
AuProcess::EnvironmentRemoveMany(keys);
}
catch (...)
{
SysPanic("Couldn't fork");
}
AuProcess::EnvironmentRemoveMany(this->envKeysToRemove_);
}
PosixFDYeetus();
if (this->startup_.environmentVariables.size())
{
SysAssert(AuProcess::EnvironmentSetMany(this->startup_.environmentVariables));
@ -659,7 +685,7 @@ namespace Aurora::Processes
#elif defined(AURORA_IS_POSIX_DERIVED)
if (auto cpuSet = cpuset_alloc())
if (auto cpuSet = heapObject->ZAlloc<cpuset_t *>(CPU_ALLOC_SIZE(512))) //cpuset_alloc())
{
cpuset_init(cpuSet);
@ -686,16 +712,46 @@ namespace Aurora::Processes
{
this->startup_.posixApplySandboxCOW();
}
::execv(this->startup_.process.c_str(), (char * const *)this->cargs_.data()); // https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
SysPushErrorGen("execv didn't overwrite the process map. Launch: {} ({})", this->startup_.process, this->debug_);
auto pProcessPath = heapObject->ZAlloc<char *>(this->startup_.process.size() + 1);
AuMemcpy(pProcessPath, this->startup_.process.c_str(), this->startup_.process.size());
auto pArgsBlock = heapObject->ZAlloc<void **>((this->cargs_.size() + 1) * sizeof(void *));
for (AU_ITERATE_N(i, this->cargs_.size()))
{
if (auto pString = this->cargs_[i])
{
auto uLength = strlen(pString) + 1;
auto pArg = heapObject->ZAlloc<char *>(uLength);
AuMemcpy(pArg, pString, uLength);
pArgsBlock[i] = pArg;
}
}
{
auto semaphoreFd = AuLoop::DbgLoopSourceToReadFd(this->pSemaphore_);
this->pSemaphore_->AddOne();
close(semaphoreFd);
}
PosixFDYeetus();
::execv(pProcessPath, (char * const *)pArgsBlock); // https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
}
bool ProcessImpl::Start()
{
this->exitCode_ = 0x10110100;
if (!this->startup_.bInheritEnvironmentVariables)
{
for (const auto &[key, value] : AuProcess::EnvironmentGetAll())
{
this->envKeysToRemove_.push_back(key);
}
}
if (this->type_ == ESpawnType::eSpawnOvermap)
{
this->ForkMain();
@ -740,7 +796,7 @@ namespace Aurora::Processes
SysAssert(AuTryInsert(gPidLookupMap, pid, this));
}
return true;
return this->pSemaphore_->WaitOn(250);
}
else
{

View File

@ -101,6 +101,9 @@ namespace Aurora::Processes
AuSInt exitCode_ {};
bool bHasExited {};
AuSPtr<AuLoop::ILSSemaphore> pSemaphore_;
AuList<AuString> envKeysToRemove_;
AuSPtr<IO::CompletionGroup::ICompletionGroup> pCompletionGroup_;
};
}