wasm_native.c 11 KB

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