wasm_loader_common.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2024 Amazon Inc. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_loader_common.h"
  6. #include "bh_log.h"
  7. #if WASM_ENABLE_GC != 0
  8. #include "../common/gc/gc_type.h"
  9. #endif
  10. static void
  11. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string,
  12. bool is_aot)
  13. {
  14. if (error_buf != NULL) {
  15. snprintf(error_buf, error_buf_size, "%s module load failed: %s",
  16. is_aot ? "AOT" : "WASM", string);
  17. }
  18. }
  19. bool
  20. wasm_memory_check_flags(const uint8 mem_flag, char *error_buf,
  21. uint32 error_buf_size, bool is_aot)
  22. {
  23. /* Check whether certain features indicated by mem_flag are enabled in
  24. * runtime */
  25. if (mem_flag > MAX_PAGE_COUNT_FLAG) {
  26. #if WASM_ENABLE_SHARED_MEMORY == 0
  27. if (mem_flag & SHARED_MEMORY_FLAG) {
  28. LOG_VERBOSE("shared memory flag was found, please enable shared "
  29. "memory, lib-pthread or lib-wasi-threads");
  30. set_error_buf(error_buf, error_buf_size, "invalid limits flags",
  31. is_aot);
  32. return false;
  33. }
  34. #endif
  35. #if WASM_ENABLE_MEMORY64 == 0
  36. if (mem_flag & MEMORY64_FLAG) {
  37. LOG_VERBOSE("memory64 flag was found, please enable memory64");
  38. set_error_buf(error_buf, error_buf_size, "invalid limits flags",
  39. is_aot);
  40. return false;
  41. }
  42. #endif
  43. }
  44. if (mem_flag > MAX_PAGE_COUNT_FLAG + SHARED_MEMORY_FLAG + MEMORY64_FLAG) {
  45. set_error_buf(error_buf, error_buf_size, "invalid limits flags",
  46. is_aot);
  47. return false;
  48. }
  49. else if ((mem_flag & SHARED_MEMORY_FLAG)
  50. && !(mem_flag & MAX_PAGE_COUNT_FLAG)) {
  51. set_error_buf(error_buf, error_buf_size,
  52. "shared memory must have maximum", is_aot);
  53. return false;
  54. }
  55. return true;
  56. }
  57. /*
  58. * compare with a bigger type set in `wasm_value_type_size_internal()`,
  59. * this function will only cover global value type, function's param
  60. * value type and function's result value type.
  61. *
  62. * please feel free to add more if there are more requirements
  63. */
  64. bool
  65. is_valid_value_type(uint8 type)
  66. {
  67. if (/* I32/I64/F32/F64, 0x7C to 0x7F */
  68. (type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
  69. #if WASM_ENABLE_GC != 0
  70. /* reference types, 0x65 to 0x70 */
  71. || wasm_is_type_reftype(type)
  72. #elif WASM_ENABLE_REF_TYPES != 0
  73. || (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
  74. #endif
  75. #if WASM_ENABLE_SIMD != 0
  76. || type == VALUE_TYPE_V128 /* 0x7B */
  77. #endif
  78. )
  79. return true;
  80. return false;
  81. }
  82. bool
  83. is_valid_value_type_for_interpreter(uint8 value_type)
  84. {
  85. #if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
  86. /*
  87. * Note: regardless of WASM_ENABLE_SIMD, our interpreters don't have
  88. * SIMD implemented. It's safer to reject v128, especially for the
  89. * fast interpreter.
  90. */
  91. if (value_type == VALUE_TYPE_V128)
  92. return false;
  93. #endif
  94. return is_valid_value_type(value_type);
  95. }
  96. bool
  97. is_valid_func_type(const WASMFuncType *func_type)
  98. {
  99. unsigned i;
  100. for (i = 0;
  101. i < (unsigned)(func_type->param_count + func_type->result_count);
  102. i++) {
  103. if (!is_valid_value_type(func_type->types[i]))
  104. return false;
  105. }
  106. return true;
  107. }
  108. /*
  109. * Indices are represented as a u32.
  110. */
  111. bool
  112. is_indices_overflow(uint32 import, uint32 other, char *error_buf,
  113. uint32 error_buf_size)
  114. {
  115. if (import > UINT32_MAX - other) {
  116. snprintf(error_buf, error_buf_size,
  117. "too many items in the index space(%" PRIu32 "+%" PRIu32 ").",
  118. import, other);
  119. return true;
  120. }
  121. return false;
  122. }