Skip to content

Commit

Permalink
KProcess: correctly handle empty optional chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbeth committed Feb 22, 2024
1 parent b51c31d commit 80b3b22
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions app/src/main/cpp/skyline/kernel/types/KProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,21 @@ namespace skyline::kernel::type {
bool isAllocated{};

u8 *pageCandidate{state.process->memory.tlsIo.guest.data()};
std::pair<u8 *, ChunkDescriptor> chunk;
while (state.process->memory.tlsIo.guest.contains(span<u8>(pageCandidate, constant::PageSize))) {
chunk = memory.GetChunk(pageCandidate).value();
auto chunk = memory.GetChunk(pageCandidate);
if (!chunk)
break;

if (chunk.second.state == memory::states::Unmapped) {
if (chunk->second.state == memory::states::Unmapped) {
memory.MapThreadLocalMemory(span<u8>{pageCandidate, constant::PageSize});
isAllocated = true;
break;
} else {
pageCandidate = chunk.first + chunk.second.size;
pageCandidate = chunk->first + chunk->second.size;
}
}

if (!isAllocated) [[unlikely]]
if (!isAllocated)
throw exception("Failed to find free memory for a tls slot!");

auto tlsPage{std::make_shared<TlsPage>(pageCandidate)};
Expand All @@ -96,20 +97,21 @@ namespace skyline::kernel::type {
bool isAllocated{};

u8 *pageCandidate{memory.stack.guest.data()};
std::pair<u8 *, ChunkDescriptor> chunk;
while (state.process->memory.stack.guest.contains(span<u8>(pageCandidate, state.process->npdm.meta.mainThreadStackSize))) {
chunk = memory.GetChunk(pageCandidate).value();
auto chunk{memory.GetChunk(pageCandidate)};
if (!chunk)
break;

if (chunk.second.state == memory::states::Unmapped && chunk.second.size >= state.process->npdm.meta.mainThreadStackSize) {
if (chunk->second.state == memory::states::Unmapped && chunk->second.size >= state.process->npdm.meta.mainThreadStackSize) {
memory.MapStackMemory(span<u8>{pageCandidate, state.process->npdm.meta.mainThreadStackSize});
isAllocated = true;
break;
} else {
pageCandidate = chunk.first + chunk.second.size;
pageCandidate = chunk->first + chunk->second.size;
}
}

if (!isAllocated) [[unlikely]]
if (!isAllocated)
throw exception("Failed to map main thread stack!");

stackTop = pageCandidate + state.process->npdm.meta.mainThreadStackSize;
Expand Down

0 comments on commit 80b3b22

Please sign in to comment.