unaligned access fault caused by memcpy in lwip

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

unaligned access fault caused by memcpy in lwip

4,087 Views
creatorwonny
Contributor III

I  am trying to add exception handling functionalities in my software and there is a problem of usage fault by unaligned memory access after enabling the exception handler. calling memcpy  inside netifapi_netif_set_up() caused the fault. What should I do to solve this fault? Is it better to disable the unaligned access fault? I tried to use the options -munaligned-access or -mnounaligned-access and none of them solved the issue.

The following are the information of my project environment:

  • Board: imxrt1060 evk
  • SDK: 2.6.2 (SDK_2.6.2_EVK-MIMXRT1060-FreeRTOS.zip)
  • Project: evkmimxrt1060_lwip_tcpecho_freertos
  • Library: Newlib (semihost)
Labels (1)
0 Kudos
Reply
4 Replies

2,841 Views
snahmad
Contributor II

I am getting same error Active Fault @ queue.c line 1479

Unaligned access (8)

0 Kudos
Reply

3,632 Views
crist_xu
NXP Employee
NXP Employee

Please check your MPU configuration

0 Kudos
Reply

3,951 Views
mjbcswitzerland
Specialist V

Hi

What optimisation do you have enabled?

The GCC compiler version used by MCUXpresso has a bug with misaligned accesses when optimisation is high and if you keep the level no higher that -O1 for code that fails it may work around it.

I have, for example, a misaligned error on the following code if an array of bytes is not long word aligned:

    CBW_READ_10 *ptrRead = (CBW_READ_10 *)ptrCBW->CBWCB;
    ulLogicalBlockAdr = ((ptrRead->ucLogicalBlockAddress[0] << 24) | (ptrRead->ucLogicalBlockAddress[1] << 16) | (ptrRead->ucLogicalBlockAddress[2] << 8) | ptrRead->ucLogicalBlockAddress[3]);

The error doesn't occur with other compilers (eg. IAR or when GCC optimisation is lowered).

The reason for the error on this case (the C code access to 4 bytes is not alignment dependent) is due to the optimiser using an M7 byte reversal instruction to change the endian ordering (very efficient, rather than 4 byte reads and shifts) but it also loads the register with a long word move (not supported by the M7 if not aligned) and so fails in case the byte array happens to not be aligned.

I had the same issue a number of times and in some cases I could force the byte array to be aligned to get around it but generally this is not practical.

You may find that the library routines you use are also using techniques to copy the largest size that is possible (eg. long to long rather than on a byte basis, when possible) and in conjunction with GCC optimisation similar things may go wrong. By watching the disassembled code the error should be visible - if it is reliable with lower optimisation it may be something similar.

Regards

Mark

[uTasker project developer for Kinetis and i.MX RT]

0 Kudos
Reply

3,951 Views
creatorwonny
Contributor III

Hi Mark.

I don't use optimization so the optimization level is -O0. For now, I decided not to enable the unaligned access usage fault because I don't know how long it will take to solve this problem or if it's really possible. Even so, I really want to know if this is possible and how. 

Best Regards,

Wonny

0 Kudos
Reply