Pointers.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /* Test Pointer Auttributes */
  32. #include <sys/types.h>
  33. #include <string.h>
  34. #include "sgx_trts.h"
  35. #include "../Enclave.h"
  36. #include "Enclave_t.h"
  37. /* checksum_internal:
  38. * get simple checksum of input buffer and length
  39. */
  40. int32_t checksum_internal(char *buf, size_t count)
  41. {
  42. register int32_t sum = 0;
  43. int16_t *ptr = (int16_t *)buf;
  44. /* Main summing loop */
  45. while(count > 1) {
  46. sum = sum + *ptr++;
  47. count = count - 2;
  48. }
  49. /* Add left-over byte, if any */
  50. if (count > 0)
  51. sum = sum + *((char *)ptr);
  52. return ~sum;
  53. }
  54. /* ecall_pointer_user_check, ecall_pointer_in, ecall_pointer_out, ecall_pointer_in_out:
  55. * The root ECALLs to test [in], [out], [user_check] attributes.
  56. */
  57. size_t ecall_pointer_user_check(void *val, size_t sz)
  58. {
  59. /* check if the buffer is allocated outside */
  60. if (sgx_is_outside_enclave(val, sz) != 1)
  61. abort();
  62. /*fence after sgx_is_outside_enclave check*/
  63. __builtin_ia32_lfence();
  64. char tmp[100] = {0};
  65. size_t len = sz>100?100:sz;
  66. /* copy the memory into the enclave to make sure 'val'
  67. * is not being changed in checksum_internal() */
  68. memcpy(tmp, val, len);
  69. int32_t sum = checksum_internal((char *)tmp, len);
  70. printf("Checksum(0x%p, %zu) = 0x%x\n",
  71. val, len, (unsigned int)sum);
  72. /* modify outside memory directly */
  73. memcpy(val, "SGX_SUCCESS", len>12?12:len);
  74. return len;
  75. }
  76. /* ecall_pointer_in:
  77. * the buffer of val is copied to the enclave.
  78. */
  79. void ecall_pointer_in(int *val)
  80. {
  81. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  82. abort();
  83. *val = 1234;
  84. }
  85. /* ecall_pointer_out:
  86. * the buffer of val is copied to the untrusted side.
  87. */
  88. void ecall_pointer_out(int *val)
  89. {
  90. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  91. abort();
  92. assert(*val == 0);
  93. *val = 1234;
  94. }
  95. /* ecall_pointer_in_out:
  96. * the buffer of val is double-copied.
  97. */
  98. void ecall_pointer_in_out(int *val)
  99. {
  100. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  101. abort();
  102. *val = 1234;
  103. }
  104. /* ocall_pointer_attr:
  105. * The root ECALL that test OCALL [in], [out], [user_check].
  106. */
  107. void ocall_pointer_attr(void)
  108. {
  109. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  110. int val = 0;
  111. ret = ocall_pointer_user_check(&val);
  112. if (ret != SGX_SUCCESS)
  113. abort();
  114. val = 0;
  115. ret = ocall_pointer_in(&val);
  116. if (ret != SGX_SUCCESS)
  117. abort();
  118. assert(val == 0);
  119. val = 0;
  120. ret = ocall_pointer_out(&val);
  121. if (ret != SGX_SUCCESS)
  122. abort();
  123. assert(val == 1234);
  124. val = 0;
  125. ret = ocall_pointer_in_out(&val);
  126. if (ret != SGX_SUCCESS)
  127. abort();
  128. assert(val == 1234);
  129. return;
  130. }
  131. /* ecall_pointer_string:
  132. * [string] defines a string.
  133. */
  134. void ecall_pointer_string(char *str)
  135. {
  136. strncpy(str, "0987654321", strlen(str));
  137. }
  138. /* ecall_pointer_string_const:
  139. * const [string] defines a string that cannot be modified.
  140. */
  141. void ecall_pointer_string_const(const char *str)
  142. {
  143. char* temp = new char[strlen(str)];
  144. strncpy(temp, str, strlen(str));
  145. delete []temp;
  146. }
  147. /* ecall_pointer_size:
  148. * 'len' needs to be specified to tell Edger8r the length of 'str'.
  149. */
  150. void ecall_pointer_size(void *ptr, size_t len)
  151. {
  152. strncpy((char*)ptr, "0987654321", len);
  153. }
  154. /* ecall_pointer_count:
  155. * 'cnt' needs to be specified to tell Edger8r the number of elements in 'arr'.
  156. */
  157. void ecall_pointer_count(int *arr, int cnt)
  158. {
  159. for (int i = (cnt - 1); i >= 0; i--)
  160. arr[i] = (cnt - 1 - i);
  161. }
  162. /* ecall_pointer_isptr_readonly:
  163. * 'buf' is user defined type, shall be tagged with [isptr].
  164. * if it's not writable, [readonly] shall be specified.
  165. */
  166. void ecall_pointer_isptr_readonly(buffer_t buf, size_t len)
  167. {
  168. strncpy((char*)buf, "0987654321", len);
  169. }
  170. /* get_buffer_len:
  171. * get the length of input buffer 'buf'.
  172. */
  173. size_t get_buffer_len(const char* buf)
  174. {
  175. (void)buf;
  176. return 10*sizeof(int);
  177. }
  178. /* ecall_pointer_sizefunc:
  179. * call get_buffer_len to determin the length of 'buf'.
  180. */
  181. void ecall_pointer_sizefunc(char *buf)
  182. {
  183. int *tmp = (int*)buf;
  184. for (int i = 0; i < 10; i++) {
  185. assert(tmp[i] == 0);
  186. tmp[i] = i;
  187. }
  188. }