Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the workaround for unaligned memory access exception on ARM9 using C?

Tags:

c++

c

embedded

arm

Architecture ARM9. Programming Language C.

We have a third-party stack and one of the calls takes a pointer(pBuffer) to a memory location. Within the stack, they are free to move around the pointer passed and access it as they wish. Unfortunately, they offset the passed in pointer and passed it into a another function that tried to do this from an odd/unalighed memory location

         ((uint16 *)pBuffer)[index] = value;

where value is of type uint16 and index is bounds checked and indexes pBuffer. This causes a unaligned memory access exception. pBuffer points to char * on the heap.

As mentioned, even though we can peek into the third-party stack, we can not update the code officially. So we notify the provider and they provide the update in the next release.

I want to understand if there is a work around for this. How do I perform the above assignment without violating the unaligned access? What is the best approach to resolving such problems.

like image 267
dubnde Avatar asked Jan 24 '23 18:01

dubnde


1 Answers

Copy the value byte by byte. Cast it to a (unsigned) char pointer, and then copy a byte at a time.

It's not pretty, but it doesn't sound like you have many options.

like image 190
jalf Avatar answered Feb 08 '23 17:02

jalf