2
0

alloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include <malloc.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stddef.h> /* for size_t */
  6. #include <stdint.h> /* for SIZE_MAX */
  7. #include <string.h> /* for strerror() */
  8. #include <unistd.h> /* for sysconf() */
  9. #include "test_helpers.h"
  10. /* For regular allocations that should succeed without particular
  11. * alignment requirements. */
  12. void test_non_null(void *ptr, int error_val) {
  13. if (ptr != NULL) {
  14. // Constant output for successful case
  15. printf("pointer: (not NULL), ");
  16. }
  17. else {
  18. printf("pointer: %p, ", ptr);
  19. }
  20. printf("error value: %d = %s\n",
  21. error_val, strerror(error_val));
  22. }
  23. /* For testing functions that should return pointers with a particular
  24. * alignment (successful case). */
  25. void test_valid_aligned(void *ptr, size_t alignment, int error_val) {
  26. /* Cast to uintptr_t to allow taking modulo of address. The
  27. * uintptr_t type is guaranteed to be able to hold any valid object
  28. * address. */
  29. uintptr_t ptr_alignment_rem = (uintptr_t)ptr % (uintptr_t)alignment;
  30. if (ptr != NULL && ptr_alignment_rem == 0) {
  31. // Constant output for successful case
  32. printf("pointer: (alignment OK), ");
  33. }
  34. else {
  35. printf("pointer: %p, ", ptr);
  36. }
  37. printf("error value: %d = %s\n",
  38. error_val, strerror(error_val));
  39. }
  40. /* For testing functions that should return pointers with a particular
  41. * alignment. With invalid alignment, we expect constant output (a NULL
  42. * pointer and EINVAL). */
  43. void test_invalid_aligned(void *ptr, int error_val) {
  44. printf("pointer: %p, error value: %d = %s\n",
  45. ptr, error_val, strerror(error_val));
  46. }
  47. /* For testing size-0 allocation requests. */
  48. void test_size_zero(void *ptr, size_t alignment, int error_val) {
  49. /* Facilitates checking alignment upon non-NULL return */
  50. uintptr_t ptr_alignment_rem = (uintptr_t)ptr % (uintptr_t)alignment;
  51. /* For allocation functions, POSIX permits returning either a NULL
  52. * pointer and optionally an implementation-defined error value, or
  53. * succeeding with a non-NULL pointer. */
  54. if (ptr == NULL || (ptr_alignment_rem == 0 && error_val == 0)) {
  55. // Constant output for successful case
  56. printf("(OK)\n");
  57. }
  58. else {
  59. printf("pointer: %p, error value: %d = %s\n",
  60. ptr, error_val, strerror(error_val));
  61. }
  62. }
  63. /* For cases where we expect allocation to fail, returning a NULL
  64. * pointer and indicating ENOMEM. */
  65. void test_cannot_alloc(void *ptr, int error_val) {
  66. printf("pointer: %p, error value: %d = %s\n",
  67. ptr, error_val, strerror(error_val));
  68. }
  69. int main(void) {
  70. size_t sample_alloc_size = 256;
  71. size_t sample_realloc_size = sample_alloc_size + 1;
  72. /* ensure values are mapped to variables */
  73. size_t zero_size = 0;
  74. size_t max_size = SIZE_MAX;
  75. size_t page_size = (size_t)sysconf(_SC_PAGESIZE);
  76. size_t aligned_alloc_alignment = 128;
  77. size_t aligned_alloc_goodsize = 256;
  78. size_t aligned_alloc_badsize = 257;
  79. size_t nonpow2_mul_voidptr_size = 3*sizeof(void *);
  80. size_t pow2_mul_voidptr_size = 4*sizeof(void *);
  81. int i;
  82. errno = 0;
  83. char * ptr_zerosize_malloc = (char *)malloc(zero_size);
  84. int malloc_zerosize_errno = errno;
  85. printf("malloc (size 0): ");
  86. test_size_zero(ptr_zerosize_malloc, 1, malloc_zerosize_errno);
  87. free(ptr_zerosize_malloc);
  88. errno = 0;
  89. char * ptr_malloc = (char *)malloc(sample_alloc_size);
  90. int malloc_errno = errno;
  91. printf("malloc: ");
  92. test_non_null(ptr_malloc, malloc_errno);
  93. for(i = 0; i < sample_alloc_size; i++) {
  94. ptr_malloc[i] = (char)i;
  95. }
  96. free(ptr_malloc);
  97. errno = 0;
  98. char * ptr_malloc_maxsize = (char *)malloc(max_size);
  99. int malloc_maxsize_errno = errno;
  100. printf("malloc (SIZE_MAX): ");
  101. test_cannot_alloc(ptr_malloc_maxsize, malloc_maxsize_errno);
  102. free(ptr_malloc_maxsize);
  103. errno = 0;
  104. char * ptr_zerosize_calloc = (char *)calloc(zero_size, 1);
  105. int calloc_zerosize_errno = errno;
  106. printf("calloc (size 0): ");
  107. test_size_zero(ptr_zerosize_calloc, 1, calloc_zerosize_errno);
  108. free(ptr_zerosize_calloc);
  109. errno = 0;
  110. char * ptr_calloc = (char *)calloc(sample_alloc_size, 1);
  111. int calloc_errno = errno;
  112. printf("calloc: ");
  113. test_non_null(ptr_calloc, calloc_errno);
  114. for(i = 0; i < sample_alloc_size; i++) {
  115. ptr_calloc[i] = (char)i;
  116. }
  117. free(ptr_calloc);
  118. errno = 0;
  119. char * ptr_calloc_overflow = (char *)calloc(max_size, max_size);
  120. int calloc_overflow_errno = errno;
  121. printf("calloc (overflowing): ");
  122. test_cannot_alloc(ptr_calloc_overflow, calloc_overflow_errno);
  123. free(ptr_calloc_overflow);
  124. char * ptr_realloc_size0 = (char *)malloc(sample_alloc_size);
  125. errno = 0;
  126. ptr_realloc_size0 = (char *)realloc(ptr_realloc_size0, zero_size);
  127. int realloc_size0_errno = errno;
  128. printf("realloc (size 0): ");
  129. test_size_zero(ptr_realloc_size0, 1, realloc_size0_errno);
  130. free(ptr_realloc_size0);
  131. char * ptr_realloc = (char *)malloc(sample_alloc_size);
  132. errno = 0;
  133. ptr_realloc = (char *)realloc(ptr_realloc, sample_realloc_size);
  134. int realloc_errno = errno;
  135. printf("realloc: ");
  136. test_non_null(ptr_realloc, realloc_errno);
  137. for(i = 0; i < sample_realloc_size; i++) {
  138. ptr_realloc[i] = (char)i;
  139. }
  140. free(ptr_realloc);
  141. char * ptr_realloc_maxsize = (char *)malloc(sample_alloc_size);
  142. errno = 0;
  143. ptr_realloc_maxsize = (char *)realloc(ptr_realloc_maxsize, max_size);
  144. int realloc_maxsize_errno = errno;
  145. printf("realloc (SIZE_MAX): ");
  146. test_cannot_alloc(ptr_realloc_maxsize, realloc_maxsize_errno);
  147. free(ptr_realloc_maxsize);
  148. errno = 0;
  149. char * ptr_reallocarray_maxsize = (char *)malloc(sample_alloc_size);
  150. ptr_reallocarray_maxsize = (char *)reallocarray(ptr_reallocarray_maxsize, 2, sample_alloc_size);
  151. int reallocarray_errno = errno;
  152. printf("reallocarray: ");
  153. test_non_null(ptr_reallocarray_maxsize, reallocarray_errno);
  154. for(i = 0; i < sample_realloc_size; i++) {
  155. ptr_realloc[i] = (char)i;
  156. }
  157. errno = 0;
  158. ptr_reallocarray_maxsize = (char *)reallocarray(ptr_reallocarray_maxsize, 2, max_size);
  159. reallocarray_errno = errno;
  160. printf("reallocarray (SIZE_MAX): ");
  161. test_cannot_alloc(ptr_reallocarray_maxsize, reallocarray_errno);
  162. free(ptr_reallocarray_maxsize);
  163. errno = 0;
  164. char * ptr_memalign_size0 = (char *)memalign(aligned_alloc_alignment, zero_size);
  165. int memalign_size0_errno = errno;
  166. printf("memalign (size 0): ");
  167. test_size_zero(ptr_memalign_size0, aligned_alloc_alignment, memalign_size0_errno);
  168. free(ptr_memalign_size0);
  169. errno = 0;
  170. char * ptr_memalign = (char *)memalign(aligned_alloc_alignment, sample_alloc_size);
  171. int memalign_errno = errno;
  172. printf("memalign: ");
  173. test_valid_aligned(ptr_memalign, aligned_alloc_alignment, memalign_errno);
  174. for(i = 0; i < sample_alloc_size; i++) {
  175. ptr_memalign[i] = (char)i;
  176. }
  177. free(ptr_memalign);
  178. errno = 0;
  179. char * ptr_memalign_maxsize = (char *)memalign(aligned_alloc_alignment, max_size);
  180. int memalign_maxsize_errno = errno;
  181. printf("memalign (SIZE_MAX): ");
  182. test_cannot_alloc(ptr_memalign_maxsize, memalign_maxsize_errno);
  183. free(ptr_memalign_maxsize);
  184. errno = 0;
  185. char * ptr_memalign_align0 = (char *)memalign(0, sample_alloc_size);
  186. int memalign_align0_errno = errno;
  187. printf("memalign (alignment 0): ");
  188. test_invalid_aligned(ptr_memalign_align0, memalign_align0_errno);
  189. free(ptr_memalign_align0);
  190. errno = 0;
  191. char * ptr_memalign_align3 = (char *)memalign(3, sample_alloc_size);
  192. int memalign_align3_errno = errno;
  193. printf("memalign (alignment 3): ");
  194. test_invalid_aligned(ptr_memalign_align3, memalign_align3_errno);
  195. free(ptr_memalign_align3);
  196. errno = 0;
  197. char * ptr_aligned_alloc_goodsize = (char *)aligned_alloc(aligned_alloc_alignment, aligned_alloc_goodsize);
  198. int aligned_alloc_goodsize_errno = errno;
  199. printf("aligned_alloc (size %% alignment == 0): ");
  200. test_valid_aligned(ptr_aligned_alloc_goodsize, aligned_alloc_alignment, aligned_alloc_goodsize_errno);
  201. free(ptr_aligned_alloc_goodsize);
  202. errno = 0;
  203. char * ptr_aligned_alloc_badsize = (char *)aligned_alloc(aligned_alloc_alignment, aligned_alloc_badsize);
  204. int aligned_alloc_badsize_errno = errno;
  205. printf("aligned_alloc (size %% alignment != 0): ");
  206. test_invalid_aligned(ptr_aligned_alloc_badsize, aligned_alloc_badsize_errno);
  207. free(ptr_aligned_alloc_badsize);
  208. errno = 0;
  209. char * ptr_valloc_size0 = (char *)valloc(zero_size);
  210. int valloc_size0_errno = errno;
  211. printf("valloc (size 0): ");
  212. test_size_zero(ptr_valloc_size0, page_size, valloc_size0_errno);
  213. free(ptr_valloc_size0);
  214. errno = 0;
  215. char * ptr_valloc = (char *)valloc(sample_alloc_size);
  216. int valloc_errno = errno;
  217. printf("valloc: ");
  218. test_valid_aligned(ptr_valloc, page_size, valloc_errno);
  219. free(ptr_valloc);
  220. errno = 0;
  221. char * ptr_valloc_maxsize = (char *)valloc(max_size);
  222. int valloc_maxsize_errno = errno;
  223. printf("valloc (SIZE_MAX): ");
  224. test_cannot_alloc(ptr_valloc_maxsize, valloc_maxsize_errno);
  225. free(ptr_valloc_maxsize);
  226. errno = 0;
  227. void * ptr_posix_memalign = NULL;
  228. int posix_memalign_return = posix_memalign(&ptr_posix_memalign, pow2_mul_voidptr_size, sample_alloc_size);
  229. printf("posix_memalign: ");
  230. test_valid_aligned(ptr_posix_memalign, pow2_mul_voidptr_size, posix_memalign_return);
  231. free(ptr_posix_memalign);
  232. errno = 0;
  233. void * ptr_posix_memalign_align0 = NULL;
  234. int posix_memalign_align0_return = posix_memalign(&ptr_posix_memalign_align0, zero_size, sample_alloc_size);
  235. printf("posix_memalign (alignment 0): ");
  236. test_invalid_aligned(ptr_posix_memalign_align0, posix_memalign_align0_return);
  237. free(ptr_posix_memalign_align0);
  238. errno = 0;
  239. void * ptr_posix_memalign_nonpow2mul = NULL;
  240. int posix_memalign_nonpow2mul_return = posix_memalign(&ptr_posix_memalign_nonpow2mul, nonpow2_mul_voidptr_size, sample_alloc_size);
  241. printf("posix_memalign (non-power-of-two multiple of sizeof(void *)): ");
  242. test_invalid_aligned(ptr_posix_memalign_nonpow2mul, posix_memalign_nonpow2mul_return);
  243. free(ptr_posix_memalign_nonpow2mul);
  244. errno = 0;
  245. void * ptr_posix_memalign_size0 = NULL;
  246. int posix_memalign_size0_return = posix_memalign(&ptr_posix_memalign_size0, pow2_mul_voidptr_size, zero_size);
  247. printf("posix_memalign (size 0): ");
  248. test_size_zero(ptr_posix_memalign_size0, pow2_mul_voidptr_size, posix_memalign_size0_return);
  249. free(ptr_posix_memalign_size0);
  250. errno = 0;
  251. void * ptr_posix_memalign_maxsize = NULL;
  252. int posix_memalign_maxsize_return = posix_memalign(&ptr_posix_memalign_maxsize, pow2_mul_voidptr_size, max_size);
  253. printf("posix_memalign (SIZE_MAX): ");
  254. test_cannot_alloc(ptr_posix_memalign_maxsize, posix_memalign_maxsize_return);
  255. free(ptr_posix_memalign_maxsize);
  256. }