2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

mem_arena: Replace insecure temporary file creation with devshm, importing Dolphin’s code.

This commit is contained in:
Emmanuel Gil Peyrot 2014-08-16 00:46:20 +02:00
parent d1c2b76ad1
commit c91d9fd837

View file

@ -19,6 +19,7 @@
#include "common/memory_util.h" #include "common/memory_util.h"
#include "common/mem_arena.h" #include "common/mem_arena.h"
#include "common/string_util.h"
#ifndef _WIN32 #ifndef _WIN32
#include <sys/stat.h> #include <sys/stat.h>
@ -98,15 +99,7 @@ int ashmem_unpin_region(int fd, size_t offset, size_t len)
#endif // Android #endif // Android
#if defined(_WIN32) && !defined(_XBOX)
#ifndef _WIN32
// do not make this "static"
#if defined(MAEMO) || defined(MEEGO_EDITION_HARMATTAN)
std::string ram_temp_file = "/home/user/gc_mem.tmp";
#else
std::string ram_temp_file = "/tmp/gc_mem.tmp";
#endif
#elif !defined(_XBOX)
SYSTEM_INFO sysInfo; SYSTEM_INFO sysInfo;
#endif #endif
@ -145,20 +138,26 @@ void MemArena::GrabLowMemSpace(size_t size)
return; return;
} }
#else #else
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // Try to find a non-existing filename for our shared memory.
fd = open(ram_temp_file.c_str(), O_RDWR | O_CREAT, mode); // In most cases the first one will be available, but it's nicer to search
if (fd < 0) // a bit more.
for (int i = 0; i < 10000; i++)
{ {
ERROR_LOG(MEMMAP, "Failed to grab memory space as a file: %s of size: %08x errno: %d", ram_temp_file.c_str(), (int)size, (int)(errno)); std::string file_name = StringFromFormat("/citramem.%d", i);
fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd != -1)
{
shm_unlink(file_name.c_str());
break;
}
else if (errno != EEXIST)
{
ERROR_LOG(MEMMAP, "shm_open failed: %s", strerror(errno));
return; return;
} }
// delete immediately, we keep the fd so it still lives
unlink(ram_temp_file.c_str());
if (ftruncate(fd, size) != 0)
{
ERROR_LOG(MEMMAP, "Failed to ftruncate %d to size %08x", (int)fd, (int)size);
} }
return; if (ftruncate(fd, size) < 0)
ERROR_LOG(MEMMAP, "Failed to allocate low memory space");
#endif #endif
} }
@ -198,12 +197,12 @@ void *MemArena::CreateView(s64 offset, size_t size, void *base)
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
MAP_NOSYNC | MAP_NOSYNC |
#endif #endif
((base == 0) ? 0 : MAP_FIXED), fd, offset); ((base == nullptr) ? 0 : MAP_FIXED), fd, offset);
if (retval == MAP_FAILED) if (retval == MAP_FAILED)
{ {
NOTICE_LOG(MEMMAP, "mmap on %s (fd: %d) failed", ram_temp_file.c_str(), (int)fd); NOTICE_LOG(MEMMAP, "mmap failed");
return 0; return nullptr;
} }
return retval; return retval;
#endif #endif