block-mmap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* s2 - allocate pages from the kernel
  2. Copyright (C) 2018 Ariadne Devos
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <sHT/block.h>
  14. #include <sHT/compiler.h>
  15. #include <sHT/nospec.h>
  16. #ifdef _WIN32
  17. # include <memoryapi.h>
  18. # include <winnt.h>
  19. #endif
  20. void *
  21. sHT_block_alloc(size_t size)
  22. {
  23. /* TODO: use large pages? */
  24. #ifdef _WIN32
  25. return VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  26. #else
  27. /* Don't test @code{size != 0}, as mmap(2) will fail anyway otherwise
  28. and array allocation is most likely not conditional. */
  29. int prot = PROT_READ | PROT_WRITE;
  30. int flags = MAP_PRIVATE | MAP_ANONYMOUS;
  31. # ifdef MAP_POPULATE
  32. flags |= MAP_POPULATE;
  33. # endif
  34. # ifdef MAP_UNINITIALIZED
  35. flags |= MAP_UNINITIALIZED;
  36. # endif
  37. return mmap(NULL, size, prot, flags, -1, 0);
  38. #endif
  39. }
  40. void
  41. sHT_block_free(void *block, size_t size)
  42. {
  43. #ifdef _WIN32
  44. /* Mingw header files are inconsistent in the type of BOOL.
  45. Pick something safe. */
  46. long ret = VirtualFree(block, 0, MEM_RELEASE);
  47. if (sHT_test_hidden(ret, ret != 0))
  48. sHT_halt("freeing unallocated memory (perhaps a double-free?)");
  49. #else
  50. /* Check that @var{block} is actually points to something. On systems
  51. with a MMU, this will probably catch many double-free cases, as the
  52. pointer then doesn't point to anything. On architectures that do
  53. not allow unaligned access, this may catch pointer corruptions. */
  54. sHT_hide_var(block);
  55. sHT_depend(block, *(int *) block);
  56. sHT_hide_var(block);
  57. munmap(block, size);
  58. #endif
  59. }
  60. _Bool
  61. sHT_block_alloc_batch(size_t n, void *dest[], const size_t sizes[])
  62. {
  63. /** @var{i} is supposedly-dependent upon all tried allocations.
  64. The return value is supposedly-dependent upon @var{i} when it
  65. is zero (such that on despeculation, the allocated objects are
  66. freshly allocated).
  67. Using @var{i} instead of a variable holding the return value
  68. reduces register therefore stack usage. */
  69. size_t i;
  70. for (i = 0; sHT_test_hidden2(i, n, i < n); i++) {
  71. /* Don't perform speculative out-of-bound writes.
  72. Speculative double-allocation is not problematic. */
  73. i = sHT_index_nospec(i, n);
  74. void *val = sHT_block_alloc(sizes[i]);
  75. if (sHT_unlikely(sHT_test_hidden(val, val == sHT_BLOCK_ALLOC_FAILED)))
  76. goto oom;
  77. sHT_depend(i, val);
  78. dest[i] = val;
  79. }
  80. return sHT_depending(0, i);
  81. oom:
  82. /* Speculative OOMs or memory leaks are not problematic,
  83. so do not track supposed dependencies or despeculate. */
  84. sHT_block_free_batch(i, dest, sizes);
  85. return 1;
  86. }
  87. void
  88. sHT_block_free_batch(const size_t n, void *const dest[], const size_t sizes[])
  89. {
  90. /* Free in reverse order of allocation, as the fresher entries are
  91. more likely to be in the cache than old. */
  92. size_t i = n;
  93. while (sHT_test_hidden(i, i-- > 0)) {
  94. /* When a block has been unmapped, its address may be reused
  95. for a new block. So don't double free, as concurrency is
  96. allowed. */
  97. sHT_despeculate(i);
  98. i = sHT_index_nospec(i, n);
  99. sHT_block_free(dest[i], sizes[i]);
  100. }
  101. }