eclipse-threadx / netxduo

Eclipse ThreadX - NetXDuo is an advanced, industrial-grade TCP/IP network stack designed specifically for deeply embedded real-time and IoT applications
https://github.com/eclipse-threadx/rtos-docs/blob/main/rtos-docs/netx-duo/index.md
MIT License
242 stars 137 forks source link

Longword-aligned access attempted with cast byte pointer #115

Closed leobbditestcom closed 2 years ago

leobbditestcom commented 2 years ago

In nx_tcp_socket_packet_process.c, line 117, a cast pointer is used to access longword data. GCC for ARM Cortex M7 assumes that the recast pointer is longword-aligned and generates a four-register ldm instruction to start the assignment statement. The pointer, in this case, however, is not longword-aligned, and the ldm instruction traps with an unaligned data fault. The assignment, as written, is not generally safe, given the alignment assumptions. This problem went away when the code was changed to use memcpy(), which does not make alignment assumptions about its arguments:

/*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
//tcp_header_copy =  *((NX_TCP_HEADER *)packet_ptr -> nx_packet_prepend_ptr);

// Direct assignment, instead of memcpy(), causes GCC to assume longword
// alignment and generate code which causes an unaligned trap when the casted
// source address is unaligned, on architectures which don't tolerate unaligned
// data.
memcpy(
  &tcp_header_copy, packet_ptr -> nx_packet_prepend_ptr, sizeof(tcp_header_copy)
);
TiejunMS commented 2 years ago

There is a requirement for incoming packet. The IP header must be aligned to 4 bytes boundary. Could you double check the packet received by driver?

leobbditestcom commented 2 years ago

Thank you for your correction! I adjusted the driver packet start address to a longword boundary + 2, and the NetX Duo code works fine with no modifications.