Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,23 @@ int APS5_VABI munmap_nid_postfix(void* address, std::size_t length) noexcept {
}
}

int APS5_VABI mprotect_nid_postfix(void* address, std::size_t length, int protection) noexcept {
const auto failed = [](int error) {
SetError(error);
return -1;
};
const auto start = reinterpret_cast<std::uintptr_t>(address);
if (length == 0) return 0;
if (start == 0 || length > std::numeric_limits<std::uintptr_t>::max() - start)
return failed(GuestInvalid);
try {
if (DoMprotect(address, length, protection) != 0) return failed(GuestInvalid);
return 0;
} catch (const std::bad_alloc&) {
return failed(GuestNoMemory);
} catch (const std::exception&) {
return failed(GuestInvalid);
}
}

}
20 changes: 20 additions & 0 deletions core/libs/tests/GuestMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
extern "C" {
void* APS5_VABI mmap_nid_postfix(void*, std::size_t, int, int, int, std::int64_t) noexcept;
int APS5_VABI munmap_nid_postfix(void*, std::size_t) noexcept;
int APS5_VABI mprotect_nid_postfix(void*, std::size_t, int) noexcept;
int* APS5_VABI __error_nid_postfix();
int APS5_VABI sceKernelMapNamedFlexibleMemory(void**, std::size_t, int, int, const char*);
int APS5_VABI sceKernelMapNamedFlexibleMemoryInternal(void**, std::size_t, int, int, const char*);
Expand Down Expand Up @@ -1099,6 +1100,20 @@ int main() {
const auto range = mutation.Find(memory);
Require(range.bytes == page * 3 && range.readable && range.writable);
}
Require(mprotect_nid_postfix(memory, 0, 1) == 0);
Require(mprotect_nid_postfix(nullptr, page, 1) == -1 && *__error_nid_postfix() == 22);
Require(mprotect_nid_postfix(memory + 1, 1, 1) == 0);
{
GuestAllocations::Mutation mutation;
const auto range = mutation.Find(memory);
Require(range.readable && !range.writable);
}
Require(memory[0] == 42);
Require(mprotect_nid_postfix(memory, page, 3) == 0);
{
GuestAllocations::Mutation mutation;
Require(mutation.Find(memory).writable);
}
Require(munmap_nid_postfix(memory + 1, page) == -1 && *__error_nid_postfix() == 22);
Require(munmap_nid_postfix(memory, 0) == -1 && *__error_nid_postfix() == 22);
Require(memory[0] == 42);
Expand All @@ -1108,6 +1123,11 @@ int main() {
Require(memory[page * 2] == 73);
Require(munmap_nid_postfix(memory + page * 2, page) == 0);
Require(munmap_nid_postfix(memory, page) == -1);
*__error_nid_postfix() = 0;
Require(mprotect_nid_postfix(memory, page, 1) == -1 && *__error_nid_postfix() == 22);
*__error_nid_postfix() = 0;
Require(mprotect_nid_postfix(memory, std::numeric_limits<std::size_t>::max(), 1) == -1 &&
*__error_nid_postfix() == 22);
for (int protection : {0, 1, 3, 5}) {
void* mapped = mmap_nid_postfix(memory, 1, protection, 0x1002, -1, 0);
Require(mapped != failed);
Expand Down
Loading