malloca.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Safe automatic memory allocation.
  2. Copyright (C) 2003, 2006-2007, 2009-2024 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003, 2018.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #define _GL_USE_STDLIB_ALLOC 1
  15. #include <config.h>
  16. /* Specification. */
  17. #include "malloca.h"
  18. #include <stdckdint.h>
  19. #if defined __CHERI_PURE_CAPABILITY__
  20. # error #include <cheri.h>
  21. #endif
  22. #include "idx.h"
  23. /* The speed critical point in this file is freea() applied to an alloca()
  24. result: it must be fast, to match the speed of alloca(). The speed of
  25. mmalloca() and freea() in the other case are not critical, because they
  26. are only invoked for big memory sizes.
  27. Here we use a bit in the address as an indicator, an idea by Ondřej Bílka.
  28. malloca() can return three types of pointers:
  29. - Pointers ≡ 0 mod 2*sa_alignment_max come from stack allocation.
  30. - Pointers ≡ sa_alignment_max mod 2*sa_alignment_max come from heap
  31. allocation.
  32. - NULL comes from a failed heap allocation. */
  33. #if defined __CHERI_PURE_CAPABILITY__
  34. /* Type for holding the original malloc() result. */
  35. typedef uintptr_t small_t;
  36. #else
  37. /* Type for holding very small pointer differences. */
  38. typedef unsigned char small_t;
  39. /* Verify that it is wide enough. */
  40. static_assert (2 * sa_alignment_max - 1 <= (small_t) -1);
  41. #endif
  42. void *
  43. mmalloca (size_t n)
  44. {
  45. #if HAVE_ALLOCA
  46. /* Allocate one more word, used to determine the address to pass to freea(),
  47. and room for the alignment ≡ sa_alignment_max mod 2*sa_alignment_max. */
  48. uintptr_t alignment2_mask = 2 * sa_alignment_max - 1;
  49. int plus = sizeof (small_t) + alignment2_mask;
  50. idx_t nplus;
  51. if (!ckd_add (&nplus, n, plus) && !xalloc_oversized (nplus, 1))
  52. {
  53. char *mem = (char *) malloc (nplus);
  54. if (mem != NULL)
  55. {
  56. uintptr_t umem = (uintptr_t) mem;
  57. /* The ckd_add avoids signed integer overflow on
  58. theoretical platforms where UINTPTR_MAX <= INT_MAX. */
  59. uintptr_t umemplus;
  60. ckd_add (&umemplus, umem, sizeof (small_t) + sa_alignment_max - 1);
  61. idx_t offset = (umemplus - umemplus % (2 * sa_alignment_max)
  62. + sa_alignment_max - umem);
  63. void *p = mem + offset;
  64. /* Here p >= mem + sizeof (small_t),
  65. and p <= mem + sizeof (small_t) + 2 * sa_alignment_max - 1
  66. hence p + n <= mem + nplus.
  67. So, the memory range [p, p+n) lies in the allocated memory range
  68. [mem, mem + nplus). */
  69. small_t *sp = p;
  70. # if defined __CHERI_PURE_CAPABILITY__
  71. sp[-1] = umem;
  72. p = (char *) cheri_bounds_set ((char *) p - sizeof (small_t),
  73. sizeof (small_t) + n)
  74. + sizeof (small_t);
  75. # else
  76. sp[-1] = offset;
  77. # endif
  78. /* p ≡ sa_alignment_max mod 2*sa_alignment_max. */
  79. return p;
  80. }
  81. }
  82. /* Out of memory. */
  83. return NULL;
  84. #else
  85. # if !MALLOC_0_IS_NONNULL
  86. if (n == 0)
  87. n = 1;
  88. # endif
  89. return malloc (n);
  90. #endif
  91. }
  92. #if HAVE_ALLOCA
  93. void
  94. freea (void *p)
  95. {
  96. /* Check argument. */
  97. uintptr_t u = (uintptr_t) p;
  98. if (u & (sa_alignment_max - 1))
  99. {
  100. /* p was not the result of a malloca() call. Invalid argument. */
  101. abort ();
  102. }
  103. /* Determine whether p was a non-NULL pointer returned by mmalloca(). */
  104. if (u & sa_alignment_max)
  105. {
  106. char *cp = p;
  107. small_t *sp = p;
  108. # if defined __CHERI_PURE_CAPABILITY__
  109. void *mem = sp[-1];
  110. # else
  111. void *mem = cp - sp[-1];
  112. # endif
  113. free (mem);
  114. }
  115. }
  116. #endif
  117. /*
  118. * Hey Emacs!
  119. * Local Variables:
  120. * coding: utf-8
  121. * End:
  122. */