ArduPilot / ArduPilot/pymavlink

C implementation: hard fault on Cortex-M0 due to unaligned access to packed structure field

Open
#5 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
728
Forks
739
Avg merge
4d 10h
Merged PRs (30d)
7

Description

The `mavlink_message_t` structure is packed, and `mavlink_finalize_message_chan` and other functions take the address of `checksum` and other fields within the structure. This is illegal because those pointers may not have the required alignment for their type, and it results in hard-faults on Cortex-M0, which does not support unaligned loads.

I've worked around this on Cortex-M0 by removing the packing from the structure. There is no internal padding, so the only effect of packing is the alignment of the entire structure. However, mavlink_helpers.h hard-codes offsets into the structure, and assumes that the layout of the structure is the same as wire format in several places, which is also not guaranteed. The ideal fix would be to keep wire-format data in byte arrays, and native-format messages as un-packed structures, and convert between the two explicitly instead of by pointer cast.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading mavlink_helpers.h and the implementations of mavlink_finalize_message_chan and related functions that take addresses of packed fields. Trace the hard-coded offsets and pointer casts against mavlink_message_t's wire layout, then review how a safe separation between wire-format byte arrays and native-format messages could be validated on Cortex-M0. Done means eliminating the unaligned-access fault without relying on layout assumptions.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.