wasm_native.c 14 KB

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