Skip to content

Commit 030e95d

Browse files
committed
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.
1 parent 95f506a commit 030e95d

4 files changed

Lines changed: 57 additions & 55 deletions

File tree

core/iwasm/aot/aot_loader.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ exchange_uint64(uint8 *p_data)
7777
{
7878
uint32 value;
7979

80-
value = *(uint32 *)p_data;
81-
*(uint32 *)p_data = *(uint32 *)(p_data + 4);
82-
*(uint32 *)(p_data + 4) = value;
80+
value = *(uint32 *)(void *)p_data;
81+
*(uint32 *)(void *)p_data = *(uint32 *)(void *)(p_data + 4);
82+
*(uint32 *)(void *)(p_data + 4) = value;
8383
exchange_uint32(p_data);
8484
exchange_uint32(p_data + 4);
8585
}
@@ -204,21 +204,21 @@ GET_U16_FROM_ADDR(const uint8 *p)
204204

205205
#else /* else of (WASM_ENABLE_WORD_ALIGN_READ != 0) */
206206

207-
#define TEMPLATE_READ(p, p_end, res, type) \
208-
do { \
209-
if (sizeof(type) != sizeof(uint64)) \
210-
p = (uint8 *)align_ptr(p, sizeof(type)); \
211-
else \
212-
/* align 4 bytes if type is uint64 */ \
213-
p = (uint8 *)align_ptr(p, sizeof(uint32)); \
214-
CHECK_BUF(p, p_end, sizeof(type)); \
215-
if (sizeof(type) != sizeof(uint64)) \
216-
res = *(type *)p; \
217-
else \
218-
res = (type)GET_U64_FROM_ADDR((uint32 *)p); \
219-
if (!is_little_endian()) \
220-
exchange_##type((uint8 *)&res); \
221-
p += sizeof(type); \
207+
#define TEMPLATE_READ(p, p_end, res, type) \
208+
do { \
209+
if (sizeof(type) != sizeof(uint64)) \
210+
p = (uint8 *)align_ptr(p, sizeof(type)); \
211+
else \
212+
/* align 4 bytes if type is uint64 */ \
213+
p = (uint8 *)align_ptr(p, sizeof(uint32)); \
214+
CHECK_BUF(p, p_end, sizeof(type)); \
215+
if (sizeof(type) != sizeof(uint64)) \
216+
res = *(type *)(void *)p; \
217+
else \
218+
res = (type)GET_U64_FROM_ADDR((uint32 *)(void *)p); \
219+
if (!is_little_endian()) \
220+
exchange_##type((uint8 *)&res); \
221+
p += sizeof(type); \
222222
} while (0)
223223

224224
/* NOLINTBEGIN, disable lint for this region with clang-tidy */
@@ -3557,7 +3557,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
35573557

35583558
read_uint32(buf, buf_end, symbol_count);
35593559

3560-
symbol_offsets = (uint32 *)buf;
3560+
symbol_offsets = (uint32 *)(void *)buf;
35613561
for (i = 0; i < symbol_count; i++) {
35623562
CHECK_BUF(buf, buf_end, sizeof(uint32));
35633563
buf += sizeof(uint32);
@@ -3709,7 +3709,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
37093709
}
37103710

37113711
group_name = symbol_buf + symbol_offsets[name_index];
3712-
group_name_len = *(uint16 *)group_name;
3712+
group_name_len = *(uint16 *)(void *)group_name;
37133713
group_name += sizeof(uint16);
37143714

37153715
read_uint32(buf, buf_end, relocation_count);
@@ -3735,7 +3735,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
37353735
}
37363736

37373737
symbol_name = symbol_buf + symbol_offsets[symbol_index];
3738-
symbol_name_len = *(uint16 *)symbol_name;
3738+
symbol_name_len = *(uint16 *)(void *)symbol_name;
37393739
symbol_name += sizeof(uint16);
37403740

37413741
bh_memcpy_s(group_name_buf, (uint32)sizeof(group_name_buf),

core/shared/mem-alloc/ems/ems_alloc.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ remove_tree_node(gc_heap_t *heap, hmu_tree_node_t *p)
6666
/* get the slot which holds pointer to node p */
6767
if (p == p->parent->right) {
6868
/* Don't use `slot = &p->parent->right` to avoid compiler warning */
69-
slot = (hmu_tree_node_t **)((uint8 *)p->parent
69+
slot = (hmu_tree_node_t **)((uintptr_t)p->parent
7070
+ offsetof(hmu_tree_node_t, right));
7171
}
7272
else if (p == p->parent->left) {
7373
/* p should be a child of its parent */
7474
/* Don't use `slot = &p->parent->left` to avoid compiler warning */
75-
slot = (hmu_tree_node_t **)((uint8 *)p->parent
75+
slot = (hmu_tree_node_t **)((uintptr_t)p->parent
7676
+ offsetof(hmu_tree_node_t, left));
7777
}
7878
else {
@@ -237,7 +237,7 @@ hmu_set_free_size(hmu_t *hmu)
237237
bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC);
238238

239239
size = hmu_get_size(hmu);
240-
*((uint32 *)((char *)hmu + size) - 1) = size;
240+
*((uint32 *)((uintptr_t)hmu + size) - 1) = size;
241241
}
242242

243243
/**
@@ -398,15 +398,15 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size)
398398
if ((gc_size_t)node_idx != (uint32)init_node_idx
399399
/* with bigger size*/
400400
&& ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) {
401-
rest = (hmu_t *)(((char *)p) + size);
401+
rest = (hmu_t *)(((uintptr_t)p) + size);
402402
if (!gci_add_fc(heap, rest, (node_idx << 3) - size)) {
403403
return NULL;
404404
}
405405
hmu_mark_pinuse(rest);
406406
}
407407
else {
408408
size = node_idx << 3;
409-
next = (hmu_t *)((char *)p + size);
409+
next = (hmu_t *)((uintptr_t)p + size);
410410
if (hmu_is_in_heap(next, base_addr, end_addr))
411411
hmu_mark_pinuse(next);
412412
}
@@ -456,14 +456,14 @@ alloc_hmu(gc_heap_t *heap, gc_size_t size)
456456
return NULL;
457457

458458
if (last_tp->size >= size + GC_SMALLEST_SIZE) {
459-
rest = (hmu_t *)((char *)last_tp + size);
459+
rest = (hmu_t *)((uintptr_t)last_tp + size);
460460
if (!gci_add_fc(heap, rest, last_tp->size - size))
461461
return NULL;
462462
hmu_mark_pinuse(rest);
463463
}
464464
else {
465465
size = last_tp->size;
466-
next = (hmu_t *)((char *)last_tp + size);
466+
next = (hmu_t *)((uintptr_t)last_tp + size);
467467
if (hmu_is_in_heap(next, base_addr, end_addr))
468468
hmu_mark_pinuse(next);
469469
}
@@ -658,7 +658,7 @@ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file,
658658
LOCK_HEAP(heap);
659659

660660
if (hmu_old) {
661-
hmu_next = (hmu_t *)((char *)hmu_old + tot_size_old);
661+
hmu_next = (hmu_t *)((uintptr_t)hmu_old + tot_size_old);
662662
if (hmu_is_in_heap(hmu_next, base_addr, end_addr)) {
663663
ut = hmu_get_ut(hmu_next);
664664
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,
675675
hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line);
676676
#endif
677677
if (tot_size < tot_size_old + tot_size_next) {
678-
hmu_next = (hmu_t *)((char *)hmu_old + tot_size);
678+
hmu_next = (hmu_t *)((uintptr_t)hmu_old + tot_size);
679679
tot_size_next = tot_size_old + tot_size_next - tot_size;
680680
if (!gci_add_fc(heap, hmu_next, tot_size_next)) {
681681
UNLOCK_HEAP(heap);
@@ -889,7 +889,7 @@ gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line)
889889
#endif
890890

891891
if (!hmu_get_pinuse(hmu)) {
892-
prev = (hmu_t *)((char *)hmu - *((int *)hmu - 1));
892+
prev = (hmu_t *)((uintptr_t)hmu - *((int *)hmu - 1));
893893

894894
if (hmu_is_in_heap(prev, base_addr, end_addr)
895895
&& hmu_get_ut(prev) == HMU_FC) {
@@ -902,15 +902,15 @@ gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line)
902902
}
903903
}
904904

905-
next = (hmu_t *)((char *)hmu + size);
905+
next = (hmu_t *)((uintptr_t)hmu + size);
906906
if (hmu_is_in_heap(next, base_addr, end_addr)) {
907907
if (hmu_get_ut(next) == HMU_FC) {
908908
size += hmu_get_size(next);
909909
if (!unlink_hmu(heap, next)) {
910910
ret = GC_ERROR;
911911
goto out;
912912
}
913-
next = (hmu_t *)((char *)hmu + size);
913+
next = (hmu_t *)((uintptr_t)hmu + size);
914914
}
915915
}
916916

@@ -966,8 +966,8 @@ gci_dump(gc_heap_t *heap)
966966
int i = 0, p, mark;
967967
char inuse = 'U';
968968

969-
cur = (hmu_t *)heap->base_addr;
970-
end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
969+
cur = (void *)heap->base_addr;
970+
end = (hmu_t *)((uintptr_t)heap->base_addr + heap->current_size);
971971

972972
while (cur < end) {
973973
ut = hmu_get_ut(cur);
@@ -1001,7 +1001,7 @@ gci_dump(gc_heap_t *heap)
10011001
}
10021002
#endif
10031003

1004-
cur = (hmu_t *)((char *)cur + size);
1004+
cur = (hmu_t *)((uintptr_t)cur + size);
10051005
i++;
10061006
}
10071007

core/shared/mem-alloc/ems/ems_gc_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ hmu_verify(void *vheap, hmu_t *hmu);
114114
#define HMU_SIZE (sizeof(hmu_t))
115115

116116
#define hmu_to_obj(hmu) (gc_object_t)(SKIP_OBJ_PREFIX((hmu_t *)(hmu) + 1))
117-
#define obj_to_hmu(obj) ((hmu_t *)((gc_uint8 *)(obj)-OBJ_PREFIX_SIZE) - 1)
117+
#define obj_to_hmu(obj) ((hmu_t *)((uintptr_t)(obj)-OBJ_PREFIX_SIZE) - 1)
118118

119119
#define HMU_UT_SIZE 2
120120
#define HMU_UT_OFFSET 30
@@ -188,7 +188,7 @@ static inline hmu_normal_node_t *
188188
get_hmu_normal_node_next(hmu_normal_node_t *node)
189189
{
190190
return node->next_offset
191-
? (hmu_normal_node_t *)((uint8 *)node + node->next_offset)
191+
? (hmu_normal_node_t *)((uintptr_t)node + node->next_offset)
192192
: NULL;
193193
}
194194

core/shared/mem-alloc/ems/ems_kfc.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size)
3232
gc_update_threshold(heap);
3333
#endif
3434

35-
root = heap->kfc_tree_root = (hmu_tree_node_t *)heap->kfc_tree_root_buf;
35+
root = heap->kfc_tree_root = (void *)(heap->kfc_tree_root_buf);
3636
memset(root, 0, sizeof *root);
3737
root->size = sizeof *root;
3838
hmu_set_ut(&root->hmu_header, HMU_FC);
3939
hmu_set_size(&root->hmu_header, sizeof *root);
4040

41-
q = (hmu_tree_node_t *)heap->base_addr;
41+
q = (void *)base_addr;
4242
memset(q, 0, sizeof *q);
4343
hmu_set_ut(&q->hmu_header, HMU_FC);
4444
hmu_set_size(&q->hmu_header, heap->current_size);
@@ -60,8 +60,8 @@ gc_handle_t
6060
gc_init_with_pool(char *buf, gc_size_t buf_size)
6161
{
6262
char *buf_end = buf + buf_size;
63-
char *buf_aligned = (char *)(((uintptr_t)buf + 7) & (uintptr_t)~7);
64-
char *base_addr = buf_aligned + sizeof(gc_heap_t);
63+
uintptr_t buf_aligned = ((uintptr_t)buf + 7) & (uintptr_t)~7;
64+
char *base_addr = (char *)(buf_aligned + sizeof(gc_heap_t));
6565
gc_heap_t *heap = (gc_heap_t *)buf_aligned;
6666
gc_size_t heap_max_size;
6767

@@ -89,7 +89,9 @@ gc_handle_t
8989
gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
9090
char *pool_buf, gc_size_t pool_buf_size)
9191
{
92-
gc_heap_t *heap = (gc_heap_t *)struct_buf;
92+
/*struct_buf is allocated with the size of gc_heap_t via
93+
* gc_get_heap_struct_size() */
94+
gc_heap_t *heap = (void *)struct_buf;
9395
char *base_addr = pool_buf + GC_HEAD_PADDING;
9496
char *pool_buf_end = pool_buf + pool_buf_size;
9597
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)
270272

271273
ASSERT_TREE_NODE_ALIGNED_ACCESS(heap->kfc_tree_root);
272274

273-
p_left = (uint8 **)((uint8 *)heap->kfc_tree_root
275+
p_left = (uint8 **)((uintptr_t)heap->kfc_tree_root
274276
+ offsetof(hmu_tree_node_t, left));
275-
p_right = (uint8 **)((uint8 *)heap->kfc_tree_root
277+
p_right = (uint8 **)((uintptr_t)heap->kfc_tree_root
276278
+ offsetof(hmu_tree_node_t, right));
277-
p_parent = (uint8 **)((uint8 *)heap->kfc_tree_root
279+
p_parent = (uint8 **)((uintptr_t)heap->kfc_tree_root
278280
+ offsetof(hmu_tree_node_t, parent));
279281
adjust_ptr(p_left, offset);
280282
adjust_ptr(p_right, offset);
281283
adjust_ptr(p_parent, offset);
282284

283-
cur = (hmu_t *)heap->base_addr;
284-
end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
285+
cur = (void *)heap->base_addr;
286+
end = (hmu_t *)((uintptr_t)heap->base_addr + heap->current_size);
285287

286288
while (cur < end) {
287289
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)
299301

300302
ASSERT_TREE_NODE_ALIGNED_ACCESS(tree_node);
301303

302-
p_left = (uint8 **)((uint8 *)tree_node
304+
p_left = (uint8 **)((uintptr_t)tree_node
303305
+ offsetof(hmu_tree_node_t, left));
304-
p_right = (uint8 **)((uint8 *)tree_node
306+
p_right = (uint8 **)((uintptr_t)tree_node
305307
+ offsetof(hmu_tree_node_t, right));
306-
p_parent = (uint8 **)((uint8 *)tree_node
308+
p_parent = (uint8 **)((uintptr_t)tree_node
307309
+ offsetof(hmu_tree_node_t, parent));
308310
adjust_ptr(p_left, offset);
309311
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)
312314
it is fixed part and isn't changed. */
313315
adjust_ptr(p_parent, offset);
314316
}
315-
cur = (hmu_t *)((char *)cur + size);
317+
cur = (hmu_t *)((uintptr_t)cur + size);
316318
}
317319

318320
#if BH_ENABLE_GC_CORRUPTION_CHECK != 0
@@ -366,8 +368,8 @@ gc_heap_stat(void *heap_ptr, gc_stat_t *stat)
366368
gc_heap_t *heap = (gc_heap_t *)heap_ptr;
367369

368370
memset(stat, 0, sizeof(gc_stat_t));
369-
cur = (hmu_t *)heap->base_addr;
370-
end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
371+
cur = (void *)heap->base_addr;
372+
end = (hmu_t *)((uintptr_t)heap->base_addr + heap->current_size);
371373

372374
while (cur < end) {
373375
ut = hmu_get_ut(cur);
@@ -401,7 +403,7 @@ gc_heap_stat(void *heap_ptr, gc_stat_t *stat)
401403
stat->usage_sizes[GC_HEAP_STAT_SIZE - 1] += 1;
402404
}
403405

404-
cur = (hmu_t *)((char *)cur + size);
406+
cur = (hmu_t *)((uintptr_t)cur + size);
405407
}
406408
}
407409

0 commit comments

Comments
 (0)