memory.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * WinPR: Windows Portable Runtime
  3. * Memory Functions
  4. *
  5. * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <winpr/crt.h>
  23. #include <winpr/memory.h>
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. /**
  28. * api-ms-win-core-memory-l1-1-2.dll:
  29. *
  30. * AllocateUserPhysicalPages
  31. * AllocateUserPhysicalPagesNuma
  32. * CreateFileMappingFromApp
  33. * CreateFileMappingNumaW
  34. * CreateFileMappingW
  35. * CreateMemoryResourceNotification
  36. * FlushViewOfFile
  37. * FreeUserPhysicalPages
  38. * GetLargePageMinimum
  39. * GetMemoryErrorHandlingCapabilities
  40. * GetProcessWorkingSetSizeEx
  41. * GetSystemFileCacheSize
  42. * GetWriteWatch
  43. * MapUserPhysicalPages
  44. * MapViewOfFile
  45. * MapViewOfFileEx
  46. * MapViewOfFileFromApp
  47. * OpenFileMappingW
  48. * PrefetchVirtualMemory
  49. * QueryMemoryResourceNotification
  50. * ReadProcessMemory
  51. * RegisterBadMemoryNotification
  52. * ResetWriteWatch
  53. * SetProcessWorkingSetSizeEx
  54. * SetSystemFileCacheSize
  55. * UnmapViewOfFile
  56. * UnmapViewOfFileEx
  57. * UnregisterBadMemoryNotification
  58. * VirtualAlloc
  59. * VirtualAllocEx
  60. * VirtualAllocExNuma
  61. * VirtualFree
  62. * VirtualFreeEx
  63. * VirtualLock
  64. * VirtualProtect
  65. * VirtualProtectEx
  66. * VirtualQuery
  67. * VirtualQueryEx
  68. * VirtualUnlock
  69. * WriteProcessMemory
  70. */
  71. #ifndef _WIN32
  72. #include <errno.h>
  73. #include <sys/mman.h>
  74. #include "memory.h"
  75. #include <winpr/wlog.h>
  76. #include "../log.h"
  77. #define TAG WINPR_TAG("memory")
  78. HANDLE CreateFileMappingA(HANDLE hFile, LPSECURITY_ATTRIBUTES lpAttributes, DWORD flProtect,
  79. DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCSTR lpName)
  80. {
  81. if (hFile != INVALID_HANDLE_VALUE)
  82. {
  83. return NULL; /* not yet implemented */
  84. }
  85. return NULL;
  86. }
  87. HANDLE CreateFileMappingW(HANDLE hFile, LPSECURITY_ATTRIBUTES lpAttributes, DWORD flProtect,
  88. DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCWSTR lpName)
  89. {
  90. return NULL;
  91. }
  92. HANDLE OpenFileMappingA(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCSTR lpName)
  93. {
  94. return NULL;
  95. }
  96. HANDLE OpenFileMappingW(DWORD dwDesiredAccess, BOOL bInheritHandle, LPCWSTR lpName)
  97. {
  98. return NULL;
  99. }
  100. LPVOID MapViewOfFile(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh,
  101. DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap)
  102. {
  103. return NULL;
  104. }
  105. LPVOID MapViewOfFileEx(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh,
  106. DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress)
  107. {
  108. return NULL;
  109. }
  110. BOOL FlushViewOfFile(LPCVOID lpBaseAddress, SIZE_T dwNumberOfBytesToFlush)
  111. {
  112. return TRUE;
  113. }
  114. BOOL UnmapViewOfFile(LPCVOID lpBaseAddress)
  115. {
  116. return TRUE;
  117. }
  118. LPVOID VirtualAlloc(LPVOID lpAddress, DWORD dwSize, DWORD flAllocationType, DWORD flProtect)
  119. {
  120. size_t len = (size_t)dwSize;
  121. int prot = PROT_READ | PROT_WRITE; // PROT_NONE;
  122. int flag = MAP_ANONYMOUS;
  123. void* result = (void*)(-1);
  124. if (lpAddress != NULL) {
  125. flag |= MAP_FIXED;
  126. }
  127. if((flAllocationType & MEM_RESERVE) == MEM_RESERVE) {
  128. }
  129. if((flProtect & PAGE_NOACCESS) == PAGE_NOACCESS) {
  130. flag |= (MAP_PRIVATE | MAP_ANON);
  131. } else {
  132. flag |= MAP_SHARED;
  133. }
  134. result = mmap(lpAddress, len, prot, flag, -1, 0);
  135. return result;
  136. }
  137. BOOL VirtualFree(LPVOID lpAddress, DWORD dwSize, DWORD dwFreeType)
  138. {
  139. if(0 == munmap(lpAddress, (size_t)dwSize)) {
  140. return TRUE;
  141. }
  142. WLog_ERR(TAG, "munmap failed: %s", strerror(errno));
  143. return FALSE;
  144. }
  145. #endif