bh_common.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_common.h"
  6. static char *
  7. align_ptr(char *src, unsigned int b)
  8. {
  9. uintptr_t v = (uintptr_t)src;
  10. uintptr_t m = b - 1;
  11. return (char *)((v + m) & ~m);
  12. }
  13. /*
  14. Memory copy, with word alignment
  15. */
  16. int
  17. b_memcpy_wa(void *s1, unsigned int s1max, const void *s2, unsigned int n)
  18. {
  19. char *dest = (char *)s1;
  20. char *src = (char *)s2;
  21. char *pa = align_ptr(src, 4);
  22. char *pb = align_ptr((src + n), 4);
  23. unsigned int buff;
  24. const char *p_byte_read;
  25. unsigned int *p;
  26. char *ps;
  27. if (n == 0) {
  28. return 0;
  29. }
  30. if (pa > src) {
  31. pa -= 4;
  32. }
  33. for (p = (unsigned int *)pa; p < (unsigned int *)pb; p++) {
  34. buff = *(p);
  35. p_byte_read = ((char *)&buff);
  36. /* read leading word */
  37. if ((char *)p <= src) {
  38. for (ps = src; ps < ((char *)p + 4); ps++) {
  39. if (ps >= src + n) {
  40. break;
  41. }
  42. p_byte_read = ((char *)&buff) + (ps - (char *)p);
  43. *dest++ = *p_byte_read;
  44. }
  45. }
  46. /* read trailing word */
  47. else if ((char *)p >= pb - 4) {
  48. for (ps = (char *)p; ps < src + n; ps++) {
  49. *dest++ = *p_byte_read++;
  50. }
  51. }
  52. /* read remaining word(s) */
  53. else {
  54. if ((char *)p + 4 >= src + n) {
  55. for (ps = (char *)p; ps < src + n; ps++) {
  56. *dest++ = *p_byte_read++;
  57. }
  58. }
  59. else {
  60. *(unsigned int *)dest = buff;
  61. dest += 4;
  62. }
  63. }
  64. }
  65. return 0;
  66. }
  67. int
  68. b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
  69. {
  70. char *dest = (char *)s1;
  71. char *src = (char *)s2;
  72. if (n == 0) {
  73. return 0;
  74. }
  75. if (s1 == NULL) {
  76. return -1;
  77. }
  78. if (s2 == NULL || n > s1max) {
  79. memset(dest, 0, s1max);
  80. return -1;
  81. }
  82. memcpy(dest, src, n);
  83. return 0;
  84. }
  85. int
  86. b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
  87. {
  88. char *dest = (char *)s1;
  89. char *src = (char *)s2;
  90. if (n == 0) {
  91. return 0;
  92. }
  93. if (s1 == NULL) {
  94. return -1;
  95. }
  96. if (s2 == NULL || n > s1max) {
  97. memset(dest, 0, s1max);
  98. return -1;
  99. }
  100. memmove(dest, src, n);
  101. return 0;
  102. }
  103. int
  104. b_strcat_s(char *s1, unsigned int s1max, const char *s2)
  105. {
  106. if (NULL == s1 || NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1)) {
  107. return -1;
  108. }
  109. memcpy(s1 + strlen(s1), s2, strlen(s2) + 1);
  110. return 0;
  111. }
  112. int
  113. b_strcpy_s(char *s1, unsigned int s1max, const char *s2)
  114. {
  115. if (NULL == s1 || NULL == s2 || s1max < (strlen(s2) + 1)) {
  116. return -1;
  117. }
  118. memcpy(s1, s2, strlen(s2) + 1);
  119. return 0;
  120. }
  121. char *
  122. bh_strdup(const char *s)
  123. {
  124. uint32 size;
  125. char *s1 = NULL;
  126. if (s) {
  127. size = (uint32)(strlen(s) + 1);
  128. if ((s1 = BH_MALLOC(size)))
  129. bh_memcpy_s(s1, size, s, size);
  130. }
  131. return s1;
  132. }
  133. char *
  134. wa_strdup(const char *s)
  135. {
  136. uint32 size;
  137. char *s1 = NULL;
  138. if (s) {
  139. size = (uint32)(strlen(s) + 1);
  140. if ((s1 = WA_MALLOC(size)))
  141. bh_memcpy_s(s1, size, s, size);
  142. }
  143. return s1;
  144. }
  145. #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
  146. int
  147. bh_system(const char *cmd)
  148. {
  149. int ret;
  150. #if !(defined(_WIN32) || defined(_WIN32_))
  151. ret = system(cmd);
  152. #else
  153. ret = _spawnlp(_P_WAIT, "cmd.exe", "/c", cmd, NULL);
  154. #endif
  155. return ret;
  156. }
  157. #if defined(_WIN32) || defined(_WIN32_)
  158. errno_t
  159. _mktemp_s(char *nameTemplate, size_t sizeInChars);
  160. #endif
  161. bool
  162. bh_mkstemp(char *file_name, size_t name_len)
  163. {
  164. int fd;
  165. #if !(defined(_WIN32) || defined(_WIN32_))
  166. (void)name_len;
  167. /* On Linux, it generates a unique temporary filename from template, creates
  168. * and opens the file, and returns an open file descriptor for the file. */
  169. if ((fd = mkstemp(file_name)) <= 0) {
  170. goto fail;
  171. }
  172. /* close and remove temp file */
  173. close(fd);
  174. unlink(file_name);
  175. #else
  176. /* On Windows, it generates a unique temporary file name but does not create
  177. * or open the file */
  178. if (_mktemp_s(file_name, name_len) != 0) {
  179. goto fail;
  180. }
  181. #endif
  182. return true;
  183. fail:
  184. return false;
  185. }
  186. #endif /* End of WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 */