Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect memory pool behavior when out of memory/nullptr passed #1485

Open
knkemu opened this issue Nov 5, 2024 · 0 comments
Open

Incorrect memory pool behavior when out of memory/nullptr passed #1485

knkemu opened this issue Nov 5, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@knkemu
Copy link

knkemu commented Nov 5, 2024

sceKernelMemoryPoolExpand can easily run into undefined behavior due to dereference of std::map::end when out of memory

PAddr MemoryManager::PoolExpand(PAddr search_start, PAddr search_end, size_t size, u64 alignment) {
std::scoped_lock lk{mutex};
auto dmem_area = FindDmemArea(search_start);
const auto is_suitable = [&] {
const auto aligned_base = alignment > 0 ? Common::AlignUp(dmem_area->second.base, alignment)
: dmem_area->second.base;
const auto alignment_size = aligned_base - dmem_area->second.base;
const auto remaining_size =
dmem_area->second.size >= alignment_size ? dmem_area->second.size - alignment_size : 0;
return dmem_area->second.is_free && remaining_size >= size;
};
while (!is_suitable() && dmem_area->second.GetEnd() <= search_end) {
dmem_area++;
}
ASSERT_MSG(is_suitable(), "Unable to find free direct memory area: size = {:#x}", size);

Some games can expect to run out of memory and take measures, but shadPS4 currently just runs into undefined behavior and crashes.
I think there's even more examples within that file so I think it should be slightly reworked to handled out of memory scenarios by returning ENOMEM

Furthermore, the following check is present on sceKernelMemoryPoolReserve:

s32 PS4_SYSV_ABI sceKernelMemoryPoolReserve(void* addrIn, size_t len, size_t alignment, int flags,
void** addrOut) {
LOG_INFO(Kernel_Vmm, "addrIn = {}, len = {:#x}, alignment = {:#x}, flags = {:#x}",
fmt::ptr(addrIn), len, alignment, flags);
if (addrIn == nullptr) {
LOG_ERROR(Kernel_Vmm, "Address is invalid!");
return SCE_KERNEL_ERROR_EINVAL;

However, CUSA49295 calls sceKernelMemoryPoolReserve with the first argument as NULL hardcoded - which suggests that recognizing addrIn == nullptr as an invalid parameter is wrong.

@polybiusproxy polybiusproxy self-assigned this Nov 5, 2024
@polybiusproxy polybiusproxy added the bug Something isn't working label Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants