Skip to content

Commit 1b1ec71

Browse files
authored
wasm loader: Reject v128 for interpreters (#3611)
discussed in: #3592
1 parent 2cf48c8 commit 1b1ec71

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

core/iwasm/common/wasm_loader_common.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ is_valid_value_type(uint8 type)
8585
return false;
8686
}
8787

88+
bool
89+
is_valid_value_type_for_interpreter(uint8 value_type)
90+
{
91+
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0)
92+
/*
93+
* Note: regardless of WASM_ENABLE_SIMD, our interpreters don't have
94+
* SIMD implemented. It's safer to reject v128, especially for the
95+
* fast interpreter.
96+
*/
97+
if (value_type == VALUE_TYPE_V128)
98+
return false;
99+
#endif
100+
return is_valid_value_type(value_type);
101+
}
102+
88103
bool
89104
is_valid_func_type(const WASMFuncType *func_type)
90105
{

core/iwasm/common/wasm_loader_common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ wasm_memory_check_flags(const uint8 mem_flag, char *error_buf,
2020
bool
2121
is_valid_value_type(uint8 value_tpye);
2222

23+
bool
24+
is_valid_value_type_for_interpreter(uint8 value_tpye);
25+
2326
bool
2427
is_valid_func_type(const WASMFuncType *func_type);
2528

@@ -31,4 +34,4 @@ is_indices_overflow(uint32 import, uint32 other, char *error_buf,
3134
}
3235
#endif
3336

34-
#endif /* end of _WASM_LOADER_COMMON_H */
37+
#endif /* end of _WASM_LOADER_COMMON_H */

core/iwasm/interpreter/wasm_loader.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,10 @@ is_packed_type(uint8 type)
334334
static bool
335335
is_byte_a_type(uint8 type)
336336
{
337-
return (is_valid_value_type(type) || (type == VALUE_TYPE_VOID)) ? true
338-
: false;
337+
return (is_valid_value_type_for_interpreter(type)
338+
|| (type == VALUE_TYPE_VOID))
339+
? true
340+
: false;
339341
}
340342

341343
#if WASM_ENABLE_SIMD != 0
@@ -1443,7 +1445,7 @@ resolve_value_type(const uint8 **p_buf, const uint8 *buf_end,
14431445
}
14441446
else {
14451447
/* type which can be represented by one byte */
1446-
if (!is_valid_value_type(type)
1448+
if (!is_valid_value_type_for_interpreter(type)
14471449
&& !(allow_packed_type && is_packed_type(type))) {
14481450
set_error_buf(error_buf, error_buf_size, "type mismatch");
14491451
return false;
@@ -1953,7 +1955,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
19531955
type->types[param_count + j] = read_uint8(p);
19541956
}
19551957
for (j = 0; j < param_count + result_count; j++) {
1956-
if (!is_valid_value_type(type->types[j])) {
1958+
if (!is_valid_value_type_for_interpreter(type->types[j])) {
19571959
set_error_buf(error_buf, error_buf_size,
19581960
"unknown value type");
19591961
return false;
@@ -3049,7 +3051,7 @@ load_global_import(const uint8 **p_buf, const uint8 *buf_end,
30493051
CHECK_BUF(p, p_end, 2);
30503052
/* global type */
30513053
declare_type = read_uint8(p);
3052-
if (!is_valid_value_type(declare_type)) {
3054+
if (!is_valid_value_type_for_interpreter(declare_type)) {
30533055
set_error_buf(error_buf, error_buf_size, "type mismatch");
30543056
return false;
30553057
}
@@ -3766,7 +3768,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
37663768
CHECK_BUF(p_code, buf_code_end, 1);
37673769
/* 0x7F/0x7E/0x7D/0x7C */
37683770
type = read_uint8(p_code);
3769-
if (!is_valid_value_type(type)) {
3771+
if (!is_valid_value_type_for_interpreter(type)) {
37703772
if (type == VALUE_TYPE_V128)
37713773
set_error_buf(error_buf, error_buf_size,
37723774
"v128 value type requires simd feature");
@@ -4046,7 +4048,7 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
40464048
CHECK_BUF(p, p_end, 2);
40474049
/* global type */
40484050
global->type.val_type = read_uint8(p);
4049-
if (!is_valid_value_type(global->type.val_type)) {
4051+
if (!is_valid_value_type_for_interpreter(global->type.val_type)) {
40504052
set_error_buf(error_buf, error_buf_size, "type mismatch");
40514053
return false;
40524054
}
@@ -12367,7 +12369,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
1236712369
#if WASM_ENABLE_GC == 0
1236812370
CHECK_BUF(p, p_end, 1);
1236912371
type = read_uint8(p);
12370-
if (!is_valid_value_type(type)) {
12372+
if (!is_valid_value_type_for_interpreter(type)) {
1237112373
set_error_buf(error_buf, error_buf_size,
1237212374
"unknown value type");
1237312375
goto fail;

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ is_64bit_type(uint8 type)
9191
static bool
9292
is_byte_a_type(uint8 type)
9393
{
94-
return is_valid_value_type(type) || (type == VALUE_TYPE_VOID);
94+
return is_valid_value_type_for_interpreter(type)
95+
|| (type == VALUE_TYPE_VOID);
9596
}
9697

9798
static void
@@ -568,7 +569,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
568569
type->types[param_count + j] = read_uint8(p);
569570
}
570571
for (j = 0; j < param_count + result_count; j++) {
571-
bh_assert(is_valid_value_type(type->types[j]));
572+
bh_assert(is_valid_value_type_for_interpreter(type->types[j]));
572573
}
573574

574575
param_cell_num = wasm_get_cell_num(type->types, param_count);
@@ -1218,7 +1219,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
12181219
CHECK_BUF(p_code, buf_code_end, 1);
12191220
/* 0x7F/0x7E/0x7D/0x7C */
12201221
type = read_uint8(p_code);
1221-
bh_assert(is_valid_value_type(type));
1222+
bh_assert(is_valid_value_type_for_interpreter(type));
12221223
for (k = 0; k < sub_local_count; k++) {
12231224
func->local_types[local_type_index++] = type;
12241225
}
@@ -6828,7 +6829,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
68286829

68296830
CHECK_BUF(p, p_end, 1);
68306831
ref_type = read_uint8(p);
6831-
if (!is_valid_value_type(ref_type)) {
6832+
if (!is_valid_value_type_for_interpreter(ref_type)) {
68326833
set_error_buf(error_buf, error_buf_size,
68336834
"unknown value type");
68346835
goto fail;

0 commit comments

Comments
 (0)