Recently I've gotten around to implementing the Undefined Behaviour Sanitizer
(UBSan) handler functions for GCC's UBSan in my kernel. I now compile both the
kernel and C library with -fsanitize=undefined and I've included it in my
automated tests. This all works as expected. With the tests I encountered
only one error with misaligned member access.
The kernel I'm writing is Multiboot 2 compliant. Multiboot 2 provides a structure
to the kernel at boot which contains various tags. These tags can contain
different kinds of information, for example a tag for the memory map and one for
the kernel ELF symbols. It is this ELF tag that causes the issue
(https://www.gnu.org/software/grub/manua ... iboot.html section
3.6.7 ELF-Symbols). I use the ELF tag in the vmm (among others parts) to mark
kernel pages appropriately (e.g. writable or no-exec). The tag is defined as
follows.
Code: Select all
u32 - type
u32 - size
u32 - num
u32 - entsize
u32 - shndx
varies - section headers
error as both the examples given in the specification and grub use the above
structure. The section headers part is the section header table.
I have an elf64_shdr structure for ELF-64 objects. I use a function to iterate
through all section headers given in the Multiboot 2 tag. This function calls a
given callback with a pointer to the section header. However, from the
structure that is given above you can see that the section headers are 4-byte
aligned (it starts at offset 20). It is this that causes a 'member access
within misaligned address X for type struct elf64_shdr which requires 8 byte
alignment' when I access a member from the elf64_shdr struct in the callback.
Everything works in terms of functionality. The data is correct and the kernel
works properly but I don't know how to get around this misaligned access.
I could copy the section headers to somewhere where they are aligned but
this seems rather wasteful.