MemoryMappedFile.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "il2cpp-config.h"
  2. #if !IL2CPP_USE_GENERIC_MEMORY_MAPPED_FILE && IL2CPP_TARGET_POSIX && !RUNTIME_TINY
  3. #include <sys/mman.h>
  4. #include <map>
  5. #include "os/File.h"
  6. #include "os/MemoryMappedFile.h"
  7. #include "os/Mutex.h"
  8. #include "utils/Memory.h"
  9. #include "FileHandle.h"
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #ifdef DEFFILEMODE
  13. #define DEFAULT_FILEMODE DEFFILEMODE
  14. #else
  15. #define DEFAULT_FILEMODE 0666
  16. #endif
  17. #ifndef MAP_32BIT
  18. #define MAP_32BIT 0
  19. #endif
  20. #define MMAP_PAGE_SIZE 4096
  21. namespace il2cpp
  22. {
  23. namespace os
  24. {
  25. enum MemoryMappedFileFlags
  26. {
  27. /* protection */
  28. MONO_MMAP_NONE = 0,
  29. MONO_MMAP_READ = 1 << 0,
  30. MONO_MMAP_WRITE = 1 << 1,
  31. MONO_MMAP_EXEC = 1 << 2,
  32. /* make the OS discard the dirty data and fill with 0 */
  33. MONO_MMAP_DISCARD = 1 << 3,
  34. /* other flags (add commit, sync) */
  35. MONO_MMAP_PRIVATE = 1 << 4,
  36. MONO_MMAP_SHARED = 1 << 5,
  37. MONO_MMAP_ANON = 1 << 6,
  38. MONO_MMAP_FIXED = 1 << 7,
  39. MONO_MMAP_32BIT = 1 << 8
  40. };
  41. static bool IsSpecialZeroSizeFile(struct stat *buf)
  42. {
  43. return buf->st_size == 0 && (buf->st_mode & (S_IFCHR | S_IFBLK | S_IFIFO | S_IFSOCK)) != 0;
  44. }
  45. static int64_t AlignUpToPageSize(int64_t size)
  46. {
  47. int64_t page_size = MMAP_PAGE_SIZE;
  48. return (size + page_size - 1) & ~(page_size - 1);
  49. }
  50. static int64_t AlignDownToPageSize(int64_t size)
  51. {
  52. int64_t page_size = MMAP_PAGE_SIZE;
  53. return size & ~(page_size - 1);
  54. }
  55. static int ConvertAccessToMemoryMappedFileFlags(int access)
  56. {
  57. switch (access)
  58. {
  59. case MMAP_FILE_ACCESS_READ_WRITE:
  60. return MONO_MMAP_WRITE | MONO_MMAP_READ | MONO_MMAP_SHARED;
  61. case MMAP_FILE_ACCESS_WRITE:
  62. return MONO_MMAP_WRITE | MONO_MMAP_SHARED;
  63. case MMAP_FILE_ACCESS_COPY_ON_WRITE:
  64. return MONO_MMAP_WRITE | MONO_MMAP_READ | MONO_MMAP_PRIVATE;
  65. case MMAP_FILE_ACCESS_READ_EXECUTE:
  66. return MONO_MMAP_EXEC | MONO_MMAP_PRIVATE | MONO_MMAP_SHARED;
  67. case MMAP_FILE_ACCESS_READ_WRITE_EXECUTE:
  68. return MONO_MMAP_WRITE | MONO_MMAP_READ | MONO_MMAP_EXEC | MONO_MMAP_SHARED;
  69. case MMAP_FILE_ACCESS_READ:
  70. return MONO_MMAP_READ | MONO_MMAP_SHARED;
  71. default:
  72. IL2CPP_ASSERT("unknown MemoryMappedFileAccess");
  73. }
  74. return 0;
  75. }
  76. static int ConvertFileModeToUnix(int mode)
  77. {
  78. switch (mode)
  79. {
  80. case FILE_MODE_CREATE_NEW:
  81. return O_CREAT | O_EXCL;
  82. case FILE_MODE_CREATE:
  83. return O_CREAT | O_TRUNC;
  84. case FILE_MODE_OPEN:
  85. return 0;
  86. case FILE_MODE_OPEN_OR_CREATE:
  87. return O_CREAT;
  88. case FILE_MODE_TRUNCATE:
  89. return O_TRUNC;
  90. case FILE_MODE_APPEND:
  91. return O_APPEND;
  92. default:
  93. IL2CPP_ASSERT("unknown FileMode");
  94. }
  95. return 0;
  96. }
  97. static int ConvertAccessModeToUnix(int access)
  98. {
  99. switch (access)
  100. {
  101. case MMAP_FILE_ACCESS_READ_WRITE:
  102. case MMAP_FILE_ACCESS_COPY_ON_WRITE:
  103. case MMAP_FILE_ACCESS_READ_WRITE_EXECUTE:
  104. return O_RDWR;
  105. case MMAP_FILE_ACCESS_READ:
  106. case MMAP_FILE_ACCESS_READ_EXECUTE:
  107. return O_RDONLY;
  108. case MMAP_FILE_ACCESS_WRITE:
  109. return O_WRONLY;
  110. default:
  111. IL2CPP_ASSERT("unknown MemoryMappedFileAccess");
  112. }
  113. return 0;
  114. }
  115. static int ConvertFlagsToProt(int flags)
  116. {
  117. int prot = PROT_NONE;
  118. /* translate the protection bits */
  119. if (flags & MONO_MMAP_READ)
  120. prot |= PROT_READ;
  121. if (flags & MONO_MMAP_WRITE)
  122. prot |= PROT_WRITE;
  123. if (flags & MONO_MMAP_EXEC)
  124. prot |= PROT_EXEC;
  125. return prot;
  126. }
  127. FileHandle* MemoryMappedFile::Create(FileHandle* file, const char* mapName, int32_t mode, int64_t *capacity, MemoryMappedFileAccess access, int32_t options, MemoryMappedFileError* error)
  128. {
  129. struct stat buf;
  130. int result, fd;
  131. if (file != NULL)
  132. result = fstat(file->fd, &buf);
  133. else
  134. result = stat(mapName, &buf);
  135. if (mode == FILE_MODE_TRUNCATE || mode == FILE_MODE_APPEND || mode == FILE_MODE_OPEN)
  136. {
  137. if (result == -1)
  138. {
  139. if (error != NULL)
  140. *error = FILE_NOT_FOUND;
  141. return NULL;
  142. }
  143. }
  144. if (mode == FILE_MODE_CREATE_NEW && result == 0)
  145. {
  146. if (error != NULL)
  147. *error = FILE_ALREADY_EXISTS;
  148. return NULL;
  149. }
  150. if (result == 0)
  151. {
  152. if (*capacity == 0)
  153. {
  154. /**
  155. * Special files such as FIFOs, sockets, and devices can have a size of 0. Specifying a capacity for these
  156. * also makes little sense, so don't do the check if th file is one of these.
  157. */
  158. if (buf.st_size == 0 && !IsSpecialZeroSizeFile(&buf))
  159. {
  160. if (error != NULL)
  161. *error = CAPACITY_SMALLER_THAN_FILE_SIZE;
  162. return NULL;
  163. }
  164. *capacity = buf.st_size;
  165. }
  166. else if (*capacity < buf.st_size)
  167. {
  168. if (error != NULL)
  169. *error = CAPACITY_SMALLER_THAN_FILE_SIZE;
  170. return NULL;
  171. }
  172. }
  173. else
  174. {
  175. if (mode == FILE_MODE_CREATE_NEW && *capacity == 0)
  176. {
  177. if (error != NULL)
  178. *error = CAPACITY_SMALLER_THAN_FILE_SIZE;
  179. return NULL;
  180. }
  181. }
  182. bool ownsFd = true;
  183. if (file != NULL)
  184. {
  185. #if IL2CPP_HAS_DUP
  186. fd = dup(file->fd);
  187. #else
  188. fd = file->fd;
  189. ownsFd = false;
  190. #endif
  191. }
  192. else
  193. {
  194. fd = open(mapName, ConvertFileModeToUnix(mode) | ConvertAccessModeToUnix(access), DEFAULT_FILEMODE);
  195. }
  196. if (fd == -1)
  197. {
  198. if (error != NULL)
  199. *error = COULD_NOT_OPEN;
  200. return NULL;
  201. }
  202. if (result != 0 || *capacity > buf.st_size)
  203. {
  204. int result = ftruncate(fd, (off_t)*capacity);
  205. IL2CPP_ASSERT(result == 0);
  206. NO_UNUSED_WARNING(result);
  207. }
  208. if (ownsFd)
  209. {
  210. FileHandle* handle = new FileHandle;
  211. handle->fd = fd;
  212. handle->doesNotOwnFd = !ownsFd;
  213. return handle;
  214. }
  215. else
  216. {
  217. file->doesNotOwnFd = true;
  218. return file;
  219. }
  220. }
  221. MemoryMappedFile::MemoryMappedFileHandle MemoryMappedFile::View(FileHandle* mappedFileHandle, int64_t* length, int64_t offset, MemoryMappedFileAccess access, int64_t* actualOffset, MemoryMappedFileError* error)
  222. {
  223. IL2CPP_ASSERT(actualOffset != NULL);
  224. int64_t mmap_offset = 0;
  225. int64_t eff_size = *length;
  226. struct stat buf = { 0 };
  227. fstat(mappedFileHandle->fd, &buf);
  228. if (offset > buf.st_size || ((eff_size + offset) > buf.st_size && !IsSpecialZeroSizeFile(&buf)))
  229. {
  230. if (error != NULL)
  231. *error = ACCESS_DENIED;
  232. return NULL;
  233. }
  234. /**
  235. * We use the file size if one of the following conditions is true:
  236. * -input size is zero
  237. * -input size is bigger than the file and the file is not a magical zero size file such as /dev/mem.
  238. */
  239. if (eff_size == 0)
  240. eff_size = AlignUpToPageSize(buf.st_size) - offset;
  241. mmap_offset = AlignDownToPageSize(offset);
  242. eff_size += (offset - mmap_offset);
  243. *length = eff_size;
  244. *actualOffset = mmap_offset;
  245. int flags = ConvertAccessToMemoryMappedFileFlags(access);
  246. int prot = ConvertFlagsToProt(flags);
  247. /* translate the flags */
  248. int mflags = 0;
  249. if (flags & MONO_MMAP_PRIVATE)
  250. mflags |= MAP_PRIVATE;
  251. if (flags & MONO_MMAP_SHARED)
  252. mflags |= MAP_SHARED;
  253. if (flags & MONO_MMAP_FIXED)
  254. mflags |= MAP_FIXED;
  255. if (flags & MONO_MMAP_32BIT)
  256. mflags |= MAP_32BIT;
  257. void* address = mmap(NULL, eff_size, prot, mflags, mappedFileHandle->fd, mmap_offset);
  258. if (address == MAP_FAILED)
  259. {
  260. if (error != NULL)
  261. *error = COULD_NOT_MAP_MEMORY;
  262. return NULL;
  263. }
  264. return address;
  265. }
  266. void MemoryMappedFile::Flush(MemoryMappedFileHandle memoryMappedFileData, int64_t length)
  267. {
  268. if (memoryMappedFileData != NULL)
  269. {
  270. int error = msync(memoryMappedFileData, length, MS_SYNC);
  271. IL2CPP_ASSERT(error == 0);
  272. NO_UNUSED_WARNING(error);
  273. }
  274. }
  275. bool MemoryMappedFile::UnmapView(MemoryMappedFileHandle memoryMappedFileData, int64_t length)
  276. {
  277. int error = munmap(memoryMappedFileData, (size_t)length);
  278. return error == 0;
  279. }
  280. bool MemoryMappedFile::Close(FileHandle* file)
  281. {
  282. bool result = true;
  283. if (!file->doesNotOwnFd)
  284. {
  285. int error = 0;
  286. os::File::Close(file, &error);
  287. if (error != 0)
  288. result = false;
  289. }
  290. return result;
  291. }
  292. void MemoryMappedFile::ConfigureHandleInheritability(FileHandle* file, bool inheritability)
  293. {
  294. #if IL2CPP_HAS_CLOSE_EXEC
  295. int flags = fcntl(file->fd, F_GETFD, 0);
  296. if (inheritability)
  297. flags &= ~FD_CLOEXEC;
  298. else
  299. flags |= FD_CLOEXEC;
  300. int result = fcntl(file->fd, F_SETFD, flags);
  301. IL2CPP_ASSERT(result != -1);
  302. NO_UNUSED_WARNING(result);
  303. #endif
  304. }
  305. bool MemoryMappedFile::OwnsDuplicatedFileHandle(FileHandle* file)
  306. {
  307. return !file->doesNotOwnFd;
  308. }
  309. }
  310. }
  311. #endif