malloc_closure.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * This file is from CPython's Modules/_ctypes/malloc_closure.c
  3. * and has received some edits.
  4. */
  5. #include <ffi.h>
  6. #ifdef MS_WIN32
  7. #include <windows.h>
  8. #else
  9. #include <sys/mman.h>
  10. #include <unistd.h>
  11. # if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
  12. # define MAP_ANONYMOUS MAP_ANON
  13. # endif
  14. #endif
  15. /* On PaX enable kernels that have MPROTECT enable we can't use PROT_EXEC.
  16. This is, apparently, an undocumented change to ffi_prep_closure():
  17. depending on the Linux kernel we're running on, we must give it a
  18. mmap that is either PROT_READ|PROT_WRITE|PROT_EXEC or only
  19. PROT_READ|PROT_WRITE. In the latter case, just trying to obtain a
  20. mmap with PROT_READ|PROT_WRITE|PROT_EXEC would kill our process(!),
  21. but in that situation libffi is fine with only PROT_READ|PROT_WRITE.
  22. There is nothing in the libffi API to know that, though, so we have
  23. to guess by parsing /proc/self/status. "Meh."
  24. */
  25. #ifdef __linux__
  26. #include <stdlib.h>
  27. static int emutramp_enabled = -1;
  28. static int
  29. emutramp_enabled_check (void)
  30. {
  31. char *buf = NULL;
  32. size_t len = 0;
  33. FILE *f;
  34. int ret;
  35. f = fopen ("/proc/self/status", "r");
  36. if (f == NULL)
  37. return 0;
  38. ret = 0;
  39. while (getline (&buf, &len, f) != -1)
  40. if (!strncmp (buf, "PaX:", 4))
  41. {
  42. char emutramp;
  43. if (sscanf (buf, "%*s %*c%c", &emutramp) == 1)
  44. ret = (emutramp == 'E');
  45. break;
  46. }
  47. free (buf);
  48. fclose (f);
  49. return ret;
  50. }
  51. #define is_emutramp_enabled() (emutramp_enabled >= 0 ? emutramp_enabled \
  52. : (emutramp_enabled = emutramp_enabled_check ()))
  53. #else
  54. #define is_emutramp_enabled() 0
  55. #endif
  56. /* 'allocate_num_pages' is dynamically adjusted starting from one
  57. page. It grows by a factor of PAGE_ALLOCATION_GROWTH_RATE. This is
  58. meant to handle both the common case of not needing a lot of pages,
  59. and the rare case of needing many of them. Systems in general have a
  60. limit of how many mmap'd blocks can be open.
  61. */
  62. #define PAGE_ALLOCATION_GROWTH_RATE 1.3
  63. static Py_ssize_t allocate_num_pages = 0;
  64. /* #define MALLOC_CLOSURE_DEBUG */ /* enable for some debugging output */
  65. /******************************************************************/
  66. union mmaped_block {
  67. ffi_closure closure;
  68. union mmaped_block *next;
  69. };
  70. static union mmaped_block *free_list = 0;
  71. static Py_ssize_t _pagesize = 0;
  72. static void more_core(void)
  73. {
  74. union mmaped_block *item;
  75. Py_ssize_t count, i;
  76. /* determine the pagesize */
  77. #ifdef MS_WIN32
  78. if (!_pagesize) {
  79. SYSTEM_INFO systeminfo;
  80. GetSystemInfo(&systeminfo);
  81. _pagesize = systeminfo.dwPageSize;
  82. }
  83. #else
  84. if (!_pagesize) {
  85. #ifdef _SC_PAGESIZE
  86. _pagesize = sysconf(_SC_PAGESIZE);
  87. #else
  88. _pagesize = getpagesize();
  89. #endif
  90. }
  91. #endif
  92. if (_pagesize <= 0)
  93. _pagesize = 4096;
  94. /* bump 'allocate_num_pages' */
  95. allocate_num_pages = 1 + (
  96. (Py_ssize_t)(allocate_num_pages * PAGE_ALLOCATION_GROWTH_RATE));
  97. /* calculate the number of mmaped_blocks to allocate */
  98. count = (allocate_num_pages * _pagesize) / sizeof(union mmaped_block);
  99. /* allocate a memory block */
  100. #ifdef MS_WIN32
  101. item = (union mmaped_block *)VirtualAlloc(NULL,
  102. count * sizeof(union mmaped_block),
  103. MEM_COMMIT,
  104. PAGE_EXECUTE_READWRITE);
  105. if (item == NULL)
  106. return;
  107. #else
  108. {
  109. int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  110. if (is_emutramp_enabled ())
  111. prot &= ~PROT_EXEC;
  112. item = (union mmaped_block *)mmap(NULL,
  113. allocate_num_pages * _pagesize,
  114. prot,
  115. MAP_PRIVATE | MAP_ANONYMOUS,
  116. -1,
  117. 0);
  118. if (item == (void *)MAP_FAILED)
  119. return;
  120. }
  121. #endif
  122. #ifdef MALLOC_CLOSURE_DEBUG
  123. printf("block at %p allocated (%ld bytes), %ld mmaped_blocks\n",
  124. item, (long)(allocate_num_pages * _pagesize), (long)count);
  125. #endif
  126. /* put them into the free list */
  127. for (i = 0; i < count; ++i) {
  128. item->next = free_list;
  129. free_list = item;
  130. ++item;
  131. }
  132. }
  133. /******************************************************************/
  134. /* put the item back into the free list */
  135. static void cffi_closure_free(ffi_closure *p)
  136. {
  137. union mmaped_block *item = (union mmaped_block *)p;
  138. item->next = free_list;
  139. free_list = item;
  140. }
  141. /* return one item from the free list, allocating more if needed */
  142. static ffi_closure *cffi_closure_alloc(void)
  143. {
  144. union mmaped_block *item;
  145. if (!free_list)
  146. more_core();
  147. if (!free_list)
  148. return NULL;
  149. item = free_list;
  150. free_list = item->next;
  151. return &item->closure;
  152. }