wasm_native.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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
  28. get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
  29. uint32
  30. get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
  31. #if WASM_ENABLE_LIB_PTHREAD != 0
  32. bool
  33. lib_pthread_init();
  34. void
  35. lib_pthread_destroy();
  36. uint32
  37. get_lib_pthread_export_apis(NativeSymbol **p_lib_pthread_apis);
  38. #endif
  39. uint32
  40. get_libc_emcc_export_apis(NativeSymbol **p_libc_emcc_apis);
  41. static bool
  42. check_symbol_signature(const WASMType *type, const char *signature)
  43. {
  44. const char *p = signature, *p_end;
  45. char sig_map[] = { 'F', 'f', 'I', 'i' }, sig;
  46. uint32 i = 0;
  47. if (!p || strlen(p) < 2)
  48. return false;
  49. p_end = p + strlen(signature);
  50. if (*p++ != '(')
  51. return false;
  52. if ((uint32)(p_end - p) < (uint32)(type->param_count + 1))
  53. /* signatures of parameters, and ')' */
  54. return false;
  55. for (i = 0; i < type->param_count; i++) {
  56. sig = *p++;
  57. if ((type->types[i] >= VALUE_TYPE_F64
  58. && type->types[i] <= VALUE_TYPE_I32
  59. && sig == sig_map[type->types[i] - VALUE_TYPE_F64])
  60. #if WASM_ENABLE_REF_TYPES != 0
  61. || (sig == 'i' && type->types[i] == VALUE_TYPE_EXTERNREF)
  62. #endif
  63. )
  64. /* normal parameter */
  65. continue;
  66. if (type->types[i] != VALUE_TYPE_I32)
  67. /* pointer and string must be i32 type */
  68. return false;
  69. if (sig == '*') {
  70. /* it is a pointer */
  71. if (i + 1 < type->param_count
  72. && type->types[i + 1] == VALUE_TYPE_I32
  73. && *p == '~') {
  74. /* pointer length followed */
  75. i++;
  76. p++;
  77. }
  78. }
  79. else if (sig == '$') {
  80. /* it is a string */
  81. }
  82. else {
  83. /* invalid signature */
  84. return false;
  85. }
  86. }
  87. if (*p++ != ')')
  88. return false;
  89. if (type->result_count) {
  90. if (p >= p_end)
  91. return false;
  92. if (*p++ != sig_map[type->types[i] - VALUE_TYPE_F64])
  93. return false;
  94. }
  95. if (*p != '\0')
  96. return false;
  97. return true;
  98. }
  99. #if ENABLE_QUICKSORT == 0
  100. static void
  101. sort_symbol_ptr(NativeSymbol *native_symbols, uint32 n_native_symbols)
  102. {
  103. uint32 i, j;
  104. NativeSymbol temp;
  105. for (i = 0; i < n_native_symbols - 1; i++) {
  106. for (j = i + 1; j < n_native_symbols; j++) {
  107. if (strcmp(native_symbols[i].symbol,
  108. native_symbols[j].symbol) > 0) {
  109. temp = native_symbols[i];
  110. native_symbols[i] = native_symbols[j];
  111. native_symbols[j] = temp;
  112. }
  113. }
  114. }
  115. }
  116. #else
  117. static void
  118. swap_symbol(NativeSymbol* left, NativeSymbol* right)
  119. {
  120. NativeSymbol temp = *left;
  121. *left = *right;
  122. *right = temp;
  123. }
  124. static void
  125. quick_sort_symbols(NativeSymbol* native_symbols, int left, int right)
  126. {
  127. NativeSymbol base_symbol;
  128. int pin_left = left;
  129. int pin_right = right;
  130. if (left >= right) {
  131. return;
  132. }
  133. base_symbol = native_symbols[left];
  134. while (left < right) {
  135. while (left < right
  136. && strcmp(native_symbols[right].symbol,
  137. base_symbol.symbol) > 0) {
  138. right--;
  139. }
  140. if (left < right) {
  141. swap_symbol(&native_symbols[left], &native_symbols[right]);
  142. left++;
  143. }
  144. while (left < right
  145. && strcmp(native_symbols[left].symbol,
  146. base_symbol.symbol) < 0) {
  147. left++;
  148. }
  149. if (left < right) {
  150. swap_symbol(&native_symbols[left], &native_symbols[right]);
  151. right--;
  152. }
  153. }
  154. native_symbols[left] = base_symbol;
  155. quick_sort_symbols(native_symbols, pin_left, left - 1);
  156. quick_sort_symbols(native_symbols, left + 1, pin_right);
  157. }
  158. #endif /* end of ENABLE_QUICKSORT */
  159. static void *
  160. lookup_symbol(NativeSymbol *native_symbols, uint32 n_native_symbols,
  161. const char *symbol, const char **p_signature, void **p_attachment)
  162. {
  163. int low = 0, mid, ret;
  164. int high = (int32)n_native_symbols - 1;
  165. while (low <= high) {
  166. mid = (low + high) / 2;
  167. ret = strcmp(symbol, native_symbols[mid].symbol);
  168. if (ret == 0) {
  169. *p_signature = native_symbols[mid].signature;
  170. *p_attachment = native_symbols[mid].attachment;
  171. return native_symbols[mid].func_ptr;
  172. }
  173. else if (ret < 0)
  174. high = mid - 1;
  175. else
  176. low = mid + 1;
  177. }
  178. return NULL;
  179. }
  180. void*
  181. wasm_native_resolve_symbol(const char *module_name, const char *field_name,
  182. const WASMType *func_type, const char **p_signature,
  183. void **p_attachment, bool *p_call_conv_raw)
  184. {
  185. NativeSymbolsNode *node, *node_next;
  186. const char *signature = NULL;
  187. void *func_ptr = NULL, *attachment;
  188. node = g_native_symbols_list;
  189. while (node) {
  190. node_next = node->next;
  191. if (!strcmp(node->module_name, module_name)) {
  192. if ((func_ptr = lookup_symbol(node->native_symbols,
  193. node->n_native_symbols,
  194. field_name, &signature, &attachment))
  195. || (field_name[0] == '_'
  196. && (func_ptr = lookup_symbol(node->native_symbols,
  197. node->n_native_symbols,
  198. field_name + 1,
  199. &signature, &attachment))))
  200. break;
  201. }
  202. node = node_next;
  203. }
  204. if (func_ptr) {
  205. if (signature && signature[0] != '\0') {
  206. /* signature is not empty, check its format */
  207. if (!check_symbol_signature(func_type, signature)) {
  208. #if WASM_ENABLE_WAMR_COMPILER == 0 /* Output warning except running aot compiler */
  209. LOG_WARNING("failed to check signature '%s' and resolve "
  210. "pointer params for import function (%s %s)\n",
  211. signature, module_name, field_name);
  212. #endif
  213. return NULL;
  214. }
  215. else
  216. /* Save signature for runtime to do pointer check and
  217. address conversion */
  218. *p_signature = signature;
  219. }
  220. else
  221. /* signature is empty */
  222. *p_signature = NULL;
  223. *p_attachment = attachment;
  224. *p_call_conv_raw = node->call_conv_raw;
  225. }
  226. return func_ptr;
  227. }
  228. static bool
  229. register_natives(const char *module_name,
  230. NativeSymbol *native_symbols,
  231. uint32 n_native_symbols,
  232. bool call_conv_raw)
  233. {
  234. NativeSymbolsNode *node;
  235. #if ENABLE_SORT_DEBUG != 0
  236. struct timeval start;
  237. struct timeval end;
  238. unsigned long timer;
  239. #endif
  240. if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
  241. return false;
  242. #if WASM_ENABLE_MEMORY_TRACING != 0
  243. os_printf("Register native, size: %u\n", sizeof(NativeSymbolsNode));
  244. #endif
  245. node->module_name = module_name;
  246. node->native_symbols = native_symbols;
  247. node->n_native_symbols = n_native_symbols;
  248. node->call_conv_raw = call_conv_raw;
  249. /* Add to list head */
  250. node->next = g_native_symbols_list;
  251. g_native_symbols_list = node;
  252. #if ENABLE_SORT_DEBUG != 0
  253. gettimeofday(&start, NULL);
  254. #endif
  255. #if ENABLE_QUICKSORT == 0
  256. sort_symbol_ptr(native_symbols, n_native_symbols);
  257. #else
  258. quick_sort_symbols(native_symbols, 0, (int)(n_native_symbols - 1));
  259. #endif
  260. #if ENABLE_SORT_DEBUG != 0
  261. gettimeofday(&end, NULL);
  262. timer = 1000000 * (end.tv_sec - start.tv_sec)
  263. + (end.tv_usec - start.tv_usec);
  264. LOG_ERROR("module_name: %s, nums: %d, sorted used: %ld us",
  265. module_name, n_native_symbols, timer);
  266. #endif
  267. return true;
  268. }
  269. bool
  270. wasm_native_register_natives(const char *module_name,
  271. NativeSymbol *native_symbols,
  272. uint32 n_native_symbols)
  273. {
  274. return register_natives(module_name, native_symbols, n_native_symbols, false);
  275. }
  276. bool
  277. wasm_native_register_natives_raw(const char *module_name,
  278. NativeSymbol *native_symbols,
  279. uint32 n_native_symbols)
  280. {
  281. return register_natives(module_name, native_symbols, n_native_symbols, true);
  282. }
  283. bool
  284. wasm_native_init()
  285. {
  286. NativeSymbol *native_symbols;
  287. uint32 n_native_symbols;
  288. #if WASM_ENABLE_LIBC_BUILTIN != 0
  289. n_native_symbols = get_libc_builtin_export_apis(&native_symbols);
  290. if (!wasm_native_register_natives("env",
  291. native_symbols, n_native_symbols))
  292. return false;
  293. #endif /* WASM_ENABLE_LIBC_BUILTIN */
  294. #if WASM_ENABLE_SPEC_TEST
  295. n_native_symbols = get_spectest_export_apis(&native_symbols);
  296. if (!wasm_native_register_natives("spectest",
  297. native_symbols, n_native_symbols))
  298. return false;
  299. #endif /* WASM_ENABLE_SPEC_TEST */
  300. #if WASM_ENABLE_LIBC_WASI != 0
  301. n_native_symbols = get_libc_wasi_export_apis(&native_symbols);
  302. if (!wasm_native_register_natives("wasi_unstable",
  303. native_symbols, n_native_symbols))
  304. return false;
  305. if (!wasm_native_register_natives("wasi_snapshot_preview1",
  306. native_symbols, n_native_symbols))
  307. return false;
  308. #endif
  309. #if WASM_ENABLE_BASE_LIB != 0
  310. n_native_symbols = get_base_lib_export_apis(&native_symbols);
  311. if (n_native_symbols > 0
  312. && !wasm_native_register_natives("env",
  313. native_symbols, n_native_symbols))
  314. return false;
  315. #endif
  316. #if WASM_ENABLE_APP_FRAMEWORK != 0
  317. n_native_symbols = get_ext_lib_export_apis(&native_symbols);
  318. if (n_native_symbols > 0
  319. && !wasm_native_register_natives("env",
  320. native_symbols, n_native_symbols))
  321. return false;
  322. #endif
  323. #if WASM_ENABLE_LIB_PTHREAD != 0
  324. if (!lib_pthread_init())
  325. return false;
  326. n_native_symbols = get_lib_pthread_export_apis(&native_symbols);
  327. if (n_native_symbols > 0
  328. && !wasm_native_register_natives("env",
  329. native_symbols, n_native_symbols))
  330. return false;
  331. #endif
  332. #if WASM_ENABLE_LIBC_EMCC != 0
  333. n_native_symbols = get_libc_emcc_export_apis(&native_symbols);
  334. if (n_native_symbols > 0
  335. && !wasm_native_register_natives("env",
  336. native_symbols, n_native_symbols))
  337. return false;
  338. #endif /* WASM_ENABLE_LIBC_EMCC */
  339. return true;
  340. }
  341. void
  342. wasm_native_destroy()
  343. {
  344. NativeSymbolsNode *node, *node_next;
  345. #if WASM_ENABLE_LIB_PTHREAD != 0
  346. lib_pthread_destroy();
  347. #endif
  348. node = g_native_symbols_list;
  349. while (node) {
  350. node_next = node->next;
  351. wasm_runtime_free(node);
  352. node = node_next;
  353. }
  354. g_native_symbols_list = NULL;
  355. }