wasm_native.c 14 KB

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