stdlib.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include <stdlib.h>
  2. #include <sys/mman.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <alloca.h>
  7. #include <assert.h>
  8. #include <AK/Assertions.h>
  9. #include <AK/Types.h>
  10. extern "C" {
  11. #define MALLOC_SCRUB_BYTE 0x85
  12. #define FREE_SCRUB_BYTE 0x82
  13. struct MallocHeader {
  14. uint16_t first_chunk_index;
  15. uint16_t chunk_count;
  16. size_t size;
  17. uint32_t compute_xorcheck() const
  18. {
  19. return 0x19820413 ^ ((first_chunk_index << 16) | chunk_count) ^ size;
  20. }
  21. };
  22. struct MallocFooter {
  23. uint32_t xorcheck;
  24. };
  25. #define CHUNK_SIZE 8
  26. #define POOL_SIZE 128 * 1024
  27. static const size_t malloc_budget = POOL_SIZE;
  28. static byte s_malloc_map[POOL_SIZE / CHUNK_SIZE / 8];
  29. static byte* s_malloc_pool;
  30. static uint32_t s_malloc_sum_alloc = 0;
  31. static uint32_t s_malloc_sum_free = POOL_SIZE;
  32. void* malloc(size_t size)
  33. {
  34. // We need space for the MallocHeader structure at the head of the block.
  35. size_t real_size = size + sizeof(MallocHeader) + sizeof(MallocFooter);
  36. if (s_malloc_sum_free < real_size) {
  37. fprintf(stderr, "malloc(): Out of memory\ns_malloc_sum_free=%u, real_size=%x\n", s_malloc_sum_free, real_size);
  38. assert(false);
  39. }
  40. size_t chunks_needed = real_size / CHUNK_SIZE;
  41. if (real_size % CHUNK_SIZE)
  42. chunks_needed++;
  43. size_t chunks_here = 0;
  44. size_t first_chunk = 0;
  45. for (unsigned i = 0; i < (POOL_SIZE / CHUNK_SIZE / 8); ++i) {
  46. if (s_malloc_map[i] == 0xff) {
  47. // Skip over completely full bucket.
  48. chunks_here = 0;
  49. continue;
  50. }
  51. // FIXME: This scan can be optimized further with TZCNT.
  52. for (unsigned j = 0; j < 8; ++j) {
  53. if ((s_malloc_map[i] & (1<<j))) {
  54. // This is in use, so restart chunks_here counter.
  55. chunks_here = 0;
  56. continue;
  57. }
  58. if (chunks_here == 0) {
  59. // Mark where potential allocation starts.
  60. first_chunk = i * 8 + j;
  61. }
  62. ++chunks_here;
  63. if (chunks_here == chunks_needed) {
  64. auto* header = (MallocHeader*)(s_malloc_pool + (first_chunk * CHUNK_SIZE));
  65. byte* ptr = ((byte*)header) + sizeof(MallocHeader);
  66. header->chunk_count = chunks_needed;
  67. header->first_chunk_index = first_chunk;
  68. header->size = size;
  69. auto* footer = (MallocFooter*)((byte*)header + (header->chunk_count * CHUNK_SIZE) - sizeof(MallocFooter));
  70. footer->xorcheck = header->compute_xorcheck();
  71. for (size_t k = first_chunk; k < (first_chunk + chunks_needed); ++k)
  72. s_malloc_map[k / 8] |= 1 << (k % 8);
  73. s_malloc_sum_alloc += header->chunk_count * CHUNK_SIZE;
  74. s_malloc_sum_free -= header->chunk_count * CHUNK_SIZE;
  75. memset(ptr, MALLOC_SCRUB_BYTE, (header->chunk_count * CHUNK_SIZE) - (sizeof(MallocHeader) + sizeof(MallocFooter)));
  76. return ptr;
  77. }
  78. }
  79. }
  80. fprintf(stderr, "malloc(): Out of memory (no consecutive chunks found for size %u)\n", size);
  81. volatile char* crashme = (char*)0xc007d00d;
  82. *crashme = 0;
  83. return nullptr;
  84. }
  85. static void validate_mallocation(void* ptr, const char* func)
  86. {
  87. auto* header = (MallocHeader*)((((byte*)ptr) - sizeof(MallocHeader)));
  88. if (header->size == 0) {
  89. fprintf(stderr, "%s called on bad pointer %p, size=0\n", func, ptr);
  90. assert(false);
  91. }
  92. auto* footer = (MallocFooter*)((byte*)header + (header->chunk_count * CHUNK_SIZE) - sizeof(MallocFooter));
  93. uint32_t expected_xorcheck = header->compute_xorcheck();
  94. if (footer->xorcheck != expected_xorcheck) {
  95. fprintf(stderr, "%s called on bad pointer %p, xorcheck=%w (expected %w)\n", func, ptr, footer->xorcheck, expected_xorcheck);
  96. assert(false);
  97. }
  98. }
  99. void free(void* ptr)
  100. {
  101. if (!ptr)
  102. return;
  103. validate_mallocation(ptr, "free()");
  104. auto* header = (MallocHeader*)((((byte*)ptr) - sizeof(MallocHeader)));
  105. for (unsigned i = header->first_chunk_index; i < (header->first_chunk_index + header->chunk_count); ++i)
  106. s_malloc_map[i / 8] &= ~(1 << (i % 8));
  107. s_malloc_sum_alloc -= header->chunk_count * CHUNK_SIZE;
  108. s_malloc_sum_free += header->chunk_count * CHUNK_SIZE;
  109. memset(header, FREE_SCRUB_BYTE, header->chunk_count * CHUNK_SIZE);
  110. }
  111. void __malloc_init()
  112. {
  113. s_malloc_pool = (byte*)mmap(nullptr, malloc_budget, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  114. int rc = set_mmap_name(s_malloc_pool, malloc_budget, "malloc pool");
  115. if (rc < 0)
  116. perror("set_mmap_name failed");
  117. }
  118. void* calloc(size_t nmemb, size_t)
  119. {
  120. (void) nmemb;
  121. ASSERT_NOT_REACHED();
  122. return nullptr;
  123. }
  124. void* realloc(void *ptr, size_t size)
  125. {
  126. validate_mallocation(ptr, "realloc()");
  127. auto* header = (MallocHeader*)((((byte*)ptr) - sizeof(MallocHeader)));
  128. size_t old_size = header->size;
  129. auto* new_ptr = malloc(size);
  130. memcpy(new_ptr, ptr, old_size);
  131. return new_ptr;
  132. }
  133. void exit(int status)
  134. {
  135. _exit(status);
  136. assert(false);
  137. }
  138. void abort()
  139. {
  140. // FIXME: Implement proper abort().
  141. exit(253);
  142. }
  143. char* getenv(const char* name)
  144. {
  145. for (size_t i = 0; environ[i]; ++i) {
  146. const char* decl = environ[i];
  147. char* eq = strchr(decl, '=');
  148. if (!eq)
  149. continue;
  150. size_t varLength = eq - decl;
  151. char* var = (char*)alloca(varLength + 1);
  152. memcpy(var, decl, varLength);
  153. var[varLength] = '\0';
  154. if (!strcmp(var, name)) {
  155. char* value = eq + 1;
  156. return value;
  157. }
  158. }
  159. return nullptr;
  160. }
  161. int atoi(const char* str)
  162. {
  163. size_t len = strlen(str);
  164. int value = 0;
  165. bool isNegative = false;
  166. for (size_t i = 0; i < len; ++i) {
  167. if (i == 0 && str[0] == '-') {
  168. isNegative = true;
  169. continue;
  170. }
  171. if (str[i] < '0' || str[i] > '9')
  172. return value;
  173. value = value * 10;
  174. value += str[i] - '0';
  175. }
  176. return isNegative ? -value : value;
  177. }
  178. long atol(const char* str)
  179. {
  180. static_assert(sizeof(int) == sizeof(long));
  181. return atoi(str);
  182. }
  183. }