Types.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 Basic Types */
  32. #include "sgx_trts.h"
  33. #include "../Enclave.h"
  34. #include "Enclave_t.h"
  35. #include <limits>
  36. #include <cmath>
  37. /* used to eliminate `unused variable' warning */
  38. #define UNUSED(val) (void)(val)
  39. #define ULP 2
  40. /* used to compare double variables in order to avoid compile warnings */
  41. bool almost_equal(double x, double y)
  42. {
  43. /* the machine epsilon has to be scaled to the magnitude of the larger value
  44. and multiplied by the desired precision in ULPs (units in the last place) */
  45. return std::abs(x-y) <= std::numeric_limits<double>::epsilon() * std::abs(x+y) * ULP;
  46. }
  47. /* used to compare double variables in order to avoid compile warnings */
  48. bool almost_equal(float x, float y)
  49. {
  50. /* the machine epsilon has to be scaled to the magnitude of the larger value
  51. and multiplied by the desired precision in ULPs (units in the last place) */
  52. return std::abs(x-y) <= std::numeric_limits<float>::epsilon() * std::abs(x+y) * ULP;
  53. }
  54. /* ecall_type_char:
  55. * [char] value passed by App.
  56. */
  57. void ecall_type_char(char val)
  58. {
  59. assert(val == 0x12);
  60. #ifndef DEBUG
  61. UNUSED(val);
  62. #endif
  63. }
  64. /* ecall_type_int:
  65. * [int] value passed by App.
  66. */
  67. void ecall_type_int(int val)
  68. {
  69. assert(val == 1234);
  70. #ifndef DEBUG
  71. UNUSED(val);
  72. #endif
  73. }
  74. /* ecall_type_float:
  75. * [float] value passed by App.
  76. */
  77. void ecall_type_float(float val)
  78. {
  79. assert(almost_equal(val, (float)1234.0));
  80. #ifndef DEBUG
  81. UNUSED(val);
  82. #endif
  83. }
  84. /* ecall_type_double:
  85. * [double] value passed by App.
  86. */
  87. void ecall_type_double(double val)
  88. {
  89. assert(almost_equal(val, (double)1234.5678));
  90. #ifndef DEBUG
  91. UNUSED(val);
  92. #endif
  93. }
  94. /* ecall_type_size_t:
  95. * [size_t] value passed by App.
  96. */
  97. void ecall_type_size_t(size_t val)
  98. {
  99. assert(val == (size_t)12345678);
  100. #ifndef DEBUG
  101. UNUSED(val);
  102. #endif
  103. }
  104. /* ecall_type_wchar_t:
  105. * [wchar_t] value passed by App.
  106. */
  107. void ecall_type_wchar_t(wchar_t val)
  108. {
  109. assert(val == (wchar_t)0x1234);
  110. #ifndef DEBUG
  111. UNUSED(val);
  112. #endif
  113. }
  114. /* ecall_type_struct:
  115. * struct_foo_t is defined in EDL and can be used in ECALL.
  116. */
  117. void ecall_type_struct(struct struct_foo_t val)
  118. {
  119. assert(val.struct_foo_0 == 1234);
  120. assert(val.struct_foo_1 == 5678);
  121. #ifndef DEBUG
  122. UNUSED(val);
  123. #endif
  124. }
  125. /*
  126. * ecall_type_enum_union:
  127. * enum_foo_t/union_foo_t is defined in EDL
  128. * and can be used in ECALL.
  129. */
  130. void ecall_type_enum_union(enum enum_foo_t val1, union union_foo_t *val2)
  131. {
  132. if (sgx_is_outside_enclave(val2, sizeof(union union_foo_t)) != 1)
  133. abort();
  134. val2->union_foo_0 = 1;
  135. val2->union_foo_1 = 2; /* overwrite union_foo_0 */
  136. assert(val1 == ENUM_FOO_0);
  137. #ifndef DEBUG
  138. UNUSED(val1);
  139. #endif
  140. }