wasm_native.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_native.h"
  6. #include "wasm_runtime_common.h"
  7. #include "bh_log.h"
  8. #if !defined(BH_PLATFORM_ZEPHYR) && !defined(BH_PLATFORM_ALIOS_THINGS)
  9. #define ENABLE_QUICKSORT 1
  10. #else
  11. #define ENABLE_QUICKSORT 0
  12. #endif
  13. #define ENABLE_SORT_DEBUG 0
  14. #if ENABLE_SORT_DEBUG != 0
  15. #include<sys/time.h>
  16. #endif
  17. static NativeSymbolsList g_native_symbols_list = NULL;
  18. static NativeSymbolsList g_native_symbols_list_end = NULL;
  19. uint32
  20. get_libc_builtin_export_apis(NativeSymbol **p_libc_builtin_apis);
  21. #if WASM_ENABLE_SPEC_TEST != 0
  22. uint32
  23. get_spectest_export_apis(NativeSymbol **p_libc_builtin_apis);
  24. #endif
  25. uint32
  26. get_libc_wasi_export_apis(NativeSymbol **p_libc_wasi_apis);
  27. uint32
  28. get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
  29. uint32
  30. get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
  31. #if WASM_ENABLE_LIB_PTHREAD != 0
  32. bool
  33. lib_pthread_init();
  34. void
  35. lib_pthread_destroy();
  36. uint32
  37. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
  38. #endif
  39. static bool
  40. check_symbol_signature(const WASMType *type, const char *signature)
  41. {
  42. const char *p = signature, *p_end;
  43. char sig_map[] = { 'F', 'f', 'I', 'i' }, sig;
  44. uint32 i = 0;
  45. if (!p || strlen(p) < 2)
  46. return false;
  47. p_end = p + strlen(signature);
  48. if (*p++ != '(')
  49. return false;
  50. if ((uint32)(p_end - p) < (uint32)(type->param_count + 1))
  51. /* signatures of parameters, and ')' */
  52. return false;
  53. for (i = 0; i < type->param_count; i++) {
  54. sig = *p++;
  55. if (sig == sig_map[type->types[i] - VALUE_TYPE_F64])
  56. /* normal parameter */
  57. continue;
  58. if (type->types[i] != VALUE_TYPE_I32)
  59. /* pointer and string must be i32 type */
  60. return false;
  61. if (sig == '*') {
  62. /* it is a pointer */
  63. if (i + 1 < type->param_count
  64. && type->types[i + 1] == VALUE_TYPE_I32
  65. && *p == '~') {
  66. /* pointer length followed */
  67. i++;
  68. p++;
  69. }
  70. }
  71. else if (sig == '$') {
  72. /* it is a string */
  73. }
  74. else {
  75. /* invalid signature */
  76. return false;
  77. }
  78. }
  79. if (*p++ != ')')
  80. return false;
  81. if (type->result_count) {
  82. if (p >= p_end)
  83. return false;
  84. if (*p++ != sig_map[type->types[i] - VALUE_TYPE_F64])
  85. return false;
  86. }
  87. if (*p != '\0')
  88. return false;
  89. return true;
  90. }
  91. #if ENABLE_QUICKSORT == 0
  92. static void
  93. sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
  94. {
  95. uint32 i, j;
  96. NativeSymbol temp;
  97. for (i = 0; i < n_native_symbols - 1; i++) {
  98. for (j = i + 1; j < n_native_symbols; j++) {
  99. if (strcmp(native_symbols[i].symbol,
  100. native_symbols[j].symbol) > 0) {
  101. temp = native_symbols[i];
  102. native_symbols[i] = native_symbols[j];
  103. native_symbols[j] = temp;
  104. }
  105. }
  106. }
  107. }
  108. #else
  109. static void
  110. swap_symbol(NativeSymbol* left, NativeSymbol* right)
  111. {
  112. NativeSymbol temp = *left;
  113. *left = *right;
  114. *right = temp;
  115. }
  116. static void
  117. quick_sort_symbols(NativeSymbol* native_symbols, int left, int right)
  118. {
  119. NativeSymbol base_symbol;
  120. int pin_left = left;
  121. int pin_right = right;
  122. if (left >= right) {
  123. return;
  124. }
  125. base_symbol = native_symbols[left];
  126. while (left < right) {
  127. while (left < right
  128. && strcmp(native_symbols[right].symbol,
  129. base_symbol.symbol) > 0) {
  130. right--;
  131. }
  132. if (left < right) {
  133. swap_symbol(&native_symbols[left], &native_symbols[right]);
  134. left++;
  135. }
  136. while (left < right
  137. && strcmp(native_symbols[left].symbol,
  138. base_symbol.symbol) < 0) {
  139. left++;
  140. }
  141. if (left < right) {
  142. swap_symbol(&native_symbols[left], &native_symbols[right]);
  143. right--;
  144. }
  145. }
  146. native_symbols[left] = base_symbol;
  147. quick_sort_symbols(native_symbols, pin_left, left - 1);
  148. quick_sort_symbols(native_symbols, left + 1, pin_right);
  149. }
  150. #endif /* end of ENABLE_QUICKSORT */
  151. static void *
  152. lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
  153. const char *symbol, const char **p_signature, void **p_attachment)
  154. {
  155. int low = 0, mid, ret;
  156. int high = (int32)n_native_symbols - 1;
  157. while (low <= high) {
  158. mid = (low + high) / 2;
  159. ret = strcmp(symbol, native_symbols[mid].symbol);
  160. if (ret == 0) {
  161. *p_signature = native_symbols[mid].signature;
  162. *p_attachment = native_symbols[mid].attachment;
  163. return native_symbols[mid].func_ptr;
  164. }
  165. else if (ret < 0)
  166. high = mid - 1;
  167. else
  168. low = mid + 1;
  169. }
  170. return NULL;
  171. }
  172. void*
  173. wasm_native_resolve_symbol(const char *module_name, const char *field_name,
  174. const WASMType *func_type, const char **p_signature,
  175. void **p_attachment, bool *p_call_conv_raw)
  176. {
  177. NativeSymbolsNode *node, *node_next;
  178. const char *signature = NULL;
  179. void *func_ptr = NULL, *attachment;
  180. node = g_native_symbols_list;
  181. while (node) {
  182. node_next = node->next;
  183. if (!strcmp(node->module_name, module_name)) {
  184. if ((func_ptr = lookup_symbol(node->native_symbols,
  185. node->n_native_symbols,
  186. field_name, &signature, &attachment))
  187. || (field_name[0] == '_'
  188. && (func_ptr = lookup_symbol(node->native_symbols,
  189. node->n_native_symbols,
  190. field_name + 1,
  191. &signature, &attachment))))
  192. break;
  193. }
  194. node = node_next;
  195. }
  196. if (func_ptr) {
  197. if (signature && signature[0] != '\0') {
  198. /* signature is not empty, check its format */
  199. if (!check_symbol_signature(func_type, signature)) {
  200. #if WASM_ENABLE_WAMR_COMPILER == 0 /* Output warning except running aot compiler */
  201. LOG_WARNING("failed to check signature '%s' and resolve "
  202. "pointer params for import function (%s %s)\n",
  203. signature, module_name, field_name);
  204. #endif
  205. return NULL;
  206. }
  207. else
  208. /* Save signature for runtime to do pointer check and
  209. address conversion */
  210. *p_signature = signature;
  211. }
  212. else
  213. /* signature is empty */
  214. *p_signature = NULL;
  215. *p_attachment = attachment;
  216. *p_call_conv_raw = node->call_conv_raw;
  217. }
  218. return func_ptr;
  219. }
  220. static bool
  221. register_natives(const char *module_name,
  222. NativeSymbol *native_symbols,
  223. uint32 n_native_symbols,
  224. bool call_conv_raw)
  225. {
  226. NativeSymbolsNode *node;
  227. #if ENABLE_SORT_DEBUG != 0
  228. struct timeval start;
  229. struct timeval end;
  230. unsigned long timer;
  231. #endif
  232. if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
  233. return false;
  234. node->module_name = module_name;
  235. node->native_symbols = native_symbols;
  236. node->n_native_symbols = n_native_symbols;
  237. node->call_conv_raw = call_conv_raw;
  238. node->next = NULL;
  239. if (g_native_symbols_list_end) {
  240. g_native_symbols_list_end->next = node;
  241. g_native_symbols_list_end = node;
  242. }
  243. else {
  244. g_native_symbols_list = g_native_symbols_list_end = node;
  245. }
  246. #if ENABLE_SORT_DEBUG != 0
  247. gettimeofday(&start, NULL);
  248. #endif
  249. #if ENABLE_QUICKSORT == 0
  250. sort_symbol_ptr(native_symbols, n_native_symbols);
  251. #else
  252. quick_sort_symbols(native_symbols, 0, (int)(n_native_symbols - 1));
  253. #endif
  254. #if ENABLE_SORT_DEBUG != 0
  255. gettimeofday(&end, NULL);
  256. timer = 1000000 * (end.tv_sec - start.tv_sec)
  257. + (end.tv_usec - start.tv_usec);
  258. LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us",
  259. module_name, n_native_symbols, timer);
  260. #endif
  261. return true;
  262. }
  263. bool
  264. wasm_native_register_natives(const char *module_name,
  265. NativeSymbol *native_symbols,
  266. uint32 n_native_symbols)
  267. {
  268. return register_natives(module_name, native_symbols, n_native_symbols, false);
  269. }
  270. bool
  271. wasm_native_register_natives_raw(const char *module_name,
  272. NativeSymbol *native_symbols,
  273. uint32 n_native_symbols)
  274. {
  275. return register_natives(module_name, native_symbols, n_native_symbols, true);
  276. }
  277. bool
  278. wasm_native_init()
  279. {
  280. NativeSymbol *native_symbols;
  281. uint32 n_native_symbols;
  282. #if WASM_ENABLE_LIBC_BUILTIN != 0
  283. n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
  284. if (!wasm_native_register_natives("env",
  285. native_symbols, n_native_symbols))
  286. return false;
  287. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  288. #if WASM_ENABLE_SPEC_TEST
  289. n_native_symbols = get_spectest_export_apis(&native_symbols);
  290. if (!wasm_native_register_natives("spectest",
  291. native_symbols, n_native_symbols))
  292. return false;
  293. #endif /* WASM_ENABLE_SPEC_TEST */
  294. #if WASM_ENABLE_LIBC_WASI != 0
  295. n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
  296. if (!wasm_native_register_natives("wasi_unstable",
  297. native_symbols, n_native_symbols))
  298. return false;
  299. if (!wasm_native_register_natives("wasi_snapshot_preview1",
  300. native_symbols, n_native_symbols))
  301. return false;
  302. #endif
  303. #if WASM_ENABLE_BASE_LIB != 0
  304. n_native_symbols = get_base_lib_export_apis(&native_symbols);
  305. if (n_native_symbols > 0
  306. && !wasm_native_register_natives("env",
  307. native_symbols, n_native_symbols))
  308. return false;
  309. #endif
  310. #if WASM_ENABLE_APP_FRAMEWORK != 0
  311. n_native_symbols = get_ext_lib_export_apis(&native_symbols);
  312. if (n_native_symbols > 0
  313. && !wasm_native_register_natives("env",
  314. native_symbols, n_native_symbols))
  315. return false;
  316. #endif
  317. #if WASM_ENABLE_LIB_PTHREAD != 0
  318. if (!lib_pthread_init())
  319. return false;
  320. n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
  321. if (n_native_symbols > 0
  322. && !wasm_native_register_natives("env",
  323. native_symbols, n_native_symbols))
  324. return false;
  325. #endif
  326. return true;
  327. }
  328. void
  329. wasm_native_destroy()
  330. {
  331. NativeSymbolsNode *node, *node_next;
  332. #if WASM_ENABLE_LIB_PTHREAD != 0
  333. lib_pthread_destroy();
  334. #endif
  335. node = g_native_symbols_list;
  336. while (node) {
  337. node_next = node->next;
  338. wasm_runtime_free(node);
  339. node = node_next;
  340. }
  341. g_native_symbols_list = g_native_symbols_list_end = NULL;
  342. }