Skip to content

Commit 3fd18cc

Browse files
committed
Darwin: Don't pass MAP_ANON to mmap when file mapping and mmap ret -1.
This appeared to work under x86, but on ARM this (rightfully) fails.
1 parent a572c80 commit 3fd18cc

2 files changed

Lines changed: 45 additions & 29 deletions

File tree

common/Darwin/DarwinMisc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ void* HostSys::MapMapping(void* handle, size_t size, const PageProtectionMode& m
364364
{
365365
const u32 mmap_prot = (mode.CanWrite() ? (PROT_READ | PROT_WRITE) : (PROT_READ)) | (mode.CanExecute() ? PROT_EXEC : 0);
366366

367-
return mmap(nullptr, size, mmap_prot, MAP_PRIVATE | MAP_ANON, static_cast<int>(reinterpret_cast<intptr_t>(handle)), 0);
367+
return mmap(nullptr, size, mmap_prot, MAP_PRIVATE, static_cast<int>(reinterpret_cast<intptr_t>(handle)), 0);
368368
}
369369

370370
void HostSys::DestroyMapping(void* handle)

pcsx2/SIO/Memcard/MemoryCardFile.cpp

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -338,43 +338,59 @@ void FileMemoryCard::Open()
338338
}
339339

340340
if (!m_file[slot])
341+
goto memoryCardOpenFailed;
342+
343+
m_fileSize[slot] = FileSystem::FSize64(m_file[slot]);
344+
345+
m_mapping_handles[slot] = HostSys::CreateMappingFromFile(m_file[slot]);
346+
if (!m_mapping_handles[slot])
341347
{
342-
Host::ReportErrorAsync(TRANSLATE_SV("MemoryCard", "Memory Card Read Failed"),
343-
fmt::format(TRANSLATE_FS("MemoryCard", "Unable to access memory card:\n\n{}\n\n"
344-
"Another instance of PCSX2 may be using this memory card "
345-
"or the memory card is stored in a write-protected folder.\n"
346-
"Close any other instances of PCSX2, or restart your computer.\n"),
347-
fname));
348+
Console.Warning("MemoryCardFile: CreateMappingFromFile failed!");
349+
goto memoryCardOpenFailed;
348350
}
349-
else // Load memory map and checksum
351+
352+
m_mappings[slot] = static_cast<u8*>(HostSys::MapMapping(m_mapping_handles[slot], m_fileSize[slot], PageAccess_ReadWrite()));
353+
if (!m_mappings[slot] || reinterpret_cast<intptr_t>(m_mappings[slot]) < 0)
350354
{
351-
m_fileSize[slot] = FileSystem::FSize64(m_file[slot]);
355+
Console.Warning("MemoryCardFile: MapSharedMemory failed! %d. %s", errno, strerror(errno));
356+
goto memoryCardOpenFailed;
357+
}
352358

353-
m_mapping_handles[slot] = HostSys::CreateMappingFromFile(m_file[slot]);
354-
if (!m_mapping_handles[slot])
355-
{
356-
Console.Warning("CreateMappingFromFile failed!");
357-
}
359+
Console.WriteLnFmt(Color_Green, "McdSlot {} [File]: {} [{} MB, {}]", slot, Path::GetFileName(fname),
360+
(m_fileSize[slot] + (MCD_SIZE + 1)) / MC2_MBSIZE,
361+
FileMcd_IsMemoryCardFormatted(m_file[slot]) ? "Formatted" : "UNFORMATTED");
358362

359-
m_mappings[slot] = static_cast<u8*>(HostSys::MapMapping(m_mapping_handles[slot], m_fileSize[slot], PageAccess_ReadWrite()));
360-
if (!m_mappings[slot])
361-
{
362-
Console.Warning("MapSharedMemory failed! %d. %s", errno, strerror(errno));
363-
}
363+
m_filenames[slot] = std::move(fname);
364+
m_ispsx[slot] = m_fileSize[slot] == 0x20000;
365+
m_chkaddr = 0x210;
364366

365-
Console.WriteLnFmt(Color_Green, "McdSlot {} [File]: {} [{} MB, {}]", slot, Path::GetFileName(fname),
366-
(m_fileSize[slot] + (MCD_SIZE + 1)) / MC2_MBSIZE,
367-
FileMcd_IsMemoryCardFormatted(m_file[slot]) ? "Formatted" : "UNFORMATTED");
367+
if (!m_ispsx[slot])
368+
{
369+
std::memcpy(&m_chksum[slot], m_mappings[slot] + m_chkaddr, sizeof(m_chksum[slot]));
370+
}
371+
continue;
368372

369-
m_filenames[slot] = std::move(fname);
370-
m_ispsx[slot] = m_fileSize[slot] == 0x20000;
371-
m_chkaddr = 0x210;
373+
memoryCardOpenFailed:
374+
Host::ReportErrorAsync(TRANSLATE_SV("MemoryCard", "Memory Card Read Failed"),
375+
fmt::format(TRANSLATE_FS("MemoryCard", "Unable to access memory card:\n\n{}\n\n"
376+
"Another instance of PCSX2 may be using this memory card "
377+
"or the memory card is stored in a write-protected folder.\n"
378+
"Close any other instances of PCSX2, or restart your computer.\n"),
379+
fname));
372380

373-
if (!m_ispsx[slot])
374-
{
375-
std::memcpy(&m_chksum[slot], m_mappings[slot] + m_chkaddr, sizeof(m_chksum[slot]));
376-
}
381+
if(m_mapping_handles[slot])
382+
{
383+
HostSys::DestroyMapping(m_mapping_handles[slot]);
384+
}
385+
386+
if(m_file[slot])
387+
{
388+
std::fclose(m_file[slot]);
389+
m_file[slot] = nullptr;
377390
}
391+
392+
m_filenames[slot] = {};
393+
m_fileSize[slot] = -1;
378394
}
379395
}
380396

0 commit comments

Comments
 (0)