shim_malloc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * shim_malloc.c
  15. *
  16. * This file implements page allocation for the library OS-internal SLAB
  17. * memory allocator. The slab allocator is in Pal/lib/slabmgr.h.
  18. *
  19. * When existing slabs are not sufficient, or a large (4k or greater)
  20. * allocation is requested, it ends up here (__system_alloc and __system_free).
  21. */
  22. #include <asm/mman.h>
  23. #include <pal.h>
  24. #include <pal_debug.h>
  25. #include <shim_checkpoint.h>
  26. #include <shim_internal.h>
  27. #include <shim_profile.h>
  28. #include <shim_utils.h>
  29. #include <shim_vma.h>
  30. static struct shim_lock slab_mgr_lock;
  31. #define SYSTEM_LOCK() lock(&slab_mgr_lock)
  32. #define SYSTEM_UNLOCK() unlock(&slab_mgr_lock)
  33. #define PAGE_SIZE allocsize
  34. #ifdef SLAB_DEBUG_TRACE
  35. #define SLAB_DEBUG
  36. #endif
  37. #define SLAB_CANARY
  38. #define STARTUP_SIZE 16
  39. #include <slabmgr.h>
  40. static SLAB_MGR slab_mgr = NULL;
  41. DEFINE_PROFILE_CATEGORY(memory, );
  42. /* Returns NULL on failure */
  43. void* __system_malloc(size_t size) {
  44. size_t alloc_size = ALIGN_UP(size);
  45. void* addr;
  46. void* ret_addr;
  47. int flags = MAP_PRIVATE | MAP_ANONYMOUS | VMA_INTERNAL;
  48. /*
  49. * If vmas are initialized, we need to request a free address range
  50. * using bkeep_unmapped_any(). The current mmap code uses this function
  51. * to synchronize all address allocation, via a "publication"
  52. * pattern. It is not safe to just call DkVirtualMemoryAlloc directly
  53. * without reserving the vma region first.
  54. */
  55. addr = bkeep_unmapped_any(alloc_size, PROT_READ | PROT_WRITE, flags, NULL, 0, "slab");
  56. if (!addr)
  57. return NULL;
  58. do {
  59. ret_addr = DkVirtualMemoryAlloc(addr, alloc_size, 0, PAL_PROT_WRITE | PAL_PROT_READ);
  60. if (!ret_addr) {
  61. /* If the allocation is interrupted by signal, try to handle the
  62. * signal and then retry the allocation. */
  63. if (PAL_NATIVE_ERRNO == PAL_ERROR_INTERRUPTED) {
  64. handle_signal();
  65. continue;
  66. }
  67. debug("failed to allocate memory (%ld)\n", -PAL_ERRNO);
  68. bkeep_munmap(addr, alloc_size, flags);
  69. return NULL;
  70. }
  71. } while (!ret_addr);
  72. assert(addr == ret_addr);
  73. return addr;
  74. }
  75. void __system_free(void* addr, size_t size) {
  76. DkVirtualMemoryFree(addr, ALIGN_UP(size));
  77. if (bkeep_munmap(addr, ALIGN_UP(size), VMA_INTERNAL) < 0)
  78. BUG();
  79. }
  80. int init_slab(void) {
  81. create_lock(&slab_mgr_lock);
  82. slab_mgr = create_slab_mgr();
  83. return 0;
  84. }
  85. EXTERN_ALIAS(init_slab);
  86. int reinit_slab(void) {
  87. if (slab_mgr) {
  88. destroy_slab_mgr(slab_mgr);
  89. slab_mgr = NULL;
  90. }
  91. return 0;
  92. }
  93. DEFINE_PROFILE_OCCURENCE(malloc_0, memory);
  94. DEFINE_PROFILE_OCCURENCE(malloc_1, memory);
  95. DEFINE_PROFILE_OCCURENCE(malloc_2, memory);
  96. DEFINE_PROFILE_OCCURENCE(malloc_3, memory);
  97. DEFINE_PROFILE_OCCURENCE(malloc_4, memory);
  98. DEFINE_PROFILE_OCCURENCE(malloc_5, memory);
  99. DEFINE_PROFILE_OCCURENCE(malloc_6, memory);
  100. DEFINE_PROFILE_OCCURENCE(malloc_7, memory);
  101. DEFINE_PROFILE_OCCURENCE(malloc_big, memory);
  102. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  103. void* __malloc_debug(size_t size, const char* file, int line)
  104. #else
  105. void* malloc(size_t size)
  106. #endif
  107. {
  108. #ifdef PROFILE
  109. int level = -1;
  110. for (size_t i = 0; i < SLAB_LEVEL; i++)
  111. if (size < slab_levels[i]) {
  112. level = i;
  113. break;
  114. }
  115. switch (level) {
  116. case 0:
  117. INC_PROFILE_OCCURENCE(malloc_0);
  118. break;
  119. case 1:
  120. INC_PROFILE_OCCURENCE(malloc_1);
  121. break;
  122. case 2:
  123. INC_PROFILE_OCCURENCE(malloc_2);
  124. break;
  125. case 3:
  126. INC_PROFILE_OCCURENCE(malloc_3);
  127. break;
  128. case 4:
  129. INC_PROFILE_OCCURENCE(malloc_4);
  130. break;
  131. case 5:
  132. INC_PROFILE_OCCURENCE(malloc_5);
  133. break;
  134. case 6:
  135. INC_PROFILE_OCCURENCE(malloc_6);
  136. break;
  137. case 7:
  138. INC_PROFILE_OCCURENCE(malloc_7);
  139. break;
  140. case -1:
  141. INC_PROFILE_OCCURENCE(malloc_big);
  142. break;
  143. }
  144. #endif
  145. #ifdef SLAB_DEBUG_TRACE
  146. void* mem = slab_alloc_debug(slab_mgr, size, file, line);
  147. #else
  148. void* mem = slab_alloc(slab_mgr, size);
  149. #endif
  150. if (!mem) {
  151. /*
  152. * Normally, the library OS should not run out of memory.
  153. * If malloc() failed internally, we cannot handle the
  154. * condition and must terminate the current process.
  155. */
  156. SYS_PRINTF("******** Out-of-memory in library OS ********\n");
  157. __abort();
  158. }
  159. #ifdef SLAB_DEBUG_PRINT
  160. debug("malloc(%d) = %p (%s:%d)\n", size, mem, file, line);
  161. #endif
  162. return mem;
  163. }
  164. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLAB_DEBUG_TRACE)
  165. EXTERN_ALIAS(malloc);
  166. #endif
  167. void* calloc(size_t nmemb, size_t size) {
  168. // This overflow checking is not a UB, because the operands are unsigned.
  169. size_t total = nmemb * size;
  170. if (total / size != nmemb)
  171. return NULL;
  172. void* ptr = malloc(total);
  173. if (ptr)
  174. memset(ptr, 0, total);
  175. return ptr;
  176. }
  177. EXTERN_ALIAS(calloc);
  178. #if 0 /* Temporarily disabling this code */
  179. void * realloc(void * ptr, size_t new_size)
  180. {
  181. /* TODO: We can't deal with this case right now */
  182. assert(!memory_migrated(ptr));
  183. size_t old_size = slab_get_buf_size(slab_mgr, ptr);
  184. /*
  185. * TODO: this realloc() implementation follows the GLIBC design, which
  186. * will avoid reallocation when the buffer is large enough. Potentially
  187. * this design can cause memory draining if user resizes an extremely
  188. * large object to much smaller.
  189. */
  190. if (old_size >= new_size)
  191. return ptr;
  192. void * new_buf = malloc(new_size);
  193. if (!new_buf)
  194. return NULL;
  195. memcpy(new_buf, ptr, old_size);
  196. /* realloc() does not zero the rest of the object */
  197. free(ptr);
  198. return new_buf;
  199. }
  200. EXTERN_ALIAS(realloc);
  201. #endif
  202. // Copies data from `mem` to a newly allocated buffer of a specified size.
  203. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  204. void* __malloc_copy_debug(const void* mem, size_t size, const char* file, int line)
  205. #else
  206. void* malloc_copy(const void* mem, size_t size)
  207. #endif
  208. {
  209. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  210. void* buff = __malloc_debug(size, file, line);
  211. #else
  212. void* buff = malloc(size);
  213. #endif
  214. if (buff)
  215. memcpy(buff, mem, size);
  216. return buff;
  217. }
  218. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  219. EXTERN_ALIAS(malloc_copy);
  220. #endif
  221. DEFINE_PROFILE_OCCURENCE(free_0, memory);
  222. DEFINE_PROFILE_OCCURENCE(free_1, memory);
  223. DEFINE_PROFILE_OCCURENCE(free_2, memory);
  224. DEFINE_PROFILE_OCCURENCE(free_3, memory);
  225. DEFINE_PROFILE_OCCURENCE(free_4, memory);
  226. DEFINE_PROFILE_OCCURENCE(free_5, memory);
  227. DEFINE_PROFILE_OCCURENCE(free_6, memory);
  228. DEFINE_PROFILE_OCCURENCE(free_7, memory);
  229. DEFINE_PROFILE_OCCURENCE(free_big, memory);
  230. DEFINE_PROFILE_OCCURENCE(free_migrated, memory);
  231. #if defined(SLAB_DEBUG_PRINT) || defined(SLABD_DEBUG_TRACE)
  232. void __free_debug(void* mem, const char* file, int line)
  233. #else
  234. void free(void* mem)
  235. #endif
  236. {
  237. if (!mem)
  238. return;
  239. if (memory_migrated(mem)) {
  240. INC_PROFILE_OCCURENCE(free_migrated);
  241. return;
  242. }
  243. #ifdef PROFILE
  244. int level = RAW_TO_LEVEL(mem);
  245. switch (level) {
  246. case 0:
  247. INC_PROFILE_OCCURENCE(free_0);
  248. break;
  249. case 1:
  250. INC_PROFILE_OCCURENCE(free_1);
  251. break;
  252. case 2:
  253. INC_PROFILE_OCCURENCE(free_2);
  254. break;
  255. case 3:
  256. INC_PROFILE_OCCURENCE(free_3);
  257. break;
  258. case 4:
  259. INC_PROFILE_OCCURENCE(free_4);
  260. break;
  261. case 5:
  262. INC_PROFILE_OCCURENCE(free_5);
  263. break;
  264. case 6:
  265. INC_PROFILE_OCCURENCE(free_6);
  266. break;
  267. case 7:
  268. INC_PROFILE_OCCURENCE(free_7);
  269. break;
  270. case -1:
  271. case 255:
  272. INC_PROFILE_OCCURENCE(free_big);
  273. break;
  274. }
  275. #endif
  276. #ifdef SLAB_DEBUG_PRINT
  277. debug("free(%p) (%s:%d)\n", mem, file, line);
  278. #endif
  279. #ifdef SLAB_DEBUG_TRACE
  280. slab_free_debug(slab_mgr, mem, file, line);
  281. #else
  282. slab_free(slab_mgr, mem);
  283. #endif
  284. }
  285. #if !defined(SLAB_DEBUG_PRINT) && !defined(SLABD_DEBUG_TRACE)
  286. EXTERN_ALIAS(free);
  287. #endif