paging.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * DUMA - Red-Zone memory allocator.
  3. * Copyright (C) 2002-2009 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
  4. * Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
  5. * Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
  6. * License: GNU GPL (GNU General Public License, see COPYING-GPL)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. * FILE CONTENTS:
  24. * internal implementation file
  25. * contains system/platform dependent paging functions
  26. */
  27. #ifndef DUMA_PAGING_H
  28. #define DUMA_PAGING_H
  29. /*
  30. * Lots of systems are missing the definition of PROT_NONE.
  31. */
  32. #ifndef PROT_NONE
  33. #define PROT_NONE 0
  34. #endif
  35. /*
  36. * 386 BSD has MAP_ANON instead of MAP_ANONYMOUS.
  37. */
  38. #if (!defined(MAP_ANONYMOUS) && defined(MAP_ANON))
  39. #define MAP_ANONYMOUS MAP_ANON
  40. #endif
  41. /* declarations */
  42. #include "print.h"
  43. /*
  44. * For some reason, I can't find mprotect() in any of the headers on
  45. * IRIX or SunOS 4.1.2
  46. */
  47. /* extern C_LINKAGE int mprotect(void * addr, size_t len, int prot); */
  48. #if !defined(WIN32)
  49. static void *startAddr = (void *)0;
  50. #endif
  51. /* Function: stringErrorReport
  52. *
  53. * Get formatted error string and return. For WIN32
  54. * FormatMessage is used, strerror all else.
  55. */
  56. static const char *stringErrorReport(void) {
  57. #if defined(WIN32)
  58. DWORD LastError;
  59. LPVOID lpMsgBuf;
  60. LastError = GetLastError();
  61. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  62. FORMAT_MESSAGE_IGNORE_INSERTS,
  63. NULL, LastError,
  64. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT) /* Default language */
  65. ,
  66. (LPTSTR)&lpMsgBuf, 0, NULL);
  67. return (char *)lpMsgBuf; /* "Unknown error.\n"; */
  68. #else
  69. #ifndef DUMA_NO_STRERROR
  70. static int failing;
  71. if (!failing) {
  72. const char *str;
  73. failing++;
  74. str = strerror(errno);
  75. failing--;
  76. if (str != NULL)
  77. return str;
  78. }
  79. #endif
  80. return DUMA_strerror(errno);
  81. #endif
  82. }
  83. /* Function: mprotectFailed
  84. *
  85. * Report that VirtualProtect or mprotect failed and abort
  86. * program execution.
  87. */
  88. static void mprotectFailed(void) {
  89. #if defined(WIN32)
  90. DUMA_Abort("VirtualProtect() failed: %s", stringErrorReport());
  91. #else
  92. DUMA_Abort("mprotect() failed: %s.\nCheck README section 'MEMORY USAGE AND "
  93. "EXECUTION SPEED'\n your (Linux) system may limit the number of "
  94. "different page mappings per process",
  95. stringErrorReport());
  96. #endif
  97. }
  98. /* Function: Page_Create
  99. *
  100. * Create memory. Allocates actual memory. Uses
  101. * VirtualAlloc on windows and mmap on unix.
  102. *
  103. * See Also:
  104. * <Page_Delete>
  105. */
  106. static void *Page_Create(size_t size, int exitonfail, int printerror) {
  107. void *allocation;
  108. #if defined(WIN32)
  109. allocation =
  110. VirtualAlloc(NULL /* address of region to reserve or commit */
  111. ,
  112. (DWORD)size /* size of region */
  113. ,
  114. (DWORD)MEM_COMMIT /* type of allocation */
  115. ,
  116. (DWORD)PAGE_READWRITE /* type of access protection */
  117. );
  118. if ((void *)0 == allocation) {
  119. if (exitonfail)
  120. DUMA_Abort("VirtualAlloc(%d) failed: %s", (DUMA_SIZE)size,
  121. stringErrorReport());
  122. else if (printerror)
  123. DUMA_Print("\nDUMA warning: VirtualAlloc(%d) failed: %s", (DUMA_SIZE)size,
  124. stringErrorReport());
  125. }
  126. #elif defined(MAP_ANONYMOUS)
  127. /*
  128. * In this version, "startAddr" is a _hint_, not a demand.
  129. * When the memory I map here is contiguous with other
  130. * mappings, the allocator can coalesce the memory from two
  131. * or more mappings into one large contiguous chunk, and thus
  132. * might be able to find a fit that would not otherwise have
  133. * been possible. I could _force_ it to be contiguous by using
  134. * the MMAP_FIXED flag, but I don't want to stomp on memory mappings
  135. * generated by other software, etc.
  136. */
  137. allocation = mmap(startAddr, (int)size, PROT_READ | PROT_WRITE,
  138. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  139. #ifndef __hpux
  140. /*
  141. * Set the "address hint" for the next mmap() so that it will abut
  142. * the mapping we just created.
  143. *
  144. * HP/UX 9.01 has a kernel bug that makes mmap() fail sometimes
  145. * when given a non-zero address hint, so we'll leave the hint set
  146. * to zero on that system. HP recently told me this is now fixed.
  147. * Someone please tell me when it is probable to assume that most
  148. * of those systems that were running 9.01 have been upgraded.
  149. */
  150. startAddr = allocation + size;
  151. #endif
  152. if (allocation == (void *)-1) {
  153. allocation = (void *)0;
  154. if (exitonfail)
  155. DUMA_Abort("mmap(%d) failed: %s", (DUMA_SIZE)size, stringErrorReport());
  156. else if (printerror)
  157. DUMA_Print("\nDUMA warning: mmap(%d) failed: %s", (DUMA_SIZE)size,
  158. stringErrorReport());
  159. }
  160. #else
  161. static int devZeroFd = -1;
  162. if (devZeroFd == -1) {
  163. devZeroFd = open("/dev/zero", O_RDWR);
  164. if (devZeroFd < 0)
  165. DUMA_Abort("open() on /dev/zero failed: %s", stringErrorReport());
  166. }
  167. /*
  168. * In this version, "startAddr" is a _hint_, not a demand.
  169. * When the memory I map here is contiguous with other
  170. * mappings, the allocator can coalesce the memory from two
  171. * or more mappings into one large contiguous chunk, and thus
  172. * might be able to find a fit that would not otherwise have
  173. * been possible. I could _force_ it to be contiguous by using
  174. * the MMAP_FIXED flag, but I don't want to stomp on memory mappings
  175. * generated by other software, etc.
  176. */
  177. allocation = mmap(startAddr, (int)size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
  178. devZeroFd, 0);
  179. startAddr = allocation + size;
  180. if (allocation == (void *)-1) {
  181. allocation = (void *)0;
  182. if (exitonfail)
  183. DUMA_Abort("mmap(%d) failed: %s", (DUMA_SIZE)size, stringErrorReport());
  184. else if (printerror)
  185. DUMA_Print("\nDUMA warning: mmap(%d) failed: %s", (DUMA_SIZE)size,
  186. stringErrorReport());
  187. }
  188. #endif
  189. // TESTING
  190. // memset((void*)allocation, 0, startAddr);
  191. return (void *)allocation;
  192. }
  193. /* Function: Page_AllowAccess
  194. *
  195. * Allow memory access to allocated memory.
  196. *
  197. * See Also:
  198. * <Page_DenyAccess>
  199. */
  200. void Page_AllowAccess(void *address, size_t size) {
  201. #if defined(WIN32)
  202. SIZE_T OldProtect, retQuery;
  203. MEMORY_BASIC_INFORMATION MemInfo;
  204. size_t tail_size;
  205. BOOL ret;
  206. while (size > 0) {
  207. retQuery = VirtualQuery(address, &MemInfo, sizeof(MemInfo));
  208. if (retQuery < sizeof(MemInfo))
  209. DUMA_Abort("VirtualQuery() failed\n");
  210. tail_size = (size > MemInfo.RegionSize) ? MemInfo.RegionSize : size;
  211. ret = VirtualProtect(
  212. (LPVOID)address /* address of region of committed pages */
  213. ,
  214. (DWORD)tail_size /* size of the region */
  215. ,
  216. (DWORD)PAGE_READWRITE /* desired access protection */
  217. ,
  218. (PDWORD)&OldProtect /* address of variable to get old protection */
  219. );
  220. if (0 == ret)
  221. mprotectFailed();
  222. address = ((char *)address) + tail_size;
  223. size -= tail_size;
  224. }
  225. #else
  226. if (mprotect(address, size, PROT_READ | PROT_WRITE) < 0)
  227. mprotectFailed();
  228. #endif
  229. }
  230. /* Function: Page_DenyAccess
  231. *
  232. * Deny access to allocated memory region.
  233. *
  234. * See Also:
  235. * <Page_AllowAccess>
  236. */
  237. static void Page_DenyAccess(void *address, size_t size) {
  238. #if defined(WIN32)
  239. SIZE_T OldProtect, retQuery;
  240. MEMORY_BASIC_INFORMATION MemInfo;
  241. size_t tail_size;
  242. BOOL ret;
  243. while (size > 0) {
  244. retQuery = VirtualQuery(address, &MemInfo, sizeof(MemInfo));
  245. if (retQuery < sizeof(MemInfo))
  246. DUMA_Abort("VirtualQuery() failed\n");
  247. tail_size = (size > MemInfo.RegionSize) ? MemInfo.RegionSize : size;
  248. ret = VirtualProtect(
  249. (LPVOID)address /* address of region of committed pages */
  250. ,
  251. (DWORD)tail_size /* size of the region */
  252. ,
  253. (DWORD)PAGE_NOACCESS /* desired access protection */
  254. ,
  255. (PDWORD)&OldProtect /* address of variable to get old protection */
  256. );
  257. if (0 == ret)
  258. mprotectFailed();
  259. address = ((char *)address) + tail_size;
  260. size -= tail_size;
  261. }
  262. #else
  263. if (mprotect(address, size, PROT_NONE) < 0)
  264. mprotectFailed();
  265. #endif
  266. }
  267. /* Function: Page_Delete
  268. *
  269. * Free's DUMA allocated memory. This is the real deal, make sure
  270. * the page is no longer in our slot list first!
  271. *
  272. * See Also:
  273. * <Page_Create>
  274. */
  275. static void Page_Delete(void *address, size_t size) {
  276. #if defined(WIN32)
  277. void *alloc_address = address;
  278. SIZE_T retQuery;
  279. MEMORY_BASIC_INFORMATION MemInfo;
  280. BOOL ret;
  281. /* release physical memory commited to virtual address space */
  282. while (size > 0) {
  283. retQuery = VirtualQuery(address, &MemInfo, sizeof(MemInfo));
  284. if (retQuery < sizeof(MemInfo))
  285. DUMA_Abort("VirtualQuery() failed\n");
  286. if (MemInfo.State == MEM_COMMIT) {
  287. ret =
  288. VirtualFree((LPVOID)MemInfo.BaseAddress /* base of committed pages */
  289. ,
  290. (DWORD)MemInfo.RegionSize /* size of the region */
  291. ,
  292. (DWORD)MEM_DECOMMIT /* type of free operation */
  293. );
  294. if (0 == ret)
  295. DUMA_Abort("VirtualFree(,,MEM_DECOMMIT) failed: %s",
  296. stringErrorReport());
  297. }
  298. address = ((char *)address) + MemInfo.RegionSize;
  299. size -= MemInfo.RegionSize;
  300. }
  301. /* release virtual address space */
  302. ret = VirtualFree((LPVOID)alloc_address, (DWORD)0, (DWORD)MEM_RELEASE);
  303. if (0 == ret)
  304. DUMA_Abort("VirtualFree(,,MEM_RELEASE) failed: %s", stringErrorReport());
  305. #else
  306. if (munmap(address, size) < 0)
  307. Page_DenyAccess(address, size);
  308. #endif
  309. }
  310. /* Function: Page_Size
  311. *
  312. * Retrieve page size.
  313. */
  314. static size_t Page_Size(void) {
  315. #if defined(WIN32)
  316. SYSTEM_INFO SystemInfo;
  317. GetSystemInfo(&SystemInfo);
  318. return (size_t)SystemInfo.dwPageSize;
  319. #elif defined(_SC_PAGESIZE)
  320. return (size_t)sysconf(_SC_PAGESIZE);
  321. #elif defined(_SC_PAGE_SIZE)
  322. return (size_t)sysconf(_SC_PAGE_SIZE);
  323. #else
  324. return getpagesize();
  325. #endif
  326. }
  327. #endif /* DUMA_PAGING_H */