wasm_native.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. && !defined(BH_PLATFORM_OPENRTOS) && !defined(BH_PLATFORM_ESP_IDF)
  10. #define ENABLE_QUICKSORT 1
  11. #else
  12. #define ENABLE_QUICKSORT 0
  13. #endif
  14. #define ENABLE_SORT_DEBUG 0
  15. #if ENABLE_SORT_DEBUG != 0
  16. #include <sys/time.h>
  17. #endif
  18. static NativeSymbolsList g_native_symbols_list = 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_t
  28. get_wasi_nn_export_apis(NativeSymbol **p_libc_wasi_apis);
  29. uint32
  30. get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
  31. uint32
  32. get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
  33. #if WASM_ENABLE_LIB_PTHREAD != 0
  34. bool
  35. lib_pthread_init();
  36. void
  37. lib_pthread_destroy();
  38. uint32
  39. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
  40. #endif
  41. uint32
  42. get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
  43. uint32
  44. get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis);
  45. static bool
  46. compare_type_with_signautre(uint8 type, const char signature)
  47. {
  48. const char num_sig_map[] = { 'F', 'f', 'I', 'i' };
  49. if (VALUE_TYPE_F64 <= type && type <= VALUE_TYPE_I32
  50. && signature == num_sig_map[type - VALUE_TYPE_F64]) {
  51. return true;
  52. }
  53. #if WASM_ENABLE_REF_TYPES != 0
  54. if ('r' == signature && type == VALUE_TYPE_EXTERNREF)
  55. return true;
  56. #endif
  57. /* TODO: a v128 parameter */
  58. return false;
  59. }
  60. static bool
  61. check_symbol_signature(const WASMType *type, const char *signature)
  62. {
  63. const char *p = signature, *p_end;
  64. char sig;
  65. uint32 i = 0;
  66. if (!p || strlen(p) < 2)
  67. return false;
  68. p_end = p + strlen(signature);
  69. if (*p++ != '(')
  70. return false;
  71. if ((uint32)(p_end - p) < (uint32)(type->param_count + 1))
  72. /* signatures of parameters, and ')' */
  73. return false;
  74. for (i = 0; i < type->param_count; i++) {
  75. sig = *p++;
  76. /* a f64/f32/i64/i32/externref parameter */
  77. if (compare_type_with_signautre(type->types[i], sig))
  78. continue;
  79. /* a pointer/string paramter */
  80. if (type->types[i] != VALUE_TYPE_I32)
  81. /* pointer and string must be i32 type */
  82. return false;
  83. if (sig == '*') {
  84. /* it is a pointer */
  85. if (i + 1 < type->param_count
  86. && type->types[i + 1] == VALUE_TYPE_I32 && *p == '~') {
  87. /* pointer length followed */
  88. i++;
  89. p++;
  90. }
  91. }
  92. else if (sig == '$') {
  93. /* it is a string */
  94. }
  95. else {
  96. /* invalid signature */
  97. return false;
  98. }
  99. }
  100. if (*p++ != ')')
  101. return false;
  102. if (type->result_count) {
  103. if (p >= p_end)
  104. return false;
  105. /* result types includes: f64,f32,i64,i32,externref */
  106. if (!compare_type_with_signautre(type->types[i], *p))
  107. return false;
  108. p++;
  109. }
  110. if (*p != '\0')
  111. return false;
  112. return true;
  113. }
  114. #if ENABLE_QUICKSORT == 0
  115. static void
  116. sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
  117. {
  118. uint32 i, j;
  119. NativeSymbol temp;
  120. for (i = 0; i < n_native_symbols - 1; i++) {
  121. for (j = i + 1; j < n_native_symbols; j++) {
  122. if (strcmp(native_symbols[i].symbol, native_symbols[j].symbol)
  123. > 0) {
  124. temp = native_symbols[i];
  125. native_symbols[i] = native_symbols[j];
  126. native_symbols[j] = temp;
  127. }
  128. }
  129. }
  130. }
  131. #else
  132. static void
  133. swap_symbol(NativeSymbol *left, NativeSymbol *right)
  134. {
  135. NativeSymbol temp = *left;
  136. *left = *right;
  137. *right = temp;
  138. }
  139. static void
  140. quick_sort_symbols(NativeSymbol *native_symbols, int left, int right)
  141. {
  142. NativeSymbol base_symbol;
  143. int pin_left = left;
  144. int pin_right = right;
  145. if (left >= right) {
  146. return;
  147. }
  148. base_symbol = native_symbols[left];
  149. while (left < right) {
  150. while (left < right
  151. && strcmp(native_symbols[right].symbol, base_symbol.symbol)
  152. > 0) {
  153. right--;
  154. }
  155. if (left < right) {
  156. swap_symbol(&native_symbols[left], &native_symbols[right]);
  157. left++;
  158. }
  159. while (left < right
  160. && strcmp(native_symbols[left].symbol, base_symbol.symbol) < 0) {
  161. left++;
  162. }
  163. if (left < right) {
  164. swap_symbol(&native_symbols[left], &native_symbols[right]);
  165. right--;
  166. }
  167. }
  168. native_symbols[left] = base_symbol;
  169. quick_sort_symbols(native_symbols, pin_left, left - 1);
  170. quick_sort_symbols(native_symbols, left + 1, pin_right);
  171. }
  172. #endif /* end of ENABLE_QUICKSORT */
  173. static void *
  174. lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
  175. const char *symbol, const char **p_signature, void **p_attachment)
  176. {
  177. int low = 0, mid, ret;
  178. int high = (int32)n_native_symbols - 1;
  179. while (low <= high) {
  180. mid = (low + high) / 2;
  181. ret = strcmp(symbol, native_symbols[mid].symbol);
  182. if (ret == 0) {
  183. *p_signature = native_symbols[mid].signature;
  184. *p_attachment = native_symbols[mid].attachment;
  185. return native_symbols[mid].func_ptr;
  186. }
  187. else if (ret < 0)
  188. high = mid - 1;
  189. else
  190. low = mid + 1;
  191. }
  192. return NULL;
  193. }
  194. void *
  195. wasm_native_resolve_symbol(const char *module_name, const char *field_name,
  196. const WASMType *func_type, const char **p_signature,
  197. void **p_attachment, bool *p_call_conv_raw)
  198. {
  199. NativeSymbolsNode *node, *node_next;
  200. const char *signature = NULL;
  201. void *func_ptr = NULL, *attachment;
  202. node = g_native_symbols_list;
  203. while (node) {
  204. node_next = node->next;
  205. if (!strcmp(node->module_name, module_name)) {
  206. if ((func_ptr =
  207. lookup_symbol(node->native_symbols, node->n_native_symbols,
  208. field_name, &signature, &attachment))
  209. || (field_name[0] == '_'
  210. && (func_ptr = lookup_symbol(
  211. node->native_symbols, node->n_native_symbols,
  212. field_name + 1, &signature, &attachment))))
  213. break;
  214. }
  215. node = node_next;
  216. }
  217. if (func_ptr) {
  218. if (signature && signature[0] != '\0') {
  219. /* signature is not empty, check its format */
  220. if (!check_symbol_signature(func_type, signature)) {
  221. #if WASM_ENABLE_WAMR_COMPILER == 0
  222. /* Output warning except running aot compiler */
  223. LOG_WARNING("failed to check signature '%s' and resolve "
  224. "pointer params for import function (%s %s)\n",
  225. signature, module_name, field_name);
  226. #endif
  227. return NULL;
  228. }
  229. else
  230. /* Save signature for runtime to do pointer check and
  231. address conversion */
  232. *p_signature = signature;
  233. }
  234. else
  235. /* signature is empty */
  236. *p_signature = NULL;
  237. *p_attachment = attachment;
  238. *p_call_conv_raw = node->call_conv_raw;
  239. }
  240. return func_ptr;
  241. }
  242. static bool
  243. register_natives(const char *module_name, NativeSymbol *native_symbols,
  244. uint32 n_native_symbols, bool call_conv_raw)
  245. {
  246. NativeSymbolsNode *node;
  247. #if ENABLE_SORT_DEBUG != 0
  248. struct timeval start;
  249. struct timeval end;
  250. unsigned long timer;
  251. #endif
  252. if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
  253. return false;
  254. #if WASM_ENABLE_MEMORY_TRACING != 0
  255. os_printf("Register native, size: %u\n", sizeof(NativeSymbolsNode));
  256. #endif
  257. node->module_name = module_name;
  258. node->native_symbols = native_symbols;
  259. node->n_native_symbols = n_native_symbols;
  260. node->call_conv_raw = call_conv_raw;
  261. /* Add to list head */
  262. node->next = g_native_symbols_list;
  263. g_native_symbols_list = node;
  264. #if ENABLE_SORT_DEBUG != 0
  265. gettimeofday(&start, NULL);
  266. #endif
  267. #if ENABLE_QUICKSORT == 0
  268. sort_symbol_ptr(native_symbols, n_native_symbols);
  269. #else
  270. quick_sort_symbols(native_symbols, 0, (int)(n_native_symbols - 1));
  271. #endif
  272. #if ENABLE_SORT_DEBUG != 0
  273. gettimeofday(&end, NULL);
  274. timer =
  275. 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
  276. LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us", module_name,
  277. n_native_symbols, timer);
  278. #endif
  279. return true;
  280. }
  281. bool
  282. wasm_native_register_natives(const char *module_name,
  283. NativeSymbol *native_symbols,
  284. uint32 n_native_symbols)
  285. {
  286. return register_natives(module_name, native_symbols, n_native_symbols,
  287. false);
  288. }
  289. bool
  290. wasm_native_register_natives_raw(const char *module_name,
  291. NativeSymbol *native_symbols,
  292. uint32 n_native_symbols)
  293. {
  294. return register_natives(module_name, native_symbols, n_native_symbols,
  295. true);
  296. }
  297. bool
  298. wasm_native_unregister_natives(const char *module_name,
  299. NativeSymbol *native_symbols)
  300. {
  301. NativeSymbolsNode **prevp;
  302. NativeSymbolsNode *node;
  303. prevp = &g_native_symbols_list;
  304. while ((node = *prevp) != NULL) {
  305. if (node->native_symbols == native_symbols
  306. && !strcmp(node->module_name, module_name)) {
  307. *prevp = node->next;
  308. wasm_runtime_free(node);
  309. return true;
  310. }
  311. prevp = &node->next;
  312. }
  313. return false;
  314. }
  315. bool
  316. wasm_native_init()
  317. {
  318. NativeSymbol *native_symbols;
  319. uint32 n_native_symbols;
  320. #if WASM_ENABLE_LIBC_BUILTIN != 0
  321. n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
  322. if (!wasm_native_register_natives("env", native_symbols, n_native_symbols))
  323. goto fail;
  324. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  325. #if WASM_ENABLE_SPEC_TEST
  326. n_native_symbols = get_spectest_export_apis(&native_symbols);
  327. if (!wasm_native_register_natives("spectest", native_symbols,
  328. n_native_symbols))
  329. goto fail;
  330. #endif /* WASM_ENABLE_SPEC_TEST */
  331. #if WASM_ENABLE_LIBC_WASI != 0
  332. n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
  333. if (!wasm_native_register_natives("wasi_unstable", native_symbols,
  334. n_native_symbols))
  335. goto fail;
  336. if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols,
  337. n_native_symbols))
  338. goto fail;
  339. #endif
  340. #if WASM_ENABLE_BASE_LIB != 0
  341. n_native_symbols = get_base_lib_export_apis(&native_symbols);
  342. if (n_native_symbols > 0
  343. && !wasm_native_register_natives("env", native_symbols,
  344. n_native_symbols))
  345. goto fail;
  346. #endif
  347. #if WASM_ENABLE_APP_FRAMEWORK != 0
  348. n_native_symbols = get_ext_lib_export_apis(&native_symbols);
  349. if (n_native_symbols > 0
  350. && !wasm_native_register_natives("env", native_symbols,
  351. n_native_symbols))
  352. goto fail;
  353. #endif
  354. #if WASM_ENABLE_LIB_PTHREAD != 0
  355. if (!lib_pthread_init())
  356. goto fail;
  357. n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
  358. if (n_native_symbols > 0
  359. && !wasm_native_register_natives("env", native_symbols,
  360. n_native_symbols))
  361. goto fail;
  362. #endif
  363. #if WASM_ENABLE_LIBC_EMCC != 0
  364. n_native_symbols = get_libc_emcc_export_apis(&native_symbols);
  365. if (n_native_symbols > 0
  366. && !wasm_native_register_natives("env", native_symbols,
  367. n_native_symbols))
  368. goto fail;
  369. #endif /* WASM_ENABLE_LIBC_EMCC */
  370. #if WASM_ENABLE_LIB_RATS != 0
  371. n_native_symbols = get_lib_rats_export_apis(&native_symbols);
  372. if (n_native_symbols > 0
  373. && !wasm_native_register_natives("env", native_symbols,
  374. n_native_symbols))
  375. goto fail;
  376. #endif /* WASM_ENABLE_LIB_RATS */
  377. #if WASM_ENABLE_WASI_NN != 0
  378. n_native_symbols = get_wasi_nn_export_apis(&native_symbols);
  379. if (!wasm_native_register_natives("wasi_nn", native_symbols,
  380. n_native_symbols))
  381. return false;
  382. #endif
  383. return true;
  384. fail:
  385. wasm_native_destroy();
  386. return false;
  387. }
  388. void
  389. wasm_native_destroy()
  390. {
  391. NativeSymbolsNode *node, *node_next;
  392. #if WASM_ENABLE_LIB_PTHREAD != 0
  393. lib_pthread_destroy();
  394. #endif
  395. node = g_native_symbols_list;
  396. while (node) {
  397. node_next = node->next;
  398. wasm_runtime_free(node);
  399. node = node_next;
  400. }
  401. g_native_symbols_list = NULL;
  402. }