From 79f163d08b8614d0a4caa0abeade430a7c237e1a Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Sat, 4 Sep 2021 11:09:30 +0900 Subject: [PATCH] Appease some UBSan complaints (#720) --- core/iwasm/interpreter/wasm_loader.c | 39 ++++++++++--------- .../shared/platform/include/platform_common.h | 5 +++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 6efd59a4..434f11a3 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -8580,26 +8580,27 @@ unsupported_opcode: goto re_scan; func->const_cell_num = loader_ctx->const_cell_num; - if (func->const_cell_num > 0 - && !(func->consts = func_const = + if (func->const_cell_num > 0) { + if (!(func->consts = func_const = loader_malloc(func->const_cell_num * 4, - error_buf, error_buf_size))) { - goto fail; - } - func_const_end = func->consts + func->const_cell_num * 4; - /* reverse the const buf */ - for (int i = loader_ctx->num_const - 1; i >= 0; i--) { - Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const)); - if (c->value_type == VALUE_TYPE_F64 - || c->value_type == VALUE_TYPE_I64) { - bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), - &(c->value.f64), (uint32)sizeof(int64)); - func_const += sizeof(int64); - } - else { - bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), - &(c->value.f32), (uint32)sizeof(int32)); - func_const += sizeof(int32); + error_buf, error_buf_size))) + goto fail; + + func_const_end = func->consts + func->const_cell_num * 4; + /* reverse the const buf */ + for (int i = loader_ctx->num_const - 1; i >= 0; i--) { + Const *c = (Const*)(loader_ctx->const_buf + i * sizeof(Const)); + if (c->value_type == VALUE_TYPE_F64 + || c->value_type == VALUE_TYPE_I64) { + bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), + &(c->value.f64), (uint32)sizeof(int64)); + func_const += sizeof(int64); + } + else { + bh_memcpy_s(func_const, (uint32)(func_const_end - func_const), + &(c->value.f32), (uint32)sizeof(int32)); + func_const += sizeof(int32); + } } } diff --git a/core/shared/platform/include/platform_common.h b/core/shared/platform/include/platform_common.h index 8a331d97..92418f81 100644 --- a/core/shared/platform/include/platform_common.h +++ b/core/shared/platform/include/platform_common.h @@ -80,8 +80,13 @@ int BH_VPRINTF(const char *format, va_list ap); /* Return the offset of the given field in the given type */ #ifndef offsetof +/* GCC 4.0 and later has the builtin. */ +#if defined(__GNUC__) && __GNUC__ >= 4 +#define offsetof(Type, field) __builtin_offsetof(Type, field) +#else #define offsetof(Type, field) ((size_t)(&((Type *)0)->field)) #endif +#endif typedef uint8_t uint8; typedef int8_t int8;