Skip to content

Commit 79f163d

Browse files
authored
Appease some UBSan complaints (bytecodealliance#720)
1 parent 4e4d48e commit 79f163d

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

core/iwasm/interpreter/wasm_loader.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8580,26 +8580,27 @@ wasm_loader_prepare_bytecode(WASMModule *module,
85808580
goto re_scan;
85818581

85828582
func->const_cell_num = loader_ctx->const_cell_num;
8583-
if (func->const_cell_num > 0
8584-
&& !(func->consts = func_const =
8583+
if (func->const_cell_num > 0) {
8584+
if (!(func->consts = func_const =
85858585
loader_malloc(func->const_cell_num * 4,
8586-
error_buf, error_buf_size))) {
8587-
goto fail;
8588-
}
8589-
func_const_end = func->consts + func->const_cell_num * 4;
8590-
/* reverse the const buf */
8591-
for (int i = loader_ctx->num_const - 1; i >= 0; i--) {
8592-
Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const));
8593-
if (c->value_type == VALUE_TYPE_F64
8594-
|| c->value_type == VALUE_TYPE_I64) {
8595-
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
8596-
&(c->value.f64), (uint32)sizeof(int64));
8597-
func_const += sizeof(int64);
8598-
}
8599-
else {
8600-
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
8601-
&(c->value.f32), (uint32)sizeof(int32));
8602-
func_const += sizeof(int32);
8586+
error_buf, error_buf_size)))
8587+
goto fail;
8588+
8589+
func_const_end = func->consts + func->const_cell_num * 4;
8590+
/* reverse the const buf */
8591+
for (int i = loader_ctx->num_const - 1; i >= 0; i--) {
8592+
Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const));
8593+
if (c->value_type == VALUE_TYPE_F64
8594+
|| c->value_type == VALUE_TYPE_I64) {
8595+
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
8596+
&(c->value.f64), (uint32)sizeof(int64));
8597+
func_const += sizeof(int64);
8598+
}
8599+
else {
8600+
bh_memcpy_s(func_const, (uint32)(func_const_end - func_const),
8601+
&(c->value.f32), (uint32)sizeof(int32));
8602+
func_const += sizeof(int32);
8603+
}
86038604
}
86048605
}
86058606

core/shared/platform/include/platform_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,13 @@ int BH_VPRINTF(const char *format, va_list ap);
8080

8181
/* Return the offset of the given field in the given type */
8282
#ifndef offsetof
83+
/* GCC 4.0 and later has the builtin. */
84+
#if defined(__GNUC__) && __GNUC__ >= 4
85+
#define offsetof(Type, field) __builtin_offsetof(Type, field)
86+
#else
8387
#define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
8488
#endif
89+
#endif
8590

8691
typedef uint8_t uint8;
8792
typedef int8_t int8;

0 commit comments

Comments
 (0)