From 030e95d15dc91e755b313320d3a7af36c4910ba0 Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Tue, 16 Sep 2025 04:36:48 +0000 Subject: [PATCH 1/2] fix some `-Wcast-align` warning - If it involves pointer arithmetic, especially with `uint8*`, use `uintptr_t` instead. - If it involves pointer type conversion, such as via `char*`, use `void*` and implicit conversion. --- core/iwasm/aot/aot_loader.c | 42 ++++++++++----------- core/shared/mem-alloc/ems/ems_alloc.c | 30 +++++++-------- core/shared/mem-alloc/ems/ems_gc_internal.h | 4 +- core/shared/mem-alloc/ems/ems_kfc.c | 36 +++++++++--------- 4 files changed, 57 insertions(+), 55 deletions(-) diff --git a/core/iwasm/aot/aot_loader.c b/core/iwasm/aot/aot_loader.c index 2f7e6c9f25..ae69e1ff5c 100644 --- a/core/iwasm/aot/aot_loader.c +++ b/core/iwasm/aot/aot_loader.c @@ -77,9 +77,9 @@ exchange_uint64(uint8 *p_data) { uint32 value; - value = *(uint32 *)p_data; - *(uint32 *)p_data = *(uint32 *)(p_data + 4); - *(uint32 *)(p_data + 4) = value; + value = *(uint32 *)(void *)p_data; + *(uint32 *)(void *)p_data = *(uint32 *)(void *)(p_data + 4); + *(uint32 *)(void *)(p_data + 4) = value; exchange_uint32(p_data); exchange_uint32(p_data + 4); } @@ -204,21 +204,21 @@ GET_U16_FROM_ADDR(const uint8 *p) #else /* else of (WASM_ENABLE_WORD_ALIGN_READ != 0) */ -#define TEMPLATE_READ(p, p_end, res, type) \ - do { \ - if (sizeof(type) != sizeof(uint64)) \ - p = (uint8 *)align_ptr(p, sizeof(type)); \ - else \ - /* align 4 bytes if type is uint64 */ \ - p = (uint8 *)align_ptr(p, sizeof(uint32)); \ - CHECK_BUF(p, p_end, sizeof(type)); \ - if (sizeof(type) != sizeof(uint64)) \ - res = *(type *)p; \ - else \ - res = (type)GET_U64_FROM_ADDR((uint32 *)p); \ - if (!is_little_endian()) \ - exchange_##type((uint8 *)&res); \ - p += sizeof(type); \ +#define TEMPLATE_READ(p, p_end, res, type) \ + do { \ + if (sizeof(type) != sizeof(uint64)) \ + p = (uint8 *)align_ptr(p, sizeof(type)); \ + else \ + /* align 4 bytes if type is uint64 */ \ + p = (uint8 *)align_ptr(p, sizeof(uint32)); \ + CHECK_BUF(p, p_end, sizeof(type)); \ + if (sizeof(type) != sizeof(uint64)) \ + res = *(type *)(void *)p; \ + else \ + res = (type)GET_U64_FROM_ADDR((uint32 *)(void *)p); \ + if (!is_little_endian()) \ + exchange_##type((uint8 *)&res); \ + p += sizeof(type); \ } while (0) /* NOLINTBEGIN, disable lint for this region with clang-tidy */ @@ -3557,7 +3557,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, read_uint32(buf, buf_end, symbol_count); - symbol_offsets = (uint32 *)buf; + symbol_offsets = (uint32 *)(void *)buf; for (i = 0; i < symbol_count; i++) { CHECK_BUF(buf, buf_end, sizeof(uint32)); buf += sizeof(uint32); @@ -3709,7 +3709,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, } group_name = symbol_buf + symbol_offsets[name_index]; - group_name_len = *(uint16 *)group_name; + group_name_len = *(uint16 *)(void *)group_name; group_name += sizeof(uint16); read_uint32(buf, buf_end, relocation_count); @@ -3735,7 +3735,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end, } symbol_name = symbol_buf + symbol_offsets[symbol_index]; - symbol_name_len = *(uint16 *)symbol_name; + symbol_name_len = *(uint16 *)(void *)symbol_name; symbol_name += sizeof(uint16); bh_memcpy_s(group_name_buf, (uint32)sizeof(group_name_buf), diff --git a/core/shared/mem-alloc/ems/ems_alloc.c b/core/shared/mem-alloc/ems/ems_alloc.c index 74214b2246..cd53fc19b8 100644 --- a/core/shared/mem-alloc/ems/ems_alloc.c +++ b/core/shared/mem-alloc/ems/ems_alloc.c @@ -66,13 +66,13 @@ remove_tree_node(gc_heap_t *heap, hmu_tree_node_t *p) /* get the slot which holds pointer to node p */ if (p == p->parent->right) { /* Don't use `slot = &p->parent->right` to avoid compiler warning */ - slot = (hmu_tree_node_t **)((uint8 *)p->parent + slot = (hmu_tree_node_t **)((uintptr_t)p->parent + offsetof(hmu_tree_node_t, right)); } else if (p == p->parent->left) { /* p should be a child of its parent */ /* Don't use `slot = &p->parent->left` to avoid compiler warning */ - slot = (hmu_tree_node_t **)((uint8 *)p->parent + slot = (hmu_tree_node_t **)((uintptr_t)p->parent + offsetof(hmu_tree_node_t, left)); } else { @@ -237,7 +237,7 @@ hmu_set_free_size(hmu_t *hmu) bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC); size = hmu_get_size(hmu); - *((uint32 *)((char *)hmu + size) - 1) = size; + *((uint32 *)((uintptr_t)hmu + size) - 1) = size; } /** @@ -398,7 +398,7 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size) if ((gc_size_t)node_idx != (uint32)init_node_idx /* with bigger size*/ && ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) { - rest = (hmu_t *)(((char *)p) + size); + rest = (hmu_t *)(((uintptr_t)p) + size); if (!gci_add_fc(heap, rest, (node_idx << 3) - size)) { return NULL; } @@ -406,7 +406,7 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size) } else { size = node_idx << 3; - next = (hmu_t *)((char *)p + size); + next = (hmu_t *)((uintptr_t)p + size); if (hmu_is_in_heap(next, base_addr, end_addr)) hmu_mark_pinuse(next); } @@ -456,14 +456,14 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size) return NULL; if (last_tp->size >= size + GC_SMALLEST_SIZE) { - rest = (hmu_t *)((char *)last_tp + size); + rest = (hmu_t *)((uintptr_t)last_tp + size); if (!gci_add_fc(heap, rest, last_tp->size - size)) return NULL; hmu_mark_pinuse(rest); } else { size = last_tp->size; - next = (hmu_t *)((char *)last_tp + size); + next = (hmu_t *)((uintptr_t)last_tp + size); if (hmu_is_in_heap(next, base_addr, end_addr)) hmu_mark_pinuse(next); } @@ -658,7 +658,7 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file, LOCK_HEAP(heap); if (hmu_old) { - hmu_next = (hmu_t *)((char *)hmu_old + tot_size_old); + hmu_next = (hmu_t *)((uintptr_t)hmu_old + tot_size_old); if (hmu_is_in_heap(hmu_next, base_addr, end_addr)) { ut = hmu_get_ut(hmu_next); tot_size_next = hmu_get_size(hmu_next); @@ -675,7 +675,7 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file, hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line); #endif if (tot_size < tot_size_old + tot_size_next) { - hmu_next = (hmu_t *)((char *)hmu_old + tot_size); + hmu_next = (hmu_t *)((uintptr_t)hmu_old + tot_size); tot_size_next = tot_size_old + tot_size_next - tot_size; if (!gci_add_fc(heap, hmu_next, tot_size_next)) { UNLOCK_HEAP(heap); @@ -889,7 +889,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line) #endif if (!hmu_get_pinuse(hmu)) { - prev = (hmu_t *)((char *)hmu - *((int *)hmu - 1)); + prev = (hmu_t *)((uintptr_t)hmu - *((int *)hmu - 1)); if (hmu_is_in_heap(prev, base_addr, end_addr) && hmu_get_ut(prev) == HMU_FC) { @@ -902,7 +902,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line) } } - next = (hmu_t *)((char *)hmu + size); + next = (hmu_t *)((uintptr_t)hmu + size); if (hmu_is_in_heap(next, base_addr, end_addr)) { if (hmu_get_ut(next) == HMU_FC) { size += hmu_get_size(next); @@ -910,7 +910,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line) ret = GC_ERROR; goto out; } - next = (hmu_t *)((char *)hmu + size); + next = (hmu_t *)((uintptr_t)hmu + size); } } @@ -966,8 +966,8 @@ gci_dump(gc_heap_t *heap) int i = 0, p, mark; char inuse = 'U'; - cur = (hmu_t *)heap->base_addr; - end = (hmu_t *)((char *)heap->base_addr + heap->current_size); + cur = (void *)heap->base_addr; + end = (hmu_t *)((uintptr_t)heap->base_addr + heap->current_size); while (cur < end) { ut = hmu_get_ut(cur); @@ -1001,7 +1001,7 @@ gci_dump(gc_heap_t *heap) } #endif - cur = (hmu_t *)((char *)cur + size); + cur = (hmu_t *)((uintptr_t)cur + size); i++; } diff --git a/core/shared/mem-alloc/ems/ems_gc_internal.h b/core/shared/mem-alloc/ems/ems_gc_internal.h index 605d764dfa..7afc6d76e1 100644 --- a/core/shared/mem-alloc/ems/ems_gc_internal.h +++ b/core/shared/mem-alloc/ems/ems_gc_internal.h @@ -114,7 +114,7 @@ hmu_verify(void *vheap, hmu_t *hmu); #define HMU_SIZE (sizeof(hmu_t)) #define hmu_to_obj(hmu) (gc_object_t)(SKIP_OBJ_PREFIX((hmu_t *)(hmu) + 1)) -#define obj_to_hmu(obj) ((hmu_t *)((gc_uint8 *)(obj)-OBJ_PREFIX_SIZE) - 1) +#define obj_to_hmu(obj) ((hmu_t *)((uintptr_t)(obj)-OBJ_PREFIX_SIZE) - 1) #define HMU_UT_SIZE 2 #define HMU_UT_OFFSET 30 @@ -188,7 +188,7 @@ static inline hmu_normal_node_t * get_hmu_normal_node_next(hmu_normal_node_t *node) { return node->next_offset - ? (hmu_normal_node_t *)((uint8 *)node + node->next_offset) + ? (hmu_normal_node_t *)((uintptr_t)node + node->next_offset) : NULL; } diff --git a/core/shared/mem-alloc/ems/ems_kfc.c b/core/shared/mem-alloc/ems/ems_kfc.c index 2d5a4b13ea..d80716c1ef 100644 --- a/core/shared/mem-alloc/ems/ems_kfc.c +++ b/core/shared/mem-alloc/ems/ems_kfc.c @@ -32,13 +32,13 @@ gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size) gc_update_threshold(heap); #endif - root = heap->kfc_tree_root = (hmu_tree_node_t *)heap->kfc_tree_root_buf; + root = heap->kfc_tree_root = (void *)(heap->kfc_tree_root_buf); memset(root, 0, sizeof *root); root->size = sizeof *root; hmu_set_ut(&root->hmu_header, HMU_FC); hmu_set_size(&root->hmu_header, sizeof *root); - q = (hmu_tree_node_t *)heap->base_addr; + q = (void *)base_addr; memset(q, 0, sizeof *q); hmu_set_ut(&q->hmu_header, HMU_FC); hmu_set_size(&q->hmu_header, heap->current_size); @@ -60,8 +60,8 @@ gc_handle_t gc_init_with_pool(char *buf, gc_size_t buf_size) { char *buf_end = buf + buf_size; - char *buf_aligned = (char *)(((uintptr_t)buf + 7) & (uintptr_t)~7); - char *base_addr = buf_aligned + sizeof(gc_heap_t); + uintptr_t buf_aligned = ((uintptr_t)buf + 7) & (uintptr_t)~7; + char *base_addr = (char *)(buf_aligned + sizeof(gc_heap_t)); gc_heap_t *heap = (gc_heap_t *)buf_aligned; gc_size_t heap_max_size; @@ -89,7 +89,9 @@ gc_handle_t gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size, char *pool_buf, gc_size_t pool_buf_size) { - gc_heap_t *heap = (gc_heap_t *)struct_buf; + /*struct_buf is allocated with the size of gc_heap_t via + * gc_get_heap_struct_size() */ + gc_heap_t *heap = (void *)struct_buf; char *base_addr = pool_buf + GC_HEAD_PADDING; char *pool_buf_end = pool_buf + pool_buf_size; gc_size_t heap_max_size; @@ -270,18 +272,18 @@ gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size) ASSERT_TREE_NODE_ALIGNED_ACCESS(heap->kfc_tree_root); - p_left = (uint8 **)((uint8 *)heap->kfc_tree_root + p_left = (uint8 **)((uintptr_t)heap->kfc_tree_root + offsetof(hmu_tree_node_t, left)); - p_right = (uint8 **)((uint8 *)heap->kfc_tree_root + p_right = (uint8 **)((uintptr_t)heap->kfc_tree_root + offsetof(hmu_tree_node_t, right)); - p_parent = (uint8 **)((uint8 *)heap->kfc_tree_root + p_parent = (uint8 **)((uintptr_t)heap->kfc_tree_root + offsetof(hmu_tree_node_t, parent)); adjust_ptr(p_left, offset); adjust_ptr(p_right, offset); adjust_ptr(p_parent, offset); - cur = (hmu_t *)heap->base_addr; - end = (hmu_t *)((char *)heap->base_addr + heap->current_size); + cur = (void *)heap->base_addr; + end = (hmu_t *)((uintptr_t)heap->base_addr + heap->current_size); while (cur < end) { size = hmu_get_size(cur); @@ -299,11 +301,11 @@ gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size) ASSERT_TREE_NODE_ALIGNED_ACCESS(tree_node); - p_left = (uint8 **)((uint8 *)tree_node + p_left = (uint8 **)((uintptr_t)tree_node + offsetof(hmu_tree_node_t, left)); - p_right = (uint8 **)((uint8 *)tree_node + p_right = (uint8 **)((uintptr_t)tree_node + offsetof(hmu_tree_node_t, right)); - p_parent = (uint8 **)((uint8 *)tree_node + p_parent = (uint8 **)((uintptr_t)tree_node + offsetof(hmu_tree_node_t, parent)); adjust_ptr(p_left, offset); adjust_ptr(p_right, offset); @@ -312,7 +314,7 @@ gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size) it is fixed part and isn't changed. */ adjust_ptr(p_parent, offset); } - cur = (hmu_t *)((char *)cur + size); + cur = (hmu_t *)((uintptr_t)cur + size); } #if BH_ENABLE_GC_CORRUPTION_CHECK != 0 @@ -366,8 +368,8 @@ gc_heap_stat(void *heap_ptr, gc_stat_t *stat) gc_heap_t *heap = (gc_heap_t *)heap_ptr; memset(stat, 0, sizeof(gc_stat_t)); - cur = (hmu_t *)heap->base_addr; - end = (hmu_t *)((char *)heap->base_addr + heap->current_size); + cur = (void *)heap->base_addr; + end = (hmu_t *)((uintptr_t)heap->base_addr + heap->current_size); while (cur < end) { ut = hmu_get_ut(cur); @@ -401,7 +403,7 @@ gc_heap_stat(void *heap_ptr, gc_stat_t *stat) stat->usage_sizes[GC_HEAP_STAT_SIZE - 1] += 1; } - cur = (hmu_t *)((char *)cur + size); + cur = (hmu_t *)((uintptr_t)cur + size); } } From 3fd0d784a2f072e4ff8e20bd2c210ad5ad4fb62d Mon Sep 17 00:00:00 2001 From: "liang.he@intel.com" Date: Tue, 16 Sep 2025 04:45:04 +0000 Subject: [PATCH 2/2] add strict cast alignment warning for better type safety --- build-scripts/warnings.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/build-scripts/warnings.cmake b/build-scripts/warnings.cmake index fbe6966ccc..d48e3883a2 100644 --- a/build-scripts/warnings.cmake +++ b/build-scripts/warnings.cmake @@ -36,6 +36,7 @@ else () # options benefit embedded system. add_compile_options ( -Wdouble-promotion + -Wcast-align=strict ) # waivers