wasm_native.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. typedef struct NativeSymbol {
  7. const char *symbol;
  8. void *func_ptr;
  9. } NativeSymbol;
  10. static bool
  11. sort_symbol_ptr(NativeSymbol *ptr, int len)
  12. {
  13. int i, j;
  14. NativeSymbol temp;
  15. for (i = 0; i < len - 1; ++i) {
  16. for (j = i + 1; j < len; ++j) {
  17. if (strcmp((ptr+i)->symbol, (ptr+j)->symbol) > 0) {
  18. temp = ptr[i];
  19. ptr[i] = ptr[j];
  20. ptr[j] = temp;
  21. }
  22. }
  23. }
  24. return true;
  25. }
  26. static void *
  27. lookup_symbol(NativeSymbol *ptr, int len, const char *symbol)
  28. {
  29. int low = 0, mid, ret;
  30. int high = len - 1;
  31. while (low <= high) {
  32. mid = (low + high) / 2;
  33. ret = strcmp(symbol, ptr[mid].symbol);
  34. if (ret == 0)
  35. return ptr[mid].func_ptr;
  36. else if (ret < 0)
  37. high = mid - 1;
  38. else
  39. low = mid + 1;
  40. }
  41. return NULL;
  42. }
  43. #if WASM_ENABLE_BASE_LIB != 0
  44. static bool is_base_lib_sorted = false;
  45. static NativeSymbol *base_native_symbol_defs;
  46. static int base_native_symbol_len;
  47. int
  48. get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);
  49. void *
  50. wasm_native_lookup_base_lib_func(const char *module_name,
  51. const char *func_name)
  52. {
  53. void *ret;
  54. if (strcmp(module_name, "env"))
  55. return NULL;
  56. if (!is_base_lib_sorted) {
  57. base_native_symbol_len = get_base_lib_export_apis(&base_native_symbol_defs);
  58. if (base_native_symbol_len > 0)
  59. sort_symbol_ptr(base_native_symbol_defs, base_native_symbol_len);
  60. is_base_lib_sorted = true;
  61. }
  62. if ((ret = lookup_symbol(base_native_symbol_defs, base_native_symbol_len,
  63. func_name))
  64. || (func_name[0] == '_'
  65. && (ret = lookup_symbol(base_native_symbol_defs, base_native_symbol_len,
  66. func_name + 1))))
  67. return ret;
  68. return NULL;
  69. }
  70. #endif /* end of WASM_ENABLE_BASE_LIB */
  71. static bool is_ext_lib_sorted = false;
  72. static NativeSymbol *ext_native_symbol_defs;
  73. static int ext_native_symbol_len;
  74. int
  75. get_ext_lib_export_apis(NativeSymbol **p_ext_lib_apis);
  76. void *
  77. wasm_native_lookup_extension_lib_func(const char *module_name,
  78. const char *func_name)
  79. {
  80. void *ret;
  81. if (strcmp(module_name, "env"))
  82. return NULL;
  83. if (!is_ext_lib_sorted) {
  84. ext_native_symbol_len = get_ext_lib_export_apis(&ext_native_symbol_defs);
  85. if (ext_native_symbol_len > 0)
  86. sort_symbol_ptr(ext_native_symbol_defs, ext_native_symbol_len);
  87. is_ext_lib_sorted = true;
  88. }
  89. if ((ret = lookup_symbol(ext_native_symbol_defs, ext_native_symbol_len,
  90. func_name))
  91. || (func_name[0] == '_'
  92. && (ret = lookup_symbol(ext_native_symbol_defs, ext_native_symbol_len,
  93. func_name + 1))))
  94. return ret;
  95. return NULL;
  96. }