wasm_native.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 WASMFuncType *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. /**
  203. * allow func_type and all outputs, like p_signature, p_attachment and
  204. * p_call_conv_raw to be NULL
  205. */
  206. void *
  207. wasm_native_resolve_symbol(const char *module_name, const char *field_name,
  208. const WASMFuncType *func_type,
  209. const char **p_signature, void **p_attachment,
  210. bool *p_call_conv_raw)
  211. {
  212. NativeSymbolsNode *node, *node_next;
  213. const char *signature = NULL;
  214. void *func_ptr = NULL, *attachment;
  215. node = g_native_symbols_list;
  216. while (node) {
  217. node_next = node->next;
  218. if (!strcmp(node->module_name, module_name)) {
  219. if ((func_ptr =
  220. lookup_symbol(node->native_symbols, node->n_native_symbols,
  221. field_name, &signature, &attachment))
  222. || (field_name[0] == '_'
  223. && (func_ptr = lookup_symbol(
  224. node->native_symbols, node->n_native_symbols,
  225. field_name + 1, &signature, &attachment))))
  226. break;
  227. }
  228. node = node_next;
  229. }
  230. if (!p_signature || !p_attachment || !p_call_conv_raw)
  231. return func_ptr;
  232. if (func_ptr) {
  233. if (signature && signature[0] != '\0') {
  234. /* signature is not empty, check its format */
  235. if (!func_type || !check_symbol_signature(func_type, signature)) {
  236. #if WASM_ENABLE_WAMR_COMPILER == 0
  237. /* Output warning except running aot compiler */
  238. LOG_WARNING("failed to check signature '%s' and resolve "
  239. "pointer params for import function (%s %s)\n",
  240. signature, module_name, field_name);
  241. #endif
  242. return NULL;
  243. }
  244. else
  245. /* Save signature for runtime to do pointer check and
  246. address conversion */
  247. *p_signature = signature;
  248. }
  249. else
  250. /* signature is empty */
  251. *p_signature = NULL;
  252. *p_attachment = attachment;
  253. *p_call_conv_raw = node->call_conv_raw;
  254. }
  255. return func_ptr;
  256. }
  257. static bool
  258. register_natives(const char *module_name, NativeSymbol *native_symbols,
  259. uint32 n_native_symbols, bool call_conv_raw)
  260. {
  261. NativeSymbolsNode *node;
  262. #if ENABLE_SORT_DEBUG != 0
  263. struct timeval start;
  264. struct timeval end;
  265. unsigned long timer;
  266. #endif
  267. if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
  268. return false;
  269. #if WASM_ENABLE_MEMORY_TRACING != 0
  270. os_printf("Register native, size: %u\n", sizeof(NativeSymbolsNode));
  271. #endif
  272. node->module_name = module_name;
  273. node->native_symbols = native_symbols;
  274. node->n_native_symbols = n_native_symbols;
  275. node->call_conv_raw = call_conv_raw;
  276. /* Add to list head */
  277. node->next = g_native_symbols_list;
  278. g_native_symbols_list = node;
  279. #if ENABLE_SORT_DEBUG != 0
  280. gettimeofday(&start, NULL);
  281. #endif
  282. #if ENABLE_QUICKSORT == 0
  283. sort_symbol_ptr(native_symbols, n_native_symbols);
  284. #else
  285. quick_sort_symbols(native_symbols, 0, (int)(n_native_symbols - 1));
  286. #endif
  287. #if ENABLE_SORT_DEBUG != 0
  288. gettimeofday(&end, NULL);
  289. timer =
  290. 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
  291. LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us", module_name,
  292. n_native_symbols, timer);
  293. #endif
  294. return true;
  295. }
  296. bool
  297. wasm_native_register_natives(const char *module_name,
  298. NativeSymbol *native_symbols,
  299. uint32 n_native_symbols)
  300. {
  301. return register_natives(module_name, native_symbols, n_native_symbols,
  302. false);
  303. }
  304. bool
  305. wasm_native_register_natives_raw(const char *module_name,
  306. NativeSymbol *native_symbols,
  307. uint32 n_native_symbols)
  308. {
  309. return register_natives(module_name, native_symbols, n_native_symbols,
  310. true);
  311. }
  312. bool
  313. wasm_native_unregister_natives(const char *module_name,
  314. NativeSymbol *native_symbols)
  315. {
  316. NativeSymbolsNode **prevp;
  317. NativeSymbolsNode *node;
  318. prevp = &g_native_symbols_list;
  319. while ((node = *prevp) != NULL) {
  320. if (node->native_symbols == native_symbols
  321. && !strcmp(node->module_name, module_name)) {
  322. *prevp = node->next;
  323. wasm_runtime_free(node);
  324. return true;
  325. }
  326. prevp = &node->next;
  327. }
  328. return false;
  329. }
  330. bool
  331. wasm_native_init()
  332. {
  333. #if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
  334. || WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
  335. || WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
  336. || WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
  337. || WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  338. NativeSymbol *native_symbols;
  339. uint32 n_native_symbols;
  340. #endif
  341. #if WASM_ENABLE_LIBC_BUILTIN != 0
  342. n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
  343. if (!wasm_native_register_natives("env", native_symbols, n_native_symbols))
  344. goto fail;
  345. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  346. #if WASM_ENABLE_SPEC_TEST
  347. n_native_symbols = get_spectest_export_apis(&native_symbols);
  348. if (!wasm_native_register_natives("spectest", native_symbols,
  349. n_native_symbols))
  350. goto fail;
  351. #endif /* WASM_ENABLE_SPEC_TEST */
  352. #if WASM_ENABLE_LIBC_WASI != 0
  353. n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
  354. if (!wasm_native_register_natives("wasi_unstable", native_symbols,
  355. n_native_symbols))
  356. goto fail;
  357. if (!wasm_native_register_natives("wasi_snapshot_preview1", native_symbols,
  358. n_native_symbols))
  359. goto fail;
  360. #endif
  361. #if WASM_ENABLE_BASE_LIB != 0
  362. n_native_symbols = get_base_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_APP_FRAMEWORK != 0
  369. n_native_symbols = get_ext_lib_export_apis(&native_symbols);
  370. if (n_native_symbols > 0
  371. && !wasm_native_register_natives("env", native_symbols,
  372. n_native_symbols))
  373. goto fail;
  374. #endif
  375. #if WASM_ENABLE_LIB_PTHREAD != 0
  376. if (!lib_pthread_init())
  377. goto fail;
  378. n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
  379. if (n_native_symbols > 0
  380. && !wasm_native_register_natives("env", native_symbols,
  381. n_native_symbols))
  382. goto fail;
  383. #endif
  384. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  385. if (!lib_wasi_threads_init())
  386. goto fail;
  387. n_native_symbols = get_lib_wasi_threads_export_apis(&native_symbols);
  388. if (n_native_symbols > 0
  389. && !wasm_native_register_natives("wasi", native_symbols,
  390. n_native_symbols))
  391. goto fail;
  392. #endif
  393. #if WASM_ENABLE_LIBC_EMCC != 0
  394. n_native_symbols = get_libc_emcc_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_LIBC_EMCC */
  400. #if WASM_ENABLE_LIB_RATS != 0
  401. n_native_symbols = get_lib_rats_export_apis(&native_symbols);
  402. if (n_native_symbols > 0
  403. && !wasm_native_register_natives("env", native_symbols,
  404. n_native_symbols))
  405. goto fail;
  406. #endif /* WASM_ENABLE_LIB_RATS */
  407. #if WASM_ENABLE_WASI_NN != 0
  408. n_native_symbols = get_wasi_nn_export_apis(&native_symbols);
  409. if (!wasm_native_register_natives("wasi_nn", native_symbols,
  410. n_native_symbols))
  411. goto fail;
  412. #endif
  413. return true;
  414. #if WASM_ENABLE_SPEC_TEST != 0 || WASM_ENABLE_LIBC_BUILTIN != 0 \
  415. || WASM_ENABLE_BASE_LIB != 0 || WASM_ENABLE_LIBC_EMCC != 0 \
  416. || WASM_ENABLE_LIB_RATS != 0 || WASM_ENABLE_WASI_NN != 0 \
  417. || WASM_ENABLE_APP_FRAMEWORK != 0 || WASM_ENABLE_LIBC_WASI != 0 \
  418. || WASM_ENABLE_LIB_PTHREAD != 0 || WASM_ENABLE_LIB_WASI_THREADS != 0
  419. fail:
  420. wasm_native_destroy();
  421. return false;
  422. #endif
  423. }
  424. void
  425. wasm_native_destroy()
  426. {
  427. NativeSymbolsNode *node, *node_next;
  428. #if WASM_ENABLE_LIB_PTHREAD != 0
  429. lib_pthread_destroy();
  430. #endif
  431. #if WASM_ENABLE_LIB_WASI_THREADS != 0
  432. lib_wasi_threads_destroy();
  433. #endif
  434. node = g_native_symbols_list;
  435. while (node) {
  436. node_next = node->next;
  437. wasm_runtime_free(node);
  438. node = node_next;
  439. }
  440. g_native_symbols_list = NULL;
  441. }