wasm_native.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 WASM_ENABLE_INTERP != 0
  9. #include "../interpreter/wasm_runtime.h"
  10. #endif
  11. #if WASM_ENABLE_AOT != 0
  12. #include "../aot/aot_runtime.h"
  13. #endif
  14. #if WASM_ENABLE_THREAD_MGR != 0
  15. #include "../libraries/thread-mgr/thread_manager.h"
  16. #endif
  17. #if !defined(BH_PLATFORM_ZEPHYR) && !defined(BH_PLATFORM_ALIOS_THINGS) \
  18. && !defined(BH_PLATFORM_OPENRTOS) && !defined(BH_PLATFORM_ESP_IDF)
  19. #define ENABLE_QUICKSORT 1
  20. #else
  21. #define ENABLE_QUICKSORT 0
  22. #endif
  23. #define ENABLE_SORT_DEBUG 0
  24. #if ENABLE_SORT_DEBUG != 0
  25. #include <sys/time.h>
  26. #endif
  27. static NativeSymbolsList g_native_symbols_list = NULL;
  28. #if WASM_ENABLE_LIBC_WASI != 0
  29. static void *g_wasi_context_key;
  30. #endif /* WASM_ENABLE_LIBC_WASI */
  31. uint32
  32. get_libc_builtin_export_apis(NativeSymbol **p_libc_builtin_apis);
  33. #if WASM_ENABLE_SPEC_TEST != 0
  34. uint32
  35. get_spectest_export_apis(NativeSymbol **p_libc_builtin_apis);
  36. #endif
  37. uint32
  38. get_libc_wasi_export_apis(NativeSymbol **p_libc_wasi_apis);
  39. uint32_t
  40. get_wasi_nn_export_apis(NativeSymbol **p_libc_wasi_apis);
  41. uint32
  42. get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
  43. uint32
  44. get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
  45. #if WASM_ENABLE_LIB_PTHREAD != 0
  46. bool
  47. lib_pthread_init();
  48. void
  49. lib_pthread_destroy();
  50. uint32
  51. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
  52. #endif
  53. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  54. bool
  55. lib_wasi_threads_init(void);
  56. void
  57. lib_wasi_threads_destroy(void);
  58. uint32
  59. get_lib_wasi_threads_export_apis(NativeSymbol **p_lib_wasi_threads_apis);
  60. #endif
  61. uint32
  62. get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
  63. uint32
  64. get_lib_rats_export_apis(NativeSymbol **p_lib_rats_apis);
  65. static bool
  66. compare_type_with_signautre(uint8 type, const char signature)
  67. {
  68. const char num_sig_map[] = { 'F', 'f', 'I', 'i' };
  69. if (VALUE_TYPE_F64 <= type && type <= VALUE_TYPE_I32
  70. && signature == num_sig_map[type - VALUE_TYPE_F64]) {
  71. return true;
  72. }
  73. #if WASM_ENABLE_REF_TYPES != 0
  74. if ('r' == signature
  75. #if WASM_ENABLE_GC != 0
  76. && (type >= REF_TYPE_NULLREF && type <= REF_TYPE_FUNCREF)
  77. #else
  78. && type == VALUE_TYPE_EXTERNREF
  79. #endif
  80. )
  81. return true;
  82. #endif
  83. /* TODO: a v128 parameter */
  84. return false;
  85. }
  86. static bool
  87. check_symbol_signature(const WASMFuncType *type, const char *signature)
  88. {
  89. const char *p = signature, *p_end;
  90. char sig;
  91. uint32 i = 0;
  92. if (!p || strlen(p) < 2)
  93. return false;
  94. p_end = p + strlen(signature);
  95. if (*p++ != '(')
  96. return false;
  97. if ((uint32)(p_end - p) < (uint32)(type->param_count + 1))
  98. /* signatures of parameters, and ')' */
  99. return false;
  100. for (i = 0; i < type->param_count; i++) {
  101. sig = *p++;
  102. /* a f64/f32/i64/i32/externref parameter */
  103. if (compare_type_with_signautre(type->types[i], sig))
  104. continue;
  105. /* a pointer/string paramter */
  106. if (type->types[i] != VALUE_TYPE_I32)
  107. /* pointer and string must be i32 type */
  108. return false;
  109. if (sig == '*') {
  110. /* it is a pointer */
  111. if (i + 1 < type->param_count
  112. && type->types[i + 1] == VALUE_TYPE_I32 && *p == '~') {
  113. /* pointer length followed */
  114. i++;
  115. p++;
  116. }
  117. }
  118. else if (sig == '$') {
  119. /* it is a string */
  120. }
  121. else {
  122. /* invalid signature */
  123. return false;
  124. }
  125. }
  126. if (*p++ != ')')
  127. return false;
  128. if (type->result_count) {
  129. if (p >= p_end)
  130. return false;
  131. /* result types includes: f64,f32,i64,i32,externref */
  132. if (!compare_type_with_signautre(type->types[i], *p))
  133. return false;
  134. p++;
  135. }
  136. if (*p != '\0')
  137. return false;
  138. return true;
  139. }
  140. #if ENABLE_QUICKSORT == 0
  141. static void
  142. sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
  143. {
  144. uint32 i, j;
  145. NativeSymbol temp;
  146. for (i = 0; i < n_native_symbols - 1; i++) {
  147. for (j = i + 1; j < n_native_symbols; j++) {
  148. if (strcmp(native_symbols[i].symbol, native_symbols[j].symbol)
  149. > 0) {
  150. temp = native_symbols[i];
  151. native_symbols[i] = native_symbols[j];
  152. native_symbols[j] = temp;
  153. }
  154. }
  155. }
  156. }
  157. #else
  158. static void
  159. swap_symbol(NativeSymbol *left, NativeSymbol *right)
  160. {
  161. NativeSymbol temp = *left;
  162. *left = *right;
  163. *right = temp;
  164. }
  165. static void
  166. quick_sort_symbols(NativeSymbol *native_symbols, int left, int right)
  167. {
  168. NativeSymbol base_symbol;
  169. int pin_left = left;
  170. int pin_right = right;
  171. if (left >= right) {
  172. return;
  173. }
  174. base_symbol = native_symbols[left];
  175. while (left < right) {
  176. while (left < right
  177. && strcmp(native_symbols[right].symbol, base_symbol.symbol)
  178. > 0) {
  179. right--;
  180. }
  181. if (left < right) {
  182. swap_symbol(&native_symbols[left], &native_symbols[right]);
  183. left++;
  184. }
  185. while (left < right
  186. && strcmp(native_symbols[left].symbol, base_symbol.symbol) < 0) {
  187. left++;
  188. }
  189. if (left < right) {
  190. swap_symbol(&native_symbols[left], &native_symbols[right]);
  191. right--;
  192. }
  193. }
  194. native_symbols[left] = base_symbol;
  195. quick_sort_symbols(native_symbols, pin_left, left - 1);
  196. quick_sort_symbols(native_symbols, left + 1, pin_right);
  197. }
  198. #endif /* end of ENABLE_QUICKSORT */
  199. static void *
  200. lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
  201. const char *symbol, const char **p_signature, void **p_attachment)
  202. {
  203. int low = 0, mid, ret;
  204. int high = (int32)n_native_symbols - 1;
  205. while (low <= high) {
  206. mid = (low + high) / 2;
  207. ret = strcmp(symbol, native_symbols[mid].symbol);
  208. if (ret == 0) {
  209. *p_signature = native_symbols[mid].signature;
  210. *p_attachment = native_symbols[mid].attachment;
  211. return native_symbols[mid].func_ptr;
  212. }
  213. else if (ret < 0)
  214. high = mid - 1;
  215. else
  216. low = mid + 1;
  217. }
  218. return NULL;
  219. }
  220. /**
  221. * allow func_type and all outputs, like p_signature, p_attachment and
  222. * p_call_conv_raw to be NULL
  223. */
  224. void *
  225. wasm_native_resolve_symbol(const char *module_name, const char *field_name,
  226. const WASMFuncType *func_type,
  227. const char **p_signature, void **p_attachment,
  228. bool *p_call_conv_raw)
  229. {
  230. NativeSymbolsNode *node, *node_next;
  231. const char *signature = NULL;
  232. void *func_ptr = NULL, *attachment;
  233. node = g_native_symbols_list;
  234. while (node) {
  235. node_next = node->next;
  236. if (!strcmp(node->module_name, module_name)) {
  237. if ((func_ptr =
  238. lookup_symbol(node->native_symbols, node->n_native_symbols,
  239. field_name, &signature, &attachment))
  240. || (field_name[0] == '_'
  241. && (func_ptr = lookup_symbol(
  242. node->native_symbols, node->n_native_symbols,
  243. field_name + 1, &signature, &attachment))))
  244. break;
  245. }
  246. node = node_next;
  247. }
  248. if (!p_signature || !p_attachment || !p_call_conv_raw)
  249. return func_ptr;
  250. if (func_ptr) {
  251. if (signature && signature[0] != '\0') {
  252. /* signature is not empty, check its format */
  253. if (!func_type || !check_symbol_signature(func_type, signature)) {
  254. #if WASM_ENABLE_WAMR_COMPILER == 0
  255. /* Output warning except running aot compiler */
  256. LOG_WARNING("failed to check signature '%s' and resolve "
  257. "pointer params for import function (%s %s)\n",
  258. signature, module_name, field_name);
  259. #endif
  260. return NULL;
  261. }
  262. else
  263. /* Save signature for runtime to do pointer check and
  264. address conversion */
  265. *p_signature = signature;
  266. }
  267. else
  268. /* signature is empty */
  269. *p_signature = NULL;
  270. *p_attachment = attachment;
  271. *p_call_conv_raw = node->call_conv_raw;
  272. }
  273. return func_ptr;
  274. }
  275. static bool
  276. register_natives(const char *module_name, NativeSymbol *native_symbols,
  277. uint32 n_native_symbols, bool call_conv_raw)
  278. {
  279. NativeSymbolsNode *node;
  280. #if ENABLE_SORT_DEBUG != 0
  281. struct timeval start;
  282. struct timeval end;
  283. unsigned long timer;
  284. #endif
  285. if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
  286. return false;
  287. #if WASM_ENABLE_MEMORY_TRACING != 0
  288. os_printf("Register native, size: %u\n", sizeof(NativeSymbolsNode));
  289. #endif
  290. node->module_name = module_name;
  291. node->native_symbols = native_symbols;
  292. node->n_native_symbols = n_native_symbols;
  293. node->call_conv_raw = call_conv_raw;
  294. /* Add to list head */
  295. node->next = g_native_symbols_list;
  296. g_native_symbols_list = node;
  297. #if ENABLE_SORT_DEBUG != 0
  298. gettimeofday(&start, NULL);
  299. #endif
  300. #if ENABLE_QUICKSORT == 0
  301. sort_symbol_ptr(native_symbols, n_native_symbols);
  302. #else
  303. quick_sort_symbols(native_symbols, 0, (int)(n_native_symbols - 1));
  304. #endif
  305. #if ENABLE_SORT_DEBUG != 0
  306. gettimeofday(&end, NULL);
  307. timer =
  308. 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
  309. LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us", module_name,
  310. n_native_symbols, timer);
  311. #endif
  312. return true;
  313. }
  314. bool
  315. wasm_native_register_natives(const char *module_name,
  316. NativeSymbol *native_symbols,
  317. uint32 n_native_symbols)
  318. {
  319. return register_natives(module_name, native_symbols, n_native_symbols,
  320. false);
  321. }
  322. bool
  323. wasm_native_register_natives_raw(const char *module_name,
  324. NativeSymbol *native_symbols,
  325. uint32 n_native_symbols)
  326. {
  327. return register_natives(module_name, native_symbols, n_native_symbols,
  328. true);
  329. }
  330. bool
  331. wasm_native_unregister_natives(const char *module_name,
  332. NativeSymbol *native_symbols)
  333. {
  334. NativeSymbolsNode **prevp;
  335. NativeSymbolsNode *node;
  336. prevp = &g_native_symbols_list;
  337. while ((node = *prevp) != NULL) {
  338. if (node->native_symbols == native_symbols
  339. && !strcmp(node->module_name, module_name)) {
  340. *prevp = node->next;
  341. wasm_runtime_free(node);
  342. return true;
  343. }
  344. prevp = &node->next;
  345. }
  346. return false;
  347. }
  348. #if WASM_ENABLE_MODULE_INST_CONTEXT != 0
  349. static uint32
  350. context_key_to_idx(void *key)
  351. {
  352. bh_assert(key != NULL);
  353. uint32 idx = (uint32)(uintptr_t)key;
  354. bh_assert(idx > 0);
  355. bh_assert(idx <= WASM_MAX_INSTANCE_CONTEXTS);
  356. return idx - 1;
  357. }
  358. static void *
  359. context_idx_to_key(uint32 idx)
  360. {
  361. bh_assert(idx < WASM_MAX_INSTANCE_CONTEXTS);
  362. return (void *)(uintptr_t)(idx + 1);
  363. }
  364. typedef void (*dtor_t)(WASMModuleInstanceCommon *, void *);
  365. static dtor_t g_context_dtors[WASM_MAX_INSTANCE_CONTEXTS];
  366. static void
  367. dtor_noop(WASMModuleInstanceCommon *inst, void *ctx)
  368. {}
  369. void *
  370. wasm_native_create_context_key(void (*dtor)(WASMModuleInstanceCommon *inst,
  371. void *ctx))
  372. {
  373. uint32 i;
  374. for (i = 0; i < WASM_MAX_INSTANCE_CONTEXTS; i++) {
  375. if (g_context_dtors[i] == NULL) {
  376. if (dtor == NULL) {
  377. dtor = dtor_noop;
  378. }
  379. g_context_dtors[i] = dtor;
  380. return context_idx_to_key(i);
  381. }
  382. }
  383. LOG_ERROR("failed to allocate instance context key");
  384. return NULL;
  385. }
  386. void
  387. wasm_native_destroy_context_key(void *key)
  388. {
  389. uint32 idx = context_key_to_idx(key);
  390. bh_assert(g_context_dtors[idx] != NULL);
  391. g_context_dtors[idx] = NULL;
  392. }
  393. static WASMModuleInstanceExtraCommon *
  394. wasm_module_inst_extra_common(WASMModuleInstanceCommon *inst)
  395. {
  396. #if WASM_ENABLE_INTERP != 0
  397. if (inst->module_type == Wasm_Module_Bytecode) {
  398. return &((WASMModuleInstance *)inst)->e->common;
  399. }
  400. #endif
  401. #if WASM_ENABLE_AOT != 0
  402. if (inst->module_type == Wasm_Module_AoT) {
  403. return &((AOTModuleInstanceExtra *)((AOTModuleInstance *)inst)->e)
  404. ->common;
  405. }
  406. #endif
  407. bh_assert(false);
  408. return NULL;
  409. }
  410. void
  411. wasm_native_set_context(WASMModuleInstanceCommon *inst, void *key, void *ctx)
  412. {
  413. uint32 idx = context_key_to_idx(key);
  414. WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
  415. common->contexts[idx] = ctx;
  416. }
  417. void
  418. wasm_native_set_context_spread(WASMModuleInstanceCommon *inst, void *key,
  419. void *ctx)
  420. {
  421. #if WASM_ENABLE_THREAD_MGR != 0
  422. wasm_cluster_set_context(inst, key, ctx);
  423. #else
  424. wasm_native_set_context(inst, key, ctx);
  425. #endif
  426. }
  427. void *
  428. wasm_native_get_context(WASMModuleInstanceCommon *inst, void *key)
  429. {
  430. uint32 idx = context_key_to_idx(key);
  431. WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
  432. return common->contexts[idx];
  433. }
  434. void
  435. wasm_native_call_context_dtors(WASMModuleInstanceCommon *inst)
  436. {
  437. WASMModuleInstanceExtraCommon *common = wasm_module_inst_extra_common(inst);
  438. uint32 i;
  439. for (i = 0; i < WASM_MAX_INSTANCE_CONTEXTS; i++) {
  440. dtor_t dtor = g_context_dtors[i];
  441. if (dtor != NULL) {
  442. dtor(inst, common->contexts[i]);
  443. }
  444. }
  445. }
  446. void
  447. wasm_native_inherit_contexts(WASMModuleInstanceCommon *child,
  448. WASMModuleInstanceCommon *parent)
  449. {
  450. WASMModuleInstanceExtraCommon *parent_common =
  451. wasm_module_inst_extra_common(parent);
  452. WASMModuleInstanceExtraCommon *child_common =
  453. wasm_module_inst_extra_common(child);
  454. bh_memcpy_s(child_common->contexts,
  455. sizeof(*child_common->contexts) * WASM_MAX_INSTANCE_CONTEXTS,
  456. parent_common->contexts,
  457. sizeof(*parent_common->contexts) * WASM_MAX_INSTANCE_CONTEXTS);
  458. }
  459. #endif /* WASM_ENABLE_MODULE_INST_CONTEXT != 0 */
  460. #if WASM_ENABLE_LIBC_WASI != 0
  461. WASIContext *
  462. wasm_runtime_get_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm)
  463. {
  464. return wasm_native_get_context(module_inst_comm, g_wasi_context_key);
  465. }
  466. void
  467. wasm_runtime_set_wasi_ctx(WASMModuleInstanceCommon *module_inst_comm,
  468. WASIContext *wasi_ctx)
  469. {
  470. return wasm_native_set_context(module_inst_comm, g_wasi_context_key,
  471. wasi_ctx);
  472. }
  473. static void
  474. wasi_context_dtor(WASMModuleInstanceCommon *inst, void *ctx)
  475. {
  476. if (ctx == NULL) {
  477. return;
  478. }
  479. wasm_runtime_destroy_wasi(inst);
  480. }
  481. #endif /* end of WASM_ENABLE_LIBC_WASI */
  482. bool
  483. wasm_native_init()
  484. {
  485. #if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
  486. || WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
  487. || WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
  488. || WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
  489. || WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  490. NativeSymbol *native_symbols;
  491. uint32 n_native_symbols;
  492. #endif
  493. #if WASM_ENABLE_LIBC_BUILTIN != 0
  494. n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
  495. if (!wasm_native_register_natives("env", native_symbols, n_native_symbols))
  496. goto fail;
  497. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  498. #if WASM_ENABLE_SPEC_TEST
  499. n_native_symbols = get_spectest_export_apis(&native_symbols);
  500. if (!wasm_native_register_natives("spectest", native_symbols,
  501. n_native_symbols))
  502. goto fail;
  503. #endif /* WASM_ENABLE_SPEC_TEST */
  504. #if WASM_ENABLE_LIBC_WASI != 0
  505. g_wasi_context_key = wasm_native_create_context_key(wasi_context_dtor);
  506. if (g_wasi_context_key == NULL) {
  507. goto fail;
  508. }
  509. n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
  510. if (!wasm_native_register_natives("wasi_unstable", native_symbols,
  511. n_native_symbols))
  512. goto fail;
  513. if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols,
  514. n_native_symbols))
  515. goto fail;
  516. #endif
  517. #if WASM_ENABLE_BASE_LIB != 0
  518. n_native_symbols = get_base_lib_export_apis(&native_symbols);
  519. if (n_native_symbols > 0
  520. && !wasm_native_register_natives("env", native_symbols,
  521. n_native_symbols))
  522. goto fail;
  523. #endif
  524. #if WASM_ENABLE_APP_FRAMEWORK != 0
  525. n_native_symbols = get_ext_lib_export_apis(&native_symbols);
  526. if (n_native_symbols > 0
  527. && !wasm_native_register_natives("env", native_symbols,
  528. n_native_symbols))
  529. goto fail;
  530. #endif
  531. #if WASM_ENABLE_LIB_PTHREAD != 0
  532. if (!lib_pthread_init())
  533. goto fail;
  534. n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
  535. if (n_native_symbols > 0
  536. && !wasm_native_register_natives("env", native_symbols,
  537. n_native_symbols))
  538. goto fail;
  539. #endif
  540. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  541. if (!lib_wasi_threads_init())
  542. goto fail;
  543. n_native_symbols = get_lib_wasi_threads_export_apis(&native_symbols);
  544. if (n_native_symbols > 0
  545. && !wasm_native_register_natives("wasi", native_symbols,
  546. n_native_symbols))
  547. goto fail;
  548. #endif
  549. #if WASM_ENABLE_LIBC_EMCC != 0
  550. n_native_symbols = get_libc_emcc_export_apis(&native_symbols);
  551. if (n_native_symbols > 0
  552. && !wasm_native_register_natives("env", native_symbols,
  553. n_native_symbols))
  554. goto fail;
  555. #endif /* WASM_ENABLE_LIBC_EMCC */
  556. #if WASM_ENABLE_LIB_RATS != 0
  557. n_native_symbols = get_lib_rats_export_apis(&native_symbols);
  558. if (n_native_symbols > 0
  559. && !wasm_native_register_natives("env", native_symbols,
  560. n_native_symbols))
  561. goto fail;
  562. #endif /* WASM_ENABLE_LIB_RATS */
  563. #if WASM_ENABLE_WASI_NN != 0
  564. n_native_symbols = get_wasi_nn_export_apis(&native_symbols);
  565. if (!wasm_native_register_natives("wasi_nn", native_symbols,
  566. n_native_symbols))
  567. goto fail;
  568. #endif
  569. return true;
  570. #if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
  571. || WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
  572. || WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
  573. || WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
  574. || WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  575. fail:
  576. wasm_native_destroy();
  577. return false;
  578. #endif
  579. }
  580. void
  581. wasm_native_destroy()
  582. {
  583. NativeSymbolsNode *node, *node_next;
  584. #if WASM_ENABLE_LIBC_WASI != 0
  585. if (g_wasi_context_key != NULL) {
  586. wasm_native_destroy_context_key(g_wasi_context_key);
  587. g_wasi_context_key = NULL;
  588. }
  589. #endif
  590. #if WASM_ENABLE_LIB_PTHREAD != 0
  591. lib_pthread_destroy();
  592. #endif
  593. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  594. lib_wasi_threads_destroy();
  595. #endif
  596. node = g_native_symbols_list;
  597. while (node) {
  598. node_next = node->next;
  599. wasm_runtime_free(node);
  600. node = node_next;
  601. }
  602. g_native_symbols_list = NULL;
  603. }