Menu

ptpd 2.3.1 alignment error on arm

Help
Jaap
2015-08-17
2015-08-28
  • Jaap

    Jaap - 2015-08-17

    Hi All,

    i'm observing an alignment error in the netInit() code when running on my armv5te machines.
    The code causing that is at line #1106:

        netPath->interfaceAddr = sin->sin_addr;
    

    I changed it to this (ugly code):

        int i;
        char *s = (char *)&sin->sin_addr;
        char *d = (char *)&netPath->interfaceAddr;
        for (i = 0; i < sizeof(netPath->interfaceAddr); ++i) *(d++) = *(s++);
    

    And then it is gone.
    I'm not sure if this is the correct approach, so any help is appriciated!

    Regards,
    Jaap

     
  • Jan Breuer

    Jan Breuer - 2015-08-18

    Hi Jaap,
    it is possible that there will be more alignment errors. Do you have also problems with regular memcpy? So there is a reason for custom for loop.

    memcpy(&netPath->interfaceAddr, &sin->sin_addr, sizeof(netPath->interfaceAddr));
    

    Regards,
    Jan

     
  • Jaap

    Jaap - 2015-08-24

    Hi Jan,
    did not find other alignment errors.
    Your suggested code gives alignment errors.
    Jaap

     
  • Wojciech Owczarek

    Yeah, on some systems this may get tricky. The problem has much to do with the casting that sin comes from.

    Does the memcpy code give you an alignment error in the line with memcpy?

    What happens if in line 1105 you define sin with a __packed prefix?

    The only way I could get armv5te working is via something like qemu. What's the OS you're running Jaap?

    Cheers,
    Wojciech

     

    Last edit: Wojciech Owczarek 2015-08-25
    • Jaap

      Jaap - 2015-08-26

      memcpy is the line with the alignment error

      I will check your suggestion, will come back on that later

      The OS I'm running is openembedded angstrom

      No alignment errors on qemu?

       
      • Wojciech Owczarek

        Hi - I meant "the only way I would be able to test armvte would be to run it on something like qemu". The only ARMs I personally own are first revision Raspberry Pi.

         
  • Jaap

    Jaap - 2015-08-26

    does not help. still alignment errors

    • struct sockaddr_in sin = (struct sockaddr_in)&(netPath->interfaceInfo.afAddress);
    • _attribute_ ((packed)) struct sockaddr_in sin = (struct sockaddr_in)&(netPath->interfaceInfo.afAddress);
     

    Last edit: Jaap 2015-08-26
  • Jan Breuer

    Jan Breuer - 2015-08-26

    What compiler are you using?

    Does this help? In src/dep/datatypes_dep.h

    exchange
    typedef struct {
    to
    typedef struct __attribute__((aligned(4))) {

    for types InterfaceInfo and NetPath

     

    Last edit: Jan Breuer 2015-08-26
  • Jaap

    Jaap - 2015-08-26

    Compiler = 4.3.3

    nope. Still alignment errors...

     
  • Jaap

    Jaap - 2015-08-26

    this seems to work:

    diff -urN a/src/dep/datatypes_dep.h b/src/dep/datatypes_dep.h
    --- a/src/dep/datatypes_dep.h   2015-08-26 12:21:56.012387829 +0200
    +++ b/src/dep/datatypes_dep.h   2015-08-26 12:24:47.256030978 +0200
    @@ -72,7 +72,7 @@
             Boolean hasHwAddress;
             Boolean hasAfAddress;
             unsigned char hwAddress[14];
    -        struct sockaddr afAddress;
    +        __attribute__((aligned(4))) struct sockaddr afAddress;
     } InterfaceInfo;
    
    @@ -87,7 +87,7 @@
        InterfaceInfo interfaceInfo;
    
        /* used by IGMP refresh */
    -   struct in_addr interfaceAddr;
    +   __attribute__((aligned(4))) struct in_addr interfaceAddr;
        /* Typically MAC address - outer 6 octers of ClockIdendity */
        Octet interfaceID[ETHER_ADDR_LEN];
        /* source address of last received packet - used for unicast replies to Delay Requests */
    
     

    Last edit: Jaap 2015-08-26
  • Wojciech Owczarek

    Jaap,

    Just out of curiosity, does it also work if you increase hwAddress array size to 18? :) Because then afAddress should be aligned to 4 bytes.

    cheers,
    Wojciech

     
  • Wojciech Owczarek

    Sorry, not 18, should be 15.

     
  • Jaap

    Jaap - 2015-08-26

    hwAddress[15] works. I removed the other patches (of course...)

     
  • Wojciech Owczarek

    OK. I'm not sure how portable the aligned attribute is, but given that interfaceaddr is the only member being used and cast as a pointer, one alternative is to move it to the top of the structure, then its address will always be aligned.

     
  • Jaap

    Jaap - 2015-08-27

    Yeah, that will probably work.
    For now my problem is solved.
    Thanks for the support!

     
  • Wojciech Owczarek

    Jaap,

    Can you confirm if this does not produce any errors on your platform:

    typedef struct {
            struct sockaddr afAddress;
            int addressFamily;
            Boolean hasHwAddress;
            Boolean hasAfAddress;
            unsigned char hwAddress[14];
            unsigned int flags;
    } InterfaceInfo;
    
     
  • Wojciech Owczarek

    Any chance you can check the latest svn? Finally it looks like this:

    typedef struct {
            struct sockaddr afAddress;
            unsigned char hwAddress[14];
            Boolean hasHwAddress;
            Boolean hasAfAddress;
            int addressFamily;
            unsigned int flags;
    } InterfaceInfo;
    
     

Log in to post a comment.