wasm_native.c 15 KB

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