Pointers.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2011-2016 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. char tmp[100] = {0};
  63. size_t len = sz>100?100:sz;
  64. /* copy the memory into the enclave to make sure 'val'
  65. * is not being changed in checksum_internal() */
  66. memcpy(tmp, val, len);
  67. int32_t sum = checksum_internal((char *)tmp, len);
  68. printf("Checksum(0x%p, %zu) = 0x%x\n",
  69. val, len, (unsigned int)sum);
  70. /* modify outside memory directly */
  71. memcpy(val, "SGX_SUCCESS", len>12?12:len);
  72. return len;
  73. }
  74. /* ecall_pointer_in:
  75. * the buffer of val is copied to the enclave.
  76. */
  77. void ecall_pointer_in(int *val)
  78. {
  79. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  80. abort();
  81. *val = 1234;
  82. }
  83. /* ecall_pointer_out:
  84. * the buffer of val is copied to the untrusted side.
  85. */
  86. void ecall_pointer_out(int *val)
  87. {
  88. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  89. abort();
  90. assert(*val == 0);
  91. *val = 1234;
  92. }
  93. /* ecall_pointer_in_out:
  94. * the buffer of val is double-copied.
  95. */
  96. void ecall_pointer_in_out(int *val)
  97. {
  98. if (sgx_is_within_enclave(val, sizeof(int)) != 1)
  99. abort();
  100. *val = 1234;
  101. }
  102. /* ocall_pointer_attr:
  103. * The root ECALL that test OCALL [in], [out], [user_check].
  104. */
  105. void ocall_pointer_attr(void)
  106. {
  107. sgx_status_t ret = SGX_ERROR_UNEXPECTED;
  108. int val = 0;
  109. ret = ocall_pointer_user_check(&val);
  110. if (ret != SGX_SUCCESS)
  111. abort();
  112. val = 0;
  113. ret = ocall_pointer_in(&val);
  114. if (ret != SGX_SUCCESS)
  115. abort();
  116. assert(val == 0);
  117. val = 0;
  118. ret = ocall_pointer_out(&val);
  119. if (ret != SGX_SUCCESS)
  120. abort();
  121. assert(val == 1234);
  122. val = 0;
  123. ret = ocall_pointer_in_out(&val);
  124. if (ret != SGX_SUCCESS)
  125. abort();
  126. assert(val == 1234);
  127. return;
  128. }
  129. /* ecall_pointer_string:
  130. * [string] defines a string.
  131. */
  132. void ecall_pointer_string(char *str)
  133. {
  134. strncpy(str, "0987654321", strlen(str));
  135. }
  136. /* ecall_pointer_string_const:
  137. * const [string] defines a string that cannot be modified.
  138. */
  139. void ecall_pointer_string_const(const char *str)
  140. {
  141. char* temp = new char[strlen(str)];
  142. strncpy(temp, str, strlen(str));
  143. delete []temp;
  144. }
  145. /* ecall_pointer_size:
  146. * 'len' needs to be specified to tell Edger8r the length of 'str'.
  147. */
  148. void ecall_pointer_size(void *ptr, size_t len)
  149. {
  150. strncpy((char*)ptr, "0987654321", len);
  151. }
  152. /* ecall_pointer_count:
  153. * 'cnt' needs to be specified to tell Edger8r the number of elements in 'arr'.
  154. */
  155. void ecall_pointer_count(int *arr, int cnt)
  156. {
  157. for (int i = (cnt - 1); i >= 0; i--)
  158. arr[i] = (cnt - 1 - i);
  159. }
  160. /* ecall_pointer_isptr_readonly:
  161. * 'buf' is user defined type, shall be tagged with [isptr].
  162. * if it's not writable, [readonly] shall be specified.
  163. */
  164. void ecall_pointer_isptr_readonly(buffer_t buf, size_t len)
  165. {
  166. strncpy((char*)buf, "0987654321", len);
  167. }
  168. /* get_buffer_len:
  169. * get the length of input buffer 'buf'.
  170. */
  171. size_t get_buffer_len(const char* buf)
  172. {
  173. (void)buf;
  174. return 10*sizeof(int);
  175. }
  176. /* ecall_pointer_sizefunc:
  177. * call get_buffer_len to determin the length of 'buf'.
  178. */
  179. void ecall_pointer_sizefunc(char *buf)
  180. {
  181. int *tmp = (int*)buf;
  182. for (int i = 0; i < 10; i++) {
  183. assert(tmp[i] == 0);
  184. tmp[i] = i;
  185. }
  186. }