sbrk.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <stddef.h>
  32. #include <unistd.h>
  33. #include "trts_util.h"
  34. #include "rts.h"
  35. #include "util.h"
  36. #include "global_data.h"
  37. #include "trts_inst.h"
  38. SE_DECLSPEC_EXPORT size_t g_peak_heap_used = 0;
  39. /* Please be aware of: sbrk is not thread safe by default. */
  40. static void *heap_base = NULL;
  41. static size_t heap_size = 0;
  42. static int is_edmm_supported = 0;
  43. static size_t heap_min_size = 0;
  44. int heap_init(void *_heap_base, size_t _heap_size, size_t _heap_min_size, int _is_edmm_supported)
  45. {
  46. if (heap_base != NULL)
  47. return SGX_ERROR_UNEXPECTED;
  48. if ((_heap_base == NULL) || (((size_t) _heap_base) & (SE_PAGE_SIZE - 1)))
  49. return SGX_ERROR_UNEXPECTED;
  50. if (_heap_size & (SE_PAGE_SIZE - 1))
  51. return SGX_ERROR_UNEXPECTED;
  52. if (_heap_min_size & (SE_PAGE_SIZE - 1))
  53. return SGX_ERROR_UNEXPECTED;
  54. if (_heap_size > SIZE_MAX - (size_t)heap_base)
  55. return SGX_ERROR_UNEXPECTED;
  56. heap_base = _heap_base;
  57. heap_size = _heap_size;
  58. heap_min_size = _heap_min_size;
  59. is_edmm_supported = _is_edmm_supported;
  60. return SGX_SUCCESS;
  61. }
  62. void* sbrk(intptr_t n)
  63. {
  64. static size_t heap_used;
  65. void *heap_ptr = NULL;
  66. size_t prev_heap_used = heap_used;
  67. void * start_addr;
  68. size_t size = 0;
  69. if (!heap_base)
  70. return (void *)(~(size_t)0);
  71. /* shrink the heap */
  72. if (n < 0) {
  73. n *= -1;
  74. if (heap_used < n)
  75. return (void *)(~(size_t)0);
  76. heap_used -= n;
  77. /* heap_used is never larger than heap_size, and since heap_size <= SIZE_MAX - (size_t)heap_base,
  78. there's no integer overflow here.
  79. */
  80. heap_ptr = (void *)((size_t)heap_base + (size_t)heap_used);
  81. if (is_edmm_supported && (prev_heap_used > heap_min_size))
  82. {
  83. assert((n & (SE_PAGE_SIZE - 1)) == 0);
  84. if (heap_used > heap_min_size)
  85. {
  86. start_addr = heap_ptr;
  87. size = n;
  88. }
  89. else
  90. {
  91. /* heap_min_size is never larger than heap_size, and since heap_size <= SIZE_MAX - (size_t)heap_base,
  92. there's no integer overflow here.
  93. */
  94. start_addr = (void *)((size_t)(heap_base) + heap_min_size);
  95. size = prev_heap_used - heap_min_size;
  96. }
  97. int ret = trim_EPC_pages(start_addr, size >> SE_PAGE_SHIFT);
  98. if (ret != 0)
  99. {
  100. heap_used = prev_heap_used;
  101. return (void *)(~(size_t)0);
  102. }
  103. }
  104. return heap_ptr;
  105. }
  106. /* extend the heap */
  107. if((heap_used > (SIZE_MAX - n)) || ((heap_used + n) > heap_size))
  108. return (void *)(~(size_t)0);
  109. /* heap_used is never larger than heap_size, and since heap_size <= SIZE_MAX - (size_t)heap_base,
  110. there's no integer overflow here.
  111. */
  112. heap_ptr = (void *)((size_t)heap_base + (size_t)heap_used);
  113. heap_used += n;
  114. /* update g_peak_heap_used */
  115. g_peak_heap_used = (g_peak_heap_used < heap_used) ? heap_used : g_peak_heap_used;
  116. if (is_edmm_supported && heap_used > heap_min_size)
  117. {
  118. assert((n & (SE_PAGE_SIZE - 1)) == 0);
  119. if (prev_heap_used > heap_min_size)
  120. {
  121. start_addr = heap_ptr;
  122. size = n;
  123. }
  124. else
  125. {
  126. /* heap_min_size is never larger than heap_size, and since heap_size <= SIZE_MAX - (size_t)heap_base,
  127. there's no integer overflow here.
  128. */
  129. start_addr = (void *)((size_t)(heap_base) + heap_min_size);
  130. size = heap_used - heap_min_size;
  131. }
  132. int ret = apply_EPC_pages(start_addr, size >> SE_PAGE_SHIFT);
  133. if (ret != 0)
  134. {
  135. heap_used = prev_heap_used;
  136. return (void *)(~(size_t)0);
  137. }
  138. }
  139. return heap_ptr;
  140. }