testsuite: coverage: fix -Wcast-align warning

While porting the coverage.c file from RIOT to Zephyr, which employs
different compiler flags, I noticed several -Wcast-align GCC warnings on
arm. I think, as is, the current implementation may perform unaligned
memory accesses which may not be supported on certain platforms. To
workaround that, I have rewritten the code for RIOT using
bytewise-writes with `memcpy`.

Signed-off-by: Sören Tempel <tempel@uni-bremen.de>
This commit is contained in:
Sören Tempel 2023-01-16 14:35:35 +01:00 committed by Anas Nashif
commit cf34afd2cc

View file

@ -8,6 +8,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include "coverage.h" #include "coverage.h"
K_HEAP_DEFINE(gcov_heap, CONFIG_COVERAGE_GCOV_HEAP_SIZE); K_HEAP_DEFINE(gcov_heap, CONFIG_COVERAGE_GCOV_HEAP_SIZE);
@ -38,12 +39,9 @@ void __gcov_exit(void)
* buff_write_u64 - Store 64 bit data on a buffer and return the size * buff_write_u64 - Store 64 bit data on a buffer and return the size
*/ */
#define MASK_32BIT (0xffffffffUL)
static inline void buff_write_u64(void *buffer, size_t *off, uint64_t v) static inline void buff_write_u64(void *buffer, size_t *off, uint64_t v)
{ {
*((uint32_t *)((uint8_t *)buffer + *off) + 0) = (uint32_t)(v & MASK_32BIT); memcpy((uint8_t *)buffer + *off, (uint8_t *)&v, sizeof(v));
*((uint32_t *)((uint8_t *)buffer + *off) + 1) = (uint32_t)((v >> 32) &
MASK_32BIT);
*off = *off + sizeof(uint64_t); *off = *off + sizeof(uint64_t);
} }
@ -52,7 +50,7 @@ static inline void buff_write_u64(void *buffer, size_t *off, uint64_t v)
*/ */
static inline void buff_write_u32(void *buffer, size_t *off, uint32_t v) static inline void buff_write_u32(void *buffer, size_t *off, uint32_t v)
{ {
*((uint32_t *)((uint8_t *)buffer + *off)) = v; memcpy((uint8_t *)buffer + *off, (uint8_t *)&v, sizeof(v));
*off = *off + sizeof(uint32_t); *off = *off + sizeof(uint32_t);
} }