diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2022-04-10 20:37:51 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2022-04-10 20:37:51 +0300 |
| commit | 4c6be9cb63ff66ce49a6f733c1e00d1889f2c95f (patch) | |
| tree | 898854069101e6616620e2bd732a48e5eca08fd2 /include | |
| parent | 8aa17b12f29536ea9c8b6ca22c7f153e8d90fa3b (diff) | |
| download | kmi-4c6be9cb63ff66ce49a6f733c1e00d1889f2c95f.tar.gz kmi-4c6be9cb63ff66ce49a6f733c1e00d1889f2c95f.zip | |
add basic ubsan and fix issues reported by it
Diffstat (limited to 'include')
| -rw-r--r-- | include/apos/bits.h | 6 | ||||
| -rw-r--r-- | include/apos/mem.h | 4 | ||||
| -rw-r--r-- | include/apos/unaligned.h | 67 | ||||
| -rw-r--r-- | include/libfdt.h | 5 |
4 files changed, 75 insertions, 7 deletions
diff --git a/include/apos/bits.h b/include/apos/bits.h index cc3cd00..699cba4 100644 --- a/include/apos/bits.h +++ b/include/apos/bits.h @@ -8,9 +8,9 @@ #define __set_bit(x, y) ((x) |= (y)) #define __clear_bit(x, y) ((x) &= ~(y)) -#define __is_nset(x, y) (__is_set((x), 1 << (y))) -#define __set_nbit(x, y) (__set_bit((x), 1 << (y))) -#define __clear_nbit(x, y) (__clear_bit((x), 1 << (y))) +#define __is_nset(x, y) (__is_set((x), 1UL << (y))) +#define __set_nbit(x, y) (__set_bit((x), 1UL << (y))) +#define __clear_nbit(x, y) (__clear_bit((x), 1UL << (y))) uint16_t __bswap16(uint16_t u); uint32_t __bswap32(uint32_t u); diff --git a/include/apos/mem.h b/include/apos/mem.h index 7c0fde1..be44104 100644 --- a/include/apos/mem.h +++ b/include/apos/mem.h @@ -30,8 +30,8 @@ #define __o_container(idx) ((idx) / MM_OINFO_WIDTH) #define __o_bit(idx) ((idx) & (MM_OINFO_WIDTH - 1)) -#define __va(x) (((char *)(x)) + VM_DMAP - RAM_BASE) -#define __pa(x) (((char *)(x)) + RAM_BASE - VM_DMAP) +#define __va(x) (void *)(((uintptr_t)(x)) + VM_DMAP - RAM_BASE) +#define __pa(x) (void *)(((uintptr_t)(x)) - VM_DMAP + RAM_BASE) #define __page(x) ((x) / BASE_PAGE_SIZE) #define __addr(x) ((x)*BASE_PAGE_SIZE) #define __pages(x) \ diff --git a/include/apos/unaligned.h b/include/apos/unaligned.h new file mode 100644 index 0000000..1dbd253 --- /dev/null +++ b/include/apos/unaligned.h @@ -0,0 +1,67 @@ +#ifndef APOS_UNALIGNED_H +#define APOS_UNALIGNED_H + +#include <apos/types.h> +#include <apos/attrs.h> + +#define get_unaligned(ptr) \ + _Generic(*(ptr), uint8_t \ + : __get_unaligned_uint8_t, uint16_t \ + : __get_unaligned_uint16_t, uint32_t \ + : __get_unaligned_uint32_t, uint64_t \ + : __get_unaligned_uint64_t, int8_t \ + : __get_unaligned_int8_t, int16_t \ + : __get_unaligned_int16_t, int32_t \ + : __get_unaligned_int32_t, int64_t \ + : __get_unaligned_int64_t)((void *)ptr) + +#define put_unaligned(val, ptr) \ + _Generic(*(ptr), uint8_t \ + : __put_unaligned_uint8_t, uint16_t \ + : __put_unaligned_uint16_t, uint32_t \ + : __put_unaligned_uint32_t, uint64_t \ + : __put_unaligned_uint64_t, int8_t \ + : __put_unaligned_int8_t, int16_t \ + : __put_unaligned_int16_t, int32_t \ + : __put_unaligned_int32_t, int64_t \ + : __put_unaligned_int64_t)(val, (void *)ptr) + +#define DEFINE_GET(type) \ + static inline type __get_unaligned_##type(void *ptr) \ + { \ + const struct __packed { \ + type x; \ + } *__pptr = ptr; \ + return __pptr->x; \ + } + +DEFINE_GET(uint8_t); +DEFINE_GET(uint16_t); +DEFINE_GET(uint32_t); +DEFINE_GET(uint64_t); +DEFINE_GET(int8_t); +DEFINE_GET(int16_t); +DEFINE_GET(int32_t); +DEFINE_GET(int64_t); + +#undef DEFINE_GET + +#define DEFINE_PUT(type) \ + static inline void __put_unaligned_##type(type val, void *ptr) \ + { \ + struct __packed { \ + type x; \ + } *__pptr = ptr; \ + __pptr->x = val; \ + } + +DEFINE_PUT(uint8_t); +DEFINE_PUT(uint16_t); +DEFINE_PUT(uint32_t); +DEFINE_PUT(uint64_t); +DEFINE_PUT(int8_t); +DEFINE_PUT(int16_t); +DEFINE_PUT(int32_t); +DEFINE_PUT(int64_t); + +#endif /* APOS_UNALIGNED_H */ diff --git a/include/libfdt.h b/include/libfdt.h index b59d6e4..ffe979d 100644 --- a/include/libfdt.h +++ b/include/libfdt.h @@ -1,6 +1,7 @@ #ifndef APOS_LIBFDT_H #define APOS_LIBFDT_H #include "../dtc/libfdt/libfdt.h" +#include <apos/unaligned.h> #include <apos/types.h> /* apos additions, implementation can be found in common/fdt.c */ @@ -20,7 +21,7 @@ void __dbg_fdt(const void *fdt, int node_offset, int depth); #endif #define fdt_load_int_ptr(c, p) \ - ((c) == 2 ? fdt64_to_cpu(*(fdt64_t *)(p)) : \ - fdt32_to_cpu(*(fdt32_t *)(p))) + ((c) == 2 ? fdt64_to_cpu(get_unaligned((uint64_t *)p)) : \ + fdt32_to_cpu(get_unaligned((uint32_t *)p))) #endif |
